phanerozoic/Coq-Iris huggingface dataset:
(* iris/algebra/lib/frac_auth.v *)
Lemma frac_auth_auth_dfrac_op_valid dq1 dq2 a b :
    ✓ (●F{dq1} a ⋅ ●F{dq2} b) → ✓ (dq1 ⋅ dq2) ∧ a ≡ b.
Proof.
    rewrite auth_auth_dfrac_op_valid.
    intros [?[[_?]%Some_equiv_inj%pair_equiv_inj ?]]; by split.
  Qed.
(* iris/algebra/lib/frac_auth.v *)
Lemma frac_auth_auth_op_valid a b :
    ✓ (●F a ⋅ ●F b) → False.
Proof. by intros [? _]%frac_auth_auth_dfrac_op_valid. Qed.
(* iris/algebra/lib/frac_auth.v *)
Lemma frac_auth_frag_op_validN n q1 q2 a b :
    ✓{n} (◯F{q1} a ⋅ ◯F{q2} b) ↔ (q1 + q2 ≤ 1)%Qp ∧ ✓{n} (a ⋅ b).
Proof. by rewrite -frac_auth_frag_op frac_auth_frag_validN. Qed.
(* iris/algebra/lib/frac_auth.v *)
Lemma frac_auth_frag_op_valid q1 q2 a b :
    ✓ (◯F{q1} a ⋅ ◯F{q2} b) ↔ (q1 + q2 ≤ 1)%Qp ∧ ✓ (a ⋅ b).
Proof. by rewrite -frac_auth_frag_op frac_auth_frag_valid. Qed.
(* iris/algebra/lib/frac_auth.v *)
Instance frac_auth_is_op (q q1 q2 : frac) (a a1 a2 : A) :
    IsOp q q1 q2 → IsOp a a1 a2 → IsOp' (◯F{q} a) (◯F{q1} a1) (◯F{q2} a2).
Proof. by rewrite /IsOp' /IsOp=> /leibniz_equiv_iff -> ->. Qed.
(* iris/algebra/lib/frac_auth.v *)
Instance frac_auth_is_op_core_id (q q1 q2 : frac) (a  : A) :
    CoreId a → IsOp q q1 q2 → IsOp' (◯F{q} a) (◯F{q1} a) (◯F{q2} a).
Proof.
    rewrite /IsOp' /IsOp=> ? /leibniz_equiv_iff ->.
    by rewrite -frac_auth_frag_op -core_id_dup.
  Qed.
(* iris/algebra/lib/frac_auth.v *)
Lemma frac_auth_update q a b a' b' :
    (a,b) ~l~> (a',b') → ●F a ⋅ ◯F{q} b ~~> ●F a' ⋅ ◯F{q} b'.
Proof.
    intros. by apply auth_update, option_local_update, prod_local_update_2.
  Qed.
(* iris/algebra/lib/frac_auth.v *)
Lemma frac_auth_update_1 a b a' : ✓ a' → ●F a ⋅ ◯F b ~~> ●F a' ⋅ ◯F a'.
Proof.
    intros. by apply auth_update, option_local_update, exclusive_local_update.
  Qed.
(* iris/algebra/lib/frac_auth.v *)
Lemma frac_auth_update_auth_persist dq a : ●F{dq} a ~~> ●F□ a.
Proof. apply auth_update_auth_persist. Qed.
(* iris/algebra/lib/frac_auth.v *)
Lemma frac_auth_updateP_auth_unpersist a :
    ●F□ a ~~>: λ k, ∃ q, k = ●F{#q} a.
Proof. apply auth_updateP_auth_unpersist. Qed.
(* iris/algebra/lib/frac_auth.v *)
Lemma frac_auth_updateP_both_unpersist q a b :
    ●F□ a ⋅ ◯F{q} b ~~>: λ k, ∃ q', k = ●F{#q'} a ⋅ ◯F{q} b.
Proof. apply auth_updateP_both_unpersist. Qed.
(* iris/algebra/lib/frac_auth.v *)
Definition frac_authURF {SI : sidx} (F : rFunctor) : urFunctor
:=
  authURF (optionURF (prodRF (constRF fracR) F)).
