phanerozoic/Coq-Iris huggingface dataset:
(* iris/algebra/gmap.v *)
Lemma gmap_cmra_mixin : CmraMixin (gmap K A).
Proof.
  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).
  - intros m; split.
    + by intros ? n i; apply cmra_valid_validN.
    + intros Hm i; apply cmra_valid_validN=> n; apply Hm.
  - intros n m Hm i ? ?; eauto using cmra_validN_le.
  - by intros m1 m2 m3 i; rewrite !lookup_op assoc.
  - by intros m1 m2 i; rewrite !lookup_op comm.
  - 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.
    rewrite !lookup_core. by apply cmra_core_mono.
  - intros n m1 m2 Hm i; apply cmra_validN_op_l with (m2 !! i).
    by rewrite -lookup_op.
  - intros n m y1 y2 Hm Heq.
    refine ((λ FUN, _) (λ i, cmra_extend n (m !! i) (y1 !! i) (y2 !! i) (Hm i) _));
      last by rewrite -lookup_op.
    exists (map_imap (λ i _, projT1 (FUN i)) y1).
    exists (map_imap (λ i _, proj1_sig (projT2 (FUN i))) y2).
    split; [|split]=>i; rewrite ?lookup_op !map_lookup_imap;
    destruct (FUN i) as (z1i&z2i&Hmi&Hz1i&Hz2i)=>/=.
    + destruct (y1 !! i), (y2 !! i); inversion Hz1i; inversion Hz2i; subst=>//.
    + revert Hz1i. case: (y1!!i)=>[?|] //.
    + revert Hz2i. case: (y2!!i)=>[?|] //.
Qed.
(* iris/algebra/gmap.v *)
Canonical gmapR
:= Cmra (gmap K A) gmap_cmra_mixin.
(* iris/algebra/gmap.v *)
Instance gmap_cmra_discrete : CmraDiscrete A → CmraDiscrete gmapR.
Proof. split; [apply _|]. intros m ? i. by apply: cmra_discrete_valid. Qed.
(* iris/algebra/gmap.v *)
Lemma gmap_ucmra_mixin : UcmraMixin (gmap K A).
Proof.
  split.
  - by intros i; rewrite lookup_empty.
  - by intros m i; rewrite /= lookup_op lookup_empty (left_id_L None _).
  - constructor=> i. by rewrite lookup_omap lookup_empty.
Qed.
(* iris/algebra/gmap.v *)
Canonical gmapUR
:= Ucmra (gmap K A) gmap_ucmra_mixin.
(* iris/algebra/gmap.v *)
Instance gmap_op_empty_l_L : LeftId (=@{gmap K A}) ∅ op.
Proof. apply _. Qed.
(* iris/algebra/gmap.v *)
Instance gmap_op_empty_r : RightId (=@{gmap K A}) ∅ op.
Proof. apply _. Qed.
(* iris/algebra/gmap.v *)
Instance lookup_op_homomorphism i :
  MonoidHomomorphism op op (≡) (lookup i : gmap K A → option A).
Proof.
  split; [split|]; try apply _.
  - intros m1 m2; by rewrite lookup_op.
  - done.
Qed.
(* iris/algebra/gmap.v *)
Lemma lookup_opM m1 mm2 i : (m1 ⋅? mm2) !! i = m1 !! i ⋅ (mm2 ≫= (.!! i)).
Proof. destruct mm2; by rewrite /= ?lookup_op ?right_id_L. Qed.
(* iris/algebra/gmap.v *)
Lemma lookup_validN_Some n m i x : ✓{n} m → m !! i ≡{n}≡ Some x → ✓{n} x.
Proof. by move=> /(_ i) Hm Hi; move:Hm; rewrite Hi. Qed.
(* iris/algebra/gmap.v *)
Lemma lookup_valid_Some m i x : ✓ m → m !! i ≡ Some x → ✓ x.
Proof. move=> Hm Hi. move:(Hm i). by rewrite Hi. Qed.
(* iris/algebra/gmap.v *)
Lemma insert_validN n m i x : ✓{n} x → ✓{n} m → ✓{n} <[i:=x]>m.
Proof. by intros ?? j; destruct (decide (i = j)); simplify_map_eq. Qed.
(* iris/algebra/gmap.v *)
Lemma insert_valid m i x : ✓ x → ✓ m → ✓ <[i:=x]>m.
Proof. by intros ?? j; destruct (decide (i = j)); simplify_map_eq. Qed.
(* iris/algebra/gmap.v *)
Lemma singleton_validN n i x : ✓{n} ({[ i := x ]} : gmap K A) ↔ ✓{n} x.
Proof.
  split.
  - move=>/(_ i); by simplify_map_eq.
  - intros. apply insert_validN; first done. apply: ucmra_unit_validN.
