upred.v 46.8 KB
Newer Older
1
From 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.

Robbert Krebbers's avatar
Robbert Krebbers committed
6
Record uPred (M : cmraT) : Type := IProp {
Robbert Krebbers's avatar
Robbert Krebbers committed
7
  uPred_holds :> nat  M  Prop;
Robbert Krebbers's avatar
Robbert Krebbers committed
8 9
  uPred_ne n x1 x2 : uPred_holds n x1  x1 {n} x2  uPred_holds n x2;
  uPred_weaken  n1 n2 x1 x2 :
Robbert Krebbers's avatar
Robbert Krebbers committed
10
    uPred_holds n1 x1  x1  x2  n2  n1  {n2} x2  uPred_holds n2 x2
Robbert Krebbers's avatar
Robbert Krebbers committed
11
}.
12
Arguments uPred_holds {_} _ _ _ : simpl never.
13 14
Global Opaque uPred_holds.
Local Transparent uPred_holds.
Robbert Krebbers's avatar
Robbert Krebbers committed
15 16
Add Printing Constructor uPred.
Instance: Params (@uPred_holds) 3.
Robbert Krebbers's avatar
Robbert Krebbers committed
17

18 19 20 21
Delimit Scope uPred_scope with I.
Bind Scope uPred_scope with uPred.
Arguments uPred_holds {_} _%I _ _.

22 23
Section cofe.
  Context {M : cmraT}.
24 25 26 27 28 29 30

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

56
Instance uPred_ne' {M} (P : uPred M) n : Proper (dist n ==> iff) (P n).
Robbert Krebbers's avatar
Robbert Krebbers committed
57
Proof. intros x1 x2 Hx; split; eauto using uPred_ne. Qed.
58 59 60 61
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} (P1 P2 : uPred M) n x :
62
  P1 {n} P2  {n} x  P1 n x  P2 n x.
63
Proof. intros HP ?; apply HP; auto. Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
64
Lemma uPred_weaken' {M} (P : uPred M) n1 n2 x1 x2 :
65
  x1  x2  n2  n1  {n2} x2  P n1 x1  P n2 x2.
66
Proof. eauto using uPred_weaken. Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
67 68

(** functor *)
69 70 71 72
Program Definition uPred_map {M1 M2 : cmraT} (f : M2 -n> M1)
  `{!CMRAMonotone f} (P : uPred M1) :
  uPred M2 := {| uPred_holds n x := P n (f x) |}.
Next Obligation. by intros M1 M2 f ? P y1 y2 n ? Hy; rewrite /= -Hy. Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
73
Next Obligation.
Robbert Krebbers's avatar
Robbert Krebbers committed
74
  naive_solver eauto using uPred_weaken, included_preserving, validN_preserving.
Robbert Krebbers's avatar
Robbert Krebbers committed
75
Qed.
76
Instance uPred_map_ne {M1 M2 : cmraT} (f : M2 -n> M1)
Robbert Krebbers's avatar
Robbert Krebbers committed
77
  `{!CMRAMonotone f} n : Proper (dist n ==> dist n) (uPred_map f).
Robbert Krebbers's avatar
Robbert Krebbers committed
78
Proof.
79 80
  intros x1 x2 Hx; split=> n' y ??.
  split; apply Hx; auto using validN_preserving.
Robbert Krebbers's avatar
Robbert Krebbers committed
81
Qed.
82
Lemma uPred_map_id {M : cmraT} (P : uPred M): uPred_map cid P  P.
83
Proof. by split=> n x ?. Qed.
84
Lemma uPred_map_compose {M1 M2 M3 : cmraT} (f : M1 -n> M2) (g : M2 -n> M3)
Robbert Krebbers's avatar
Robbert Krebbers committed
85
    `{!CMRAMonotone f, !CMRAMonotone g} (P : uPred M3):
86
  uPred_map (g  f) P  uPred_map f (uPred_map g P).
87
Proof. by split=> n x Hx. Qed.
88
Lemma uPred_map_ext {M1 M2 : cmraT} (f g : M1 -n> M2)
89 90 91
      `{!CMRAMonotone f} `{!CMRAMonotone g}:
  ( 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.
Robbert Krebbers's avatar
Robbert Krebbers committed
92
Definition uPredC_map {M1 M2 : cmraT} (f : M2 -n> M1) `{!CMRAMonotone f} :
Robbert Krebbers's avatar
Robbert Krebbers committed
93
  uPredC M1 -n> uPredC M2 := CofeMor (uPred_map f : uPredC M1  uPredC M2).