(* iris/algebra/lib/frac_auth.v *)
Instance frac_authURF_contractive {SI : sidx} F :
  rFunctorContractive F → urFunctorContractive (frac_authURF F).
Proof. apply _. Qed.
(* iris/algebra/lib/frac_auth.v *)
Definition frac_authRF {SI : sidx} (F : rFunctor) : rFunctor
:=
  authRF (optionURF (prodRF (constRF fracR) F)).
(* iris/algebra/lib/frac_auth.v *)
Instance frac_authRF_contractive {SI : sidx} F :
  rFunctorContractive F → rFunctorContractive (frac_authRF F).
Proof. apply _. Qed.
(* iris/algebra/lib/gmap_view.v *)
(* * CMRA for a "view of a gmap".

The authoritative element [gmap_view_auth] is any [gmap K V].  The fragments
[gmap_view_frag] represent ownership of a single key in that map.  Ownership is
governed by a discardable fraction, which provides the possibiltiy to obtain
persistent read-only ownership of a key.

The key frame-preserving updates are [gmap_view_alloc] to allocate a new key,
[gmap_view_update] to update a key given full ownership of the corresponding
fragment, and [gmap_view_persist] to make a key read-only by discarding any
fraction of the corresponding fragment. Crucially, the latter does not require
owning the authoritative element.

NOTE: The API surface for [gmap_view] is experimental and subject to change.  We
plan to add notations for authoritative elements and fragments, and hope to
support arbitrary maps as fragments. *)
Definition gmap_view_fragUR {SI : sidx}
     (K : Type) `{Countable K} (V : cmra) : ucmra
:=
  gmapUR K (prodR dfracR V).
(* iris/algebra/lib/gmap_view.v *)
(* If we exactly followed [auth], we'd write something like [f ≼{n} m ∧ ✓{n} m],
  which is equivalent to:
  [map_Forall (λ k fv, ∃ v, m !! k = Some v ∧ Some fv ≼{n} Some v ∧ ✓{n} v) f].
  (Note the use of [Some] in the inclusion; the elementwise RA might not have a
  unit and we want a reflexive relation!) However, [f] and [m] do not have the
  same type, so this definition does not type-check: the fractions have been
  erased from the authoritative [m]. So we additionally quantify over the erased
  fraction [dq] and [(dq, v)] becomes the authoritative value.

  An alternative definition one might consider is to replace the erased fraction
  by a hard-coded [DfracOwn 1], the biggest possible fraction. That would not
  work: we would end up with [Some dv ≼{n} Some (DfracOwn 1, v)] but that cannot
  be satisfied if [dv.1 = DfracDiscarded], a case that we definitely want to
  allow!

  It is possible that [∀ k, ∃ dq, let auth := (pair dq) <$> m !! k in ✓{n} auth
  ∧ f !! k ≼{n} auth] would also work, but now the proofs are all done already.  ;)
  The two are probably equivalent, with a proof similar to [lookup_includedN]? *)
Definition gmap_view_rel_raw n m f
:=
    map_Forall (λ k fv,
      ∃ v dq, m !! k = Some v ∧ ✓{n} (dq, v) ∧ (Some fv ≼{n} Some (dq, v))) f.
(* iris/algebra/lib/gmap_view.v *)
Lemma gmap_view_rel_raw_mono n1 n2 m1 m2 f1 f2 :
    gmap_view_rel_raw n1 m1 f1 →
    m1 ≡{n2}≡ m2 →
    f2 ≼{n2} f1 →
    (n2 ≤ n1)%sidx →
    gmap_view_rel_raw n2 m2 f2.
Proof.
    intros Hrel Hm Hf Hn k [dqa va] Hk.
    (* For some reason applying the lemma in [Hf] does not work... *)
    destruct (lookup_includedN n2 f2 f1) as [Hf' _].
    specialize (Hf' Hf k). clear Hf. rewrite Hk in Hf'.
    destruct (Some_includedN_is_Some _ _ _ Hf') as [[q' va'] Heq]. rewrite Heq in Hf'.
    specialize (Hrel _ _ Heq) as (v & dq & Hm1 & [Hvval Hdqval] & Hvincl). simpl in *.
    specialize (Hm k).
    edestruct (dist_Some_inv_l _ _ _ _ Hm Hm1) as (v' & Hm2 & Hv).
    eexists. exists dq. split; first done. split.
    { split; first done. simpl. rewrite -Hv. eapply cmra_validN_le; done. }
    rewrite -Hv. etrans; first exact Hf'.
    apply: cmra_includedN_le; done.
  Qed.
(* iris/algebra/lib/gmap_view.v *)
Lemma gmap_view_rel_raw_valid n m f :
    gmap_view_rel_raw n m f → ✓{n} f.
Proof.
    intros Hrel k. destruct (f !! k) as [[dqa va]|] eqn:Hf; rewrite Hf; last done.
    specialize (Hrel _ _ Hf) as (v & dq & Hmval & Hvval & Hvincl). simpl in *.
    eapply cmra_validN_includedN. 2:done. done.
  Qed.
(* iris/algebra/lib/gmap_view.v *)
Lemma gmap_view_rel_raw_unit n :
    ∃ m, gmap_view_rel_raw n m ε.
Proof. exists ∅. apply: map_Forall_empty. Qed.
(* iris/algebra/lib/gmap_view.v *)
Canonical gmap_view_rel :
      view_rel (gmapO K V) (gmap_view_fragUR K V)
:=
    ViewRel gmap_view_rel_raw gmap_view_rel_raw_mono
            gmap_view_rel_raw_valid gmap_view_rel_raw_unit.
(* iris/algebra/lib/gmap_view.v *)
Lemma gmap_view_rel_exists n f :
    (∃ m, gmap_view_rel n m f) ↔ ✓{n} f.
Proof.
    split.
    { intros [m Hrel]. eapply gmap_view_rel_raw_valid, Hrel. }
    intros Hf.
    cut (∃ m, gmap_view_rel n m f ∧ ∀ k, f !! k = None → m !! k = None).
    { naive_solver. }
    induction f as [|k [dq v] f Hk' IH] using map_ind.
    { exists ∅. split; [|done]. apply: map_Forall_empty. }
    move: (Hf k). rewrite lookup_insert_eq=> -[/= ??].
    destruct IH as (m & Hm & Hdom).
    { intros k'. destruct (decide (k = k')) as [->|?]; [by rewrite Hk'|].
      move: (Hf k'). by rewrite lookup_insert_ne. }
    exists (<[k:=v]> m).
    rewrite /gmap_view_rel /= /gmap_view_rel_raw map_Forall_insert //=. split_and!.
    - exists v, dq. split; first by rewrite lookup_insert_eq.
      split; first by split. done.
    - eapply map_Forall_impl; [apply Hm|]; simpl.
      intros k' [dq' ag'] (v'&?&?&?). exists v'.
      rewrite lookup_insert_ne; naive_solver.
    - intros k'. rewrite !lookup_insert_None. naive_solver.
  Qed.
(* iris/algebra/lib/gmap_view.v *)
Lemma gmap_view_rel_unit n m : gmap_view_rel n m ε.
Proof. apply: map_Forall_empty. Qed.
(* iris/algebra/lib/gmap_view.v *)
Lemma gmap_view_rel_discrete :
    CmraDiscrete V → ViewRelDiscrete gmap_view_rel.
Proof.
    intros ? n m f Hrel k [df va] Hk.
    destruct (Hrel _ _ Hk) as (v & dq & Hm & Hvval & Hvincl).
    exists v, dq. split; first done.
    split; first by apply cmra_discrete_valid_iff_0.
    rewrite -cmra_discrete_included_iff_0. done.
  Qed.
(* iris/algebra/lib/gmap_view.v *)
(* [gmap_view] is a notation to give canonical structure search the chance
to infer the right instances (see [auth]). *)
Notation gmap_view K V
:= (view (gmap_view_rel_raw K V)).
(* iris/algebra/lib/gmap_view.v *)
Definition gmap_viewO {SI : sidx} (K : Type) `{Countable K} (V : cmra) : ofe
:=
  viewO (gmap_view_rel K V).
