upred.v 54.1 KB
Newer Older
1
From iris.algebra Require Export cmra.
2 3
Local Hint Extern 1 (_  _) => etrans; [eassumption|].
Local Hint Extern 1 (_  _) => etrans; [|eassumption].
Robbert Krebbers's avatar
Robbert Krebbers committed
4 5
Local Hint Extern 10 (_  _) => omega.

6
Record uPred (M : ucmraT) : Type := IProp {
Robbert Krebbers's avatar
Robbert Krebbers committed
7
  uPred_holds :> nat  M  Prop;
8
  uPred_mono n x1 x2 : uPred_holds n x1  x1 {n} x2  uPred_holds n x2;
9
  uPred_closed n1 n2 x : uPred_holds n1 x  n2  n1  {n2} x  uPred_holds n2 x
Robbert Krebbers's avatar
Robbert Krebbers committed
10
}.
11
Arguments uPred_holds {_} _ _ _ : simpl never.
Robbert Krebbers's avatar
Robbert Krebbers committed
12 13
Add Printing Constructor uPred.
Instance: Params (@uPred_holds) 3.
Robbert Krebbers's avatar
Robbert Krebbers committed
14

15 16 17 18
Delimit Scope uPred_scope with I.
Bind Scope uPred_scope with uPred.
Arguments uPred_holds {_} _%I _ _.

19
Section cofe.
20
  Context {M : ucmraT}.
21 22 23 24 25 26 27

  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'.
28
  Program Instance uPred_compl : Compl (uPred M) := λ c,
29
    {| uPred_holds n x := c n n x |}.
30
  Next Obligation. naive_solver eauto using uPred_mono. Qed.
31
  Next Obligation.
32 33
    intros c n1 n2 x ???; simpl in *.
    apply (chain_cauchy c n2 n1); eauto using uPred_closed.
34 35 36 37
  Qed.
  Definition uPred_cofe_mixin : CofeMixin (uPred M).
  Proof.
    split.
38 39 40
    - intros P Q; split.
      + by intros HPQ n; split=> i x ??; apply HPQ.
      + intros HPQ; split=> n x ?; apply HPQ with n; auto.
41
    - intros n; split.
42 43 44 45 46
      + 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.
47
    - intros n c; split=>i x ??; symmetry; apply (chain_cauchy c i n); auto.
48
  Qed.
49
  Canonical Structure uPredC : cofeT := CofeT (uPred M) uPred_cofe_mixin.
50 51 52
End cofe.
Arguments uPredC : clear implicits.

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

Robbert Krebbers's avatar
Robbert Krebbers committed
60
(** functor *)
61
Program Definition uPred_map {M1 M2 : ucmraT} (f : M2 -n> M1)
62 63
  `{!CMRAMonotone f} (P : uPred M1) :
  uPred M2 := {| uPred_holds n x := P n (f x) |}.
64
Next Obligation. naive_solver eauto using uPred_mono, includedN_preserving. Qed.
65 66
Next Obligation. naive_solver eauto using uPred_closed, validN_preserving. Qed.

67
Instance uPred_map_ne {M1 M2 : ucmraT} (f : M2 -n> M1)
Robbert Krebbers's avatar
Robbert Krebbers committed
68
  `{!CMRAMonotone f} n : Proper (dist n ==> dist n) (uPred_map f).
Robbert Krebbers's avatar
Robbert Krebbers committed
69
Proof.
70 71
  intros x1 x2 Hx; split=> n' y ??.
  split; apply Hx; auto using validN_preserving.
Robbert Krebbers's avatar
Robbert Krebbers committed
72
Qed.
73
Lemma uPred_map_id {M : ucmraT} (P : uPred M): uPred_map cid P  P.
74
Proof. by split=> n x ?. Qed.
75
Lemma uPred_map_compose {M1 M2 M3 : ucmraT} (f : M1 -n> M2) (g : M2 -n> M3)
Robbert Krebbers's avatar
Robbert Krebbers committed
76
    `{!CMRAMonotone f, !CMRAMonotone g} (P : uPred M3):
77
  uPred_map (g  f) P  uPred_map f (uPred_map g P).
78
Proof. by split=> n x Hx. Qed.
79
Lemma uPred_map_ext {M1 M2 : ucmraT} (f g : M1 -n> M2)
80
      `{!CMRAMonotone f} `{!CMRAMonotone g}:
81
  ( x, f x  g x)   x, uPred_map f x  uPred_map g x.
82
Proof. intros Hf P; split=> n x Hx /=; by rewrite /uPred_holds /= Hf. Qed.
83
Definition uPredC_map {M1 M2 : ucmraT} (f : M2 -n> M1) `{!CMRAMonotone f} :
Robbert Krebbers's avatar
Robbert Krebbers committed
84
  uPredC M1 -n> uPredC M2 := CofeMor (uPred_map f : uPredC M1  uPredC M2).