Robbert Krebbers's avatar
Robbert Krebbers committed
94
Lemma upredC_map_ne {M1 M2 : cmraT} (f g : M2 -n> M1)
Robbert Krebbers's avatar
Robbert Krebbers committed
95
    `{!CMRAMonotone f, !CMRAMonotone g} n :
96
  f {n} g  uPredC_map f {n} uPredC_map g.
Robbert Krebbers's avatar
Robbert Krebbers committed
97
Proof.
98
  by intros Hfg P; split=> n' y ??;
99
    rewrite /uPred_holds /= (dist_le _ _ _ _(Hfg y)); last lia.
Robbert Krebbers's avatar
Robbert Krebbers committed
100
Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
101 102

(** logical entailement *)
103 104
Inductive uPred_entails {M} (P Q : uPred M) : Prop :=
  { uPred_in_entails :  n x, {n} x  P n x  Q n x }.
105
Hint Extern 0 (uPred_entails _ _) => reflexivity.
106
Instance uPred_entails_rewrite_relation M : RewriteRelation (@uPred_entails M).
Robbert Krebbers's avatar
Robbert Krebbers committed
107 108

(** logical connectives *)
109
Program Definition uPred_const {M} (φ : Prop) : uPred M :=
110
  {| uPred_holds n x := φ |}.
Robbert Krebbers's avatar
Robbert Krebbers committed
111
Solve Obligations with done.
112
Instance uPred_inhabited M : Inhabited (uPred M) := populate (uPred_const True).
Robbert Krebbers's avatar
Robbert Krebbers committed
113

Robbert Krebbers's avatar
Robbert Krebbers committed
114 115 116 117 118 119 120
Program Definition uPred_and {M} (P Q : uPred M) : uPred M :=
  {| uPred_holds n x := P n x  Q n x |}.
Solve Obligations with naive_solver eauto 2 using uPred_ne, uPred_weaken.
Program Definition uPred_or {M} (P Q : uPred M) : uPred M :=
  {| uPred_holds n x := P n x  Q n x |}.
Solve Obligations with naive_solver eauto 2 using uPred_ne, uPred_weaken.
Program Definition uPred_impl {M} (P Q : uPred M) : uPred M :=
Robbert Krebbers's avatar
Robbert Krebbers committed
121
  {| uPred_holds n x :=  n' x',
Robbert Krebbers's avatar
Robbert Krebbers committed
122
       x  x'  n'  n  {n'} x'  P n' x'  Q n' x' |}.
Robbert Krebbers's avatar
Robbert Krebbers committed
123
Next Obligation.
Robbert Krebbers's avatar
Robbert Krebbers committed
124 125
  intros M P Q n1 x1' x1 HPQ Hx1 n2 x2 ????.
  destruct (cmra_included_dist_l n1 x1 x2 x1') as (x2'&?&Hx2); auto.
126
  assert (x2' {n2} x2) as Hx2' by (by apply dist_le with n1).
127
  assert ({n2} x2') by (by rewrite Hx2'); rewrite -Hx2'.
Robbert Krebbers's avatar
Robbert Krebbers committed
128
  eauto using uPred_weaken, uPred_ne.
Robbert Krebbers's avatar
Robbert Krebbers committed
129
Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
130
Next Obligation. intros M P Q [|n] x1 x2; auto with lia. Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
131

132 133
Program Definition uPred_forall {M A} (Ψ : A  uPred M) : uPred M :=
  {| uPred_holds n x :=  a, Ψ a n x |}.
Robbert Krebbers's avatar
Robbert Krebbers committed
134
Solve Obligations with naive_solver eauto 2 using uPred_ne, uPred_weaken.
135 136
Program Definition uPred_exist {M A} (Ψ : A  uPred M) : uPred M :=
  {| uPred_holds n x :=  a, Ψ a n x |}.
137
Solve Obligations with naive_solver eauto 2 using uPred_ne, uPred_weaken.
Robbert Krebbers's avatar
Robbert Krebbers committed
138

Robbert Krebbers's avatar
Robbert Krebbers committed
139
Program Definition uPred_eq {M} {A : cofeT} (a1 a2 : A) : uPred M :=
140
  {| uPred_holds n x := a1 {n} a2 |}.
141
Solve Obligations with naive_solver eauto 2 using (dist_le (A:=A)).
Robbert Krebbers's avatar
Robbert Krebbers committed
142

Robbert Krebbers's avatar
Robbert Krebbers committed
143
Program Definition uPred_sep {M} (P Q : uPred M) : uPred M :=
144
  {| uPred_holds n x :=  x1 x2, x {n} x1  x2  P n x1  Q n x2 |}.
Robbert Krebbers's avatar
Robbert Krebbers committed
145
Next Obligation.
Robbert Krebbers's avatar
Robbert Krebbers committed
146
  by intros M P Q n x y (x1&x2&?&?&?) Hxy; exists x1, x2; rewrite -Hxy.
Robbert Krebbers's avatar
Robbert Krebbers committed
147 148
Qed.
Next Obligation.
Robbert Krebbers's avatar
Robbert Krebbers committed
149
  intros M P Q n1 n2 x y (x1&x2&Hx&?&?) Hxy ??.
150
  assert ( x2', y {n2} x1  x2'  x2  x2') as (x2'&Hy&?).
151
  { destruct Hxy as [z Hy]; exists (x2  z); split; eauto using cmra_included_l.
152
    apply dist_le with n1; auto. by rewrite (assoc op) -Hx Hy. }
153
  clear Hxy; cofe_subst y; exists x1, x2'; split_and?; [done| |].
Robbert Krebbers's avatar
Robbert Krebbers committed
154 155
  - apply uPred_weaken with n1 x1; eauto using cmra_validN_op_l.
  - apply uPred_weaken with n1 x2; eauto using cmra_validN_op_r.
Robbert Krebbers's avatar
Robbert Krebbers committed
156 157
Qed.

Robbert Krebbers's avatar
Robbert Krebbers committed
158
Program Definition uPred_wand {M} (P Q : uPred M) : uPred M :=
Robbert Krebbers's avatar
Robbert Krebbers committed
159
  {| uPred_holds n x :=  n' x',
Robbert Krebbers's avatar
Robbert Krebbers committed
160
       n'  n  {n'} (x  x')  P n' x'  Q n' (x  x') |}.
Robbert Krebbers's avatar
Robbert Krebbers committed
161
Next Obligation.
Robbert Krebbers's avatar
Robbert Krebbers committed
162
  intros M P Q n1 x1 x2 HPQ Hx n2 x3 ???; simpl in *.
Robbert Krebbers's avatar
Robbert Krebbers committed
163
  rewrite -(dist_le _ _ _ _ Hx) //; apply HPQ; auto.
Robbert Krebbers's avatar
Robbert Krebbers committed
164
  by rewrite (dist_le _ _ _ _ Hx).
Robbert Krebbers's avatar
Robbert Krebbers committed
165 166
Qed.
Next Obligation.
Robbert Krebbers's avatar
Robbert Krebbers committed
167 168
  intros M P Q n1 n2 x1 x2 HPQ ??? n3 x3 ???; simpl in *.
  apply uPred_weaken with n3 (x1  x3);
169
    eauto using cmra_validN_included, cmra_preserving_r.
Robbert Krebbers's avatar
Robbert Krebbers committed
170 171
Qed.

Robbert Krebbers's avatar
Robbert Krebbers committed
172 173
Program Definition uPred_always {M} (P : uPred M) : uPred M :=
  {| uPred_holds n x := P n (unit x) |}.
174
Next Obligation. by intros M P x1 x2 n ? Hx; rewrite /= -Hx. Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
175
Next Obligation.
Robbert Krebbers's avatar
Robbert Krebbers committed
176
  intros M P n1 n2 x1 x2 ????; eapply uPred_weaken with n1 (unit x1);
177
    eauto using cmra_unit_preserving, cmra_unit_validN.
Robbert Krebbers's avatar
Robbert Krebbers committed
178
Qed.
179 180 181 182 183 184
Program Definition uPred_later {M} (P : uPred M) : uPred M :=
  {| uPred_holds n x := match n return _ with 0 => True | S n' => P n' x end |}.
Next Obligation. intros M P [|n] ??; eauto using uPred_ne,(dist_le (A:=M)). Qed.
Next Obligation.
  intros M P [|n1] [|n2] x1 x2; eauto using uPred_weaken,cmra_validN_S; try lia.
Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
185

Ralf Jung's avatar
Ralf Jung committed
186
Program Definition uPred_ownM {M : cmraT} (a : M) : uPred M :=
Robbert Krebbers's avatar
Robbert Krebbers committed
187
  {| uPred_holds n x := a {n} x |}.
Robbert Krebbers's avatar
Robbert Krebbers committed
188
Next Obligation. by intros M a n x1 x2 [a' ?] Hx; exists a'; rewrite -Hx. Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
189
Next Obligation.
Robbert Krebbers's avatar
Robbert Krebbers committed
190
  intros M a n1 n2 x1 x [a' Hx1] [x2 Hx] ??.
191
  exists (a'  x2). by rewrite (assoc op) -(dist_le _ _ _ _ Hx1) // Hx.
Robbert Krebbers's avatar
Robbert Krebbers committed
192
Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
193
Program Definition uPred_valid {M A : cmraT} (a : A) : uPred M :=
Robbert Krebbers's avatar
Robbert Krebbers committed
194
  {| uPred_holds n x := {n} a |}.
195
Solve Obligations with naive_solver eauto 2 using cmra_validN_le.
Robbert Krebbers's avatar
Robbert Krebbers committed
196

197 198
Notation "P ⊑ Q" := (uPred_entails P%I Q%I) (at level 70) : C_scope.
Notation "(⊑)" := uPred_entails (only parsing) : C_scope.
199 200
Notation "■ φ" := (uPred_const φ%C%type)
  (at level 20, right associativity) : uPred_scope.
Ralf Jung's avatar
Ralf Jung committed
201
Notation "x = y" := (uPred_const (x%C%type = y%C%type)) : uPred_scope.
Robbert Krebbers's avatar
Robbert Krebbers committed
202 203 204
Notation "'False'" := (uPred_const False) : uPred_scope.
Notation "'True'" := (uPred_const True) : uPred_scope.
Infix "∧" := uPred_and : uPred_scope.
205
Notation "(∧)" := uPred_and (only parsing) : uPred_scope.
Robbert Krebbers's avatar
Robbert Krebbers committed
206
Infix "∨" := uPred_or : uPred_scope.
207
Notation "(∨)" := uPred_or (only parsing) : uPred_scope.
Robbert Krebbers's avatar
Robbert Krebbers committed
208 209
Infix "→" := uPred_impl : uPred_scope.
Infix "★" := uPred_sep (at level 80, right associativity) : uPred_scope.
210
Notation "(★)" := uPred_sep (only parsing) : uPred_scope.
211
Notation "P -★ Q" := (uPred_wand P Q)
212
  (at level 199, Q at level 200, right associativity) : uPred_scope.
Robbert Krebbers's avatar
Robbert Krebbers committed
213
Notation "∀ x .. y , P" :=
214
  (uPred_forall (λ x, .. (uPred_forall (λ y, P)) ..)%I) : uPred_scope.
Robbert Krebbers's avatar
Robbert Krebbers committed
215
Notation "∃ x .. y , P" :=
216
  (uPred_exist (λ x, .. (uPred_exist (λ y, P)) ..)%I) : uPred_scope.
217 218
Notation "□ P" := (uPred_always P)
  (at level 20, right associativity) : uPred_scope.
219 220
Notation "▷ P" := (uPred_later P)
  (at level 20, right associativity) : uPred_scope.
Robbert Krebbers's avatar
Robbert Krebbers committed
221
Infix "≡" := uPred_eq : uPred_scope.
222
Notation "✓ x" := (uPred_valid x) (at level 20) : uPred_scope.
Robbert Krebbers's avatar
Robbert Krebbers committed
223

224 225 226
Definition uPred_iff {M} (P Q : uPred M) : uPred M := ((P  Q)  (Q  P))%I.
Infix "↔" := uPred_iff : uPred_scope.

227
Class TimelessP {M} (P : uPred M) := timelessP :  P  (P   False).
228
Arguments timelessP {_} _ {_}.
229
Class AlwaysStable {M} (P : uPred M) := always_stable : P   P.
230
Arguments always_stable {_} _ {_}.
Robbert Krebbers's avatar
Robbert Krebbers committed
231

Robbert Krebbers's avatar
Robbert Krebbers committed
232
Module uPred. Section uPred_logic.
233
Context {M : cmraT}.
234
Implicit Types φ : Prop.
Robbert Krebbers's avatar
Robbert Krebbers committed
235
Implicit Types P Q : uPred M.
236
Implicit Types A : Type.
237
Notation "P ⊑ Q" := (@uPred_entails M P%I Q%I). (* Force implicit argument M *)
238
Arguments uPred_holds {_} !_ _ _ /.
239
Hint Immediate uPred_in_entails.
Robbert Krebbers's avatar
Robbert Krebbers committed
240

241
Global Instance: PreOrder (@uPred_entails M).
242 243 244 245 246
Proof.
  split.
  * by intros P; split=> x i.
  * by intros P Q Q' HP HQ; split=> x i ??; apply HQ, HP.
Qed.
247
Global Instance: AntiSymm () (@uPred_entails M).
248
Proof. intros P Q HPQ HQP; split=> x n; by split; [apply HPQ|apply HQP]. Qed.
249
Lemma equiv_spec P Q : P  Q  P  Q  Q  P.
Robbert Krebbers's avatar
Robbert Krebbers committed
250
Proof.
251
  split; [|by intros [??]; apply (anti_symm ())].
252
  intros HPQ; split; split=> x i; apply HPQ.
Robbert Krebbers's avatar
Robbert Krebbers committed
253
Qed.
254 255 256 257
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.
Robbert Krebbers's avatar
Robbert Krebbers committed
258
Global Instance entails_proper :
259
  Proper (() ==> () ==> iff) (() : relation (uPred M)).
Robbert Krebbers's avatar
Robbert Krebbers committed
260
Proof.
Robbert Krebbers's avatar
Robbert Krebbers committed
261
  move => P1 P2 /equiv_spec [HP1 HP2] Q1 Q2 /equiv_spec [HQ1 HQ2]; split; intros.
262 263
  - by trans P1; [|trans Q1].
  - by trans P2; [|trans Q2].
Robbert Krebbers's avatar
Robbert Krebbers committed
264
Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
265

266
(** Non-expansiveness and setoid morphisms *)
Robbert Krebbers's avatar
Robbert Krebbers committed
267
Global Instance const_proper : Proper (iff ==> ()) (@uPred_const M).
268
Proof. intros φ1 φ2 Hφ. by split=> -[|n] ?; try apply Hφ. Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
269
Global Instance and_ne n : Proper (dist n ==> dist n ==> dist n) (@uPred_and M).
Robbert Krebbers's avatar
Robbert Krebbers committed
270
Proof.
271 272
  intros P P' HP Q Q' HQ; split=> x n' ??.
  split; (intros [??]; split; [by apply HP|by apply HQ]).
Robbert Krebbers's avatar
Robbert Krebbers committed
273
Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
274
Global Instance and_proper :
Robbert Krebbers's avatar
Robbert Krebbers committed
275
  Proper (() ==> () ==> ()) (@uPred_and M) := ne_proper_2 _.
Robbert Krebbers's avatar
Robbert Krebbers committed
276
Global Instance or_ne n : Proper (dist n ==> dist n ==> dist n) (@uPred_or M).
Robbert Krebbers's avatar
Robbert Krebbers committed
277
Proof.
278 279
  intros P P' HP Q Q' HQ; split=> x n' ??.
  split; (intros [?|?]; [left; by apply HP|right; by apply HQ]).
Robbert Krebbers's avatar
Robbert Krebbers committed
280
Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
281
Global Instance or_proper :
Robbert Krebbers's avatar
Robbert Krebbers committed
282
  Proper (() ==> () ==> ()) (@uPred_or M) := ne_proper_2 _.
Robbert Krebbers's avatar
Robbert Krebbers committed
283
Global Instance impl_ne n :
Robbert Krebbers's avatar
Robbert Krebbers committed
284
  Proper (dist n ==> dist n ==> dist n) (@uPred_impl M).
Robbert Krebbers's avatar
Robbert Krebbers committed
285
Proof.
286 287
  intros P P' HP Q Q' HQ; split=> x n' ??.
  split; intros HPQ x' n'' ????; apply HQ, HPQ, HP; auto.
Robbert Krebbers's avatar
Robbert Krebbers committed
288
Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
289
Global Instance impl_proper :
Robbert Krebbers's avatar
Robbert Krebbers committed
290
  Proper (() ==> () ==> ()) (@uPred_impl M) := ne_proper_2 _.
Robbert Krebbers's avatar
Robbert Krebbers committed
291
Global Instance sep_ne n : Proper (dist n ==> dist n ==> dist n) (@uPred_sep M).
Robbert Krebbers's avatar
Robbert Krebbers committed
292
Proof.
293 294 295
  intros P P' HP Q Q' HQ; split=> n' x ??.
  split; intros (x1&x2&?&?&?); cofe_subst x;
    exists x1, x2; split_and!; try (apply HP || apply HQ);
296
    eauto using cmra_validN_op_l, cmra_validN_op_r.
Robbert Krebbers's avatar
Robbert Krebbers committed
297
Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
298
Global Instance sep_proper :
Robbert Krebbers's avatar
Robbert Krebbers committed
299
  Proper (() ==> () ==> ()) (@uPred_sep M) := ne_proper_2 _.
Robbert Krebbers's avatar
Robbert Krebbers committed
300
Global Instance wand_ne n :
Robbert Krebbers's avatar
Robbert Krebbers committed
301
  Proper (dist n ==> dist n ==> dist n) (@uPred_wand M).
Robbert Krebbers's avatar
Robbert Krebbers committed
302
Proof.
303
  intros P P' HP Q Q' HQ; split=> n' x ??; split; intros HPQ x' n'' ???;
304
    apply HQ, HPQ, HP; eauto using cmra_validN_op_r.
Robbert Krebbers's avatar
Robbert Krebbers committed
305
Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
306
Global Instance wand_proper :
Robbert Krebbers's avatar
Robbert Krebbers committed
307
  Proper (() ==> () ==> ()) (@uPred_wand M) := ne_proper_2 _.
Robbert Krebbers's avatar
Robbert Krebbers committed
308
Global Instance eq_ne (A : cofeT) n :
Robbert Krebbers's avatar
Robbert Krebbers committed
309
  Proper (dist n ==> dist n ==> dist n) (@uPred_eq M A).
Robbert Krebbers's avatar
Robbert Krebbers committed
310
Proof.
311 312 313
  intros x x' Hx y y' Hy; split=> n' z; split; intros; simpl in *.
  * 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
314
Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
315
Global Instance eq_proper (A : cofeT) :
Robbert Krebbers's avatar
Robbert Krebbers committed
316
  Proper (() ==> () ==> ()) (@uPred_eq M A) := ne_proper_2 _.
317
Global Instance forall_ne A n :
Robbert Krebbers's avatar
Robbert Krebbers committed
318
  Proper (pointwise_relation _ (dist n) ==> dist n) (@uPred_forall M A).
319
Proof. by intros Ψ1 Ψ2 HΨ; split=> n' x; split; intros HP a; apply HΨ. Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
320
Global Instance forall_proper A :
Robbert Krebbers's avatar
Robbert Krebbers committed
321
  Proper (pointwise_relation _ () ==> ()) (@uPred_forall M A).
322 323
Proof. by intros Ψ1 Ψ2 HΨ; split=> n' x; split; intros HP a; apply HΨ. Qed.
Global Instance exist_ne A n :
Robbert Krebbers's avatar
Robbert Krebbers committed
324
  Proper (pointwise_relation _ (dist n) ==> dist n) (@uPred_exist M A).
325 326 327
Proof.
  intros Ψ1 Ψ2 HΨ; split=> n' x ??; split; intros [a ?]; exists a; by apply HΨ.
Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
328
Global Instance exist_proper A :
Robbert Krebbers's avatar
Robbert Krebbers committed
329
  Proper (pointwise_relation _ () ==> ()) (@uPred_exist M A).
330 331 332
Proof.
  intros Ψ1 Ψ2 HΨ; split=> n' x ?; split; intros [a ?]; exists a; by apply HΨ.
Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
333
Global Instance later_contractive : Contractive (@uPred_later M).
Robbert Krebbers's avatar
Robbert Krebbers committed
334
Proof.
335
  intros n P Q HPQ; split=> -[|n'] x ??; simpl; [done|].
336
  apply (HPQ n'); eauto using cmra_validN_S.
Robbert Krebbers's avatar
Robbert Krebbers committed
337
Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
338
Global Instance later_proper :
Robbert Krebbers's avatar
Robbert Krebbers committed
339
  Proper (() ==> ()) (@uPred_later M) := ne_proper _.
340 341 342 343
Global Instance always_ne n : Proper (dist n ==> dist n) (@uPred_always M).
Proof.
  intros P1 P2 HP; split=> n' x; split; apply HP; eauto using cmra_unit_validN.
Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
344
Global Instance always_proper :
Robbert Krebbers's avatar
Robbert Krebbers committed
345
  Proper (() ==> ()) (@uPred_always M) := ne_proper _.
346
Global Instance ownM_ne n : Proper (dist n ==> dist n) (@uPred_ownM M).
347 348 349
Proof.
  intros a b Ha; split=> n' x ? /=. by rewrite (dist_le _ _ _ _ Ha); last lia.
Qed.
350 351
Global Instance ownM_proper: Proper (() ==> ()) (@uPred_ownM M) := ne_proper _.
Global Instance valid_ne {A : cmraT} n :
352 353 354 355
Proper (dist n ==> dist n) (@uPred_valid M A).
Proof.
  intros a b Ha; split=> n' x ? /=. by rewrite (dist_le _ _ _ _ Ha); last lia.
Qed.
356 357
Global Instance valid_proper {A : cmraT} :
  Proper (() ==> ()) (@uPred_valid M A) := ne_proper _.
Robbert Krebbers's avatar
Robbert Krebbers committed
358
Global Instance iff_ne n : Proper (dist n ==> dist n ==> dist n) (@uPred_iff M).
359
Proof. unfold uPred_iff; solve_proper. Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
360
Global Instance iff_proper :
361
  Proper (() ==> () ==> ()) (@uPred_iff M) := ne_proper_2 _.
Robbert Krebbers's avatar
Robbert Krebbers committed
362 363

(** Introduction and elimination rules *)
364
Lemma const_intro φ P : φ  P   φ.
365
Proof. by intros ?; split. Qed.
366
Lemma const_elim φ Q R : Q   φ  (φ  Q  R)  Q  R.
367
Proof. intros HQP HQR; split=> n x ??; apply HQR; first eapply HQP; eauto. Qed.
368
Lemma False_elim P : False  P.
369
Proof. by split=> n x ?. Qed.
370
Lemma and_elim_l P Q : (P  Q)  P.
371
Proof. by split=> n x ? [??]. Qed.
372
Lemma and_elim_r P Q : (P  Q)  Q.
373
Proof. by split=> n x ? [??]. Qed.
374
Lemma and_intro P Q R : P  Q  P  R  P  (Q  R).
375
Proof. intros HQ HR; split=> n x ??; by split; [apply HQ|apply HR]. Qed.
376
Lemma or_intro_l P Q : P  (P  Q).
377
Proof. split=> n x ??; left; auto. Qed.
378
Lemma or_intro_r P Q : Q  (P  Q).
379
Proof. split=> n x ??; right; auto. Qed.
380
Lemma or_elim P Q R : P  R  Q  R  (P  Q)  R.
381
Proof. intros HP HQ; split=> n x ? [?|?]. by apply HP. by apply HQ. Qed.
382
Lemma impl_intro_r P Q R : (P  Q)  R  P  (Q  R).
Robbert Krebbers's avatar
Robbert Krebbers committed
383
Proof.
384 385
  intros HQ; split=> n x ?? n' x' ????.
  apply HQ; naive_solver eauto using uPred_weaken.
Robbert Krebbers's avatar
Robbert Krebbers committed
386
Qed.
387
Lemma impl_elim P Q R : P  (Q  R)  P  Q  P  R.
388
Proof. by intros HP HP'; split=> n x ??; apply HP with n x, HP'. Qed.
389
Lemma forall_intro {A} P (Ψ : A  uPred M): ( a, P  Ψ a)  P  ( a, Ψ a).
390
Proof. by intros HPΨ; split=> n x ?? a; apply HPΨ. Qed.
391
Lemma forall_elim {A} {Ψ : A  uPred M} a : ( a, Ψ a)  Ψ a.
392
Proof. split=> n x ? HP; apply HP. Qed.
393
Lemma exist_intro {A} {Ψ : A  uPred M} a : Ψ a  ( a, Ψ a).
394
Proof. by split=> n x ??; exists a. Qed.
395
Lemma exist_elim {A} (Φ : A  uPred M) Q : ( a, Φ a  Q)  ( a, Φ a)  Q.
396
Proof. by intros HΦΨ; split=> n x ? [a ?]; apply HΦΨ with a. Qed.
397
Lemma eq_refl {A : cofeT} (a : A) P : P  (a  a).
398
Proof. by split=> n x ??; simpl. Qed.
399 400
Lemma eq_rewrite {A : cofeT} a b (Ψ : A  uPred M) P
  `{HΨ :  n, Proper (dist n ==> dist n) Ψ} : P  (a  b)  P  Ψ a  P  Ψ b.
