upred.v 31.4 KB
Newer Older
Robbert Krebbers's avatar
Robbert Krebbers committed
1
From iris.algebra Require Export cmra updates.
Robbert Krebbers's avatar
Robbert Krebbers committed
2
From iris.bi Require Export derived_connectives updates plainly.
3
From stdpp Require Import finite.
4
Set Default Proof Using "Type".
Robbert Krebbers's avatar
Robbert Krebbers committed
5 6 7
Local Hint Extern 1 (_  _) => etrans; [eassumption|].
Local Hint Extern 1 (_  _) => etrans; [|eassumption].
Local Hint Extern 10 (_  _) => omega.
8

Ralf Jung's avatar
Ralf Jung committed
9 10 11 12 13
(** The basic definition of the uPred type, its metric and functor laws.
    You probably do not want to import this file. Instead, import
    base_logic.base_logic; that will also give you all the primitive
    and many derived laws for the logic. *)

Ralf Jung's avatar
Ralf Jung committed
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
(* A good way of understanding this definition of the uPred OFE is to
   consider the OFE uPred0 of monotonous SProp predicates. That is,
   uPred0 is the OFE of non-expansive functions from M to SProp that
   are monotonous with respect to CMRA inclusion. This notion of
   monotonicity has to be stated in the SProp logic. Together with the
   usual closedness property of SProp, this gives exactly uPred_mono.

   Then, we quotient uPred0 *in the sProp logic* with respect to
   equivalence on valid elements of M. That is, we quotient with
   respect to the following *sProp* equivalence relation:
     P1 ≡ P2 := ∀ x, ✓ x → (P1(x) ↔ P2(x))       (1)
   When seen from the ambiant logic, obtaining this quotient requires
   definig both a custom Equiv and Dist.


   It is worth noting that this equivalence relation admits canonical
   representatives. More precisely, one can show that every
   equivalence class contains exactly one element P0 such that:
Ralf Jung's avatar
Ralf Jung committed
32 33 34 35 36
     ∀ x, (✓ x → P0(x)) → P0(x)                 (2)
   (Again, this assertion has to be understood in sProp). Intuitively,
   this says that P0 trivially holds whenever the resource is invalid.
   Starting from any element P, one can find this canonical
   representative by choosing:
Ralf Jung's avatar
Ralf Jung committed
37 38 39 40 41 42 43 44 45 46 47 48
     P0(x) := ✓ x → P(x)                        (3)

   Hence, as an alternative definition of uPred, we could use the set
   of canonical representatives (i.e., the subtype of monotonous
   sProp predicates that verify (2)). This alternative definition would
   save us from using a quotient. However, the definitions of the various
   connectives would get more complicated, because we have to make sure
   they all verify (2), which sometimes requires some adjustments. We
   would moreover need to prove one more property for every logical
   connective.
 *)

49 50
Record uPred (M : ucmraT) : Type := IProp {
  uPred_holds :> nat  M  Prop;
51

52 53
  uPred_mono n1 n2 x1 x2 :
    uPred_holds n1 x1  x1 {n1} x2  n2  n1  uPred_holds n2 x2
54 55 56 57 58
}.
Arguments uPred_holds {_} _ _ _ : simpl never.
Add Printing Constructor uPred.
Instance: Params (@uPred_holds) 3.

Robbert Krebbers's avatar
Robbert Krebbers committed
59
Bind Scope bi_scope with uPred.
60 61 62 63 64 65 66 67 68 69 70
Arguments uPred_holds {_} _%I _ _.