(* iris/algebra/lib/gmap_view.v *)
Definition gmap_viewR {SI : sidx} (K : Type) `{Countable K} (V : cmra) : cmra
:=
  viewR (gmap_view_rel K V).
(* iris/algebra/lib/gmap_view.v *)
Definition gmap_viewUR {SI : sidx} (K : Type) `{Countable K} (V : cmra) : ucmra
:=
  viewUR (gmap_view_rel K V).
(* iris/algebra/lib/gmap_view.v *)
Definition gmap_view_auth (dq : dfrac) (m : gmap K V) : gmap_viewR K V
:=
    ●V{dq} m.
(* iris/algebra/lib/gmap_view.v *)
Definition gmap_view_frag (k : K) (dq : dfrac) (v : V) : gmap_viewR K V
:=
    ◯V {[k := (dq, v)]}.
(* iris/algebra/lib/gmap_view.v *)
Instance : Params (@gmap_view_auth) 6
:= {}.
(* iris/algebra/lib/gmap_view.v *)
Instance gmap_view_auth_ne dq :
    NonExpansive (gmap_view_auth (K:=K) (V:=V) dq).
Proof. solve_proper. Qed.
(* iris/algebra/lib/gmap_view.v *)
Instance gmap_view_auth_proper dq :
    Proper ((≡) ==> (≡)) (gmap_view_auth (K:=K) (V:=V) dq).
