vcgen.v 44.9 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 denv.
Dan Frumin's avatar
Dan Frumin committed
5
6
From iris_c.c_translation Require Import monad translation proofmode.

7

Dan Frumin's avatar
Dan Frumin committed
8
9
10
Section vcg.
  Context `{amonadG Σ}.

Léon Gondelman's avatar
Léon Gondelman committed
11
12
  Fixpoint mapsto_wand_list_aux
       (E: known_locs) (m : denv) (Φ : iProp Σ) (i : nat) : iProp Σ :=
13
    match m with
14
    | [] => Φ
Léon Gondelman's avatar
Léon Gondelman committed
15
    | None :: m' => mapsto_wand_list_aux E m' Φ (S i)
16
    | Some (DenvItem x q dv) :: m' =>
Léon Gondelman's avatar
Léon Gondelman committed
17
18
      dloc_interp E (dLoc i) C[x]{q} dval_interp E dv -
        mapsto_wand_list_aux E m' Φ (S i)
Léon Gondelman's avatar
Léon Gondelman committed
19
    end%I.
20

Léon Gondelman's avatar
Léon Gondelman committed
21
22
  Definition mapsto_wand_list
       (E: known_locs) (m : denv) (Φ : iProp Σ ) : iProp Σ :=
Léon Gondelman's avatar
Léon Gondelman committed
23
    mapsto_wand_list_aux E m Φ O.
24

25
26
  Definition popstack (ms : list denv) : option (list denv * denv) :=
    match ms with
Dan Frumin's avatar
Dan Frumin committed
27
    | [] => None
28
    | m :: ms => Some (ms, m)
Dan Frumin's avatar
Dan Frumin committed
29
30
    end.

31
  Fixpoint vcg_sp (E: known_locs) (ms : list denv) (de : dcexpr)
Robbert Krebbers's avatar
Robbert Krebbers committed
32
       : option (list denv * denv * dval) :=
33
    match de with
Robbert Krebbers's avatar
Robbert Krebbers committed
34
    | dCRet dv    => Some (ms, [], dv)
35
    | dCLoad de1  =>
Robbert Krebbers's avatar
Robbert Krebbers committed
36
37
38
39
       ''(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)
40
    | dCStore de1 de2 =>
Robbert Krebbers's avatar
Robbert Krebbers committed
41
42
43
44
45
       ''(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)
46
    | dCBinOp op de1 de2 =>
Robbert Krebbers's avatar
Robbert Krebbers committed
47
48
       ''(ms1, mNew1, dv1)  vcg_sp E ms de1;
       ''(ms2, mNew2, dv2)  vcg_sp E ms1 de2;
49
       match dcbin_op_eval E op dv1 dv2 with
Robbert Krebbers's avatar
Robbert Krebbers committed
50
       | dSome dv => Some (ms2, denv_merge mNew1 mNew2, dv)
51
52
       | dNone | dUnknown _ => None
       end
Dan Frumin's avatar
Dan Frumin committed
53
54
55
56
57
    | dCPreBinOp op de1 de2 =>
       ''(ms1, mNew1, dl)  vcg_sp E ms de1;
       i                   is_dloc E dl;
       ''(ms2, mNew2, dv2)  vcg_sp E ms1 de2;
       ''(ms3, mNew3, dv)  denv_delete_full_2 i ms2 (denv_merge mNew1 mNew2);
58
       match dcbin_op_eval E op dv dv2 with
Dan Frumin's avatar
Dan Frumin committed
59
60
61
       | dSome dv3 => Some (ms3, denv_insert i LLvl 1 dv3 mNew3, dv)
       | dNone | dUnknown _ => None
       end
62
    | dCUnOp op de =>
Robbert Krebbers's avatar
Robbert Krebbers committed
63
       ''(ms1, mNew1, dv)  vcg_sp E ms de;
64
       match dun_op_eval E op dv with
Robbert Krebbers's avatar
Robbert Krebbers committed
65
       | dSome dv' => Some (ms1, mNew1, dv')
66
67
       | dNone | dUnknown _ => None
       end
68
    | dCSeq de1 de2 =>
Robbert Krebbers's avatar
Robbert Krebbers committed
69
70
71
72
       ''(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)
Léon Gondelman's avatar
Léon Gondelman committed
73
74
75
76
    | dCPar de1 de2 =>
       ''(ms1, mNew1, dv1)  vcg_sp E ms de1;
       ''(ms2, mNew2, dv2)  vcg_sp E ms1 de2;
         Some (ms2, denv_merge mNew1 mNew2, (dPairV dv1 dv2))
77
    | dCAlloc _ |  dCUnknown _ | dCInvoke _ _ => None
78
79
    end.

Léon Gondelman's avatar
Léon Gondelman committed
80
81
  Definition vcg_sp'
       (E: known_locs) (m : denv) (de : dcexpr) : option (denv * denv * dval) :=
Robbert Krebbers's avatar
Robbert Krebbers committed
82
83
84
    ''(ms,mNew,dv)  vcg_sp E [m] de;
    ''(_, m')  popstack ms;
    Some (m', mNew, dv).
Léon Gondelman's avatar
Léon Gondelman committed
85

Robbert Krebbers's avatar
Robbert Krebbers committed
86
  Definition vcg_wp_continuation (E: known_locs)
87
88
      (Φ : known_locs  denv  dval  iProp Σ) : val  iProp Σ :=
    λ v, ( E' dv m',
Léon Gondelman's avatar
Léon Gondelman committed
89
90
         v = dval_interp E' dv  
         E `prefix_of` E'  
Robbert Krebbers's avatar
Robbert Krebbers committed
91
         denv_wf E' m' 
92
         dval_wf E' dv  
Léon Gondelman's avatar
Léon Gondelman committed
93
        denv_interp E' m' 
Robbert Krebbers's avatar
Robbert Krebbers committed
94
        Φ E' m' dv)%I.
95

Léon Gondelman's avatar
Léon Gondelman committed
96
97
  Definition vcg_wp_awp_continuation (R : iProp Σ) (E: known_locs) (de: dcexpr)
      (m: denv) (Φ : known_locs  denv  dval  iProp Σ) : iProp Σ :=
Robbert Krebbers's avatar
Robbert Krebbers committed
98
    mapsto_wand_list E m (awp (dcexpr_interp E de) R (vcg_wp_continuation E Φ)).
99

100
  Definition vcg_wp_load (E : known_locs) (dv : dval) (m : denv)
Robbert Krebbers's avatar
Robbert Krebbers committed
101
      (Φ : known_locs  denv  dval  iProp Σ) : iProp Σ :=
102
103
104
    match is_dloc E dv with
    | Some i =>
       match denv_lookup i m with
Robbert Krebbers's avatar
Robbert Krebbers committed
105
       | Some (_, dw) => Φ E m dw
106
       | None =>
Léon Gondelman's avatar
Léon Gondelman committed
107
108
109
         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) -
Robbert Krebbers's avatar
Robbert Krebbers committed
110
           vcg_wp_continuation E Φ v))
111
       end
112
    | _ =>
113
114
115
      mapsto_wand_list E m ( (cl : cloc) q v,
        dval_interp E dv = cloc_to_val cl 
        cl C{q} v  (cl C{q} v - vcg_wp_continuation E Φ v))%I
Léon Gondelman's avatar
Léon Gondelman committed
116
    end%I.
117
118

  Definition vcg_wp_store (E : known_locs) (dv1 dv2 : dval) (m : denv)
Robbert Krebbers's avatar
Robbert Krebbers committed
119
      (Φ : known_locs  denv  dval  iProp Σ) : iProp Σ :=
120
121
122
    match is_dloc E dv1 with
    | Some i =>
       match denv_delete_full i m with
Robbert Krebbers's avatar
Robbert Krebbers committed
123
       | Some (m', dw) => Φ E (denv_insert i LLvl 1 dv2 m') dv2
124
       | None =>
Léon Gondelman's avatar
Léon Gondelman committed
125
126
         mapsto_wand_list E m ( v : val,
           dloc_interp E (dLoc i) C v 
Robbert Krebbers's avatar
Robbert Krebbers committed
127
128
           (dloc_interp E (dLoc i) C[LLvl] dval_interp E dv2 -
            vcg_wp_continuation E Φ (dval_interp E dv2)))
129
       end
Robbert Krebbers's avatar
Robbert Krebbers committed
130
    | _ =>
131
132
133
134
       mapsto_wand_list E m ( (cl : cloc) (v : val),
         dval_interp E dv1 = cloc_to_val cl 
         cl C v 
         (cl C[LLvl] dval_interp E dv2 -
Robbert Krebbers's avatar
Robbert Krebbers committed
135
          vcg_wp_continuation E Φ (dval_interp E dv2)))%I
Léon Gondelman's avatar
Léon Gondelman committed
136
    end%I.
137

138
139
  (* DF: the handling of pointer operations is uniform wrt other binary operations, but it doesnt work very well because of the deep embedding of cbin_op_eval *)
  Definition vcg_wp_bin_op (E : known_locs) (op : cbin_op) (dv1 dv2 : dval)
Robbert Krebbers's avatar
Robbert Krebbers committed
140
      (m : denv) (Φ : known_locs  denv  dval  iProp Σ) : iProp Σ :=
141
    match dcbin_op_eval E op dv1 dv2 with
Robbert Krebbers's avatar
Robbert Krebbers committed
142
    | dSome dw => Φ E m dw
143
144
    | mdw =>  v,  dval_interp E <$> (doption_interp mdw) = Some v  
                  mapsto_wand_list E m (vcg_wp_continuation E Φ v)
Léon Gondelman's avatar
Léon Gondelman committed
145
    end%I.
146

147
  Definition vcg_wp_pre_bin_op (E : known_locs) (op : cbin_op) (dv1 dv2 : dval)
Dan Frumin's avatar
Dan Frumin committed
148
149
150
      (m : denv) (Φ : known_locs  denv  dval  iProp Σ) : iProp Σ :=
    match is_dloc E dv1 with
    | Some i =>
151
152
153
154
155
156
157
158
159
160
161
162
163
164
      match denv_delete_full i m with
      | Some (m', dw) =>
        match dcbin_op_eval E op dw dv2 with
        | dSome dw' => Φ E (denv_insert i LLvl 1 dw' m') dw
        | mdw =>  dw', doption_interp mdw = Some dw' 
            mapsto_wand_list E (denv_insert i LLvl 1 dw' m')
              (vcg_wp_continuation E Φ (dval_interp E dw))
        end
      | None => mapsto_wand_list E m ( (v w : val),
                  dloc_interp E (dLoc i) C v 
                  cbin_op_eval op v (dval_interp E dv2) = Some w 
                  (dloc_interp E (dLoc i) C[LLvl] w -
                              vcg_wp_continuation E Φ v))
      end
Dan Frumin's avatar
Dan Frumin committed
165
    | _ =>
166
167
168
169
170
      mapsto_wand_list E m ( (cl : cloc) (v w : val),
        dval_interp E dv1 = cloc_to_val cl 
        cl C v 
        cbin_op_eval op v (dval_interp E dv2) = Some w 
        (cl C[LLvl] w - vcg_wp_continuation E Φ v))
Dan Frumin's avatar
Dan Frumin committed
171
172
    end%I.

173
  Fixpoint vcg_wp (E : known_locs) (m : denv) (de : dcexpr)
Léon Gondelman's avatar
Léon Gondelman committed
174
      (R : iProp Σ) (Φ : known_locs  denv  dval  iProp Σ) : iProp Σ :=
175
    match de with
Robbert Krebbers's avatar
Robbert Krebbers committed
176
    | dCRet dv => Φ E m dv
177
    | dCLoad de1 =>
Robbert Krebbers's avatar
Robbert Krebbers committed
178
       vcg_wp E m de1 R (λ E m' dv, vcg_wp_load E dv m' Φ)
179
    | dCStore de1 de2 =>
Robbert Krebbers's avatar
Robbert Krebbers committed
180
181
182
       match vcg_sp' E m de1 with
       | Some (m', mNew, dv1) =>
          vcg_wp E m' de2 R (λ E m'' dv2,
Robbert Krebbers's avatar
Robbert Krebbers committed
183
            vcg_wp_store E dv1 dv2 (denv_merge mNew m'') Φ)
184
       | None =>
Robbert Krebbers's avatar
Robbert Krebbers committed
185
186
187
          match vcg_sp' E m de2 with
          | Some (m', mNew, dv2) =>
             vcg_wp E m' de1 R (λ E m'' dv1,
Robbert Krebbers's avatar
Robbert Krebbers committed
188
189
               vcg_wp_store E dv1 dv2 (denv_merge mNew m'') Φ)
          | None => vcg_wp_awp_continuation R E de m Φ
190
191
192
          end
        end
    | dCBinOp op de1 de2 =>
Robbert Krebbers's avatar
Robbert Krebbers committed
193
194
195
       match vcg_sp' E m de1 with
       | Some (m', mNew, dv1) =>
          vcg_wp E m' de2 R (λ E m'' dv2,
Robbert Krebbers's avatar
Robbert Krebbers committed
196
            vcg_wp_bin_op E op dv1 dv2 (denv_merge mNew m'') Φ)
197
       | None =>
Robbert Krebbers's avatar
Robbert Krebbers committed
198
199
200
          match vcg_sp' E m de2 with
          | Some (m', mNew, dv2) =>
             vcg_wp E m' de1 R (λ E m'' dv1,
Robbert Krebbers's avatar
Robbert Krebbers committed
201
202
               vcg_wp_bin_op E op dv1 dv2 (denv_merge mNew m'') Φ)
          | None => vcg_wp_awp_continuation R E de m Φ
203
          end
Robbert Krebbers's avatar
Robbert Krebbers committed
204
       end
Dan Frumin's avatar
Dan Frumin committed
205
206
207
208
209
210
211
212
213
214
215
216
217
    | dCPreBinOp op de1 de2 =>
       match vcg_sp' E m de1 with
       | Some (m', mNew, dv1) =>
          vcg_wp E m' de2 R (λ E m'' dv2,
            vcg_wp_pre_bin_op E op dv1 dv2 (denv_merge mNew m'') Φ)
       | None =>
          match vcg_sp' E m de2 with
          | Some (m', mNew, dv2) =>
             vcg_wp E m' de1 R (λ E m'' dv1,
               vcg_wp_pre_bin_op E op dv1 dv2 (denv_merge mNew m'') Φ)
          | None => vcg_wp_awp_continuation R E de m Φ
          end
       end
218
    | dCUnOp op de =>
Robbert Krebbers's avatar
Robbert Krebbers committed
219
       vcg_wp E m de R (λ E m dv,
220
         match dun_op_eval E op dv with
Robbert Krebbers's avatar
Robbert Krebbers committed
221
         | dSome dw => Φ E m dw
Robbert Krebbers's avatar
Robbert Krebbers committed
222
         | mdw =>  dw, doption_interp mdw = Some dw  Φ E m dw
223
224
         end)
    | dCSeq de1 de2 =>
Robbert Krebbers's avatar
Robbert Krebbers committed
225
       vcg_wp E m de1 R (λ E m _,
Léon Gondelman's avatar
Léon Gondelman committed
226
         U (vcg_wp E (denv_unlock m) de2 R Φ))
Léon Gondelman's avatar
Léon Gondelman committed
227
228
229
230
231
232
233
234
235
236
237
238
239
    | dCPar de1 de2 =>
      match vcg_sp' E m de1 with
      | Some (m', mNew, dv1) =>
        vcg_wp E m' de2 R (λ E m'' dv2,
          (Φ E (denv_merge mNew m'') (dPairV dv1 dv2)))
      | None =>
        match vcg_sp' E m de2 with
        | Some (m', mNew, dv2) =>
          vcg_wp E m' de1 R (λ E m'' dv1,
            (Φ E (denv_merge mNew m'') (dPairV dv1 dv2)))
        | None => vcg_wp_awp_continuation R E de m Φ
        end
      end
240
241
242
    | dCInvoke ef de1 => vcg_wp E m de1 R (λ E m dv,
        (vcg_wp_awp_continuation R E
           (dCUnknown (ef (dval_interp E dv))) m Φ))
Robbert Krebbers's avatar
Robbert Krebbers committed
243
    | _ => vcg_wp_awp_continuation R E de m Φ
Léon Gondelman's avatar
Léon Gondelman committed
244
    end%I.
245
246
247
248
249
End vcg.

Section vcg_spec.
  Context `{amonadG Σ}.