Qed.
(* iris/algebra/gmap.v *)
Lemma singleton_valid i x : ✓ ({[ i := x ]} : gmap K A) ↔ ✓ x.
Proof. rewrite !cmra_valid_validN. by setoid_rewrite singleton_validN. Qed.
(* iris/algebra/gmap.v *)
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.
(* iris/algebra/gmap.v *)
Lemma delete_valid m i : ✓ m → ✓ (delete i m).
Proof. intros Hm j; destruct (decide (i = j)); by simplify_map_eq. Qed.
(* iris/algebra/gmap.v *)
Lemma insert_singleton_op m i x : m !! i = None → <[i:=x]> m = {[ i := x ]} ⋅ m.
Proof.
  intros Hi; apply map_eq=> j; destruct (decide (i = j)) as [->|].
  - by rewrite lookup_op lookup_insert_eq lookup_singleton_eq Hi right_id_L.
  - by rewrite lookup_op lookup_insert_ne // lookup_singleton_ne // left_id_L.
Qed.
(* iris/algebra/gmap.v *)
Lemma singleton_core (i : K) (x : A) cx :
  pcore x = Some cx → core {[ i := x ]} =@{gmap K A} {[ i := cx ]}.
Proof. apply omap_singleton_Some. Qed.
(* iris/algebra/gmap.v *)
Lemma singleton_core' (i : K) (x : A) cx :
  pcore x ≡ Some cx → core {[ i := x ]} ≡@{gmap K A} {[ i := cx ]}.
Proof.
  intros (cx'&?&<-)%Some_equiv_eq. by rewrite (singleton_core _ _ cx').
