wp_tactics.v 4.42 KB
Newer Older
1
From iris.algebra Require Export upred_tactics.
2
From iris.heap_lang Require Export tactics derived.
3
4
Import uPred.

5
(** wp-specific helper tactics *)
6
7
8
Ltac wp_bind K :=
  lazymatch eval hnf in K with
  | [] => idtac
9
  | _ => etrans; [|fast_by apply (wp_bind K)]; simpl
10
  end.
11

12
(* Solves side-conditions generated by the wp tactics *)
Robbert Krebbers's avatar
Robbert Krebbers committed
13
Ltac wp_done :=
14
15
  match goal with
  | |- Closed _ _ => solve_closed
16
  | |- is_Some (to_val _) => solve_to_val
17
18
19
20
  | |- to_val _ = Some _ => solve_to_val
  | |- language.to_val _ = Some _ => solve_to_val
  | _ => fast_done
  end.
21

22
23
24
25
26
27
28
(* sometimes, we will have to do a final view shift, so only apply
pvs_intro if we obtain a consecutive wp *)
Ltac wp_strip_pvs :=
  lazymatch goal with
  | |- _  |={?E}=> _ =>
    etrans; [|apply pvs_intro];
    match goal with |- _  wp E _ _ => simpl | _ => fail end
Robbert Krebbers's avatar
Robbert Krebbers committed
29
30
  end.

31
32
Ltac wp_value_head := etrans; [|eapply wp_value_pvs; wp_done]; lazy beta.

33
34
Ltac wp_strip_later := idtac. (* a hook to be redefined later *)

35
36
Ltac wp_seq_head :=
  lazymatch goal with
37
38
  | |- _  wp ?E (Seq _ _) ?Q =>
    etrans; [|eapply wp_seq; wp_done]; wp_strip_later
39
40
  end.

Robbert Krebbers's avatar
Robbert Krebbers committed
41
Ltac wp_finish := intros_revert ltac:(
42
43
44
45
46
47
48
49
  rewrite /= ?to_of_val;
  try wp_strip_later;
  repeat lazymatch goal with
  | |- _  wp ?E (Seq _ _) ?Q =>
     etrans; [|eapply wp_seq; wp_done]; wp_strip_later
  | |- _  wp ?E _ ?Q => wp_value_head
  | |- _  |={_}=> _ => wp_strip_pvs
  end).
Robbert Krebbers's avatar
Robbert Krebbers committed
50
51

Tactic Notation "wp_value" :=
52
  lazymatch goal with
Robbert Krebbers's avatar
Robbert Krebbers committed
53
  | |- _  wp ?E ?e ?Q => reshape_expr e ltac:(fun K e' =>
54
55
    wp_bind K; wp_value_head) || fail "wp_value: cannot find value in" e
  | _ => fail "wp_value: not a wp"
Robbert Krebbers's avatar
Robbert Krebbers committed
56
57
  end.

58
Tactic Notation "wp_rec" :=
Robbert Krebbers's avatar
Robbert Krebbers committed
59
60
61
  lazymatch goal with
  | |- _  wp ?E ?e ?Q => reshape_expr e ltac:(fun K e' =>
    match eval hnf in e' with App ?e1 _ =>
62
63
(* hnf does not reduce through an of_val *)
(*      match eval hnf in e1 with Rec _ _ _ => *)
64
    wp_bind K; etrans; [|eapply wp_rec; wp_done]; simpl_subst; wp_finish
65
66
67
(*      end *) end) || fail "wp_rec: cannot find 'Rec' in" e
  | _ => fail "wp_rec: not a 'wp'"
  end.
68

69
Tactic Notation "wp_lam" :=
70
  lazymatch goal with
71
  | |- _  wp ?E ?e ?Q => reshape_expr e ltac:(fun K e' =>
72
73
    match eval hnf in e' with App ?e1 _ =>
(*    match eval hnf in e1 with Rec BAnon _ _ => *)
74
    wp_bind K; etrans; [|eapply wp_lam; wp_done]; simpl_subst; wp_finish
75
76
(*    end *) end) || fail "wp_lam: cannot find 'Lam' in" e
  | _ => fail "wp_lam: not a 'wp'"
77
78
79
80
81
  end.

Tactic Notation "wp_let" := wp_lam.
Tactic Notation "wp_seq" := wp_let.

82
Tactic Notation "wp_op" :=
83
  lazymatch goal with
84
  | |- _  wp ?E ?e ?Q => reshape_expr e ltac:(fun K e' =>
Ralf Jung's avatar
Ralf Jung committed
85
    lazymatch eval hnf in e' with
86
87
88
89
    | BinOp LtOp _ _ => wp_bind K; apply wp_lt; wp_finish
    | BinOp LeOp _ _ => wp_bind K; apply wp_le; wp_finish
    | BinOp EqOp _ _ => wp_bind K; apply wp_eq; wp_finish
    | BinOp _ _ _ =>
90
       wp_bind K; etrans; [|eapply wp_bin_op; try fast_done]; wp_finish
91
    | UnOp _ _ =>
92
       wp_bind K; etrans; [|eapply wp_un_op; try fast_done]; wp_finish
93
94
    end) || fail "wp_op: cannot find 'BinOp' or 'UnOp' in" e
  | _ => fail "wp_op: not a 'wp'"
95
  end.
96

97
Tactic Notation "wp_proj" :=
98
  lazymatch goal with
99
  | |- _  wp ?E ?e ?Q => reshape_expr e ltac:(fun K e' =>
100
    match eval hnf in e' with
101
102
    | Fst _ => wp_bind K; etrans; [|eapply wp_fst; wp_done]; wp_finish
    | Snd _ => wp_bind K; etrans; [|eapply wp_snd; wp_done]; wp_finish
103
104
    end) || fail "wp_proj: cannot find 'Fst' or 'Snd' in" e
  | _ => fail "wp_proj: not a 'wp'"
105
106
  end.

107
Tactic Notation "wp_if" :=
108
  lazymatch goal with
109
  | |- _  wp ?E ?e ?Q => reshape_expr e ltac:(fun K e' =>
110
111
112
113
114
115
    match eval hnf in e' with
    | If _ _ _ =>
      wp_bind K;
      etrans; [|eapply wp_if_true || eapply wp_if_false]; wp_finish
    end) || fail "wp_if: cannot find 'If' in" e
  | _ => fail "wp_if: not a 'wp'"
116
  end.
117

118
Tactic Notation "wp_match" :=
119
  lazymatch goal with
120
  | |- _  wp ?E ?e ?Q => reshape_expr e ltac:(fun K e' =>
121
122
    match eval hnf in e' with
    | Case _ _ _ =>
123
      wp_bind K;
124
125
126
127
      etrans; [|first[eapply wp_match_inl; wp_done|eapply wp_match_inr; wp_done]];
      simpl_subst; wp_finish
    end) || fail "wp_match: cannot find 'Match' in" e
  | _ => fail "wp_match: not a 'wp'"
128
129
  end.

130
Tactic Notation "wp_focus" open_constr(efoc) :=
131
  lazymatch goal with
132
  | |- _  wp ?E ?e ?Q => reshape_expr e ltac:(fun K e' =>
133
134
135
136
    match e' with
    | efoc => unify e' efoc; wp_bind K
    end) || fail "wp_focus: cannot find" efoc "in" e
  | _ => fail "wp_focus: not a 'wp'"
137
  end.