gen_heap.v 6.4 KB
Newer Older
Ralf Jung's avatar
Ralf Jung committed
1
From iris.algebra Require Import auth gmap frac agree.
2
3
4
5
6
From iris.base_logic.lib Require Export own.
From iris.base_logic.lib Require Import fractional.
From iris.proofmode Require Import tactics.
Import uPred.

Ralf Jung's avatar
Ralf Jung committed
7
8
9
10
Definition gen_heapUR (L V : Type) `{Countable L} : ucmraT :=
  gmapUR L (prodR fracR (agreeR (leibnizC V))).
Definition to_gen_heap {L V} `{Countable L} : gmap L V  gen_heapUR L V :=
  fmap (λ v, (1%Qp, to_agree (v : leibnizC V))).
11
12

(** The CMRA we need. *)
Ralf Jung's avatar
Ralf Jung committed
13
Class gen_heapG (L V : Type) (Σ : gFunctors) `{Countable L} := GenHeapG {
14
15
16
17
  gen_heap_inG :> inG Σ (authR (gen_heapUR L V));
  gen_heap_name : gname
}.

Ralf Jung's avatar
Ralf Jung committed
18
Class gen_heapPreG (L V : Type) (Σ : gFunctors) `{Countable L} :=
19
20
  { gen_heap_preG_inG :> inG Σ (authR (gen_heapUR L V)) }.

Ralf Jung's avatar
Ralf Jung committed
21
Definition gen_heapΣ (L V : Type) `{Countable L} : gFunctors :=
22
23
  #[GFunctor (constRF (authR (gen_heapUR L V)))].

Ralf Jung's avatar
Ralf Jung committed
24
Instance subG_gen_heapPreG {Σ L V} `{Countable L} :
25
26
27
28
29
30
31
32
33
34
  subG (gen_heapΣ L V) Σ  gen_heapPreG L V Σ.
Proof. intros ?%subG_inG; split; apply _. Qed.

Section definitions.
  Context `{gen_heapG L V Σ}.

  Definition gen_heap_ctx (σ : gmap L V) : iProp Σ :=
    own gen_heap_name ( to_gen_heap σ).

  Definition mapsto_def (l : L) (q : Qp) (v: V) : iProp Σ :=
Ralf Jung's avatar
Ralf Jung committed
35
    own gen_heap_name ( {[ l := (q, to_agree (v : leibnizC V)) ]}).
36
37
38
39
40
41
42
43
44
45
46
47
48
  Definition mapsto_aux : { x | x = @mapsto_def }. by eexists. Qed.
  Definition mapsto := proj1_sig mapsto_aux.
  Definition mapsto_eq : @mapsto = @mapsto_def := proj2_sig mapsto_aux.
End definitions.

Local Notation "l ↦{ q } v" := (mapsto l q v)
  (at level 20, q at level 50, format "l  ↦{ q }  v") : uPred_scope.
Local Notation "l ↦ v" := (mapsto l 1 v) (at level 20) : uPred_scope.

Local Notation "l ↦{ q } -" := ( v, l {q} v)%I
  (at level 20, q at level 50, format "l  ↦{ q }  -") : uPred_scope.
Local Notation "l ↦ -" := (l {1} -)%I (at level 20) : uPred_scope.

49
50
Section to_gen_heap.
  Context (L V : Type) `{Countable L}.
51
52
53
54
55
56
57
58
  Implicit Types σ : gmap L V.

  (** Conversion to heaps and back *)
  Lemma to_gen_heap_valid σ :  to_gen_heap σ.
  Proof. intros l. rewrite lookup_fmap. by case (σ !! l). Qed.
  Lemma lookup_to_gen_heap_None σ l : σ !! l = None  to_gen_heap σ !! l = None.
  Proof. by rewrite /to_gen_heap lookup_fmap=> ->. Qed.
  Lemma gen_heap_singleton_included σ l q v :
Ralf Jung's avatar
Ralf Jung committed
59
    {[l := (q, to_agree v)]}  to_gen_heap σ  σ !! l = Some v.
60
  Proof.