Qed.
(* iris/algebra/gmap.v *)
Lemma singleton_core_total `{!CmraTotal A} (i : K) (x : A) :
  core {[ i := x ]} =@{gmap K A} {[ i := core x ]}.
Proof. apply singleton_core. rewrite cmra_pcore_core //. Qed.
(* iris/algebra/gmap.v *)
Lemma singleton_op (i : K) (x y : A) :
  {[ i := x ]} ⋅ {[ i := y ]} =@{gmap K A} {[ i := x ⋅ y ]}.
Proof. by apply (merge_singleton _ _ _ x y). Qed.
(* iris/algebra/gmap.v *)
Instance singleton_is_op i a a1 a2 :
  IsOp a a1 a2 → IsOp' ({[ i := a ]} : gmap K A) {[ i := a1 ]} {[ i := a2 ]}.
Proof. rewrite /IsOp' /IsOp=> ->. by rewrite -singleton_op. Qed.
(* iris/algebra/gmap.v *)
Lemma gmap_core_id m : (∀ i x, m !! i = Some x → CoreId x) → CoreId m.
Proof.
  intros Hcore; apply core_id_total=> i.
  rewrite lookup_core. destruct (m !! i) as [x|] eqn:Hix; rewrite Hix; [|done].
  by eapply Hcore.
Qed.
(* iris/algebra/gmap.v *)
Instance gmap_core_id' m : (∀ x : A, CoreId x) → CoreId m.
Proof. auto using gmap_core_id. Qed.
(* iris/algebra/gmap.v *)
Instance gmap_singleton_core_id i (x : A) :
  CoreId x → CoreId {[ i := x ]}.
Proof. intros. by apply core_id_total, singleton_core'. Qed.
(* iris/algebra/gmap.v *)
Lemma singleton_includedN_l n m i x :
  {[ i := x ]} ≼{n} m ↔ ∃ y, m !! i ≡{n}≡ Some y ∧ Some x ≼{n} Some y.
Proof.
  split.
  - move=> [m' /(_ i)]; rewrite lookup_op lookup_singleton_eq=> Hi.
    exists (x ⋅? m' !! i). rewrite -Some_op_opM.
    split; first 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_eq lookup_partial_alter_eq Hi.
    + by rewrite lookup_op lookup_singleton_ne// lookup_partial_alter_ne// left_id.
Qed.
(* iris/algebra/gmap.v *)
(* We do not have [x ≼ y ↔ ∀ n, x ≼{n} y], so we cannot use the previous lemma *)
Lemma singleton_included_l m i x :
  {[ i := x ]} ≼ m ↔ ∃ y, m !! i ≡ Some y ∧ Some x ≼ Some y.
Proof.
  split.
  - move=> [m' /(_ i)]; rewrite lookup_op lookup_singleton_eq.
    exists (x ⋅? m' !! i). by rewrite -Some_op_opM.
  - intros (y&Hi&[mz Hy]). exists (partial_alter (λ _, mz) i m).
    intros j; destruct (decide (i = j)) as [->|].
    + by rewrite lookup_op lookup_singleton_eq lookup_partial_alter_eq Hi.
    + by rewrite lookup_op lookup_singleton_ne// lookup_partial_alter_ne// left_id.
Qed.
(* iris/algebra/gmap.v *)
Lemma singleton_included_exclusive_l m i x :
  Exclusive x → ✓ m →
  {[ i := x ]} ≼ m ↔ m !! i ≡ Some x.
Proof.
  intros ? Hm. rewrite singleton_included_l. split; last by eauto.
  intros (y&?&->%(Some_included_exclusive _)); eauto using lookup_valid_Some.
Qed.
(* iris/algebra/gmap.v *)
Lemma singleton_included i x y :
  {[ i := x ]} ≼ ({[ i := y ]} : gmap K A) ↔ Some x ≼ Some y.
Proof.
  rewrite singleton_included_l. split.
  - intros (y'&Hi&?). rewrite lookup_insert_eq in Hi. by rewrite Hi.
  - intros ?. exists y. by rewrite lookup_insert_eq.
Qed.
(* iris/algebra/gmap.v *)
Lemma singleton_included_total `{!CmraTotal A}  i x y :
  {[ i := x ]} ≼ ({[ i := y ]} : gmap K A) ↔ x ≼ y.
Proof. rewrite singleton_included Some_included_total. done. Qed.
(* iris/algebra/gmap.v *)
Lemma singleton_included_mono i x y :
  x ≼ y → {[ i := x ]} ≼ ({[ i := y ]} : gmap K A).
Proof. intros Hincl. apply singleton_included, Some_included_mono. done. Qed.
(* iris/algebra/gmap.v *)
Instance singleton_cancelable i x :
  Cancelable (Some x) → Cancelable {[ i := x ]}.
Proof.
  intros ? n m1 m2 Hv EQ j. move: (Hv j) (EQ j). rewrite !lookup_op.
  destruct (decide (i = j)) as [->|].
  - rewrite lookup_singleton_eq. by apply cancelableN.
  - by rewrite lookup_singleton_ne // !(left_id None _).
Qed.
(* iris/algebra/gmap.v *)
Instance gmap_cancelable (m : gmap K A) :
  (∀ x : A, IdFree x) → (∀ x : A, Cancelable x) → Cancelable m.
Proof.
  intros ?? n m1 m2 ?? i. apply (cancelableN (m !! i)); by rewrite -!lookup_op.
Qed.
(* iris/algebra/gmap.v *)
Lemma insert_op m1 m2 i x y :
  <[i:=x ⋅ y]>(m1 ⋅ m2) =  <[i:=x]>m1 ⋅ <[i:=y]>m2.
