lifting.v 10.2 KB
Newer Older
1
From iris.base_logic Require Export gen_heap.
2 3
From iris.program_logic Require Export weakestpre.
From iris.program_logic Require Import ectx_lifting total_ectx_lifting.
4
From iris.heap_lang Require Export lang.
5
From iris.heap_lang Require Import tactics.
6
From iris.proofmode Require Import tactics.
Ralf Jung's avatar
Ralf Jung committed
7
From stdpp Require Import fin_maps.
8
Set Default Proof Using "Type".
Ralf Jung's avatar
Ralf Jung committed
9

10 11 12 13 14 15 16 17 18 19 20 21
Class heapG Σ := HeapG {
  heapG_invG : invG Σ;
  heapG_gen_heapG :> gen_heapG loc val Σ
}.

Instance heapG_irisG `{heapG Σ} : irisG heap_lang Σ := {
  iris_invG := heapG_invG;
  state_interp := gen_heap_ctx
}.

(** Override the notations so that scopes and coercions work out *)
Notation "l ↦{ q } v" := (mapsto (L:=loc) (V:=val) l q v%V)
Robbert Krebbers's avatar
Robbert Krebbers committed
22
  (at level 20, q at level 50, format "l  ↦{ q }  v") : bi_scope.
23
Notation "l ↦ v" :=
Robbert Krebbers's avatar
Robbert Krebbers committed
24
  (mapsto (L:=loc) (V:=val) l 1 v%V) (at level 20) : bi_scope.
25
Notation "l ↦{ q } -" := ( v, l {q} v)%I
Robbert Krebbers's avatar
Robbert Krebbers committed
26 27
  (at level 20, q at level 50, format "l  ↦{ q }  -") : bi_scope.
Notation "l ↦ -" := (l {1} -)%I (at level 20) : bi_scope.
28

29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
(** The tactic [inv_head_step] performs inversion on hypotheses of the shape
[head_step]. The tactic will discharge head-reductions starting from values, and
simplifies hypothesis related to conversions from and to values, and finite map
operations. This tactic is slightly ad-hoc and tuned for proving our lifting
lemmas. *)
Ltac inv_head_step :=
  repeat match goal with
  | _ => progress simplify_map_eq/= (* simplify memory stuff *)
  | H : to_val _ = Some _ |- _ => apply of_to_val in H
  | H : head_step ?e _ _ _ _ |- _ =>
     try (is_var e; fail 1); (* inversion yields many goals if [e] is a variable
     and can thus better be avoided. *)
     inversion H; subst; clear H
  end.

44
Local Hint Extern 0 (atomic _ _) => solve_atomic.
45 46 47 48 49
Local Hint Extern 0 (head_reducible _ _) => eexists _, _, _; simpl.

Local Hint Constructors head_step.
Local Hint Resolve alloc_fresh.
Local Hint Resolve to_of_val.
50

51 52
Local Ltac solve_exec_safe := intros; subst; do 3 eexists; econstructor; eauto.
Local Ltac solve_exec_puredet := simpl; intros; by inv_head_step.
53
Local Ltac solve_pure_exec :=
54 55
  unfold IntoVal in *;
  repeat match goal with H : AsVal _ |- _ => destruct H as [??] end; subst;
56
  apply det_head_step_pure_exec; [ solve_exec_safe | solve_exec_puredet ].
57

58 59
Class AsRec (e : expr) (f x : binder) (erec : expr) :=
  as_rec : e = Rec f x erec.
60 61
Instance AsRec_rec f x e : AsRec (Rec f x e) f x e := eq_refl.
Instance AsRec_rec_locked_val v f x e :
62 63 64
  AsRec (of_val v) f x e  AsRec (of_val (locked v)) f x e.
Proof. by unlock. Qed.

65
Instance pure_rec f x (erec e1 e2 : expr)
Robbert Krebbers's avatar
Robbert Krebbers committed
66
    `{!AsVal e2, AsRec e1 f x erec, Closed (f :b: x :b: []) erec} :
67
  PureExec True (App e1 e2) (subst' x e2 (subst' f e1 erec)).
68
Proof. unfold AsRec in *; solve_pure_exec. Qed.
69

70
Instance pure_unop op e v v' `{!IntoVal e v} :
71
  PureExec (un_op_eval op v = Some v') (UnOp op e) (of_val v').
72
Proof. solve_pure_exec. Qed.
73

