gmap.v 22.7 KB
Newer Older
1
From iris.algebra Require Export cmra.
2
From iris.prelude Require Export gmap.
3
(*
4
5
From iris.algebra Require Import updates local_updates.
From iris.base_logic Require Import base_logic.
6
*)
7
Set Default Proof Using "Type".
8

9
Section cofe.
10
Context `{Countable K} {A : ofeT}.
11
Implicit Types m : gmap K A.
12

13
Instance gmap_dist : Dist (gmap K A) := λ n m1 m2,
14
   i, m1 !! i {n} m2 !! i.
15
Definition gmap_ofe_mixin : ofe_laws (gmap K A).
16
17
Proof.
  split.
18
  - intros m1 m2; split.
19
    + by intros Hm n k; apply equiv_dist.
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
    + intros Hm k.
Check @equiv_dist.
apply equiv_dist.
(** FOOBAR -- This gives:
Error:
In environment
K : Type
EqDecision0 : EqDecision K
H : Countable K
A : ofeT
m1, m2 : gmap K A
Hm : ∀ n : nat, m1 ≡{n}≡ m2
k : K
Unable to unify
 "(?M4301 ≡ ?M4302 → ∀ n : nat, ?M4301 ≡{n}≡ ?M4302)
  ∧ ((∀ n : nat, ?M4301 ≡{n}≡ ?M4302) → ?M4301 ≡ ?M4302)" with
 "option_Forall2 equiv (m1 !! k) (m2 !! k)".

*)
 (A:=optionC A).
apply H0.
apply equiv_dist. intros n; apply Hm.
42
  - intros n; split.
43
44
    + by intros m k.
    + by intros m1 m2 ? k.
45
    + by intros m1 m2 m3 ?? k; trans (m2 !! k).
46
  - by intros n m1 m2 ? k; apply dist_S.
47
Qed.
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
Canonical Structure gmapC : ofeT := OfeT (gmap K A) gmap_ofe_mixin.

Program Definition gmap_chain (c : chain gmapC)
  (k : K) : chain (optionC A) := {| chain_car n := c n !! k |}.
Next Obligation. by intros c k n i ?; apply (chain_cauchy c). Qed.
Definition gmap_compl `{Cofe A} : Compl gmapC := λ c,
  map_imap (λ i _, compl (gmap_chain c i)) (c 0).
Global Program Instance gmap_cofe `{Cofe A} : Cofe gmapC :=
  {| compl := gmap_compl |}.
Next Obligation.
  intros ? n c k. rewrite /compl /gmap_compl lookup_imap.
  feed inversion (λ H, chain_cauchy c 0 n H k);simplify_option_eq;auto with lia.
  by rewrite conv_compl /=; apply reflexive_eq.
Qed.

63
Global Instance gmap_discrete : Discrete A  Discrete gmapC.
64
Proof. intros ? m m' ? i. by apply (timeless _). Qed.
65
(* why doesn't this go automatic? *)
66
Global Instance gmapC_leibniz: LeibnizEquiv A  LeibnizEquiv gmapC.
67
68
Proof. intros; change (LeibnizEquiv (gmap K A)); apply _. Qed.

69
70
Global Instance lookup_ne k :
  NonExpansive (lookup k : gmap K A  option A).
71
Proof. by intros m1 m2. Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
72
73
Global Instance lookup_proper k :
  Proper (() ==> ()) (lookup k : gmap K A  option A) := _.
74
75
76
77
Global Instance alter_ne f k n :
  Proper (dist n ==> dist n) f  Proper (dist n ==> dist n) (alter f k).
Proof.
  intros ? m m' Hm k'.
78
  by destruct (decide (k = k')); simplify_map_eq; rewrite (Hm k').
79
Qed.
80
81
Global Instance insert_ne i :
  NonExpansive2 (insert (M:=gmap K A) i).
82
Proof.
83
  intros n x y ? m m' ? j; destruct (decide (i = j)); simplify_map_eq;
84
85
    [by constructor|by apply lookup_ne].
Qed.
86
87
88
89
90
Global Instance singleton_ne i :
  NonExpansive (singletonM i : A  gmap K A).
Proof. by intros ????; apply insert_ne. Qed.
Global Instance delete_ne i :
  NonExpansive (delete (M:=gmap K A) i).
91
Proof.
92
  intros n m m' ? j; destruct (decide (i = j)); simplify_map_eq;
93
94
    [by constructor|by apply lookup_ne].
Qed.
95

96
Global Instance gmap_empty_timeless : Timeless ( : gmap K A).
97
98
99
100
Proof.
  intros m Hm i; specialize (Hm i); rewrite lookup_empty in Hm |- *.
  inversion_clear Hm; constructor.
Qed.
101
Global Instance gmap_lookup_timeless m i : Timeless m  Timeless (m !! i).
102
Proof.
103
  intros ? [x|] Hx; [|by symmetry; apply: timeless].
104
  assert (m {0} <[i:=x]> m)
Robbert Krebbers's avatar
Robbert Krebbers committed
105
106
    by (by symmetry in Hx; inversion Hx; cofe_subst; rewrite insert_id).
  by rewrite (timeless m (<[i:=x]>m)) // lookup_insert.
107
Qed.
108
Global Instance gmap_insert_timeless m i x :
109
110
  Timeless x  Timeless m  Timeless (<[i:=x]>m).
Proof.
111
  intros ?? m' Hm j; destruct (decide (i = j)); simplify_map_eq.
112
113
  { by apply: timeless; rewrite -Hm lookup_insert. }
  by apply: timeless; rewrite -Hm lookup_insert_ne.
114
Qed.
115
Global Instance gmap_singleton_timeless i x :
116
  Timeless x  Timeless ({[ i := x ]} : gmap K A) := _.
117
End cofe.
118

119
Arguments gmapC _ {_ _} _.
120
121

(* CMRA *)
122
123
Section cmra.
Context `{Countable K} {A : cmraT}.
124
Implicit Types m : gmap K A.
125