Section cofe.
  Context {M : ucmraT}.

  Inductive uPred_equiv' (P Q : uPred M) : Prop :=
    { uPred_in_equiv :  n x, {n} x  P n x  Q n x }.
  Instance uPred_equiv : Equiv (uPred M) := uPred_equiv'.
  Inductive uPred_dist' (n : nat) (P Q : uPred M) : Prop :=
    { uPred_in_dist :  n' x, n'  n  {n'} x  P n' x  Q n' x }.
  Instance uPred_dist : Dist (uPred M) := uPred_dist'.
71
  Definition uPred_ofe_mixin : OfeMixin (uPred M).
72 73 74 75 76 77 78 79 80 81 82 83
  Proof.
    split.
    - intros P Q; split.
      + by intros HPQ n; split=> i x ??; apply HPQ.
      + intros HPQ; split=> n x ?; apply HPQ with n; auto.
    - intros n; split.
      + by intros P; split=> x i.
      + by intros P Q HPQ; split=> x i ??; symmetry; apply HPQ.
      + intros P Q Q' HP HQ; split=> i x ??.
        by trans (Q i x);[apply HP|apply HQ].
    - intros n P Q HPQ; split=> i x ??; apply HPQ; auto.
  Qed.
84 85 86
  Canonical Structure uPredC : ofeT := OfeT (uPred M) uPred_ofe_mixin.

  Program Definition uPred_compl : Compl uPredC := λ c,
87
    {| uPred_holds n x :=  n', n'  n  {n'}x  c n' n' x |}.
88
  Next Obligation.
89 90 91
    move=> /= c n1 n2 x1 x2 HP Hx12 Hn12 n3 Hn23 Hv. eapply uPred_mono.
    eapply HP, cmra_validN_includedN, cmra_includedN_le=>//; lia.
    eapply cmra_includedN_le=>//; lia. done.
92 93 94
  Qed.
  Global Program Instance uPred_cofe : Cofe uPredC := {| compl := uPred_compl |}.
  Next Obligation.
95 96
    intros n c; split=>i x Hin Hv.
    etrans; [|by symmetry; apply (chain_cauchy c i n)]. split=>H; [by apply H|].
97
    repeat intro. apply (chain_cauchy c n' i)=>//. by eapply uPred_mono.
98
  Qed.
99 100 101 102 103 104 105 106 107 108 109 110 111
End cofe.
Arguments uPredC : clear implicits.

Instance uPred_ne {M} (P : uPred M) n : Proper (dist n ==> iff) (P n).
Proof.
  intros x1 x2 Hx; split=> ?; eapply uPred_mono; eauto; by rewrite Hx.
Qed.
Instance uPred_proper {M} (P : uPred M) n : Proper (() ==> iff) (P n).
Proof. by intros x1 x2 Hx; apply uPred_ne, equiv_dist. Qed.

Lemma uPred_holds_ne {M} (P Q : uPred M) n1 n2 x :
  P {n2} Q  n2  n1  {n2} x  Q n1 x  P n2 x.
Proof.
112
  intros [Hne] ???. eapply Hne; try done. eauto using uPred_mono, cmra_validN_le.
113 114
Qed.

115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
(* Equivalence to the definition of uPred in the appendix. *)
Lemma uPred_alt {M : ucmraT} (P: nat  M  Prop) :
  ( n1 n2 x1 x2, P n1 x1  x1 {n1} x2  n2  n1  P n2 x2) 
  ( ( x n1 n2, n2  n1  P n1 x  P n2 x) (* Pointwise down-closed *)
   ( n x1 x2, x1 {n} x2   m, m  n  P m x1  P m x2) (* Non-expansive *)
   ( n x1 x2, x1 {n} x2   m, m  n  P m x1  P m x2) (* Monotonicity *)
  ).
Proof.
  (* Provide this lemma to eauto. *)
  assert ( n1 n2 (x1 x2 : M), n2  n1  x1 {n1} x2  x1 {n2} x2).
  { intros ????? H. eapply cmra_includedN_le; last done. by rewrite H. }
  (* Now go ahead. *)
  split.
  - intros Hupred. repeat split; eauto using cmra_includedN_le.
  - intros (Hdown & _ & Hmono) **. eapply Hmono; [done..|]. eapply Hdown; done.
130 131 132 133
Qed.

(** functor *)
Program Definition uPred_map {M1 M2 : ucmraT} (f : M2 -n> M1)
134
  `{!CmraMorphism f} (P : uPred M1) :
135
  uPred M2 := {| uPred_holds n x := P n (f x) |}.
136
Next Obligation. naive_solver eauto using uPred_mono, cmra_morphism_monotoneN. Qed.
137 138

Instance uPred_map_ne {M1 M2 : ucmraT} (f : M2 -n> M1)
139
  `{!CmraMorphism f} n : Proper (dist n ==> dist n) (uPred_map f).
140 141
Proof.
  intros x1 x2 Hx; split=> n' y ??.
142
  split; apply Hx; auto using cmra_morphism_validN.
143 144 145 146
Qed.
Lemma uPred_map_id {M : ucmraT} (P : uPred M): uPred_map cid P  P.
Proof. by split=> n x ?. Qed.
Lemma uPred_map_compose {M1 M2 M3 : ucmraT} (f : M1 -n> M2) (g : M2 -n> M3)
147
    `{!CmraMorphism f, !CmraMorphism g} (P : uPred M3):
148 149 150
  uPred_map (g  f) P  uPred_map f (uPred_map g P).
Proof. by split=> n x Hx. Qed.
Lemma uPred_map_ext {M1 M2 : ucmraT} (f g : M1 -n> M2)
151
      `{!CmraMorphism f} `{!CmraMorphism g}:
152 153
  ( x, f x  g x)   x, uPred_map f x  uPred_map g x.
Proof. intros Hf P; split=> n x Hx /=; by rewrite /uPred_holds /= Hf. Qed.
154
Definition uPredC_map {M1 M2 : ucmraT} (f : M2 -n> M1) `{!CmraMorphism f} :
155 156
  uPredC M1 -n> uPredC M2 := CofeMor (uPred_map f : uPredC M1  uPredC M2).
Lemma uPredC_map_ne {M1 M2 : ucmraT} (f g : M2 -n> M1)
157
    `{!CmraMorphism f, !CmraMorphism g} n :
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
  f {n} g  uPredC_map f {n} uPredC_map g.
Proof.
  by intros Hfg P; split=> n' y ??;
    rewrite /uPred_holds /= (dist_le _ _ _ _(Hfg y)); last lia.
Qed.

Program Definition uPredCF (F : urFunctor) : cFunctor := {|
  cFunctor_car A B := uPredC (urFunctor_car F B A);
  cFunctor_map A1 A2 B1 B2 fg := uPredC_map (urFunctor_map F (fg.2, fg.1))
|}.
Next Obligation.
  intros F A1 A2 B1 B2 n P Q HPQ.
  apply uPredC_map_ne, urFunctor_ne; split; by apply HPQ.
Qed.
Next Obligation.
  intros F A B P; simpl. rewrite -{2}(uPred_map_id P).
  apply uPred_map_ext=>y. by rewrite urFunctor_id.
Qed.
Next Obligation.
  intros F A1 A2 A3 B1 B2 B3 f g f' g' P; simpl. rewrite -uPred_map_compose.
  apply uPred_map_ext=>y; apply urFunctor_compose.
Qed.

Instance uPredCF_contractive F :
  urFunctorContractive F  cFunctorContractive (uPredCF F).
Proof.
184 185
  intros ? A1 A2 B1 B2 n P Q HPQ. apply uPredC_map_ne, urFunctor_contractive.
  destruct n; split; by apply HPQ.
186 187 188 189 190
Qed.

(** logical entailement *)
Inductive uPred_entails {M} (P Q : uPred M) : Prop :=
  { uPred_in_entails :  n x, {n} x  P n x  Q n x }.
191
Hint Resolve uPred_mono : uPred_def.
192

Robbert Krebbers's avatar
Robbert Krebbers committed
193 194 195 196 197
(** logical connectives *)
Program Definition uPred_pure_def {M} (φ : Prop) : uPred M :=
  {| uPred_holds n x := φ |}.
Solve Obligations with done.
Definition uPred_pure_aux : seal (@uPred_pure_def). by eexists. Qed.
198
Definition uPred_pure {M} := uPred_pure_aux.(unseal) M.
Robbert Krebbers's avatar
Robbert Krebbers committed
199
Definition uPred_pure_eq :
200
  @uPred_pure = @uPred_pure_def := uPred_pure_aux.(seal_eq).
Robbert Krebbers's avatar
Robbert Krebbers committed
201 202 203 204 205 206 207

Definition uPred_emp {M} : uPred M := uPred_pure True.

Program Definition uPred_and_def {M} (P Q : uPred M) : uPred M :=
  {| uPred_holds n x := P n x  Q n x |}.
Solve Obligations with naive_solver eauto 2 with uPred_def.
Definition uPred_and_aux : seal (@uPred_and_def). by eexists. Qed.
208 209
Definition uPred_and {M} := uPred_and_aux.(unseal) M.
Definition uPred_and_eq: @uPred_and = @uPred_and_def := uPred_and_aux.(seal_eq).
Robbert Krebbers's avatar
Robbert Krebbers committed
210 211 212 213 214

Program Definition uPred_or_def {M} (P Q : uPred M) : uPred M :=
  {| uPred_holds n x := P n x  Q n x |}.
Solve Obligations with naive_solver eauto 2 with uPred_def.
Definition uPred_or_aux : seal (@uPred_or_def). by eexists. Qed.
215 216
Definition uPred_or {M} := uPred_or_aux.(unseal) M.
Definition uPred_or_eq: @uPred_or = @uPred_or_def := uPred_or_aux.(seal_eq).
Robbert Krebbers's avatar
Robbert Krebbers committed
217 218 219 220 221

Program Definition uPred_impl_def {M} (P Q : uPred M) : uPred M :=
  {| uPred_holds n x :=  n' x',
       x  x'  n'  n  {n'} x'  P n' x'  Q n' x' |}.
Next Obligation.
222
  intros M P Q n1 n1' x1 x1' HPQ [x2 Hx1'] Hn1 n2 x3 [x4 Hx3] ?; simpl in *.
Robbert Krebbers's avatar
Robbert Krebbers committed
223 224 225 226
  rewrite Hx3 (dist_le _ _ _ _ Hx1'); auto. intros ??.
  eapply HPQ; auto. exists (x2  x4); by rewrite assoc.
Qed.
Definition uPred_impl_aux : seal (@uPred_impl_def). by eexists. Qed.
227
Definition uPred_impl {M} := uPred_impl_aux.(unseal) M.
Robbert Krebbers's avatar
Robbert Krebbers committed
228
Definition uPred_impl_eq :
229
  @uPred_impl = @uPred_impl_def := uPred_impl_aux.(seal_eq).
Robbert Krebbers's avatar
Robbert Krebbers committed
230 231 232 233 234

Program Definition uPred_forall_def {M A} (Ψ : A  uPred M) : uPred M :=
  {| uPred_holds n x :=  a, Ψ a n x |}.
Solve Obligations with naive_solver eauto 2 with uPred_def.
Definition uPred_forall_aux : seal (@uPred_forall_def). by eexists. Qed.
235
Definition uPred_forall {M A} := uPred_forall_aux.(unseal) M A.
Robbert Krebbers's avatar
Robbert Krebbers committed
236
Definition uPred_forall_eq :
237
  @uPred_forall = @uPred_forall_def := uPred_forall_aux.(seal_eq).
Robbert Krebbers's avatar
Robbert Krebbers committed
238 239 240 241 242

Program Definition uPred_exist_def {M A} (Ψ : A  uPred M) : uPred M :=
  {| uPred_holds n x :=  a, Ψ a n x |}.
Solve Obligations with naive_solver eauto 2 with uPred_def.
Definition uPred_exist_aux : seal (@uPred_exist_def). by eexists. Qed.
243 244
Definition uPred_exist {M A} := uPred_exist_aux.(unseal) M A.
Definition uPred_exist_eq: @uPred_exist = @uPred_exist_def := uPred_exist_aux.(seal_eq).
Robbert Krebbers's avatar
Robbert Krebbers committed
245 246 247 248 249

Program Definition uPred_internal_eq_def {M} {A : ofeT} (a1 a2 : A) : uPred M :=
  {| uPred_holds n x := a1 {n} a2 |}.
Solve Obligations with naive_solver eauto 2 using (dist_le (A:=A)).
Definition uPred_internal_eq_aux : seal (@uPred_internal_eq_def). by eexists. Qed.
250
Definition uPred_internal_eq {M A} := uPred_internal_eq_aux.(unseal) M A.
Robbert Krebbers's avatar
Robbert Krebbers committed
251
Definition uPred_internal_eq_eq:
252
  @uPred_internal_eq = @uPred_internal_eq_def := uPred_internal_eq_aux.(seal_eq).
Robbert Krebbers's avatar
Robbert Krebbers committed
253 254 255 256

Program Definition uPred_sep_def {M} (P Q : uPred M) : uPred M :=
  {| uPred_holds n x :=  x1 x2, x {n} x1  x2  P n x1  Q n x2 |}.
Next Obligation.
257
  intros M P Q n1 n2 x y (x1&x2&Hx&?&?) [z Hy] Hn.
Robbert Krebbers's avatar
Robbert Krebbers committed
258
  exists x1, (x2  z); split_and?; eauto using uPred_mono, cmra_includedN_l.
259
  eapply dist_le, Hn. by rewrite Hy Hx assoc.
Robbert Krebbers's avatar
Robbert Krebbers committed
260 261
Qed.
Definition uPred_sep_aux : seal (@uPred_sep_def). by eexists. Qed.
262 263
Definition uPred_sep {M} := uPred_sep_aux.(unseal) M.
Definition uPred_sep_eq: @uPred_sep = @uPred_sep_def := uPred_sep_aux.(seal_eq).
Robbert Krebbers's avatar
Robbert Krebbers committed
264 265 266 267 268

Program Definition uPred_wand_def {M} (P Q : uPred M) : uPred M :=
  {| uPred_holds n x :=  n' x',
       n'  n  {n'} (x  x')  P n' x'  Q n' (x  x') |}.
Next Obligation.
269 270
  intros M P Q n1 n1' x1 x1' HPQ ? Hn n3 x3 ???; simpl in *.
  eapply uPred_mono with n3 (x1  x3);
Robbert Krebbers's avatar
Robbert Krebbers committed
271 272 273
    eauto using cmra_validN_includedN, cmra_monoN_r, cmra_includedN_le.
Qed.
Definition uPred_wand_aux : seal (@uPred_wand_def). by eexists. Qed.
274
Definition uPred_wand {M} := uPred_wand_aux.(unseal) M.
Robbert Krebbers's avatar
Robbert Krebbers committed
275
Definition uPred_wand_eq :
276
  @uPred_wand = @uPred_wand_def := uPred_wand_aux.(seal_eq).
Robbert Krebbers's avatar
Robbert Krebbers committed
277

278 279 280
(* Equivalently, this could be `∀ y, P n y`.  That's closer to the intuition
   of "embedding the step-indexed logic in Iris", but the two are equivalent
   because Iris is afine.  The following is easier to work with. *)
Robbert Krebbers's avatar
Robbert Krebbers committed
281
Program Definition uPred_plainly_def {M} : Plainly (uPred M) := λ P,
282
  {| uPred_holds n x := P n ε |}.
283
Solve Obligations with naive_solver eauto using uPred_mono, ucmra_unit_validN.
284 285 286 287
Definition uPred_plainly_aux : seal (@uPred_plainly_def). by eexists. Qed.
Definition uPred_plainly {M} := uPred_plainly_aux.(unseal) M.
Definition uPred_plainly_eq :
  @uPred_plainly = @uPred_plainly_def := uPred_plainly_aux.(seal_eq).
288

Robbert Krebbers's avatar
Robbert Krebbers committed
289 290 291 292 293 294
Program Definition uPred_persistently_def {M} (P : uPred M) : uPred M :=
  {| uPred_holds n x := P n (core x) |}.
Next Obligation.
  intros M; naive_solver eauto using uPred_mono, @cmra_core_monoN.
Qed.
Definition uPred_persistently_aux : seal (@uPred_persistently_def). by eexists. Qed.
295
Definition uPred_persistently {M} := uPred_persistently_aux.(unseal) M.
Robbert Krebbers's avatar
Robbert Krebbers committed
296
Definition uPred_persistently_eq :
297
  @uPred_persistently = @uPred_persistently_def := uPred_persistently_aux.(seal_eq).
Robbert Krebbers's avatar
Robbert Krebbers committed
298 299 300 301

Program Definition uPred_later_def {M} (P : uPred M) : uPred M :=
  {| uPred_holds n x := match n return _ with 0 => True | S n' => P n' x end |}.
Next Obligation.
302
  intros M P [|n1] [|n2] x1 x2; eauto using uPred_mono, cmra_includedN_S with lia.
Robbert Krebbers's avatar
Robbert Krebbers committed
303 304
Qed.
Definition uPred_later_aux : seal (@uPred_later_def). by eexists. Qed.
305
Definition uPred_later {M} := uPred_later_aux.(unseal) M.
Robbert Krebbers's avatar
Robbert Krebbers committed
306
Definition uPred_later_eq :
307
  @uPred_later = @uPred_later_def := uPred_later_aux.(seal_eq).
Robbert Krebbers's avatar
Robbert Krebbers committed
308 309 310 311

Program Definition uPred_ownM_def {M : ucmraT} (a : M) : uPred M :=
  {| uPred_holds n x := a {n} x |}.
Next Obligation.
312 313
  intros M a n1 n2 x1 x [a' Hx1] [x2 Hx] Hn. eapply cmra_includedN_le=>//.
  exists (a'  x2). by rewrite Hx(assoc op) Hx1.
Robbert Krebbers's avatar
Robbert Krebbers committed
314 315
Qed.
Definition uPred_ownM_aux : seal (@uPred_ownM_def). by eexists. Qed.
316
Definition uPred_ownM {M} := uPred_ownM_aux.(unseal) M.
Robbert Krebbers's avatar
Robbert Krebbers committed
317
Definition uPred_ownM_eq :
318
  @uPred_ownM = @uPred_ownM_def := uPred_ownM_aux.(seal_eq).
Robbert Krebbers's avatar
Robbert Krebbers committed
319 320 321 322 323

Program Definition uPred_cmra_valid_def {M} {A : cmraT} (a : A) : uPred M :=
  {| uPred_holds n x := {n} a |}.
Solve Obligations with naive_solver eauto 2 using cmra_validN_le.
Definition uPred_cmra_valid_aux : seal (@uPred_cmra_valid_def). by eexists. Qed.
324
Definition uPred_cmra_valid {M A} := uPred_cmra_valid_aux.(unseal) M A.
Robbert Krebbers's avatar
Robbert Krebbers committed
325
Definition uPred_cmra_valid_eq :
326
  @uPred_cmra_valid = @uPred_cmra_valid_def := uPred_cmra_valid_aux.(seal_eq).
Robbert Krebbers's avatar
Robbert Krebbers committed
327 328 329 330 331

Program Definition uPred_bupd_def {M} (Q : uPred M) : uPred M :=
  {| uPred_holds n x :=  k yf,
      k  n  {k} (x  yf)   x', {k} (x'  yf)  Q k x' |}.
Next Obligation.
332
  intros M Q n1 n2 x1 x2 HQ [x3 Hx] Hn k yf Hk.
Robbert Krebbers's avatar
Robbert Krebbers committed
333 334 335
  rewrite (dist_le _ _ _ _ Hx); last lia. intros Hxy.
  destruct (HQ k (x3  yf)) as (x'&?&?); [auto|by rewrite assoc|].
  exists (x'  x3); split; first by rewrite -assoc.
336
  eauto using uPred_mono, cmra_includedN_l.
Robbert Krebbers's avatar
Robbert Krebbers committed
337
Qed.
338
Definition uPred_bupd_aux {M} : seal (@uPred_bupd_def M). by eexists. Qed.
339
Definition uPred_bupd {M} : BUpd (uPred M) := uPred_bupd_aux.(unseal).
340
Definition uPred_bupd_eq {M} :
341
  @bupd _ uPred_bupd = @uPred_bupd_def M := uPred_bupd_aux.(seal_eq).
Robbert Krebbers's avatar
Robbert Krebbers committed
342 343 344 345 346

Module uPred_unseal.
Definition unseal_eqs :=
  (uPred_pure_eq, uPred_and_eq, uPred_or_eq, uPred_impl_eq, uPred_forall_eq,
  uPred_exist_eq, uPred_internal_eq_eq, uPred_sep_eq, uPred_wand_eq,
347
  uPred_plainly_eq, uPred_persistently_eq, uPred_later_eq, uPred_ownM_eq,
348
  uPred_cmra_valid_eq, @uPred_bupd_eq).
349
Ltac unseal := (* Coq unfold is used to circumvent bug #5699 in rewrite /foo *)
Robbert Krebbers's avatar
Robbert Krebbers committed
350
  unfold bi_emp; simpl; unfold sbi_emp; simpl;
351
  unfold uPred_emp, bi_pure, bi_and, bi_or, bi_impl, bi_forall, bi_exist,
Robbert Krebbers's avatar
Robbert Krebbers committed
352
  bi_sep, bi_wand, bi_persistently, sbi_internal_eq, sbi_later; simpl;
353
  unfold sbi_emp, sbi_pure, sbi_and, sbi_or, sbi_impl, sbi_forall, sbi_exist,
Robbert Krebbers's avatar
Robbert Krebbers committed
354
  sbi_internal_eq, sbi_sep, sbi_wand, sbi_persistently; simpl;
355
  unfold plainly, bi_plainly_plainly; simpl;
356
  rewrite !unseal_eqs /=.
Robbert Krebbers's avatar
Robbert Krebbers committed
357 358 359 360 361
End uPred_unseal.
Import uPred_unseal.

Local Arguments uPred_holds {_} !_ _ _ /.

362 363 364
Lemma uPred_bi_mixin (M : ucmraT) :
  BiMixin
    uPred_entails uPred_emp uPred_pure uPred_and uPred_or uPred_impl
Robbert Krebbers's avatar
Robbert Krebbers committed
365
    (@uPred_forall M) (@uPred_exist M) uPred_sep uPred_wand
366
    uPred_persistently.
Robbert Krebbers's avatar
Robbert Krebbers committed
367 368 369 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 396 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 423 424
Proof.
  split.
  - (* PreOrder uPred_entails *)
    split.
    + by intros P; split=> x i.
    + by intros P Q Q' HP HQ; split=> x i ??; apply HQ, HP.
  - (* (P ⊣⊢ Q) ↔ (P ⊢ Q) ∧ (Q ⊢ P) *)
    intros P Q. split.
    + intros HPQ; split; split=> x i; apply HPQ.
    + intros [HPQ HQP]; split=> x n; by split; [apply HPQ|apply HQP].
  - (* Proper (iff ==> dist n) (@uPred_pure M) *)
    intros φ1 φ2 Hφ. by unseal; split=> -[|n] ?; try apply Hφ.
  - (* NonExpansive2 uPred_and *)
    intros n P P' HP Q Q' HQ; unseal; split=> x n' ??.
    split; (intros [??]; split; [by apply HP|by apply HQ]).
  - (* NonExpansive2 uPred_or *)
    intros n P P' HP Q Q' HQ; split=> x n' ??.
    unseal; split; (intros [?|?]; [left; by apply HP|right; by apply HQ]).
  - (* NonExpansive2 uPred_impl *)
    intros n P P' HP Q Q' HQ; split=> x n' ??.
    unseal; split; intros HPQ x' n'' ????; apply HQ, HPQ, HP; auto.
  - (* Proper (pointwise_relation A (dist n) ==> dist n) uPred_forall *)
    by intros A n Ψ1 Ψ2 HΨ; unseal; split=> n' x; split; intros HP a; apply HΨ.
  - (* Proper (pointwise_relation A (dist n) ==> dist n) uPred_exist *)
    intros A n Ψ1 Ψ2 HΨ.
    unseal; split=> n' x ??; split; intros [a ?]; exists a; by apply HΨ.
  - (* NonExpansive2 uPred_sep *)
    intros n P P' HP Q Q' HQ; split=> n' x ??.
    unseal; split; intros (x1&x2&?&?&?); ofe_subst x;
      exists x1, x2; split_and!; try (apply HP || apply HQ);
      eauto using cmra_validN_op_l, cmra_validN_op_r.
  - (* NonExpansive2 uPred_wand *)
    intros n P P' HP Q Q' HQ; split=> n' x ??.
    unseal; split; intros HPQ x' n'' ???;
      apply HQ, HPQ, HP; eauto using cmra_validN_op_r.
  - (* NonExpansive uPred_persistently *)
    intros n P1 P2 HP.
    unseal; split=> n' x; split; apply HP; eauto using @cmra_core_validN.
  - (* φ → P ⊢ ⌜φ⌝ *)
    intros P φ ?. unseal; by split.
  - (* (φ → True ⊢ P) → ⌜φ⌝ ⊢ P *)
    intros φ P. unseal=> HP; split=> n x ??. by apply HP.
  - (* (∀ x : A, ⌜φ x⌝) ⊢ ⌜∀ x : A, φ x⌝ *)
    by unseal.
  - (* P ∧ Q ⊢ P *)
    intros P Q. unseal; by split=> n x ? [??].
  - (* P ∧ Q ⊢ Q *)
    intros P Q. unseal; by split=> n x ? [??].
  - (* (P ⊢ Q) → (P ⊢ R) → P ⊢ Q ∧ R *)
    intros P Q R HQ HR; unseal; split=> n x ??; by split; [apply HQ|apply HR].
  - (* P ⊢ P ∨ Q *)
    intros P Q. unseal; split=> n x ??; left; auto.
  - (* Q ⊢ P ∨ Q *)
    intros P Q. unseal; split=> n x ??; right; auto.
  - (* (P ⊢ R) → (Q ⊢ R) → P ∨ Q ⊢ R *)
    intros P Q R HP HQ. unseal; split=> n x ? [?|?]. by apply HP. by apply HQ.
  - (* (P ∧ Q ⊢ R) → P ⊢ Q → R. *)
    intros P Q R. unseal => HQ; split=> n x ?? n' x' ????. apply HQ;
425
      naive_solver eauto using uPred_mono, cmra_included_includedN.
Robbert Krebbers's avatar
Robbert Krebbers committed
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456
  - (* (P ⊢ Q → R) → P ∧ Q ⊢ R *)
    intros P Q R. unseal=> HP; split=> n x ? [??]. apply HP with n x; auto.
  - (* (∀ a, P ⊢ Ψ a) → P ⊢ ∀ a, Ψ a *)
    intros A P Ψ. unseal; intros HPΨ; split=> n x ?? a; by apply HPΨ.
  - (* (∀ a, Ψ a) ⊢ Ψ a *)
    intros A Ψ a. unseal; split=> n x ? HP; apply HP.
  - (* Ψ a ⊢ ∃ a, Ψ a *)
    intros A Ψ a. unseal; split=> n x ??; by exists a.
  - (* (∀ a, Ψ a ⊢ Q) → (∃ a, Ψ a) ⊢ Q *)
    intros A Ψ Q. unseal; intros HΨ; split=> n x ? [a ?]; by apply HΨ with a.
  - (* (P ⊢ Q) → (P' ⊢ Q') → P ∗ P' ⊢ Q ∗ Q' *)
    intros P P' Q Q' HQ HQ'; unseal.
    split; intros n' x ? (x1&x2&?&?&?); exists x1,x2; ofe_subst x;
      eauto 7 using cmra_validN_op_l, cmra_validN_op_r, uPred_in_entails.
  - (* P ⊢ emp ∗ P *)
    intros P. rewrite /uPred_emp. unseal; split=> n x ?? /=.
    exists (core x), x. by rewrite cmra_core_l.
  - (* emp ∗ P ⊢ P *)
    intros P. unseal; split; intros n x ? (x1&x2&?&_&?); ofe_subst;
      eauto using uPred_mono, cmra_includedN_r.
  - (* P ∗ Q ⊢ Q ∗ P *)
    intros P Q. unseal; split; intros n x ? (x1&x2&?&?&?).
    exists x2, x1; by rewrite (comm op).
  - (* (P ∗ Q) ∗ R ⊢ P ∗ (Q ∗ R) *)
    intros P Q R. unseal; split; intros n x ? (x1&x2&Hx&(y1&y2&Hy&?&?)&?).
    exists y1, (y2  x2); split_and?; auto.
    + by rewrite (assoc op) -Hy -Hx.
    + by exists y2, x2.
  - (* (P ∗ Q ⊢ R) → P ⊢ Q -∗ R *)
    intros P Q R. unseal=> HPQR; split=> n x ?? n' x' ???; apply HPQR; auto.
    exists x, x'; split_and?; auto.
457
    eapply uPred_mono; eauto using cmra_validN_op_l.
Robbert Krebbers's avatar
Robbert Krebbers committed
458 459 460
  - (* (P ⊢ Q -∗ R) → P ∗ Q ⊢ R *)
    intros P Q R. unseal=> HPQR. split; intros n x ? (?&?&?&?&?). ofe_subst.
    eapply HPQR; eauto using cmra_validN_op_l.
461
  - (* (P ⊢ Q) → <pers> P ⊢ <pers> Q *)
Robbert Krebbers's avatar
Robbert Krebbers committed
462
    intros P QR HP. unseal; split=> n x ? /=. by apply HP, cmra_core_validN.
463
  - (* <pers> P ⊢ <pers> <pers> P *)
Robbert Krebbers's avatar
Robbert Krebbers committed
464
    intros P. unseal; split=> n x ?? /=. by rewrite cmra_core_idemp.
465
  - (* P ⊢ <pers> emp (ADMISSIBLE) *)
Robbert Krebbers's avatar
Robbert Krebbers committed
466
    by unseal.
467
  - (* (∀ a, <pers> (Ψ a)) ⊢ <pers> (∀ a, Ψ a) *)
Robbert Krebbers's avatar
Robbert Krebbers committed
468
    by unseal.
469
  - (* <pers> (∃ a, Ψ a) ⊢ ∃ a, <pers> (Ψ a) *)
Robbert Krebbers's avatar
Robbert Krebbers committed
470
    by unseal.
471
  - (* <pers> P ∗ Q ⊢ <pers> P (ADMISSIBLE) *)
Robbert Krebbers's avatar
Robbert Krebbers committed
472 473 474
    intros P Q. move: (uPred_persistently P)=> P'.
    unseal; split; intros n x ? (x1&x2&?&?&_); ofe_subst;
      eauto using uPred_mono, cmra_includedN_l.
475
  - (* <pers> P ∧ Q ⊢ P ∗ Q *)
Robbert Krebbers's avatar
Robbert Krebbers committed
476 477
    intros P Q. unseal; split=> n x ? [??]; simpl in *.
    exists (core x), x; rewrite ?cmra_core_l; auto.
Robbert Krebbers's avatar
Robbert Krebbers committed
478 479
Qed.

Robbert Krebbers's avatar
Robbert Krebbers committed
480 481 482 483
Lemma uPred_sbi_mixin (M : ucmraT) : SbiMixin
  uPred_entails uPred_pure uPred_or uPred_impl
  (@uPred_forall M) (@uPred_exist M) uPred_sep
  uPred_persistently (@uPred_internal_eq M) uPred_later.
Robbert Krebbers's avatar
Robbert Krebbers committed
484 485
Proof.
  split.
Jacques-Henri Jourdan's avatar
Jacques-Henri Jourdan committed
486
  - (* Contractive sbi_later *)
Robbert Krebbers's avatar
Robbert Krebbers committed
487 488
    unseal; intros [|n] P Q HPQ; split=> -[|n'] x ?? //=; try omega.
    apply HPQ; eauto using cmra_validN_S.
489 490 491 492 493 494 495 496 497 498 499 500 501 502 503
  - (* NonExpansive2 (@uPred_internal_eq M A) *)
    intros A n x x' Hx y y' Hy; split=> n' z; unseal; split; intros; simpl in *.
    + by rewrite -(dist_le _ _ _ _ Hx) -?(dist_le _ _ _ _ Hy); auto.
    + by rewrite (dist_le _ _ _ _ Hx) ?(dist_le _ _ _ _ Hy); auto.
  - (* P ⊢ a ≡ a *)
    intros A P a. unseal; by split=> n x ?? /=.
  - (* a ≡ b ⊢ Ψ a → Ψ b *)
    intros A a b Ψ Hnonexp.
    unseal; split=> n x ? Hab n' x' ??? HΨ. eapply Hnonexp with n a; auto.
  - (* (∀ x, f x ≡ g x) ⊢ f ≡ g *)
    by unseal.
  - (* `x ≡ `y ⊢ x ≡ y *)
    by unseal.
  - (* Discrete a → a ≡ b ⊣⊢ ⌜a ≡ b⌝ *)
    intros A a b ?. unseal; split=> n x ?; by apply (discrete_iff n).
Robbert Krebbers's avatar
Robbert Krebbers committed
504 505 506 507 508 509 510 511 512
  - (* Next x ≡ Next y ⊢ ▷ (x ≡ y) *)
    by unseal.
  - (* ▷ (x ≡ y) ⊢ Next x ≡ Next y *)
    by unseal.
  - (* (P ⊢ Q) → ▷ P ⊢ ▷ Q *)
    intros P Q.
    unseal=> HP; split=>-[|n] x ??; [done|apply HP; eauto using cmra_validN_S].
  - (* (▷ P → P) ⊢ P *)
    intros P. unseal; split=> n x ? HP; induction n as [|n IH]; [by apply HP|].
513
    apply HP, IH, uPred_mono with (S n) x; eauto using cmra_validN_S.
Robbert Krebbers's avatar
Robbert Krebbers committed
514 515 516 517 518 519 520 521 522 523 524 525 526
  - (* (∀ a, ▷ Φ a) ⊢ ▷ ∀ a, Φ a *)
    intros A Φ. unseal; by split=> -[|n] x.
  - (* (▷ ∃ a, Φ a) ⊢ ▷ False ∨ (∃ a, ▷ Φ a) *)
    intros A Φ. unseal; split=> -[|[|n]] x /=; eauto.
  - (* ▷ (P ∗ Q) ⊢ ▷ P ∗ ▷ Q *)
    intros P Q. unseal; split=> -[|n] x ? /=.
    { by exists x, (core x); rewrite cmra_core_r. }
    intros (x1&x2&Hx&?&?); destruct (cmra_extend n x x1 x2)
      as (y1&y2&Hx'&Hy1&Hy2); eauto using cmra_validN_S; simpl in *.
    exists y1, y2; split; [by rewrite Hx'|by rewrite Hy1 Hy2].
  - (* ▷ P ∗ ▷ Q ⊢ ▷ (P ∗ Q) *)
    intros P Q. unseal; split=> -[|n] x ? /=; [done|intros (x1&x2&Hx&?&?)].
    exists x1, x2; eauto using dist_S.
527
  - (* ▷ <pers> P ⊢ <pers> ▷ P *)
Robbert Krebbers's avatar
Robbert Krebbers committed
528
    by unseal.
529
  - (* <pers> ▷ P ⊢ ▷ <pers> P *)
Robbert Krebbers's avatar
Robbert Krebbers committed
530 531 532 533
    by unseal.
  - (* ▷ P ⊢ ▷ False ∨ (▷ False → P) *)
    intros P. unseal; split=> -[|n] x ? /= HP; [by left|right].
    intros [|n'] x' ????; [|done].
534
    eauto using uPred_mono, cmra_included_includedN.
Robbert Krebbers's avatar
Robbert Krebbers committed
535 536 537 538 539 540 541 542 543 544 545 546
Qed.

Canonical Structure uPredI (M : ucmraT) : bi :=
  {| bi_ofe_mixin := ofe_mixin_of (uPred M); bi_bi_mixin := uPred_bi_mixin M |}.
Canonical Structure uPredSI (M : ucmraT) : sbi :=
  {| sbi_ofe_mixin := ofe_mixin_of (uPred M);
     sbi_bi_mixin := uPred_bi_mixin M; sbi_sbi_mixin := uPred_sbi_mixin M |}.

Coercion uPred_valid {M} : uPred M  Prop := bi_valid.

(* Latest notation *)
Notation "✓ x" := (uPred_cmra_valid x) (at level 20) : bi_scope.
547

Robbert Krebbers's avatar
Robbert Krebbers committed
548 549 550 551 552 553 554 555
Lemma uPred_plainly_mixin M : BiPlainlyMixin (uPredSI M) uPred_plainly.
Proof.
  split.
  - (* NonExpansive uPred_plainly *)
    intros n P1 P2 HP.
    unseal; split=> n' x; split; apply HP; eauto using @ucmra_unit_validN.
  - (* (P ⊢ Q) → ■ P ⊢ ■ Q *)
    intros P QR HP. unseal; split=> n x ? /=. by apply HP, ucmra_unit_validN.
556
  - (* ■ P ⊢ <pers> P *)
Robbert Krebbers's avatar
Robbert Krebbers committed
557 558 559 560 561
    unseal; split; simpl; eauto using uPred_mono, @ucmra_unit_leastN.
  - (* ■ P ⊢ ■ ■ P *)
    unseal; split=> n x ?? //.
  - (* (∀ a, ■ (Ψ a)) ⊢ ■ (∀ a, Ψ a) *)
    by unseal.
562
  - (* (■ P → <pers> Q) ⊢ <pers> (■ P → Q) *)
Robbert Krebbers's avatar
Robbert Krebbers committed
563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587
    unseal; split=> /= n x ? HPQ n' x' ????.
    eapply uPred_mono with n' (core x)=>//; [|by apply cmra_included_includedN].
    apply (HPQ n' x); eauto using cmra_validN_le.
  - (* (■ P → ■ Q) ⊢ ■ (■ P → Q) *)
    unseal; split=> /= n x ? HPQ n' x' ????.
    eapply uPred_mono with n' ε=>//; [|by apply cmra_included_includedN].
    apply (HPQ n' x); eauto using cmra_validN_le.
  - (* P ⊢ ■ emp (ADMISSIBLE) *)
    by unseal.
  - (* ■ P ∗ Q ⊢ ■ P *)
    intros P Q. move: (uPred_persistently P)=> P'.
    unseal; split; intros n x ? (x1&x2&?&?&_); ofe_subst;
      eauto using uPred_mono, cmra_includedN_l.
  - (* ■ ((P -∗ Q) ∧ (Q -∗ P)) ⊢ P ≡ Q *)
    unseal; split=> n x ? /= HPQ. split=> n' x' ??.
    move: HPQ=> [] /(_ n' x'); rewrite !left_id=> ?.
    move=> /(_ n' x'); rewrite !left_id=> ?. naive_solver.
  - (* ▷ ■ P ⊢ ■ ▷ P *)
    by unseal.
  - (* ■ ▷ P ⊢ ▷ ■ P *)
    by unseal.
Qed.
Instance uPred_plainlyC M : BiPlainly (uPredSI M) :=
  {| bi_plainly_mixin := uPred_plainly_mixin M |}.

Robbert Krebbers's avatar
Robbert Krebbers committed
588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608
Lemma uPred_bupd_mixin M : BiBUpdMixin (uPredI M) uPred_bupd.
Proof.
  split.
  - intros n P Q HPQ.
    unseal; split=> n' x; split; intros HP k yf ??;
    destruct (HP k yf) as (x'&?&?); auto;
    exists x'; split; auto; apply HPQ; eauto using cmra_validN_op_l.
  - unseal. split=> n x ? HP k yf ?; exists x; split; first done.
    apply uPred_mono with n x; eauto using cmra_validN_op_l.
  - unseal. intros HPQ; split=> n x ? HP k yf ??.
    destruct (HP k yf) as (x'&?&?); eauto.
    exists x'; split; eauto using uPred_in_entails, cmra_validN_op_l.
  - unseal; split; naive_solver.
  - unseal. split; intros n x ? (x1&x2&Hx&HP&?) k yf ??.
    destruct (HP k (x2  yf)) as (x'&?&?); eauto.
    { by rewrite assoc -(dist_le _ _ _ _ Hx); last lia. }
    exists (x'  x2); split; first by rewrite -assoc.
    exists x', x2. eauto using uPred_mono, cmra_validN_op_l, cmra_validN_op_r.
Qed.
Instance uPred_bi_bupd M : BiBUpd (uPredI M) := {| bi_bupd_mixin := uPred_bupd_mixin M |}.

Robbert Krebbers's avatar
Robbert Krebbers committed
609 610 611 612 613 614 615
Instance uPred_bi_bupd_plainly M : BiBUpdPlainly (uPredSI M).
Proof.
  rewrite /BiBUpdPlainly. unseal; split => n x Hnx /= Hng.
  destruct (Hng n ε) as [? [_ Hng']]; try rewrite right_id; auto.
  eapply uPred_mono; eauto using ucmra_unit_leastN.
Qed.

616
Module uPred.
Robbert Krebbers's avatar
Robbert Krebbers committed
617 618
Include uPred_unseal.
Section uPred.
619
Context {M : ucmraT}.
Robbert Krebbers's avatar
Robbert Krebbers committed
620
Implicit Types φ : Prop.
621
Implicit Types P Q : uPred M.
Robbert Krebbers's avatar
Robbert Krebbers committed
622 623 624
Implicit Types A : Type.
Arguments uPred_holds {_} !_ _ _ /.
Hint Immediate uPred_in_entails.
625

Robbert Krebbers's avatar
Robbert Krebbers committed
626
Global Instance ownM_ne : NonExpansive (@uPred_ownM M).
627
Proof.
Robbert Krebbers's avatar
Robbert Krebbers committed
628 629
  intros n a b Ha.
  unseal; split=> n' x ? /=. by rewrite (dist_le _ _ _ _ Ha); last lia.
630
Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
631
Global Instance ownM_proper: Proper (() ==> ()) (@uPred_ownM M) := ne_proper _.
632

Robbert Krebbers's avatar
Robbert Krebbers committed
633 634
Global Instance cmra_valid_ne {A : cmraT} :
  NonExpansive (@uPred_cmra_valid M A).
635
Proof.
Robbert Krebbers's avatar
Robbert Krebbers committed
636 637
  intros n a b Ha; unseal; split=> n' x ? /=.
  by rewrite (dist_le _ _ _ _ Ha); last lia.
638
Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
639 640 641
Global Instance cmra_valid_proper {A : cmraT} :
  Proper (() ==> ()) (@uPred_cmra_valid M A) := ne_proper _.

642
(** BI instances *)
643

644 645 646
Global Instance uPred_affine : BiAffine (uPredI M) | 0.
Proof. intros P. rewrite /Affine. by apply bi.pure_intro. Qed.

Robbert Krebbers's avatar
Robbert Krebbers committed
647
Global Instance uPred_plainly_exist_1 : BiPlainlyExist (uPredSI M).
648
Proof. unfold BiPlainlyExist. by unseal. Qed.
649

Robbert Krebbers's avatar
Robbert Krebbers committed
650
(** Limits *)
Robbert Krebbers's avatar
Robbert Krebbers committed
651 652
Lemma entails_lim (cP cQ : chain (uPredC M)) :
  ( n, cP n  cQ n)  compl cP  compl cQ.
653
Proof.
Robbert Krebbers's avatar
Robbert Krebbers committed
654
  intros Hlim; split=> n m ? HP.
655 656 657
  eapply uPred_holds_ne, Hlim, HP; eauto using conv_compl.
Qed.

Robbert Krebbers's avatar
Robbert Krebbers committed
658 659 660 661 662 663 664 665 666 667 668
(* Own *)
Lemma ownM_op (a1 a2 : M) :
  uPred_ownM (a1  a2)  uPred_ownM a1  uPred_ownM a2.
Proof.
  rewrite /bi_sep /=; unseal. split=> n x ?; split.
  - intros [z ?]; exists a1, (a2  z); split; [by rewrite (assoc op)|].
    split. by exists (core a1); rewrite cmra_core_r. by exists z.
  - intros (y1&y2&Hx&[z1 Hy1]&[z2 Hy2]); exists (z1  z2).
    by rewrite (assoc op _ z1) -(comm op z1) (assoc op z1)
      -(assoc op _ a2) (comm op z1) -Hy1 -Hy2.
Qed.
669
Lemma persistently_ownM_core (a : M) : uPred_ownM a  <pers> uPred_ownM (core a).
Robbert Krebbers's avatar
Robbert Krebbers committed
670 671 672 673
Proof.
  rewrite /bi_persistently /=. unseal.
  split=> n x Hx /=. by apply cmra_core_monoN.
Qed.
674
Lemma ownM_unit : bi_valid (uPred_ownM (ε:M)).
Robbert Krebbers's avatar
Robbert Krebbers committed
675 676 677
Proof. unseal; split=> n x ??; by  exists x; rewrite left_id. Qed.
Lemma later_ownM (a : M) :  uPred_ownM a   b, uPred_ownM b   (a  b).
Proof.
678
  rewrite /bi_and /sbi_later /bi_exist /sbi_internal_eq /=; unseal.
Robbert Krebbers's avatar
Robbert Krebbers committed
679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699
  split=> -[|n] x /= ? Hax; first by eauto using ucmra_unit_leastN.
  destruct Hax as [y ?].
  destruct (cmra_extend n x a y) as (a'&y'&Hx&?&?); auto using cmra_validN_S.
  exists a'. rewrite Hx. eauto using cmra_includedN_l.
Qed.

(* Valid *)
Lemma discrete_valid {A : cmraT} `{!CmraDiscrete A} (a : A) :
   a  (⌜✓ a : uPred M).
Proof. unseal. split=> n x _. by rewrite /= -cmra_discrete_valid_iff. Qed.
Lemma ownM_valid (a : M) : uPred_ownM a   a.
Proof.
  unseal; split=> n x Hv [a' ?]; ofe_subst; eauto using cmra_validN_op_l.
Qed.
Lemma cmra_valid_intro {A : cmraT} (a : A) :
   a  bi_valid (PROP:=uPredI M) ( a).
Proof. unseal=> ?; split=> n x ? _ /=; by apply cmra_valid_validN. Qed.
Lemma cmra_valid_elim {A : cmraT} (a : A) : ¬ {0} a   a  (False : uPred M).
Proof.
  intros Ha. unseal. split=> n x ??; apply Ha, cmra_validN_le with n; auto.
Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
700
Lemma plainly_cmra_valid_1 {A : cmraT} (a : A) :  a   ( a : uPred M).
Robbert Krebbers's avatar
Robbert Krebbers committed
701 702 703 704 705 706 707 708 709 710
Proof. by unseal. Qed.
Lemma cmra_valid_weaken {A : cmraT} (a b : A) :  (a  b)  ( a : uPred M).
Proof. unseal; split=> n x _; apply cmra_validN_op_l. Qed.

Lemma prod_validI {A B : cmraT} (x : A * B) :  x  ( x.1   x.2 : uPred M).
Proof. by unseal. Qed.
Lemma option_validI {A : cmraT} (mx : option A) :
   mx  match mx with Some x =>  x | None => True : uPred M end.
Proof. unseal. by destruct mx. Qed.

711
Lemma ofe_fun_validI `{B : A  ucmraT} (g : ofe_fun B) :
712 713 714
  ( g : uPred M)   i,  g i.
Proof. by uPred.unseal. Qed.

Robbert Krebbers's avatar
Robbert Krebbers committed
715 716 717 718 719 720 721 722 723 724
Lemma bupd_ownM_updateP x (Φ : M  Prop) :
  x ~~>: Φ  uPred_ownM x ==  y, ⌜Φ y  uPred_ownM y.
Proof.
  intros Hup. unseal. split=> n x2 ? [x3 Hx] k yf ??.
  destruct (Hup k (Some (x3  yf))) as (y&?&?); simpl in *.
  { rewrite /= assoc -(dist_le _ _ _ _ Hx); auto. }
  exists (y  x3); split; first by rewrite -assoc.
  exists y; eauto using cmra_includedN_l.
Qed.
End uPred.
725
End uPred.