heap.v 8.48 KB
Newer Older
1
From heap_lang Require Export lifting.
2
From algebra Require Import upred_big_op.
3
From program_logic Require Export invariants ghost_ownership.
4
5
6
7
8
9
From program_logic Require Import ownership auth.
Import uPred.
(* TODO: The entire construction could be generalized to arbitrary languages that have
   a finmap as their state. Or maybe even beyond "as their state", i.e. arbitrary
   predicates over finmaps instead of just ownP. *)

10
Definition heapRA := mapRA loc (exclRA (leibnizC val)).
11
Definition heapF := authF heapRA.
12

13
14
15
16
17
18
Class heapG Σ := HeapG {
  heap_inG : inG heap_lang Σ (authRA heapRA);
  heap_name : gname
}.
Instance heap_authG `{i : heapG Σ} : authG heap_lang Σ heapRA :=
  {| auth_inG := heap_inG |}.
19

20
Definition to_heap : state  heapRA := fmap Excl.
21
Definition of_heap : heapRA  state := omap (maybe Excl).
22

23
Definition heap_mapsto `{heapG Σ} (l : loc) (v: val) : iPropG heap_lang Σ :=
24
  auth_own heap_name ({[ l := Excl v ]} : heapRA).
25
26
27
28
29
Definition heap_inv `{i : heapG Σ} (h : heapRA) : iPropG heap_lang Σ :=
  ownP (of_heap h).
Definition heap_ctx `{i : heapG Σ} (N : namespace) : iPropG heap_lang Σ :=
  auth_ctx heap_name N heap_inv.

30
Notation "l ↦ v" := (heap_mapsto l v) (at level 20) : uPred_scope.
31

32
Section heap.
33
  Context {Σ : iFunctorG}.
34
  Implicit Types N : namespace.
35
36
  Implicit Types P Q : iPropG heap_lang Σ.
  Implicit Types Φ : val  iPropG heap_lang Σ.
37
38
  Implicit Types σ : state.
  Implicit Types h g : heapRA.
39

40
  (** Conversion to heaps and back *)
41
  Global Instance of_heap_proper : Proper (() ==> (=)) of_heap.
42
  Proof. by intros ??; fold_leibniz=>->. Qed.
43
  Lemma from_to_heap σ : of_heap (to_heap σ) = σ.
44
  Proof.
Robbert Krebbers's avatar
Robbert Krebbers committed
45
46
47
48
    apply map_eq=>l. rewrite lookup_omap lookup_fmap. by case (σ !! l).
  Qed.
  Lemma to_heap_valid σ :  to_heap σ.
  Proof. intros n l. rewrite lookup_fmap. by case (σ !! l). Qed.
49
  Lemma of_heap_insert l v h : of_heap (<[l:=Excl v]> h) = <[l:=v]> (of_heap h).
50
  Proof. by rewrite /of_heap -(omap_insert _ _ _ (Excl v)). Qed.
51
52
  Lemma to_heap_insert l v σ : to_heap (<[l:=v]> σ) = <[l:=Excl v]> (to_heap σ).
  Proof. by rewrite /to_heap -fmap_insert. Qed.
53
54
  Lemma of_heap_None h l :
     h  of_heap h !! l = None  h !! l = None  h !! l  Some ExclUnit.
55
  Proof.
56
    move=> /(_ O l). rewrite /of_heap lookup_omap.
57
58
59
    by case: (h !! l)=> [[]|]; auto.
  Qed.
  Lemma heap_singleton_inv_l h l v :
60
     ({[l := Excl v]}  h)  h !! l = None  h !! l  Some ExclUnit.
61
62
63
64
  Proof.
    move=> /(_ O l). rewrite lookup_op lookup_singleton.
    by case: (h !! l)=> [[]|]; auto.
  Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
65

66
  (** Allocation *)
67
68
  Lemma heap_alloc E N σ :
    authG heap_lang Σ heapRA  nclose N  E 
69
    ownP σ  (|={E}=>  _ : heapG Σ, heap_ctx N  Π★{map σ} heap_mapsto).
Ralf Jung's avatar
Ralf Jung committed
70
  Proof.
71
    intros. rewrite -{1}(from_to_heap σ). etrans.
72
    { rewrite [ownP _]later_intro.
73
74
      apply (auth_alloc (ownP  of_heap) E N (to_heap σ)); last done.
      apply to_heap_valid. }
75
76
    apply pvs_mono, exist_elim=> γ.
    rewrite -(exist_intro (HeapG _ _ γ)); apply and_mono_r.
