gmap.v 22.1 KB
Newer Older
1
From iris.algebra Require Export cmra.
Ralf Jung's avatar
Ralf Jung committed
2
From stdpp Require Export gmap.
3 4
From iris.algebra Require Import updates local_updates.
From iris.base_logic Require Import base_logic.
5
Set Default Proof Using "Type".
6

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

12
Instance gmap_dist : Dist (gmap K A) := λ n m1 m2,
13
   i, m1 !! i {n} m2 !! i.
14
Definition gmap_ofe_mixin : OfeMixin (gmap K A).
15 16
Proof.
  split.
17
  - intros m1 m2; split.
18 19
    + by intros Hm n k; apply equiv_dist.
    + intros Hm k; apply equiv_dist; intros n; apply Hm.
20
  - intros n; split.
21 22
    + by intros m k.
    + by intros m1 m2 ? k.
23
    + by intros m1 m2 m3 ?? k; trans (m2 !! k).
24
  - by intros n m1 m2 ? k; apply dist_S.
25
Qed.
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
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.

41
Global Instance gmap_ofe_discrete : OfeDiscrete A  OfeDiscrete gmapC.
42
Proof. intros ? m m' ? i. by apply (discrete _). Qed.
43
(* why doesn't this go automatic? *)
44
Global Instance gmapC_leibniz: LeibnizEquiv A  LeibnizEquiv gmapC.
45 46
Proof. intros; change (LeibnizEquiv (gmap K A)); apply _. Qed.

47 48
Global Instance lookup_ne k :
  NonExpansive (lookup k : gmap K A  option A).
49
Proof. by intros m1 m2. Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
50 51
Global Instance lookup_proper k :
  Proper (() ==> ()) (lookup k : gmap K A  option A) := _.
52 53 54 55
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'.
56
  by destruct (decide (k = k')); simplify_map_eq; rewrite (Hm k').
57
Qed.
58 59
Global Instance insert_ne i :
  NonExpansive2 (insert (M:=gmap K A) i).
60
Proof.
61
  intros n x y ? m m' ? j; destruct (decide (i = j)); simplify_map_eq;
62 63
    [by constructor|by apply lookup_ne].
Qed.
64 65 66 67 68
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).
69
Proof.
70
  intros n m m' ? j; destruct (decide (i = j)); simplify_map_eq;
71 72
    [by constructor|by apply lookup_ne].
Qed.
73

74
Global Instance gmap_empty_discrete : Discrete ( : gmap K A).
75 76 77 78
Proof.
  intros m Hm i; specialize (Hm i); rewrite lookup_empty in Hm |- *.
  inversion_clear Hm; constructor.
Qed.
79
Global Instance gmap_lookup_discrete m i : Discrete m  Discrete (m !! i).
80
Proof.
81
  intros ? [x|] Hx; [|by symmetry; apply: discrete].
82
  assert (m {0} <[i:=x]> m)
83
    by (by symmetry in Hx; inversion Hx; ofe_subst; rewrite insert_id).
84
  by rewrite (discrete m (<[i:=x]>m)) // lookup_insert.
85
Qed.
86 87
Global Instance gmap_insert_discrete m i x :
  Discrete x  Discrete m  Discrete (<[i:=x]>m).
88
Proof.
89
  intros ?? m' Hm j; destruct (decide (i = j)); simplify_map_eq.
90 91
  { by apply: discrete; rewrite -Hm lookup_insert. }
  by apply: discrete; rewrite -Hm lookup_insert_ne.
92
Qed.
93 94
Global Instance gmap_singleton_discrete i x :
  Discrete x  Discrete ({[ i := x ]} : gmap K A) := _.
95
End cofe.
96

97
Arguments gmapC _ {_ _} _.
98 99

(* CMRA *)
100 101
Section cmra.
Context `{Countable K} {A : cmraT}.
102
Implicit Types m : gmap K A.
103

Robbert Krebbers's avatar
Robbert Krebbers committed
104
Instance gmap_unit : Unit (gmap K A) := ( : gmap K A).
105
Instance gmap_op : Op (gmap K A) := merge op.
Robbert Krebbers's avatar
Robbert Krebbers committed
106
Instance gmap_pcore : PCore (gmap K A) := λ m, Some (omap pcore m).
107 108
Instance gmap_valid : Valid (gmap K A) := λ m,  i,  (m !! i).
Instance gmap_validN : ValidN (gmap K A) := λ n m,  i, {n} (m !! i).
109

110
Lemma lookup_op m1 m2 i : (m1  m2) !! i = m1 !! i  m2 !! i.
111
Proof. by apply lookup_merge. Qed.
Ralf Jung's avatar
Ralf Jung committed
112
Lemma lookup_core m i : core m !! i = core (m !! i).
Robbert Krebbers's avatar
Robbert Krebbers committed
113
Proof. by apply lookup_omap. Qed.
114

115
Lemma lookup_included (m1 m2 : gmap K A) : m1  m2   i, m1 !! i  m2 !! i.
116
Proof.
Robbert Krebbers's avatar
Robbert Krebbers committed
117 118 119 120 121
  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 [->|].
122
    - intros _. rewrite Hi. apply: ucmra_unit_least.
Robbert Krebbers's avatar
Robbert Krebbers committed
123 124 125 126 127 128
    - 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.
129
Qed.
130

131
Lemma gmap_cmra_mixin : CmraMixin (gmap K A).
132
Proof.
Robbert Krebbers's avatar
Robbert Krebbers committed
133 134 135 136 137
  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).
138 139 140
  - intros m; split.
    + by intros ? n i; apply cmra_valid_validN.
    + intros Hm i; apply cmra_valid_validN=> n; apply Hm.
141 142 143
  - 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
144 145 146
  - 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.
147
    rewrite !lookup_core. by apply cmra_core_mono.
148
  - intros n m1 m2 Hm i; apply cmra_validN_op_l with (m2 !! i).
Robbert Krebbers's avatar
Robbert Krebbers committed
149
    by rewrite -lookup_op.
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
  - intros n m. induction m as [|i x m Hi IH] using map_ind=> m1 m2 Hmv Hm.
    { eexists , . split_and!=> -i; symmetry; symmetry in Hm; move: Hm=> /(_ i);
        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&?&?&?).
    { move: Hmv=> /(_ i). by rewrite lookup_insert. }
    { 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.
174
Qed.
175
Canonical Structure gmapR := CmraT (gmap K A) gmap_cmra_mixin.
176

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

180
Lemma gmap_ucmra_mixin : UcmraMixin (gmap K A).
181 182
Proof.
  split.
183
  - by intros i; rewrite lookup_empty.
184
  - by intros m i; rewrite /= lookup_op lookup_empty (left_id_L None _).
Robbert Krebbers's avatar
Robbert Krebbers committed
185
  - constructor=> i. by rewrite lookup_omap lookup_empty.
186
Qed.
187
Canonical Structure gmapUR := UcmraT (gmap K A) gmap_ucmra_mixin.
188 189

(** Internalized properties *)
190
Lemma gmap_equivI {M} m1 m2 : m1  m2  ( i, m1 !! i  m2 !! i : uPred M).
191
Proof. by uPred.unseal. Qed.
192
Lemma gmap_validI {M} m :  m  ( i,  (m !! i) : uPred M).
193
Proof. by uPred.unseal. Qed.
194
End cmra.
195

196
Arguments gmapR _ {_ _} _.
197
Arguments gmapUR _ {_ _} _.
198 199

Section properties.
200
Context `{Countable K} {A : cmraT}.
Robbert Krebbers's avatar
Robbert Krebbers committed
201
Implicit Types m : gmap K A.
202
Implicit Types i : K.
203 204
Implicit Types x y : A.

205
Global Instance lookup_op_homomorphism :
206 207
  MonoidHomomorphism op op () (lookup i : gmap K A  option A).
Proof. split; [split|]; try apply _. intros m1 m2; by rewrite lookup_op. done. Qed.
208

209
Lemma lookup_opM m1 mm2 i : (m1 ? mm2) !! i = m1 !! i  (mm2 = (!! i)).
210
Proof. destruct mm2; by rewrite /= ?lookup_op ?right_id_L. Qed.
211

212
Lemma lookup_validN_Some n m i x : {n} m  m !! i {n} Some x  {n} x.
Robbert Krebbers's avatar
Robbert Krebbers committed
213
Proof. by move=> /(_ i) Hm Hi; move:Hm; rewrite Hi. Qed.
214
Lemma lookup_valid_Some m i x :  m  m !! i  Some x   x.
215
Proof. move=> Hm Hi. move:(Hm i). by rewrite Hi. Qed.
216

217
Lemma insert_validN n m i x : {n} x  {n} m  {n} <[i:=x]>m.
218
Proof. by intros ?? j; destruct (decide (i = j)); simplify_map_eq. Qed.
219
Lemma insert_valid m i x :  x   m   <[i:=x]>m.
220
Proof. by intros ?? j; destruct (decide (i = j)); simplify_map_eq. Qed.
221
Lemma singleton_validN n i x : {n} ({[ i := x ]} : gmap K A)  {n} x.
222
Proof.
Robbert Krebbers's avatar
Robbert Krebbers committed
223 224 225
  split.
  - move=>/(_ i); by simplify_map_eq.
  - intros. apply insert_validN. done. apply: ucmra_unit_validN.
226
Qed.
227 228
Lemma singleton_valid i x :  ({[ i := x ]} : gmap K A)   x.
Proof. rewrite !cmra_valid_validN. by setoid_rewrite singleton_validN. Qed.
229

230 231 232 233 234
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.

235
Lemma insert_singleton_op m i x : m !! i = None  <[i:=x]> m = {[ i := x ]}  m.
236
Proof.
237 238 239
  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.
240 241
Qed.

Robbert Krebbers's avatar
Robbert Krebbers committed
242 243 244 245 246 247 248 249
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.
250
Lemma op_singleton (i : K) (x y : A) :
251
  {[ i := x ]}  {[ i := y ]} = ({[ i := x  y ]} : gmap K A).
252
Proof. by apply (merge_singleton _ _ _ x y). Qed.
253

254
Global Instance gmap_core_id m : ( x : A, CoreId x)  CoreId m.
Robbert Krebbers's avatar
Robbert Krebbers committed
255
Proof.
256 257
  intros; apply core_id_total=> i.
  rewrite lookup_core. apply (core_id_core _).
Robbert Krebbers's avatar
Robbert Krebbers committed
258
Qed.
259 260 261
Global Instance gmap_singleton_core_id i (x : A) :
  CoreId x  CoreId {[ i := x ]}.
Proof. intros. by apply core_id_total, core_singleton'. Qed.
262

Robbert Krebbers's avatar
Robbert Krebbers committed
263
Lemma singleton_includedN n m i x :
264
  {[ i := x ]} {n} m   y, m !! i {n} Some y  Some x {n} Some y.
Robbert Krebbers's avatar
Robbert Krebbers committed
265 266
Proof.
  split.
267 268 269 270 271 272 273
  - 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.
274 275 276
Qed.
(* We do not have [x ≼ y ↔ ∀ n, x ≼{n} y], so we cannot use the previous lemma *)
Lemma singleton_included m i x :
277
  {[ i := x ]}  m   y, m !! i  Some y  Some x  Some y.
278 279 280
Proof.
  split.
  - move=> [m' /(_ i)]; rewrite lookup_op lookup_singleton.
281 282 283 284 285 286
    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
287 288
Qed.

289 290 291
Global Instance singleton_cancelable i x :
  Cancelable (Some x)  Cancelable {[ i := x ]}.
Proof.
292 293 294 295
  intros ? n m1 m2 Hv EQ j. move: (Hv j) (EQ j). rewrite !lookup_op.
  destruct (decide (i = j)) as [->|].
  - rewrite lookup_singleton. by apply cancelableN.
  - by rewrite lookup_singleton_ne // !(left_id None _).
296 297
Qed.

298
Lemma insert_updateP (P : A  Prop) (Q : gmap K A  Prop) m i x :
299
  x ~~>: P  ( y, P y  Q (<[i:=y]>m))  <[i:=x]>m ~~>: Q.
300
Proof.
Robbert Krebbers's avatar
Robbert Krebbers committed
301 302
  intros Hx%option_updateP' HP; apply cmra_total_updateP=> n mf Hm.
  destruct (Hx n (Some (mf !! i))) as ([y|]&?&?); try done.
303
  { by generalize (Hm i); rewrite lookup_op; simplify_map_eq. }
304 305
  exists (<[i:=y]> m); split; first by auto.
  intros j; move: (Hm j)=>{Hm}; rewrite !lookup_op=>Hm.
306
  destruct (decide (i = j)); simplify_map_eq/=; auto.
307
Qed.
308
Lemma insert_updateP' (P : A  Prop) m i x :
309
  x ~~>: P  <[i:=x]>m ~~>: λ m',  y, m' = <[i:=y]>m  P y.
310 311 312
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.
313

314
Lemma singleton_updateP (P : A  Prop) (Q : gmap K A  Prop) i x :
315
  x ~~>: P  ( y, P y  Q {[ i := y ]})  {[ i := x ]} ~~>: Q.
316 317
Proof. apply insert_updateP. Qed.
Lemma singleton_updateP' (P : A  Prop) i x :
318
  x ~~>: P  {[ i := x ]} ~~>: λ m,  y, m = {[ i := y ]}  P y.
319 320 321
Proof. apply insert_updateP'. Qed.
Lemma singleton_update i (x y : A) : x ~~> y  {[ i := x ]} ~~> {[ i := y ]}.
Proof. apply insert_update. Qed.
322

323
Lemma delete_update m i : m ~~> delete i m.
324
Proof.
325 326 327 328
  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.
329
Qed.
330

331 332 333 334 335 336 337 338 339 340 341
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.

342
Section freshness.
343
  Local Set Default Proof Using "Type*".
344 345 346 347 348 349 350 351 352
  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.
353 354
    { apply HQ; last done. by eapply not_elem_of_dom. }
    rewrite insert_singleton_op; last by eapply not_elem_of_dom.
355
    rewrite -assoc -insert_singleton_op;
356
      last by eapply (not_elem_of_dom (D:=gset K)); rewrite dom_op not_elem_of_union.
357 358 359 360 361 362 363 364 365 366 367 368 369
    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.

370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395
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.

396 397
Lemma alloc_local_update m1 m2 i x :
  m1 !! i = None   x  (m1,m2) ~l~> (<[i:=x]>m1, <[i:=x]>m2).
398
Proof.
399 400 401 402 403 404 405
  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 //.
406
Qed.
407

408 409 410
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.
411

412 413 414 415
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).
416
Proof.
417 418 419 420 421 422 423 424
  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.
425 426
Qed.

427 428 429 430
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' ]}).
431
Proof.
432
  intros. rewrite /singletonM /map_singleton -(insert_insert  i y' y).
Robbert Krebbers's avatar
Robbert Krebbers committed
433
  by eapply insert_local_update; [|eapply lookup_insert|].
434
Qed.
435

436 437
Lemma delete_local_update m1 m2 i x `{!Exclusive x} :
  m2 !! i = Some x  (m1, m2) ~l~> (delete i m1, delete i m2).
438
Proof.
439 440 441 442 443 444
  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.
445
  - by rewrite lookup_op !lookup_delete_ne // lookup_op.
446 447 448 449 450 451
Qed.

Lemma delete_singleton_local_update m i x `{!Exclusive x} :
  (m, {[ i := x ]}) ~l~> (delete i m, ).
Proof.
  rewrite -(delete_singleton i x).
Robbert Krebbers's avatar
Robbert Krebbers committed
452
  by eapply delete_local_update, lookup_singleton.
453
Qed.
454 455 456 457 458

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.
459 460 461 462 463 464
  intros Hm1i Hm2i. apply local_update_unital=> n mf Hmv Hm; simpl in *.
  split; [eauto using delete_validN|].
  intros j. destruct (decide (i = j)) as [->|].
  - move: (Hm j). rewrite !lookup_op Hm1i Hm2i !lookup_delete. intros Hmx.
    rewrite (cancelableN mx n (mf !! j) None) ?right_id // -Hmx -Hm1i. apply Hmv.
  - by rewrite lookup_op !lookup_delete_ne // Hm lookup_op.
465 466 467 468 469 470 471 472 473
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.
474 475
End properties.

476
(** Functor *)
477
Instance gmap_fmap_ne `{Countable K} {A B : ofeT} (f : A  B) n :
478 479
  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.
480
Instance gmap_fmap_cmra_morphism `{Countable K} {A B : cmraT} (f : A  B)
481
  `{!CmraMorphism f} : CmraMorphism (fmap f : gmap K A  gmap K B).
482
Proof.
Robbert Krebbers's avatar
Robbert Krebbers committed
483
  split; try apply _.
484 485 486 487
  - by intros n m ? i; rewrite lookup_fmap; apply (cmra_morphism_validN _).
  - intros m. apply Some_proper=>i. rewrite lookup_fmap !lookup_omap lookup_fmap.
    case: (m!!i)=>//= ?. apply cmra_morphism_pcore, _.
  - intros m1 m2 i. by rewrite lookup_op !lookup_fmap lookup_op cmra_morphism_op.
488
Qed.
489 490
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).
491 492
Instance gmapC_map_ne `{Countable K} {A B} :
  NonExpansive (@gmapC_map K _ _ A B).
493
Proof.
494
  intros n f g Hf m k; rewrite /= !lookup_fmap.
495 496
  destruct (_ !! k) eqn:?; simpl; constructor; apply Hf.
Qed.
Ralf Jung's avatar
Ralf Jung committed
497

498 499 500
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
501
|}.
502
Next Obligation.
503
  by intros K ?? F A1 A2 B1 B2 n f g Hfg; apply gmapC_map_ne, cFunctor_ne.
504
Qed.
Ralf Jung's avatar
Ralf Jung committed
505
Next Obligation.
506
  intros K ?? F A B x. rewrite /= -{2}(map_fmap_id x).
507
  apply map_fmap_equiv_ext=>y ??; apply cFunctor_id.
Ralf Jung's avatar
Ralf Jung committed
508 509
Qed.
Next Obligation.
510
  intros K ?? F A1 A2 A3 B1 B2 B3 f g f' g' x. rewrite /= -map_fmap_compose.
511
  apply map_fmap_equiv_ext=>y ??; apply cFunctor_compose.
512
Qed.
513 514
Instance gmapCF_contractive K `{Countable K} F :
  cFunctorContractive F  cFunctorContractive (gmapCF K F).
515
Proof.
516
  by intros ? A1 A2 B1 B2 n f g Hfg; apply gmapC_map_ne, cFunctor_contractive.
517 518
Qed.

519 520 521
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)
522
|}.
523
Next Obligation.
524
  by intros K ?? F A1 A2 B1 B2 n f g Hfg; apply gmapC_map_ne, rFunctor_ne.
525
Qed.
526 527
Next Obligation.
  intros K ?? F A B x. rewrite /= -{2}(map_fmap_id x).
528
  apply map_fmap_equiv_ext=>y ??; apply rFunctor_id.
529 530 531
Qed.
Next Obligation.
  intros K ?? F A1 A2 A3 B1 B2 B3 f g f' g' x. rewrite /= -map_fmap_compose.
532
  apply map_fmap_equiv_ext=>y ??; apply rFunctor_compose.
Ralf Jung's avatar
Ralf Jung committed
533
Qed.
534
Instance gmapRF_contractive K `{Countable K} F :
535
  rFunctorContractive F  urFunctorContractive (gmapURF K F).
536
Proof.
537
  by intros ? A1 A2 B1 B2 n f g Hfg; apply gmapC_map_ne, rFunctor_contractive.
538
Qed.