lifting.v 5.68 KB
Newer Older
1
From program_logic Require Export weakestpre.
2
From heap_lang Require Export lang.
3
4
5
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
Context {Σ : iFunctor}.
12
13
Implicit Types P Q : iProp heap_lang Σ.
Implicit Types Φ : val  iProp heap_lang Σ.
14
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 Φ :
19
  || e @ E {{ λ v, || fill K (of_val v) @ E {{ Φ }}}}  || fill K e @ E {{ Φ }}.
20
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 Φ :
24
  to_val e = Some v 
25
  (ownP σ   ( l, σ !! l = None  ownP (<[l:=v]>σ) - Φ (LocV l)))
26
   || Alloc e @ E {{ Φ }}.
27
Proof.
28
  (* TODO RJ: This works around ssreflect bug #22. *)
29
30
  intros. set (φ v' σ' ef :=  l,
    ef = None  v' = LocV l  σ' = <[l:=v]>σ  σ !! l = None).
31
  rewrite -(wp_lift_atomic_step (Alloc e) φ σ) // /φ;
Ralf Jung's avatar
Ralf Jung committed
32
    last by intros; inv_step; eauto 8.
33
  apply sep_mono, later_mono; first done.
34
35
  apply forall_intro=>e2; apply forall_intro=>σ2; apply forall_intro=>ef.
  apply wand_intro_l.
36
  rewrite always_and_sep_l -assoc -always_and_sep_l.
37
38
  apply const_elim_l=>-[l [-> [-> [-> ?]]]].
  by rewrite (forall_elim l) right_id const_equiv // left_id wand_elim_r.
39
Qed.
40

41
Lemma wp_load_pst E σ l v Φ :
Ralf Jung's avatar
Ralf Jung committed
42
  σ !! l = Some v 
43
  (ownP σ   (ownP σ - Φ v))  || Load (Loc l) @ E {{ Φ }}.
Ralf Jung's avatar
Ralf Jung committed
44
Proof.
45
46
  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
47
Qed.
48

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

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

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

76
(** Base axioms for core primitives of the language: Stateless reductions *)
77
Lemma wp_fork E e Φ :
78
  ( Φ (LitV LitUnit)   || e {{ λ _, True }})  || Fork e @ E {{ Φ }}.
79
Proof.
80
  rewrite -(wp_lift_pure_det_step (Fork e) (Lit LitUnit) (Some e)) //=;
81
    last by intros; inv_step; eauto.
82
  rewrite later_sep -(wp_value _ _ (Lit _)) //.
83
Qed.
84

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

97
Lemma wp_un_op E op l l' Φ :
98
  un_op_eval op l = Some l' 
99
   Φ (LitV l')  || UnOp op (Lit l) @ E {{ Φ }}.
100
Proof.
101
  intros. rewrite -(wp_lift_pure_det_step (UnOp op _) (Lit l') None)
102
    ?right_id -?wp_value //; intros; inv_step; eauto.
Ralf Jung's avatar
Ralf Jung committed
103
Qed.
104

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

113
Lemma wp_if_true E e1 e2 Φ :
114
   || e1 @ E {{ Φ }}  || If (Lit (LitBool true)) e1 e2 @ E {{ Φ }}.
Ralf Jung's avatar
Ralf Jung committed
115
Proof.
116
  rewrite -(wp_lift_pure_det_step (If _ _ _) e1 None)
117
    ?right_id //; intros; inv_step; eauto.
118
119
Qed.

120
Lemma wp_if_false E e1 e2 Φ :
121
   || e2 @ E {{ Φ }}  || If (Lit (LitBool false)) e1 e2 @ E {{ Φ }}.
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 Φ :
128
  to_val e1 = Some v1  to_val e2 = Some v2 
129
   Φ v1  || Fst (Pair e1 e2) @ E {{ Φ }}.
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 Φ :
136
  to_val e1 = Some v1  to_val e2 = Some v2 
137
   Φ v2  || Snd (Pair e1 e2) @ E {{ Φ }}.
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 Φ :
144
  to_val e0 = Some v0 
145
   || subst e1 x1 v0 @ E {{ Φ }}  || Case (InjL e0) x1 e1 x2 e2 @ E {{ Φ }}.
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 Φ :
152
  to_val e0 = Some v0 
153
   || subst e2 x2 v0 @ E {{ Φ }}  || Case (InjR e0) x1 e1 x2 e2 @ E {{ Φ }}.
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.