proofmode.v 8.06 KB
Newer Older
1
From iris.program_logic Require Export weakestpre.
Robbert Krebbers's avatar
Robbert Krebbers committed
2
From iris.proofmode Require Import coq_tactics.
3
From iris.proofmode Require Export tactics.
4
From iris.heap_lang Require Export tactics lifting.
5
Set Default Proof Using "Type".
Robbert Krebbers's avatar
Robbert Krebbers committed
6
7
Import uPred.

8
9
10
11
12
13
(** wp-specific helper tactics *)
Ltac wp_bind_core K :=
  lazymatch eval hnf in K with
  | [] => idtac
  | _ => etrans; [|fast_by apply (wp_bind K)]; simpl
  end.
Robbert Krebbers's avatar
Robbert Krebbers committed
14

15
Ltac wp_value_head := etrans; [|eapply wp_value; apply _]; simpl.
16

17
18
19
Lemma tac_wp_pure `{heapG Σ} K Δ Δ' E e1 e2 φ Φ :
  PureExec φ e1 e2 
  φ 
20
  IntoLaterNEnvs 1 Δ Δ' 
21
22
23
  (Δ'  WP fill K e2 @ E {{ Φ }}) 
  (Δ  WP fill K e1 @ E {{ Φ }}).
Proof.
24
  intros ??? HΔ'. rewrite into_laterN_env_sound /=.
Dan Frumin's avatar
Dan Frumin committed
25
  rewrite -lifting.wp_bind HΔ' -wp_pure_step_later //.
26
  by rewrite -ectx_lifting.wp_ectx_bind_inv.
27
Qed.
28

29
Tactic Notation "wp_pure" open_constr(efoc) :=
30
31
32
  iStartProof;
  lazymatch goal with
  | |- _  wp ?E ?e ?Q => reshape_expr e ltac:(fun K e' =>
33
    unify e' efoc;
34
    eapply (tac_wp_pure K);
35
36
37
    [simpl; apply _                 (* PureExec *)
    |try fast_done                  (* The pure condition for PureExec *)
    |apply _                        (* IntoLaters *)
38
    |simpl_subst; try wp_value_head (* new goal *)])
39
40
   || fail "wp_pure: cannot find" efoc "in" e "or" efoc "is not a reduct"
  | _ => fail "wp_pure: not a 'wp'"
41
42
  end.

43
44
45
46
47
48
49
50
51
52
53
54
Tactic Notation "wp_if" := wp_pure (If _ _ _).
Tactic Notation "wp_if_true" := wp_pure (If (Lit (LitBool true)) _ _).
Tactic Notation "wp_if_false" := wp_pure (If (Lit (LitBool false)) _ _).
Tactic Notation "wp_unop" := wp_pure (UnOp _ _).
Tactic Notation "wp_binop" := wp_pure (BinOp _ _ _).
Tactic Notation "wp_op" := wp_unop || wp_binop.
Tactic Notation "wp_rec" := wp_pure (App _ _).
Tactic Notation "wp_lam" := wp_rec.
Tactic Notation "wp_let" := wp_lam.
Tactic Notation "wp_seq" := wp_lam.
Tactic Notation "wp_proj" := wp_pure (Fst _) || wp_pure (Snd _).
Tactic Notation "wp_case" := wp_pure (Case _ _ _).
55
Tactic Notation "wp_match" := wp_case; wp_let.
56
57
58
59
60
61
62
63
64
65
66
67

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

(** Heap tactics *)
Robbert Krebbers's avatar
Robbert Krebbers committed
68
Section heap.
69
Context `{heapG Σ}.
70
71
72
Implicit Types P Q : iProp Σ.
Implicit Types Φ : val  iProp Σ.
Implicit Types Δ : envs (iResUR Σ).
Robbert Krebbers's avatar
Robbert Krebbers committed
73

74
Lemma tac_wp_alloc Δ Δ' E j K e v Φ :
75
  IntoVal e v 
76
  IntoLaterNEnvs 1 Δ Δ' 
Robbert Krebbers's avatar
Robbert Krebbers committed
77
  ( l,  Δ'',
78
    envs_app false (Esnoc Enil j (l  v)) Δ' = Some Δ'' 
79
80
    (Δ''  WP fill K (Lit (LitLoc l)) @ E {{ Φ }})) 
  Δ  WP fill K (Alloc e) @ E {{ Φ }}.
Robbert Krebbers's avatar
Robbert Krebbers committed
81
Proof.
82
  intros ?? HΔ. rewrite -wp_bind. eapply wand_apply; first exact: wp_alloc.
83
  rewrite left_id into_laterN_env_sound; apply later_mono, forall_intro=> l.