126
Instance gmap_op : Op (gmap K A) := merge op.
Robbert Krebbers's avatar
Robbert Krebbers committed
127
Instance gmap_pcore : PCore (gmap K A) := λ m, Some (omap pcore m).
128
129
Instance gmap_valid : Valid (gmap K A) := λ m,  i,  (m !! i).
Instance gmap_validN : ValidN (gmap K A) := λ n m,  i, {n} (m !! i).
130

131
Lemma lookup_op m1 m2 i : (m1  m2) !! i = m1 !! i  m2 !! i.
132
Proof. by apply lookup_merge. Qed.
Ralf Jung's avatar
Ralf Jung committed
133
Lemma lookup_core m i : core m !! i = core (m !! i).
Robbert Krebbers's avatar
Robbert Krebbers committed
134
Proof. by apply lookup_omap. Qed.
135

136
Lemma lookup_included (m1 m2 : gmap K A) : m1  m2   i, m1 !! i  m2 !! i.
137
Proof.
Robbert Krebbers's avatar
Robbert Krebbers committed
138
139
140
141
142
  split; [by intros [m Hm] i; exists (m !! i); rewrite -lookup_op Hm|].
  revert m2. induction m1 as [|i x m Hi IH] using map_ind=> m2 Hm.
  { exists m2. by rewrite left_id. }
  destruct (IH (delete i m2)) as [m2' Hm2'].
  { intros j. move: (Hm j); destruct (decide (i = j)) as [->|].
143
    - intros _. rewrite Hi. apply: ucmra_unit_least.
Robbert Krebbers's avatar
Robbert Krebbers committed
144
145
146
147
148
149
    - rewrite lookup_insert_ne // lookup_delete_ne //. }
  destruct (Hm i) as [my Hi']; simplify_map_eq.
  exists (partial_alter (λ _, my) i m2')=>j; destruct (decide (i = j)) as [->|].
  - by rewrite Hi' lookup_op lookup_insert lookup_partial_alter.
  - move: (Hm2' j). by rewrite !lookup_op lookup_delete_ne //
      lookup_insert_ne // lookup_partial_alter_ne.
150
Qed.
151

152
Lemma gmap_cmra_mixin : CMRAMixin (gmap K A).
153
Proof.
Robbert Krebbers's avatar
Robbert Krebbers committed
154
155
156
157
158
  apply cmra_total_mixin.
  - eauto.
  - intros n m1 m2 m3 Hm i; by rewrite !lookup_op (Hm i).
  - intros n m1 m2 Hm i; by rewrite !lookup_core (Hm i).
  - intros n m1 m2 Hm ? i; by rewrite -(Hm i).
159
160
161
  - intros m; split.
    + by intros ? n i; apply cmra_valid_validN.
    + intros Hm i; apply cmra_valid_validN=> n; apply Hm.
162
163
164
  - intros n m Hm i; apply cmra_validN_S, Hm.
  - by intros m1 m2 m3 i; rewrite !lookup_op assoc.
  - by intros m1 m2 i; rewrite !lookup_op comm.
Robbert Krebbers's avatar
Robbert Krebbers committed
165
166
167
  - intros m i. by rewrite lookup_op lookup_core cmra_core_l.
  - intros m i. by rewrite !lookup_core cmra_core_idemp.
  - intros m1 m2; rewrite !lookup_included=> Hm i.
168
    rewrite !lookup_core. by apply cmra_core_mono.
169
  - intros n m1 m2 Hm i; apply cmra_validN_op_l with (m2 !! i).
Robbert Krebbers's avatar
Robbert Krebbers committed
170
    by rewrite -lookup_op.
171
  - intros n m. induction m as [|i x m Hi IH] using map_ind=> m1 m2 Hmv Hm.
172
    { exists , . split_and!=> -i; symmetry; symmetry in Hm; move: Hm=> /(_ i);
173
174
175
176
177
178
179
180
181
        rewrite !lookup_op !lookup_empty ?dist_None op_None; intuition. }
    destruct (IH (delete i m1) (delete i m2)) as (m1'&m2'&Hm'&Hm1'&Hm2').
    { intros j; move: Hmv=> /(_ j). destruct (decide (i = j)) as [->|].
      + intros _. by rewrite Hi.
      + by rewrite lookup_insert_ne. }
    { intros j; move: Hm=> /(_ j); destruct (decide (i = j)) as [->|].
      + intros _. by rewrite lookup_op !lookup_delete Hi.
      + by rewrite !lookup_op lookup_insert_ne // !lookup_delete_ne. }
    destruct (cmra_extend n (Some x) (m1 !! i) (m2 !! i)) as (y1&y2&?&?&?).
182
    { move: Hmv=> /(_ i). by rewrite lookup_insert. }
183
184
185
186
187
188
189
190
191
192
193
194
    { move: Hm=> /(_ i). by rewrite lookup_insert lookup_op. }
    exists (partial_alter (λ _, y1) i m1'), (partial_alter (λ _, y2) i m2').
    split_and!.
    + intros j. destruct (decide (i = j)) as [->|].
      * by rewrite lookup_insert lookup_op !lookup_partial_alter.
      * by rewrite lookup_insert_ne // Hm' !lookup_op !lookup_partial_alter_ne.
    + intros j. destruct (decide (i = j)) as [->|].
      * by rewrite lookup_partial_alter.
      * by rewrite lookup_partial_alter_ne // Hm1' lookup_delete_ne.
    + intros j. destruct (decide (i = j)) as [->|].
      * by rewrite lookup_partial_alter.
      * by rewrite lookup_partial_alter_ne // Hm2' lookup_delete_ne.
195
Qed.
196
Canonical Structure gmapR :=
197
  CMRAT (gmap K A) gmap_ofe_mixin gmap_cmra_mixin.
198
199
200
201
202

Global Instance gmap_cmra_discrete : CMRADiscrete A  CMRADiscrete gmapR.
Proof. split; [apply _|]. intros m ? i. by apply: cmra_discrete_valid. Qed.

Lemma gmap_ucmra_mixin : UCMRAMixin (gmap K A).
203
204
Proof.
  split.
205
  - by intros i; rewrite lookup_empty.
206
  - by intros m i; rewrite /= lookup_op lookup_empty (left_id_L None _).
Robbert Krebbers's avatar
Robbert Krebbers committed
207
  - constructor=> i. by rewrite lookup_omap lookup_empty.
208
Qed.
209
Canonical Structure gmapUR :=
210
  UCMRAT (gmap K A) gmap_ofe_mixin gmap_cmra_mixin gmap_ucmra_mixin.
211
212

(** Internalized properties *)
213
Lemma gmap_equivI {M} m1 m2 : m1  m2  ( i, m1 !! i  m2 !! i : uPred M).
214
Proof. by uPred.unseal. Qed.
215
Lemma gmap_validI {M} m :  m  ( i,  (m !! i) : uPred M).
216
Proof. by uPred.unseal. Qed.
217
End cmra.
218

219
Arguments gmapR _ {_ _} _.
220
Arguments gmapUR _ {_ _} _.
221
222

Section properties.
223
Context `{Countable K} {A : cmraT}.
Robbert Krebbers's avatar
Robbert Krebbers committed
224
Implicit Types m : gmap K A.
225
Implicit Types i : K.
226
227
Implicit Types x y : A.

228
229
230
231
Global Instance lookup_cmra_homomorphism :
  UCMRAHomomorphism (lookup i : gmap K A  option A).
Proof. split. split. apply _. intros m1 m2; by rewrite lookup_op. done. Qed.

232
Lemma lookup_opM m1 mm2 i : (m1 ? mm2) !! i = m1 !! i  (mm2 = (!! i)).
233
Proof. destruct mm2; by rewrite /= ?lookup_op ?right_id_L. Qed.
234

235
Lemma lookup_validN_Some n m i x : {n} m  m !! i {n} Some x  {n} x.
Robbert Krebbers's avatar
Robbert Krebbers committed
236
Proof. by move=> /(_ i) Hm Hi; move:Hm; rewrite Hi. Qed.
237
Lemma lookup_valid_Some m i x :  m  m !! i  Some x   x.
238
Proof. move=> Hm Hi. move:(Hm i). by rewrite Hi. Qed.
239

240
Lemma insert_validN n m i x : {n} x  {n} m  {n} <[i:=x]>m.
241
Proof. by intros ?? j; destruct (decide (i = j)); simplify_map_eq. Qed.
242
Lemma insert_valid m i x :  x   m   <[i:=x]>m.
243
Proof. by intros ?? j; destruct (decide (i = j)); simplify_map_eq. Qed.
244
Lemma singleton_validN n i x : {n} ({[ i := x ]} : gmap K A)  {n} x.
245
Proof.
246
  split; [|by intros; apply insert_validN, ucmra_unit_validN].
247
  by move=>/(_ i); simplify_map_eq.
248
Qed.
249
250
Lemma singleton_valid i x :  ({[ i := x ]} : gmap K A)   x.
Proof. rewrite !cmra_valid_validN. by setoid_rewrite singleton_validN. Qed.
251

252
253
254
255
256
Lemma delete_validN n m i : {n} m  {n} (delete i m).
Proof. intros Hm j; destruct (decide (i = j)); by simplify_map_eq. Qed.
Lemma delete_valid m i :  m   (delete i m).
Proof. intros Hm j; destruct (decide (i = j)); by simplify_map_eq. Qed.

257
Lemma insert_singleton_op m i x : m !! i = None  <[i:=x]> m = {[ i := x ]}  m.
258
Proof.
259
260
261
  intros Hi; apply map_eq=> j; destruct (decide (i = j)) as [->|].
  - by rewrite lookup_op lookup_insert lookup_singleton Hi right_id_L.
  - by rewrite lookup_op lookup_insert_ne // lookup_singleton_ne // left_id_L.
262
263
Qed.

Robbert Krebbers's avatar
Robbert Krebbers committed
264
265
266
267
268
269
270
271
Lemma core_singleton (i : K) (x : A) cx :
  pcore x = Some cx  core ({[ i := x ]} : gmap K A) = {[ i := cx ]}.
Proof. apply omap_singleton. Qed.
Lemma core_singleton' (i : K) (x : A) cx :
  pcore x  Some cx  core ({[ i := x ]} : gmap K A)  {[ i := cx ]}.
Proof.
  intros (cx'&?&->)%equiv_Some_inv_r'. by rewrite (core_singleton _ _ cx').
Qed.
272
Lemma op_singleton (i : K) (x y : A) :
273
  {[ i := x ]}  {[ i := y ]} = ({[ i := x  y ]} : gmap K A).
274
Proof. by apply (merge_singleton _ _ _ x y). Qed.
275
276
277
Global Instance singleton_cmra_homomorphism :
  CMRAHomomorphism (singletonM i : A  gmap K A).
Proof. split. apply _. intros. by rewrite op_singleton. Qed.
278

279
Global Instance gmap_persistent m : ( x : A, Persistent x)  Persistent m.
Robbert Krebbers's avatar
Robbert Krebbers committed
280
281
282
283
Proof.
  intros; apply persistent_total=> i.
  rewrite lookup_core. apply (persistent_core _).
Qed.
284
Global Instance gmap_singleton_persistent i (x : A) :
285
  Persistent x  Persistent {[ i := x ]}.
Robbert Krebbers's avatar
Robbert Krebbers committed
286
Proof. intros. by apply persistent_total, core_singleton'. Qed.
287

Robbert Krebbers's avatar
Robbert Krebbers committed
288
Lemma singleton_includedN n m i x :
289
  {[ i := x ]} {n} m   y, m !! i {n} Some y  Some x {n} Some y.
Robbert Krebbers's avatar
Robbert Krebbers committed
290
291
Proof.
  split.
292
293
294
295
296
297
298
  - move=> [m' /(_ i)]; rewrite lookup_op lookup_singleton=> Hi.
    exists (x ? m' !! i). rewrite -Some_op_opM.
    split. done. apply cmra_includedN_l.
  - intros (y&Hi&[mz Hy]). exists (partial_alter (λ _, mz) i m).
    intros j; destruct (decide (i = j)) as [->|].
    + by rewrite lookup_op lookup_singleton lookup_partial_alter Hi.
    + by rewrite lookup_op lookup_singleton_ne// lookup_partial_alter_ne// left_id.
299
300
301
Qed.
(* We do not have [x ≼ y ↔ ∀ n, x ≼{n} y], so we cannot use the previous lemma *)
Lemma singleton_included m i x :
302
  {[ i := x ]}  m   y, m !! i  Some y  Some x  Some y.
303
304
305
Proof.
  split.
  - move=> [m' /(_ i)]; rewrite lookup_op lookup_singleton.
306
307
308
309
310
311
    exists (x ? m' !! i). rewrite -Some_op_opM.
    split. done. apply cmra_included_l.
  - intros (y&Hi&[mz Hy]). exists (partial_alter (λ _, mz) i m).
    intros j; destruct (decide (i = j)) as [->|].
    + by rewrite lookup_op lookup_singleton lookup_partial_alter Hi.
    + by rewrite lookup_op lookup_singleton_ne// lookup_partial_alter_ne// left_id.
Robbert Krebbers's avatar
Robbert Krebbers committed
312
313
Qed.

314
315
316
317
318
319
320
321
322
323
324
Global Instance singleton_cancelable i x :
  Cancelable (Some x)  Cancelable {[ i := x ]}.
Proof.
  intros ???? Hv EQ j. specialize (EQ j). specialize (Hv j).
  rewrite !lookup_op in EQ, Hv. destruct (decide (i = j)).
  - subst. rewrite lookup_singleton in EQ, Hv.
    by eapply cancelableN.
  - rewrite lookup_singleton_ne // in EQ, Hv.
    by rewrite ->!(left_id None _) in EQ.
Qed.

325
Lemma insert_updateP (P : A  Prop) (Q : gmap K A  Prop) m i x :
326
  x ~~>: P  ( y, P y  Q (<[i:=y]>m))  <[i:=x]>m ~~>: Q.
327
Proof.
Robbert Krebbers's avatar
Robbert Krebbers committed
328
329
  intros Hx%option_updateP' HP; apply cmra_total_updateP=> n mf Hm.
  destruct (Hx n (Some (mf !! i))) as ([y|]&?&?); try done.
330
  { by generalize (Hm i); rewrite lookup_op; simplify_map_eq. }
331
332
  exists (<[i:=y]> m); split; first by auto.
  intros j; move: (Hm j)=>{Hm}; rewrite !lookup_op=>Hm.
333
  destruct (decide (i = j)); simplify_map_eq/=; auto.
334
Qed.
335
Lemma insert_updateP' (P : A  Prop) m i x :
336
  x ~~>: P  <[i:=x]>m ~~>: λ m',  y, m' = <[i:=y]>m  P y.
337
338
339
Proof. eauto using insert_updateP. Qed.
Lemma insert_update m i x y : x ~~> y  <[i:=x]>m ~~> <[i:=y]>m.
Proof. rewrite !cmra_update_updateP; eauto using insert_updateP with subst. Qed.
340

341
Lemma singleton_updateP (P : A  Prop) (Q : gmap K A  Prop) i x :
342
  x ~~>: P  ( y, P y  Q {[ i := y ]})  {[ i := x ]} ~~>: Q.
343
344
Proof. apply insert_updateP. Qed.
Lemma singleton_updateP' (P : A  Prop) i x :
345
  x ~~>: P  {[ i := x ]} ~~>: λ m,  y, m = {[ i := y ]}  P y.
346
347
348
Proof. apply insert_updateP'. Qed.
Lemma singleton_update i (x y : A) : x ~~> y  {[ i := x ]} ~~> {[ i := y ]}.
Proof. apply insert_update. Qed.
349

350
Lemma delete_update m i : m ~~> delete i m.
351
Proof.
352
353
354
355
  apply cmra_total_update=> n mf Hm j; destruct (decide (i = j)); subst.
  - move: (Hm j). rewrite !lookup_op lookup_delete left_id.
    apply cmra_validN_op_r.
  - move: (Hm j). by rewrite !lookup_op lookup_delete_ne.
356
Qed.
357

358
359
360
361
362
363
364
365
366
367
368
Lemma dom_op m1 m2 : dom (gset K) (m1  m2) = dom _ m1  dom _ m2.
Proof.
  apply elem_of_equiv_L=> i; rewrite elem_of_union !elem_of_dom.
  unfold is_Some; setoid_rewrite lookup_op.
  destruct (m1 !! i), (m2 !! i); naive_solver.
Qed.
Lemma dom_included m1 m2 : m1  m2  dom (gset K) m1  dom _ m2.
Proof.
  rewrite lookup_included=>? i; rewrite !elem_of_dom. by apply is_Some_included.
Qed.

369
Section freshness.
370
  Local Set Default Proof Using "Type*".
371
372
373
374
375
376
377
378
379
380
  Context `{Fresh K (gset K), !FreshSpec K (gset K)}.
  Lemma alloc_updateP_strong (Q : gmap K A  Prop) (I : gset K) m x :
     x  ( i, m !! i = None  i  I  Q (<[i:=x]>m))  m ~~>: Q.
  Proof.
    intros ? HQ. apply cmra_total_updateP.
    intros n mf Hm. set (i := fresh (I  dom (gset K) (m  mf))).
    assert (i  I  i  dom (gset K) m  i  dom (gset K) mf) as [?[??]].
    { rewrite -not_elem_of_union -dom_op -not_elem_of_union; apply is_fresh. }
    exists (<[i:=x]>m); split.
    { by apply HQ; last done; apply not_elem_of_dom. }
381
382
    rewrite insert_singleton_op; last by apply not_elem_of_dom.
    rewrite -assoc -insert_singleton_op;
383
384
385
386
387
388
389
390
391
392
393
394
395
396
      last by apply not_elem_of_dom; rewrite dom_op not_elem_of_union.
    by apply insert_validN; [apply cmra_valid_validN|].
  Qed.
  Lemma alloc_updateP (Q : gmap K A  Prop) m x :
     x  ( i, m !! i = None  Q (<[i:=x]>m))  m ~~>: Q.
  Proof. move=>??. eapply alloc_updateP_strong with (I:=); by eauto. Qed.
  Lemma alloc_updateP_strong' m x (I : gset K) :
     x  m ~~>: λ m',  i, i  I  m' = <[i:=x]>m  m !! i = None.
  Proof. eauto using alloc_updateP_strong. Qed.
  Lemma alloc_updateP' m x :
     x  m ~~>: λ m',  i, m' = <[i:=x]>m  m !! i = None.
  Proof. eauto using alloc_updateP. Qed.
End freshness.

397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
Lemma alloc_unit_singleton_updateP (P : A  Prop) (Q : gmap K A  Prop) u i :
   u  LeftId () u () 
  u ~~>: P  ( y, P y  Q {[ i := y ]})   ~~>: Q.
Proof.
  intros ?? Hx HQ. apply cmra_total_updateP=> n gf Hg.
  destruct (Hx n (gf !! i)) as (y&?&Hy).
  { move:(Hg i). rewrite !left_id.
    case: (gf !! i)=>[x|]; rewrite /= ?left_id //.
    intros; by apply cmra_valid_validN. }
  exists {[ i := y ]}; split; first by auto.
  intros i'; destruct (decide (i' = i)) as [->|].
  - rewrite lookup_op lookup_singleton.
    move:Hy; case: (gf !! i)=>[x|]; rewrite /= ?right_id //.
  - move:(Hg i'). by rewrite !lookup_op lookup_singleton_ne // !left_id.
Qed.
Lemma alloc_unit_singleton_updateP' (P: A  Prop) u i :
   u  LeftId () u () 
  u ~~>: P   ~~>: λ m,  y, m = {[ i := y ]}  P y.
Proof. eauto using alloc_unit_singleton_updateP. Qed.
Lemma alloc_unit_singleton_update (u : A) i (y : A) :
   u  LeftId () u ()  u ~~> y  (:gmap K A) ~~> {[ i := y ]}.
Proof.
  rewrite !cmra_update_updateP;
    eauto using alloc_unit_singleton_updateP with subst.
Qed.

423
424
Lemma alloc_local_update m1 m2 i x :
  m1 !! i = None   x  (m1,m2) ~l~> (<[i:=x]>m1, <[i:=x]>m2).
425
Proof.
426
427
428
429
430
431
432
  rewrite cmra_valid_validN=> Hi ?.
  apply local_update_unital=> n mf Hmv Hm; simpl in *.
  split; auto using insert_validN.
  intros j; destruct (decide (i = j)) as [->|].
  - move: (Hm j); rewrite Hi symmetry_iff dist_None lookup_op op_None=>-[_ Hj].
    by rewrite lookup_op !lookup_insert Hj.
  - rewrite Hm lookup_insert_ne // !lookup_op lookup_insert_ne //.
433
Qed.
434

435
436
437
Lemma alloc_singleton_local_update m i x :
  m !! i = None   x  (m,) ~l~> (<[i:=x]>m, {[ i:=x ]}).
Proof. apply alloc_local_update. Qed.
438

439
440
441
442
Lemma insert_local_update m1 m2 i x y x' y' :
  m1 !! i = Some x  m2 !! i = Some y 
  (x, y) ~l~> (x', y') 
  (m1, m2) ~l~> (<[i:=x']>m1, <[i:=y']>m2).
443
Proof.
444
445
446
447
448
449
450
451
  intros Hi1 Hi2 Hup; apply local_update_unital=> n mf Hmv Hm; simpl in *.
  destruct (Hup n (mf !! i)) as [? Hx']; simpl in *.
  { move: (Hmv i). by rewrite Hi1. }
  { move: (Hm i). by rewrite lookup_op Hi1 Hi2 Some_op_opM (inj_iff Some). }
  split; auto using insert_validN.
  rewrite Hm Hx'=> j; destruct (decide (i = j)) as [->|].
  - by rewrite lookup_insert lookup_op lookup_insert Some_op_opM.
  - by rewrite lookup_insert_ne // !lookup_op lookup_insert_ne.
452
453
Qed.

454
455
456
457
Lemma singleton_local_update m i x y x' y' :
  m !! i = Some x 
  (x, y) ~l~> (x', y') 
  (m, {[ i := y ]}) ~l~> (<[i:=x']>m, {[ i := y' ]}).
458
Proof.
459
460
  intros. rewrite /singletonM /map_singleton -(insert_insert  i y' y).
  eapply insert_local_update; eauto using lookup_insert.
461
Qed.
462

463
464
Lemma delete_local_update m1 m2 i x `{!Exclusive x} :
  m2 !! i = Some x  (m1, m2) ~l~> (delete i m1, delete i m2).
465
Proof.
466
467
468
469
470
471
  intros Hi. apply local_update_unital=> n mf Hmv Hm; simpl in *.
  split; auto using delete_validN.
  rewrite Hm=> j; destruct (decide (i = j)) as [<-|].
  - rewrite lookup_op !lookup_delete left_id symmetry_iff dist_None.
    apply eq_None_not_Some=> -[y Hi'].
    move: (Hmv i). rewrite Hm lookup_op Hi Hi' -Some_op. by apply exclusiveN_l.
472
  - by rewrite lookup_op !lookup_delete_ne // lookup_op.
473
474
475
476
477
478
479
Qed.

Lemma delete_singleton_local_update m i x `{!Exclusive x} :
  (m, {[ i := x ]}) ~l~> (delete i m, ).
Proof.
  rewrite -(delete_singleton i x).
  eapply delete_local_update; eauto using lookup_singleton.
480
Qed.
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503

Lemma delete_local_update_cancelable m1 m2 i mx `{!Cancelable mx} :
  m1 !! i  mx  m2 !! i  mx 
  (m1, m2) ~l~> (delete i m1, delete i m2).
Proof.
  intros EQ1 EQ2.
  destruct mx as [x|], (m1 !! i) as [m1i|] eqn:?, (m2 !! i) as [m2i|] eqn:?;
    inversion_clear EQ1; inversion_clear EQ2.
  - rewrite -{1}(insert_id m1 i m1i) // -{1}(insert_id m2 i m2i) //
            -(insert_delete m1) -(insert_delete m2) !insert_singleton_op;
    try by apply lookup_delete.
    assert (m1i  x) as -> by done. assert (m2i  x) as -> by done.
    apply cancel_local_update, _.
  - rewrite !delete_notin //.
Qed.

Lemma delete_singleton_local_update_cancelable m i x `{!Cancelable (Some x)} :
  m !! i  Some x  (m, {[ i := x ]}) ~l~> (delete i m, ).
Proof.
  intros. rewrite -(delete_singleton i x).
  apply (delete_local_update_cancelable m _ i (Some x));
    [done|by rewrite lookup_singleton].
Qed.
504
505
End properties.

506
(** Functor *)
507
Instance gmap_fmap_ne `{Countable K} {A B : ofeT} (f : A  B) n :
508
509
  Proper (dist n ==> dist n) f  Proper (dist n ==>dist n) (fmap (M:=gmap K) f).
Proof. by intros ? m m' Hm k; rewrite !lookup_fmap; apply option_fmap_ne. Qed.
510
Instance gmap_fmap_cmra_monotone `{Countable K} {A B : cmraT} (f : A  B)
511
512
  `{!CMRAMonotone f} : CMRAMonotone (fmap f : gmap K A  gmap K B).
Proof.
Robbert Krebbers's avatar
Robbert Krebbers committed
513
  split; try apply _.
514
  - by intros n m ? i; rewrite lookup_fmap; apply (cmra_monotone_validN _).
515
  - intros m1 m2; rewrite !lookup_included=> Hm i.
516
    by rewrite !lookup_fmap; apply: cmra_monotone.
517
Qed.
518
519
Definition gmapC_map `{Countable K} {A B} (f: A -n> B) :
  gmapC K A -n> gmapC K B := CofeMor (fmap f : gmapC K A  gmapC K B).
520
521
Instance gmapC_map_ne `{Countable K} {A B} :
  NonExpansive (@gmapC_map K _ _ A B).
522
Proof.
523
  intros n f g Hf m k; rewrite /= !lookup_fmap.
524
525
  destruct (_ !! k) eqn:?; simpl; constructor; apply Hf.
Qed.
Ralf Jung's avatar
Ralf Jung committed
526

527
528
529
Program Definition gmapCF K `{Countable K} (F : cFunctor) : cFunctor := {|
  cFunctor_car A B := gmapC K (cFunctor_car F A B);
  cFunctor_map A1 A2 B1 B2 fg := gmapC_map (cFunctor_map F fg)
Ralf Jung's avatar
Ralf Jung committed
530
|}.
531
Next Obligation.
532
  by intros K ?? F A1 A2 B1 B2 n f g Hfg; apply gmapC_map_ne, cFunctor_ne.
533
Qed.
Ralf Jung's avatar
Ralf Jung committed
534
Next Obligation.
535
  intros K ?? F A B x. rewrite /= -{2}(map_fmap_id x).
536
  apply map_fmap_equiv_ext=>y ??; apply cFunctor_id.
Ralf Jung's avatar
Ralf Jung committed
537
538
Qed.
Next Obligation.
539
  intros K ?? F A1 A2 A3 B1 B2 B3 f g f' g' x. rewrite /= -map_fmap_compose.
540
  apply map_fmap_equiv_ext=>y ??; apply cFunctor_compose.
541
Qed.
542
543
Instance gmapCF_contractive K `{Countable K} F :
  cFunctorContractive F  cFunctorContractive (gmapCF K F).
544
Proof.
545
  by intros ? A1 A2 B1 B2 n f g Hfg; apply gmapC_map_ne, cFunctor_contractive.
546
547
Qed.

548
549
550
Program Definition gmapURF K `{Countable K} (F : rFunctor) : urFunctor := {|
  urFunctor_car A B := gmapUR K (rFunctor_car F A B);
  urFunctor_map A1 A2 B1 B2 fg := gmapC_map (rFunctor_map F fg)
551
|}.
552
Next Obligation.
553
  by intros K ?? F A1 A2 B1 B2 n f g Hfg; apply gmapC_map_ne, rFunctor_ne.
554
Qed.
555
556
Next Obligation.
  intros K ?? F A B x. rewrite /= -{2}(map_fmap_id x).
557
  apply map_fmap_equiv_ext=>y ??; apply rFunctor_id.
558
559
560
Qed.
Next Obligation.
  intros K ?? F A1 A2 A3 B1 B2 B3 f g f' g' x. rewrite /= -map_fmap_compose.
561
  apply map_fmap_equiv_ext=>y ??; apply rFunctor_compose.
Ralf Jung's avatar
Ralf Jung committed
562
Qed.
563
Instance gmapRF_contractive K `{Countable K} F :
564
  rFunctorContractive F  urFunctorContractive (gmapURF K F).
565
Proof.
566
  by intros ? A1 A2 B1 B2 n f g Hfg; apply gmapC_map_ne, rFunctor_contractive.
567
Qed.