proofmode.v 9.33 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
Lemma tac_wp_expr_eval `{heapG Σ} Δ s E Φ e e' :
9
  e = e' 
10
  envs_entails Δ (WP e' @ s; E {{ Φ }})  envs_entails Δ (WP e @ s; E {{ Φ }}).
11
12
Proof. by intros ->. Qed.

13
Ltac wp_expr_eval t :=
14
  try iStartProof;
15
  try (eapply tac_wp_expr_eval; [t; reflexivity|]).
16

17
18
Ltac wp_expr_simpl := wp_expr_eval simpl.
Ltac wp_expr_simpl_subst := wp_expr_eval simpl_subst.
19

20
Lemma tac_wp_pure `{heapG Σ} Δ Δ' s E e1 e2 φ Φ :
21
22
  PureExec φ e1 e2 
  φ 
23
  IntoLaterNEnvs 1 Δ Δ' 
24
25
  envs_entails Δ' (WP e2 @ s; E {{ Φ }}) 
  envs_entails Δ (WP e1 @ s; E {{ Φ }}).
26
Proof.
27
  rewrite /envs_entails=> ??? HΔ'. rewrite into_laterN_env_sound /=.
28
  rewrite HΔ' -wp_pure_step_later //.
29
Qed.
30

31
Lemma tac_wp_value `{heapG Σ} Δ s E Φ e v :
32
  IntoVal e v 
33
  envs_entails Δ (Φ v)  envs_entails Δ (WP e @ s; E {{ Φ }}).
34
35
36
Proof. rewrite /envs_entails=> ? ->. by apply wp_value. Qed.

Ltac wp_value_head := eapply tac_wp_value; [apply _|lazy beta].
Robbert Krebbers's avatar
Robbert Krebbers committed
37

38
Tactic Notation "wp_pure" open_constr(efoc) :=
39
40
  iStartProof;
  lazymatch goal with
41
  | |- envs_entails _ (wp ?s ?E ?e ?Q) =>
Robbert Krebbers's avatar
Robbert Krebbers committed
42
43
44
    let e := eval simpl in e in
    reshape_expr e ltac:(fun K e' =>
      unify e' efoc;
45
      eapply (tac_wp_pure _ _ _ _ (fill K e'));
Robbert Krebbers's avatar
Robbert Krebbers committed
46
47
48
      [apply _                        (* PureExec *)
      |try fast_done                  (* The pure condition for PureExec *)
      |apply _                        (* IntoLaters *)
49
      |wp_expr_simpl_subst; try wp_value_head (* new goal *)
Robbert Krebbers's avatar
Robbert Krebbers committed
50
      ])
51
52
   || fail "wp_pure: cannot find" efoc "in" e "or" efoc "is not a reduct"
  | _ => fail "wp_pure: not a 'wp'"
53
54
  end.

55
56
57
58
59
60
61
62
63
64
65
66
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 _ _ _).
67
Tactic Notation "wp_match" := wp_case; wp_let.
68

Ralf Jung's avatar
Ralf Jung committed
69
Lemma tac_wp_bind `{heapG Σ} K Δ s E Φ e f :
70
  f = (λ e, fill K e)  (* as an eta expanded hypothesis so that we can `simpl` it *)
Ralf Jung's avatar
Ralf Jung committed
71
72
  envs_entails Δ (WP e @ s; E {{ v, WP f (of_val v) @ s; E {{ Φ }} }})%I 
  envs_entails Δ (WP fill K e @ s; E {{ Φ }}).
73
Proof. rewrite /envs_entails=> -> ->. by apply: wp_bind. Qed.
74

Robbert Krebbers's avatar
Robbert Krebbers committed
75
76
77
Ltac wp_bind_core K :=
  lazymatch eval hnf in K with
  | [] => idtac
78
  | _ => eapply (tac_wp_bind K); [simpl; reflexivity|lazy beta]
Robbert Krebbers's avatar
Robbert Krebbers committed
79
80
  end.

81
82
83
Tactic Notation "wp_bind" open_constr(efoc) :=
  iStartProof;
  lazymatch goal with
84
  | |- envs_entails _ (wp ?s ?E ?e ?Q) =>
85
86
    reshape_expr e ltac:(fun K e' => unify e' efoc; wp_bind_core K)
    || fail "wp_bind: cannot find" efoc "in" e
87
88
89
90
  | _ => fail "wp_bind: not a 'wp'"
  end.

