lifting.v 5.48 KB
Newer Older
1 2
Require Import prelude.gmap program_logic.lifting.
Require Export program_logic.weakestpre heap_lang.heap_lang_tactics.
3
Import uPred.
4
Import heap_lang.
5
Local Hint Extern 0 (language.reducible _ _) => do_step ltac:(eauto 2).
6

7
Section lifting.
8 9
Context {Σ : iFunctor}.
Implicit Types P : iProp heap_lang Σ.
10 11
Implicit Types Q : val  iProp heap_lang Σ.
Implicit Types K : ectx.
Ralf Jung's avatar
Ralf Jung committed
12 13

(** Bind. *)
14
Lemma wp_bind {E e} K Q :
15
  wp E e (λ v, wp E (fill K (of_val v)) Q)  wp E (fill K e) Q.
16 17 18 19 20
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
21

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

40
Lemma wp_load_pst E σ l v Q :
Ralf Jung's avatar
Ralf Jung committed
41
  σ !! l = Some v 
42
  (ownP σ   (ownP σ - Q v))  wp E (Load (Loc l)) Q.
Ralf Jung's avatar
Ralf Jung committed
43
Proof.
44
  intros; rewrite -(wp_lift_atomic_det_step σ v σ None) ?right_id //;
45
    last (by intros; inv_step; eauto).
Ralf Jung's avatar
Ralf Jung committed
46
Qed.
47

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

57
Lemma wp_cas_fail_pst E σ l e1 v1 e2 v2 v' Q :
58 59
  to_val e1 = Some v1  to_val e2 = Some v2  σ !! l = Some v'  v'  v1 
  (ownP σ   (ownP σ - Q LitFalseV))  wp E (Cas (Loc l) e1 e2) Q.
Ralf Jung's avatar
Ralf Jung committed
60
Proof.
61
  intros; rewrite -(wp_lift_atomic_det_step σ LitFalseV σ None) ?right_id //;
62
    last by intros; inv_step; eauto.
Ralf Jung's avatar
Ralf Jung committed
63
Qed.
64

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

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

84
Lemma wp_rec E ef e v Q :
85 86
  to_val e = Some v 
   wp E ef.[Rec ef, e /] Q  wp E (App (Rec ef) e) Q.
87
Proof.
88 89
  intros; rewrite -(wp_lift_pure_det_step (App _ _) ef.[Rec ef, e /] None)
                     ?right_id //=;
90
    last by intros; inv_step; eauto.
91
Qed.
92

93
Lemma wp_plus E n1 n2 Q :
94
   Q (LitNatV (n1 + n2))  wp E (Plus (LitNat n1) (LitNat n2)) Q.
95
Proof.
96
  rewrite -(wp_lift_pure_det_step (Plus _ _) (LitNat (n1 + n2)) None) ?right_id //;
97 98
    last by intros; inv_step; eauto.
  by rewrite -wp_value'.
Ralf Jung's avatar
Ralf Jung committed
99
Qed.
100

101
Lemma wp_le_true E n1 n2 Q :
Ralf Jung's avatar
Ralf Jung committed
102
  n1  n2 
103
   Q LitTrueV  wp E (Le (LitNat n1) (LitNat n2)) Q.
Ralf Jung's avatar
Ralf Jung committed
104
Proof.
105
  intros; rewrite -(wp_lift_pure_det_step (Le _ _) LitTrue None) ?right_id //;
Ralf Jung's avatar
Ralf Jung committed
106
    last by intros; inv_step; eauto with omega.
107
  by rewrite -wp_value'.
Ralf Jung's avatar
Ralf Jung committed
108
Qed.
109

110
Lemma wp_le_false E n1 n2 Q :
Ralf Jung's avatar
Ralf Jung committed
111
  n1 > n2 
112
   Q LitFalseV  wp E (Le (LitNat n1) (LitNat n2)) Q.
Ralf Jung's avatar
Ralf Jung committed
113
Proof.
114
  intros; rewrite -(wp_lift_pure_det_step (Le _ _) LitFalse None) ?right_id //;
Ralf Jung's avatar
Ralf Jung committed
115
    last by intros; inv_step; eauto with omega.
116
  by rewrite -wp_value'.
117
Qed.
118

119
Lemma wp_fst E e1 v1 e2 v2 Q :
120
  to_val e1 = Some v1  to_val e2 = Some v2 
121
  Q v1  wp E (Fst (Pair e1 e2)) Q.
Ralf Jung's avatar
Ralf Jung committed
122
Proof.
123
  intros; rewrite -(wp_lift_pure_det_step (Fst _) e1 None) ?right_id //;
124 125
    last by intros; inv_step; eauto.
  by rewrite -wp_value'.
Ralf Jung's avatar
Ralf Jung committed
126
Qed.
127

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

137
Lemma wp_case_inl E e0 v0 e1 e2 Q :
138 139
  to_val e0 = Some v0 
   wp E e1.[e0/] Q  wp E (Case (InjL e0) e1 e2) Q.
Ralf Jung's avatar
Ralf Jung committed
140
Proof.
141
  intros; rewrite -(wp_lift_pure_det_step (Case _ _ _) e1.[e0/] None) ?right_id //;
142
    last by intros; inv_step; eauto.
Ralf Jung's avatar
Ralf Jung committed
143
Qed.
144

145
Lemma wp_case_inr E e0 v0 e1 e2 Q :
146 147
  to_val e0 = Some v0 
   wp E e2.[e0/] Q  wp E (Case (InjR e0) e1 e2) Q.
Ralf Jung's avatar
Ralf Jung committed
148
Proof.
149
  intros; rewrite -(wp_lift_pure_det_step (Case _ _ _) e2.[e0/] None) ?right_id //;
150
    last by intros; inv_step; eauto.
Ralf Jung's avatar
Ralf Jung committed
151
Qed.
152

Ralf Jung's avatar
Ralf Jung committed
153
(** Some derived stateless axioms *)
154
Lemma wp_le E n1 n2 P Q :
155 156
  (n1  n2  P   Q LitTrueV) 
  (n1 > n2  P   Q LitFalseV) 
157
  P  wp E (Le (LitNat n1) (LitNat n2)) Q.
Ralf Jung's avatar
Ralf Jung committed
158
Proof.
159 160
  intros; destruct (decide (n1  n2)).
  * rewrite -wp_le_true; auto.
Ralf Jung's avatar
Ralf Jung committed
161
  * rewrite -wp_le_false; auto with omega.
Ralf Jung's avatar
Ralf Jung committed
162
Qed.
163

164
End lifting.