vcgen.v 33.3 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 Φ))
Léon Gondelman's avatar
Léon Gondelman committed
199
    | _ => vcg_wp_unknown R E de m Φ
Léon Gondelman's avatar
Léon Gondelman committed
200
    end%I.
201
202
203
204
205
End vcg.

Section vcg_spec.
  Context `{amonadG Σ}.

Léon Gondelman's avatar
Léon Gondelman committed
206
207
  Lemma mapsto_wand_list_aux_spec E m Φ (k : nat) :
    mapsto_wand_list_aux E m Φ k -
Dan Frumin's avatar
Dan Frumin committed
208
    ([ list] ndio  m, from_option
Léon Gondelman's avatar
Léon Gondelman committed
209
210
        (λ '{| 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
211
  Proof.
212
213
214
215
216
217
    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
218
219
  Qed.

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

Dan Frumin's avatar
Dan Frumin committed
227
228
229
  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
230
  Proof.
Dan Frumin's avatar
Dan Frumin committed
231
232
233
    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
234
235
      destruct dv1 as [dv1|dv1]; destruct dv1; simplify_eq/=.
      destruct d as [i|?]; simplify_eq/=.
Dan Frumin's avatar
Dan Frumin committed
236
237
      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
238
      + by eapply IHde.
Dan Frumin's avatar
Dan Frumin committed
239
240
      + 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
241
242
      destruct dv1 as [dv1|dv1]; destruct dv1; simplify_eq/=.
      destruct d as [i|?]; simplify_eq/=.
Dan Frumin's avatar
Dan Frumin committed
243
244
245
      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
246
      + by eapply IHde1.
Dan Frumin's avatar
Dan Frumin committed
247
248
249
250
      + 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
251
      destruct (dbin_op_eval E b dv1 dv2) eqn:Hboe; simplify_eq/=.
252
      transitivity (length ms1); eauto.
Dan Frumin's avatar
Dan Frumin committed
253
    - destruct (vcg_sp E ms de) as [[[ms1 mNew1] dv1]|] eqn:Hde; simplify_eq/=.
Dan Frumin's avatar
Dan Frumin committed
254
255
      destruct (dun_op_eval E u dv1); simplify_eq/=.
      by eapply IHde.
Dan Frumin's avatar
Dan Frumin committed
256
257
258
259
    - 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
260
261
262
263
      + by eapply IHde1.
      + apply IHde2 in Hde2. by simplify_eq/=.
  Qed.

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

Dan Frumin's avatar
Dan Frumin committed
358
359
  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
360
    denv_interp E m -
Dan Frumin's avatar
Dan Frumin committed
361
    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
362
  Proof.
363
    iIntros (Hsp) "Hm".
Dan Frumin's avatar
Dan Frumin committed
364
365
366
367
368
    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
369
  Qed.
Léon Gondelman's avatar
Léon Gondelman committed
370

Dan Frumin's avatar
Dan Frumin committed
371
372
373
374
  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
375
  Proof.
Dan Frumin's avatar
Dan Frumin committed
376
377
378
379
380
381
382
383
    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
384
385
  Qed.

Dan Frumin's avatar
Dan Frumin committed
386
387
  Lemma vcg_sp_wf' E de ms ms' mNew dv :
    Forall (denv_wf E) ms 
Dan Frumin's avatar
Dan Frumin committed
388
    dcexpr_wf E de 
Dan Frumin's avatar
Dan Frumin committed
389
390
    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
391
  Proof.
Dan Frumin's avatar
Dan Frumin committed
392
393
394
    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
395
396
      destruct dv1 as [dv1|dv1]; destruct dv1; simplify_eq/=.
      destruct d as [i|?]; simplify_eq/=.
Dan Frumin's avatar
Dan Frumin committed
397
398
399
400
      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
401
      repeat split; eauto using denv_wf_insert.
Dan Frumin's avatar
Dan Frumin committed
402
    - destruct (vcg_sp E ms de1) as [[[ms1 mNew1] dv1]|] eqn:Hsp1; simplify_eq /=.
Dan Frumin's avatar
Dan Frumin committed
403
404
      destruct dv1 as [dv1|dv1]; destruct dv1; simplify_eq/=.
      destruct d as [i|?]; simplify_eq/=.
Dan Frumin's avatar
Dan Frumin committed
405
406
      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
407
      apply andb_True in Hwfde. destruct Hwfde as [Hwfde1 Hwfde2].
Dan Frumin's avatar
Dan Frumin committed
408
409
410
411
      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
412
413
      repeat split; eauto using denv_wf_insert.
      eauto using denv_wf_merge.
Dan Frumin's avatar
Dan Frumin committed
414
415
    - 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
416
417
      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
418
419
      destruct (IHde1 _ _ _ _ Hwfms Hwfde1 Hsp1) as (Hwfms1&HwfNew1&Hwfdv1).
      destruct (IHde2 _ _ _ _ Hwfms1 Hwfde2 Hsp2) as (?&?&?).
Dan Frumin's avatar
Dan Frumin committed
420
421
      repeat split; eauto. apply denv_wf_merge; eauto.
      eapply (dbin_op_eval_dSome_wf _ dv1 dv2); eauto.
Dan Frumin's avatar
Dan Frumin committed
422
423
    - 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
424
425
426
      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
427
428
429
    - 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
430
      apply andb_True in Hwfde. destruct Hwfde as [Hwfde1 Hwfde2].
Dan Frumin's avatar
Dan Frumin committed
431
432
      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
433
      { apply Forall_cons. split; eauto. by apply denv_wf_unlock. }
Dan Frumin's avatar
Dan Frumin committed
434
      destruct (IHde2 _ _ _ _ Hwfms2 Hwfde2 Hsp2) as (Hall&?&?).
Dan Frumin's avatar
Dan Frumin committed
435
436
      apply Forall_cons in Hall. destruct Hall.
      repeat split; eauto using denv_wf_merge.
Dan Frumin's avatar
Dan Frumin committed
437
  Qed.
Dan Frumin's avatar
Dan Frumin committed
438

Dan Frumin's avatar
Dan Frumin committed
439
  Lemma vcg_sp'_wf E de m m' mNew dv :
440
441
    denv_wf E m 
    dcexpr_wf E de 
Dan Frumin's avatar
Dan Frumin committed
442
443
    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
444
  Proof.
Dan Frumin's avatar
Dan Frumin committed
445
446
447
448
449
450
451
452
    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
453
  Qed.
454

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

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

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

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

567
 Lemma vcg_wp_correct R E m de Φ :
568
    dcexpr_wf E de 
569
    denv_wf E m 
570
    denv_interp E m -
Léon Gondelman's avatar
Léon Gondelman committed
571
    vcg_wp E m de R Φ -
572
    awp (dcexpr_interp E de) R (vcg_wp_postcondition E Φ).
573
  Proof.
574
575
576
    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.
Léon Gondelman's avatar
Léon Gondelman committed
577
    - by iApply (vcg_wp_unknown_correct with "Hm Hwp").
578
    - rewrite IHde //. iRename "Hm" into "Hawp".
579
      iSpecialize ("Hawp" with "Hwp"); simpl.
580
      iApply (a_load_spec_exists_frac with "[Hawp]"). iApply (awp_wand with "Hawp").
581
582
      iIntros (v) "H". unfold vcg_wp_postcondition.
      iDestruct "H" as (E' dv m' Heq Hwf0 Hm'wf Hwf2) "(Hm & Hwp)".
583
      iPoseProof (vcg_wp_load_correct E' m' with "Hm Hwp") as "Hload"; first done.
584
585
586
587
588
589
      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.
590
    - rewrite{1} /vcg_wp; fold vcg_wp.
Robbert Krebbers's avatar
Robbert Krebbers committed
591
      simpl in Hwf. apply andb_prop_elim in Hwf as [Hwf1 Hwf2].
Dan Frumin's avatar
Dan Frumin committed
592
593
      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.
594
        { by iApply (vcg_wp_unknown_correct with "Hm Hwp"). }
Dan Frumin's avatar
Dan Frumin committed
595
596
        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
597
        clear Heqsp2 Heqsp.
Dan Frumin's avatar
Dan Frumin committed
598
        iDestruct (IHde1 with "Hm' Hwp") as "Hde1"; try done.
Robbert Krebbers's avatar
Robbert Krebbers committed
599
        iApply (a_store_spec with "Hde1 Hde2"). iIntros "!>" (v1 v2).
600
601
        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
602
        iApply (vcg_wp_store_correct with "[-H] H");
603
          eauto using dval_wf_mono, denv_wf_merge, denv_wf_mono.
Dan Frumin's avatar
Dan Frumin committed
604
        rewrite -!denv_merge_interp. iFrame "Hm'".
Dan Frumin's avatar
Dan Frumin committed
605
606
607
608
        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.
609
        iApply (a_store_spec with "Hde1 Hde2"). iIntros "!>" (v1 v2).
610
611
        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
612
        iApply (vcg_wp_store_correct with "[-H] H");
Robbert Krebbers's avatar
Robbert Krebbers committed
613
          eauto using dval_wf_mono, denv_wf_merge, denv_wf_mono.
Léon Gondelman's avatar
Léon Gondelman committed
614
        rewrite -!denv_merge_interp. iFrame "Hm'".
Dan Frumin's avatar
Dan Frumin committed
615
        rewrite -!(denv_interp_mono E E'); eauto.
Léon Gondelman's avatar
Léon Gondelman committed
616
    - rewrite{1} /vcg_wp; fold vcg_wp.
617
      simpl in Hwf; apply andb_prop_elim in Hwf; destruct Hwf as [Hwf1 Hwf2].
Dan Frumin's avatar
Dan Frumin committed
618
619
      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.
620
        { by iApply (vcg_wp_unknown_correct with "Hm Hwp"). }
Dan Frumin's avatar
Dan Frumin committed
621
622
623
624
625
626
        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
627
        iNext. iIntros (v1 v2) "Hex (-> & HmNew)".
628
        iDestruct "Hex" as (E' dv1 m'' Heq Hpre Hm'wf) "(% & Hm' & Hwp)"; simplify_eq /=.
Dan Frumin's avatar
Dan Frumin committed
629
        rewrite (dval_interp_mono E E' dv2); eauto.
Dan Frumin's avatar
Dan Frumin committed
630
        iPoseProof (denv_interp_mono with "Hm'") as "Hm'"; [done | done |].
631
632
633
634
635
636
637
638
        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".
639
        iApply (vcg_wp_bin_op_correct with "Hm' Hwp [HmNew]");
Dan Frumin's avatar
Dan Frumin committed
640
641
          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
642
643
644
645
646
      + 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
647
648
        iApply (a_bin_op_spec with "Hde1 Hde2");  fold dcexpr_interp.
        iNext. iIntros (v1 v2) "(-> & HmNew) Hex".
649
        iDestruct "Hex" as (E' dv2 m2 Hpre ? Hm'wf) "(% & Hm' & Hwp)"; simplify_eq /=.
Dan Frumin's avatar
Dan Frumin committed
650
        rewrite (dval_interp_mono E E' dv1); 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.
663
    - rewrite IHde //. iApply a_un_op_spec. iSpecialize ("Hm" with "Hwp").
664
      iApply (awp_wand with "Hm"). iIntros (v) "Hex".
665
      iDestruct "Hex" as (E' dv m' Hpre Hpre' Hm'wf) "(% & Hm & Hwp)".
666
667
      destruct (dun_op_eval E' u dv) as [| dw | dw] eqn:Hop; simpl.
      + iDestruct "Hwp" as (?) "[% _]"; simplify_eq /=.
668
669
      + rewrite Hpre. iExists (dval_interp E' dw); iSplit.
        iPureIntro. eapply dun_op_eval_correct. by rewrite Hop.
670
671
        iExists E', dw, m'. apply dun_op_eval_dSome_wf in Hop; last done.
        eauto with iFrame.
672
673
674
      + iDestruct "Hwp" as (w) "[% Hwp]"; simplify_eq /=.
        iExists (dval_interp E' w). iSplit. iPureIntro.
        apply dun_op_eval_correct. by rewrite Hop.
675
676
677
678
        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.
679
680
      iApply (a_sequence_spec' with "[Hm]"); fold dcexpr_interp.
      iNext. iApply (awp_wand with "Hm").  iIntros (v) "Hex".
681
      iDestruct "Hex" as (Enew dv m' Hpre Hpre' Hm'wf) "(% & Hm & Hwp)". simpl.
682
      rewrite denv_unlock_interp.
683
      iModIntro. rewrite IHde2 //. iSpecialize ("Hm" with "Hwp").
684
      specialize (dcexpr_interp_mono E Enew de2 Hwf2 Hpre').
Léon Gondelman's avatar
Léon Gondelman committed
685
      intro Heq; rewrite Heq. iApply (awp_wand with "Hm").
686
687
688
689
690
691
      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 ; done.
      iSplit; first done.
      iFrame. iPureIntro; done. eapply dcexpr_wf_mono in Hwf2.
      done. done. by apply denv_wf_unlock.
692
    - by iApply (vcg_wp_unknown_correct with "Hm Hwp").
693
 Admitted.
694

Léon Gondelman's avatar
Léon Gondelman committed
695
  Lemma tac_vcg_sound Γs_in Γs_out Γls Γp m c e R Φ E de :
Dan Frumin's avatar
Dan Frumin committed
696
    MapstoListFromEnv Γs_in Γs_out Γls 
697
    E = penv_to_known_locs Γls 
Léon Gondelman's avatar
Léon Gondelman committed
698
    dcexpr_wf E de 
699
    denv_wf (penv_to_known_locs Γls) m 
700
    ListOfMapsto Γls E m 
Léon Gondelman's avatar
Léon Gondelman committed
701
    IntoDCexpr E e de 
702
    environments.envs_entails (environments.Envs Γp Γs_out c)