Léon Gondelman's avatar
Léon Gondelman committed
250
251
  Lemma mapsto_wand_list_aux_spec E m Φ (k : nat) :
    mapsto_wand_list_aux E m Φ k -
Dan Frumin's avatar
Dan Frumin committed
252
    ([ list] ndio  m, from_option
Léon Gondelman's avatar
Léon Gondelman committed
253
        (λ '{| denv_level := lv; denv_frac := q; denv_dval := dv |},
254
           default inhabitant (E !! (k + n)%nat) C[lv]{q} dval_interp E dv) True dio) - Φ.
Dan Frumin's avatar
Dan Frumin committed
255
  Proof.
Léon Gondelman's avatar
Léon Gondelman committed
256
257
    iIntros "H".
    iInduction m as [|[[x q dv]|]] "IH" forall (k); simpl; first auto.
258
259
    - iIntros "[H1 H2]". rewrite -plus_n_O. iSpecialize ("H" with "H1").
      iApply ("IH" with "H [H2]"). iApply (big_sepL_impl with "H2").
Léon Gondelman's avatar
Léon Gondelman committed
260
261
262
263
264
265
      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
266
267
  Qed.

Léon Gondelman's avatar
Léon Gondelman committed
268
269
  Lemma mapsto_wand_list_spec E m Φ :
    mapsto_wand_list E m Φ - denv_interp E m - Φ.
