upred.v 9.97 KB
Newer Older
1
From iris.algebra Require Export cmra.
2
Set Default Proof Using "Type".
3

Ralf Jung's avatar
Ralf Jung committed
4 5 6 7 8
(** 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
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
(* 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
27 28 29 30 31
     ∀ 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
32 33 34 35 36 37 38 39 40 41 42 43
     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.
 *)

44 45
Record uPred (M : ucmraT) : Type := IProp {
  uPred_holds :> nat  M  Prop;
46

47 48
  uPred_mono n1 n2 x1 x2 :
    uPred_holds n1 x1  x1 {n1} x2  n2  n1  uPred_holds n2 x2
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
}.
Arguments uPred_holds {_} _ _ _ : simpl never.
Add Printing Constructor uPred.
Instance: Params (@uPred_holds) 3.

Delimit Scope uPred_scope with I.
Bind Scope uPred_scope with uPred.
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'.
67
  Definition uPred_ofe_mixin : OfeMixin (uPred M).
68 69 70 71 72 73 74 75 76 77 78 79
  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.
80 81 82
  Canonical Structure uPredC : ofeT := OfeT (uPred M) uPred_ofe_mixin.

  Program Definition uPred_compl : Compl uPredC := λ c,
83 84
    {| uPred_holds n x :=  n', n'  n  {n'}x  c n' n' x |}.
  Next Obligation.
85 86 87
    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.
88 89 90
  Qed.
  Global Program Instance uPred_cofe : Cofe uPredC := {| compl := uPred_compl |}.
  Next Obligation.
91 92
    intros n c; split=>i x Hin Hv.
    etrans; [|by symmetry; apply (chain_cauchy c i n)]. split=>H; [by apply H|].
93
    repeat intro. apply (chain_cauchy c n' i)=>//. by eapply uPred_mono.
94
  Qed.
95 96 97 98 99 100 101 102 103 104 105 106 107
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.
108
  intros [Hne] ???. eapply Hne; try done. eauto using uPred_mono, cmra_validN_le.
109 110
Qed.

111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
(* 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.
Qed.

128 129
(** functor *)
Program Definition uPred_map {M1 M2 : ucmraT} (f : M2 -n> M1)
130
  `{!CmraMorphism f} (P : uPred M1) :
131
  uPred M2 := {| uPred_holds n x := P n (f x) |}.
132
Next Obligation. naive_solver eauto using uPred_mono, cmra_morphism_monotoneN. Qed.
133 134

Instance uPred_map_ne {M1 M2 : ucmraT} (f : M2 -n> M1)
135
  `{!CmraMorphism f} n : Proper (dist n ==> dist n) (uPred_map f).
136 137
Proof.
  intros x1 x2 Hx; split=> n' y ??.
138
  split; apply Hx; auto using cmra_morphism_validN.
139 140 141 142
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)
143
    `{!CmraMorphism f, !CmraMorphism g} (P : uPred M3):
144 145 146
  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)
147
      `{!CmraMorphism f} `{!CmraMorphism g}:
148 149
  ( 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.
150
Definition uPredC_map {M1 M2 : ucmraT} (f : M2 -n> M1) `{!CmraMorphism f} :
151 152
  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)
153
    `{!CmraMorphism f, !CmraMorphism g} n :
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
  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.
180 181
  intros ? A1 A2 B1 B2 n P Q HPQ. apply uPredC_map_ne, urFunctor_contractive.
  destruct n; split; by apply HPQ.
182 183 184 185 186 187 188 189
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 }.
Hint Extern 0 (uPred_entails _ _) => reflexivity.
Instance uPred_entails_rewrite_relation M : RewriteRelation (@uPred_entails M).

190
Hint Resolve uPred_mono : uPred_def.
191 192 193

(** Notations *)
Notation "P ⊢ Q" := (uPred_entails P%I Q%I)
Robbert Krebbers's avatar
Robbert Krebbers committed
194 195
  (at level 99, Q at level 200, right associativity) : stdpp_scope.
Notation "(⊢)" := uPred_entails (only parsing) : stdpp_scope.
196
Notation "P ⊣⊢ Q" := (equiv (A:=uPred _) P%I Q%I)
Robbert Krebbers's avatar
Robbert Krebbers committed
197 198
  (at level 95, no associativity) : stdpp_scope.
Notation "(⊣⊢)" := (equiv (A:=uPred _)) (only parsing) : stdpp_scope.
199

200
Module uPred.
201 202 203 204
Section entails.
Context {M : ucmraT}.
Implicit Types P Q : uPred M.

Robbert Krebbers's avatar
Robbert Krebbers committed
205
Global Instance entails_po : PreOrder (@uPred_entails M).
206 207 208 209 210
Proof.
  split.
  - by intros P; split=> x i.
  - by intros P Q Q' HP HQ; split=> x i ??; apply HQ, HP.
Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
211
Global Instance entails_anti_sym : AntiSymm () (@uPred_entails M).
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
Proof. intros P Q HPQ HQP; split=> x n; by split; [apply HPQ|apply HQP]. Qed.

Lemma equiv_spec P Q : (P  Q)  (P  Q)  (Q  P).
Proof.
  split; [|by intros [??]; apply (anti_symm ())].
  intros HPQ; split; split=> x i; apply HPQ.
Qed.
Lemma equiv_entails P Q : (P  Q)  (P  Q).
Proof. apply equiv_spec. Qed.
Lemma equiv_entails_sym P Q : (Q  P)  (P  Q).
Proof. apply equiv_spec. Qed.
Global Instance entails_proper :
  Proper (() ==> () ==> iff) (() : relation (uPred M)).
Proof.
  move => P1 P2 /equiv_spec [HP1 HP2] Q1 Q2 /equiv_spec [HQ1 HQ2]; split; intros.
  - by trans P1; [|trans Q1].
  - by trans P2; [|trans Q2].
Qed.
Lemma entails_equiv_l (P Q R : uPred M) : (P  Q)  (Q  R)  (P  R).
Proof. by intros ->. Qed.
Lemma entails_equiv_r (P Q R : uPred M) : (P  Q)  (Q  R)  (P  R).
Proof. by intros ? <-. Qed.
234

Robbert Krebbers's avatar
Robbert Krebbers committed
235 236
Lemma entails_lim (cP cQ : chain (uPredC M)) :
  ( n, cP n  cQ n)  compl cP  compl cQ.
237
Proof.
Robbert Krebbers's avatar
Robbert Krebbers committed
238
  intros Hlim; split=> n m ? HP.
239 240 241
  eapply uPred_holds_ne, Hlim, HP; eauto using conv_compl.
Qed.

Robbert Krebbers's avatar
Robbert Krebbers committed
242 243 244
Lemma limit_preserving_entails `{Cofe A} (Φ Ψ : A  uPred M) :
  NonExpansive Φ  NonExpansive Ψ  LimitPreserving (λ x, Φ x  Ψ x).
Proof. intros HΦ HΨ c Hc. rewrite -!compl_chain_map /=. by apply entails_lim. Qed.
245
End entails.
246
End uPred.