74
Instance pure_binop op e1 e2 v1 v2 v' `{!IntoVal e1 v1, !IntoVal e2 v2} :
75
  PureExec (bin_op_eval op v1 v2 = Some v') (BinOp op e1 e2) (of_val v').
76
Proof. solve_pure_exec. Qed.
77

78
Instance pure_if_true e1 e2 : PureExec True (If (Lit (LitBool true)) e1 e2) e1.
79
Proof. solve_pure_exec. Qed.
80

81
Instance pure_if_false e1 e2 : PureExec True (If (Lit (LitBool false)) e1 e2) e2.
82
Proof. solve_pure_exec. Qed.
83

84
Instance pure_fst e1 e2 v1 `{!IntoVal e1 v1, !AsVal e2} :
85
  PureExec True (Fst (Pair e1 e2)) e1.
86
Proof. solve_pure_exec. Qed.
87

88
Instance pure_snd e1 e2 v2 `{!AsVal e1, !IntoVal e2 v2} :
89
  PureExec True (Snd (Pair e1 e2)) e2.
90
Proof. solve_pure_exec. Qed.
91

92
Instance pure_case_inl e0 v e1 e2 `{!IntoVal e0 v} :
93
  PureExec True (Case (InjL e0) e1 e2) (App e1 e0).
94
Proof. solve_pure_exec. Qed.
95

96
Instance pure_case_inr e0 v e1 e2 `{!IntoVal e0 v} :
97
  PureExec True (Case (InjR e0) e1 e2) (App e2 e0).
98
Proof. solve_pure_exec. Qed.
99