Proof. by rewrite (insert_merge (⋅) m1 m2 i (x ⋅ y) x y). Qed.
(* iris/algebra/gmap.v *)
Lemma insert_updateP (P : A → Prop) (Q : gmap K A → Prop) m i x :
  x ~~>: P →
  (∀ y, P y → Q (<[i:=y]>m)) →
  <[i:=x]>m ~~>: Q.
Proof.
  intros Hx%option_updateP' HP; apply cmra_total_updateP=> n mf Hm.
  destruct (Hx n (Some (mf !! i))) as ([y|]&?&?); try done.
  { by generalize (Hm i); rewrite lookup_op; simplify_map_eq. }
  exists (<[i:=y]> m); split; first by auto.
  intros j; move: (Hm j)=>{Hm}; rewrite !lookup_op=>Hm.
  destruct (decide (i = j)); simplify_map_eq/=; auto.
Qed.
(* iris/algebra/gmap.v *)
Lemma insert_updateP' (P : A → Prop) m i x :
  x ~~>: P → <[i:=x]>m ~~>: λ m', ∃ y, m' = <[i:=y]>m ∧ P y.
Proof. eauto using insert_updateP. Qed.
(* iris/algebra/gmap.v *)
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.
(* iris/algebra/gmap.v *)
Lemma singleton_updateP (P : A → Prop) (Q : gmap K A → Prop) i x :
  x ~~>: P → (∀ y, P y → Q {[ i := y ]}) → {[ i := x ]} ~~>: Q.
Proof. apply insert_updateP. Qed.
(* iris/algebra/gmap.v *)
Lemma singleton_updateP' (P : A → Prop) i x :
  x ~~>: P → {[ i := x ]} ~~>: λ m, ∃ y, m = {[ i := y ]} ∧ P y.
Proof. apply insert_updateP'. Qed.
(* iris/algebra/gmap.v *)
Lemma singleton_update i (x y : A) : x ~~> y → {[ i := x ]} ~~> {[ i := y ]}.
Proof. apply insert_update. Qed.
(* iris/algebra/gmap.v *)
Lemma delete_update m i : m ~~> delete i m.
Proof.
  apply cmra_total_update=> n mf Hm j; destruct (decide (i = j)); subst.
  - move: (Hm j). rewrite !lookup_op lookup_delete_eq left_id.
    apply cmra_validN_op_r.
  - move: (Hm j). by rewrite !lookup_op lookup_delete_ne.
Qed.
(* iris/algebra/gmap.v *)
Lemma gmap_op_union m1 m2 : m1 ##ₘ m2 → m1 ⋅ m2 = m1 ∪ m2.
Proof. intros Hm. apply map_disjoint_merge_as_union; done. Qed.
(* iris/algebra/gmap.v *)
Lemma gmap_op_valid0_disjoint m1 m2 :
  ✓{0ᵢ} (m1 ⋅ m2) → (∀ k x, m1 !! k = Some x → Exclusive x) → m1 ##ₘ m2.
Proof.
  unfold Exclusive. intros Hvalid Hexcl k.
  specialize (Hvalid k). rewrite lookup_op in Hvalid. specialize (Hexcl k).
  destruct (m1 !! k), (m2 !! k); [|done..].
  rewrite -Some_op Some_validN in Hvalid. naive_solver.
Qed.
(* iris/algebra/gmap.v *)
Lemma gmap_op_valid_disjoint m1 m2 :
  ✓ (m1 ⋅ m2) → (∀ k x, m1 !! k = Some x → Exclusive x) → m1 ##ₘ m2.
Proof. move=> /cmra_valid_validN /(_ 0ᵢ). apply gmap_op_valid0_disjoint. Qed.
(* iris/algebra/gmap.v *)
Lemma dom_op m1 m2 : dom (m1 ⋅ m2) = dom m1 ∪ dom m2.
Proof.
  apply set_eq=> i; rewrite elem_of_union !elem_of_dom.
  unfold is_Some; setoid_rewrite lookup_op.
  destruct (m1 !! i), (m2 !! i); naive_solver.