Robbert Krebbers's avatar
Robbert Krebbers committed
84
85
86
87
  destruct (HΔ l) as (Δ''&?&HΔ'). rewrite envs_app_sound //; simpl.
  by rewrite right_id HΔ'.
Qed.

88
Lemma tac_wp_load Δ Δ' E i K l q v Φ :
89
  IntoLaterNEnvs 1 Δ Δ' 
Robbert Krebbers's avatar
Robbert Krebbers committed
90
  envs_lookup i Δ' = Some (false, l {q} v)%I 
91
92
  (Δ'  WP fill K (of_val v) @ E {{ Φ }}) 
  Δ  WP fill K (Load (Lit (LitLoc l))) @ E {{ Φ }}.
Robbert Krebbers's avatar
Robbert Krebbers committed
93
Proof.
94
  intros. rewrite -wp_bind. eapply wand_apply; first exact: wp_load.
95
  rewrite into_laterN_env_sound -later_sep envs_lookup_split //; simpl.
Robbert Krebbers's avatar
Robbert Krebbers committed
96
97
98
  by apply later_mono, sep_mono_r, wand_mono.
Qed.

99
Lemma tac_wp_store Δ Δ' Δ'' E i K l v e v' Φ :
100
  IntoVal e v' 
101
  IntoLaterNEnvs 1 Δ Δ' 
Robbert Krebbers's avatar
Robbert Krebbers committed
102
103
  envs_lookup i Δ' = Some (false, l  v)%I 
  envs_simple_replace i false (Esnoc Enil i (l  v')) Δ' = Some Δ'' 
104
105
  (Δ''  WP fill K (Lit LitUnit) @ E {{ Φ }}) 
  Δ  WP fill K (Store (Lit (LitLoc l)) e) @ E {{ Φ }}.
Robbert Krebbers's avatar
Robbert Krebbers committed
106
Proof.
107
  intros. rewrite -wp_bind. eapply wand_apply; first by eapply wp_store.
108
  rewrite into_laterN_env_sound -later_sep envs_simple_replace_sound //; simpl.
Robbert Krebbers's avatar
Robbert Krebbers committed
109
110
111
  rewrite right_id. by apply later_mono, sep_mono_r, wand_mono.
Qed.

112
Lemma tac_wp_cas_fail Δ Δ' E i K l q v e1 v1 e2 v2 Φ :
113
  IntoVal e1 v1  IntoVal e2 v2 
114
  IntoLaterNEnvs 1 Δ Δ' 
Robbert Krebbers's avatar
Robbert Krebbers committed
115
  envs_lookup i Δ' = Some (false, l {q} v)%I  v  v1 
116
117
  (Δ'  WP fill K (Lit (LitBool false)) @ E {{ Φ }}) 
  Δ  WP fill K (CAS (Lit (LitLoc l)) e1 e2) @ E {{ Φ }}.
Robbert Krebbers's avatar
Robbert Krebbers committed
118
Proof.
119
  intros. rewrite -wp_bind. eapply wand_apply; first exact: wp_cas_fail.
120
  rewrite into_laterN_env_sound -later_sep envs_lookup_split //; simpl.
Robbert Krebbers's avatar
Robbert Krebbers committed
121
122
123
  by apply later_mono, sep_mono_r, wand_mono.
Qed.

124
Lemma tac_wp_cas_suc Δ Δ' Δ'' E i K l v e1 v1 e2 v2 Φ :
125
  IntoVal e1 v1  IntoVal e2 v2 
126
  IntoLaterNEnvs 1 Δ Δ' 
127
  envs_lookup i Δ' = Some (false, l  v)%I  v = v1 
Robbert Krebbers's avatar
Robbert Krebbers committed
128
  envs_simple_replace i false (Esnoc Enil i (l  v2)) Δ' = Some Δ'' 
129
130
  (Δ''  WP fill K (Lit (LitBool true)) @ E {{ Φ }}) 
  Δ  WP fill K (CAS (Lit (LitLoc l)) e1 e2) @ E {{ Φ }}.
Robbert Krebbers's avatar
Robbert Krebbers committed
131
Proof.
132
  intros; subst. rewrite -wp_bind. eapply wand_apply; first exact: wp_cas_suc.
133
  rewrite into_laterN_env_sound -later_sep envs_simple_replace_sound //; simpl.
Robbert Krebbers's avatar
Robbert Krebbers committed
134
135
136
137
138
  rewrite right_id. by apply later_mono, sep_mono_r, wand_mono.
Qed.
End heap.

Tactic Notation "wp_apply" open_constr(lem) :=
139
140
141
142
143
144
145
146
147
148
  iPoseProofCore lem as false true (fun H =>
    lazymatch goal with
    | |- _  wp ?E ?e ?Q =>
      reshape_expr e ltac:(fun K e' =>
        wp_bind_core K; iApplyHyp H; try iNext; simpl) ||
      lazymatch iTypeOf H with
      | Some (_,?P) => fail "wp_apply: cannot apply" P
      end
    | _ => fail "wp_apply: not a 'wp'"
    end).
Robbert Krebbers's avatar
Robbert Krebbers committed
149
150

Tactic Notation "wp_alloc" ident(l) "as" constr(H) :=
151
  iStartProof;
152
  lazymatch goal with
153
154
  | |- _  wp ?E ?e ?Q =>
    first
155
156
      [reshape_expr e ltac:(fun K e' =>
         eapply (tac_wp_alloc _ _ _ H K); [apply _|..])
157
      |fail 1 "wp_alloc: cannot find 'Alloc' in" e];
158
    [apply _
159
160
161
162
    |first [intros l | fail 1 "wp_alloc:" l "not fresh"];
      eexists; split;
        [env_cbv; reflexivity || fail "wp_alloc:" H "not fresh"
        |simpl; try wp_value_head]]
163
  | _ => fail "wp_alloc: not a 'wp'"
Robbert Krebbers's avatar
Robbert Krebbers committed
164
  end.
165

166
167
Tactic Notation "wp_alloc" ident(l) :=
  let H := iFresh in wp_alloc l as H.
Robbert Krebbers's avatar
Robbert Krebbers committed
168
169

Tactic Notation "wp_load" :=
170
  iStartProof;
171
  lazymatch goal with
172
173
  | |- _  wp ?E ?e ?Q =>
    first
174
      [reshape_expr e ltac:(fun K e' => eapply (tac_wp_load _ _ _ _ K))
175
      |fail 1 "wp_load: cannot find 'Load' in" e];
176
177
178
179
    [apply _
    |let l := match goal with |- _ = Some (_, (?l {_} _)%I) => l end in
     iAssumptionCore || fail "wp_load: cannot find" l "↦ ?"
    |simpl; try wp_value_head]
180
  | _ => fail "wp_load: not a 'wp'"
Robbert Krebbers's avatar
Robbert Krebbers committed
181
182
183
  end.

Tactic Notation "wp_store" :=
184
  iStartProof;
185
  lazymatch goal with
186
187
  | |- _  wp ?E ?e ?Q =>
    first
188
189
      [reshape_expr e ltac:(fun K e' =>
         eapply (tac_wp_store _ _ _ _ _ K); [apply _|..])
190
      |fail 1 "wp_store: cannot find 'Store' in" e];
191
    [apply _
192
193
194
195
    |let l := match goal with |- _ = Some (_, (?l {_} _)%I) => l end in
     iAssumptionCore || fail "wp_store: cannot find" l "↦ ?"
    |env_cbv; reflexivity
    |simpl; try first [wp_pure (Seq (Lit LitUnit) _)|wp_value_head]]
196
  | _ => fail "wp_store: not a 'wp'"
Robbert Krebbers's avatar
Robbert Krebbers committed
197
198
199
  end.

Tactic Notation "wp_cas_fail" :=
200
  iStartProof;
201
  lazymatch goal with
202
203
  | |- _  wp ?E ?e ?Q =>
    first
204
205
      [reshape_expr e ltac:(fun K e' =>
         eapply (tac_wp_cas_fail _ _ _ _ K); [apply _|apply _|..])
206
      |fail 1 "wp_cas_fail: cannot find 'CAS' in" e];
207
    [apply _
208
209
210
211
    |let l := match goal with |- _ = Some (_, (?l {_} _)%I) => l end in
     iAssumptionCore || fail "wp_cas_fail: cannot find" l "↦ ?"
    |try congruence
    |simpl; try wp_value_head]
212
  | _ => fail "wp_cas_fail: not a 'wp'"
Robbert Krebbers's avatar
Robbert Krebbers committed
213
214
215
  end.

Tactic Notation "wp_cas_suc" :=
216
  iStartProof;
217
  lazymatch goal with
218
219
  | |- _  wp ?E ?e ?Q =>
    first
220
221
      [reshape_expr e ltac:(fun K e' =>
         eapply (tac_wp_cas_suc _ _ _ _ _ K); [apply _|apply _|..])
222
      |fail 1 "wp_cas_suc: cannot find 'CAS' in" e];
223
    [apply _
224
225
226
227
228
    |let l := match goal with |- _ = Some (_, (?l {_} _)%I) => l end in
     iAssumptionCore || fail "wp_cas_suc: cannot find" l "↦ ?"
    |try congruence
    |env_cbv; reflexivity
    |simpl; try wp_value_head]
229
  | _ => fail "wp_cas_suc: not a 'wp'"
Robbert Krebbers's avatar
Robbert Krebbers committed
230
  end.