Proof. apply ne_proper, _. Qed.
(* iris/algebra/lib/gmap_view.v *)
Instance : Params (@gmap_view_frag) 7
:= {}.
(* iris/algebra/lib/gmap_view.v *)
Instance gmap_view_frag_ne k oq : NonExpansive (gmap_view_frag (V:=V) k oq).
Proof. solve_proper. Qed.
(* iris/algebra/lib/gmap_view.v *)
Instance gmap_view_frag_proper k oq :
    Proper ((≡) ==> (≡)) (gmap_view_frag (V:=V) k oq).
Proof. apply ne_proper, _. Qed.
(* iris/algebra/lib/gmap_view.v *)
(* Helper lemmas *)
Lemma gmap_view_rel_lookup n m k dq v :
    gmap_view_rel K V n m {[k := (dq, v)]} ↔
    ∃ v' dq', m !! k = Some v' ∧ ✓{n} (dq', v') ∧ Some (dq, v) ≼{n} Some (dq', v').
Proof.
    split.
    - intros Hrel.
      edestruct (Hrel k) as (v' & dq' & Hlookup & Hval & Hinc).
      { rewrite lookup_singleton_eq. done. }
      simpl in *. eexists _, _. split_and!; done.
    - intros (v' & dq' & Hlookup & Hval & ?) j [df va].
      destruct (decide (k = j)) as [<-|Hne]; last by rewrite lookup_singleton_ne.
      rewrite lookup_singleton_eq. intros [= <- <-]. simpl.
      exists v', dq'. split_and!; by rewrite ?Hv'.
  Qed.
(* iris/algebra/lib/gmap_view.v *)
(* Composition and validity *)
Lemma gmap_view_auth_dfrac_op dp dq m :
    gmap_view_auth (dp ⋅ dq) m ≡
    gmap_view_auth dp m ⋅ gmap_view_auth dq m.
Proof. by rewrite /gmap_view_auth view_auth_dfrac_op. Qed.
(* iris/algebra/lib/gmap_view.v *)
Instance gmap_view_auth_dfrac_is_op dq dq1 dq2 m :
    IsOp dq dq1 dq2 →
    IsOp' (gmap_view_auth dq m) (gmap_view_auth dq1 m) (gmap_view_auth dq2 m).
Proof. rewrite /gmap_view_auth. apply _. Qed.
(* iris/algebra/lib/gmap_view.v *)
Lemma gmap_view_auth_dfrac_op_invN n dp m1 dq m2 :
    ✓{n} (gmap_view_auth dp m1 ⋅ gmap_view_auth dq m2) → m1 ≡{n}≡ m2.
Proof. apply view_auth_dfrac_op_invN. Qed.
(* iris/algebra/lib/gmap_view.v *)
Lemma gmap_view_auth_dfrac_op_inv dp m1 dq m2 :
    ✓ (gmap_view_auth dp m1 ⋅ gmap_view_auth dq m2) → m1 ≡ m2.