Qed.
(* iris/algebra/gmap.v *)
Lemma dom_included m1 m2 : m1 ≼ m2 → dom m1 ⊆ dom m2.
Proof.
  rewrite lookup_included=>? i; rewrite !elem_of_dom. by apply is_Some_included.
Qed.
(* iris/algebra/gmap.v *)
Lemma alloc_updateP_strong_dep (Q : gmap K A → Prop) (I : K → Prop) m (f : K → A) :
    pred_infinite I →
    (∀ i, m !! i = None → I i → ✓ (f i)) →
    (∀ i, m !! i = None → I i → Q (<[i:=f i]>m)) → m ~~>: Q.
Proof.
    move=> /(pred_infinite_set I (C:=gset K)) HP ? HQ.
    apply cmra_total_updateP. intros n mf Hm.
    destruct (HP (dom (m ⋅ mf))) as [i [Hi1 Hi2]].
    assert (m !! i = None).
    { eapply not_elem_of_dom. revert Hi2.
      rewrite dom_op not_elem_of_union. naive_solver. }
    exists (<[i:=f i]>m); split.
    - by apply HQ.
    - rewrite insert_singleton_op //.
      rewrite -assoc -insert_singleton_op; last by eapply not_elem_of_dom.
    apply insert_validN; [apply cmra_valid_validN|]; auto.
  Qed.
(* iris/algebra/gmap.v *)
Lemma alloc_updateP_strong (Q : gmap K A → Prop) (I : K → Prop) m x :
    pred_infinite I →
    ✓ x → (∀ i, m !! i = None → I i → Q (<[i:=x]>m)) → m ~~>: Q.
Proof.
    move=> HP ? HQ. eapply (alloc_updateP_strong_dep _ _ _ (λ _, x)); eauto.
  Qed.
(* iris/algebra/gmap.v *)
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 _ (λ _, True));
    eauto using pred_infinite_True.
  Qed.
(* iris/algebra/gmap.v *)
Lemma alloc_updateP_cofinite (Q : gmap K A → Prop) (J : gset K) m x :
    ✓ x → (∀ i, m !! i = None → i ∉ J → Q (<[i:=x]>m)) → m ~~>: Q.
Proof.
    eapply alloc_updateP_strong.
    apply (pred_infinite_set (C:=gset K)).
    intros E. exists (fresh (J ∪ E)).
    apply not_elem_of_union, is_fresh.
  Qed.
(* iris/algebra/gmap.v *)
(* Variants without the universally quantified Q, for use in case that is an evar. *)
Lemma alloc_updateP_strong_dep' m (f : K → A) (I : K → Prop) :
    pred_infinite I →
    (∀ i, m !! i = None → I i → ✓ (f i)) →
    m ~~>: λ m', ∃ i, I i ∧ m' = <[i:=f i]>m ∧ m !! i = None.
Proof. eauto using alloc_updateP_strong_dep. Qed.
(* iris/algebra/gmap.v *)
Lemma alloc_updateP_strong' m x (I : K → Prop) :
    pred_infinite I →
    ✓ x → m ~~>: λ m', ∃ i, I i ∧ m' = <[i:=x]>m ∧ m !! i = None.
Proof. eauto using alloc_updateP_strong. Qed.
(* iris/algebra/gmap.v *)
Lemma alloc_updateP' m x :
    ✓ x → m ~~>: λ m', ∃ i, m' = <[i:=x]>m ∧ m !! i = None.
Proof. eauto using alloc_updateP. Qed.
(* iris/algebra/gmap.v *)
Lemma alloc_updateP_cofinite' m x (J : gset K) :
    ✓ x → m ~~>: λ m', ∃ i, i ∉ J ∧ m' = <[i:=x]>m ∧ m !! i = None.