270
  Proof.
Dan Frumin's avatar
Dan Frumin committed
271
272
273
    unfold mapsto_wand_list, denv_interp.
    iIntros "H1 H2". iApply (mapsto_wand_list_aux_spec with "H1 H2").
  Qed.
274

Dan Frumin's avatar
Dan Frumin committed
275
276
277
  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
278
  Proof.
Dan Frumin's avatar
Dan Frumin committed
279
280
281
    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
282
      destruct dv1 as [?|?|[i|?]|]; simplify_eq/=.
Dan Frumin's avatar
Dan Frumin committed
283
      destruct (denv_delete_frac_2 i ms1 mNew1) as [[[[ms2 mNew2] q] dv1]|] eqn:Hout1; simplify_eq/=.
284
285
286
      transitivity (length ms1).
      + by eapply IHde.
      + by eapply denv_delete_frac_2_length.
Dan Frumin's avatar
Dan Frumin committed
287
    - destruct (vcg_sp E ms de1) as [[[ms1 mNew1] dv1]|] eqn:Hde1; simplify_eq /=.
Dan Frumin's avatar
Dan Frumin committed
288
      destruct dv1 as [?|?|[i|?]|]; simplify_eq/=.
Dan Frumin's avatar
Dan Frumin committed
289
290
      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/=.
