heap.v 8.43 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

12
13
14
15
16
17
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 |}.
18

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

22
Definition heap_mapsto `{heapG Σ} (l : loc) (v: val) : iPropG heap_lang Σ :=
23
  auth_own heap_name {[ l := Excl v ]}.
24
25
26
27
28
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.

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

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

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

65
  (** Allocation *)
66
67
  Lemma heap_alloc E N σ :
    authG heap_lang Σ heapRA  nclose N  E 
68
    ownP σ  (|={E}=>  _ : heapG Σ, heap_ctx N  Π★{map σ} heap_mapsto).
Ralf Jung's avatar
Ralf Jung committed
69
  Proof.
70
    intros. rewrite -{1}(from_to_heap σ). etrans.
71
    { rewrite [ownP _]later_intro.
72
73
      apply (auth_alloc (ownP  of_heap) E N (to_heap σ)); last done.
      apply to_heap_valid. }
74
75
    apply pvs_mono, exist_elim=> γ.
    rewrite -(exist_intro (HeapG _ _ γ)); apply and_mono_r.
76
77
78
79
80
81
    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
82
  Qed.
Ralf Jung's avatar
Ralf Jung committed
83

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

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

  (** General properties of mapsto *)
91
  Lemma heap_mapsto_disjoint l v1 v2 : (l  v1  l  v2)%I  False.
92
93
94
95
96
97
  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.

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

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

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

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

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