vcgen.v 22.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 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)`
Robbert Krebbers's avatar
Robbert Krebbers committed
15
16
17
18
19
- Support alloc in `vcg_wp`
- Automatically come up with the new `E`, `m` and `dv` and stuff in the
  unknown case
- Finish the proofs
- Maybe drop `wp_expr`? We are not taking it as an input of anything anymore
Léon Gondelman's avatar
Léon Gondelman committed
20
- Maybe generate vcg_unknown in the cases None and '_' in vcg_store and vcg_load
Robbert Krebbers's avatar
Robbert Krebbers committed
21
22
23
24
25
26
27
28
29
30

Less urgent TODO

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

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


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

42
43
  (** Deep embedding of formulae used to build the verification condition generator *)

44
  Inductive wp_expr :=
Dan Frumin's avatar
Dan Frumin committed
45
    | Base : iProp Σ  wp_expr
46
47
    | MapstoStar : dloc  (frac  dval  wp_expr)  wp_expr
    | MapstoStarFull : dloc  (dval  wp_expr)  wp_expr
48
    | MapstoWand : dloc  dval  lvl  frac  wp_expr  wp_expr
Dan Frumin's avatar
Dan Frumin committed
49
50
    | IsSome {A} : doption A  (A  wp_expr)  wp_expr
    | IsLoc : dval  (dloc  wp_expr)  wp_expr
51
    | UMod : wp_expr  wp_expr.
52

53
  Arguments Base _%I.
Dan Frumin's avatar
Dan Frumin committed
54

55
  Fixpoint wp_interp (E : known_locs) (a : wp_expr) : iProp Σ :=
Dan Frumin's avatar
Dan Frumin committed
56
57
    match a with
    | Base P => P
58
    | MapstoStar dl Φ =>
59
        q dv, dloc_interp E dl C{q} dval_interp E dv  wp_interp E (Φ q dv)
60
    | MapstoStarFull dl Φ =>
61
        dv, dloc_interp E dl C{1} dval_interp E dv  wp_interp E (Φ dv)
62
    | MapstoWand dl dv x q Φ =>
63
       dloc_interp E dl C[x]{q} dval_interp E dv - wp_interp E Φ
Dan Frumin's avatar
Dan Frumin committed
64
65
    | IsSome dmx Φ  =>  x, doption_interp dmx = Some x  wp_interp E (Φ x)
    | IsLoc dv Φ =>
66
        dl : dloc, dval_interp E dv = #(dloc_interp E dl)  wp_interp E (Φ dl)
Dan Frumin's avatar
Dan Frumin committed
67
    | UMod P => U (wp_interp E P)
68
    end%I.
Dan Frumin's avatar
Dan Frumin committed
69

70
  Fixpoint wp_interp_sane (E : known_locs) (a : wp_expr) : iProp Σ :=
Dan Frumin's avatar
Dan Frumin committed
71
72
    match a with
    | Base Φ => Φ
73
    | MapstoStar dl Φ =>
74
        q v, dloc_interp E dl C{q} v  wp_interp_sane E (Φ q (dValUnknown v))
75
    | MapstoStarFull dl Φ =>
Robbert Krebbers's avatar
Robbert Krebbers committed
76
        v, dloc_interp E dl C{1} v  wp_interp_sane E (Φ (dValUnknown v))
77
    | MapstoWand dl dv x q Φ =>
78
       dloc_interp E dl C[x]{q} dval_interp E dv - wp_interp_sane E Φ
Dan Frumin's avatar
Dan Frumin committed
79
    | IsSome dmx Φ  =>
80
        x, doption_interp dmx = Some x  wp_interp_sane E (Φ x)
Dan Frumin's avatar
Dan Frumin committed
81
    | IsLoc dv Φ =>
82
        l : loc, dval_interp E dv = #l  wp_interp_sane E (Φ (dLocUnknown l))
Dan Frumin's avatar
Dan Frumin committed
83
    | UMod P => U (wp_interp_sane E P)
84
    end%I.
Dan Frumin's avatar
Dan Frumin committed
85

86
  Fixpoint mapsto_wand_list_aux (m : denv) (Φ : wp_expr) (i : nat) : wp_expr :=
87
    match m with
88
    | [] => Φ
89
90
    | dio :: m' =>
      match dio with
91
      | None => mapsto_wand_list_aux m' Φ (S i)
92
      | Some (DenvItem x q dv)  =>
93
         MapstoWand (dLoc i) dv x q (mapsto_wand_list_aux m' Φ (S i))
94
      end
95
96
    end.

97
98
  Definition mapsto_wand_list (m : denv) (Φ : wp_expr) : wp_expr :=
    mapsto_wand_list_aux m Φ O.
99

Dan Frumin's avatar
Dan Frumin committed
100
  Fixpoint vcg_sp (E: known_locs) (mIn mOut : denv) (de : dcexpr) : option (denv * denv * dval) :=
101
    match de with
Dan Frumin's avatar
Dan Frumin committed
102
    | dCRet dv    => Some (mIn, mOut, dv)
103
    | dCLoad de1  =>
Dan Frumin's avatar
Dan Frumin committed
104
105
106
107
       ''(mIn1, mOut1, dl)      vcg_sp E mIn mOut de1;
       i                        is_dloc E dl;
       ''(mIn2, mOut2, q, dv)   denv_delete_frac_2 i mIn1 mOut1;
       Some (mIn2, denv_insert i ULvl q dv mOut2, dv)
108
    | dCStore de1 de2 =>
Dan Frumin's avatar
Dan Frumin committed
109
       ''(mIn1, mOut1, dl)  vcg_sp E mIn mOut de1;
110
       i                    is_dloc E dl;
Dan Frumin's avatar
Dan Frumin committed
111
       ''(mIn2, mOut2, dv)  vcg_sp E mIn1 [] de2;
112
113
       ''(mIn3, mOut3, _)   denv_delete_full_2 i mIn2 (denv_merge mOut1 mOut2);
       Some (mIn3, denv_insert i LLvl 1 dv mOut3, dv)
114
    | dCBinOp op de1 de2 =>
Dan Frumin's avatar
Dan Frumin committed
115
116
       ''(mIn1, mOut1, dv1)  vcg_sp E mIn mOut de1;
       ''(mIn2, mOut2, dv2)  vcg_sp E mIn1 [] de2;
117
118
119
120
       match dbin_op_eval E op dv1 dv2 with
       | dSome dv => Some (mIn2, denv_merge mOut1 mOut2, dv)
       | dNone | dUnknown _ => None
       end
121
    | dCUnOp op de =>
Dan Frumin's avatar
Dan Frumin committed
122
       ''(mIn1, mOut1, dv)  vcg_sp E mIn mOut de;
123
       match dun_op_eval E op dv with
Dan Frumin's avatar
Dan Frumin committed
124
       | dSome dv' => Some (mIn1, mOut1, dv')
125
126
       | dNone | dUnknown _ => None
       end
127
    | dCSeq de1 de2 =>
Dan Frumin's avatar
Dan Frumin committed
128
129
       ''(mIn1, mOut1, _)    vcg_sp E mIn mOut de1;
       ''(mIn2, mOut2, dv2)  vcg_sp E mIn1 (denv_unlock mOut1) de2;
Dan Frumin's avatar
Dan Frumin committed
130
       Some (mIn2, mOut2, dv2)
131
    | dCAlloc _ |  dCUnknown _ => None
132
133
    end.

Léon Gondelman's avatar
Léon Gondelman committed
134
135
136
137
138
139
140
141
142
143
144
145

  Definition vcg_wp_unknown (R : iProp Σ) (E: known_locs) (de: dcexpr) (m: denv)
      (Φ : known_locs  denv  dval  wp_expr) : wp_expr :=
    mapsto_wand_list m $ Base $
      awp (dcexpr_interp E de) R (λ v,
         E' m' dv,
         v = dval_interp E' dv  
         E `prefix_of` E'  
        denv_interp E' m' 
        wp_interp_sane E' (Φ E' m' dv))%I.
  Arguments vcg_wp_unknown : simpl never.

146
147
148
149
150
151
152
153
154
155
  Definition vcg_wp_load (E : known_locs) (dv : dval) (m : denv)
      (Φ : denv  dval  wp_expr) : wp_expr :=
    match is_dloc E dv with
    | Some i =>
       match denv_lookup i m with
       | Some (_, dw) => Φ m dw
       | None =>
          mapsto_wand_list m $ MapstoStar (dLoc i) $ λ q dw,
            MapstoWand (dLoc i) dw ULvl q (Φ [] dw)
       end
Robbert Krebbers's avatar
Robbert Krebbers committed
156
157
158
159
    | _ =>
       mapsto_wand_list m $ IsLoc dv (λ dl,
         MapstoStar dl $ λ q dw,
           MapstoWand dl dw ULvl q (Φ [] dw))
160
161
162
163
164
165
166
167
168
169
170
171
    end.

  Definition vcg_wp_store (E : known_locs) (dv1 dv2 : dval) (m : denv)
      (Φ : denv  dval  wp_expr) : wp_expr :=
    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 =>
          mapsto_wand_list m $ MapstoStarFull (dLoc i) $ λ _,
            MapstoWand (dLoc i) dv2 LLvl 1 (Φ [] dv2)
       end
Robbert Krebbers's avatar
Robbert Krebbers committed
172
173
174
175
    | _ =>
       mapsto_wand_list m $ IsLoc dv1 (λ dl,
         MapstoStarFull dl $ λ _,
            MapstoWand dl dv2 LLvl 1 (Φ [] dv2))
176
177
178
179
180
181
    end.

  Definition vcg_wp_bin_op (E : known_locs) (op : bin_op) (dv1 dv2 : dval)
      (m : denv) (Φ : denv  dval  wp_expr) : wp_expr :=
    match dbin_op_eval E op dv1 dv2 with
    | dSome dw => Φ m dw
182
183
    | dUnknown (Some dw) => Φ m dw
    | _ => Base False
184
185
186
    end.

  Fixpoint vcg_wp (E : known_locs) (m : denv) (de : dcexpr)
Robbert Krebbers's avatar
Robbert Krebbers committed
187
      (R : iProp Σ) (Φ : known_locs  denv  dval  wp_expr) : wp_expr :=
188
    match de with
Robbert Krebbers's avatar
Robbert Krebbers committed
189
    | dCRet dv => Φ E m dv
190
    | dCLoad de1 =>
191
       vcg_wp E m de1 R (λ E m' dv, vcg_wp_load E dv m' (Φ E))
192
    | dCStore de1 de2 =>
Dan Frumin's avatar
Dan Frumin committed
193
       match vcg_sp E m [] de1 with
194
       | Some (mIn, mOut, dv1) =>
Robbert Krebbers's avatar
Robbert Krebbers committed
195
196
          vcg_wp E mIn de2 R (λ E mIn dv2,
            vcg_wp_store E dv1 dv2 (denv_merge mOut mIn) (Φ E))
197
       | None =>
Dan Frumin's avatar
Dan Frumin committed
198
          match vcg_sp E m [] de2 with
199
          | Some (mIn, mOut, dv2) =>
Robbert Krebbers's avatar
Robbert Krebbers committed
200
201
             vcg_wp E mIn de1 R (λ E mIn dv1,
               vcg_wp_store E dv1 dv2 (denv_merge mOut mIn) (Φ E))
Léon Gondelman's avatar
Léon Gondelman committed
202
          | None => vcg_wp_unknown R E de m Φ
203
204
205
          end
        end
    | dCBinOp op de1 de2 =>
Dan Frumin's avatar
Dan Frumin committed
206
       match vcg_sp E m [] de1 with
207
       | Some (mIn, mOut, dv1) =>
Robbert Krebbers's avatar
Robbert Krebbers committed
208
209
          vcg_wp E mIn de2 R (λ E mIn dv2,
            vcg_wp_bin_op E op dv1 dv2 (denv_merge mOut mIn) (Φ E))
210
       | None =>
Dan Frumin's avatar
Dan Frumin committed
211
          match vcg_sp E m [] de2 with
212
          | Some (mIn, mOut, dv2) =>
Robbert Krebbers's avatar
Robbert Krebbers committed
213
214
             vcg_wp E mIn de1 R (λ E mIn dv1,
               vcg_wp_bin_op E op dv1 dv2 (denv_merge mOut mIn) (Φ E))
Léon Gondelman's avatar
Léon Gondelman committed
215
          | None => vcg_wp_unknown R E de m Φ
216
          end
Robbert Krebbers's avatar
Robbert Krebbers committed
217
       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
222
         | dSome dw => Φ E m dw
         | mdw => IsSome mdw (Φ E m)
223
224
         end)
    | dCSeq de1 de2 =>
Robbert Krebbers's avatar
Robbert Krebbers committed
225
       vcg_wp E m de1 R (λ E m _,
226
         UMod (vcg_wp E (denv_unlock m) de2 R Φ))
Léon Gondelman's avatar
Léon Gondelman committed
227
    | _ => vcg_wp_unknown R E de m Φ
228
    end.
229
230
231
232
233
234
End vcg.

Section vcg_spec.
  Context `{amonadG Σ}.

  Lemma wp_interp_sane_sound E a : wp_interp_sane E a  wp_interp E a.