Proof. eauto using alloc_updateP_cofinite. Qed.
(* iris/algebra/gmap.v *)
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_eq.
    move:Hy; case: (gf !! i)=>[x|]; rewrite /= ?right_id //.
  - move:(Hg i'). by rewrite !lookup_op lookup_singleton_ne // !left_id.
Qed.
(* iris/algebra/gmap.v *)
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.
(* iris/algebra/gmap.v *)
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.
(* iris/algebra/gmap.v *)
Lemma gmap_local_update m1 m2 m1' m2' :
  (∀ i, (m1 !! i, m2 !! i) ~l~> (m1' !! i, m2' !! i)) →
  (m1, m2) ~l~> (m1', m2').
Proof.
  intros Hupd. apply local_update_unital=> n mf Hmv Hm.
  apply forall_and_distr=> i. rewrite lookup_op -cmra_opM_fmap_Some.
  apply Hupd; simpl; first done. by rewrite Hm lookup_op cmra_opM_fmap_Some.
Qed.
(* iris/algebra/gmap.v *)
Lemma alloc_local_update m1 m2 i x :
  m1 !! i = None → ✓ x → (m1,m2) ~l~> (<[i:=x]>m1, <[i:=x]>m2).
Proof.
  intros Hi ?. apply gmap_local_update=> j.
  destruct (decide (i = j)) as [->|]; last by rewrite !lookup_insert_ne.
  rewrite !lookup_insert_eq Hi. by apply alloc_option_local_update.
Qed.
(* iris/algebra/gmap.v *)
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.
(* iris/algebra/gmap.v *)
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).
Proof.
  intros Hi1 Hi2 Hup. apply gmap_local_update=> j.
  destruct (decide (i = j)) as [->|]; last by rewrite !lookup_insert_ne.
  rewrite !lookup_insert_eq Hi1 Hi2. by apply option_local_update.
Qed.
(* iris/algebra/gmap.v *)
Lemma singleton_local_update_any m i y x' y' :
  (∀ x, m !! i = Some x → (x, y) ~l~> (x', y')) →
  (m, {[ i := y ]}) ~l~> (<[i:=x']>m, {[ i := y' ]}).
Proof.
  intros. apply gmap_local_update=> j.
  destruct (decide (i = j)) as [->|]; last by rewrite !lookup_insert_ne.
  rewrite !lookup_singleton_eq lookup_insert_eq.
  destruct (m !! j); first by eauto using option_local_update.
  apply local_update_total_valid0=> _ _ /option_includedN; naive_solver.
Qed.
(* iris/algebra/gmap.v *)
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' ]}).
Proof.
  intros Hmi ?. apply singleton_local_update_any.
  intros x2. rewrite Hmi=>[=<-]. done.
Qed.
(* iris/algebra/gmap.v *)
Lemma delete_local_update m1 m2 i x `{!Exclusive x} :
  m2 !! i = Some x → (m1, m2) ~l~> (delete i m1, delete i m2).
Proof.
  intros Hi. apply gmap_local_update=> j.
  destruct (decide (i = j)) as [->|]; last by rewrite !lookup_delete_ne.
  rewrite !lookup_delete_eq Hi. by apply delete_option_local_update.
Qed.
(* iris/algebra/gmap.v *)
Lemma delete_singleton_local_update m i x `{!Exclusive x} :
  (m, {[ i := x ]}) ~l~> (delete i m, ∅).
Proof.
  rewrite -(delete_singleton_eq i x).
  by eapply delete_local_update, lookup_singleton_eq.
Qed.
(* iris/algebra/gmap.v *)
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 Hi1 Hi2. apply gmap_local_update=> j.
  destruct (decide (i = j)) as [->|]; last by rewrite !lookup_delete_ne.
  rewrite !lookup_delete_eq Hi1 Hi2. by apply delete_option_local_update_cancelable.
Qed.
(* iris/algebra/gmap.v *)
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_eq i x).
  apply (delete_local_update_cancelable m _ i (Some x));
    [done|by rewrite lookup_singleton_eq].
Qed.
(* iris/algebra/gmap.v *)
Lemma gmap_fmap_mono {B : cmra} (f : A → B) m1 m2 :
  Proper ((≡) ==> (≡)) f →
  (∀ x y, x ≼ y → f x ≼ f y) → m1 ≼ m2 → fmap f m1 ≼ fmap f m2.
Proof.
  intros ??. rewrite !lookup_included=> Hm i.
  rewrite !lookup_fmap. by apply option_fmap_mono.
