vcgen.v 34.5 KB
Newer Older
Dan Frumin's avatar
Dan Frumin committed
1
From iris.heap_lang Require Export proofmode notation.
2
From iris.algebra Require Import frac.
Dan Frumin's avatar
Dan Frumin committed
3
From iris.bi Require Import big_op.
4
From iris_c.vcgen Require Import dcexpr splitenv denv.
Dan Frumin's avatar
Dan Frumin committed
5
6
7
From iris_c.c_translation Require Import monad translation proofmode.
From iris_c.lib Require Import locking_heap U.

Robbert Krebbers's avatar
Robbert Krebbers committed
8
9
10
(*
TODO

11
[DONE] Fix all the unknown cases, introduce a function for that (which should be
Robbert Krebbers's avatar
Robbert Krebbers committed
12
13
  simpl never)
- Write more tests with unknown stuff in it
14
  add tests like `!(l := 10;;;; l)`
Léon Gondelman's avatar
Léon Gondelman committed
15
[DONE] Support alloc in `vcg_wp`
Robbert Krebbers's avatar
Robbert Krebbers committed
16
- Automatically come up with the new `E`, `m` and `dv` and stuff in the unknown case
Robbert Krebbers's avatar
Robbert Krebbers committed
17
- Finish the proofs
Léon Gondelman's avatar
Léon Gondelman committed
18
[DONE] Maybe drop `wp_expr`? We are not taking it as an input of anything anymore
Robbert Krebbers's avatar
Robbert Krebbers committed
19
20
21
22
23
24
25
26
27
28

Less urgent TODO

- Symbolic fractions
- Support bind
- Support conditional
- Write examples with R
- Deal with functions
*)

Robbert Krebbers's avatar
Robbert Krebbers committed
29
30
31
32
33
34
(*
TODO:
Inductive dfrac :=
  | dFrac : frac → dfrac
  | dFracUnknown : nat → nat → frac → dfrac.
*)
35
36