(** Heap tactics *)
Robbert Krebbers's avatar
Robbert Krebbers committed
91
Section heap.
92
Context `{heapG Σ}.
93
94
95
Implicit Types P Q : iProp Σ.
Implicit Types Φ : val  iProp Σ.
Implicit Types Δ : envs (iResUR Σ).
Robbert Krebbers's avatar
Robbert Krebbers committed
96

97
Lemma tac_wp_alloc Δ Δ' s E j K e v Φ :
98
  IntoVal e v 
99
  IntoLaterNEnvs 1 Δ Δ' 
Robbert Krebbers's avatar
Robbert Krebbers committed
100
  ( l,  Δ'',
101
    envs_app false (Esnoc Enil j (l  v)) Δ' = Some Δ'' 
102
103
    envs_entails Δ'' (WP fill K (Lit (LitLoc l)) @ s; E {{ Φ }})) 
  envs_entails Δ (WP fill K (Alloc e) @ s; E {{ Φ }}).
Robbert Krebbers's avatar
Robbert Krebbers committed
104
Proof.
105
106
  rewrite /envs_entails=> ?? HΔ.
  rewrite -wp_bind. eapply wand_apply; first exact: wp_alloc.
107
  rewrite left_id into_laterN_env_sound; apply later_mono, forall_intro=> l.
Robbert Krebbers's avatar
Robbert Krebbers committed
108
109
110
111
  destruct (HΔ l) as (Δ''&?&HΔ'). rewrite envs_app_sound //; simpl.
  by rewrite right_id HΔ'.
Qed.

112
Lemma tac_wp_load Δ Δ' s E i K l q v Φ :
113
  IntoLaterNEnvs 1 Δ Δ' 
Robbert Krebbers's avatar
Robbert Krebbers committed
114
  envs_lookup i Δ' = Some (false, l {q} v)%I 
115
116
  envs_entails Δ' (WP fill K (of_val v) @ s; E {{ Φ }}) 
  envs_entails Δ (WP fill K (Load (Lit (LitLoc l))) @ s; E {{ Φ }}).
Robbert Krebbers's avatar
Robbert Krebbers committed
117
Proof.
118
119
  rewrite /envs_entails=> ???.
  rewrite -wp_bind. eapply wand_apply; first exact: wp_load.
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_store Δ Δ' Δ'' s E i K l v e v' Φ :
125
  IntoVal e v' 
126
  IntoLaterNEnvs 1 Δ Δ' 
Robbert Krebbers's avatar
Robbert Krebbers committed
127
128
  envs_lookup i Δ' = Some (false, l  v)%I 
  envs_simple_replace i false (Esnoc Enil i (l  v')) Δ' = Some Δ'' 
129
130
  envs_entails Δ'' (WP fill K (Lit LitUnit) @ s; E {{ Φ }}) 
  envs_entails Δ (WP fill K (Store (Lit (LitLoc l)) e) @ s; E {{ Φ }}).
Robbert Krebbers's avatar
Robbert Krebbers committed
131
Proof.
132
133
  rewrite /envs_entails=> ?????.
  rewrite -wp_bind. eapply wand_apply; first by eapply wp_store.
134
  rewrite into_laterN_env_sound -later_sep envs_simple_replace_sound //; simpl.
Robbert Krebbers's avatar
Robbert Krebbers committed
135
136
137
  rewrite right_id. by apply later_mono, sep_mono_r, wand_mono.
Qed.

138
Lemma tac_wp_cas_fail Δ Δ' s E i K l q v e1 v1 e2 Φ :
Robbert Krebbers's avatar
Robbert Krebbers committed
139
  IntoVal e1 v1  AsVal e2 
140
  IntoLaterNEnvs 1 Δ Δ' 
Robbert Krebbers's avatar
Robbert Krebbers committed
141
  envs_lookup i Δ' = Some (false, l {q} v)%I  v  v1 
142
143
  envs_entails Δ' (WP fill K (Lit (LitBool false)) @ s; E {{ Φ }}) 
  envs_entails Δ (WP fill K (CAS (Lit (LitLoc l)) e1 e2) @ s; E {{ Φ }}).
Robbert Krebbers's avatar
Robbert Krebbers committed
144
Proof.
145
146
  rewrite /envs_entails=> ??????.
  rewrite -wp_bind. eapply wand_apply; first exact: wp_cas_fail.
147
  rewrite into_laterN_env_sound -later_sep envs_lookup_split //; simpl.
Robbert Krebbers's avatar
Robbert Krebbers committed
148
149
150
  by apply later_mono, sep_mono_r, wand_mono.
Qed.

151
Lemma tac_wp_cas_suc Δ Δ' Δ'' s E i K l v e1 v1 e2 v2 Φ :
152
  IntoVal e1 v1  IntoVal e2 v2 
153
  IntoLaterNEnvs 1 Δ Δ' 
154
  envs_lookup i Δ' = Some (false, l  v)%I  v = v1 
Robbert Krebbers's avatar
Robbert Krebbers committed
155
  envs_simple_replace i false (Esnoc Enil i (l  v2)) Δ' = Some Δ'' 
156
157
  envs_entails Δ'' (WP fill K (Lit (LitBool true)) @ s; E {{ Φ }}) 
  envs_entails Δ (WP fill K (CAS (Lit (LitLoc l)) e1 e2) @ s; E {{ Φ }}).
Robbert Krebbers's avatar
Robbert Krebbers committed
158
Proof.
159
160
  rewrite /envs_entails=> ???????; subst.
  rewrite -wp_bind. eapply wand_apply; first exact: wp_cas_suc.
161
  rewrite into_laterN_env_sound -later_sep envs_simple_replace_sound //; simpl.
Robbert Krebbers's avatar
Robbert Krebbers committed
162
163
164
165
166
  rewrite right_id. by apply later_mono, sep_mono_r, wand_mono.
Qed.
End heap.

Tactic Notation "wp_apply" open_constr(lem) :=
167
168
  iPoseProofCore lem as false true (fun H =>
    lazymatch goal with
169
    | |- envs_entails _ (wp ?s ?E ?e ?Q) =>
170
      reshape_expr e ltac:(fun K e' =>
171
        wp_bind_core K; iApplyHyp H; try iNext; wp_expr_simpl) ||
172
173
174
175
176
      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
177
178

Tactic Notation "wp_alloc" ident(l) "as" constr(H) :=
179
  iStartProof;
180
  lazymatch goal with
181
  | |- envs_entails _ (wp ?s ?E ?e ?Q) =>
182
    first
183
      [reshape_expr e ltac:(fun K e' =>
184
         eapply (tac_wp_alloc _ _ _ _ H K); [apply _|..])
185
      |fail 1 "wp_alloc: cannot find 'Alloc' in" e];
186
    [apply _
187
188
189
    |first [intros l | fail 1 "wp_alloc:" l "not fresh"];
      eexists; split;
        [env_cbv; reflexivity || fail "wp_alloc:" H "not fresh"
190
        |wp_expr_simpl; try wp_value_head]]
191
  | _ => fail "wp_alloc: not a 'wp'"
Robbert Krebbers's avatar
Robbert Krebbers committed
192
  end.
193

194
195
Tactic Notation "wp_alloc" ident(l) :=
  let H := iFresh in wp_alloc l as H.
Robbert Krebbers's avatar
Robbert Krebbers committed
196
197

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

Tactic Notation "wp_store" :=
212
  iStartProof;
213
  lazymatch goal with
214
  | |- envs_entails _ (wp ?s ?E ?e ?Q) =>
215
    first
216
      [reshape_expr e ltac:(fun K e' =>
217
         eapply (tac_wp_store _ _ _ _ _ _ K); [apply _|..])
218
      |fail 1 "wp_store: cannot find 'Store' in" e];
219
    [apply _
220
221
222
    |let l := match goal with |- _ = Some (_, (?l {_} _)%I) => l end in
     iAssumptionCore || fail "wp_store: cannot find" l "↦ ?"
    |env_cbv; reflexivity
223
    |wp_expr_simpl; try first [wp_pure (Seq (Lit LitUnit) _)|wp_value_head]]
224
  | _ => fail "wp_store: not a 'wp'"
Robbert Krebbers's avatar
Robbert Krebbers committed
225
226
227
  end.

Tactic Notation "wp_cas_fail" :=
228
  iStartProof;
229
  lazymatch goal with
230
  | |- envs_entails _ (wp ?s ?E ?e ?Q) =>
231
    first
232
      [reshape_expr e ltac:(fun K e' =>
233
         eapply (tac_wp_cas_fail _ _ _ _ _ K); [apply _|apply _|..])
234
      |fail 1 "wp_cas_fail: cannot find 'CAS' in" e];
235
    [apply _
236
237
238
    |let l := match goal with |- _ = Some (_, (?l {_} _)%I) => l end in
     iAssumptionCore || fail "wp_cas_fail: cannot find" l "↦ ?"
    |try congruence
239
    |wp_expr_simpl; try wp_value_head]
240
  | _ => fail "wp_cas_fail: not a 'wp'"
Robbert Krebbers's avatar
Robbert Krebbers committed
241
242
243
  end.

Tactic Notation "wp_cas_suc" :=
244
  iStartProof;
245
  lazymatch goal with
246
  | |- envs_entails _ (wp ?s ?E ?e ?Q) =>
247
    first
248
      [reshape_expr e ltac:(fun K e' =>
249
         eapply (tac_wp_cas_suc _ _ _ _ _ _ K); [apply _|apply _|..])
250
      |fail 1 "wp_cas_suc: cannot find 'CAS' in" e];
251
    [apply _
252
253
254
255
    |let l := match goal with |- _ = Some (_, (?l {_} _)%I) => l end in
     iAssumptionCore || fail "wp_cas_suc: cannot find" l "↦ ?"
    |try congruence
    |env_cbv; reflexivity
256
    |wp_expr_simpl; try wp_value_head]
257
  | _ => fail "wp_cas_suc: not a 'wp'"
Robbert Krebbers's avatar
Robbert Krebbers committed
258
  end.