lifting.v 5.92 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
  ( ownP σ   (ownP (<[l:=v]>σ) - Φ (LitV LitUnit)))
52
   || 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
  ( ownP σ   (ownP σ - Φ (LitV $ LitBool false)))
61
   || 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
98
99
100
101
102
103
Lemma wp_rec' E f x erec v1 e2 v2 Φ :
  v1 = RecV f x erec 
  to_val e2 = Some v2 
   || subst (subst erec f v1) x v2 @ E {{ Φ }}
   || App (of_val v1) e2 @ E {{ Φ }}.
Proof. intros ->. apply wp_rec. Qed.

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

112
Lemma wp_bin_op E op l1 l2 l' Φ :
113
  bin_op_eval op l1 l2 = Some l' 
114
   Φ (LitV l')  || BinOp op (Lit l1) (Lit l2) @ E {{ Φ }}.
Ralf Jung's avatar
Ralf Jung committed
115
Proof.
116
  intros Heval. rewrite -(wp_lift_pure_det_step (BinOp op _ _) (Lit l') None)
117
    ?right_id -?wp_value //; intros; inv_step; eauto.
Ralf Jung's avatar
Ralf Jung committed
118
Qed.
119

120
Lemma wp_if_true E e1 e2 Φ :
121
   || e1 @ E {{ Φ }}  || If (Lit (LitBool true)) e1 e2 @ E {{ Φ }}.
Ralf Jung's avatar
Ralf Jung committed
122
Proof.
123
  rewrite -(wp_lift_pure_det_step (If _ _ _) e1 None)
124
    ?right_id //; intros; inv_step; eauto.
125
126
Qed.

127
Lemma wp_if_false E e1 e2 Φ :
128
   || e2 @ E {{ Φ }}  || If (Lit (LitBool false)) e1 e2 @ E {{ Φ }}.
129
130
Proof.
  rewrite -(wp_lift_pure_det_step (If _ _ _) e2 None)
131
    ?right_id //; intros; inv_step; eauto.
132
Qed.
133

134
Lemma wp_fst E e1 v1 e2 v2 Φ :
135
  to_val e1 = Some v1  to_val e2 = Some v2 
136
   Φ v1  || Fst (Pair e1 e2) @ E {{ Φ }}.
Ralf Jung's avatar
Ralf Jung committed
137
Proof.
138
  intros. rewrite -(wp_lift_pure_det_step (Fst _) e1 None)
139
    ?right_id -?wp_value //; intros; inv_step; eauto.
Ralf Jung's avatar
Ralf Jung committed
140
Qed.
141

142
Lemma wp_snd E e1 v1 e2 v2 Φ :
143
  to_val e1 = Some v1  to_val e2 = Some v2 
144
   Φ v2  || Snd (Pair e1 e2) @ E {{ Φ }}.
Ralf Jung's avatar
Ralf Jung committed
145
Proof.
146
  intros. rewrite -(wp_lift_pure_det_step (Snd _) e2 None)
147
    ?right_id -?wp_value //; intros; inv_step; eauto.
Ralf Jung's avatar
Ralf Jung committed
148
Qed.
149

150
Lemma wp_case_inl E e0 v0 x1 e1 x2 e2 Φ :
151
  to_val e0 = Some v0 
152
   || subst e1 x1 v0 @ E {{ Φ }}  || Case (InjL e0) x1 e1 x2 e2 @ E {{ Φ }}.
Ralf Jung's avatar
Ralf Jung committed
153
Proof.
154
155
  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
156
Qed.
157

158
Lemma wp_case_inr E e0 v0 x1 e1 x2 e2 Φ :
159
  to_val e0 = Some v0 
160
   || subst e2 x2 v0 @ E {{ Φ }}  || Case (InjR e0) x1 e1 x2 e2 @ E {{ Φ }}.
Ralf Jung's avatar
Ralf Jung committed
161
Proof.
162
163
  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
164
Qed.
165

166
End lifting.