Dan Frumin's avatar
Dan Frumin committed
37
38
39
Section vcg.
  Context `{amonadG Σ}.

Léon Gondelman's avatar
Léon Gondelman committed
40
  Fixpoint mapsto_wand_list_aux (E: known_locs) (m : denv) (Φ : iProp Σ) (i : nat) : iProp Σ :=
41
    match m with
42
    | [] => Φ
Léon Gondelman's avatar
Léon Gondelman committed
43
    | None :: m' => mapsto_wand_list_aux E m' Φ (S i)
44
    | Some (DenvItem x q dv) :: m' =>
Léon Gondelman's avatar
Léon Gondelman committed
45
46
       dloc_interp E (dLoc i) C[x]{q} dval_interp E dv - mapsto_wand_list_aux E m' Φ (S i)
    end%I.
47

Léon Gondelman's avatar
Léon Gondelman committed
48
49
  Definition mapsto_wand_list (E: known_locs) (m : denv) (Φ : iProp Σ ) : iProp Σ :=
    mapsto_wand_list_aux E m Φ O.
50

51
52
  Definition popstack (ms : list denv) : option (list denv * denv) :=
    match ms with
Dan Frumin's avatar
Dan Frumin committed
53
    | [] => None
54
    | m :: ms => Some (ms, m)
Dan Frumin's avatar
Dan Frumin committed
55
56
    end.

57
  Fixpoint vcg_sp (E: known_locs) (ms : list denv) (de : dcexpr)
Robbert Krebbers's avatar
Robbert Krebbers committed
58
       : option (list denv * denv * dval) :=
59
    match de with
Robbert Krebbers's avatar
Robbert Krebbers committed
60
    | dCRet dv    => Some (ms, [], dv)
61
    | dCLoad de1  =>
Robbert Krebbers's avatar
Robbert Krebbers committed
62
63
64
65
       ''(ms1, mNew, dl)      vcg_sp E ms de1;
       i                      is_dloc E dl;
       ''(ms2, mNew2, q, dv)  denv_delete_frac_2 i ms1 mNew;
       Some (ms2, denv_insert i ULvl q dv mNew2, dv)
66
    | dCStore de1 de2 =>
Robbert Krebbers's avatar
Robbert Krebbers committed
67
68
69
70
71
       ''(ms1, mNew1, dl)  vcg_sp E ms de1;
       i                   is_dloc E dl;
       ''(ms2, mNew2, dv)  vcg_sp E ms1 de2;
       ''(ms3, mNew3, _)   denv_delete_full_2 i ms2 (denv_merge mNew1 mNew2);
       Some (ms3, denv_insert i LLvl 1 dv mNew3, dv)
72
    | dCBinOp op de1 de2 =>
Robbert Krebbers's avatar
Robbert Krebbers committed
73
74
       ''(ms1, mNew1, dv1)  vcg_sp E ms de1;
       ''(ms2, mNew2, dv2)  vcg_sp E ms1 de2;
75
       match dbin_op_eval E op dv1 dv2 with
Robbert Krebbers's avatar
Robbert Krebbers committed
76
       | dSome dv => Some (ms2, denv_merge mNew1 mNew2, dv)
77
78
       | dNone | dUnknown _ => None
       end
79
    | dCUnOp op de =>
Robbert Krebbers's avatar
Robbert Krebbers committed
80
       ''(ms1, mNew1, dv)  vcg_sp E ms de;
81
       match dun_op_eval E op dv with
Robbert Krebbers's avatar
Robbert Krebbers committed
82
       | dSome dv' => Some (ms1, mNew1, dv')
83
84
       | dNone | dUnknown _ => None
       end
85
    | dCSeq de1 de2 =>
Robbert Krebbers's avatar
Robbert Krebbers committed
86
87
88
89
       ''(ms1, mNew1, _)    vcg_sp E ms de1;
       ''(ms2, mNew2, dv2)  vcg_sp E (denv_unlock mNew1 :: ms1) de2;
       ''(ms3, mNew3)  popstack ms2;
       Some (ms3, denv_merge mNew2 mNew3, dv2)
90
    | dCAlloc _ |  dCUnknown _ => None
91
92
    end.

Robbert Krebbers's avatar
Robbert Krebbers committed
93
94
95
96
  Definition vcg_sp' (E: known_locs) (m : denv) (de : dcexpr) : option (denv * denv * dval) :=
    ''(ms,mNew,dv)  vcg_sp E [m] de;
    ''(_, m')  popstack ms;
    Some (m', mNew, dv).
Léon Gondelman's avatar
Léon Gondelman committed
97

98
99
100
  Definition vcg_wp_postcondition (E: known_locs)
      (Φ : known_locs  denv  dval  iProp Σ) : val  iProp Σ :=
    λ v, ( E' dv m',
Léon Gondelman's avatar
Léon Gondelman committed
101
102
         v = dval_interp E' dv  
         E `prefix_of` E'  
Robbert Krebbers's avatar
Robbert Krebbers committed
103
         denv_wf E' m' 
104
         dval_wf E' dv  
Léon Gondelman's avatar
Léon Gondelman committed
105
        denv_interp E' m' 
Robbert Krebbers's avatar
Robbert Krebbers committed
106
        Φ E' m' dv)%I.
107
108
109
110
111
112
113

  Arguments vcg_wp_postcondition : simpl never.

  Definition vcg_wp_unknown (R : iProp Σ) (E: known_locs) (de: dcexpr) (m: denv)
      (Φ : known_locs  denv  dval  iProp Σ) : iProp Σ :=
    mapsto_wand_list E m (awp (dcexpr_interp E de) R (vcg_wp_postcondition E Φ)).

114
  Definition vcg_wp_load (E : known_locs) (dv : dval) (m : denv)
Léon Gondelman's avatar
Léon Gondelman committed
115
      (Φ : denv  dval  iProp Σ) : iProp Σ :=
116
117
118
119
120
    match is_dloc E dv with
    | Some i =>
       match denv_lookup i m with
       | Some (_, dw) => Φ m dw
       | None =>
Léon Gondelman's avatar
Léon Gondelman committed
121
122
123
124
         mapsto_wand_list E m ( q v,
           dloc_interp E (dLoc i) C{q} v 
           (dloc_interp E (dLoc i) C[ULvl]{q} dval_interp E (dValUnknown v) -
           Φ [] (dValUnknown v)))
125
       end
126
127
128
129
    | _ =>
      mapsto_wand_list E m ( (l : loc) q v,
        dval_interp E dv = #l 
        l C{q} v  (l C{q} v - Φ [] (dValUnknown v)))%I
Léon Gondelman's avatar
Léon Gondelman committed
130
    end%I.
131
132

  Definition vcg_wp_store (E : known_locs) (dv1 dv2 : dval) (m : denv)
Léon Gondelman's avatar
Léon Gondelman committed
133
      (Φ : denv  dval  iProp Σ) : iProp Σ :=
134
135
136
137
138
    match is_dloc E dv1 with
    | Some i =>
       match denv_delete_full i m with
       | Some (m', dw) => Φ (denv_insert i LLvl 1 dv2 m') dv2
       | None =>
Léon Gondelman's avatar
Léon Gondelman committed
139
140
141
         mapsto_wand_list E m ( v : val,
           dloc_interp E (dLoc i) C v 
           (dloc_interp E (dLoc i) C[LLvl] dval_interp E dv2 - Φ [] dv2))
142
       end
Robbert Krebbers's avatar
Robbert Krebbers committed
143
    | _ =>
144
145
146
147
       mapsto_wand_list E m ( (l : loc) (v : val),
         dval_interp E dv1 = #l 
         dloc_interp E (dLocUnknown l) C v 
         (dloc_interp E (dLocUnknown l) C[LLvl] dval_interp E dv2 - Φ [] dv2))%I
Léon Gondelman's avatar
Léon Gondelman committed
148
    end%I.
149
150

  Definition vcg_wp_bin_op (E : known_locs) (op : bin_op) (dv1 dv2 : dval)
Léon Gondelman's avatar
Léon Gondelman committed
151
      (m : denv) (Φ : denv  dval  iProp Σ) : iProp Σ :=
152
153
    match dbin_op_eval E op dv1 dv2 with
    | dSome dw => Φ m dw
154
    | dUnknown (Some dw) => Φ m dw
Léon Gondelman's avatar
Léon Gondelman committed
155
156
    | _ => False
    end%I.
157
158

  Fixpoint vcg_wp (E : known_locs) (m : denv) (de : dcexpr)
Léon Gondelman's avatar
Léon Gondelman committed
159
      (R : iProp Σ) (Φ : known_locs  denv  dval  iProp Σ) : iProp Σ :=
160
    match de with
Robbert Krebbers's avatar
Robbert Krebbers committed
161
    | dCRet dv => Φ E m dv
162
    | dCLoad de1 =>
163
       vcg_wp E m de1 R (λ E m' dv, vcg_wp_load E dv m' (Φ E))
164
    | dCStore de1 de2 =>
Robbert Krebbers's avatar
Robbert Krebbers committed
165
166
167
168
       match vcg_sp' E m de1 with
       | Some (m', mNew, dv1) =>
          vcg_wp E m' de2 R (λ E m'' dv2,
            vcg_wp_store E dv1 dv2 (denv_merge mNew m'') (Φ E))
169
       | None =>
Robbert Krebbers's avatar
Robbert Krebbers committed
170
171
172
173
          match vcg_sp' E m de2 with
          | Some (m', mNew, dv2) =>
             vcg_wp E m' de1 R (λ E m'' dv1,
               vcg_wp_store E dv1 dv2 (denv_merge mNew m'') (Φ E))
Léon Gondelman's avatar
Léon Gondelman committed
174
          | None => vcg_wp_unknown R E de m Φ
175
176
177
          end
        end
    | dCBinOp op de1 de2 =>
Robbert Krebbers's avatar
Robbert Krebbers committed
178
179
180
181
       match vcg_sp' E m de1 with
       | Some (m', mNew, dv1) =>
          vcg_wp E m' de2 R (λ E m'' dv2,
            vcg_wp_bin_op E op dv1 dv2 (denv_merge mNew m'') (Φ E))
182
       | None =>
Robbert Krebbers's avatar
Robbert Krebbers committed
183
184
185
186
          match vcg_sp' E m de2 with
          | Some (m', mNew, dv2) =>
             vcg_wp E m' de1 R (λ E m'' dv1,
               vcg_wp_bin_op E op dv1 dv2 (denv_merge mNew m'') (Φ E))
Léon Gondelman's avatar
Léon Gondelman committed
187
          | None => vcg_wp_unknown R E de m Φ
188
          end
Robbert Krebbers's avatar
Robbert Krebbers committed
189
       end
190
    | dCUnOp op de =>
Robbert Krebbers's avatar
Robbert Krebbers committed
191
       vcg_wp E m de R (λ E m dv,
192
         match dun_op_eval E op dv with
Robbert Krebbers's avatar
Robbert Krebbers committed
193
         | dSome dw => Φ E m dw
Léon Gondelman's avatar
Léon Gondelman committed
194
         | mdw =>  x, doption_interp mdw = Some x  Φ E m x
195
196
         end)
    | dCSeq de1 de2 =>
Robbert Krebbers's avatar
Robbert Krebbers committed
197
       vcg_wp E m de1 R (λ E m _,
Léon Gondelman's avatar
Léon Gondelman committed
198
         U (vcg_wp E (denv_unlock m) de2 R Φ))
199
200
201
    | dCAlloc de =>
      vcg_wp E m de R (λ E m' dv,
          l : loc, Φ (E++[l]) (denv_insert (length E) ULvl 1 dv m')  (dLitV $ dLitLoc (dLoc (length E))))
Léon Gondelman's avatar
Léon Gondelman committed
202
    | _ => vcg_wp_unknown R E de m Φ
Léon Gondelman's avatar
Léon Gondelman committed
203
    end%I.
204
205
206
207
208
End vcg.

Section vcg_spec.
  Context `{amonadG Σ}.

Léon Gondelman's avatar
Léon Gondelman committed
209
210
  Lemma mapsto_wand_list_aux_spec E m Φ (k : nat) :
    mapsto_wand_list_aux E m Φ k -
Dan Frumin's avatar
Dan Frumin committed
211
    ([ list] ndio  m, from_option
Léon Gondelman's avatar
Léon Gondelman committed
212
213
        (λ '{| denv_level := lv; denv_frac := q; denv_dval := dv |},
           default 1%positive (E !! (k + n)%nat) C[lv]{q} dval_interp E dv) True dio) - Φ.
Dan Frumin's avatar
Dan Frumin committed
214
  Proof.
215
216
217
218
219
220
    iIntros "H". iInduction m as [|[[x q dv]|]] "IH" forall (k); simpl; first auto.
    - iIntros "[H1 H2]". rewrite -plus_n_O. iSpecialize ("H" with "H1").
      iApply ("IH" with "H [H2]"). iApply (big_sepL_impl with "H2").
      iIntros "!>" (n y ?) "/= H". by replace (k + S n)%nat with (S k + n)%nat by omega.
    - iIntros "[_ H2]". iApply ("IH" with "H [H2]"). iApply (big_sepL_impl with "H2").
      iIntros "!>" (n y ?) "/= H". by replace (k + S n)%nat with (S k + n)%nat by omega.
Dan Frumin's avatar
Dan Frumin committed
221
222
  Qed.

