lifting.v 12.3 KB
Newer Older
Ralf Jung's avatar
Ralf Jung committed
1
Require Import prelude.gmap iris.lifting.
2
Require Export iris.weakestpre barrier.heap_lang.
3
4
Import uPred.

5
Section lifting.
6
7
8
Context {Σ : iFunctor}.
Implicit Types P : iProp heap_lang Σ.
Implicit Types Q : val heap_lang  iProp heap_lang Σ.
Ralf Jung's avatar
Ralf Jung committed
9
10

(** Bind. *)
11
Lemma wp_bind {E e} K Q :
12
13
  wp E e (λ v, wp E (fill K (v2e v)) Q)  wp E (fill K e) Q.
Proof. apply (wp_bind (K:=fill K)), fill_is_ctx. Qed.
Ralf Jung's avatar
Ralf Jung committed
14

15
(** Base axioms for core primitives of the language: Stateful reductions. *)
16
17
18
19
20

Lemma wp_lift_step E1 E2 (φ : expr  state  Prop) Q e1 σ1 :
  E1  E2  to_val e1 = None 
  reducible e1 σ1 
  ( e2 σ2 ef, prim_step e1 σ1 e2 σ2 ef  ef = None  φ e2 σ2) 
21
22
23
  pvs E2 E1 (ownP σ1    e2 σ2, ( φ e2 σ2  ownP σ2) -
    pvs E1 E2 (wp E2 e2 Q))
   wp E2 e1 Q.
24
25
Proof.
  intros ? He Hsafe Hstep.
