lifting.v 5.71 KB
Newer Older
1 2 3 4 5
From program_logic Require Export weakestpre.
From heap_lang Require Export heap_lang.
From program_logic Require Import lifting.
From program_logic Require Import ownership. (* for ownP *)
From heap_lang Require Import tactics.
6 7
Export heap_lang. (* Prefer heap_lang names over language names. *)
Import uPred.
8
Local Hint Extern 0 (language.reducible _ _) => do_step ltac:(eauto 2).
9

10
Section lifting.
11 12
Context {Σ : iFunctor}.
Implicit Types P : iProp heap_lang Σ.
13 14
Implicit Types Q : val  iProp heap_lang Σ.
Implicit Types K : ectx.
15
Implicit Types ef : option expr.
Ralf Jung's avatar
Ralf Jung committed
16 17

(** Bind. *)
18
Lemma wp_bind {E e} K Q :
19
  wp E e (λ v, wp E (fill K (of_val v)) Q)  wp E (fill K e) Q.
20 21 22 23 24
Proof. apply weakestpre.wp_bind. Qed.

Lemma wp_bindi {E e} Ki Q :
  wp E e (λ v, wp E (fill_item Ki (of_val v)) Q)  wp E (fill_item Ki e) Q.
Proof. apply weakestpre.wp_bind. Qed.
Ralf Jung's avatar
Ralf Jung committed
25

26
(** Base axioms for core primitives of the language: Stateful reductions. *)
27
Lemma wp_alloc_pst E σ e v Q :
28
  to_val e = Some v 
29
  (ownP σ   ( l, σ !! l = None  ownP (<[l:=v]>σ) - Q (LocV l)))
30
        wp E (Alloc e) Q.
