fracpred.v 28.2 KB
Newer Older
Robbert Krebbers's avatar
Robbert Krebbers committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
(* This file shows that given a BI [PROP], the type of predicates over
non-negative fractions [fracPred PROP], is again form a BI. This BI is the key
to the abstract Iron++ logic in the paper.

Apart from showing that [fracPred PROP] is a BI, this file also shows that
it is a step-indexed BI (i.e. with the [▷] modality), and that various other
operators on BIs can be lifted, namely the basic update modality [|=>] and the
plain modality [■]. It does not lift the fancy update modality [|={E1,E2}=>]
because that requires the [perm] connective which is specific to the Iron logic
(i.e. [perm] is not a BI generic construct).

The file moreover defines type class instances for the embedding [ ⎡ ⎤ ] from
the underlying BI [PROP] into [fracPred PROP], and defines the class
[FObjective] of elements of [fracPred PROP] that is independent of the fraction.

As a technical detail: Since we only have the type [Qp] of positive fractions,
we use the type [option Qp] to represent non-negative fractions. *)
From iris.algebra Require Export frac.
From stdpp Require Import coPset.
From iris.bi Require Export bi tactics.

Record fracPred (PROP : Type) := FracPred { fracPred_at :> option Qp  PROP }.
Arguments FracPred {_} _.
Arguments fracPred_at {_} _ _.

Section ofe.
  Context {PROP : bi}.
  Inductive fracPred_equiv' (P Q : fracPred PROP) :=
    { monPred_in_equiv π : P π  Q π }.
  Instance fracPred_equiv : Equiv (fracPred PROP) := fracPred_equiv'.

  Inductive fracPred_dist' (n : nat) (P Q : fracPred PROP) :=
    { monPred_in_dist π : P π {n} Q π }.
  Instance fracPred_dist : Dist (fracPred PROP) := fracPred_dist'.

  Lemma fracPred_ofe_mixin : OfeMixin (fracPred PROP).
  Proof.
38
    refine (iso_ofe_mixin (λ P : fracPred PROP, P : _ -d> _) _ _);
Robbert Krebbers's avatar
Robbert Krebbers committed
39 40
      (split; [by destruct 1|by constructor]).
  Qed.
41
  Canonical Structure fracPredO : ofeT := OfeT (fracPred PROP) fracPred_ofe_mixin.
Robbert Krebbers's avatar
Robbert Krebbers committed
42