26
  (* RJ: working around https://coq.inria.fr/bugs/show_bug.cgi?id=4536 *)
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
  etransitivity; last eapply wp_lift_step with (σ2 := σ1)
    (φ0 := λ e' σ' ef, ef = None  φ e' σ'); last first.
  - intros e2 σ2 ef Hstep'%prim_ectx_step; last done.
    by apply Hstep.
  - destruct Hsafe as (e' & σ' & ? & ?).
    do 3 eexists. exists EmptyCtx. do 2 eexists.
    split_ands; try (by rewrite fill_empty); eassumption.
  - done.
  - eassumption.
  - apply pvs_mono. apply sep_mono; first done.
    apply later_mono. apply forall_mono=>e2. apply forall_mono=>σ2.
    apply forall_intro=>ef. apply wand_intro_l.
    rewrite always_and_sep_l' -associative -always_and_sep_l'.
    apply const_elim_l; move=>[-> Hφ]. eapply const_intro_l; first eexact Hφ.
    rewrite always_and_sep_l' associative -always_and_sep_l' wand_elim_r.
    apply pvs_mono. rewrite right_id. done.
Qed.
44
45
46

(* TODO RJ: Figure out some better way to make the
   postcondition a predicate over a *location* *)
47
Lemma wp_alloc_pst E σ e v Q :
48
  e2v e = Some v 
49
50
  (ownP σ  ( l, (σ !! l = None)  ownP (<[l:=v]>σ) - Q (LocV l)))
        wp E (Alloc e) Q.
51
Proof.
52
  (* RJ FIXME (also for most other lemmas in this file): rewrite would be nicer... *)
53
  intros Hvl. etransitivity; last eapply wp_lift_step with (σ1 := σ)
54
    (φ := λ e' σ',  l, e' = Loc l  σ' = <[l:=v]>σ  σ !! l = None);
55
    last first.
56
  - intros e2 σ2 ef Hstep. inversion_clear Hstep. split; first done.
57
    rewrite Hv in Hvl. inversion_clear Hvl.
58
    eexists; split_ands; done.
59
  - set (l := fresh $ dom (gset loc) σ).
60
    exists (Loc l), ((<[l:=v]>)σ), None. eapply AllocS; first done.
61
    apply (not_elem_of_dom (D := gset loc)). apply is_fresh.
62
63
  - reflexivity.
  - reflexivity.
64
  - rewrite -pvs_intro. apply sep_mono; first done. apply later_mono.
65
    apply forall_intro=>e2. apply forall_intro=>σ2.
66
67
    apply wand_intro_l. rewrite -pvs_intro.
    rewrite always_and_sep_l' -associative -always_and_sep_l'.
68
    apply const_elim_l. intros [l [-> [-> Hl]]].
69
    rewrite (forall_elim l). eapply const_intro_l; first eexact Hl.
70
71
    rewrite always_and_sep_l' associative -always_and_sep_l' wand_elim_r.
    rewrite -wp_value'; done.
72
Qed.
Ralf Jung's avatar
Ralf Jung committed
73

74
Lemma wp_load_pst E σ l v Q :
Ralf Jung's avatar
Ralf Jung committed
75
  σ !! l = Some v 
76
  (ownP σ  (ownP σ - Q v))  wp E (Load (Loc l)) Q.
Ralf Jung's avatar
Ralf Jung committed
77
78
Proof.
  intros Hl. etransitivity; last eapply wp_lift_step with (σ1 := σ)
79
80
81
    (φ := λ e' σ', e' = v2e v  σ' = σ); last first.
  - intros e2 σ2 ef Hstep. move: Hl. inversion_clear Hstep=>{σ}.
    rewrite Hlookup. case=>->. done.
82
  - do 3 eexists. econstructor; eassumption.
Ralf Jung's avatar
Ralf Jung committed
83
84
  - reflexivity.
  - reflexivity.
85
86
  - rewrite -pvs_intro.
    apply sep_mono; first done. apply later_mono.
87
    apply forall_intro=>e2. apply forall_intro=>σ2.
88
89
    apply wand_intro_l. rewrite -pvs_intro.
    rewrite always_and_sep_l' -associative -always_and_sep_l'.
90
    apply const_elim_l. intros [-> ->].
91
    by rewrite wand_elim_r -wp_value.
Ralf Jung's avatar
Ralf Jung committed
92
93
Qed.

94
Lemma wp_store_pst E σ l e v v' Q :
95
  e2v e = Some v 
Ralf Jung's avatar
Ralf Jung committed
96
  σ !! l = Some v' 
97
  (ownP σ  (ownP (<[l:=v]>σ) - Q LitUnitV))  wp E (Store (Loc l) e) Q.
Ralf Jung's avatar
Ralf Jung committed
98
Proof.
99
  intros Hvl Hl. etransitivity; last eapply wp_lift_step with (σ1 := σ)
100
101
    (φ := λ e' σ', e' = LitUnit  σ' = <[l:=v]>σ); last first.
  - intros e2 σ2 ef Hstep. move: Hl. inversion_clear Hstep=>{σ2}.
102
103
    rewrite Hvl in Hv. inversion_clear Hv. done.
  - do 3 eexists. eapply StoreS; last (eexists; eassumption). eassumption.
Ralf Jung's avatar
Ralf Jung committed
104
105
  - reflexivity.
  - reflexivity.
106
107
  - rewrite -pvs_intro.
    apply sep_mono; first done. apply later_mono.
108
    apply forall_intro=>e2. apply forall_intro=>σ2.
109
110
    apply wand_intro_l. rewrite -pvs_intro.
    rewrite always_and_sep_l' -associative -always_and_sep_l'.
111
    apply const_elim_l. intros [-> ->].
112
    by rewrite wand_elim_r -wp_value'.
Ralf Jung's avatar
Ralf Jung committed
113
Qed.
114

115
Lemma wp_cas_fail_pst E σ l e1 v1 e2 v2 v' Q :
Ralf Jung's avatar
Ralf Jung committed
116
117
  e2v e1 = Some v1  e2v e2 = Some v2 
  σ !! l = Some v'  v' <> v1 
118
  (ownP σ  (ownP σ - Q LitFalseV))  wp E (Cas (Loc l) e1 e2) Q.
Ralf Jung's avatar
Ralf Jung committed
119
120
Proof.
  intros Hvl Hl. etransitivity; last eapply wp_lift_step with (σ1 := σ)
121
122
123
    (φ := λ e' σ', e' = LitFalse  σ' = σ) (E1:=E); auto; last first.
  - by inversion_clear 1; simplify_map_equality.
  - do 3 eexists; econstructor; eauto.
124
125
  - rewrite -pvs_intro.
    apply sep_mono; first done. apply later_mono.
Ralf Jung's avatar
Ralf Jung committed
126
    apply forall_intro=>e2'. apply forall_intro=>σ2'.
127
128
    apply wand_intro_l. rewrite -pvs_intro.
    rewrite always_and_sep_l' -associative -always_and_sep_l'.
Ralf Jung's avatar
Ralf Jung committed
129
    apply const_elim_l. intros [-> ->].
130
    by rewrite wand_elim_r -wp_value'.
Ralf Jung's avatar
Ralf Jung committed
131
132
Qed.

133
Lemma wp_cas_suc_pst E σ l e1 v1 e2 v2 Q :
Ralf Jung's avatar
Ralf Jung committed
134
135
  e2v e1 = Some v1  e2v e2 = Some v2 
  σ !! l = Some v1 
136
  (ownP σ  (ownP (<[l:=v2]>σ) - Q LitTrueV))  wp E (Cas (Loc l) e1 e2) Q.
Ralf Jung's avatar
Ralf Jung committed
137
138
139
140
141
142
143
144
145
146
147
148
Proof.
  intros Hvl Hl. etransitivity; last eapply wp_lift_step with (σ1 := σ)
    (φ := λ e' σ', e' = LitTrue  σ' = <[l:=v2]>σ); last first.
  - intros e2' σ2' ef Hstep. move:H. inversion_clear Hstep=>H.
    (* FIXME this rewriting is rather ugly. *)
    + exfalso. rewrite H in Hlookup. case:Hlookup=>?; subst vl.
      rewrite Hvl in Hv1. case:Hv1=>?; subst v1. done.
    + rewrite H in Hlookup. case:Hlookup=>?; subst v1.
      rewrite Hl in Hv2. case:Hv2=>?; subst v2. done.
  - do 3 eexists. eapply CasSucS; eassumption.
  - reflexivity.
  - reflexivity.
149
150
  - rewrite -pvs_intro.
    apply sep_mono; first done. apply later_mono.
Ralf Jung's avatar
Ralf Jung committed
151
    apply forall_intro=>e2'. apply forall_intro=>σ2'.
152
153
    apply wand_intro_l. rewrite -pvs_intro.
    rewrite always_and_sep_l' -associative -always_and_sep_l'.
Ralf Jung's avatar
Ralf Jung committed
154
    apply const_elim_l. intros [-> ->].
155
    by rewrite wand_elim_r -wp_value'.
Ralf Jung's avatar
Ralf Jung committed
156
157
Qed.

158
159
160
(** Base axioms for core primitives of the language: Stateless reductions *)

Lemma wp_fork E e :
161
162
   wp coPset_all e (λ _, True : iProp heap_lang Σ)
   wp E (Fork e) (λ v, (v = LitUnitV)).
163
164
165
166
167
168
169
170
171
172
173
174
Proof.
  etransitivity; last eapply wp_lift_pure_step with
    (φ := λ e' ef, e' = LitUnit  ef = Some e);
    last first.
  - intros σ1 e2 σ2 ef Hstep%prim_ectx_step; last first.
    { do 3 eexists. eapply ForkS. }
    inversion_clear Hstep. done.
  - intros ?. do 3 eexists. exists EmptyCtx. do 2 eexists.
    split_ands; try (by rewrite fill_empty); [].
    eapply ForkS.
  - reflexivity.
  - apply later_mono.
175
176
177
    apply forall_intro=>e2; apply forall_intro=>ef.
    apply impl_intro_l, const_elim_l=>-[-> ->] /=; apply sep_intro_True_l; auto.
    by rewrite -wp_value' //; apply const_intro.
178
Qed.
179
180
181
182
183

Lemma wp_lift_pure_step E (φ : expr  Prop) Q e1 :
  to_val e1 = None 
  ( σ1, reducible e1 σ1) 
  ( σ1 e2 σ2 ef, prim_step e1 σ1 e2 σ2 ef  σ1 = σ2  ef = None  φ e2) 
184
  (  e2,  φ e2  wp E e2 Q)  wp E e1 Q.
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
Proof.
  intros He Hsafe Hstep.
  (* RJ: working around https://coq.inria.fr/bugs/show_bug.cgi?id=4536 *)
  etransitivity; last eapply wp_lift_pure_step with
    (φ0 := λ e' ef, ef = None  φ e'); last first.
  - intros σ1 e2 σ2 ef Hstep'%prim_ectx_step; last done.
    by apply Hstep.
  - intros σ1. destruct (Hsafe σ1) as (e' & σ' & ? & ?).
    do 3 eexists. exists EmptyCtx. do 2 eexists.
    split_ands; try (by rewrite fill_empty); eassumption.
  - done.
  - apply later_mono. apply forall_mono=>e2. apply forall_intro=>ef.
    apply impl_intro_l. apply const_elim_l; move=>[-> Hφ].
    eapply const_intro_l; first eexact Hφ. rewrite impl_elim_r.
    rewrite right_id. done.
Qed.
201

202
203
Lemma wp_rec E ef e v Q :
  e2v e = Some v 
204
  wp E ef.[Rec ef, e /] Q  wp E (App (Rec ef) e) Q.
205
206
Proof.
  etransitivity; last eapply wp_lift_pure_step with
207
    (φ := λ e', e' = ef.[Rec ef, e /]); last first.
208
  - intros ? ? ? ? Hstep. inversion_clear Hstep. done.
209
  - intros ?. do 3 eexists. eapply BetaS; eassumption.
210
211
212
213
214
  - reflexivity.
  - apply later_mono, forall_intro=>e2. apply impl_intro_l.
    apply const_elim_l=>->. done.
Qed.

Ralf Jung's avatar
Ralf Jung committed
215
Lemma wp_plus n1 n2 E Q :
216
  Q (LitNatV (n1 + n2))  wp E (Plus (LitNat n1) (LitNat n2)) Q.
217
218
219
220
221
222
223
224
Proof.
  etransitivity; last eapply wp_lift_pure_step with
    (φ := λ e', e' = LitNat (n1 + n2)); last first.
  - intros ? ? ? ? Hstep. inversion_clear Hstep; done.
  - intros ?. do 3 eexists. econstructor.
  - reflexivity.
  - apply later_mono, forall_intro=>e2. apply impl_intro_l.
    apply const_elim_l=>->.
Ralf Jung's avatar
Ralf Jung committed
225
226
227
228
229
    rewrite -wp_value'; last reflexivity; done.
Qed.

Lemma wp_le_true n1 n2 E Q :
  n1  n2 
230
  Q LitTrueV  wp E (Le (LitNat n1) (LitNat n2)) Q.
Ralf Jung's avatar
Ralf Jung committed
231
232
233
234
235
236
237
238
239
240
241
242
243
Proof.
  intros Hle. etransitivity; last eapply wp_lift_pure_step with
    (φ := λ e', e' = LitTrue); last first.
  - intros ? ? ? ? Hstep. inversion_clear Hstep; first done.
    exfalso. eapply le_not_gt with (n := n1); eassumption.
  - intros ?. do 3 eexists. econstructor; done.
  - reflexivity.
  - apply later_mono, forall_intro=>e2. apply impl_intro_l.
    apply const_elim_l=>->.
    rewrite -wp_value'; last reflexivity; done.
Qed.

Lemma wp_le_false n1 n2 E Q :
Ralf Jung's avatar
Ralf Jung committed
244
  n1 > n2 
245
  Q LitFalseV  wp E (Le (LitNat n1) (LitNat n2)) Q.
Ralf Jung's avatar
Ralf Jung committed
246
247
248
249
Proof.
  intros Hle. etransitivity; last eapply wp_lift_pure_step with
    (φ := λ e', e' = LitFalse); last first.
  - intros ? ? ? ? Hstep. inversion_clear Hstep; last done.
Ralf Jung's avatar
Ralf Jung committed
250
251
    exfalso. eapply le_not_gt with (n := n1); eassumption.
  - intros ?. do 3 eexists. econstructor; done.
Ralf Jung's avatar
Ralf Jung committed
252
253
254
  - reflexivity.
  - apply later_mono, forall_intro=>e2. apply impl_intro_l.
    apply const_elim_l=>->.
255
256
    rewrite -wp_value'; last reflexivity; done.
Qed.
Ralf Jung's avatar
Ralf Jung committed
257
258
259

Lemma wp_fst e1 v1 e2 v2 E Q :
  e2v e1 = Some v1  e2v e2 = Some v2 
260
  Q v1  wp E (Fst (Pair e1 e2)) Q.
Ralf Jung's avatar
Ralf Jung committed
261
262
263
264
265
266
267
268
269
270
271
272
273
Proof.
  intros Hv1 Hv2. etransitivity; last eapply wp_lift_pure_step with
    (φ := λ e', e' = e1); last first.
  - intros ? ? ? ? Hstep. inversion_clear Hstep. done.
  - intros ?. do 3 eexists. econstructor; eassumption.
  - reflexivity.
  - apply later_mono, forall_intro=>e2'. apply impl_intro_l.
    apply const_elim_l=>->.
    rewrite -wp_value'; last eassumption; done.
Qed.

Lemma wp_snd e1 v1 e2 v2 E Q :
  e2v e1 = Some v1  e2v e2 = Some v2 
274
  Q v2  wp E (Snd (Pair e1 e2)) Q.
Ralf Jung's avatar
Ralf Jung committed
275
276
277
278
279
280
281
282
283
284
285
286
287
Proof.
  intros Hv1 Hv2. etransitivity; last eapply wp_lift_pure_step with
    (φ := λ e', e' = e2); last first.
  - intros ? ? ? ? Hstep. inversion_clear Hstep; done.
  - intros ?. do 3 eexists. econstructor; eassumption.
  - reflexivity.
  - apply later_mono, forall_intro=>e2'. apply impl_intro_l.
    apply const_elim_l=>->.
    rewrite -wp_value'; last eassumption; done.
Qed.

Lemma wp_case_inl e0 v0 e1 e2 E Q :
  e2v e0 = Some v0 
288
  wp E e1.[e0/] Q  wp E (Case (InjL e0) e1 e2) Q.
Ralf Jung's avatar
Ralf Jung committed
289
290
291
292
293
294
295
296
297
298
299
300
Proof.
  intros Hv0. etransitivity; last eapply wp_lift_pure_step with
    (φ := λ e', e' = e1.[e0/]); last first.
  - intros ? ? ? ? Hstep. inversion_clear Hstep; done.
  - intros ?. do 3 eexists. econstructor; eassumption.
  - reflexivity.
  - apply later_mono, forall_intro=>e1'. apply impl_intro_l.
    by apply const_elim_l=>->.
Qed.

Lemma wp_case_inr e0 v0 e1 e2 E Q :
  e2v e0 = Some v0 
301
  wp E e2.[e0/] Q  wp E (Case (InjR e0) e1 e2) Q.
Ralf Jung's avatar
Ralf Jung committed
302
303
304
305
306
307
308
309
310
Proof.
  intros Hv0. etransitivity; last eapply wp_lift_pure_step with
    (φ := λ e', e' = e2.[e0/]); last first.
  - intros ? ? ? ? Hstep. inversion_clear Hstep; done.
  - intros ?. do 3 eexists. econstructor; eassumption.
  - reflexivity.
  - apply later_mono, forall_intro=>e2'. apply impl_intro_l.
    by apply const_elim_l=>->.
Qed.
311

Ralf Jung's avatar
Ralf Jung committed
312
(** Some derived stateless axioms *)
313

Ralf Jung's avatar
Ralf Jung committed
314
315
316
Lemma wp_le n1 n2 E P Q :
  (n1  n2  P  Q LitTrueV) 
  (n1 > n2  P  Q LitFalseV) 
317
  P  wp E (Le (LitNat n1) (LitNat n2)) Q.
Ralf Jung's avatar
Ralf Jung committed
318
319
320
321
322
323
324
Proof.
  intros HPle HPgt.
  assert (Decision (n1  n2)) as Hn12 by apply _.
  destruct Hn12 as [Hle|Hgt].
  - rewrite -wp_le_true; auto.
  - assert (n1 > n2) by omega. rewrite -wp_le_false; auto.
Qed.
325
End lifting.