wp_tactics.v 4.24 KB
Newer Older
1
From iris.heap_lang Require Export tactics derived.
2
3
Import uPred.

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

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

21
Ltac wp_value_head := etrans; [|eapply wp_value; wp_done]; lazy beta.
22

23
24
Ltac wp_strip_later := idtac. (* a hook to be redefined later *)

25
26
Ltac wp_seq_head :=
  lazymatch goal with
27
28
  | |- _  wp ?E (Seq _ _) ?Q =>
    etrans; [|eapply wp_seq; wp_done]; wp_strip_later
29
30
  end.

Robbert Krebbers's avatar
Robbert Krebbers committed
31
Ltac wp_finish := intros_revert ltac:(
32
33
34
35
36
37
38
  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
  end).
Robbert Krebbers's avatar
Robbert Krebbers committed
39
40

Tactic Notation "wp_value" :=
41
  lazymatch goal with
Robbert Krebbers's avatar
Robbert Krebbers committed
42
  | |- _  wp ?E ?e ?Q => reshape_expr e ltac:(fun K e' =>
43
    wp_bind_core K; wp_value_head) || fail "wp_value: cannot find value in" e
44
  | _ => fail "wp_value: not a wp"
Robbert Krebbers's avatar
Robbert Krebbers committed
45
46
  end.

47
Tactic Notation "wp_rec" :=
Robbert Krebbers's avatar
Robbert Krebbers committed
48
49
50
  lazymatch goal with
  | |- _  wp ?E ?e ?Q => reshape_expr e ltac:(fun K e' =>
    match eval hnf in e' with App ?e1 _ =>
51
52
(* hnf does not reduce through an of_val *)
(*      match eval hnf in e1 with Rec _ _ _ => *)
53
    wp_bind_core K; etrans; [|(eapply wp_rec; wp_done) || (eapply wp_rec_locked; wp_done)]; simpl_subst; wp_finish
54
55
56
(*      end *) end) || fail "wp_rec: cannot find 'Rec' in" e
  | _ => fail "wp_rec: not a 'wp'"
  end.
57

58
Tactic Notation "wp_lam" :=
59
  lazymatch goal with
60
  | |- _  wp ?E ?e ?Q => reshape_expr e ltac:(fun K e' =>
61
62
    match eval hnf in e' with App ?e1 _ =>
(*    match eval hnf in e1 with Rec BAnon _ _ => *)
63
    wp_bind_core K; etrans; [|eapply wp_lam; wp_done]; simpl_subst; wp_finish
64
65
(*    end *) end) || fail "wp_lam: cannot find 'Lam' in" e
  | _ => fail "wp_lam: not a 'wp'"
66
67
68
69
70
  end.

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

71
Tactic Notation "wp_op" :=
72
  lazymatch goal with
73
  | |- _  wp ?E ?e ?Q => reshape_expr e ltac:(fun K e' =>
Ralf Jung's avatar
Ralf Jung committed
74
    lazymatch eval hnf in e' with
75
76
    | BinOp LtOp _ _ => wp_bind_core K; apply wp_lt; wp_finish
    | BinOp LeOp _ _ => wp_bind_core K; apply wp_le; wp_finish
77
78
    | BinOp EqOp _ _ =>
       wp_bind_core K; eapply wp_eq; [wp_done|wp_done|wp_finish|wp_finish]
79
    | BinOp _ _ _ =>
80
81
       wp_bind_core K; etrans;
         [|eapply wp_bin_op; [wp_done|wp_done|try fast_done]]; wp_finish
82
    | UnOp _ _ =>
83
84
       wp_bind_core K; etrans;
         [|eapply wp_un_op; [wp_done|try fast_done]]; wp_finish
85
86
    end) || fail "wp_op: cannot find 'BinOp' or 'UnOp' in" e
  | _ => fail "wp_op: not a 'wp'"
87
  end.
88

89
Tactic Notation "wp_proj" :=
90
  lazymatch goal with
91
  | |- _  wp ?E ?e ?Q => reshape_expr e ltac:(fun K e' =>
92
    match eval hnf in e' with
93
94
    | Fst _ => wp_bind_core K; etrans; [|eapply wp_fst; wp_done]; wp_finish
    | Snd _ => wp_bind_core K; etrans; [|eapply wp_snd; wp_done]; wp_finish
95
96
    end) || fail "wp_proj: cannot find 'Fst' or 'Snd' in" e
  | _ => fail "wp_proj: not a 'wp'"
97
98
  end.

99
Tactic Notation "wp_if" :=
100
  lazymatch goal with
101
  | |- _  wp ?E ?e ?Q => reshape_expr e ltac:(fun K e' =>
102
103
    match eval hnf in e' with
    | If _ _ _ =>
104
      wp_bind_core K;
105
106
107
      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'"
108
  end.
109

110
Tactic Notation "wp_match" :=
111
  lazymatch goal with
112
  | |- _  wp ?E ?e ?Q => reshape_expr e ltac:(fun K e' =>
113
114
    match eval hnf in e' with
    | Case _ _ _ =>
115
      wp_bind_core K;
116
117
118
119
      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'"
120
121
  end.

122
Tactic Notation "wp_bind" open_constr(efoc) :=
123
  lazymatch goal with
124
  | |- _  wp ?E ?e ?Q => reshape_expr e ltac:(fun K e' =>
125
    match e' with
126
127
128
    | efoc => unify e' efoc; wp_bind_core K
    end) || fail "wp_bind: cannot find" efoc "in" e
  | _ => fail "wp_bind: not a 'wp'"
129
  end.