31
Proof.
32
  (* TODO RJ: This works around ssreflect bug #22. *)
33 34
  intros. set (φ v' σ' ef :=  l,
    ef = None  v' = LocV l  σ' = <[l:=v]>σ  σ !! l = None).
35
  rewrite -(wp_lift_atomic_step (Alloc e) φ σ) // /φ;
Ralf Jung's avatar
Ralf Jung committed
36
    last by intros; inv_step; eauto 8.
37
  apply sep_mono, later_mono; first done.
38 39
  apply forall_intro=>e2; apply forall_intro=>σ2; apply forall_intro=>ef.
  apply wand_intro_l.
40
  rewrite always_and_sep_l -assoc -always_and_sep_l.
41 42
  apply const_elim_l=>-[l [-> [-> [-> ?]]]].
  by rewrite (forall_elim l) right_id const_equiv // left_id wand_elim_r.
43
Qed.
44

45
Lemma wp_load_pst E σ l v Q :
Ralf Jung's avatar
Ralf Jung committed
46
  σ !! l = Some v 
47
  (ownP σ   (ownP σ - Q v))  wp E (Load (Loc l)) Q.
Ralf Jung's avatar
Ralf Jung committed
48
Proof.
49 50
  intros. rewrite -(wp_lift_atomic_det_step σ v σ None) ?right_id //;
    last by intros; inv_step; eauto using to_of_val.
Ralf Jung's avatar
Ralf Jung committed
51
Qed.
52

53
Lemma wp_store_pst E σ l e v v' Q :
54
  to_val e = Some v  σ !! l = Some v' 
55
  (ownP σ   (ownP (<[l:=v]>σ) - Q (LitV LitUnit)))  wp E (Store (Loc l) e) Q.
Ralf Jung's avatar
Ralf Jung committed
56
Proof.
57
  intros. rewrite -(wp_lift_atomic_det_step σ (LitV LitUnit) (<[l:=v]>σ) None)
58
    ?right_id //; last by intros; inv_step; eauto.
Ralf Jung's avatar
Ralf Jung committed
59
Qed.
60

61
Lemma wp_cas_fail_pst E σ l e1 v1 e2 v2 v' Q :
62
  to_val e1 = Some v1  to_val e2 = Some v2  σ !! l = Some v'  v'  v1 
Ralf Jung's avatar
Ralf Jung committed
63
  (ownP σ   (ownP σ - Q (LitV $ LitBool false)))  wp E (Cas (Loc l) e1 e2) Q.
Ralf Jung's avatar
Ralf Jung committed
64
Proof.
Ralf Jung's avatar
Ralf Jung committed
65
  intros. rewrite -(wp_lift_atomic_det_step σ (LitV $ LitBool false) σ None)
66
    ?right_id //; last by intros; inv_step; eauto.
Ralf Jung's avatar
Ralf Jung committed
67
Qed.
68

69
Lemma wp_cas_suc_pst E σ l e1 v1 e2 v2 Q :
70
  to_val e1 = Some v1  to_val e2 = Some v2  σ !! l = Some v1 
Ralf Jung's avatar
Ralf Jung committed
71
  (ownP σ   (ownP (<[l:=v2]>σ) - Q (LitV $ LitBool true)))
72
   wp E (Cas (Loc l) e1 e2) Q.
Ralf Jung's avatar
Ralf Jung committed
73
Proof.
Ralf Jung's avatar
Ralf Jung committed
74
  intros. rewrite -(wp_lift_atomic_det_step σ (LitV $ LitBool true) (<[l:=v2]>σ) None)
75
    ?right_id //; last by intros; inv_step; eauto.
Ralf Jung's avatar
Ralf Jung committed
76 77
Qed.

78 79
(** Base axioms for core primitives of the language: Stateless reductions *)
Lemma wp_fork E e :
80
   wp (Σ:=Σ) coPset_all e (λ _, True)  wp E (Fork e) (λ v, v = LitV LitUnit).
81
Proof.
82
  rewrite -(wp_lift_pure_det_step (Fork e) (Lit LitUnit) (Some e)) //=;
83
    last by intros; inv_step; eauto.
84
  apply later_mono, sep_intro_True_l; last done.
85
  by rewrite -(wp_value _ _ (Lit _)) //; apply const_intro.
86
Qed.
87

88 89 90
(* For the lemmas involving substitution, we only derive a preliminary version.
   The final version is defined in substitution.v. *)
Lemma wp_rec' E f x e1 e2 v Q :
91
  to_val e2 = Some v 
92
   wp E (subst (subst e1 f (RecV f x e1)) x v) Q  wp E (App (Rec f x e1) e2) Q.
93
Proof.
94
  intros. rewrite -(wp_lift_pure_det_step (App _ _)
95
    (subst (subst e1 f (RecV f x e1)) x v) None) ?right_id //=;
96
    intros; inv_step; eauto.
97
Qed.
98

99 100 101
Lemma wp_un_op E op l l' Q :
  un_op_eval op l = Some l' 
   Q (LitV l')  wp E (UnOp op (Lit l)) Q.
102
Proof.
103
  intros. rewrite -(wp_lift_pure_det_step (UnOp op _) (Lit l') None)
104
    ?right_id -?wp_value //; intros; inv_step; eauto.
Ralf Jung's avatar
Ralf Jung committed
105
Qed.
106

107 108 109
Lemma wp_bin_op E op l1 l2 l' Q :
  bin_op_eval op l1 l2 = Some l' 
   Q (LitV l')  wp E (BinOp op (Lit l1) (Lit l2)) Q.
Ralf Jung's avatar
Ralf Jung committed
110
Proof.
111
  intros Heval. rewrite -(wp_lift_pure_det_step (BinOp op _ _) (Lit l') None)
112
    ?right_id -?wp_value //; intros; inv_step; eauto.
Ralf Jung's avatar
Ralf Jung committed
113
Qed.
114

Ralf Jung's avatar
Ralf Jung committed
115
Lemma wp_if_true E e1 e2 Q :  wp E e1 Q  wp E (If (Lit $ LitBool true) e1 e2) Q.
Ralf Jung's avatar
Ralf Jung committed
116
Proof.
117
  rewrite -(wp_lift_pure_det_step (If _ _ _) e1 None)
118
    ?right_id //; intros; inv_step; eauto.
119 120
Qed.

Ralf Jung's avatar
Ralf Jung committed
121
Lemma wp_if_false E e1 e2 Q :  wp E e2 Q  wp E (If (Lit $ LitBool false) e1 e2) Q.
122 123
Proof.
  rewrite -(wp_lift_pure_det_step (If _ _ _) e2 None)
124
    ?right_id //; intros; inv_step; eauto.
125
Qed.
126

127
Lemma wp_fst E e1 v1 e2 v2 Q :
128
  to_val e1 = Some v1  to_val e2 = Some v2 
129
   Q v1  wp E (Fst $ Pair e1 e2) Q.
Ralf Jung's avatar
Ralf Jung committed
130
Proof.
131
  intros. rewrite -(wp_lift_pure_det_step (Fst _) e1 None)
132
    ?right_id -?wp_value //; intros; inv_step; eauto.
Ralf Jung's avatar
Ralf Jung committed
133
Qed.
134

135
Lemma wp_snd E e1 v1 e2 v2 Q :
136
  to_val e1 = Some v1  to_val e2 = Some v2 
137
   Q v2  wp E (Snd $ Pair e1 e2) Q.
Ralf Jung's avatar
Ralf Jung committed
138
Proof.
139
  intros. rewrite -(wp_lift_pure_det_step (Snd _) e2 None)
140
    ?right_id -?wp_value //; intros; inv_step; eauto.
Ralf Jung's avatar
Ralf Jung committed
141
Qed.
142

143
Lemma wp_case_inl' E e0 v0 x1 e1 x2 e2 Q :
144
  to_val e0 = Some v0 
145
   wp E (subst e1 x1 v0) Q  wp E (Case (InjL e0) x1 e1 x2 e2) Q.
Ralf Jung's avatar
Ralf Jung committed
146
Proof.
147 148
  intros. rewrite -(wp_lift_pure_det_step (Case _ _ _ _ _)
    (subst e1 x1 v0) None) ?right_id //; intros; inv_step; eauto.
Ralf Jung's avatar
Ralf Jung committed
149
Qed.
150

151
Lemma wp_case_inr' E e0 v0 x1 e1 x2 e2 Q :
152
  to_val e0 = Some v0 
153
   wp E (subst e2 x2 v0) Q  wp E (Case (InjR e0) x1 e1 x2 e2) Q.
Ralf Jung's avatar
Ralf Jung committed
154
Proof.
155 156
  intros. rewrite -(wp_lift_pure_det_step (Case _ _ _ _ _)
    (subst e2 x2 v0) None) ?right_id //; intros; inv_step; eauto.
Ralf Jung's avatar
Ralf Jung committed
157
Qed.
158

159
End lifting.