401
Proof.
402 403
  intros Hab Ha; split=> n x ??.
  apply HΨ with n a; auto. by symmetry; apply Hab with x. by apply Ha.
404
Qed.
405
Lemma eq_equiv `{Empty M, !CMRAIdentity M} {A : cofeT} (a b : A) :
406
  True  (a  b)  a  b.
407
Proof.
408 409
  intros Hab; apply equiv_dist; intros n; apply Hab with ; last done.
  apply cmra_valid_validN, cmra_empty_valid.
410
Qed.
411
Lemma iff_equiv P Q : True  (P  Q)  P  Q.
412
Proof. by intros HPQ; split=> n x ?; split; intros; apply HPQ with n x. Qed.
413 414

(* Derived logical stuff *)
Robbert Krebbers's avatar
Robbert Krebbers committed
415 416
Lemma True_intro P : P  True.
Proof. by apply const_intro. Qed.
417
Lemma and_elim_l' P Q R : P  R  (P  Q)  R.
418
Proof. by rewrite and_elim_l. Qed.
419
Lemma and_elim_r' P Q R : Q  R  (P  Q)  R.
420
Proof. by rewrite and_elim_r. Qed.
421
Lemma or_intro_l' P Q R : P  Q  P  (Q  R).
422
Proof. intros ->; apply or_intro_l. Qed.
423
Lemma or_intro_r' P Q R : P  R  P  (Q  R).
424
Proof. intros ->; apply or_intro_r. Qed.
425
Lemma exist_intro' {A} P (Ψ : A  uPred M) a : P  Ψ a  P  ( a, Ψ a).
426
Proof. intros ->; apply exist_intro. Qed.
Ralf Jung's avatar
Ralf Jung committed
427
Lemma forall_elim' {A} P (Ψ : A  uPred M) : P  ( a, Ψ a)  ( a, P  Ψ a).
428
Proof. move=> HP a. by rewrite HP forall_elim. Qed.
429

430
Hint Resolve or_elim or_intro_l' or_intro_r'.
Robbert Krebbers's avatar
Robbert Krebbers committed
431 432
Hint Resolve and_intro and_elim_l' and_elim_r'.
Hint Immediate True_intro False_elim.
433

434 435
Lemma impl_intro_l P Q R : (Q  P)  R  P  (Q  R).
Proof. intros HR; apply impl_intro_r; rewrite -HR; auto. Qed.
436 437 438 439 440 441 442 443
Lemma impl_elim_l P Q : ((P  Q)  P)  Q.
Proof. apply impl_elim with P; auto. Qed.
Lemma impl_elim_r P Q : (P  (P  Q))  Q.
Proof. apply impl_elim with P; auto. Qed.
Lemma impl_elim_l' P Q R : P  (Q  R)  (P  Q)  R.
Proof. intros; apply impl_elim with Q; auto. Qed.
Lemma impl_elim_r' P Q R : Q  (P  R)  (P  Q)  R.
Proof. intros; apply impl_elim with P; auto. Qed.
444
Lemma impl_entails P Q : True  (P  Q)  P  Q.
445
Proof. intros HPQ; apply impl_elim with P; rewrite -?HPQ; auto. Qed.
446
Lemma entails_impl P Q : (P  Q)  True  (P  Q).
447
Proof. auto using impl_intro_l. Qed.
448

449 450
Lemma const_mono φ1 φ2 : (φ1  φ2)   φ1   φ2.
Proof. intros; apply const_elim with φ1; eauto using const_intro. Qed.
451
Lemma and_mono P P' Q Q' : P  Q  P'  Q'  (P  P')  (Q  Q').
452
Proof. auto. Qed.
453 454 455 456
Lemma and_mono_l P P' Q : P  Q  (P  P')  (Q  P').
Proof. by intros; apply and_mono. Qed.
Lemma and_mono_r P P' Q' : P'  Q'  (P  P')  (P  Q').
Proof. by apply and_mono. Qed.
457
Lemma or_mono P P' Q Q' : P  Q  P'  Q'  (P  P')  (Q  Q').
458
Proof. auto. Qed.
459 460 461 462
Lemma or_mono_l P P' Q : P  Q  (P  P')  (Q  P').
Proof. by intros; apply or_mono. Qed.
Lemma or_mono_r P P' Q' : P'  Q'  (P  P')  (P  Q').
Proof. by apply or_mono. Qed.
463
Lemma impl_mono P P' Q Q' : Q  P  P'  Q'  (P  P')  (Q  Q').
464
Proof.
465
  intros HP HQ'; apply impl_intro_l; rewrite -HQ'.
466
  apply impl_elim with P; eauto.
467
Qed.
468 469
Lemma forall_mono {A} (Φ Ψ : A  uPred M) :
  ( a, Φ a  Ψ a)  ( a, Φ a)  ( a, Ψ a).
470
Proof.
471
  intros HP. apply forall_intro=> a; rewrite -(HP a); apply forall_elim.
472
Qed.
473 474 475
Lemma exist_mono {A} (Φ Ψ : A  uPred M) :
  ( a, Φ a  Ψ a)  ( a, Φ a)  ( a, Ψ a).
Proof. intros HΦ. apply exist_elim=> a; rewrite (HΦ a); apply exist_intro. Qed.
476
Global Instance const_mono' : Proper (impl ==> ()) (@uPred_const M).
477
Proof. intros φ1 φ2; apply const_mono. Qed.
478
Global Instance and_mono' : Proper (() ==> () ==> ()) (@uPred_and M).
Robbert Krebbers's avatar
Robbert Krebbers committed
479
Proof. by intros P P' HP Q Q' HQ; apply and_mono. Qed.
480 481 482
Global Instance and_flip_mono' :
  Proper (flip () ==> flip () ==> flip ()) (@uPred_and M).
Proof. by intros P P' HP Q Q' HQ; apply and_mono. Qed.
483
Global Instance or_mono' : Proper (() ==> () ==> ()) (@uPred_or M).
Robbert Krebbers's avatar
Robbert Krebbers committed
484
Proof. by intros P P' HP Q Q' HQ; apply or_mono. Qed.
485 486 487
Global Instance or_flip_mono' :
  Proper (flip () ==> flip () ==> flip ()) (@uPred_or M).
Proof. by intros P P' HP Q Q' HQ; apply or_mono. Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
488
Global Instance impl_mono' :
489
  Proper (flip () ==> () ==> ()) (@uPred_impl M).
Robbert Krebbers's avatar
Robbert Krebbers committed
490 491
Proof. by intros P P' HP Q Q' HQ; apply impl_mono. Qed.
Global Instance forall_mono' A :
492
  Proper (pointwise_relation _ () ==> ()) (@uPred_forall M A).
Robbert Krebbers's avatar
Robbert Krebbers committed
493 494
Proof. intros P1 P2; apply forall_mono. Qed.
Global Instance exist_mono' A :
495
  Proper (pointwise_relation _ () ==> ()) (@uPred_exist M A).
Robbert Krebbers's avatar
Robbert Krebbers committed
496
Proof. intros P1 P2; apply exist_mono. Qed.
497

498 499 500 501 502 503
Global Instance and_idem : IdemP () (@uPred_and M).
Proof. intros P; apply (anti_symm ()); auto. Qed.
Global Instance or_idem : IdemP () (@uPred_or M).
Proof. intros P; apply (anti_symm ()); auto. Qed.
Global Instance and_comm : Comm () (@uPred_and M).
Proof. intros P Q; apply (anti_symm ()); auto. Qed.
504
Global Instance True_and : LeftId () True%I (@uPred_and M).
505
Proof. intros P; apply (anti_symm ()); auto. Qed.
506
Global Instance and_True : RightId () True%I (@uPred_and M).
507
Proof. intros P; apply (anti_symm ()); auto. Qed.
508
Global Instance False_and : LeftAbsorb () False%I (@uPred_and M).
509
Proof. intros P; apply (anti_symm ()); auto. Qed.
510
Global Instance and_False : RightAbsorb () False%I (@uPred_and M).
511
Proof. intros P; apply (anti_symm ()); auto. Qed.
512
Global Instance True_or : LeftAbsorb () True%I (@uPred_or M).
513
Proof. intros P; apply (anti_symm ()); auto. Qed.
514
Global Instance or_True : RightAbsorb () True%I (@uPred_or M).
515
Proof. intros P; apply (anti_symm ()); auto. Qed.
516
Global Instance False_or : LeftId () False%I (@uPred_or M).
517
Proof. intros P; apply (anti_symm ()); auto. Qed.