Ralf Jung's avatar
Ralf Jung committed
61
62
63
    rewrite singleton_included=> -[[q' av] []].
    rewrite /to_gen_heap lookup_fmap fmap_Some_equiv => -[v' [Hl [/= -> ->]]].
    move=> /Some_pair_included_total_2 [_] /to_agree_included /leibniz_equiv_iff -> //.
64
65
  Qed.
  Lemma to_gen_heap_insert l v σ :
Ralf Jung's avatar
Ralf Jung committed
66
    to_gen_heap (<[l:=v]> σ) = <[l:=(1%Qp, to_agree (v:leibnizC V))]> (to_gen_heap σ).
67
  Proof. by rewrite /to_gen_heap fmap_insert. Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
68
69
70
  Lemma to_gen_heap_delete l σ :
    to_gen_heap (delete l σ) = delete l (to_gen_heap σ).
  Proof. by rewrite /to_gen_heap fmap_delete. Qed.
71
72
73
74
75
76
77
78
End to_gen_heap.

Section gen_heap.
  Context `{gen_heapG L V Σ}.
  Implicit Types P Q : iProp Σ.
  Implicit Types Φ : V  iProp Σ.
  Implicit Types σ : gmap L V.
  Implicit Types h g : gen_heapUR L V.
79
80
81
82
83
84
85

  (** General properties of mapsto *)
  Global Instance mapsto_timeless l q v : TimelessP (l {q} v).
  Proof. rewrite mapsto_eq /mapsto_def. apply _. Qed.
  Global Instance mapsto_fractional l v : Fractional (λ q, l {q} v)%I.
  Proof.
    intros p q. by rewrite mapsto_eq -own_op -auth_frag_op
Ralf Jung's avatar
Ralf Jung committed
86
      op_singleton pair_op agree_idemp.
87
88
89
  Qed.
  Global Instance mapsto_as_fractional l q v :
    AsFractional (l {q} v) (λ q, l {q} v)%I q.
90
  Proof. split. done. apply _. Qed.
91
92
93
94
95

  Lemma mapsto_agree l q1 q2 v1 v2 : l {q1} v1  l {q2} v2  v1 = v2.
  Proof.
    rewrite mapsto_eq -own_op -auth_frag_op own_valid discrete_valid.
    f_equiv=> /auth_own_valid /=. rewrite op_singleton singleton_valid pair_op.
Ralf Jung's avatar
Ralf Jung committed
96
    by intros [_ ?%agree_op_inv%(inj to_agree)%leibniz_equiv].
97
98
99
100
101
102
103
104
105
106
107
  Qed.

  Global Instance heap_ex_mapsto_fractional l : Fractional (λ q, l {q} -)%I.
  Proof.
    intros p q. iSplit.
    - iDestruct 1 as (v) "[H1 H2]". iSplitL "H1"; eauto.
    - iIntros "[H1 H2]". iDestruct "H1" as (v1) "H1". iDestruct "H2" as (v2) "H2".
      iDestruct (mapsto_agree with "[$H1 $H2]") as %->. iExists v2. by iFrame.
  Qed.
  Global Instance heap_ex_mapsto_as_fractional l q :
    AsFractional (l {q} -) (λ q, l {q} -)%I q.
108
  Proof. split. done. apply _. Qed.
109
110
111
112
113
114

  Lemma mapsto_valid l q v : l {q} v   q.
  Proof.
    rewrite mapsto_eq /mapsto_def own_valid !discrete_valid.
    by apply pure_mono=> /auth_own_valid /singleton_valid [??].
  Qed.
Ralf Jung's avatar
Ralf Jung committed
115
  Lemma mapsto_valid_2 l q1 q2 v1 v2 : l {q1} v1  l {q2} v2   (q1 + q2)%Qp.
116
117
118
119
120
121
122
123
124
125
126
  Proof.
    iIntros "[H1 H2]". iDestruct (mapsto_agree with "[$H1 $H2]") as %->.
    iApply (mapsto_valid l _ v2). by iFrame.
  Qed.

  Lemma gen_heap_alloc σ l v :
    σ !! l = None  gen_heap_ctx σ == gen_heap_ctx (<[l:=v]>σ)  l  v.
  Proof.
    iIntros (?) "Hσ". rewrite /gen_heap_ctx mapsto_eq /mapsto_def.
    iMod (own_update with "Hσ") as "[Hσ Hl]".
    { eapply auth_update_alloc,
Ralf Jung's avatar
Ralf Jung committed
127
        (alloc_singleton_local_update _ _ (1%Qp, to_agree (v:leibnizC _)))=> //.
128
129
130
131
      by apply lookup_to_gen_heap_None. }
    iModIntro. rewrite to_gen_heap_insert. iFrame.
  Qed.

Robbert Krebbers's avatar
Robbert Krebbers committed
132
133
134
135
136
137
138
139
  Lemma gen_heap_dealloc σ l v :
    gen_heap_ctx σ - l  v == gen_heap_ctx (delete l σ).
  Proof.
    iIntros "Hσ Hl". rewrite /gen_heap_ctx mapsto_eq /mapsto_def.
    rewrite to_gen_heap_delete. iApply (own_update_2 with "Hσ Hl").
    eapply auth_update_dealloc, (delete_singleton_local_update _ _ _).
  Qed.

140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
  Lemma gen_heap_valid σ l q v : gen_heap_ctx σ - l {q} v - ⌜σ !! l = Some v.
  Proof.
    iIntros "Hσ Hl". rewrite /gen_heap_ctx mapsto_eq /mapsto_def.
    iDestruct (own_valid_2 with "Hσ Hl")
      as %[Hl%gen_heap_singleton_included _]%auth_valid_discrete_2; auto.
  Qed.

  Lemma gen_heap_update σ l v1 v2 :
    gen_heap_ctx σ - l  v1 == gen_heap_ctx (<[l:=v2]>σ)  l  v2.
  Proof.
    iIntros "Hσ Hl". rewrite /gen_heap_ctx mapsto_eq /mapsto_def.
    iDestruct (own_valid_2 with "Hσ Hl")
      as %[Hl%gen_heap_singleton_included _]%auth_valid_discrete_2.
    iMod (own_update_2 with "Hσ Hl") as "[Hσ Hl]".
    { eapply auth_update, singleton_local_update,
Ralf Jung's avatar
Ralf Jung committed
155
        (exclusive_local_update _ (1%Qp, to_agree (v2:leibnizC _)))=> //.
156
157
158
159
      by rewrite /to_gen_heap lookup_fmap Hl. }
    iModIntro. rewrite to_gen_heap_insert. iFrame.
  Qed.
End gen_heap.