Proof. apply view_auth_dfrac_op_inv. Qed.
(* iris/algebra/lib/gmap_view.v *)
Lemma gmap_view_auth_dfrac_validN m n dq : ✓{n} gmap_view_auth dq m ↔ ✓ dq.
Proof.
    rewrite view_auth_dfrac_validN. intuition eauto using gmap_view_rel_unit.
  Qed.
(* iris/algebra/lib/gmap_view.v *)
Lemma gmap_view_auth_dfrac_valid m dq : ✓ gmap_view_auth dq m ↔ ✓ dq.
Proof.
    rewrite view_auth_dfrac_valid. intuition eauto using gmap_view_rel_unit.
  Qed.
(* iris/algebra/lib/gmap_view.v *)
Lemma gmap_view_auth_valid m : ✓ gmap_view_auth (DfracOwn 1) m.
Proof. rewrite gmap_view_auth_dfrac_valid. done. Qed.
(* iris/algebra/lib/gmap_view.v *)
Lemma gmap_view_auth_dfrac_op_validN n dq1 dq2 m1 m2 :
    ✓{n} (gmap_view_auth dq1 m1 ⋅ gmap_view_auth dq2 m2) ↔ ✓ (dq1 ⋅ dq2) ∧ m1 ≡{n}≡ m2.
Proof.
    rewrite view_auth_dfrac_op_validN. intuition eauto using gmap_view_rel_unit.
  Qed.
(* iris/algebra/lib/gmap_view.v *)
Lemma gmap_view_auth_dfrac_op_valid dq1 dq2 m1 m2 :
    ✓ (gmap_view_auth dq1 m1 ⋅ gmap_view_auth dq2 m2) ↔ ✓ (dq1 ⋅ dq2) ∧ m1 ≡ m2.
Proof.
    rewrite view_auth_dfrac_op_valid. intuition eauto using gmap_view_rel_unit.
  Qed.
(* iris/algebra/lib/gmap_view.v *)
Lemma gmap_view_auth_op_validN n m1 m2 :
    ✓{n} (gmap_view_auth (DfracOwn 1) m1 ⋅ gmap_view_auth (DfracOwn 1) m2) ↔ False.
Proof. apply view_auth_op_validN. Qed.
(* iris/algebra/lib/gmap_view.v *)
Lemma gmap_view_auth_op_valid m1 m2 :
    ✓ (gmap_view_auth (DfracOwn 1) m1 ⋅ gmap_view_auth (DfracOwn 1) m2) ↔ False.
Proof. apply view_auth_op_valid. Qed.
(* iris/algebra/lib/gmap_view.v *)
Lemma gmap_view_frag_validN n k dq v : ✓{n} gmap_view_frag k dq v ↔ ✓ dq ∧ ✓{n} v.
Proof.
    rewrite view_frag_validN gmap_view_rel_exists singleton_validN pair_validN.
    naive_solver.
  Qed.
(* iris/algebra/lib/gmap_view.v *)
Lemma gmap_view_frag_valid k dq v : ✓ gmap_view_frag k dq v ↔ ✓ dq ∧ ✓ v.
Proof.
    rewrite cmra_valid_validN. setoid_rewrite gmap_view_frag_validN.
    rewrite [_ v]cmra_valid_validN. pose 0ᵢ. naive_solver.
  Qed.
(* iris/algebra/lib/gmap_view.v *)
Lemma gmap_view_frag_op k dq1 dq2 v1 v2 :
    gmap_view_frag k (dq1 ⋅ dq2) (v1 ⋅ v2) ≡
      gmap_view_frag k dq1 v1 ⋅ gmap_view_frag k dq2 v2.
Proof. rewrite -view_frag_op singleton_op -pair_op //. Qed.
(* iris/algebra/lib/gmap_view.v *)
Lemma gmap_view_frag_add k q1 q2 v1 v2 :
    gmap_view_frag k (DfracOwn (q1 + q2)) (v1 ⋅ v2) ≡
      gmap_view_frag k (DfracOwn q1) v1 ⋅ gmap_view_frag k (DfracOwn q2) v2.