Qed.
(* iris/algebra/gmap.v *)
Lemma big_opM_singletons m :
  ([^op map] k ↦ x ∈ m, {[ k := x ]}) = m.
Proof.
  (* We are breaking the big_opM abstraction here. The reason is that [map_ind]
     is too weak: we need an induction principle that visits all the keys in the
     right order, namely the order in which they appear in map_to_list.  Here,
     we achieve this by unfolding [big_opM] and doing induction over that list
     instead. *)
  rewrite big_op.big_opM_unseal /big_op.big_opM_def -{2}(list_to_map_to_list m).
  assert (NoDup (map_to_list m).*1) as Hnodup by apply NoDup_fst_map_to_list.
  revert Hnodup. induction (map_to_list m) as [|[k x] l IH]; csimpl; first done.
  intros [??]%NoDup_cons. rewrite IH //.
  rewrite insert_singleton_op ?not_elem_of_list_to_map_1 //.
Qed.
(* iris/algebra/gmap.v *)
Lemma big_opS_gset_to_gmap (X : gset K) (a : A) :
  ([^op set] x ∈ X, {[ x := a ]}) ≡ gset_to_gmap a X.
Proof.
  induction X as [|x X ? IH] using set_ind_L.
  { rewrite big_opS_empty gset_to_gmap_empty //. }
  rewrite big_opS_insert //.
  rewrite gset_to_gmap_union_singleton.
  rewrite insert_singleton_op; [|by rewrite lookup_gset_to_gmap_None].
  by rewrite IH.
Qed.
(* iris/algebra/gmap.v *)
Lemma big_opS_gset_to_gmap_L `{!LeibnizEquiv A} (X : gset K) (a : A) :
  ([^op set] x ∈ X, {[ x := a ]}) = gset_to_gmap a X.
Proof. apply leibniz_equiv, big_opS_gset_to_gmap. Qed.
(* iris/algebra/gmap.v *)
Lemma insert_alloc_local_update m1 m2 i x x' y' :
  m1 !! i = Some x → m2 !! i = None →
  (x, ε) ~l~> (x', y') →
  (m1, m2) ~l~> (<[i:=x']>m1, <[i:=y']>m2).
Proof.
  intros Hi1 Hi2 Hup. apply local_update_unital=> n mf Hm1v Hm.
  assert (mf !! i ≡{n}≡ Some x) as Hif.
  { move: (Hm i). by rewrite lookup_op Hi1 Hi2 left_id. }
  destruct (Hup n (mf !! i)) as [Hx'v Hx'eq].
  { move: (Hm1v i). by rewrite Hi1. }
  { by rewrite Hif -(inj_iff Some) -Some_op_opM -Some_op left_id. }
  split.
  - by apply insert_validN.
  - simpl in Hx'eq. by rewrite -(insert_idN n mf i x) // -insert_op -Hm Hx'eq Hif.
Qed.
(* iris/algebra/gmap.v *)
(* Functor *)
Instance gmap_fmap_ne {SI : sidx} `{Countable K} {A B : ofe} (f : A → B) n :
  Proper (dist n ==> dist n) f → Proper (dist n ==> dist (A:=gmap K _) n) (fmap f).
Proof. by intros ? m m' Hm k; rewrite !lookup_fmap; apply option_fmap_ne. Qed.
(* iris/algebra/gmap.v *)
Lemma gmap_fmap_ne_ext {SI : sidx} `{Countable K}
  {A : Type} {B : ofe} (f1 f2 : A → B) (m : gmap K A) n :
  (∀ i x, m !! i = Some x → f1 x ≡{n}≡ f2 x) →
  f1 <$> m ≡{n}≡ f2 <$> m.
Proof.
  move => Hf i. rewrite !lookup_fmap.
  destruct (m !! i) eqn:?; constructor; by eauto.
Qed.
(* iris/algebra/gmap.v *)
Instance gmap_fmap_cmra_morphism {SI : sidx} `{Countable K} {A B : cmra}
  (f : A → B) `{!CmraMorphism f} : CmraMorphism (fmap f : gmap K A → gmap K B).
Proof.
  split; try apply _.
  - 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.
Qed.
(* iris/algebra/gmap.v *)
Definition gmapO_map {SI : sidx} `{Countable K} {A B: ofe} (f: A -n> B) :
  gmapO K A -n> gmapO K B
:= OfeMor (fmap f : gmapO K A → gmapO K B).
(* iris/algebra/gmap.v *)
Instance gmapO_map_ne {SI : sidx} `{Countable K} {A B: ofe} :
  NonExpansive (@gmapO_map _ K _ _ A B).
Proof.
  intros n f g Hf m k; rewrite /= !lookup_fmap.
  destruct (_ !! k) eqn:?; simpl; constructor; apply Hf.
Qed.
(* iris/algebra/gmap.v *)
Definition gmapOF {SI : sidx} K `{Countable K}
    (F : oFunctor) : oFunctor := {|
  oFunctor_car A _ B _ := gmapO K (oFunctor_car F A B);
  oFunctor_map A1 _ A2 _ B1 _ B2 _ fg := gmapO_map (oFunctor_map F fg)
|}.
Next Obligation.
  intros ? K ?? F A1 ? A2 ? B1 ? B2 ? n f g Hfg.
  by apply gmapO_map_ne, oFunctor_map_ne.
Qed.
(* iris/algebra/gmap.v *)
Instance gmapOF_contractive {SI : sidx} `{Countable K} F :
  oFunctorContractive F → oFunctorContractive (gmapOF K F).
Proof.
  by intros ? ? A1 ? A2 ? B1 ? B2 ? n f g Hfg;
    apply gmapO_map_ne, oFunctor_map_contractive.
Qed.
(* iris/algebra/gmap.v *)
Definition gmapURF {SI : sidx} K `{Countable K}
    (F : rFunctor) : urFunctor := {|
  urFunctor_car A _ B _ := gmapUR K (rFunctor_car F A B);
  urFunctor_map A1 _ A2 _ B1 _ B2 _ fg := gmapO_map (rFunctor_map F fg)
|}.
Next Obligation.
  by intros ? K ?? F A1 ? A2 ? B1 ? B2 ? n f g Hfg;
    apply gmapO_map_ne, rFunctor_map_ne.
Qed.
(* iris/algebra/gmap.v *)
Instance gmapURF_contractive {SI : sidx} `{Countable K} F :
  rFunctorContractive F → urFunctorContractive (gmapURF K F).
Proof.
  intros ? A1 ? A2 ? B1 ? B2 ? n f g Hfg;
    by apply gmapO_map_ne, rFunctor_map_contractive.
Qed.
(* iris/algebra/gmap.v *)
Definition gmapRF {SI : sidx} K `{Countable K}
    (F : rFunctor) : rFunctor
:= {|
  rFunctor_car A _ B _ := gmapR K (rFunctor_car F A B);
  rFunctor_map A1 _ A2 _ B1 _ B2 _ fg := gmapO_map (rFunctor_map F fg)
|}.
(* iris/algebra/gmap.v *)
Instance gmapRF_contractive {SI : sidx} `{Countable K} F :
  rFunctorContractive F → rFunctorContractive (gmapRF K F).
Proof. apply gmapURF_contractive. Qed.
(* iris/algebra/gmultiset.v *)
Canonical gmultisetO
:= discreteO (gmultiset K).
(* iris/algebra/gmultiset.v *)
Instance gmultiset_valid_instance : Valid (gmultiset K)
:= λ _, True.
(* iris/algebra/gmultiset.v *)
Instance gmultiset_validN_instance : ValidN (gmultiset K)
:= λ _ _, True.
(* iris/algebra/gmultiset.v *)
Instance gmultiset_unit_instance : Unit (gmultiset K)
:= (∅ : gmultiset K).
(* iris/algebra/gmultiset.v *)
Instance gmultiset_op_instance : Op (gmultiset K)
:= disj_union.
(* iris/algebra/gmultiset.v *)
Instance gmultiset_pcore_instance : PCore (gmultiset K)
:= λ X, Some ∅.