100 101 102 103 104 105 106 107 108
Section lifting.
Context `{heapG Σ}.
Implicit Types P Q : iProp Σ.
Implicit Types Φ : val  iProp Σ.
Implicit Types efs : list expr.
Implicit Types σ : state.

(** Base axioms for core primitives of the language: Stateless reductions *)
Lemma wp_fork s E e Φ :
109
   (Φ (LitV LitUnit)  WP e @ s;  {{ _, True }})  WP Fork e @ s; E {{ Φ }}.
110 111 112 113 114 115 116 117 118 119 120 121 122
Proof.
  iIntros "[HΦ He]".
  iApply wp_lift_pure_det_head_step; [auto|intros; inv_head_step; eauto|].
  iModIntro; iNext; iIntros "!> /= {$He}". by iApply wp_value.
Qed.
Lemma twp_fork s E e Φ :
  Φ (LitV LitUnit)  WP e @ s;  [{ _, True }]  WP Fork e @ s; E [{ Φ }].
Proof.
  iIntros "[HΦ He]".
  iApply twp_lift_pure_det_head_step; [auto|intros; inv_head_step; eauto|].
  iIntros "!> /= {$He}". by iApply twp_value.
Qed.

123
(** Heap *)
124
Lemma wp_alloc s E e v :
Robbert Krebbers's avatar
Robbert Krebbers committed
125
  IntoVal e v 
126
  {{{ True }}} Alloc e @ s; E {{{ l, RET LitV (LitLoc l); l  v }}}.
127
Proof.
128
  iIntros (<- Φ) "_ HΦ". iApply wp_lift_atomic_head_step_no_fork; auto.
129 130 131 132 133
  iIntros (σ1) "Hσ !>"; iSplit; first by auto.
  iNext; iIntros (v2 σ2 efs Hstep); inv_head_step.
  iMod (@gen_heap_alloc with "Hσ") as "[Hσ Hl]"; first done.
  iModIntro; iSplit=> //. iFrame. by iApply "HΦ".
Qed.
134 135 136 137
Lemma twp_alloc s E e v :
  IntoVal e v 
  [[{ True }]] Alloc e @ s; E [[{ l, RET LitV (LitLoc l); l  v }]].
Proof.
138
  iIntros (<- Φ) "_ HΦ". iApply twp_lift_atomic_head_step_no_fork; auto.
139 140 141 142 143
  iIntros (σ1) "Hσ !>"; iSplit; first by auto.
  iIntros (v2 σ2 efs Hstep); inv_head_step.
  iMod (@gen_heap_alloc with "Hσ") as "[Hσ Hl]"; first done.
  iModIntro; iSplit=> //. iFrame. by iApply "HΦ".
Qed.
144

145 146
Lemma wp_load s E l q v :
  {{{  l {q} v }}} Load (Lit (LitLoc l)) @ s; E {{{ RET v; l {q} v }}}.
147 148 149 150 151 152 153
Proof.
  iIntros (Φ) ">Hl HΦ". iApply wp_lift_atomic_head_step_no_fork; auto.
  iIntros (σ1) "Hσ !>". iDestruct (@gen_heap_valid with "Hσ Hl") as %?.
  iSplit; first by eauto.
  iNext; iIntros (v2 σ2 efs Hstep); inv_head_step.
  iModIntro; iSplit=> //. iFrame. by iApply "HΦ".
Qed.
154 155 156 157 158 159 160 161 162
Lemma twp_load s E l q v :
  [[{ l {q} v }]] Load (Lit (LitLoc l)) @ s; E [[{ RET v; l {q} v }]].
Proof.
  iIntros (Φ) "Hl HΦ". iApply twp_lift_atomic_head_step_no_fork; auto.
  iIntros (σ1) "Hσ !>". iDestruct (@gen_heap_valid with "Hσ Hl") as %?.
  iSplit; first by eauto.
  iIntros (v2 σ2 efs Hstep); inv_head_step.
  iModIntro; iSplit=> //. iFrame. by iApply "HΦ".
Qed.
163

164
Lemma wp_store s E l v' e v :
Robbert Krebbers's avatar
Robbert Krebbers committed
165
  IntoVal e v 
166
  {{{  l  v' }}} Store (Lit (LitLoc l)) e @ s; E {{{ RET LitV LitUnit; l  v }}}.
167
Proof.
168
  iIntros (<- Φ) ">Hl HΦ".
169 170 171 172 173 174
  iApply wp_lift_atomic_head_step_no_fork; auto.
  iIntros (σ1) "Hσ !>". iDestruct (@gen_heap_valid with "Hσ Hl") as %?.
  iSplit; first by eauto. iNext; iIntros (v2 σ2 efs Hstep); inv_head_step.
  iMod (@gen_heap_update with "Hσ Hl") as "[$ Hl]".
  iModIntro. iSplit=>//. by iApply "HΦ".
Qed.
175 176 177 178
Lemma twp_store s E l v' e v :
  IntoVal e v 
  [[{ l  v' }]] Store (Lit (LitLoc l)) e @ s; E [[{ RET LitV LitUnit; l  v }]].
Proof.
179
  iIntros (<- Φ) "Hl HΦ".
180 181 182 183 184 185
  iApply twp_lift_atomic_head_step_no_fork; auto.
  iIntros (σ1) "Hσ !>". iDestruct (@gen_heap_valid with "Hσ Hl") as %?.
  iSplit; first by eauto. iIntros (v2 σ2 efs Hstep); inv_head_step.
  iMod (@gen_heap_update with "Hσ Hl") as "[$ Hl]".
  iModIntro. iSplit=>//. by iApply "HΦ".
Qed.
186

187
Lemma wp_cas_fail s E l q v' e1 v1 e2 :
Ralf Jung's avatar
Ralf Jung committed
188
  IntoVal e1 v1  AsVal e2  v'  v1  vals_cas_compare_safe v' v1 
189
  {{{  l {q} v' }}} CAS (Lit (LitLoc l)) e1 e2 @ s; E
190 191
  {{{ RET LitV (LitBool false); l {q} v' }}}.
Proof.
Ralf Jung's avatar
Ralf Jung committed
192
  iIntros (<- [v2 <-] ?? Φ) ">Hl HΦ".
193 194 195 196 197
  iApply wp_lift_atomic_head_step_no_fork; auto.
  iIntros (σ1) "Hσ !>". iDestruct (@gen_heap_valid with "Hσ Hl") as %?.
  iSplit; first by eauto. iNext; iIntros (v2' σ2 efs Hstep); inv_head_step.
  iModIntro; iSplit=> //. iFrame. by iApply "HΦ".
Qed.
198
Lemma twp_cas_fail s E l q v' e1 v1 e2 :
Ralf Jung's avatar
Ralf Jung committed
199
  IntoVal e1 v1  AsVal e2  v'  v1  vals_cas_compare_safe v' v1 
200 201 202
  [[{ l {q} v' }]] CAS (Lit (LitLoc l)) e1 e2 @ s; E
  [[{ RET LitV (LitBool false); l {q} v' }]].
Proof.
Ralf Jung's avatar
Ralf Jung committed
203
  iIntros (<- [v2 <-] ?? Φ) "Hl HΦ".
204 205 206 207 208
  iApply twp_lift_atomic_head_step_no_fork; auto.
  iIntros (σ1) "Hσ !>". iDestruct (@gen_heap_valid with "Hσ Hl") as %?.
  iSplit; first by eauto. iIntros (v2' σ2 efs Hstep); inv_head_step.
  iModIntro; iSplit=> //. iFrame. by iApply "HΦ".
Qed.
209

210
Lemma wp_cas_suc s E l e1 v1 e2 v2 :
Ralf Jung's avatar
Ralf Jung committed
211
  IntoVal e1 v1  IntoVal e2 v2  vals_cas_compare_safe v1 v1 
212
  {{{  l  v1 }}} CAS (Lit (LitLoc l)) e1 e2 @ s; E
213 214
  {{{ RET LitV (LitBool true); l  v2 }}}.
Proof.
Ralf Jung's avatar
Ralf Jung committed
215
  iIntros (<- <- ? Φ) ">Hl HΦ".
216 217 218 219 220 221
  iApply wp_lift_atomic_head_step_no_fork; auto.
  iIntros (σ1) "Hσ !>". iDestruct (@gen_heap_valid with "Hσ Hl") as %?.
  iSplit; first by eauto. iNext; iIntros (v2' σ2 efs Hstep); inv_head_step.
  iMod (@gen_heap_update with "Hσ Hl") as "[$ Hl]".
  iModIntro. iSplit=>//. by iApply "HΦ".
Qed.
222
Lemma twp_cas_suc s E l e1 v1 e2 v2 :
Ralf Jung's avatar
Ralf Jung committed
223
  IntoVal e1 v1  IntoVal e2 v2  vals_cas_compare_safe v1 v1 
224 225 226
  [[{ l  v1 }]] CAS (Lit (LitLoc l)) e1 e2 @ s; E
  [[{ RET LitV (LitBool true); l  v2 }]].
Proof.
Ralf Jung's avatar
Ralf Jung committed
227
  iIntros (<- <- ? Φ) "Hl HΦ".
228 229 230 231 232 233
  iApply twp_lift_atomic_head_step_no_fork; auto.
  iIntros (σ1) "Hσ !>". iDestruct (@gen_heap_valid with "Hσ Hl") as %?.
  iSplit; first by eauto. iIntros (v2' σ2 efs Hstep); inv_head_step.
  iMod (@gen_heap_update with "Hσ Hl") as "[$ Hl]".
  iModIntro. iSplit=>//. by iApply "HΦ".
Qed.
234

Ralf Jung's avatar
Ralf Jung committed
235
Lemma wp_faa s E l i1 e2 i2 :
236
  IntoVal e2 (LitV (LitInt i2)) 
Ralf Jung's avatar
Ralf Jung committed
237
  {{{  l  LitV (LitInt i1) }}} FAA (Lit (LitLoc l)) e2 @ s; E
238 239
  {{{ RET LitV (LitInt i1); l  LitV (LitInt (i1 + i2)) }}}.
Proof.
240
  iIntros (<- Φ) ">Hl HΦ".
241 242 243 244 245 246
  iApply wp_lift_atomic_head_step_no_fork; auto.
  iIntros (σ1) "Hσ !>". iDestruct (@gen_heap_valid with "Hσ Hl") as %?.
  iSplit; first by eauto. iNext; iIntros (v2' σ2 efs Hstep); inv_head_step.
  iMod (@gen_heap_update with "Hσ Hl") as "[$ Hl]".
  iModIntro. iSplit=>//. by iApply "HΦ".
Qed.
247 248 249 250 251
Lemma twp_faa s E l i1 e2 i2 :
  IntoVal e2 (LitV (LitInt i2)) 
  [[{ l  LitV (LitInt i1) }]] FAA (Lit (LitLoc l)) e2 @ s; E
  [[{ RET LitV (LitInt i1); l  LitV (LitInt (i1 + i2)) }]].
Proof.
252
  iIntros (<- Φ) "Hl HΦ".
253 254 255 256 257 258
  iApply twp_lift_atomic_head_step_no_fork; auto.
  iIntros (σ1) "Hσ !>". iDestruct (@gen_heap_valid with "Hσ Hl") as %?.
  iSplit; first by eauto. iIntros (v2' σ2 efs Hstep); inv_head_step.
  iMod (@gen_heap_update with "Hσ Hl") as "[$ Hl]".
  iModIntro. iSplit=>//. by iApply "HΦ".
Qed.
Ralf Jung's avatar
Ralf Jung committed
259
End lifting.