Proof. rewrite -gmap_view_frag_op. done. Qed.
(* iris/algebra/lib/gmap_view.v *)
Lemma gmap_view_frag_op_validN n k dq1 dq2 v1 v2 :
    ✓{n} (gmap_view_frag k dq1 v1 ⋅ gmap_view_frag k dq2 v2) ↔
      ✓ (dq1 ⋅ dq2) ∧ ✓{n} (v1 ⋅ v2).
Proof.
    rewrite view_frag_validN gmap_view_rel_exists singleton_op singleton_validN.
    by rewrite -pair_op pair_validN.
  Qed.
(* iris/algebra/lib/gmap_view.v *)
Lemma gmap_view_frag_op_valid k dq1 dq2 v1 v2 :
    ✓ (gmap_view_frag k dq1 v1 ⋅ gmap_view_frag k dq2 v2) ↔
      ✓ (dq1 ⋅ dq2) ∧ ✓ (v1 ⋅ v2).
Proof.
    rewrite view_frag_valid. setoid_rewrite gmap_view_rel_exists.
    rewrite -cmra_valid_validN singleton_op singleton_valid.
    by rewrite -pair_op pair_valid.
  Qed.
(* iris/algebra/lib/gmap_view.v *)
Lemma gmap_view_both_dfrac_validN n dp m k dq v :
    ✓{n} (gmap_view_auth dp m ⋅ gmap_view_frag k dq v) ↔
      ∃ v' dq', ✓ dp ∧ m !! k = Some v' ∧ ✓{n} (dq', v') ∧
                Some (dq, v) ≼{n} Some (dq', v').
Proof.
    rewrite /gmap_view_auth /gmap_view_frag.
    rewrite view_both_dfrac_validN gmap_view_rel_lookup. naive_solver.
  Qed.
(* iris/algebra/lib/gmap_view.v *)
Lemma gmap_view_both_validN n dp m k v :
    ✓{n} (gmap_view_auth dp m ⋅ gmap_view_frag k (DfracOwn 1) v) ↔
      ✓ dp ∧ ✓{n} v ∧ m !! k ≡{n}≡ Some v.
Proof.
    rewrite gmap_view_both_dfrac_validN. split.
    - intros [Hdp (v' & dq' & Hlookup & Hvalid & Hincl)].
      split; first done. rewrite Hlookup.
      destruct (Some_includedN_exclusive _ _ _ Hincl Hvalid) as [_ Heq].
      simpl in Heq. split.
      + rewrite pair_validN in Hvalid. rewrite Heq. naive_solver.
      + by rewrite Heq.
    - intros (Hdp & Hval & Hlookup).
      apply dist_Some_inv_r' in Hlookup as [v' [Hlookup Heq]].
      exists v', (DfracOwn 1). do 2 (split; [done|]). split.
      + rewrite pair_validN. by rewrite -Heq.
      + by apply: Some_includedN_refl.
  Qed.