291
292
293
294
      transitivity (length ms1).
      + by eapply IHde1.
      + transitivity (length ms2). by eapply IHde2.
        by eapply denv_delete_full_2_length.
Dan Frumin's avatar
Dan Frumin committed
295
    - destruct (vcg_sp E ms de1) as [[[ms1 mNew1] dv1]|] eqn:Hde1; simplify_eq/=.
296
297
      destruct (vcg_sp E ms1 de2) as [[[ms2 mNew2] dv2]|] eqn:Hde2;
      simplify_eq/=.
298
      destruct (dcbin_op_eval E c dv1 dv2) eqn:Hboe; simplify_eq/=.
299
      transitivity (length ms1); eauto.
Dan Frumin's avatar
Dan Frumin committed
300
    - destruct (vcg_sp E ms de1) as [[[ms1 mNew1] dv1]|] eqn:Hde1; simplify_eq/=.
Dan Frumin's avatar
Dan Frumin committed
301
      destruct dv1 as [?|?|?|dv1]; try destruct dv1; simplify_eq/=.
Dan Frumin's avatar
Dan Frumin committed
302
      destruct d as [i|?]; simplify_eq/=.
303
304
305
306
      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/=.
307
      destruct (dcbin_op_eval E c dv1 dv2) eqn:Hboe; simplify_eq/=.
Dan Frumin's avatar
Dan Frumin committed
308
309
      transitivity (length ms1); eauto.
      transitivity (length ms2); eauto using denv_delete_full_2_length.
Dan Frumin's avatar
Dan Frumin committed
310
    - destruct (vcg_sp E ms de) as [[[ms1 mNew1] dv1]|] eqn:Hde; simplify_eq/=.
Dan Frumin's avatar
Dan Frumin committed
311
312
      destruct (dun_op_eval E u dv1); simplify_eq/=.
      by eapply IHde.
Dan Frumin's avatar
Dan Frumin committed
313
    - destruct (vcg_sp E ms de1) as [[[ms1 mNew1] dv1]|] eqn:Hde1; simplify_eq/=.
Léon Gondelman's avatar
Léon Gondelman committed
314
315
      destruct (vcg_sp E (denv_unlock mNew1 :: ms1) de2)
      as [[[ms2 mNew2] dv2]|] eqn:Hde2; simplify_eq/=.
316
317
318
319
320
321
322
      destruct ms2; simplify_eq/=. transitivity (length ms1).
      + by eapply IHde1.
      + apply IHde2 in Hde2; by simplify_eq/=.
    - 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/=.
Léon Gondelman's avatar
Léon Gondelman committed
323
      transitivity (length ms1); eauto.
Dan Frumin's avatar
Dan Frumin committed
324
325
  Qed.

Dan Frumin's avatar
Dan Frumin committed
326
327
328
  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
329
330
       (awp (dcexpr_interp E de) R
          (λ v, v = dval_interp E dv  denv_interp E mNew)))%I.
331
  Proof.
Dan Frumin's avatar
Dan Frumin committed
332
333
    revert ms ms' mNew dv. induction de;
    iIntros (ms ms' mNew dv Hsp); simplify_eq/=.
Dan Frumin's avatar
Dan Frumin committed
334
335
    - iFrame. iApply denv_stack_interp_intro.
      iApply awp_ret. wp_value_head. iSplit; eauto. rewrite /denv_interp //.
Dan Frumin's avatar
Dan Frumin committed
336
337
    - specialize (IHde ms).
      destruct (vcg_sp E ms de) as [[[ms1 mNew1] dv1]|]; simplify_eq /=.
Dan Frumin's avatar
Dan Frumin committed
338
      destruct dv1 as [?|?|[i|?]|]; simplify_eq/=.
Dan Frumin's avatar
Dan Frumin committed
339
340
      destruct (denv_delete_frac_2 i ms1 mNew1) as [[[[ms2 mNew2] q] dv1]|] eqn:Hfar; simplify_eq/=.
      iPoseProof IHde as "Hawp"; first done.
341
      iPoseProof denv_delete_frac_2_interp as "Hm"; first eassumption.
Dan Frumin's avatar
Dan Frumin committed
342
343
344
      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
345
      iIntros "[Hawp Hm]".
Léon Gondelman's avatar
Léon Gondelman committed
346
347
      iApply a_load_spec.
      iApply (awp_wand with "Hawp").
Dan Frumin's avatar
Dan Frumin committed
348
      iIntros (?) "[% HmNew1]". simplify_eq/=.
Léon Gondelman's avatar
Léon Gondelman committed
349
350
      iExists _, _. iSplit; eauto.
      iDestruct ("Hm" with "HmNew1") as "[HmNew2 $]".
Dan Frumin's avatar
Dan Frumin committed
351
      iIntros "Hl". iSplit; eauto.
352
353
      rewrite -denv_insert_interp. by iFrame.
      - specialize (IHde1 ms).
Dan Frumin's avatar
Dan Frumin committed
354
      destruct (vcg_sp E ms de1) as [[[ms1 mNew1] dv1]|]; simplify_eq /=.
Dan Frumin's avatar
Dan Frumin committed
355
      destruct dv1 as [?|?|[i|?]|]; simplify_eq/=.
Dan Frumin's avatar
Dan Frumin committed
356
357
358
359
360
361
362
363
364
365
      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
366
367
368
369
370
      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
371
372
373
      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
374
      iIntros "Hl". by iFrame.
Dan Frumin's avatar
Dan Frumin committed
375
376
377
378
    - 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 /=.
379
      destruct (dcbin_op_eval E c dv1 dv2) eqn:Hboe; simplify_eq/=.
Dan Frumin's avatar
Dan Frumin committed
380
381
      iPoseProof IHde1 as "Hawp1"; first done.
      iPoseProof IHde2 as "Hawp2"; first done.
Dan Frumin's avatar
Dan Frumin committed
382
      iDestruct (denv_stack_interp_trans with "Hawp1 Hawp2") as "Hawp".
Dan Frumin's avatar
Dan Frumin committed
383
      iClear "Hawp1 Hawp2".
Dan Frumin's avatar
Dan Frumin committed
384
      iApply (denv_stack_interp_mono with "Hawp"). iIntros "[Hawp1 Hawp2]".
Léon Gondelman's avatar
Léon Gondelman committed
385
      iApply (a_bin_op_spec with "Hawp1 Hawp2").
Dan Frumin's avatar
Dan Frumin committed
386
      iNext. iIntros (? ?) "[% HmNew1] [% HmNew2]"; simplify_eq/=.
Léon Gondelman's avatar
Léon Gondelman committed
387
      iExists (dval_interp E dv). repeat iSplit; eauto.
388
      + iPureIntro. apply dcbin_op_eval_correct. by rewrite Hboe.
Léon Gondelman's avatar
Léon Gondelman committed
389
      + rewrite -denv_merge_interp. iFrame.
Dan Frumin's avatar
Dan Frumin committed
390
391
    - specialize (IHde1 ms).
      destruct (vcg_sp E ms de1) as [[[ms1 mNew1] dv1]|]; simplify_eq /=.
Dan Frumin's avatar
Dan Frumin committed
392
      destruct dv1 as [?|?|d|?]; simplify_eq/=.
Dan Frumin's avatar
Dan Frumin committed
393
394
395
396
397
      destruct d as [i|?]; simplify_eq/=.
      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] dv'] |] eqn:Hfar; simplify_eq/=.