85
Lemma uPredC_map_ne {M1 M2 : ucmraT} (f g : M2 -n> M1)
86
    `{!CMRAMonotone f, !CMRAMonotone g} n :
87
  f {n} g  uPredC_map f {n} uPredC_map g.
Robbert Krebbers's avatar
Robbert Krebbers committed
88
Proof.
89
  by intros Hfg P; split=> n' y ??;
90
    rewrite /uPred_holds /= (dist_le _ _ _ _(Hfg y)); last lia.
Robbert Krebbers's avatar
Robbert Krebbers committed
91
Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
92

93 94 95
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))
96
|}.
97 98
Next Obligation.
  intros F A1 A2 B1 B2 n P Q HPQ.
99
  apply uPredC_map_ne, urFunctor_ne; split; by apply HPQ.
100
Qed.
101 102
Next Obligation.
  intros F A B P; simpl. rewrite -{2}(uPred_map_id P).
103
  apply uPred_map_ext=>y. by rewrite urFunctor_id.
104 105 106
Qed.
Next Obligation.
  intros F A1 A2 A3 B1 B2 B3 f g f' g' P; simpl. rewrite -uPred_map_compose.
107
  apply uPred_map_ext=>y; apply urFunctor_compose.
108 109
Qed.

110
Instance uPredCF_contractive F :
111
  urFunctorContractive F  cFunctorContractive (uPredCF F).
112 113
Proof.
  intros ? A1 A2 B1 B2 n P Q HPQ.
114
  apply uPredC_map_ne, urFunctor_contractive=> i ?; split; by apply HPQ.
115 116
Qed.

Robbert Krebbers's avatar
Robbert Krebbers committed
117
(** logical entailement *)
118 119
Inductive uPred_entails {M} (P Q : uPred M) : Prop :=
  { uPred_in_entails :  n x, {n} x  P n x  Q n x }.
120
Hint Extern 0 (uPred_entails _ _) => reflexivity.
121
Instance uPred_entails_rewrite_relation M : RewriteRelation (@uPred_entails M).
Robbert Krebbers's avatar
Robbert Krebbers committed
122

123
Hint Resolve uPred_mono uPred_closed : uPred_def.
124

Robbert Krebbers's avatar
Robbert Krebbers committed
125
(** logical connectives *)
126
Program Definition uPred_const_def {M} (φ : Prop) : uPred M :=
127
  {| uPred_holds n x := φ |}.
Robbert Krebbers's avatar
Robbert Krebbers committed
128
Solve Obligations with done.
129 130 131 132 133
Definition uPred_const_aux : { x | x = @uPred_const_def }. by eexists. Qed.
Definition uPred_const {M} := proj1_sig uPred_const_aux M.
Definition uPred_const_eq :
  @uPred_const = @uPred_const_def := proj2_sig uPred_const_aux.

134
Instance uPred_inhabited M : Inhabited (uPred M) := populate (uPred_const True).
Robbert Krebbers's avatar
Robbert Krebbers committed
135

136
Program Definition uPred_and_def {M} (P Q : uPred M) : uPred M :=
Robbert Krebbers's avatar
Robbert Krebbers committed
137
  {| uPred_holds n x := P n x  Q n x |}.
138
Solve Obligations with naive_solver eauto 2 with uPred_def.
139 140 141 142 143
Definition uPred_and_aux : { x | x = @uPred_and_def }. by eexists. Qed.
Definition uPred_and {M} := proj1_sig uPred_and_aux M.
Definition uPred_and_eq: @uPred_and = @uPred_and_def := proj2_sig uPred_and_aux.

Program Definition uPred_or_def {M} (P Q : uPred M) : uPred M :=
Robbert Krebbers's avatar
Robbert Krebbers committed
144
  {| uPred_holds n x := P n x  Q n x |}.
145
Solve Obligations with naive_solver eauto 2 with uPred_def.
146 147 148 149 150
Definition uPred_or_aux : { x | x = @uPred_or_def }. by eexists. Qed.
Definition uPred_or {M} := proj1_sig uPred_or_aux M.
Definition uPred_or_eq: @uPred_or = @uPred_or_def := proj2_sig uPred_or_aux.

Program Definition uPred_impl_def {M} (P Q : uPred M) : uPred M :=
Robbert Krebbers's avatar
Robbert Krebbers committed
151
  {| uPred_holds n x :=  n' x',
Robbert Krebbers's avatar
Robbert Krebbers committed
152
       x  x'  n'  n  {n'} x'  P n' x'  Q n' x' |}.
Robbert Krebbers's avatar
Robbert Krebbers committed
153
Next Obligation.
154 155 156
  intros M P Q n1 x1 x1' HPQ [x2 Hx1'] n2 x3 [x4 Hx3] ?; simpl in *.
  rewrite Hx3 (dist_le _ _ _ _ Hx1'); auto. intros ??.
  eapply HPQ; auto. exists (x2  x4); by rewrite assoc.
Robbert Krebbers's avatar
Robbert Krebbers committed
157
Qed.
158
Next Obligation. intros M P Q [|n1] [|n2] x; auto with lia. Qed.
159 160 161 162
Definition uPred_impl_aux : { x | x = @uPred_impl_def }. by eexists. Qed.
Definition uPred_impl {M} := proj1_sig uPred_impl_aux M.
Definition uPred_impl_eq :
  @uPred_impl = @uPred_impl_def := proj2_sig uPred_impl_aux.
Robbert Krebbers's avatar
Robbert Krebbers committed
163

164
Program Definition uPred_forall_def {M A} (Ψ : A  uPred M) : uPred M :=
165
  {| uPred_holds n x :=  a, Ψ a n x |}.
166
Solve Obligations with naive_solver eauto 2 with uPred_def.
167 168 169 170 171 172
Definition uPred_forall_aux : { x | x = @uPred_forall_def }. by eexists. Qed.
Definition uPred_forall {M A} := proj1_sig uPred_forall_aux M A.
Definition uPred_forall_eq :
  @uPred_forall = @uPred_forall_def := proj2_sig uPred_forall_aux.

Program Definition uPred_exist_def {M A} (Ψ : A  uPred M) : uPred M :=
173
  {| uPred_holds n x :=  a, Ψ a n x |}.
174
Solve Obligations with naive_solver eauto 2 with uPred_def.
175 176 177
Definition uPred_exist_aux : { x | x = @uPred_exist_def }. by eexists. Qed.
Definition uPred_exist {M A} := proj1_sig uPred_exist_aux M A.
Definition uPred_exist_eq: @uPred_exist = @uPred_exist_def := proj2_sig uPred_exist_aux.
Robbert Krebbers's avatar
Robbert Krebbers committed
178

179
Program Definition uPred_eq_def {M} {A : cofeT} (a1 a2 : A) : uPred M :=
180
  {| uPred_holds n x := a1 {n} a2 |}.
181
Solve Obligations with naive_solver eauto 2 using (dist_le (A:=A)).
182 183 184
Definition uPred_eq_aux : { x | x = @uPred_eq_def }. by eexists. Qed.
Definition uPred_eq {M A} := proj1_sig uPred_eq_aux M A.
Definition uPred_eq_eq: @uPred_eq = @uPred_eq_def := proj2_sig uPred_eq_aux.
Robbert Krebbers's avatar
Robbert Krebbers committed
185

186
Program Definition uPred_sep_def {M} (P Q : uPred M) : uPred M :=
187
  {| uPred_holds n x :=  x1 x2, x {n} x1  x2  P n x1  Q n x2 |}.
Robbert Krebbers's avatar
Robbert Krebbers committed
188
Next Obligation.
189
  intros M P Q n x y (x1&x2&Hx&?&?) [z Hy].
190
  exists x1, (x2  z); split_and?; eauto using uPred_mono, cmra_includedN_l.
191 192 193 194 195 196
  by rewrite Hy Hx assoc.
Qed.
Next Obligation.
  intros M P Q n1 n2 x (x1&x2&Hx&?&?) ?; rewrite {1}(dist_le _ _ _ _ Hx) // =>?.
  exists x1, x2; cofe_subst; split_and!;
    eauto using dist_le, uPred_closed, cmra_validN_op_l, cmra_validN_op_r.
Robbert Krebbers's avatar
Robbert Krebbers committed
197
Qed.
198 199 200
Definition uPred_sep_aux : { x | x = @uPred_sep_def }. by eexists. Qed.
Definition uPred_sep {M} := proj1_sig uPred_sep_aux M.
Definition uPred_sep_eq: @uPred_sep = @uPred_sep_def := proj2_sig uPred_sep_aux.
Robbert Krebbers's avatar
Robbert Krebbers committed
201

202
Program Definition uPred_wand_def {M} (P Q : uPred M) : uPred M :=
Robbert Krebbers's avatar
Robbert Krebbers committed
203
  {| uPred_holds n x :=  n' x',
Robbert Krebbers's avatar
Robbert Krebbers committed
204
       n'  n  {n'} (x  x')  P n' x'  Q n' (x  x') |}.
Robbert Krebbers's avatar
Robbert Krebbers committed
205
Next Obligation.
206
  intros M P Q n x1 x1' HPQ ? n3 x3 ???; simpl in *.
207
  apply uPred_mono with (x1  x3);
208
    eauto using cmra_validN_includedN, cmra_preservingN_r, cmra_includedN_le.
Robbert Krebbers's avatar
Robbert Krebbers committed
209
Qed.
210
Next Obligation. naive_solver. Qed.
211 212 213 214
Definition uPred_wand_aux : { x | x = @uPred_wand_def }. by eexists. Qed.
Definition uPred_wand {M} := proj1_sig uPred_wand_aux M.
Definition uPred_wand_eq :
  @uPred_wand = @uPred_wand_def := proj2_sig uPred_wand_aux.
Robbert Krebbers's avatar
Robbert Krebbers committed
215

216
Program Definition uPred_always_def {M} (P : uPred M) : uPred M :=
Ralf Jung's avatar
Ralf Jung committed
217
  {| uPred_holds n x := P n (core x) |}.
218
Next Obligation. naive_solver eauto using uPred_mono, cmra_core_preservingN. Qed.
219
Next Obligation. naive_solver eauto using uPred_closed, cmra_core_validN. Qed.
220 221 222 223 224 225
Definition uPred_always_aux : { x | x = @uPred_always_def }. by eexists. Qed.
Definition uPred_always {M} := proj1_sig uPred_always_aux M.
Definition uPred_always_eq :
  @uPred_always = @uPred_always_def := proj2_sig uPred_always_aux.

Program Definition uPred_later_def {M} (P : uPred M) : uPred M :=
226
  {| uPred_holds n x := match n return _ with 0 => True | S n' => P n' x end |}.
227 228 229
Next Obligation.
  intros M P [|n] x1 x2; eauto using uPred_mono, cmra_includedN_S.
Qed.
230
Next Obligation.
231
  intros M P [|n1] [|n2] x; eauto using uPred_closed, cmra_validN_S with lia.
232
Qed.
233 234 235 236
Definition uPred_later_aux : { x | x = @uPred_later_def }. by eexists. Qed.
Definition uPred_later {M} := proj1_sig uPred_later_aux M.
Definition uPred_later_eq :
  @uPred_later = @uPred_later_def := proj2_sig uPred_later_aux.
Robbert Krebbers's avatar
Robbert Krebbers committed
237

238
Program Definition uPred_ownM_def {M : ucmraT} (a : M) : uPred M :=
239
  {| uPred_holds n x := a {n} x |}.
Robbert Krebbers's avatar
Robbert Krebbers committed
240
Next Obligation.
241 242
  intros M a n x1 x [a' Hx1] [x2 ->].
  exists (a'  x2). by rewrite (assoc op) Hx1.
Robbert Krebbers's avatar
Robbert Krebbers committed
243
Qed.
244
Next Obligation. naive_solver eauto using cmra_includedN_le. Qed.
245 246 247 248 249
Definition uPred_ownM_aux : { x | x = @uPred_ownM_def }. by eexists. Qed.
Definition uPred_ownM {M} := proj1_sig uPred_ownM_aux M.
Definition uPred_ownM_eq :
  @uPred_ownM = @uPred_ownM_def := proj2_sig uPred_ownM_aux.

250
Program Definition uPred_valid_def {M : ucmraT} {A : cmraT} (a : A) : uPred M :=
Robbert Krebbers's avatar
Robbert Krebbers committed
251
  {| uPred_holds n x := {n} a |}.
252
Solve Obligations with naive_solver eauto 2 using cmra_validN_le.
253 254 255 256
Definition uPred_valid_aux : { x | x = @uPred_valid_def }. by eexists. Qed.
Definition uPred_valid {M A} := proj1_sig uPred_valid_aux M A.
Definition uPred_valid_eq :
  @uPred_valid = @uPred_valid_def := proj2_sig uPred_valid_aux.
Robbert Krebbers's avatar
Robbert Krebbers committed
257

258 259 260 261
Notation "P ⊢ Q" := (uPred_entails P%I Q%I) (at level 70) : C_scope.
Notation "(⊢)" := uPred_entails (only parsing) : C_scope.
Notation "P ⊣⊢ Q" := (equiv (A:=uPred _) P%I Q%I) (at level 70) : C_scope.
Notation "(⊣⊢)" := (equiv (A:=uPred _)) (only parsing) : C_scope.
262 263
Notation "■ φ" := (uPred_const φ%C%type)
  (at level 20, right associativity) : uPred_scope.
Ralf Jung's avatar
Ralf Jung committed
264
Notation "x = y" := (uPred_const (x%C%type = y%C%type)) : uPred_scope.
265
Notation "x ⊥ y" := (uPred_const (x%C%type  y%C%type)) : uPred_scope.
Robbert Krebbers's avatar
Robbert Krebbers committed
266 267 268
Notation "'False'" := (uPred_const False) : uPred_scope.
Notation "'True'" := (uPred_const True) : uPred_scope.
Infix "∧" := uPred_and : uPred_scope.
269
Notation "(∧)" := uPred_and (only parsing) : uPred_scope.
Robbert Krebbers's avatar
Robbert Krebbers committed
270
Infix "∨" := uPred_or : uPred_scope.
271
Notation "(∨)" := uPred_or (only parsing) : uPred_scope.
Robbert Krebbers's avatar
Robbert Krebbers committed
272 273
Infix "→" := uPred_impl : uPred_scope.
Infix "★" := uPred_sep (at level 80, right associativity) : uPred_scope.
274
Notation "(★)" := uPred_sep (only parsing) : uPred_scope.
275
Notation "P -★ Q" := (uPred_wand P Q)
Robbert Krebbers's avatar
Robbert Krebbers committed
276
  (at level 99, Q at level 200, right associativity) : uPred_scope.
Robbert Krebbers's avatar
Robbert Krebbers committed
277
Notation "∀ x .. y , P" :=
278
  (uPred_forall (λ x, .. (uPred_forall (λ y, P)) ..)%I) : uPred_scope.
Robbert Krebbers's avatar
Robbert Krebbers committed
279
Notation "∃ x .. y , P" :=
280
  (uPred_exist (λ x, .. (uPred_exist (λ y, P)) ..)%I) : uPred_scope.
281 282
Notation "□ P" := (uPred_always P)
  (at level 20, right associativity) : uPred_scope.
283 284
Notation "▷ P" := (uPred_later P)
  (at level 20, right associativity) : uPred_scope.
Robbert Krebbers's avatar
Robbert Krebbers committed
285
Infix "≡" := uPred_eq : uPred_scope.
286
Notation "✓ x" := (uPred_valid x) (at level 20) : uPred_scope.
Robbert Krebbers's avatar
Robbert Krebbers committed
287

288
Definition uPred_iff {M} (P Q : uPred M) : uPred M := ((P  Q)  (Q  P))%I.
289
Instance: Params (@uPred_iff) 1.
290 291
Infix "↔" := uPred_iff : uPred_scope.

292 293 294 295 296 297 298
Definition uPred_always_if {M} (p : bool) (P : uPred M) : uPred M :=
  (if p then  P else P)%I.
Instance: Params (@uPred_always_if) 2.
Arguments uPred_always_if _ !_ _/.
Notation "□? p P" := (uPred_always_if p P)
  (at level 20, p at level 0, P at level 20, format "□? p  P").

299
Class TimelessP {M} (P : uPred M) := timelessP :  P  (P   False).
300
Arguments timelessP {_} _ {_}.
301

302 303
Class PersistentP {M} (P : uPred M) := persistentP : P   P.
Arguments persistentP {_} _ {_}.
Robbert Krebbers's avatar
Robbert Krebbers committed
304

305 306 307 308 309
Module uPred.
Definition unseal :=
  (uPred_const_eq, uPred_and_eq, uPred_or_eq, uPred_impl_eq, uPred_forall_eq,
  uPred_exist_eq, uPred_eq_eq, uPred_sep_eq, uPred_wand_eq, uPred_always_eq,
  uPred_later_eq, uPred_ownM_eq, uPred_valid_eq).
310
Ltac unseal := rewrite !unseal /=.
311 312

Section uPred_logic.
313
Context {M : ucmraT}.
314
Implicit Types φ : Prop.
Robbert Krebbers's avatar
Robbert Krebbers committed
315
Implicit Types P Q : uPred M.
316
Implicit Types A : Type.
317 318
Notation "P ⊢ Q" := (@uPred_entails M P%I Q%I). (* Force implicit argument M *)
Notation "P ⊣⊢ Q" := (equiv (A:=uPred M) P%I Q%I). (* Force implicit argument M *)
319
Arguments uPred_holds {_} !_ _ _ /.
320
Hint Immediate uPred_in_entails.
Robbert Krebbers's avatar
Robbert Krebbers committed
321

322
Global Instance: PreOrder (@uPred_entails M).
323 324 325 326 327
Proof.
  split.
  * by intros P; split=> x i.
  * by intros P Q Q' HP HQ; split=> x i ??; apply HQ, HP.
Qed.
328
Global Instance: AntiSymm () (@uPred_entails M).
329
Proof. intros P Q HPQ HQP; split=> x n; by split; [apply HPQ|apply HQP]. Qed.
330
Lemma equiv_spec P Q : P  Q  P  Q  Q  P.
Robbert Krebbers's avatar
Robbert Krebbers committed
331
Proof.
332
  split; [|by intros [??]; apply (anti_symm ())].
333
  intros HPQ; split; split=> x i; apply HPQ.
Robbert Krebbers's avatar
Robbert Krebbers committed
334
Qed.
335
Lemma equiv_entails P Q : P  Q  P  Q.
336
Proof. apply equiv_spec. Qed.
337
Lemma equiv_entails_sym P Q : Q  P  P  Q.
338
Proof. apply equiv_spec. Qed.
339
Global Instance entails_proper :
340
  Proper (() ==> () ==> iff) (() : relation (uPred M)).
Robbert Krebbers's avatar
Robbert Krebbers committed
341
Proof.
342
  move => P1 P2 /equiv_spec [HP1 HP2] Q1 Q2 /equiv_spec [HQ1 HQ2]; split; intros.
343 344
  - by trans P1; [|trans Q1].
  - by trans P2; [|trans Q2].
Robbert Krebbers's avatar
Robbert Krebbers committed
345
Qed.
346
Lemma entails_equiv_l (P Q R : uPred M) : P  Q  Q  R  P  R.
347
Proof. by intros ->. Qed.
348
Lemma entails_equiv_r (P Q R : uPred M) : P  Q  Q  R  P  R.
349
Proof. by intros ? <-. Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
350

351
(** Non-expansiveness and setoid morphisms *)
352
Global Instance const_proper : Proper (iff ==> ()) (@uPred_const M).
353
Proof. intros φ1 φ2 Hφ. by unseal; split=> -[|n] ?; try apply Hφ. Qed.
354
Global Instance and_ne n : Proper (dist n ==> dist n ==> dist n) (@uPred_and M).
Robbert Krebbers's avatar
Robbert Krebbers committed
355
Proof.
356
  intros P P' HP Q Q' HQ; unseal; split=> x n' ??.
357
  split; (intros [??]; split; [by apply HP|by apply HQ]).
Robbert Krebbers's avatar
Robbert Krebbers committed
358
Qed.
359
Global Instance and_proper :
360
  Proper (() ==> () ==> ()) (@uPred_and M) := ne_proper_2 _.
361
Global Instance or_ne n : Proper (dist n ==> dist n ==> dist n) (@uPred_or M).
Robbert Krebbers's avatar
Robbert Krebbers committed
362
Proof.
363
  intros P P' HP Q Q' HQ; split=> x n' ??.
364
  unseal; split; (intros [?|?]; [left; by apply HP|right; by apply HQ]).
Robbert Krebbers's avatar
Robbert Krebbers committed
365
Qed.
366
Global Instance or_proper :
367
  Proper (() ==> () ==> ()) (@uPred_or M) := ne_proper_2 _.
368
Global Instance impl_ne n :
Robbert Krebbers's avatar
Robbert Krebbers committed
369
  Proper (dist n ==> dist n ==> dist n) (@uPred_impl M).
Robbert Krebbers's avatar
Robbert Krebbers committed
370
Proof.
371
  intros P P' HP Q Q' HQ; split=> x n' ??.
372
  unseal; split; intros HPQ x' n'' ????; apply HQ, HPQ, HP; auto.
Robbert Krebbers's avatar
Robbert Krebbers committed
373
Qed.
374
Global Instance impl_proper :
375
  Proper (() ==> () ==> ()) (@uPred_impl M) := ne_proper_2 _.
376
Global Instance sep_ne n : Proper (dist n ==> dist n ==> dist n) (@uPred_sep M).
Robbert Krebbers's avatar
Robbert Krebbers committed
377
Proof.
378
  intros P P' HP Q Q' HQ; split=> n' x ??.
379
  unseal; split; intros (x1&x2&?&?&?); cofe_subst x;
380
    exists x1, x2; split_and!; try (apply HP || apply HQ);
381
    eauto using cmra_validN_op_l, cmra_validN_op_r.
Robbert Krebbers's avatar
Robbert Krebbers committed
382
Qed.
383
Global Instance sep_proper :
384
  Proper (() ==> () ==> ()) (@uPred_sep M) := ne_proper_2 _.
385
Global Instance wand_ne n :
Robbert Krebbers's avatar
Robbert Krebbers committed
386
  Proper (dist n ==> dist n ==> dist n) (@uPred_wand M).
Robbert Krebbers's avatar
Robbert Krebbers committed
387
Proof.
388
  intros P P' HP Q Q' HQ; split=> n' x ??; unseal; split; intros HPQ x' n'' ???;
389
    apply HQ, HPQ, HP; eauto using cmra_validN_op_r.
Robbert Krebbers's avatar
Robbert Krebbers committed
390
Qed.
391
Global Instance wand_proper :
392
  Proper (() ==> () ==> ()) (@uPred_wand M) := ne_proper_2 _.
393
Global Instance eq_ne (A : cofeT) n :
Robbert Krebbers's avatar
Robbert Krebbers committed
394
  Proper (dist n ==> dist n ==> dist n) (@uPred_eq M A).
Robbert Krebbers's avatar
Robbert Krebbers committed
395
Proof.
396
  intros x x' Hx y y' Hy; split=> n' z; unseal; split; intros; simpl in *.
397 398
  * by rewrite -(dist_le _ _ _ _ Hx) -?(dist_le _ _ _ _ Hy); auto.
  * by rewrite (dist_le _ _ _ _ Hx) ?(dist_le _ _ _ _ Hy); auto.
Robbert Krebbers's avatar
Robbert Krebbers committed
399
Qed.
400
Global Instance eq_proper (A : cofeT) :
401
  Proper (() ==> () ==> ()) (@uPred_eq M A) := ne_proper_2 _.
402
Global Instance forall_ne A n :
Robbert Krebbers's avatar
Robbert Krebbers committed
403
  Proper (pointwise_relation _ (dist n) ==> dist n) (@uPred_forall M A).
404 405 406
Proof.
  by intros Ψ1 Ψ2 HΨ; unseal; split=> n' x; split; intros HP a; apply HΨ.
Qed.
407
Global Instance forall_proper A :
408
  Proper (pointwise_relation _ () ==> ()) (@uPred_forall M A).
409 410 411
Proof.
  by intros Ψ1 Ψ2 HΨ; unseal; split=> n' x; split; intros HP a; apply HΨ.
Qed.
412
Global Instance exist_ne A n :
Robbert Krebbers's avatar
Robbert Krebbers committed
413
  Proper (pointwise_relation _ (dist n) ==> dist n) (@uPred_exist M A).
414
Proof.
415 416
  intros Ψ1 Ψ2 HΨ.
  unseal; split=> n' x ??; split; intros [a ?]; exists a; by apply HΨ.
417
Qed.
418
Global Instance exist_proper A :
419
  Proper (pointwise_relation _ () ==> ()) (@uPred_exist M A).
420
Proof.
421 422
  intros Ψ1 Ψ2 HΨ.
  unseal; split=> n' x ?; split; intros [a ?]; exists a; by apply HΨ.
423
Qed.
424
Global Instance later_contractive : Contractive (@uPred_later M).
Robbert Krebbers's avatar
Robbert Krebbers committed
425
Proof.
426
  intros n P Q HPQ; unseal; split=> -[|n'] x ??; simpl; [done|].
427
  apply (HPQ n'); eauto using cmra_validN_S.
Robbert Krebbers's avatar
Robbert Krebbers committed
428
Qed.
429
Global Instance later_proper :
430
  Proper (() ==> ()) (@uPred_later M) := ne_proper _.
431 432
Global Instance always_ne n : Proper (dist n ==> dist n) (@uPred_always M).
Proof.
433
  intros P1 P2 HP.
Ralf Jung's avatar
Ralf Jung committed
434
  unseal; split=> n' x; split; apply HP; eauto using cmra_core_validN.
435
Qed.
436
Global Instance always_proper :
437
  Proper (() ==> ()) (@uPred_always M) := ne_proper _.
438
Global Instance ownM_ne n : Proper (dist n ==> dist n) (@uPred_ownM M).
439
Proof.
440 441
  intros a b Ha.
  unseal; split=> n' x ? /=. by rewrite (dist_le _ _ _ _ Ha); last lia.
442
Qed.
443
Global Instance ownM_proper: Proper (() ==> ()) (@uPred_ownM M) := ne_proper _.
444
Global Instance valid_ne {A : cmraT} n :
445 446
Proper (dist n ==> dist n) (@uPred_valid M A).
Proof.
447 448
  intros a b Ha; unseal; split=> n' x ? /=.
  by rewrite (dist_le _ _ _ _ Ha); last lia.
449
Qed.
450
Global Instance valid_proper {A : cmraT} :
451
  Proper (() ==> ()) (@uPred_valid M A) := ne_proper _.
452
Global Instance iff_ne n : Proper (dist n ==> dist n ==> dist n) (@uPred_iff M).
453
Proof. unfold uPred_iff; solve_proper. Qed.
454
Global Instance iff_proper :
455
  Proper (() ==> () ==> ()) (@uPred_iff M) := ne_proper_2 _.
Robbert Krebbers's avatar
Robbert Krebbers committed
456 457

(** Introduction and elimination rules *)
458
Lemma const_intro φ P : φ  P   φ.
459
Proof. by intros ?; unseal; split. Qed.
460
Lemma const_elim φ Q R : Q   φ  (φ  Q  R)  Q  R.
461 462 463
Proof.
  unseal; intros HQP HQR; split=> n x ??; apply HQR; first eapply HQP; eauto.
Qed.
464
Lemma and_elim_l P Q : (P  Q)  P.
465
Proof. by unseal; split=> n x ? [??]. Qed.
466
Lemma and_elim_r P Q : (P  Q)  Q.
467
Proof. by unseal; split=> n x ? [??]. Qed.
468
Lemma and_intro P Q R : P  Q  P  R  P  (Q  R).
469
Proof. intros HQ HR; unseal; split=> n x ??; by split; [apply HQ|apply HR]. Qed.
470
Lemma or_intro_l P Q : P  (P  Q).
471
Proof. unseal; split=> n x ??; left; auto. Qed.
472
Lemma or_intro_r P Q : Q  (P  Q).
473
Proof. unseal; split=> n x ??; right; auto. Qed.
474
Lemma or_elim P Q R : P  R  Q  R  (P  Q)  R.
475
Proof. intros HP HQ; unseal; split=> n x ? [?|?]. by apply HP. by apply HQ. Qed.
476
Lemma impl_intro_r P Q R : (P  Q)  R  P  (Q  R).
Robbert Krebbers's avatar
Robbert Krebbers committed
477
Proof.
478 479
  unseal; intros HQ; split=> n x ?? n' x' ????. apply HQ;
    naive_solver eauto using uPred_mono, uPred_closed, cmra_included_includedN.
Robbert Krebbers's avatar
Robbert Krebbers committed
480
Qed.
481
Lemma impl_elim P Q R : P  (Q  R)  P  Q  P  R.
482
Proof. by unseal; intros HP HP'; split=> n x ??; apply HP with n x, HP'. Qed.
483
Lemma forall_intro {A} P (Ψ : A  uPred M): ( a, P  Ψ a)  P  ( a, Ψ a).
484
Proof. unseal; intros HPΨ; split=> n x ?? a; by apply HPΨ. Qed.
485
Lemma forall_elim {A} {Ψ : A  uPred M} a : ( a, Ψ a)  Ψ a.
486
Proof. unseal; split=> n x ? HP; apply HP. Qed.
487
Lemma exist_intro {A} {Ψ : A  uPred M} a : Ψ a  ( a, Ψ a).
488
Proof. unseal; split=> n x ??; by exists a. Qed.
489
Lemma exist_elim {A} (Φ : A  uPred M) Q : ( a, Φ a  Q)  ( a, Φ a)  Q.
490
Proof. unseal; intros HΦΨ; split=> n x ? [a ?]; by apply HΦΨ with a. Qed.
491
Lemma eq_refl {A : cofeT} (a : A) : True  (a  a).
492
Proof. unseal; by split=> n x ??; simpl. Qed.
493
Lemma eq_rewrite {A : cofeT} a b (Ψ : A  uPred M) P
494
  {HΨ :  n, Proper (dist n ==> dist n) Ψ} : P  (a  b)  P  Ψ a  P  Ψ b.
495
Proof.
496 497 498
  unseal; intros Hab Ha; split=> n x ??. apply HΨ with n a; auto.
  - by symmetry; apply Hab with x.
  - by apply Ha.
499
Qed.
500
Lemma eq_equiv {A : cofeT} (a b : A) : True  (a  b)  a  b.
501
Proof.
502
  unseal=> Hab; apply equiv_dist; intros n; apply Hab with ; last done.
503
  apply cmra_valid_validN, ucmra_unit_valid.
504
Qed.
505 506 507 508 509 510 511 512
Lemma eq_rewrite_contractive {A : cofeT} a b (Ψ : A  uPred M) P
  {HΨ : Contractive Ψ} : P   (a  b)  P  Ψ a  P  Ψ b.
Proof.
  unseal; intros Hab Ha; split=> n x ??. apply HΨ with n a; auto.
  - destruct n; intros m ?; first omega. apply (dist_le n); last omega.
    symmetry. by destruct Hab as [Hab]; eapply (Hab (S n)).
  - by apply Ha.
Qed.
513 514

(* Derived logical stuff *)
515 516
Lemma False_elim P : False  P.
Proof. by apply (const_elim False). Qed.
517
Lemma True_intro P : P  True.
Robbert Krebbers's avatar
Robbert Krebbers committed
518
Proof. by apply const_intro. Qed.
519
Lemma and_elim_l' P Q R : P  R  (P  Q)  R.
520
Proof. by rewrite and_elim_l. Qed.
521
Lemma and_elim_r' P Q R : Q  R  (P  Q)  R.
522
Proof. by rewrite and_elim_r. Qed.
523
Lemma or_intro_l' P Q R : P  Q  P  (Q  R).
524
Proof. intros ->; apply or_intro_l. Qed.
525
Lemma or_intro_r' P Q R : P  R  P  (Q  R).
526
Proof. intros ->; apply or_intro_r. Qed.
527
Lemma exist_intro' {A} P (Ψ : A  uPred M) a : P  Ψ a  P  ( a, Ψ a).
528
Proof. intros ->; apply exist_intro. Qed.
529
Lemma forall_elim' {A} P (Ψ : A  uPred M) : P  ( a, Ψ a)  ( a, P  Ψ a).
530
Proof. move=> HP a. by rewrite HP forall_elim. Qed.
531

532
Hint Resolve or_elim or_intro_l' or_intro_r'.
533 534
Hint Resolve and_intro and_elim_l' and_elim_r'.
Hint Immediate True_intro False_elim.
535

536
Lemma impl_intro_l P Q R : (Q  P)  R  P  (Q  R).
537
Proof. intros HR; apply impl_intro_r; rewrite -HR; auto. Qed.
538
Lemma impl_elim_l P Q : ((P  Q)  P)  Q.
539
Proof. apply impl_elim with P; auto. Qed.
540
Lemma impl_elim_r P Q : (P  (P  Q))  Q.
541
Proof. apply impl_elim with P; auto. Qed.
542
Lemma impl_elim_l' P Q R : P  (Q  R)  (P  Q)  R.
543
Proof. intros; apply impl_elim with Q; auto. Qed.
544
Lemma impl_elim_r' P Q R : Q  (P  R)  (P  Q)  R.
545
Proof. intros; apply impl_elim with P; auto. Qed.
546
Lemma impl_entails P Q : True  (P  Q)  P  Q.
547
Proof. intros HPQ; apply impl_elim with P; rewrite -?HPQ; auto. Qed.
548
Lemma entails_impl P Q : (P  Q)  True  (P  Q).
549
Proof. auto using impl_intro_l. Qed.
550

551 552 553 554 555 556 557 558 559 560
Lemma iff_refl Q P : Q  (P  P).
Proof. rewrite /uPred_iff; apply and_intro; apply impl_intro_l; auto. Qed.
Lemma iff_equiv P Q : True  (P  Q)  P  Q.
Proof.
  intros HPQ; apply (anti_symm ());
    apply impl_entails; rewrite HPQ /uPred_iff; auto.
Qed.
Lemma equiv_iff P Q : P  Q  True  (P  Q).
Proof. intros ->; apply iff_refl. Qed.

561
Lemma const_mono φ1 φ2 : (φ1  φ2)   φ1   φ2.
562
Proof. intros; apply const_elim with φ1; eauto using const_intro. Qed.
563
Lemma and_mono P P' Q Q' : P  Q  P'  Q'  (P  P')  (Q  Q').
564
Proof. auto. Qed.
565
Lemma and_mono_l P P' Q : P  Q  (P  P')  (Q  P').
566
Proof. by intros; apply and_mono. Qed.
567
Lemma and_mono_r P P' Q' : P'  Q'  (P  P')  (P  Q').
568
Proof. by apply and_mono. Qed.
569
Lemma or_mono P P' Q Q' : P  Q  P'  Q'  (P  P')  (Q  Q').
570
Proof. auto. Qed.
571
Lemma or_mono_l P P' Q : P  Q  (P  P')  (Q  P').
572
Proof. by intros; apply or_mono. Qed.
573
Lemma or_mono_r P P' Q' : P'  Q'  (P  P')  (P  Q').
574
Proof. by apply or_mono. Qed.
575
Lemma impl_mono P P' Q Q' : Q  P  P'  Q'  (P  P')  (Q  Q').
576
Proof.
577
  intros HP HQ'; apply impl_intro_l; rewrite -HQ'.
578
  apply impl_elim with P; eauto.
579
Qed.
580
Lemma forall_mono {A} (Φ Ψ : A  uPred M) :
581
  ( a, Φ a  Ψ a)  ( a, Φ a)  ( a, Ψ a).
582
Proof.
583
  intros HP. apply forall_intro=> a; rewrite -(HP a); apply forall_elim.
584
Qed.
585
Lemma exist_mono {A} (Φ Ψ : A  uPred M) :
586
  ( a, Φ a  Ψ a)  ( a, Φ a)  ( a, Ψ a).
587
Proof. intros HΦ. apply exist_elim=> a; rewrite (HΦ a); apply exist_intro. Qed.
588
Global Instance const_mono' : Proper (impl ==> ()) (@uPred_const M).
589
Proof. intros φ1 φ2; apply const_mono. Qed.
590
Global Instance and_mono' : Proper (() ==> () ==> ()) (@uPred_and M).
591
Proof. by intros P P' HP Q Q' HQ; apply and_mono. Qed.
592
Global Instance and_flip_mono' :
593
  Proper (flip () ==> flip () ==> flip ()) (@uPred_and M).
594
Proof. by intros P P' HP Q Q' HQ; apply and_mono. Qed.
595
Global Instance or_mono' : Proper (() ==> () ==> ()) (@uPred_or M).
596
Proof. by intros P P' HP Q Q' HQ; apply or_mono. Qed.
597
Global Instance or_flip_mono' :
598
  Proper (flip () ==> flip () ==> flip ()) (@uPred_or M).