235
  Proof.
236
237
238
    generalize dependent E.
    induction a.
    - done.
239
    - simpl. iIntros (E) "He". iDestruct "He" as (q v) "[H1 H2]".
240
      iExists q, (dValUnknown v). iFrame. by (iApply H0).
241
    - simpl. iIntros (E) "He". iDestruct "He" as (v) "[H1 H2]".
242
      iExists (dValUnknown v). simpl. iFrame. by (iApply H0).
243
244
245
246
247
248
    - simpl. iIntros (E) "He H". iApply IHa. by iApply "He".
    - simpl. iIntros (E) "He". iDestruct "He" as (v) "[H1 H2]".
      iExists v. iFrame. by iApply H0.
    - simpl. iIntros (E) "He". iDestruct "He" as (l) "[H1 H2]".
      iExists (dLocUnknown l). simpl. iFrame. by iApply H0.
    - simpl. intros E. by apply U_mono.
249
  Qed.
250

Dan Frumin's avatar
Dan Frumin committed
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269

  Lemma mapsto_wand_list_aux_spec E m t (k : nat) :
    wp_interp E (mapsto_wand_list_aux m t k) -
    ([ list] ndio  m, from_option
                              (λ '{| 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) -
    wp_interp E t.
  Proof.
    generalize dependent k.
    induction m; simpl. eauto.
    iIntros (k) "H1 [Hl H2]".
    destruct a as [[x q dv]|]; simpl.
    - rewrite -plus_n_O. iSpecialize ("H1" with "Hl").
      iApply (IHm with "H1 [H2]"). iApply (big_sepL_mono with "H2").
      intros n y ?. simpl. assert ((k + S n)%nat = (S k + n)%nat) as -> by omega. done.
    - iApply (IHm with "H1 [H2]"). iApply (big_sepL_mono with "H2").
      intros n y ?. simpl. assert ((k + S n)%nat = (S k + n)%nat) as -> by omega. done.
  Qed.

270
271
  Lemma mapsto_wand_list_spec E m t :
    wp_interp E (mapsto_wand_list m t) - denv_interp E m - wp_interp E t.
272
  Proof.
Dan Frumin's avatar
Dan Frumin committed
273
274
275
    unfold mapsto_wand_list, denv_interp.
    iIntros "H1 H2". iApply (mapsto_wand_list_aux_spec with "H1 H2").
  Qed.
276

Dan Frumin's avatar
Dan Frumin committed
277
278
279
280
281
  Lemma vcg_sp_correct' E de mIn mOut mIn' mOut' dv R :
    vcg_sp E mIn mOut de = Some (mIn', mOut', dv) 
    denv_interp E mIn -
    denv_interp E mIn' 
    (denv_interp E mOut - awp (dcexpr_interp E de) R (λ v, v = dval_interp E dv  denv_interp E mOut')).
Dan Frumin's avatar
Dan Frumin committed
282
  Proof.
Dan Frumin's avatar
Dan Frumin committed
283
284
285
286
287
   revert mIn mOut mIn' mOut' dv. induction de;
    iIntros (mIn mOut mIn' mOut' dv Hsp) "HmIn"; simplify_eq/=.
    - iFrame. iIntros "HmOut". iApply awp_ret. wp_value_head. iSplit; eauto.
    - specialize (IHde mIn mOut).
      destruct (vcg_sp E mIn mOut de) as [[[mIn1 mOut1] dv1]|]; simplify_eq /=.
Léon Gondelman's avatar
Léon Gondelman committed
288
      destruct dv1 as [dv1|dv1]; destruct dv1; simplify_eq/=.
Léon Gondelman's avatar
Léon Gondelman committed
289
      destruct d as [i|?]; simplify_eq/=.
Dan Frumin's avatar
Dan Frumin committed
290
      destruct (denv_delete_frac_2 i mIn1 mOut1) as [[[[mIn2 mOut2] q] dv1]|] eqn:Hfar; simplify_eq/=.
Dan Frumin's avatar
Dan Frumin committed
291
      rewrite IHde; last done. iDestruct "HmIn" as "[HmIn1 Hawp]".
Dan Frumin's avatar
Dan Frumin committed
292
      iDestruct (denv_delete_frac_2_interp with "HmIn1") as "[$ Hm]"; first eassumption.
Dan Frumin's avatar
Dan Frumin committed
293
      iIntros "HmOut". iSpecialize ("Hawp" with "HmOut").
Léon Gondelman's avatar
Léon Gondelman committed
294
295
      iApply a_load_spec.
      iApply (awp_wand with "Hawp").
Léon Gondelman's avatar
Léon Gondelman committed
296
      iIntros (?) "[% HmOut1]". simplify_eq/=. iExists _, _. iSplit; eauto.
Dan Frumin's avatar
Dan Frumin committed
297
      iDestruct ("Hm" with "HmOut1") as "[HmOut2 $]". iIntros "Hl". iSplit; eauto.
Dan Frumin's avatar
Dan Frumin committed
298
      rewrite -denv_insert_interp.
Léon Gondelman's avatar
Léon Gondelman committed
299
      by iFrame.
Dan Frumin's avatar
Dan Frumin committed
300
301
    - specialize (IHde1 mIn mOut).
      destruct (vcg_sp E mIn mOut de1) as [[[mIn1 mOut1] dv1]|]; simplify_eq /=.
Léon Gondelman's avatar
Léon Gondelman committed
302
      destruct dv1 as [dv1|dv1]; destruct dv1; simplify_eq/=.
Léon Gondelman's avatar
Léon Gondelman committed
303
      destruct d as [i|?]; simplify_eq/=.
Dan Frumin's avatar
Dan Frumin committed
304
305
      specialize (IHde2 mIn1 []).
      destruct (vcg_sp E mIn1 [] de2) as [[[mIn2 mOut2] dv2]|]; simplify_eq /=.
306
307
      destruct (denv_delete_full_2 i mIn2 (denv_merge mOut1 mOut2))
        as [[[mIn3 mOut3 ] dv3] |] eqn:Hfar; simplify_eq/=.
Dan Frumin's avatar
Dan Frumin committed
308
309
      rewrite IHde1; last done. iDestruct "HmIn" as "[HmIn1 Hawp1]".
      rewrite IHde2; last done. iDestruct "HmIn1" as "[HmIn2 Hawp2]".
Léon Gondelman's avatar
Léon Gondelman committed
310
      rewrite denv_delete_full_2_interp; last done.
Dan Frumin's avatar
Dan Frumin committed
311
312
313
      iDestruct "HmIn2" as "[$ Hl]". iIntros "HmOut".
      iSpecialize ("Hawp2" with "[]"). rewrite /denv_interp; done.
      iSpecialize ("Hawp1" with "HmOut").
Léon Gondelman's avatar
Léon Gondelman committed
314
315
      iApply (a_store_spec _ _
            (λ j, j = dloc_interp E (dLoc i)  denv_interp E mOut1)%I with "[Hawp1] Hawp2").
Léon Gondelman's avatar
Léon Gondelman committed
316
317
      { iApply (awp_wand with "Hawp1").
        iIntros (?) "[% H]". simplify_eq/=. iExists _; eauto. }
Léon Gondelman's avatar
Léon Gondelman committed
318
319
320
      iNext. iIntros (? ?) "[% HmOut1] [% HmOut2]". simplify_eq/=.
      iExists _. rewrite -denv_merge_interp.
      iDestruct ("Hl" with "[$HmOut1 $HmOut2]") as "[HmOut3 $]".
Léon Gondelman's avatar
Léon Gondelman committed
321
      iIntros "Hl". iSplit; eauto.
Dan Frumin's avatar
Dan Frumin committed
322
      rewrite -denv_insert_interp.
Léon Gondelman's avatar
Léon Gondelman committed
323
      iFrame "Hl HmOut3".
Dan Frumin's avatar
Dan Frumin committed
324
325
326
327
    - specialize (IHde1 mIn mOut).
      destruct (vcg_sp E mIn mOut de1) as [[[mIn1 mOut1] dv1]|]; simplify_eq /=.
      specialize (IHde2 mIn1 []).
      destruct (vcg_sp E mIn1 [] de2) as [[[mIn2 mOut2] dv2]|]; simplify_eq /=.
328
      destruct (dbin_op_eval E b dv1 dv2) eqn:Hboe; simplify_eq/=.
Dan Frumin's avatar
Dan Frumin committed
329
      rewrite IHde1; last done. iDestruct "HmIn" as "[HmIn1 Hawp1]".
Léon Gondelman's avatar
Léon Gondelman committed
330
      rewrite IHde2; last done. iDestruct "HmIn1" as "[$ Hawp2]".
Dan Frumin's avatar
Dan Frumin committed
331
332
      iIntros "HmOut". iSpecialize ("Hawp1" with "HmOut").
      iSpecialize ("Hawp2" with "[]"). by rewrite /denv_interp.
Léon Gondelman's avatar
Léon Gondelman committed
333
      iApply (a_bin_op_spec with "Hawp1 Hawp2").
Léon Gondelman's avatar
Léon Gondelman committed
334
      iNext. iIntros (? ?) "[% HmOut1] [% HmOut2]"; simplify_eq/=.
Léon Gondelman's avatar
Léon Gondelman committed
335
      iExists (dval_interp E dv). repeat iSplit; eauto.
336
      + iPureIntro. apply dbin_op_eval_correct. by rewrite Hboe.
Léon Gondelman's avatar
Léon Gondelman committed
337
      + rewrite -denv_merge_interp. iFrame.
Dan Frumin's avatar
Dan Frumin committed
338
339
    - specialize (IHde mIn mOut).
      destruct (vcg_sp E mIn mOut de) as [[[mIn1 mOut1] dv1]|]; simplify_eq /=.
340
      remember (dun_op_eval E u dv1) as Hu; destruct Hu; simplify_eq/=.
Dan Frumin's avatar
Dan Frumin committed
341
342
      rewrite IHde; last done. iDestruct "HmIn" as "[$ Hawp]".
      iIntros "HmOut". iSpecialize ("Hawp" with "HmOut").
343
344
345
346
      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
347
348
349
350
351
352
353
354
    - specialize (IHde1 mIn mOut).
      destruct (vcg_sp E mIn mOut de1) as [[[mIn1 mOut1] dv1]|]; simplify_eq /=.
      rewrite IHde1; last done. iDestruct "HmIn" as "[HmIn1 Hawp1]".
      specialize (IHde2 mIn1 (denv_unlock mOut1)).
      destruct (vcg_sp E mIn1 (denv_unlock mOut1) de2) as [[[mIn2 mOut2] dv2]|]; simplify_eq /=.
      rewrite IHde2; last done. iDestruct "HmIn1" as "[$ Hawp2]".
      iIntros "HmOut". iSpecialize ("Hawp1" with "HmOut").
      (* TODO: wtf? *)
Dan Frumin's avatar
Dan Frumin committed
355
      assert (AsVal (λ: <>, dcexpr_interp E de2)).
356
      { exists (λ: <>, dcexpr_interp E de2)%V. by unlock. }
Dan Frumin's avatar
Dan Frumin committed
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
      iApply a_sequence_spec. iApply (awp_wand with "Hawp1").
      iIntros (?) "[_ HmOut1]". rewrite (denv_unlock_interp E mOut1).
      iModIntro. iSpecialize ("Hawp2" with "HmOut1").
      awp_seq. iApply "Hawp2".
  Qed.

  Lemma vcg_sp_correct E de m mIn mOut dv R :
    vcg_sp E m [] de = Some (mIn, mOut, dv) 
    denv_interp E m -
    denv_interp E mIn  awp (dcexpr_interp E de) R (λ v, v = dval_interp E dv  denv_interp E mOut).
  Proof.
    iIntros (?) "Hm".
    iDestruct (vcg_sp_correct' with "Hm") as "[$ Hawp]"; first eassumption.
    iApply "Hawp". by rewrite /denv_interp.
  Qed.
Léon Gondelman's avatar
Léon Gondelman committed
372

373
374
375
  Lemma vcg_wp_unknown_correct R E m de Φ :
    denv_interp E m -
    wp_interp E (vcg_wp_unknown R E de m Φ) -
376
377
378
    awp (dcexpr_interp E de) R (λ v,  E' dv m',
      E `prefix_of` E'  dval_interp E' dv = v 
      denv_interp E' m'  wp_interp E' (Φ E' m' dv)).
379
380
381
382
383
384
  Proof.
    rewrite /vcg_wp_unknown mapsto_wand_list_spec.
    iIntros "Hm Hwp". iSpecialize ("Hwp" with "Hm").
    iApply (awp_wand with "Hwp"). iIntros (v) "H".
    iDestruct "H" as (E' m' dv ->) "(Hpre & Hm & Hwp)".
    rewrite wp_interp_sane_sound.
Léon Gondelman's avatar
Léon Gondelman committed
385
    iExists E', dv, m'. by iSplit; first done; iFrame.
386
387
  Qed.

388
389
390
391
  Lemma vcg_wp_load_correct E m dv Φ :
    denv_interp E m -
    wp_interp E (vcg_wp_load E dv m (Φ E)) -
     (l:loc) q w, dval_interp E dv = #l  l C{q} w   (l C{q} w -
392
393
     E' dv' m', E `prefix_of` E'  dval_interp E' dv' = w 
                  denv_interp E' m'  wp_interp E' (Φ E' m' dv')).
394
395
396
  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
397
398
399
400
401
      * destruct (denv_lookup_interp E i q dv' m) as [m0 Hm0]; first assumption.
        rewrite Hm0. iIntros "[Hi Hm0] HΦ". apply is_dloc_some in Hdloc. simplify_eq/=.
        iExists (dloc_interp E (dLoc i)), q,  (dval_interp E dv').
        iSplit. simplify_eq /=; done. iFrame. iIntros "Hl".
        iExists E, dv', m; iSplit; first done. iFrame.
Léon Gondelman's avatar
Léon Gondelman committed
402
        rewrite Hm0. by iFrame.
403
404
405
406
      * rewrite mapsto_wand_list_spec. iIntros "Hm Hwp". iSpecialize ("Hwp" with "Hm"). simpl.
        iDestruct "Hwp" as (q dv') "[Hl Hwp]". apply is_dloc_some in Hdloc. subst.
        iExists (dloc_interp E (dLoc i)), q, (dval_interp E dv'). iSplit; first done.
        iFrame. iIntros "Hl". iSpecialize ("Hwp" with "Hl").
Léon Gondelman's avatar
Léon Gondelman committed
407
        iExists E, dv', []; iSplit; first done. iFrame. unfold denv_interp. eauto.
408
409
410
411
    +  rewrite mapsto_wand_list_spec. iIntros "Hm Hwp". iSpecialize ("Hwp" with "Hm"); simpl.
       iDestruct "Hwp" as (dl) "(-> & Hwp)". iDestruct "Hwp" as (q dv') "[Hl Hwp]".
       iExists (dloc_interp E dl), q, (dval_interp E dv'). iSplit; first done.
       iFrame. iIntros "Hl". iSpecialize ("Hwp" with "Hl").
Léon Gondelman's avatar
Léon Gondelman committed
412
       iExists E, dv', []; iSplit; first done. iFrame. unfold denv_interp. eauto.
413
414
  Qed.

415
  Lemma vcg_wp_correct R E m de Φ :
416
    dcexpr_wf E de 
417
418
    denv_interp E m -
    wp_interp E (vcg_wp E m de R Φ) -
419
420
421
    awp (dcexpr_interp E de) R (λ v,  E' dv m',
      E `prefix_of` E'  dval_interp E' dv = v 
      denv_interp E' m'  wp_interp E' (Φ E' m' dv)).
422
  Proof.
423
    revert Φ E m. induction de; intros Φ E m Hwf; iIntros "Hm Hwp".
Léon Gondelman's avatar
Léon Gondelman committed
424
425
    - iApply awp_ret. wp_value_head.  iExists E, d, m. iSplit; first done; by iFrame.
    -  iApply (vcg_wp_unknown_correct with "Hm Hwp").
426
    - rewrite IHde; last done. iSpecialize ("Hm" with "Hwp"); simpl.
427
      iApply (a_load_spec_exists_frac with "[Hm]"). iApply (awp_wand with "Hm").
Léon Gondelman's avatar
Léon Gondelman committed
428
429
430
431
432
      iIntros (v) "H". iDestruct "H" as (E' dv m' Hpre <-) "(Hm & Hwp)".
      iPoseProof (vcg_wp_load_correct E' m' with "Hm Hwp") as "Hload".
      iDestruct "Hload" as (l q w ->) "(Hl & Hwp)".
      iExists l, q, w; iSplit; first done. iFrame. iIntros "Hl".
      iSpecialize ("Hwp" with "Hl"). iDestruct "Hwp" as (Ef dvf mf Hpre' <-) "(Hmf & Hwp)".
433
      iExists Ef, dvf, mf. iFrame. by iSplit; first by iPureIntro; trans E'; done.
Dan Frumin's avatar
Dan Frumin committed
434
    - admit.
Léon Gondelman's avatar
Léon Gondelman committed
435
436
    - rewrite{1} /vcg_wp; fold vcg_wp.
      destruct (vcg_sp E m [] de1) as [[[mIn mOut] dv1]|] eqn:Heqsp; last first.
437
      + destruct (vcg_sp E m [] de2) as [[[mIn mOut] dv2]|] eqn:Heqsp2; last first.
Léon Gondelman's avatar
Léon Gondelman committed
438
439
440
        { iApply (vcg_wp_unknown_correct with "[$Hm] [$Hwp]"). }
        iPoseProof (vcg_sp_correct _ _ _ _ _ _ R) as "Hsp". eassumption.
        iDestruct ("Hsp" with "Hm") as "[Hm Hde2]". iClear "Hsp". clear Heqsp2 Heqsp.
441
442
443
        rewrite IHde1; last first.
        { simpl in Hwf. apply andb_prop_elim in Hwf. by destruct Hwf. }
        iSpecialize ("Hm" with "Hwp").
Léon Gondelman's avatar
Léon Gondelman committed
444
445
446
        iApply (a_bin_op_spec with "[$Hm] [$Hde2]"); fold dcexpr_interp.
        iNext. iIntros (v1 v2) "Hex (-> & HmOut)".
        iDestruct "Hex" as (E' dv1 m' Hpre <-) "(Hm' & Hwp)"; simplify_eq /=.
447
448
449
450
451
452
453
454
455
456
457
458
459
460
        rewrite /vcg_wp_bin_op.
        destruct (dbin_op_eval E' b dv1 dv2) as [ | dw | dw ] eqn:Hbin; first done.
        * iExists  (dval_interp E' dw); iSplit.
          { iPureIntro.  (*TODO: we need (dval_interp E dv2) = (dval_interp E' dv2) *) admit. }
          iExists E', dw, (denv_merge mOut m').
          rewrite -denv_merge_interp. iFrame. repeat (iSplit; first done).
          (*TODO: we have to relate denv_interp and prefix_of *) admit.
        * destruct dw as [dw1|] eqn:Hdw; last done.
          iExists  (dval_interp E' dw1); iSplit.
          { iPureIntro.  (*TODO: we need (dval_interp E dv2) = (dval_interp E' dv2) *) admit. }
          iExists E', dw1, (denv_merge mOut m').
          rewrite -denv_merge_interp. iFrame. repeat (iSplit; first done).
          (*TODO: we have to relate denv_interp and prefix_of *) admit.
      + admit.
461
    - rewrite IHde; last done. iApply a_un_op_spec. iSpecialize ("Hm" with "Hwp").
462
      iApply (awp_wand with "Hm"). iIntros (v) "Hex".
463
      iDestruct "Hex" as (E' dv m' Hpre <-) "(Hm & Hwp)".
464
465
466
467
      destruct (dun_op_eval E' u dv) as [| dw | dw] eqn:Hop; simpl.
      + iDestruct "Hwp" as (?) "[% _]"; simplify_eq /=.
      + iExists (dval_interp E' dw); iSplit.
        iPureIntro. apply dun_op_eval_correct. by rewrite Hop.
468
        iExists E', dw, m'; eauto with iFrame.
469
470
471
      + iDestruct "Hwp" as (w) "[% Hwp]"; simplify_eq /=.
        iExists (dval_interp E' w). iSplit. iPureIntro.
        apply dun_op_eval_correct. by rewrite Hop.
472
        iExists E', w, m'; eauto with iFrame.
473
474
475
    - rewrite IHde1; last first.
      { simpl in Hwf. apply andb_prop_elim in Hwf. by destruct Hwf. }
      iSpecialize ("Hm" with "Hwp"); fold vcg_wp.
476
477
478
      iApply (a_sequence_spec with "[Hm]"); fold dcexpr_interp.
      { exists (λ: <>, dcexpr_interp E de2)%V. by unlock. }
      iApply (awp_wand with "Hm").  iIntros (v) "Hex".
479
480
      iDestruct "Hex" as (Enew dv m' Hpre <-) "(Hm & Hwp)". simpl.
      rewrite denv_unlock_interp.
481
      iModIntro. rewrite IHde2. awp_seq. iSpecialize ("Hm" with "Hwp").
482
483
      simpl in Hwf; apply andb_prop_elim in Hwf; destruct Hwf.
      specialize (dcexpr_interp_mono E Enew de2 H1 Hpre).
Léon Gondelman's avatar
Léon Gondelman committed
484
485
486
      intro Heq; rewrite Heq. iApply (awp_wand with "Hm").
      iIntros (v) "Hex".
      iDestruct "Hex" as (Ef dvf mf Hpref <-) "(Hm & Hwp)". simpl.
487
488
489
      iExists Ef, dvf, mf. iSplit. iPureIntro. trans Enew ; done. iSplit; first done.
      iFrame. simpl in Hwf; apply andb_prop_elim in Hwf; destruct Hwf.
      by eapply dcexpr_wf_mono in Hpre.
Dan Frumin's avatar
Dan Frumin committed
490
    - iApply (vcg_wp_unknown_correct with "Hm Hwp").
491
   Admitted.
492

Léon Gondelman's avatar
Léon Gondelman committed
493
  Lemma tac_vcg_sound Γs_in Γs_out Γls Γp m c e R Φ E de :
Dan Frumin's avatar
Dan Frumin committed
494
    MapstoListFromEnv Γs_in Γs_out Γls 
495
    E = penv_to_known_locs Γls 
Léon Gondelman's avatar
Léon Gondelman committed
496
    dcexpr_wf E de 
497
    ListOfMapsto Γls E m 
Léon Gondelman's avatar
Léon Gondelman committed
498
    IntoDCexpr E e de 
499
    environments.envs_entails (environments.Envs Γp Γs_out c)
Léon Gondelman's avatar
Léon Gondelman committed
500
      (wp_interp_sane E (vcg_wp E m de R (λ E m dv,
501
        mapsto_wand_list m $ Base (Φ (dval_interp E dv))))) 
502
    environments.envs_entails (environments.Envs Γp Γs_in c) (awp e R Φ).
Dan Frumin's avatar
Dan Frumin committed
503
  Proof.
Léon Gondelman's avatar
Léon Gondelman committed
504
505
506
507
508
509
510
511
512
513
    rewrite /IntoDCexpr /=. intros Hsplit ->.
    rewrite /ListOfMapsto. intros Hwf Hpenv -> Hgoal.
    eapply tac_envs_split_mapsto; try eassumption.
    revert Hgoal. rewrite environments.envs_entails_eq.
    rewrite (vcg_wp_correct R); last done.
    iIntros (->) "H1 H2". rewrite wp_interp_sane_sound.
    iSpecialize ("H2" with "H1"). iApply (awp_wand with "H2").
    iIntros (v) "H". iDestruct "H" as (E' dv m' Hpre) "(-> & Hm & Hwp)".
    rewrite mapsto_wand_list_spec. iApply ("Hwp" with "Hm").
  Qed.
514

515
End vcg_spec.
516

517
518
Arguments wp_interp : simpl never.

519
520
521
522
Ltac vcg_solver :=
  eapply tac_vcg_sound;
    [ apply _    (*  MapstoListFromEnv Γs_in Γs_out Γls *)
    | compute; reflexivity (*  E = penv_to_known_locs Γls *)
Léon Gondelman's avatar
Léon Gondelman committed
523
    |  (* dcexpr_wf E de *)
524
525
    | apply _ (* ListOfMapsto Γls E m *)
    | apply _ (* IntoDCexpr E e dce *)
Léon Gondelman's avatar
Léon Gondelman committed
526
    | simpl ]; intuition.