(* iris/algebra/lib/gmap_view.v *)
(* The backwards direction here does not hold: if [dq = DfracOwn 1] but
  [v ≠ v'], we have to find a suitable erased fraction [dq'] to satisfy the view
  relation, but there is no way to satisfy [Some (DfracOwn 1, v) ≼{n} Some (dq', v')]
  for any [dq']. The "if and only if" version of this lemma would have to
  involve some extra condition like [dq = DfracOwn 1 → v = v'], or phrased
  more like the view relation itself: [∃ dq', ✓ dq' ∧ Some (v, dq) ≼{n} Some (v', dq')]. *)
Lemma gmap_view_both_dfrac_validN_total `{!CmraTotal V} n dp m k dq v :
    ✓{n} (gmap_view_auth dp m ⋅ gmap_view_frag k dq v) →
    ∃ v', ✓ dp ∧ ✓ dq ∧ m !! k = Some v' ∧ ✓{n} v' ∧ v ≼{n} v'.
Proof.
    rewrite gmap_view_both_dfrac_validN.
    intros (v' & dq' & Hdp & Hlookup & Hvalid & Hincl).
    exists v'. split; first done. split.
    - eapply (cmra_valid_Some_included dq'); first by apply Hvalid.
      eapply cmra_discrete_included_iff.
      eapply Some_pair_includedN_l. done.
    - split; first done. split; first apply Hvalid.
      move:Hincl=> /Some_pair_includedN_r /Some_includedN_total. done.
  Qed.
(* iris/algebra/lib/gmap_view.v *)
(* Without [CmraDiscrete], we cannot do much better than [∀ n, <same as above>].
  This is because both the [dq'] and the witness for the [≼{n}] can be different for
  each step-index. It is totally possible that at low step-indices, [v] has a frame
  (and [dq' > dq]) while at higher step-indices, [v] has no frame (and [dq' = dq]). *)
Lemma gmap_view_both_dfrac_valid_discrete `{!CmraDiscrete V} dp m k dq v :
    ✓ (gmap_view_auth dp m ⋅ gmap_view_frag k dq v) ↔
      ∃ v' dq', ✓ dp ∧ m !! k = Some v' ∧
                ✓ (dq', v') ∧
                Some (dq, v) ≼ Some (dq', v').
Proof.
    rewrite cmra_valid_validN. setoid_rewrite gmap_view_both_dfrac_validN. split.
    - intros Hvalid. specialize (Hvalid 0ᵢ).
      destruct Hvalid as (v' & dq' & Hdp & Hlookup & Hvalid & Hincl).
      exists v', dq'. do 2 (split; first done).
      split; first by apply cmra_discrete_valid.
      by apply: cmra_discrete_included_r.
    - intros (v' & dq' & Hdp & Hlookup & Hvalid & Hincl) n.
      exists v', dq'. do 2 (split; first done).
      split; first by apply cmra_valid_validN.
      by apply: cmra_included_includedN.
  Qed.
(* iris/algebra/lib/gmap_view.v *)
(* The backwards direction here does not hold: if [dq = DfracOwn 1] but
  [v ≠ v'], we have to find a suitable erased fraction [dq'] to satisfy the view
  relation, but there is no way to satisfy [Some (DfracOwn 1, v) ≼ Some (dq', v')]
  for any [dq']. The "if and only if" version of this lemma would have to
  involve some extra condition like [dq = DfracOwn 1 → v = v'], or phrased
  more like the view relation itself: [∃ dq', ✓ dq' ∧ Some (v, dq) ≼ Some (v', dq')]. *)
Lemma gmap_view_both_dfrac_valid_discrete_total
      `{!CmraDiscrete V, !CmraTotal V} dp m k dq v :
    ✓ (gmap_view_auth dp m ⋅ gmap_view_frag k dq v) →
    ∃ v', ✓ dp ∧ ✓ dq ∧ m !! k = Some v' ∧ ✓ v' ∧ v ≼ v'.
Proof.
    rewrite gmap_view_both_dfrac_valid_discrete.
    intros (v' & dq' & Hdp & Hlookup & Hvalid & Hincl).
    exists v'. split; first done. split.
    - eapply (cmra_valid_Some_included dq'); first by apply Hvalid.
      eapply Some_pair_included_l. done.
    - split; first done. split; first apply Hvalid.
      move:Hincl=> /Some_pair_included_r /Some_included_total. done.
  Qed.
(* iris/algebra/lib/gmap_view.v *)
(* On the other hand, this one holds for all CMRAs, not just discrete ones. *)
Lemma gmap_view_both_valid m dp k v :
    ✓ (gmap_view_auth dp m ⋅ gmap_view_frag k (DfracOwn 1) v) ↔
    ✓ dp ∧ ✓ v ∧ m !! k ≡ Some v.
Proof.
    rewrite cmra_valid_validN. setoid_rewrite gmap_view_both_validN. split.
    - intros Hvalid. split; last split.
      + eapply (Hvalid 0ᵢ).
      + apply cmra_valid_validN. intros n. eapply Hvalid.
      + apply equiv_dist. intros n. eapply Hvalid.
    - intros (Hdp & Hval & Hlookup). intros n.
      split; first done. split.
      + apply cmra_valid_validN. done.
      + rewrite Hlookup. done.
  Qed.
(* iris/algebra/lib/gmap_view.v *)
(* Frame-preserving updates *)
Lemma gmap_view_alloc m k dq v :
    m !! k = None →
    ✓ dq →
    ✓ v →
    gmap_view_auth (DfracOwn 1) m ~~>
      gmap_view_auth (DfracOwn 1) (<[k := v]> m) ⋅ gmap_view_frag k dq v.
Proof.
    intros Hfresh Hdq Hval. apply view_update_alloc=>n bf Hrel j [df va] /=.
    rewrite lookup_op. destruct (decide (j = k)) as [->|Hne].
    - assert (bf !! k = None) as Hbf.
      { destruct (bf !! k) as [[df' va']|] eqn:Hbf; last done.
        specialize (Hrel _ _ Hbf). destruct Hrel as (v' & dq' & Hm & _).
        exfalso. rewrite Hm in Hfresh. done. }
      rewrite lookup_singleton_eq Hbf right_id.
      intros [= <- <-]. eexists _, _.
      rewrite lookup_insert_eq. split; first done.
      split; last by apply: Some_includedN_refl.
      split; first done. by eapply cmra_valid_validN.
    - rewrite lookup_singleton_ne; last done.
      rewrite left_id=>Hbf.
      specialize (Hrel _ _ Hbf). destruct Hrel as (v' & ? & Hm & ?).
      eexists _, _. split; last done.
      rewrite lookup_insert_ne //.
  Qed.
(* iris/algebra/lib/gmap_view.v *)
Lemma gmap_view_alloc_big m m' dq :
    m' ##ₘ m →
    ✓ dq →
    map_Forall (λ k v, ✓ v) m' →
    gmap_view_auth (DfracOwn 1) m ~~>
      gmap_view_auth (DfracOwn 1) (m' ∪ m) ⋅
      ([^op map] k↦v ∈ m', gmap_view_frag k dq v).
Proof.
    intros ?? Hm'.
    induction m' as [|k v m' ? IH] using map_ind; decompose_map_disjoint.
    { rewrite big_opM_empty left_id_L right_id. done. }
    apply map_Forall_insert in Hm' as [??]; last done.
    rewrite IH //. rewrite big_opM_insert // assoc.
    apply cmra_update_op; last done.
    rewrite -insert_union_l. apply (gmap_view_alloc _ k dq); [|done..].
    by apply lookup_union_None.
  Qed.
(* iris/algebra/lib/gmap_view.v *)
Lemma gmap_view_delete m k v :
    gmap_view_auth (DfracOwn 1) m ⋅ gmap_view_frag k (DfracOwn 1) v ~~>
    gmap_view_auth (DfracOwn 1) (delete k m).
Proof.
    apply view_update_dealloc=>n bf Hrel j [df va] Hbf /=.
    destruct (decide (j = k)) as [->|Hne].
    - edestruct (Hrel k) as (v' & dq' & ? & Hval & Hincl).
      { rewrite lookup_op Hbf lookup_singleton_eq -Some_op. done. }
      eapply (cmra_validN_Some_includedN _ _ _ Hval) in Hincl as Hval'.
      exfalso. clear Hval Hincl.
      rewrite pair_validN /= in Hval'.
      apply: dfrac_full_exclusive. apply Hval'.
    - edestruct (Hrel j) as (v' & ? & ? & ?).
      { rewrite lookup_op lookup_singleton_ne // Hbf. done. }
      eexists v', _. split; last done.
      rewrite lookup_delete_ne //.
  Qed.
(* iris/algebra/lib/gmap_view.v *)
Lemma gmap_view_delete_big m m' :
    gmap_view_auth (DfracOwn 1) m ⋅
    ([^op map] k↦v ∈ m', gmap_view_frag k (DfracOwn 1) v) ~~>
      gmap_view_auth (DfracOwn 1) (m ∖ m').
Proof.
    induction m' as [|k v m' ? IH] using map_ind.
    { rewrite right_id_L big_opM_empty right_id //. }
    rewrite big_opM_insert //.
    rewrite [gmap_view_frag _ _ _ ⋅ _]comm assoc IH gmap_view_delete.
    rewrite -delete_difference. done.
  Qed.