398
      destruct (dcbin_op_eval E c dv' dv2) eqn:Hboe; simplify_eq/=.
Dan Frumin's avatar
Dan Frumin committed
399
400
401
402
403
404
405
406
407
408
409
410
411
      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 Hawp' Hl".
      iApply (denv_stack_interp_mono with "Hawp"). iIntros "[[Hawp1 Hawp2] Hl]".
      iApply (a_pre_bin_op_spec with "Hawp1 Hawp2").
      iNext. iIntros (? ?) "[% HmNew1] [% HmNew2] HR"; simplify_eq/=.
      iCombine "HmNew1 HmNew2" as "HmNew". rewrite denv_merge_interp.
      iExists (dloc_interp E (dLoc i)), (dval_interp E dv), (dval_interp E d).
      iDestruct ("Hl" with "HmNew") as "[HmNew $]".
      repeat iSplit; eauto.
412
      + iPureIntro. apply dcbin_op_eval_correct. by rewrite Hboe.
Dan Frumin's avatar
Dan Frumin committed
413
414
      + iIntros "Hl". iFrame. iSplit; first done.
        rewrite -denv_insert_interp. by iFrame.
Dan Frumin's avatar
Dan Frumin committed
415
416
    - specialize (IHde ms).
      destruct (vcg_sp E ms de) as [[[ms1 mNew1] dv1]|]; simplify_eq /=.
417
      remember (dun_op_eval E u dv1) as Hu; destruct Hu; simplify_eq/=.
Dan Frumin's avatar
Dan Frumin committed
418
419
      iPoseProof IHde as "Hawp"; first done.
      iApply (denv_stack_interp_mono with "Hawp"). iClear "Hawp".
Dan Frumin's avatar
Dan Frumin committed
420
      iIntros "Hawp".
421
422
423
424
      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
425
426
427
428
429
430
    - 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
431
      unfold popstack in Hsp.
Dan Frumin's avatar
Dan Frumin committed
432
      destruct ms2 as [|t ms2'] eqn:Houteq; simplify_eq/=.
Dan Frumin's avatar
Dan Frumin committed
433
      iDestruct (denv_stack_interp_trans with "Hawp1 Hawp2") as "Hawp".
Dan Frumin's avatar
Dan Frumin committed
434
      iClear "Hawp1 Hawp2".
Dan Frumin's avatar
Dan Frumin committed
435
      iApply (denv_stack_interp_mono with "Hawp"). iIntros "[Hawp1 Hawp2]".
436
      iApply a_sequence_spec'. iNext. iApply (awp_wand with "Hawp1").
Dan Frumin's avatar
Dan Frumin committed
437
      iIntros (?) "[% HmNew1]". simplify_eq/=. iClear "Hawp".
Dan Frumin's avatar
Dan Frumin committed
438
      rewrite (denv_unlock_interp E mNew1).
439
      iModIntro. iDestruct ("Hawp2" with "HmNew1") as "[HmNew' Hawp2]".
Dan Frumin's avatar
Dan Frumin committed
440
441
      iApply (awp_wand with "Hawp2"). iIntros (?) "[% HmNew2]".
      rewrite -denv_merge_interp. iSplit; eauto with iFrame.
Léon Gondelman's avatar
Léon Gondelman committed
442
443
444
445
446
447
448
449
450
451
    - 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 /=.
      iPoseProof IHde1 as "Hawp1"; first done.
      iPoseProof IHde2 as "Hawp2"; first done.
      iDestruct (denv_stack_interp_trans with "Hawp1 Hawp2") as "Hawp".
      iClear "Hawp1 Hawp2".
      iApply (denv_stack_interp_mono with "Hawp"). iIntros "[Hawp1 Hawp2]".
      iApply (awp_par with "Hawp1 Hawp2").
452
453
      iNext. iIntros (? ?) "[% HmNew1] [% HmNew2] !>";
      simplify_eq/=; iSplit; eauto.
Léon Gondelman's avatar
Léon Gondelman committed
454
      rewrite -denv_merge_interp. iFrame.
455
  Qed.
Dan Frumin's avatar
Dan Frumin committed
456

Dan Frumin's avatar
Dan Frumin committed
457
458
  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
459
    denv_interp E m -
Léon Gondelman's avatar
Léon Gondelman committed
460
461
462
      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
463
  Proof.
464
    iIntros (Hsp) "Hm".
Dan Frumin's avatar
Dan Frumin committed
465
466
467
468
469
    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
470
  Qed.
Léon Gondelman's avatar
Léon Gondelman committed
471

Dan Frumin's avatar
Dan Frumin committed
472
473
474
  Lemma vcg_sp'_correct E de m m' mNew dv R :
    vcg_sp' E m de = Some (m', mNew, dv) 
    denv_interp E m -
Léon Gondelman's avatar
Léon Gondelman committed
475
476
477
      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
478
  Proof.
Dan Frumin's avatar
Dan Frumin committed
479
480
481
482
483
484
485
486
    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
487
488
  Qed.

Dan Frumin's avatar
Dan Frumin committed
489
490
  Lemma vcg_sp_wf' E de ms ms' mNew dv :
    Forall (denv_wf E) ms 
Dan Frumin's avatar
Dan Frumin committed
491
    dcexpr_wf E de 
Dan Frumin's avatar
Dan Frumin committed
492
493
    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
494
  Proof.
Dan Frumin's avatar
Dan Frumin committed
495
496
497
    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
498
      destruct dv1 as [|?|[i|?]|]; simplify_eq/=.
Dan Frumin's avatar
Dan Frumin committed
499
500
501
502
      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
503
      repeat split; eauto using denv_wf_insert.
Dan Frumin's avatar
Dan Frumin committed
504
    - destruct (vcg_sp E ms de1) as [[[ms1 mNew1] dv1]|] eqn:Hsp1; simplify_eq /=.
Dan Frumin's avatar
Dan Frumin committed
505
      destruct dv1 as [?|?|[i|?]|]; simplify_eq/=.
Dan Frumin's avatar
Dan Frumin committed
506
507
      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
508
      apply andb_True in Hwfde. destruct Hwfde as [Hwfde1 Hwfde2].
Dan Frumin's avatar
Dan Frumin committed
509
510
511
512
      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
513
514
      repeat split; eauto using denv_wf_insert.
      eauto using denv_wf_merge.
515
516
    - destruct (vcg_sp E ms de1) as [[[ms1 mNew1] dv1]|] eqn:Hsp1;
      simplify_eq/=.
Léon Gondelman's avatar
Léon Gondelman committed
517
518
      destruct (vcg_sp E ms1 de2) as [[[ms2 mNew2] dv2]|] eqn:Hsp2;
      simplify_eq/=.
519
      destruct (dcbin_op_eval E c dv1 dv2) eqn:Hboe; simplify_eq/=.
Dan Frumin's avatar
Dan Frumin committed
520
      apply andb_True in Hwfde. destruct Hwfde as [Hwfde1 Hwfde2].
Dan Frumin's avatar
Dan Frumin committed
521
522
      destruct (IHde1 _ _ _ _ Hwfms Hwfde1 Hsp1) as (Hwfms1&HwfNew1&Hwfdv1).
      destruct (IHde2 _ _ _ _ Hwfms1 Hwfde2 Hsp2) as (?&?&?).
Dan Frumin's avatar
Dan Frumin committed
523
      repeat split; eauto. apply denv_wf_merge; eauto.
524
      eapply (dcbin_op_eval_dSome_wf _ dv1 dv2); eauto.
525
526
    - destruct (vcg_sp E ms de1) as [[[ms1 mNew1] dv1]|] eqn:Hsp1;
      simplify_eq/=.
Dan Frumin's avatar
Dan Frumin committed
527
      destruct dv1 as [|?|d|]; simplify_eq/=.
Dan Frumin's avatar
Dan Frumin committed
528
      destruct d as [i|?]; simplify_eq/=.
529
530
531
532
      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
533
      apply andb_True in Hwfde. destruct Hwfde as [Hwfde1 Hwfde2].
534
      destruct (dcbin_op_eval E c dv1 dv2) eqn:Hboe; simplify_eq/=.
Dan Frumin's avatar
Dan Frumin committed
535
536
537
538
539
540
      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 (?&?&?).
      repeat split; eauto using denv_wf_insert, denv_wf_merge.
      eapply denv_wf_insert; eauto.
541
      eapply (dcbin_op_eval_dSome_wf _ dv dv2); eauto.
Dan Frumin's avatar
Dan Frumin committed
542
      by eapply denv_wf_merge.
543

Dan Frumin's avatar
Dan Frumin committed
544
545
    - 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
546
547
548
      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
549
    - destruct (vcg_sp E ms de1) as [[[ms1 mNew1] dv1]|] eqn:Hsp1; simplify_eq/=.
Léon Gondelman's avatar
Léon Gondelman committed
550
551
      destruct (vcg_sp E (denv_unlock mNew1 :: ms1) de2)
        as [[[ms2 mNew2] dv2]|] eqn:Hsp2; simplify_eq/=.
Dan Frumin's avatar
Dan Frumin committed
552
      destruct ms2 as [|t ms2]; simplify_eq/=.
Dan Frumin's avatar
Dan Frumin committed
553
      apply andb_True in Hwfde. destruct Hwfde as [Hwfde1 Hwfde2].
Dan Frumin's avatar
Dan Frumin committed
554
555
      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
556
      { apply Forall_cons. split; eauto. by apply denv_wf_unlock. }
Dan Frumin's avatar
Dan Frumin committed
557
      destruct (IHde2 _ _ _ _ Hwfms2 Hwfde2 Hsp2) as (Hall&?&?).
Dan Frumin's avatar
Dan Frumin committed
558
559
      apply Forall_cons in Hall. destruct Hall.
      repeat split; eauto using denv_wf_merge.
Léon Gondelman's avatar
Léon Gondelman committed
560
561
562
563
564
565
566
    - 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/=.
      apply andb_True in Hwfde. destruct Hwfde as [Hwfde1 Hwfde2].
      destruct (IHde1 _ _ _ _ Hwfms Hwfde1 Hsp1) as (Hwfms1&HwfNew1&Hwfdv1).
      destruct (IHde2 _ _ _ _ Hwfms1 Hwfde2 Hsp2) as (?&?&?).
      repeat split; eauto. apply denv_wf_merge; eauto.
Dan Frumin's avatar
Dan Frumin committed
567
  Qed.
Dan Frumin's avatar
Dan Frumin committed
568

Dan Frumin's avatar
Dan Frumin committed
569
  Lemma vcg_sp'_wf E de m m' mNew dv :
570
571
    denv_wf E m 
    dcexpr_wf E de 
Dan Frumin's avatar
Dan Frumin committed
572
573
    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
574
  Proof.
Dan Frumin's avatar
Dan Frumin committed
575
576
577
578
579
580
581
582
    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
583
  Qed.
584

Robbert Krebbers's avatar
Robbert Krebbers committed
585
  Lemma vcg_wp_awp_continuation_correct R E m de Φ :
586
    denv_wf E m 
587
    denv_interp E m -
Robbert Krebbers's avatar
Robbert Krebbers committed
588
589
    vcg_wp_awp_continuation R E de m Φ -
    awp (dcexpr_interp E de) R (vcg_wp_continuation E Φ).
590
  Proof.
Robbert Krebbers's avatar
Robbert Krebbers committed
591
    rewrite /vcg_wp_awp_continuation mapsto_wand_list_spec.
592
    iIntros (Hmwf) "Hm Hwp". iSpecialize ("Hwp" with "Hm").
593
    iApply (awp_wand with "Hwp"). iIntros (v) "H".
594
    iDestruct "H" as (E' dv m' ->) "(% & % & Hm & Hwp)".
595
    iExists E', dv, m'. repeat (iSplit; first done); iFrame.
596
597
  Qed.

598
  Lemma vcg_wp_load_correct E m dv Φ :
599
    denv_wf E m 
600
    denv_interp E m -
Robbert Krebbers's avatar
Robbert Krebbers committed
601
    vcg_wp_load E dv m Φ -
602
603
     (cl : cloc) q w, dval_interp E dv = cloc_to_val cl 
       cl C{q} w  (cl C{q} w - vcg_wp_continuation E Φ w).
604
605
606
  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
607
      * destruct (denv_lookup_interp E i q dv' m) as [m0 Hm0]; first assumption.
Léon Gondelman's avatar
Léon Gondelman committed
608
609
        rewrite Hm0. iIntros (Hmwf) "[Hi Hm0] HΦ".
        apply is_dloc_some in Hdloc. simplify_eq/=.
610
611
612
613
        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.
614
615
      * rewrite mapsto_wand_list_spec. iIntros (Hmwf) "Hm Hwp".
        iSpecialize ("Hwp" with "Hm"). simpl.
616
        iDestruct "Hwp" as (q v') "[Hl Hwp]". apply is_dloc_some in Hdloc. subst.
617
        iExists (dloc_interp E (dLoc i)), q, v'; iSplit; first done. iFrame.
618
619
    +  rewrite mapsto_wand_list_spec. iIntros (Hmwf) "Hm Hwp".
       iSpecialize ("Hwp" with "Hm"); simpl.
620
       iDestruct "Hwp" as (l q v' ->) "[Hl Hwp]".
Robbert Krebbers's avatar
Robbert Krebbers committed
621
       iExists l, q, v'. iSplit; first done. iFrame.
622
623
  Qed.

624
  Lemma vcg_wp_bin_op_correct E0 E m mOut dv1 dv2 b Φ :
625
    E0 `prefix_of` E  dval_wf E dv1  dval_wf E dv2 
626
627
    denv_wf E (denv_merge mOut m) 
    denv_interp E m -
Robbert Krebbers's avatar
Robbert Krebbers committed
628
    vcg_wp_bin_op E b dv1 dv2 (denv_merge mOut m) Φ -
629
    denv_interp E mOut -
630
     w : val, cbin_op_eval b (dval_interp E dv1) (dval_interp E dv2) = Some w 
Robbert Krebbers's avatar
Robbert Krebbers committed
631
               vcg_wp_continuation E Φ w.
632
  Proof.
633
    iIntros (Hpre Hwf1 Hwf2 Hwf3) "Hm Hwp HmOut". rewrite /vcg_wp_bin_op.
634
    destruct (dcbin_op_eval E b dv1 dv2) as [ | dw | dw ] eqn:Hbin.
Léon Gondelman's avatar
Léon Gondelman committed
635
    * iDestruct "Hwp" as (dw Hopt) "Hcont"; simplify_eq.
636
    * iExists (dval_interp E dw); iSplit.
637
      { iPureIntro. apply dcbin_op_eval_correct. by rewrite Hbin. }
638
      iExists E, dw, (denv_merge mOut m).
639
      apply dcbin_op_eval_dSome_wf in Hbin; try done.
640
      rewrite -denv_merge_interp //. eauto with iFrame.
641
    * iDestruct "Hwp" as (w0 Hopt) "Hcont"; simplify_eq.
Léon Gondelman's avatar
Léon Gondelman committed
642
      destruct dw as [dw1|] eqn:Hdw; last done.
643
644
645
646
647
648
      iCombine "HmOut Hm" as "Hm". rewrite denv_merge_interp.
      rewrite mapsto_wand_list_spec. iSpecialize ("Hcont" with "Hm").
      iExists w0; iSplit; last done.
      iPureIntro. rewrite -Hbin in Hopt.
      destruct (doption_interp (dcbin_op_eval E b dv1 dv2)) as [dw0|] eqn:Hdw0; simplify_eq/=.
      apply dcbin_op_eval_correct. by rewrite Hdw0.
Léon Gondelman's avatar
Léon Gondelman committed
649
  Qed.
650

Dan Frumin's avatar
Dan Frumin committed
651
652
653
654
655
  Lemma vcg_wp_pre_bin_op_correct E m op dv1 dv2 Φ :
    denv_wf E m 
    dval_wf E dv2 
    denv_interp E m -
    vcg_wp_pre_bin_op E op dv1 dv2 m Φ -
Dan Frumin's avatar
Dan Frumin committed
656
     l v w, l C v  dval_interp E dv1 = cloc_to_val l 
657
      cbin_op_eval op v (dval_interp E dv2) = Some w 
658
      (l C[LLvl] w - vcg_wp_continuation E Φ v).
Dan Frumin's avatar
Dan Frumin committed
659
660
661
662
663
664
665
666
667
668
669
  Proof.
    iIntros (? ?) "Hm Hwp". rewrite /vcg_wp_pre_bin_op.
    destruct (is_dloc E dv1) as [i|] eqn:Hloc; last first.
    { rewrite mapsto_wand_list_spec.
      iDestruct ("Hwp" with "Hm") as (l v w) "(%&Hl&%&Hwp)".
      iExists l,v,w. iFrame. eauto. }
    apply is_dloc_some in Hloc. simplify_eq/=.
    destruct (denv_delete_full i m) as [[m' dw] | ] eqn:Hdel; last first.
    - rewrite mapsto_wand_list_spec.
      iDestruct ("Hwp" with "Hm") as (v w) "(Hl&%&Hwp)".
      iExists (dloc_interp E (dLoc i)),v,w. iFrame. eauto.
670
671
    - iDestruct (denv_delete_full_interp E with "Hm") as "[Hm' Hl]";
      first eassumption.
672
      destruct (dcbin_op_eval E op dw dv2) as [|dw'|] eqn:Hop.
Dan Frumin's avatar
Dan Frumin committed
673
      { iDestruct "Hwp" as (dw') "[% Hwp]".
674
675
        iExists (dloc_interp E (dLoc i)),
        (dval_interp E dw), (dval_interp E dw'). iFrame.
Dan Frumin's avatar
Dan Frumin committed
676
677
678
        repeat iSplit; eauto.
        iIntros "Hl". iCombine "Hm' Hl" as "Hm'". rewrite denv_insert_interp.
        rewrite mapsto_wand_list_spec. by iApply "Hwp". }
679
680
      { iExists (dloc_interp E (dLoc i)),
        (dval_interp E dw), (dval_interp E dw'). iFrame.
Dan Frumin's avatar
Dan Frumin committed
681
        repeat iSplit; eauto; try iPureIntro.
682
        - apply dcbin_op_eval_correct. by rewrite Hop.
Dan Frumin's avatar
Dan Frumin committed
683
684
685
        - iIntros "Hl". iCombine "Hm' Hl" as "Hm'". rewrite denv_insert_interp.
          rewrite /vcg_wp_continuation. iExists E,dw,_. iFrame.
          eapply denv_wf_delete_full in Hdel; eauto. destruct_and!.
686
          apply dcbin_op_eval_dSome_wf in Hop; eauto.
Dan Frumin's avatar
Dan Frumin committed
687
688
          repeat iSplit; eauto using denv_wf_insert. }
      { iDestruct "Hwp" as (dw') "[% Hwp]".
689
690
        iExists (dloc_interp E (dLoc i)), (dval_interp E dw),
        (dval_interp E dw'). iFrame.
Dan Frumin's avatar
Dan Frumin committed
691
        repeat iSplit; eauto.
692
        - iPureIntro. apply dcbin_op_eval_correct. by rewrite Hop.
Dan Frumin's avatar
Dan Frumin committed
693
694
695
696
        - iIntros "Hl". iCombine "Hm' Hl" as "Hm'". rewrite denv_insert_interp.
          rewrite mapsto_wand_list_spec. by iApply "Hwp". }
  Qed.

697
 Lemma vcg_wp_store_correct E m dv1 dv2 Φ :
Robbert Krebbers's avatar
Robbert Krebbers committed
698
699
700
701
    denv_wf E m 
    dval_wf E dv2 
    denv_interp E m -
    vcg_wp_store E dv1 dv2 m Φ -
702
703
704
705
     (cl : cloc) (w : val),
      dval_interp E dv1 = cloc_to_val cl 
      cl C w 
      (cl C[LLvl] dval_interp E dv2 - vcg_wp_continuation E Φ (dval_interp E dv2)).
Robbert Krebbers's avatar
Robbert Krebbers committed
706
707
708
709
710
  Proof.
    iIntros (Hwf Hwf2) "Hm Hwp". rewrite{1} /vcg_wp_store; fold vcg_wp.
    destruct (is_dloc E dv1) as [i|] eqn:Hdloc.
    - apply is_dloc_some in Hdloc; rewrite Hdloc.
      destruct (denv_delete_full i m)  as [[m' dv_old]|] eqn:Hdel.
Léon Gondelman's avatar
Léon Gondelman committed
711
712
      + iExists (dloc_interp E (dLoc i)), (dval_interp E dv_old);
        iSplit; first done.
713
        iPoseProof (denv_delete_full_interp E) as "Hdel". eassumption.
Dan Frumin's avatar
Dan Frumin committed
714
        iSpecialize ("Hdel" with "Hm"). iDestruct "Hdel" as "[HmDel Hl]"; iFrame.
715
716
717
        iIntros "Hl".
        iExists E, dv2, (denv_insert i LLvl 1 dv2 m');
          repeat (iSplit; first done).
718
        iSplit. iPureIntro. apply denv_wf_insert; last done.
Dan Frumin's avatar
Dan Frumin committed
719
        by destruct (denv_wf_delete_full E dv_old i m m' Hwf Hdel) as [Hdelwf ?].
Robbert Krebbers's avatar
Robbert Krebbers committed
720
721
722
723
724
725
726
727
728
        rewrite -denv_insert_interp. eauto with iFrame.
      + rewrite mapsto_wand_list_spec.
        iSpecialize ("Hwp" with "[Hm]"); iFrame.
        iDestruct "Hwp" as (dv_old) "[Hl Hwp]";
        iExists (dloc_interp E (dLoc i)), dv_old; iSplit; first done. iFrame.
    - rewrite mapsto_wand_list_spec.
      iSpecialize ("Hwp" with "[Hm]"); iFrame.
  Qed.

729

Robbert Krebbers's avatar
Robbert Krebbers committed
730
731
732
733
734
735
736
737
738
739
  Lemma vcg_wp_continuation_mono E E' Φ w:
    E `prefix_of` E'  vcg_wp_continuation E' Φ w - vcg_wp_continuation E Φ w.
  Proof.
    iIntros (Hpre) "Hp".
    iDestruct "Hp" as (E1 dv