77
78
79
80
81
82
    induction σ as [|l v σ Hl IH] using map_ind.
    { rewrite big_sepM_empty; apply True_intro. }
    rewrite to_heap_insert big_sepM_insert //.
    rewrite (map_insert_singleton_op (to_heap σ));
      last rewrite lookup_fmap Hl; auto.
    by rewrite auto_own_op IH.
Ralf Jung's avatar
Ralf Jung committed
83
  Qed.
Ralf Jung's avatar
Ralf Jung committed
84

85
86
87
88
89
90
91
  Context `{heapG Σ}.

  (** Propers *)
  Global Instance heap_inv_proper : Proper (() ==> ()) heap_inv.
  Proof. intros h1 h2. by fold_leibniz=> ->. Qed.

  (** General properties of mapsto *)
92
  Lemma heap_mapsto_disjoint l v1 v2 : (l  v1  l  v2)%I  False.
93
94
95
96
97
98
  Proof.
    rewrite /heap_mapsto -auto_own_op auto_own_valid map_op_singleton.
    rewrite map_validI (forall_elim l) lookup_singleton.
    by rewrite option_validI excl_validI.
  Qed.

99
  (** Weakest precondition *)
100
  Lemma wp_alloc N E e v P Φ :
101
    to_val e = Some v  nclose N  E 
102
    P  heap_ctx N 
103
    P  (  l, l  v - Φ (LocV l)) 
104
    P  || Alloc e @ E {{ Φ }}.
Ralf Jung's avatar
Ralf Jung committed
105
  Proof.
106
    rewrite /heap_ctx /heap_inv /heap_mapsto=> ?? Hctx HP.
107
    trans (|={E}=> auth_own heap_name ( : heapRA)  P)%I.
108
109
110
    { by rewrite -pvs_frame_r -(auth_empty _ E) left_id. }
    apply wp_strip_pvs, (auth_fsa heap_inv (wp_fsa (Alloc e)))
      with N heap_name ; simpl; eauto with I.
111
    rewrite -later_intro. apply sep_mono_r,forall_intro=> h; apply wand_intro_l.
112
    rewrite -assoc left_id; apply const_elim_sep_l=> ?.
113
    rewrite -(wp_alloc_pst _ (of_heap h)) //.
114
    apply sep_mono_r; rewrite HP; apply later_mono.
Ralf Jung's avatar
Ralf Jung committed
115
    apply forall_mono=> l; apply wand_intro_l.
116
    rewrite always_and_sep_l -assoc; apply const_elim_sep_l=> ?.
117
    rewrite -(exist_intro (op {[ l := Excl v ]})).
118
    repeat erewrite <-exist_intro by apply _; simpl.
119
    rewrite -of_heap_insert left_id right_id !assoc.
120
    apply sep_mono_l.
121
    rewrite -(map_insert_singleton_op h); last by apply of_heap_None.
122
123
    rewrite const_equiv ?left_id; last by apply (map_insert_valid h).
    apply later_intro.
124
125
  Qed.

126
  Lemma wp_load N E l v P Φ :
Ralf Jung's avatar
Ralf Jung committed
127
    nclose N  E 
128
    P  heap_ctx N 
129
    P  ( l  v   (l  v - Φ v)) 
130
    P  || Load (Loc l) @ E {{ Φ }}.
Ralf Jung's avatar
Ralf Jung committed
131
  Proof.
132
    rewrite /heap_ctx /heap_inv /heap_mapsto=>HN ? HPΦ.
133
    apply (auth_fsa' heap_inv (wp_fsa _) id)
134
      with N heap_name {[ l := Excl v ]}; simpl; eauto with I.
135
    rewrite HPΦ{HPΦ}; apply sep_mono_r, forall_intro=> h; apply wand_intro_l.
136
    rewrite -assoc; apply const_elim_sep_l=> ?.
137
    rewrite -(wp_load_pst _ (<[l:=v]>(of_heap h))) ?lookup_insert //.
138
139
    rewrite const_equiv // left_id.
    rewrite -(map_insert_singleton_op h); last by eapply heap_singleton_inv_l.
140
    rewrite -of_heap_insert.
141
    apply sep_mono_r, later_mono, wand_intro_l. by rewrite -later_intro.
Ralf Jung's avatar
Ralf Jung committed
142
143
  Qed.

144
  Lemma wp_store N E l v' e v P Φ :
145
    to_val e = Some v  nclose N  E  
146
    P  heap_ctx N 
147
    P  ( l  v'   (l  v - Φ (LitV LitUnit))) 
148
    P  || Store (Loc l) e @ E {{ Φ }}.
Ralf Jung's avatar
Ralf Jung committed
149
  Proof.
150
    rewrite /heap_ctx /heap_inv /heap_mapsto=>? HN ? HPΦ.
151
    apply (auth_fsa' heap_inv (wp_fsa _) (alter (λ _, Excl v) l))
152
      with N heap_name {[ l := Excl v' ]}; simpl; eauto with I.
153
    rewrite HPΦ{HPΦ}; apply sep_mono_r, forall_intro=> h; apply wand_intro_l.
154
    rewrite -assoc; apply const_elim_sep_l=> ?.
155
    rewrite -(wp_store_pst _ (<[l:=v']>(of_heap h))) ?lookup_insert //.
156
157
    rewrite /heap_inv alter_singleton insert_insert.
    rewrite -!(map_insert_singleton_op h); try by eapply heap_singleton_inv_l.
158
    rewrite -!of_heap_insert const_equiv;
159
160
      last (split; [naive_solver|by eapply map_insert_valid, cmra_valid_op_r]).
    apply sep_mono_r, later_mono, wand_intro_l. by rewrite left_id -later_intro.
Ralf Jung's avatar
Ralf Jung committed
161
162
  Qed.

163
  Lemma wp_cas_fail N E l v' e1 v1 e2 v2 P Φ :
Ralf Jung's avatar
Ralf Jung committed
164
165
    to_val e1 = Some v1  to_val e2 = Some v2  v'  v1 
    nclose N  E 
166
    P  heap_ctx N 
167
    P  ( l  v'   (l  v' - Φ (LitV (LitBool false)))) 
168
    P  || Cas (Loc l) e1 e2 @ E {{ Φ }}.
Ralf Jung's avatar
Ralf Jung committed
169
  Proof.
170
    rewrite /heap_ctx /heap_inv /heap_mapsto=>??? HN ? HPΦ.
171
    apply (auth_fsa' heap_inv (wp_fsa _) id)
172
      with N heap_name {[ l := Excl v' ]}; simpl; eauto 10 with I.
173
    rewrite HPΦ{HPΦ}; apply sep_mono_r, forall_intro=> h; apply wand_intro_l.
174
    rewrite -assoc; apply const_elim_sep_l=> ?.
175
    rewrite -(wp_cas_fail_pst _ (<[l:=v']>(of_heap h))) ?lookup_insert //.
176
177
    rewrite const_equiv // left_id.
    rewrite -(map_insert_singleton_op h); last by eapply heap_singleton_inv_l.
178
    rewrite -of_heap_insert.
179
    apply sep_mono_r, later_mono, wand_intro_l. by rewrite -later_intro.
Ralf Jung's avatar
Ralf Jung committed
180
  Qed.
Ralf Jung's avatar
Ralf Jung committed
181

182
  Lemma wp_cas_suc N E l e1 v1 e2 v2 P Φ :
Ralf Jung's avatar
Ralf Jung committed
183
184
    to_val e1 = Some v1  to_val e2 = Some v2 
    nclose N  E 
185
    P  heap_ctx N 
186
    P  ( l  v1   (l  v2 - Φ (LitV (LitBool true)))) 
187
    P  || Cas (Loc l) e1 e2 @ E {{ Φ }}.
Ralf Jung's avatar
Ralf Jung committed
188
  Proof.
189
    rewrite /heap_ctx /heap_inv /heap_mapsto=> ?? HN ? HPΦ.
190
    apply (auth_fsa' heap_inv (wp_fsa _) (alter (λ _, Excl v2) l))
191
      with N heap_name {[ l := Excl v1 ]}; simpl; eauto 10 with I.
192
    rewrite HPΦ{HPΦ}; apply sep_mono_r, forall_intro=> h; apply wand_intro_l.
193
    rewrite -assoc; apply const_elim_sep_l=> ?.
194
    rewrite -(wp_cas_suc_pst _ (<[l:=v1]>(of_heap h))) ?lookup_insert //.
195
196
    rewrite /heap_inv alter_singleton insert_insert.
    rewrite -!(map_insert_singleton_op h); try by eapply heap_singleton_inv_l.
197
    rewrite -!of_heap_insert const_equiv;
198
199
      last (split; [naive_solver|by eapply map_insert_valid, cmra_valid_op_r]).
    apply sep_mono_r, later_mono, wand_intro_l. by rewrite left_id -later_intro.
Ralf Jung's avatar
Ralf Jung committed
200
  Qed.
201
End heap.