43
  Global Instance fracPred_cofe `{Cofe PROP} : Cofe fracPredO.
Robbert Krebbers's avatar
Robbert Krebbers committed
44
  Proof.
45
    refine (iso_cofe (λ P : _ -d> _, @FracPred PROP P) id _ _).
Robbert Krebbers's avatar
Robbert Krebbers committed
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 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 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
    - split; [by destruct 1|by constructor].
    - done.
  Qed.

  Global Instance fracPred_at_ne n : Proper (dist n ==> (=) ==> dist n) fracPred_at.
  Proof. by intros ?? [?] ?? ->. Qed.
  Global Instance fracPred_at_proper : Proper (() ==> (=) ==> ()) fracPred_at.
  Proof. by intros ?? [?] ?? ->. Qed.
End ofe.

(** BI and SBI structures. *)
Section bi.
  Context {PROP : bi}.
  Notation fracPred := (fracPred PROP).

  Inductive fracPred_entails (P1 P2 : fracPred) : Prop :=
    { fracPred_in_entails π : P1 π  P2 π }.
  Hint Immediate fracPred_in_entails.

  Definition fracPred_embed_def (P : PROP) : fracPred := FracPred (λ _, P)%I.
  Definition fracPred_embed_aux : seal (@fracPred_embed_def). by eexists. Qed.
  Definition fracPred_embed : Embed PROP fracPred := fracPred_embed_aux.(unseal).
  Definition fracPred_embed_eq : @embed _ _ fracPred_embed = _ := fracPred_embed_aux.(seal_eq).

  Definition fracPred_emp_def : fracPred := FracPred (λ π, <affine>  π = ε )%I.
  Definition fracPred_emp_aux : seal (@fracPred_emp_def). by eexists. Qed.
  Definition fracPred_emp := fracPred_emp_aux.(unseal).
  Definition fracPred_emp_eq : @fracPred_emp = _ := fracPred_emp_aux.(seal_eq).

  Definition fracPred_pure_def (φ : Prop) : fracPred := FracPred (λ _, ⌜φ⌝)%I.
  Definition fracPred_pure_aux : seal (@fracPred_pure_def). by eexists. Qed.
  Definition fracPred_pure := fracPred_pure_aux.(unseal).
  Definition fracPred_pure_eq : @fracPred_pure = _ := fracPred_pure_aux.(seal_eq).

  Definition fracPred_and_def (P Q : fracPred) : fracPred := FracPred (λ π, P π  Q π)%I.
  Definition fracPred_and_aux : seal (@fracPred_and_def). by eexists. Qed.
  Definition fracPred_and := fracPred_and_aux.(unseal).
  Definition fracPred_and_eq : @fracPred_and = _ := fracPred_and_aux.(seal_eq).

  Definition fracPred_or_def (P Q : fracPred) : fracPred := FracPred (λ π, P π  Q π)%I.
  Definition fracPred_or_aux : seal (@fracPred_or_def). by eexists. Qed.
  Definition fracPred_or := fracPred_or_aux.(unseal).
  Definition fracPred_or_eq : @fracPred_or = _ := fracPred_or_aux.(seal_eq).

  Definition fracPred_impl_def (P Q : fracPred) : fracPred := FracPred (λ π, P π  Q π)%I.
  Definition fracPred_impl_aux : seal (@fracPred_impl_def). by eexists. Qed.
  Definition fracPred_impl := fracPred_impl_aux.(unseal).
  Definition fracPred_impl_eq : @fracPred_impl = _ := fracPred_impl_aux.(seal_eq).

  Definition fracPred_forall_def {A} (Φ : A  fracPred) : fracPred :=
    FracPred (λ π,  x : A, Φ x π)%I.
  Definition fracPred_forall_aux : seal (@fracPred_forall_def). by eexists. Qed.
  Definition fracPred_forall := fracPred_forall_aux.(unseal).
  Definition fracPred_forall_eq : @fracPred_forall = _ := fracPred_forall_aux.(seal_eq).

  Definition fracPred_exist_def {A} (Φ : A  fracPred) : fracPred :=
    FracPred (λ π,  x : A, Φ x π)%I.
  Definition fracPred_exist_aux : seal (@fracPred_exist_def). by eexists. Qed.
  Definition fracPred_exist := fracPred_exist_aux.(unseal).
  Definition fracPred_exist_eq : @fracPred_exist = _ := fracPred_exist_aux.(seal_eq).

  Definition fracPred_sep_def (P Q : fracPred) : fracPred :=
    FracPred (λ π,  π1 π2,  π = π1  π2   (P π1  Q π2))%I.
  Definition fracPred_sep_aux : seal (@fracPred_sep_def). by eexists. Qed.
  Definition fracPred_sep := fracPred_sep_aux.(unseal).
  Definition fracPred_sep_eq : @fracPred_sep = _ := fracPred_sep_aux.(seal_eq).

  Definition fracPred_wand_def (P Q : fracPred) : fracPred :=
    FracPred (λ π,  π', P π' - Q (π  π'))%I.
  Definition fracPred_wand_aux : seal (@fracPred_wand_def). by eexists. Qed.
  Definition fracPred_wand := fracPred_wand_aux.(unseal).
  Definition fracPred_wand_eq : @fracPred_wand = _ := fracPred_wand_aux.(seal_eq).

  Definition fracPred_persistently_def (P : fracPred) : fracPred :=
    FracPred (λ _, <pers> P ε)%I.
  Definition fracPred_persistently_aux : seal (@fracPred_persistently_def). by eexists. Qed.
  Definition fracPred_persistently := fracPred_persistently_aux.(unseal).
  Definition fracPred_persistently_eq : @fracPred_persistently = _ :=
    fracPred_persistently_aux.(seal_eq).
End bi.

Section sbi.
  Context {PROP : sbi}.
  Notation fracPred := (fracPred PROP).

  Definition fracPred_internal_eq_def (A : ofeT) (a b : A) : fracPred :=
    FracPred (λ _, a  b)%I.
  Definition fracPred_internal_eq_aux : seal (@fracPred_internal_eq_def). by eexists. Qed.
  Definition fracPred_internal_eq := fracPred_internal_eq_aux.(unseal).
  Definition fracPred_internal_eq_eq : @fracPred_internal_eq = _ :=
    fracPred_internal_eq_aux.(seal_eq).

  Definition fracPred_later_def (P : fracPred) : fracPred := FracPred (λ π,  P π)%I.
  Definition fracPred_later_aux : seal fracPred_later_def. by eexists. Qed.
  Definition fracPred_later := fracPred_later_aux.(unseal).
  Definition fracPred_later_eq : fracPred_later = _ := fracPred_later_aux.(seal_eq).
End sbi.

Module Import fracPred.
  Definition unseal_eqs :=
    (@fracPred_and_eq, @fracPred_or_eq, @fracPred_impl_eq,
     @fracPred_forall_eq, @fracPred_exist_eq, @fracPred_sep_eq, @fracPred_wand_eq,
     @fracPred_persistently_eq, @fracPred_later_eq, @fracPred_internal_eq_eq,
     @fracPred_embed_eq, @fracPred_emp_eq, @fracPred_pure_eq).
  Ltac unseal :=
    unfold bi_affinely, bi_absorbingly, sbi_except_0, bi_pure, bi_emp, bi_and, bi_or,
           bi_impl, bi_forall, bi_exist, sbi_internal_eq, bi_sep, bi_wand,
           bi_persistently, bi_affinely, sbi_later;
    simpl;
    unfold sbi_emp, sbi_pure, sbi_and, sbi_or, sbi_impl, sbi_forall, sbi_exist,
           sbi_internal_eq, sbi_sep, sbi_wand, sbi_persistently;
    simpl;
    rewrite !unseal_eqs /=.
End fracPred.

Lemma fracPred_bi_mixin (PROP : bi) : BiMixin (PROP:=fracPred PROP)
  fracPred_entails fracPred_emp fracPred_pure fracPred_and fracPred_or
  fracPred_impl fracPred_forall fracPred_exist fracPred_sep fracPred_wand
  fracPred_persistently.
Proof.
  split; try unseal; try by (split=> ? /=; repeat f_equiv).
  - split.
    + intros P. by split.
    + intros P Q R [H1] [H2]. split => ?. by rewrite H1 H2.
  - split.
    + intros [HPQ]. split; split => π; move: (HPQ π); by apply bi.equiv_spec.
    + intros [[] []]. split=>π. by apply bi.equiv_spec.
  - intros P φ ?. split=> π /=. by apply bi.pure_intro.
  - intros φ P HP. split=> π. apply bi.pure_elim'=> ?. by apply HP.
  - intros A φ. split=> π. by apply bi.pure_forall_2.
  - intros P Q. split=> π. by apply bi.and_elim_l.
  - intros P Q. split=> π. by apply bi.and_elim_r.
  - intros P Q R [?] [?]. split=> π. by apply bi.and_intro.
  - intros P Q. split=> π. by apply bi.or_intro_l.
  - intros P Q. split=> π. by apply bi.or_intro_r.
  - intros P Q R [?] [?]. split=> π. by apply bi.or_elim.
  - intros P Q R [HR]. split=> π /=. apply bi.impl_intro_r, HR.
  - intros P Q R [HR]. split=> π /=. by rewrite HR bi.impl_elim_l.
  - intros A P Ψ HΨ. split=> π. apply bi.forall_intro => ?. by apply HΨ.
  - intros A Ψ. split=> π. by apply: bi.forall_elim.
  - intros A Ψ a. split=> π. by rewrite /= -bi.exist_intro.
  - intros A Ψ Q HΨ. split=> π. apply bi.exist_elim => a. by apply HΨ.
  - intros P P' Q Q' [?] [?]. split=> π /=. apply bi.exist_mono=> π1.
    apply bi.exist_mono=> π2. by apply bi.and_mono_r, bi.sep_mono.
  - intros P. split=> π /=. rewrite -(bi.exist_intro None) -(bi.exist_intro π).
    by rewrite -bi.persistent_and_affinely_sep_l !bi.pure_True ?left_id_L // !left_id.
  - intros P. split=> π /=. apply bi.exist_elim=> π1. apply bi.exist_elim=> π2.
    apply bi.pure_elim_l=> ->. rewrite -bi.persistent_and_affinely_sep_l.
    apply bi.pure_elim_l=> ->. by rewrite left_id_L.
  - intros P Q. split=> π /=. apply bi.exist_elim=> π1. apply bi.exist_elim=> π2.
    by rewrite -(bi.exist_intro π2) -(bi.exist_intro π1) comm_L (comm bi_sep).
  - intros P Q R. split=> π /=. apply bi.exist_elim=> π'. apply bi.exist_elim=> π3.
    apply bi.pure_elim_l=> ->. repeat setoid_rewrite bi.sep_exist_r.
    apply bi.exist_elim=> π1. apply bi.exist_elim=> π2.
    rewrite bi.persistent_and_affinely_sep_l -assoc.
    rewrite -bi.persistent_and_affinely_sep_l. apply bi.pure_elim_l=> ->.
    rewrite -(bi.exist_intro π1) -(bi.exist_intro (π2  π3)) assoc_L.
    rewrite bi.pure_True // left_id -(bi.exist_intro π2) -(bi.exist_intro π3).
    by rewrite bi.pure_True // left_id assoc.
  - intros P Q R [HR]. split=> π /=. apply bi.forall_intro=> π'.
    apply bi.wand_intro_r.
    by rewrite -HR /= -!bi.exist_intro bi.pure_True // left_id.
  - intros P Q R [HP]. split=> π /=. apply bi.exist_elim=> π1.
    apply bi.exist_elim=> π2. apply bi.pure_elim_l=> ->.
    by rewrite HP /= (bi.forall_elim π2) bi.wand_elim_l.
  - intros P Q [?]. split=> π /=. by f_equiv.
  - intros P. split=> π. by apply bi.persistently_idemp_2.
  - split=> π /=. apply (bi.persistently_intro _ _), (bi.affinely_intro _ _).
    by apply bi.pure_intro.
  - intros A Ψ. split=> π /=. by apply bi.persistently_forall_2.
  - intros A Ψ. split=> π /=. by apply bi.persistently_exist_1.
  - intros P Q. split=> π /=. apply bi.exist_elim=> π1. apply bi.exist_elim=> π2.
    apply bi.pure_elim_l=> _. by rewrite bi.sep_elim_l.
  - intros P Q. split=> π /=. rewrite -(bi.exist_intro ε) -(bi.exist_intro π).
    rewrite left_id_L bi.pure_True // left_id. by apply bi.persistently_and_sep_elim.
Qed.

Canonical Structure fracPredI (PROP : bi) : bi :=
  {| bi_ofe_mixin := fracPred_ofe_mixin; bi_bi_mixin := fracPred_bi_mixin PROP |}.

Lemma fracPred_sbi_mixin (PROP : sbi) :
  SbiMixin (PROP:=fracPred PROP) fracPred_entails fracPred_pure
           fracPred_or fracPred_impl fracPred_forall fracPred_exist
           fracPred_sep fracPred_persistently fracPred_internal_eq fracPred_later.
Proof.
  split; unseal.
  - intros n P Q HPQ. split=> π /=.
    apply bi.later_contractive. destruct n as [|n]=> //. by apply HPQ.
  - by split=> ? /=; repeat f_equiv.
  - intros A P a. split=> π. by apply bi.internal_eq_refl.
  - intros A a b Ψ ?. split=> π /=.
    erewrite (bi.internal_eq_rewrite _ _ (flip Ψ _)) => //=. solve_proper.
  - intros A1 A2 f g. split=> π. by apply bi.fun_ext.
  - intros A P x y. split=> π. by apply bi.sig_eq.
  - intros A a b ?. split=> π. by apply bi.discrete_eq_1.
  - intros A x y. split=> π. by apply bi.later_eq_1.
  - intros A x y. split=> π. by apply bi.later_eq_2.
  - intros P Q [?]. split=> π. by apply bi.later_mono.
  - intros P. split=> π /=. by apply bi.later_intro.
  - intros A Ψ. split=> π. by apply bi.later_forall_2.
  - intros A Ψ. split=> π. by apply bi.later_exist_false.
  - intros P Q. split=> π /=. repeat setoid_rewrite bi.later_exist.
    apply bi.exist_elim=> π1. apply bi.exist_elim=> π2.
    rewrite bi.later_and (timeless  _ %I) /sbi_except_0 bi.and_or_r.
    apply bi.or_elim.
    { rewrite bi.and_elim_l -(bi.exist_intro π) -(bi.exist_intro ε).
      rewrite right_id_L -bi.later_sep.
      auto using bi.and_intro, bi.False_elim, bi.later_mono, bi.pure_intro. }
    by rewrite -(bi.exist_intro π1) -(bi.exist_intro π2) bi.later_sep.
  - intros P Q. split=> π /=. repeat setoid_rewrite bi.later_exist.
    apply bi.exist_mono=> π1; apply bi.exist_mono=> π2.
    by rewrite {1}(bi.later_intro  _ )%I -bi.later_sep -bi.later_and.
  - intros P. split=> π. by apply bi.later_persistently_1.
  - intros P. split=> π. by apply bi.later_persistently_2.
  - intros P. split=> π /=. apply bi.later_false_em.
Qed.

Canonical Structure fracPredSI (PROP: sbi) : sbi :=
  {| sbi_ofe_mixin := fracPred_ofe_mixin; sbi_bi_mixin := fracPred_bi_mixin PROP;
     sbi_sbi_mixin := fracPred_sbi_mixin PROP |}.

Class FObjective {PROP : bi} (P : fracPred PROP) :=
  fobjective_at π π' : P π - P π'.
Arguments FObjective {_} _%I.
Arguments fobjective_at {_} _%I {_}.
Hint Mode FObjective + ! : typeclass_instances.
272
Instance: Params (@FObjective) 1 := {}.
Robbert Krebbers's avatar
Robbert Krebbers committed
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 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 425 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 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506

(** Primitive facts that cannot be deduced from the BI structure. *)
Section bi_facts.
  Context {PROP : bi}.
  Local Notation fracPred := (fracPred PROP).
  Local Notation fracPredI := (fracPredI PROP).
  Local Notation fracPred_at := (@fracPred_at PROP).
  Implicit Types P Q : fracPred.

  (** Instances *)
  Global Instance fracPred_at_mono : Proper (() ==> (=) ==> ()) fracPred_at.
  Proof. by move=> ?? [?] ?? ->. Qed.
  Global Instance fracPred_at_flip_mono :
    Proper (flip () ==> (=) ==> flip ()) fracPred_at.
  Proof. solve_proper. Qed.

  Global Instance fracPred_positive : BiPositive PROP  BiPositive fracPredI.
  Proof.
    split => π. unseal. rewrite bi.affinely_and_l -bi.affinely_and_r.
    apply bi.pure_elim_l=> ->. repeat setoid_rewrite bi.affinely_exist.
    apply bi.exist_mono=> π1; apply bi.exist_mono=> π2.
    rewrite -bi.affinely_and_r bi_positive (symmetry_iff (=)) op_None.
    apply bi.pure_elim_l=> -[-> ->]. rewrite bi.affinely_and_l -bi.affinely_and_r.
    auto using bi.and_intro, bi.sep_mono, bi.pure_intro.
  Qed.

  Lemma fracPred_objective_persistent P :
    FObjective P  Persistent (P ε)  Persistent P.
  Proof.
    rewrite /FObjective /Persistent=> HP HP'; split=> π. unseal.
    by rewrite (HP π ε) {1}HP'.
  Qed.

  Global Instance fracPred_at_persistent P : Persistent P  Persistent (P ε).
  Proof. move => [] /(_ ε). by unseal. Qed.
  Global Instance fracPred_at_absorbing P π : Absorbing P  Absorbing (P π).
  Proof.
    move => [] /(_ π). rewrite /Absorbing. unseal.
    by rewrite -(bi.exist_intro ε) -(bi.exist_intro π) left_id_L bi.pure_True // left_id.
  Qed.
  Global Instance fracPred_at_affine P π : Affine P  Affine (P π).
  Proof. move => [] /(_ π). rewrite /Affine. unseal. by rewrite bi.affinely_elim_emp. Qed.

  Definition fracPred_embedding_mixin : BiEmbedMixin PROP fracPredI fracPred_embed.
  Proof.
    split; try apply _; unfold bi_emp_valid; unseal; try done.
    - move => P [/(_ inhabitant) /= <-].
      by apply (bi.affinely_intro _ _), bi.pure_intro.
    - split=> π /=. by apply bi.affinely_elim_emp.
    - split=> π /=. apply (anti_symm _).
      + by rewrite -(bi.exist_intro π) -(bi.exist_intro ε) right_id_L bi.pure_True // left_id.
      + apply bi.exist_elim=> π1. apply bi.exist_elim=> π2. by apply bi.pure_elim_l.
    - split=> π /=. by rewrite (bi.forall_elim ε).
  Qed.
  Global Instance fracPred_bi_embed : BiEmbed PROP fracPredI :=
    {| bi_embed_mixin := fracPred_embedding_mixin |}.

  (** fracPred_at unfolding laws *)
  Lemma fracPred_at_embed π (P : PROP) : fracPred_at P π  P.
  Proof. by unseal. Qed.
  Lemma fracPred_at_pure π (φ : Prop) : fracPred_at ⌜φ⌝ π  ⌜φ⌝.
  Proof. by unseal. Qed.
  Lemma fracPred_at_emp π : fracPred_at emp π  <affine>  π = ε .
  Proof. by unseal. Qed.
  Lemma fracPred_at_and π P Q : (P  Q) π  P π  Q π.
  Proof. by unseal. Qed.
  Lemma fracPred_at_or π P Q : (P  Q) π  P π  Q π.
  Proof. by unseal. Qed.
  Lemma fracPred_at_impl π P Q : (P  Q) π  (P π  Q π).
  Proof. by unseal. Qed.
  Lemma fracPred_at_forall {A} π (Φ : A  fracPred) : ( x, Φ x) π   x, Φ x π.
  Proof. by unseal. Qed.
  Lemma fracPred_at_exist {A} π (Φ : A  fracPred) : ( x, Φ x) π   x, Φ x π.
  Proof. by unseal. Qed.
  Lemma fracPred_at_sep π P Q : (P  Q) π   π1 π2,  π = π1  π2   (P π1  Q π2).
  Proof. by unseal. Qed.
  Lemma fracPred_at_sep_2 π1 π2 P Q : P π1  Q π2  (P  Q) (π1  π2).
  Proof.
    rewrite fracPred_at_sep -(bi.exist_intro π1) -(bi.exist_intro π2).
    by rewrite bi.pure_True // left_id.
  Qed.
  Lemma fracPred_at_wand π P Q : (P - Q) π   π', P π' - Q (π  π').
  Proof. by unseal. Qed.
  Lemma fracPred_at_persistently π P : (<pers> P) π  <pers> (P ε).
  Proof. by unseal. Qed.
  Lemma fracPred_at_affinely π P : (<affine> P) π  <affine> ( π = ε   P ε).
  Proof.
    rewrite /bi_affinely fracPred_at_and fracPred_at_emp -assoc.
    rewrite bi.pure_alt !bi.and_exist_r !bi.and_exist_l.
    by apply bi.exist_proper=> ->.
  Qed.
  Lemma fracPred_at_intuitionistically π P : ( P) π   ( π = ε   P ε).
  Proof.
    rewrite fracPred_at_affinely fracPred_at_persistently.
    by rewrite -{1}bi.persistently_pure -bi.persistently_and.
  Qed.
  Lemma fracPred_at_absorbingly π P : (<absorb> P) π  <absorb> ( π',  π'  π   P π').
  Proof.
    rewrite /bi_absorbingly fracPred_at_sep. apply (anti_symm _).
    - apply bi.exist_elim=> π1; apply bi.exist_elim=> π2; apply bi.pure_elim_l=> ->.
      rewrite -(bi.exist_intro π2) fracPred_at_pure.
      rewrite bi.pure_True; last apply: cmra_included_r. by rewrite left_id.
    - rewrite bi.sep_exist_l. apply bi.exist_elim=> π'.
      rewrite bi.persistent_and_affinely_sep_l assoc (comm _ True%I) -assoc.
      rewrite -bi.persistent_and_affinely_sep_l.
      apply bi.pure_elim_l=> -[π'' /leibniz_equiv_iff->].
      rewrite -(bi.exist_intro π'') -(bi.exist_intro π') comm_L bi.pure_True //.
      by rewrite left_id fracPred_at_pure.
  Qed.

  (** Objective *)
  Global Instance Objective_proper : Proper (() ==> iff) (@FObjective PROP).
  Proof. rewrite /FObjective. intros P P' HP. by setoid_rewrite HP. Qed.
  Global Instance embed_objective (P : PROP) : @FObjective PROP P.
  Proof. intros ??. by unseal. Qed.
  Global Instance pure_objective φ : @FObjective PROP ⌜φ⌝.
  Proof. intros ??. by unseal. Qed.

  Global Instance and_objective P Q : FObjective P  FObjective Q  FObjective (P  Q).
  Proof. intros ?? π π'. unseal. by f_equiv. Qed.
  Global Instance or_objective P Q : FObjective P  FObjective Q  FObjective (P  Q).
  Proof. intros ?? π π'. unseal. by f_equiv. Qed.
  Global Instance impl_objective P Q : FObjective P  FObjective Q  FObjective (P  Q).
  Proof. intros ?? π π'. unseal. f_equiv; by simpl. Qed.
  Global Instance forall_objective {A} (Φ : A  fracPred) :
    ( x : A, FObjective (Φ x))  FObjective ( x, Φ x).
  Proof. intros H π π'. unseal. f_equiv=> x /=. apply H. Qed.
  Global Instance exist_objective {A} (Φ : A  fracPred) :
    ( x : A, FObjective (Φ x))  FObjective ( x, Φ x).
  Proof. intros H π π'. unseal. f_equiv=> x /=. apply H. Qed.

  Global Instance sep_objective P Q : FObjective P  FObjective Q  FObjective (P  Q).
  Proof.
    intros ?? π π'. unseal. apply bi.exist_elim=> π1; apply bi.exist_elim=> π2.
    rewrite -(bi.exist_intro π') -(bi.exist_intro ε) right_id_L.
    f_equiv; [auto using bi.pure_intro|by f_equiv].
  Qed.
  Global Instance wand_objective P Q : FObjective Q  FObjective (P - Q).
  Proof. intros ? π π'. unseal. f_equiv=> π''. by f_equiv. Qed.

  Global Instance persistently_objective P : FObjective (<pers> P).
  Proof. intros π π'. by unseal. Qed.
  (** Not an instance, it will blow up instance search *)
  Lemma persistent_objective P : Absorbing P  Persistent P  FObjective P.
  Proof. intros ??. rewrite -(bi.persistent_persistently P). apply _. Qed.
  Global Instance absorbingly_objective P : FObjective P  FObjective (<absorb> P).
  Proof. rewrite /bi_absorbingly. apply _. Qed.
  Global Instance persistently_if_objective P p : FObjective P  FObjective (<pers>?p P).
  Proof. rewrite /bi_persistently_if. destruct p; apply _. Qed.

  (** Big op *)
  Global Instance fracPred_at_monoid_and_homomorphism π :
    MonoidHomomorphism bi_and bi_and () (flip fracPred_at π).
  Proof. split; [split|]; try apply _. apply fracPred_at_and. apply fracPred_at_pure. Qed.
  Global Instance fracPred_at_monoid_or_homomorphism π :
    MonoidHomomorphism bi_or bi_or () (flip fracPred_at π).
  Proof. split; [split|]; try apply _. apply fracPred_at_or. apply fracPred_at_pure. Qed.

  (** BUpd *)
  Definition fracPred_bupd_def `{BiBUpd PROP} (P : fracPred) : fracPred :=
    FracPred (λ π, |==> P π)%I.
  Definition fracPred_bupd_aux `{BiBUpd PROP} : seal fracPred_bupd_def. by eexists. Qed.
  Definition fracPred_bupd `{BiBUpd PROP} : BUpd _ := fracPred_bupd_aux.(unseal).
  Definition fracPred_bupd_eq `{BiBUpd PROP} : @bupd _ fracPred_bupd = _ :=
    fracPred_bupd_aux.(seal_eq).

  Lemma fracPred_bupd_mixin `{BiBUpd PROP} : BiBUpdMixin fracPredI fracPred_bupd.
  Proof.
    split; rewrite fracPred_bupd_eq.
    - split=>/= π. solve_proper.
    - intros P. split=>/= π. apply bupd_intro.
    - intros P Q HPQ. split=>/= π. by rewrite HPQ.
    - intros P. split=>/= π. apply bupd_trans.
    - intros P Q. split=> π /=. unseal.
      apply bi.exist_elim=> π1. apply bi.exist_elim=> π2. apply bi.pure_elim_l=> ->.
      rewrite -(bi.exist_intro π1) -(bi.exist_intro π2) bi.pure_True // left_id.
      apply bupd_frame_r.
  Qed.
  Global Instance fracPred_bi_bupd `{BiBUpd PROP} : BiBUpd fracPredI :=
    {| bi_bupd_mixin := fracPred_bupd_mixin |}.

  Lemma fracPred_at_bupd `{BiBUpd PROP} π P : (|==> P) π  |==> P π.
  Proof. by rewrite fracPred_bupd_eq. Qed.

  Global Instance fracPred_bi_embed_bupd `{BiBUpd PROP} : BiEmbedBUpd PROP fracPredI.
  Proof. split. split=>π /=. by rewrite fracPred_at_bupd !fracPred_at_embed. Qed.

  Global Instance bupd_objective `{BiBUpd PROP} P :
    FObjective P  FObjective (|==> P).
  Proof. intros ? π π'. rewrite fracPred_bupd_eq /=. by f_equiv. Qed.

  Lemma fracPred_affinely_bupd `{BiBUpd PROP, BiAffine PROP} P :
    (<affine> |==> P)  |==> <affine> P.
  Proof.
    split=> π. rewrite fracPred_at_affinely !fracPred_at_bupd fracPred_at_affinely.
    rewrite !bi.affine_affinely. apply bi.pure_elim_l=> ->.
    by rewrite bi.pure_True // left_id. 
  Qed.
End bi_facts.

Hint Immediate persistent_objective : typeclass_instances.

Section sbi_facts.
  Context {PROP : sbi}.
  Local Notation fracPred := (fracPred PROP).
  Local Notation fracPredSI := (fracPredSI PROP).
  Implicit Types P Q : fracPred.

  Global Instance fracPred_sbi_embed : SbiEmbed PROP fracPredSI.
  Proof.
    split; unseal=> //. intros ? P Q.
    apply (@bi.f_equiv _ _ _ (λ P, fracPred_at P inhabitant)); solve_proper.
  Qed.

  Lemma fracPred_internal_eq_unfold : @sbi_internal_eq fracPredSI = λ A x y,  x  y %I.
  Proof. by unseal. Qed.

  (** Unfolding lemmas *)
  Lemma fracPred_at_internal_eq {A : ofeT} π (a b : A) :
    @fracPred_at PROP (a  b) π  a  b.
  Proof. rewrite fracPred_internal_eq_unfold. by apply fracPred_at_embed. Qed.
  Lemma fracPred_at_later π P : ( P) π   P π.
  Proof. by unseal. Qed.

  Lemma fracPred_at_except_0 π P : ( P) π   P π.
  Proof. by unseal. Qed.

  Lemma fracPred_equivI {PROP' : sbi} P Q :
    sbi_internal_eq (PROP:=PROP') P Q   π, P π  Q π.
  Proof.
    apply bi.equiv_spec. split.
    - apply bi.forall_intro=>?. apply (bi.f_equiv (flip fracPred_at _)).
    - rewrite {2}(_ : P = FracPred P); last by destruct P.
      rewrite {2}(_ : Q = FracPred Q); last by destruct Q.
507
      by rewrite -(@bi.f_equiv _ _ _ (@FracPred PROP : (_ -d> _)  fracPred)
Robbert Krebbers's avatar
Robbert Krebbers committed
508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580
        ltac:(by constructor)) -bi.fun_ext.
  Qed.

  Lemma fracPred_at_timeless_alt P : Timeless P   π, Timeless (P π).
  Proof.
    rewrite /Timeless; split.
    - intros [HP] π. by rewrite -fracPred_at_later -fracPred_at_except_0.
    - intros HP; split=> π. by rewrite fracPred_at_later fracPred_at_except_0.
  Qed.
  Global Instance fracPred_emp_timeless :
    Timeless (@bi_emp PROP)  Timeless (@bi_emp (fracPredI PROP)).
  Proof. intros. apply fracPred_at_timeless_alt. unseal. apply _. Qed.
  Global Instance fracPred_at_timeless P π : Timeless P  Timeless (P π).
  Proof. rewrite fracPred_at_timeless_alt. auto. Qed.

  (** Objective  *)
  Global Instance internal_eq_objective {A : ofeT} (x y : A) :
    @FObjective PROP (x  y).
  Proof. intros ??. by unseal. Qed.

  Global Instance later_objective P : FObjective P  FObjective ( P).
  Proof. intros ? π π'. unseal. by f_equiv. Qed.
  Global Instance laterN_objective P n : FObjective P  FObjective (^n P).
  Proof. induction n; apply _. Qed.
  Global Instance except0_objective P : FObjective P  FObjective ( P).
  Proof. rewrite /sbi_except_0. apply _. Qed.

  (** Plainly *)
  Definition fracPred_plainly_def `{BiPlainly PROP} P : fracPred :=
    FracPred (λ _,  P ε)%I.
  Definition fracPred_plainly_aux `{BiPlainly PROP} : seal fracPred_plainly_def. by eexists. Qed.
  Definition fracPred_plainly `{BiPlainly PROP} : Plainly _ := fracPred_plainly_aux.(unseal).
  Definition fracPred_plainly_eq `{BiPlainly PROP} :
    @plainly _ fracPred_plainly = _ := fracPred_plainly_aux.(seal_eq).

  Lemma fracPred_plainly_mixin `{BiPlainly PROP} : BiPlainlyMixin fracPredSI fracPred_plainly.
  Proof.
    split; rewrite fracPred_plainly_eq; try unseal.
    - by (split=> ? /=; repeat f_equiv).
    - intros P Q [?]. split=> π /=. by f_equiv.
    - intros P. split=> π /=. by rewrite plainly_elim_persistently.
    - intros P. split=> π /=. by rewrite -plainly_idemp_2.
    - intros A Ψ. split=> π /=. by rewrite plainly_forall.
    - intros P Q. split=> π /=. by rewrite persistently_impl_plainly.
    - intros P. split=> π /=. by rewrite plainly_impl_plainly.
    - intros P. split=> π /=. rewrite bi.pure_True // bi.affinely_True_emp.
      by rewrite bi.affinely_emp -plainly_emp_intro.
    - intros P Q. split=> π /=. apply bi.exist_elim=> π1; apply bi.exist_elim=> π2.
      apply bi.pure_elim_l=> _. by rewrite bi.sep_elim_l.
    - intros P Q. split=> π /=. rewrite fracPred_equivI. apply bi.forall_intro=> π'.
      by rewrite -prop_ext !(bi.forall_elim π') left_id_L.
    - intros P. split=> π /=. by rewrite -later_plainly_1.
    - intros P. split=> π /=. by rewrite -later_plainly_2.
  Qed.
  Global Instance fracPred_bi_plainly `{BiPlainly PROP} : BiPlainly fracPredSI :=
    {| bi_plainly_mixin := fracPred_plainly_mixin |}.

  Global Instance fracPred_bi_plainly_exist `{BiPlainly PROP} :
    BiPlainlyExist PROP  BiPlainlyExist fracPredSI.
  Proof.
    split=> π /=. rewrite fracPred_plainly_eq /= !fracPred_at_exist.
    by rewrite -plainly_exist_1.
  Qed.

  Global Instance fracPred_bi_embed_plainly `{BiPlainly PROP} :
    BiEmbedPlainly PROP fracPredSI.
  Proof. constructor=> P. by rewrite fracPred_plainly_eq; unseal. Qed.

  Global Instance plainly_objective `{BiPlainly PROP} P : FObjective ( P).
  Proof. intros π π'. by rewrite fracPred_plainly_eq. Qed.
  Lemma fracPred_at_plainly `{BiPlainly PROP} π P : ( P) π   (P ε).
  Proof. by rewrite fracPred_plainly_eq. Qed.
End sbi_facts.