Léon Gondelman's avatar
Léon Gondelman committed
223
224
  Lemma mapsto_wand_list_spec E m Φ :
    mapsto_wand_list E m Φ - denv_interp E m - Φ.
225
  Proof.
Dan Frumin's avatar
Dan Frumin committed
226
227
228
    unfold mapsto_wand_list, denv_interp.
    iIntros "H1 H2". iApply (mapsto_wand_list_aux_spec with "H1 H2").
  Qed.
229

Dan Frumin's avatar
Dan Frumin committed
230
231
232
  Lemma vcg_sp_length E de ms ms' mNew dv :
    vcg_sp E ms de = Some (ms', mNew, dv) 
    length ms = length ms'.
Dan Frumin's avatar
Dan Frumin committed
233
  Proof.
Dan Frumin's avatar
Dan Frumin committed
234
235
236
    revert ms ms' mNew dv. induction de;
    intros ms ms' mNew dv Hsp; simplify_eq/=; eauto.
    - destruct (vcg_sp E ms de) as [[[ms1 mNew1] dv1]|] eqn:Hout; simplify_eq /=.
Dan Frumin's avatar
Dan Frumin committed
237
238
      destruct dv1 as [dv1|dv1]; destruct dv1; simplify_eq/=.
      destruct d as [i|?]; simplify_eq/=.
Dan Frumin's avatar
Dan Frumin committed
239
240
      destruct (denv_delete_frac_2 i ms1 mNew1) as [[[[ms2 mNew2] q] dv1]|] eqn:Hout1; simplify_eq/=.
      transitivity (length ms1).
Dan Frumin's avatar
Dan Frumin committed
241
      + by eapply IHde.
Dan Frumin's avatar
Dan Frumin committed
242
243
      + by eapply denv_delete_frac_2_length.
    - destruct (vcg_sp E ms de1) as [[[ms1 mNew1] dv1]|] eqn:Hde1; simplify_eq /=.
Dan Frumin's avatar
Dan Frumin committed
244
245
      destruct dv1 as [dv1|dv1]; destruct dv1; simplify_eq/=.
      destruct d as [i|?]; simplify_eq/=.
Dan Frumin's avatar
Dan Frumin committed
246
247
248
      destruct (vcg_sp E ms1 de2) as [[[ms2 mNew2] dv2]|] eqn:Hde2; simplify_eq /=.
      destruct (denv_delete_full_2 i ms2 (denv_merge mNew1 mNew2)) as [[[ms3 mNew3] dv1]|] eqn:Hout1; simplify_eq/=.
      transitivity (length ms1).
Dan Frumin's avatar
Dan Frumin committed
249
      + by eapply IHde1.
Dan Frumin's avatar
Dan Frumin committed
250
251
252
253
      + transitivity (length ms2). by eapply IHde2.
        by eapply denv_delete_full_2_length.
    - destruct (vcg_sp E ms de1) as [[[ms1 mNew1] dv1]|] eqn:Hde1; simplify_eq/=.
      destruct (vcg_sp E ms1 de2) as [[[ms2 mNew2] dv2]|] eqn:Hde2; simplify_eq/=.
Dan Frumin's avatar
Dan Frumin committed
254
      destruct (dbin_op_eval E b dv1 dv2) eqn:Hboe; simplify_eq/=.
255
      transitivity (length ms1); eauto.
Dan Frumin's avatar
Dan Frumin committed
256
    - destruct (vcg_sp E ms de) as [[[ms1 mNew1] dv1]|] eqn:Hde; simplify_eq/=.
Dan Frumin's avatar
Dan Frumin committed
257
258
      destruct (dun_op_eval E u dv1); simplify_eq/=.
      by eapply IHde.
Dan Frumin's avatar
Dan Frumin committed
259
260
261
262
    - destruct (vcg_sp E ms de1) as [[[ms1 mNew1] dv1]|] eqn:Hde1; simplify_eq/=.
      destruct (vcg_sp E (denv_unlock mNew1 :: ms1) de2) as [[[ms2 mNew2] dv2]|] eqn:Hde2; simplify_eq/=.
      destruct ms2; simplify_eq/=.
      transitivity (length ms1).
Dan Frumin's avatar
Dan Frumin committed
263
264
265
266
      + by eapply IHde1.
      + apply IHde2 in Hde2. by simplify_eq/=.
  Qed.

Dan Frumin's avatar
Dan Frumin committed
267
268
269
270
  Lemma vcg_sp_correct' E de ms ms' mNew dv R :
    vcg_sp E ms de = Some (ms', mNew, dv) 
    (denv_stack_interp ms ms' E
        (awp (dcexpr_interp E de) R (λ v, v = dval_interp E dv  denv_interp E mNew)))%I.
271
  Proof.
Dan Frumin's avatar
Dan Frumin committed
272
273
    revert ms ms' mNew dv. induction de;
    iIntros (ms ms' mNew dv Hsp); simplify_eq/=.
Dan Frumin's avatar
Dan Frumin committed
274
275
    - iFrame. iApply denv_stack_interp_intro.
      iApply awp_ret. wp_value_head. iSplit; eauto. rewrite /denv_interp //.
Dan Frumin's avatar
Dan Frumin committed
276
277
    - specialize (IHde ms).
      destruct (vcg_sp E ms de) as [[[ms1 mNew1] dv1]|]; simplify_eq /=.
Léon Gondelman's avatar
Léon Gondelman committed
278
      destruct dv1 as [dv1|dv1]; destruct dv1; simplify_eq/=.
Léon Gondelman's avatar
Léon Gondelman committed
279
      destruct d as [i|?]; simplify_eq/=.
Dan Frumin's avatar
Dan Frumin committed
280
281
      destruct (denv_delete_frac_2 i ms1 mNew1) as [[[[ms2 mNew2] q] dv1]|] eqn:Hfar; simplify_eq/=.
      iPoseProof IHde as "Hawp"; first done.
282
      iPoseProof denv_delete_frac_2_interp as "Hm"; first eassumption.
Dan Frumin's avatar
Dan Frumin committed
283
284
285
      iDestruct (denv_stack_interp_trans with "Hawp Hm") as "Hawp'".
      iClear "Hawp Hm".
      iApply (denv_stack_interp_mono with "Hawp'").
Dan Frumin's avatar
Dan Frumin committed
286
      iIntros "[Hawp Hm]".
Léon Gondelman's avatar
Léon Gondelman committed
287
288
      iApply a_load_spec.
      iApply (awp_wand with "Hawp").
Dan Frumin's avatar
Dan Frumin committed
289
290
291
      iIntros (?) "[% HmNew1]". simplify_eq/=.
      iExists _, _. iSplit; eauto. iDestruct ("Hm" with "HmNew1") as "[HmNew2 $]".
      iIntros "Hl". iSplit; eauto.
Dan Frumin's avatar
Dan Frumin committed
292
      rewrite -denv_insert_interp.
Léon Gondelman's avatar
Léon Gondelman committed
293
      by iFrame.
Dan Frumin's avatar
Dan Frumin committed
294
295
    - specialize (IHde1 ms).
      destruct (vcg_sp E ms de1) as [[[ms1 mNew1] dv1]|]; simplify_eq /=.
Léon Gondelman's avatar
Léon Gondelman committed
296
      destruct dv1 as [dv1|dv1]; destruct dv1; simplify_eq/=.
Léon Gondelman's avatar
Léon Gondelman committed
297
      destruct d as [i|?]; simplify_eq/=.
Dan Frumin's avatar
Dan Frumin committed
298
299
300
301
302
303
304
305
306
307
      specialize (IHde2 ms1).
      destruct (vcg_sp E ms1 de2) as [[[ms2 mNew2] dv2]|]; simplify_eq /=.
      destruct (denv_delete_full_2 i ms2 (denv_merge mNew1 mNew2))
        as [[[ms3 mNew3] dv3] |] eqn:Hfar; simplify_eq/=.
      iPoseProof IHde1 as "Hawp1"; first done.
      iPoseProof IHde2 as "Hawp2"; first done.
      iPoseProof denv_delete_full_2_interp as "Hl"; first done.
      iDestruct (denv_stack_interp_trans with "Hawp1 Hawp2") as "Hawp'".
      iDestruct (denv_stack_interp_trans with "Hawp' Hl") as "Hawp".
      iClear "Hawp1 Hawp2 Hl Hawp'".
Dan Frumin's avatar
Dan Frumin committed
308
309
310
311
312
      iApply (denv_stack_interp_mono with "Hawp").
      iIntros "[[Hawp1  Hawp2] Hl]".
      iApply (a_store_spec with "Hawp1 Hawp2").
      iNext. iIntros (? ?) "[% HmNew1] [% HmNew2]". simplify_eq/=.
      iExists _, _; iSplit; eauto.
Dan Frumin's avatar
Dan Frumin committed
313
314
315
      iCombine "HmNew1 HmNew2" as "HmNew".
      rewrite denv_merge_interp -denv_insert_interp.
      iDestruct ("Hl" with "HmNew") as "[HmNew3 $]".
Dan Frumin's avatar
Dan Frumin committed
316
      iIntros "Hl". by iFrame.
Dan Frumin's avatar
Dan Frumin committed
317
318
319
320
    - specialize (IHde1 ms).
      destruct (vcg_sp E ms de1) as [[[ms1 mNew1] dv1]|]; simplify_eq /=.
      specialize (IHde2 ms1).
      destruct (vcg_sp E ms1 de2) as [[[ms2 mNew2] dv2]|]; simplify_eq /=.
321
      destruct (dbin_op_eval E b dv1 dv2) eqn:Hboe; simplify_eq/=.
Dan Frumin's avatar
Dan Frumin committed
322
323
      iPoseProof IHde1 as "Hawp1"; first done.
      iPoseProof IHde2 as "Hawp2"; first done.
Dan Frumin's avatar
Dan Frumin committed
324
      iDestruct (denv_stack_interp_trans with "Hawp1 Hawp2") as "Hawp".
Dan Frumin's avatar
Dan Frumin committed
325
      iClear "Hawp1 Hawp2".
Dan Frumin's avatar
Dan Frumin committed
326
      iApply (denv_stack_interp_mono with "Hawp"). iIntros "[Hawp1 Hawp2]".
Léon Gondelman's avatar
Léon Gondelman committed
327
      iApply (a_bin_op_spec with "Hawp1 Hawp2").
Dan Frumin's avatar
Dan Frumin committed
328
      iNext. iIntros (? ?) "[% HmNew1] [% HmNew2]"; simplify_eq/=.
Léon Gondelman's avatar
Léon Gondelman committed
329
      iExists (dval_interp E dv). repeat iSplit; eauto.
330
      + iPureIntro. apply dbin_op_eval_correct. by rewrite Hboe.
Léon Gondelman's avatar
Léon Gondelman committed
331
      + rewrite -denv_merge_interp. iFrame.
Dan Frumin's avatar
Dan Frumin committed
332
333
    - specialize (IHde ms).
      destruct (vcg_sp E ms de) as [[[ms1 mNew1] dv1]|]; simplify_eq /=.
334
      remember (dun_op_eval E u dv1) as Hu; destruct Hu; simplify_eq/=.
Dan Frumin's avatar
Dan Frumin committed
335
336
      iPoseProof IHde as "Hawp"; first done.
      iApply (denv_stack_interp_mono with "Hawp"). iClear "Hawp".
Dan Frumin's avatar
Dan Frumin committed
337
      iIntros "Hawp".
338
339
340
341
      iApply a_un_op_spec.
      iApply (awp_wand with "Hawp"). iIntros (v) "[% H]". simplify_eq/=.
      iExists (dval_interp E dv). repeat iSplit; eauto.
      iPureIntro. apply dun_op_eval_correct. by rewrite -HeqHu.
Dan Frumin's avatar
Dan Frumin committed
342
343
344
345
346
347
    - specialize (IHde1 ms).
      destruct (vcg_sp E ms de1) as [[[ms1 mNew1] dv1]|]; simplify_eq /=.
      specialize (IHde2 (denv_unlock mNew1 :: ms1)).
      destruct (vcg_sp E _ de2) as [[[ms2 mNew2] dv2]|] eqn:Hsp2; simplify_eq /=.
      iPoseProof IHde1 as "Hawp1"; first done.
      iPoseProof IHde2 as "Hawp2"; first done.
Dan Frumin's avatar
Dan Frumin committed
348
      unfold popstack in Hsp.
Dan Frumin's avatar
Dan Frumin committed
349
      destruct ms2 as [|t ms2'] eqn:Houteq; simplify_eq/=.
Dan Frumin's avatar
Dan Frumin committed
350
      iDestruct (denv_stack_interp_trans with "Hawp1 Hawp2") as "Hawp".
Dan Frumin's avatar
Dan Frumin committed
351
      iClear "Hawp1 Hawp2".
Dan Frumin's avatar
Dan Frumin committed
352
      iApply (denv_stack_interp_mono with "Hawp"). iIntros "[Hawp1 Hawp2]".
353
      iApply a_sequence_spec'. iNext. iApply (awp_wand with "Hawp1").
Dan Frumin's avatar
Dan Frumin committed
354
      iIntros (?) "[% HmNew1]". simplify_eq/=. iClear "Hawp".
Dan Frumin's avatar
Dan Frumin committed
355
      rewrite (denv_unlock_interp E mNew1).
356
      iModIntro. iDestruct ("Hawp2" with "HmNew1") as "[HmNew' Hawp2]".
Dan Frumin's avatar
Dan Frumin committed
357
358
      iApply (awp_wand with "Hawp2"). iIntros (?) "[% HmNew2]".
      rewrite -denv_merge_interp. iSplit; eauto with iFrame.
359
  Qed.
Dan Frumin's avatar
Dan Frumin committed
360

Dan Frumin's avatar
Dan Frumin committed
361
362
  Lemma vcg_sp_correct E de m ms mNew dv R :
    vcg_sp E [m] de = Some (ms, mNew, dv) 
Dan Frumin's avatar
Dan Frumin committed
363
    denv_interp E m -
Dan Frumin's avatar
Dan Frumin committed
364
    denv_interp E (denv_stack_merge ms)  awp (dcexpr_interp E de) R (λ v, v = dval_interp E dv  denv_interp E mNew).
Dan Frumin's avatar
Dan Frumin committed
365
  Proof.
366
    iIntros (Hsp) "Hm".
Dan Frumin's avatar
Dan Frumin committed
367
368
369
370
371
    iPoseProof vcg_sp_correct' as "Hawp"; first eassumption.
    pose (vcg_sp_length _ _ _ _ _ _ Hsp) as Hlen.
    assert ( m', ms = [m']) as [m' ->]=>/=.
    { destruct ms as [|m' [|m'' ms'']]; eauto; inversion Hlen. }
    rewrite denv_merge_nil_r. iDestruct ("Hawp" with "Hm") as "[$ $]".
Dan Frumin's avatar
Dan Frumin committed
372
  Qed.
Léon Gondelman's avatar
Léon Gondelman committed
373

Dan Frumin's avatar
Dan Frumin committed
374
375
376
377
  Lemma vcg_sp'_correct E de m m' mNew dv R :
    vcg_sp' E m de = Some (m', mNew, dv) 
    denv_interp E m -
    denv_interp E m'  awp (dcexpr_interp E de) R (λ v, v = dval_interp E dv  denv_interp E mNew).
Dan Frumin's avatar
Dan Frumin committed
378
  Proof.
Dan Frumin's avatar
Dan Frumin committed
379
380
381
382
383
384
385
386
    rewrite /vcg_sp'.
    iIntros (Hsp') "Hm".
    destruct (vcg_sp E [m] de) as [[[ms ?mNew] ?dv]|] eqn:Hsp; simplify_eq/=.
    destruct ms as [|?m' ms]; simplify_eq/=.
    pose (vcg_sp_length _ _ _ _ _ _ Hsp) as Hlen.
    assert (ms = []) as -> by (destruct ms; eauto; inversion Hlen).
    rewrite vcg_sp_correct; last eassumption. simpl.
    rewrite denv_merge_nil_r. iFrame.
Dan Frumin's avatar
Dan Frumin committed
387
388
  Qed.

Dan Frumin's avatar
Dan Frumin committed
389
390
  Lemma vcg_sp_wf' E de ms ms' mNew dv :
    Forall (denv_wf E) ms 
Dan Frumin's avatar
Dan Frumin committed
391
    dcexpr_wf E de 
Dan Frumin's avatar
Dan Frumin committed
392
393
    vcg_sp E ms de = Some (ms', mNew, dv) 
    Forall (denv_wf E) ms'  denv_wf E mNew  dval_wf E dv .
Dan Frumin's avatar
Dan Frumin committed
394
  Proof.
Dan Frumin's avatar
Dan Frumin committed
395
396
397
    revert ms ms' mNew dv. induction de;
    intros ms ms' mNew dv Hwfms Hwfde Hsp; simplify_eq/=; eauto.
    - destruct (vcg_sp E ms de) as [[[ms1 mNew1] dv1]|] eqn:Hsp1; simplify_eq /=.
Dan Frumin's avatar
Dan Frumin committed
398
399
      destruct dv1 as [dv1|dv1]; destruct dv1; simplify_eq/=.
      destruct d as [i|?]; simplify_eq/=.
Dan Frumin's avatar
Dan Frumin committed
400
401
402
403
      destruct (denv_delete_frac_2 i ms1 mNew1) as [[[[ms2 mNew2] q] dv1]|] eqn:Hout1; simplify_eq/=.
      destruct (IHde _ _ _ _ Hwfms Hwfde Hsp1) as (?&?&?).
      eapply denv_wf_delete_frac_2 in Hout1; eauto.
      destruct Hout1 as (?&?&?).
Dan Frumin's avatar
Dan Frumin committed
404
      repeat split; eauto using denv_wf_insert.
Dan Frumin's avatar
Dan Frumin committed
405
    - destruct (vcg_sp E ms de1) as [[[ms1 mNew1] dv1]|] eqn:Hsp1; simplify_eq /=.
Dan Frumin's avatar
Dan Frumin committed
406
407
      destruct dv1 as [dv1|dv1]; destruct dv1; simplify_eq/=.
      destruct d as [i|?]; simplify_eq/=.
Dan Frumin's avatar
Dan Frumin committed
408
409
      destruct (vcg_sp E ms1 de2) as [[[ms2 mNew2] dv2]|] eqn:Hsp2; simplify_eq /=.
      destruct (denv_delete_full_2 i ms2 (denv_merge mNew1 mNew2)) as [[[ms3 mNew3] dv1]|] eqn:Hout1; simplify_eq/=.
Dan Frumin's avatar
Dan Frumin committed
410
      apply andb_True in Hwfde. destruct Hwfde as [Hwfde1 Hwfde2].
Dan Frumin's avatar
Dan Frumin committed
411
412
413
414
      destruct (IHde1 _ _ _ _ Hwfms Hwfde1 Hsp1) as (Hwfms1&HwfNew1&Hwfdv1).
      destruct (IHde2 _ _ _ _ Hwfms1 Hwfde2 Hsp2) as (?&?&?).
      eapply denv_wf_delete_full_2 in Hout1; try eassumption.
      destruct Hout1 as (?&?&?).
Dan Frumin's avatar
Dan Frumin committed
415
416
      repeat split; eauto using denv_wf_insert.
      eauto using denv_wf_merge.
Dan Frumin's avatar
Dan Frumin committed
417
418
    - destruct (vcg_sp E ms de1) as [[[ms1 mNew1] dv1]|] eqn:Hsp1; simplify_eq/=.
      destruct (vcg_sp E ms1 de2) as [[[ms2 mNew2] dv2]|] eqn:Hsp2; simplify_eq/=.
Dan Frumin's avatar
Dan Frumin committed
419
420
      destruct (dbin_op_eval E b dv1 dv2) eqn:Hboe; simplify_eq/=.
      apply andb_True in Hwfde. destruct Hwfde as [Hwfde1 Hwfde2].
Dan Frumin's avatar
Dan Frumin committed
421
422
      destruct (IHde1 _ _ _ _ Hwfms Hwfde1 Hsp1) as (Hwfms1&HwfNew1&Hwfdv1).
      destruct (IHde2 _ _ _ _ Hwfms1 Hwfde2 Hsp2) as (?&?&?).
Dan Frumin's avatar
Dan Frumin committed
423
424
      repeat split; eauto. apply denv_wf_merge; eauto.
      eapply (dbin_op_eval_dSome_wf _ dv1 dv2); eauto.
Dan Frumin's avatar
Dan Frumin committed
425
426
    - destruct (vcg_sp E ms de) as [[[ms1 mNew1] dv1]|] eqn:Hsp1; simplify_eq/=.
      destruct (IHde _ _ _ _ Hwfms Hwfde Hsp1) as (Hwfms1&HwfNew1&Hwfdv1).
Dan Frumin's avatar
Dan Frumin committed
427
428
429
      destruct (dun_op_eval E u dv1) as [|?|?] eqn:Hop; simplify_eq/=.
      repeat split; eauto.
      eapply dun_op_eval_dSome_wf; eauto.
Dan Frumin's avatar
Dan Frumin committed
430
431
432
    - destruct (vcg_sp E ms de1) as [[[ms1 mNew1] dv1]|] eqn:Hsp1; simplify_eq/=.
      destruct (vcg_sp E (denv_unlock mNew1 :: ms1) de2) as [[[ms2 mNew2] dv2]|] eqn:Hsp2; simplify_eq/=.
      destruct ms2 as [|t ms2]; simplify_eq/=.
Dan Frumin's avatar
Dan Frumin committed
433
      apply andb_True in Hwfde. destruct Hwfde as [Hwfde1 Hwfde2].
Dan Frumin's avatar
Dan Frumin committed
434
435
      destruct (IHde1 _ _ _ _ Hwfms Hwfde1 Hsp1) as (Hwfms1&HwfNew1&Hwfdv1).
      assert (Forall (denv_wf E) (denv_unlock mNew1 :: ms1)) as Hwfms2.
Dan Frumin's avatar
Dan Frumin committed
436
      { apply Forall_cons. split; eauto. by apply denv_wf_unlock. }
Dan Frumin's avatar
Dan Frumin committed
437
      destruct (IHde2 _ _ _ _ Hwfms2 Hwfde2 Hsp2) as (Hall&?&?).
Dan Frumin's avatar
Dan Frumin committed
438
439
      apply Forall_cons in Hall. destruct Hall.
      repeat split; eauto using denv_wf_merge.
Dan Frumin's avatar
Dan Frumin committed
440
  Qed.
Dan Frumin's avatar
Dan Frumin committed
441

Dan Frumin's avatar
Dan Frumin committed
442
  Lemma vcg_sp'_wf E de m m' mNew dv :
443
444
    denv_wf E m 
    dcexpr_wf E de 
Dan Frumin's avatar
Dan Frumin committed
445
446
    vcg_sp' E m de = Some (m', mNew, dv) 
    denv_wf E m'  denv_wf E mNew  dval_wf E dv .
Dan Frumin's avatar
Dan Frumin committed
447
  Proof.
Dan Frumin's avatar
Dan Frumin committed
448
449
450
451
452
453
454
455
    rewrite /vcg_sp'. intros Hm Hde Hsp'.
    assert (Forall (denv_wf E) [m]) as Hms by (econstructor; eauto).
    destruct (vcg_sp E [m] de) as [[[ms ?mNew] ?dv]|] eqn:Hsp; simplify_eq/=.
    destruct ms as [|?m' ms]; simplify_eq/=.
    pose (vcg_sp_length _ _ _ _ _ _ Hsp) as Hlen.
    assert (ms = []) as -> by (destruct ms; eauto; inversion Hlen).
    destruct (vcg_sp_wf' E de [m] [m'] mNew _ Hms Hde Hsp) as (Hm'&?&?).
    repeat split; eauto. by inversion Hm'.
Dan Frumin's avatar
Dan Frumin committed
456
  Qed.
457

458
  Lemma vcg_wp_unknown_correct R E m de Φ :
459
    denv_wf E m 
460
    denv_interp E m -
Léon Gondelman's avatar
Léon Gondelman committed
461
    vcg_wp_unknown R E de m Φ -
462
    awp (dcexpr_interp E de) R (vcg_wp_postcondition E Φ).
463
464
  Proof.
    rewrite /vcg_wp_unknown mapsto_wand_list_spec.
465
    iIntros (Hmwf) "Hm Hwp". iSpecialize ("Hwp" with "Hm").
466
    iApply (awp_wand with "Hwp"). iIntros (v) "H".
467
468
    iDestruct "H" as (E' dv m' ->) "(% & % & Hm & Hwp)".
    unfold vcg_wp_postcondition.
469
    iExists E', dv, m'. repeat (iSplit; first done); iFrame.
470
471
  Qed.

472
  Lemma vcg_wp_load_correct E m dv Φ :
473
    denv_wf E m 
474
    denv_interp E m -
Léon Gondelman's avatar
Léon Gondelman committed
475
    vcg_wp_load E dv m (Φ E) -
476
477
     (l:loc) q w, dval_interp E dv = #l 
       l C{q} w  (l C{q} w - (vcg_wp_postcondition E Φ w)).
478
479
480
  Proof.
    rewrite /vcg_wp_load. destruct (is_dloc E dv) as [i|] eqn:Hdloc.
    + destruct (denv_lookup i m) as [[q dv'] |] eqn:Hlkp; simpl; simplify_eq /=.
Dan Frumin's avatar
Dan Frumin committed
481
      * destruct (denv_lookup_interp E i q dv' m) as [m0 Hm0]; first assumption.
482
        rewrite Hm0. iIntros (Hmwf) "[Hi Hm0] HΦ". apply is_dloc_some in Hdloc. simplify_eq/=.
483
484
485
486
        iExists (dloc_interp E (dLoc i)), q,  (dval_interp E dv');
          iSplit; first done. iFrame.  iIntros "Hl".
        iExists E, dv', m; repeat (iSplit; first done). iFrame.
        rewrite Hm0.  eapply denv_lookup_wf in Hlkp; eauto with iFrame.
487
488
      * rewrite mapsto_wand_list_spec. iIntros (Hmwf) "Hm Hwp".
        iSpecialize ("Hwp" with "Hm"). simpl.
489
        iDestruct "Hwp" as (q v') "[Hl Hwp]". apply is_dloc_some in Hdloc. subst.
490
491
        iExists (dloc_interp E (dLoc i)), q, v'; iSplit; first done. iFrame.
        iIntros "Hl". iSpecialize ("Hwp" with "Hl").
492
        iExists E, (dValUnknown v'), []; repeat (iSplit; first done); iFrame.
493
494
495
        unfold denv_interp, denv_wf. eauto.
    +  rewrite mapsto_wand_list_spec. iIntros (Hmwf) "Hm Hwp".
       iSpecialize ("Hwp" with "Hm"); simpl.
496
       iDestruct "Hwp" as (l q v' ->) "[Hl Hwp]".
497
       iExists l, q, v'. iSplit; first done.
498
       iFrame. iIntros "Hl". iSpecialize ("Hwp" with "Hl").
499
500
       iExists E, (dValUnknown v'), []; iSplit; first done.
       iFrame. unfold denv_interp, denv_wf. eauto.
501
502
  Qed.

503
  Lemma vcg_wp_bin_op_correct E0 E m mOut dv1 dv2 b Φ :
504
    E0 `prefix_of` E  dval_wf E dv1  dval_wf E dv2 
505
506
    denv_wf E (denv_merge mOut m) 
    denv_interp E m -
Léon Gondelman's avatar
Léon Gondelman committed
507
    vcg_wp_bin_op E b dv1 dv2 (denv_merge mOut m) (Φ E) -
508
509
    denv_interp E mOut -
     w : val, bin_op_eval b (dval_interp E dv1) (dval_interp E dv2) = Some w 
510
               (vcg_wp_postcondition E Φ w).
511
  Proof.
512
    iIntros (Hpre Hwf1 Hwf2 Hwf3) "Hm Hwp HmOut". rewrite /vcg_wp_bin_op.
513
    destruct (dbin_op_eval E b dv1 dv2) as [ | dw | dw ] eqn:Hbin; first done.
514
    * iExists (dval_interp E dw); iSplit.
515
516
      { iPureIntro. apply dbin_op_eval_correct. by rewrite Hbin. }
      iExists E, dw, (denv_merge mOut m).
517
518
      apply dbin_op_eval_dSome_wf in Hbin; try done.
      rewrite -denv_merge_interp //. eauto with iFrame.
519
520
    * destruct dw as [dw1|] eqn:Hdw; last done.
      iExists  (dval_interp E dw1); iSplit.
521
      iPureIntro. apply dbin_op_eval_correct. by rewrite Hbin.
522
      iExists E, dw1, (denv_merge mOut m).
523
524
      rewrite -denv_merge_interp.
      apply dbin_op_eval_dUnknown_wf in Hbin; try done. eauto with iFrame.
525
  Qed.
526

527
 Lemma vcg_wp_store_correct E0 E m dv1 dv2 Φ :
528
   E0 `prefix_of` E 
529
530
   denv_wf E m 
   dval_wf E dv2 
531
   denv_interp E m -
Léon Gondelman's avatar
Léon Gondelman committed
532
   vcg_wp_store E dv1 dv2 m (Φ E) -
533
534
535
    (l : loc) (w : val), dval_interp E dv1 = #l  l C w 
     (l C[LLvl] dval_interp E dv2 - vcg_wp_postcondition E0 Φ (dval_interp E dv2)).
 Proof.
536
   iIntros (Hpre Hwf Hwf2) "Hm Hwp". rewrite{1} /vcg_wp_store; fold vcg_wp.
537
   destruct (is_dloc E dv1) as [i|] eqn:Hdloc.
538
539
   - apply is_dloc_some in Hdloc; rewrite Hdloc.
     destruct (denv_delete_full i m)  as [[m' dv_old]|] eqn:Hdel.
540
541
542
543
544
     +  iExists (dloc_interp E (dLoc i)), (dval_interp E dv_old); iSplit; first done.
        iPoseProof (denv_delete_full_interp E) as "Hdel". eassumption.
        iSpecialize ("Hdel" with "[$Hm]"). iDestruct "Hdel" as "[HmDel Hl]"; iFrame.
        iIntros "Hl". iExists E, dv2, (denv_insert i LLvl 1 dv2 m'); repeat (iSplit; first done).
        iSplit. iPureIntro. apply denv_wf_insert; last done.
545
       by specialize (denv_wf_delete_full E dv_old i m m' Hwf Hdel) as Hdelwf.
546
       rewrite -denv_insert_interp. eauto with iFrame.
547
548
     + rewrite mapsto_wand_list_spec.
       iSpecialize ("Hwp" with "[Hm]"); iFrame.
Léon Gondelman's avatar
Léon Gondelman committed
549
       iDestruct "Hwp" as (dv_old) "[Hl Hwp]";
550
       iExists (dloc_interp E (dLoc i)), dv_old; iSplit; first done. iFrame.
551
552
553
554
       iIntros "Hl". iSpecialize ("Hwp" with "Hl").
       iExists E, dv2, []; repeat (iSplit; first done). unfold denv_interp. by iFrame.
   - rewrite mapsto_wand_list_spec.
     iSpecialize ("Hwp" with "[Hm]"); iFrame.
555
     iDestruct "Hwp" as (l v ->) "[Hdv Hwp]".
556
     iExists l,v ; iSplit; first done. iFrame.
557
558
     iIntros "Hl". iSpecialize ("Hwp" with "Hl").
     iExists E, dv2, []; repeat (iSplit; first done). unfold denv_interp. by iFrame.
559
560
561
562
563
564
565
566
567
568
 Qed.

 Lemma vcg_wp_postcondition_mono E E' Φ w:
   E `prefix_of` E'  vcg_wp_postcondition E' Φ w - vcg_wp_postcondition E Φ w.
 Proof.
   iIntros (Hpre) "Hp".
   iDestruct "Hp" as (E1 dv m' ? Hpre1 ?) "(% & Hm' & H)"; simplify_eq /=.
   iExists E1, dv, m'; repeat (iSplit; first done).
   iSplit. iPureIntro. by trans E'. eauto with iFrame.
 Qed.
569

570
 Lemma vcg_wp_correct R E m de Φ :
571
    dcexpr_wf E de 
572
    denv_wf E m 
573
    denv_interp E m -
Léon Gondelman's avatar
Léon Gondelman committed
574
    vcg_wp E m de R Φ -
575
    awp (dcexpr_interp E de) R (vcg_wp_postcondition E Φ).
576
  Proof.
577
578
579
    revert Φ E m. induction de; intros Φ E m Hwf; iIntros (Hmwf) "Hm Hwp".
    - iApply awp_ret. wp_value_head.
      iExists E, d, m. iSplit; first done; by iFrame.
580
581
582
583
584
585
586
    -  rewrite IHde //. iRename "Hm" into "Hawp".
       iSpecialize ("Hawp" with "Hwp"); simpl.
       iApply a_alloc_spec. iApply (awp_wand with "Hawp").
       iIntros (v) "Hpost". iDestruct "Hpost" as (E' dv m' -> Hwf0 Hm'wf Hwf2) "(Hm & Hwp)".
       iIntros (l) "Hl". iSpecialize ("Hwp" $! l).
       unfold vcg_wp_postcondition.
       iExists (E' ++ [l]), (dLitV (dLitLoc (dLoc (length E')))), (denv_insert (length E') ULvl 1 dv m').
587
588
       iSplit. iPureIntro. compute[dval_interp dbase_lit_interp]. do 2 f_equal.
       apply (dloc_interp_lookup_endl E' l).
589
       iSplit. iPureIntro. trans E'; first done. by apply prefix_app_r.
590
591
592
593
       iSplit. iPureIntro. by apply denv_wf_extend.
       iSplit. iPureIntro. compute[dval_wf].
       case_bool_decide; first done. apply H0. apply lookup_lt_is_Some_2.
       rewrite app_length. simpl. lia.
594
595
       iFrame. rewrite -denv_insert_interp.
       iSplitL "Hm". iApply (denv_interp_mono with "Hm"); first done.
596
597
598
       trans E'; first done. by apply prefix_app_r.
       rewrite -(dloc_interp_lookup_endl E' l).
       rewrite (dval_interp_mono E' (E'++[l])) //. by apply prefix_app_r .
599
    - rewrite IHde //. iRename "Hm" into "Hawp".
600
      iSpecialize ("Hawp" with "Hwp"); simpl.
601
      iApply (a_load_spec_exists_frac with "[Hawp]"). iApply (awp_wand with "Hawp").
602
603
      iIntros (v) "H". unfold vcg_wp_postcondition.
      iDestruct "H" as (E' dv m' Heq Hwf0 Hm'wf Hwf2) "(Hm & Hwp)".
604
      iPoseProof (vcg_wp_load_correct E' m' with "Hm Hwp") as "Hload"; first done.
605
606
607
608
609
610
      iDestruct "Hload" as (l q w) "(% & Hl & Hwp)". rewrite Heq.
      iExists l, q, w. iSplit; first done. iFrame. iIntros "Hl".
      iSpecialize ("Hwp" with "Hl"). unfold vcg_wp_postcondition.
      iDestruct "Hwp" as (Ef dvf mf Hpre' Hwf5 Hwf3 Hwf4) "(Hmf & Hwp)".
      iExists Ef, dvf, mf. iFrame. iSplit; first by iPureIntro. iSplit.
      iPureIntro. trans E'; done. done.
611
    - rewrite{1} /vcg_wp; fold vcg_wp.
Robbert Krebbers's avatar
Robbert Krebbers committed
612
      simpl in Hwf. apply andb_prop_elim in Hwf as [Hwf1 Hwf2].
Dan Frumin's avatar
Dan Frumin committed
613
614
      destruct (vcg_sp' E m de1) as [[[m' mNew] dv1]|] eqn:Heqsp; last first.
      + destruct (vcg_sp' E m de2) as [[[m' mNew] dv2]|] eqn:Heqsp2; last first.
615
        { by iApply (vcg_wp_unknown_correct with "Hm Hwp"). }
Dan Frumin's avatar
Dan Frumin committed
616
617
        specialize (vcg_sp'_wf _ _ _ _ _ _ Hmwf Hwf2 Heqsp2) as (? & ? & ?).
        iDestruct (vcg_sp'_correct with "Hm") as "[Hm' Hde2]"; first done.
Robbert Krebbers's avatar
Robbert Krebbers committed
618
        clear Heqsp2 Heqsp.
Dan Frumin's avatar
Dan Frumin committed
619
        iDestruct (IHde1 with "Hm' Hwp") as "Hde1"; try done.
Robbert Krebbers's avatar
Robbert Krebbers committed
620
        iApply (a_store_spec with "Hde1 Hde2"). iIntros "!>" (v1 v2).
621
622
        iDestruct 1 as (E' dw m'' ? Hpre ? ?) "(Hm' & H)". iIntros "[-> HmNew]".
        rewrite (dval_interp_mono E E') //. subst v1.
Dan Frumin's avatar
Dan Frumin committed
623
        iApply (vcg_wp_store_correct with "[-H] H");
624
          eauto using dval_wf_mono, denv_wf_merge, denv_wf_mono.
Dan Frumin's avatar
Dan Frumin committed
625
        rewrite -!denv_merge_interp. iFrame "Hm'".
Dan Frumin's avatar
Dan Frumin committed
626
627
628
629
        rewrite -!(denv_interp_mono E E'); eauto.
      + specialize (vcg_sp'_wf _ _ _ _ _ _ Hmwf Hwf1 Heqsp) as (?&?&?).
        iDestruct (vcg_sp'_correct with "Hm") as "[Hm' Hde1]"; first done. clear Heqsp.
        iDestruct (IHde2 with "Hm' Hwp") as "Hde2"; try done.
630
        iApply (a_store_spec with "Hde1 Hde2"). iIntros "!>" (v1 v2).
631
632
        iIntros "[-> HmNew]". iDestruct 1 as (E' d_new m1 ? ? ?) "(% & Hm' & H)".
        rewrite (dval_interp_mono E E') //. subst v2.
Dan Frumin's avatar
Dan Frumin committed
633
        iApply (vcg_wp_store_correct with "[-H] H");
Robbert Krebbers's avatar
Robbert Krebbers committed
634
          eauto using dval_wf_mono, denv_wf_merge, denv_wf_mono.
Léon Gondelman's avatar
Léon Gondelman committed
635
        rewrite -!denv_merge_interp. iFrame "Hm'".
Dan Frumin's avatar
Dan Frumin committed
636
        rewrite -!(denv_interp_mono E E'); eauto.
Léon Gondelman's avatar
Léon Gondelman committed
637
    - rewrite{1} /vcg_wp; fold vcg_wp.
638
      simpl in Hwf; apply andb_prop_elim in Hwf; destruct Hwf as [Hwf1 Hwf2].
Dan Frumin's avatar
Dan Frumin committed
639
640
      destruct (vcg_sp' E m de1) as [[[m' mNew] dv1]|] eqn:Heqsp; last first.
      + destruct (vcg_sp' E m de2) as [[[m' mNew] dv2]|] eqn:Heqsp2; last first.
641
        { by iApply (vcg_wp_unknown_correct with "Hm Hwp"). }
Dan Frumin's avatar
Dan Frumin committed
642
643
644
645
646
647
        iPoseProof (vcg_sp'_correct) as "Hsp"; first eassumption.
        specialize (vcg_sp'_wf _ _ _ _ _ _ Hmwf Hwf2 Heqsp2) as (?&?&?).
        iDestruct ("Hsp" with "Hm") as "[Hm' Hde2]".
        iClear "Hsp"; clear Heqsp2 Heqsp.
        rewrite IHde1; [| done | done]. iSpecialize ("Hm'" with "Hwp").
        iApply (a_bin_op_spec with "Hm' Hde2"); fold dcexpr_interp.
Dan Frumin's avatar
Dan Frumin committed
648
        iNext. iIntros (v1 v2) "Hex (-> & HmNew)".
649
        iDestruct "Hex" as (E' dv1 m'' Heq Hpre Hm'wf) "(% & Hm' & Hwp)"; simplify_eq /=.
Dan Frumin's avatar
Dan Frumin committed
650
        rewrite (dval_interp_mono E E' dv2); eauto.
Dan Frumin's avatar
Dan Frumin committed
651
        iPoseProof (denv_interp_mono with "Hm'") as "Hm'"; [done | done |].
652
653
654
655
656
657
658
659
        iAssert (( w : val,
                    bin_op_eval b (dval_interp E' dv1) (dval_interp E' dv2) = Some w 
                    vcg_wp_postcondition E' Φ w) -
                w : val, bin_op_eval b (dval_interp E' dv1) (dval_interp E' dv2) = Some w 
                    vcg_wp_postcondition E Φ w)%I as "Hassert".
        iIntros "H". iDestruct "H" as (w) "(H1 & H2)".
        iExists w. iFrame. iApply vcg_wp_postcondition_mono; done.
        iApply "Hassert".
660
        iApply (vcg_wp_bin_op_correct with "Hm' Hwp [HmNew]");
Dan Frumin's avatar
Dan Frumin committed
661
662
          eauto using dval_wf_mono, denv_wf_merge, denv_wf_mono.
        * iApply (denv_interp_mono with "HmNew"); eauto.
Dan Frumin's avatar
Dan Frumin committed
663
664
665
666
667
      + iPoseProof (vcg_sp'_correct) as "Hsp"; first eassumption.
        specialize (vcg_sp'_wf _ _ _ _ _ _ Hmwf Hwf1 Heqsp) as (?&?&?).
        iDestruct ("Hsp" with "Hm") as "[Hm' Hde1]"; iClear "Hsp"; clear Heqsp.
        rewrite IHde2; [| done | done]. iSpecialize ("Hm'" with "Hwp").
        iRename "Hm'" into "Hde2".
Dan Frumin's avatar
Dan Frumin committed
668
669
        iApply (a_bin_op_spec with "Hde1 Hde2");  fold dcexpr_interp.
        iNext. iIntros (v1 v2) "(-> & HmNew) Hex".
670
        iDestruct "Hex" as (E' dv2 m2 Hpre ? Hm'wf) "(% & Hm' & Hwp)"; simplify_eq /=.
Dan Frumin's avatar
Dan Frumin committed
671
        rewrite (dval_interp_mono E E' dv1); eauto.
Dan Frumin's avatar
Dan Frumin committed
672
        iPoseProof (denv_interp_mono with "Hm'") as "Hm'"; [done | done |].
673
674
675
676
677
678
679
680
        iAssert (( w : val,
                    bin_op_eval b (dval_interp E' dv1) (dval_interp E' dv2) = Some w 
                    vcg_wp_postcondition E' Φ w) -
                w : val, bin_op_eval b (dval_interp E' dv1) (dval_interp E' dv2) = Some w 
                    vcg_wp_postcondition E Φ w)%I as "Hassert".
        iIntros "H". iDestruct "H" as (w) "(H1 & H2)".
        iExists w. iFrame. iApply vcg_wp_postcondition_mono; done.
        iApply "Hassert".
681
        iApply (vcg_wp_bin_op_correct with "Hm' Hwp [HmNew]");
Dan Frumin's avatar
Dan Frumin committed
682
683
          eauto using dval_wf_mono, denv_wf_merge, denv_wf_mono.
        * iApply (denv_interp_mono with "HmNew"); eauto.
684
    - rewrite IHde //. iApply a_un_op_spec. iSpecialize ("Hm" with "Hwp").
685
      iApply (awp_wand with "Hm"). iIntros (v) "Hex".
686
      iDestruct "Hex" as (E' dv m' Hpre Hpre' Hm'wf) "(% & Hm & Hwp)".
687
688
      destruct (dun_op_eval E' u dv) as [| dw | dw] eqn:Hop; simpl.
      + iDestruct "Hwp" as (?) "[% _]"; simplify_eq /=.
689
690
      + rewrite Hpre. iExists (dval_interp E' dw); iSplit.
        iPureIntro. eapply dun_op_eval_correct. by rewrite Hop.
691
692
        iExists E', dw, m'. apply dun_op_eval_dSome_wf in Hop; last done.
        eauto with iFrame.
693
694
695
      + iDestruct "Hwp" as (w) "[% Hwp]"; simplify_eq /=.
        iExists (dval_interp E' w). iSplit. iPureIntro.
        apply dun_op_eval_correct. by rewrite Hop.
696
697
698
699
        iExists E', w, m'. apply dun_op_eval_dUnknown_wf in Hop; last done.
        eauto with iFrame.
    - simpl in Hwf; apply andb_prop_elim in Hwf as [Hwf1 Hwf2].
      rewrite IHde1 //. iSpecialize ("Hm" with "Hwp"); fold vcg_wp.
700
701
      iApply (a_sequence_spec' with "[Hm]"); fold dcexpr_interp.
      iNext. iApply (awp_wand with "Hm").  iIntros (v) "Hex".
702
      iDestruct "Hex" as (Enew dv m' Hpre Hpre' Hm'wf) "(% & Hm & Hwp)". simpl.
703
      rewrite denv_unlock_interp.
704
      iModIntro. rewrite IHde2 //. iSpecialize ("Hm" with "Hwp").
705
      specialize (dcexpr_interp_mono E Enew de2 Hwf2 Hpre').
Léon Gondelman's avatar
Léon Gondelman committed
706
      intro Heq; rewrite Heq. iApply (awp_wand with "Hm").
707
708
709
710
711
712
      iIntros (v1) "Hex".
      iDestruct "Hex" as (Ef dvf mf Hpref Hpre3 Hyp4 Hyp5) "(Hm & Hwp)". simpl.
      iExists Ef, dvf, mf; iSplit; first done. iSplit. iPureIntro. trans Enew ;