logic.v 19.6 KB
Newer Older
1
Require Import iris.cmra.
Robbert Krebbers's avatar
Robbert Krebbers committed
2
3
4
5
Local Hint Extern 1 (_  _) => etransitivity; [eassumption|].
Local Hint Extern 1 (_  _) => etransitivity; [|eassumption].
Local Hint Extern 10 (_  _) => omega.

Robbert Krebbers's avatar
Robbert Krebbers committed
6
Structure 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
10
  uPred_ne x1 x2 n : uPred_holds n x1  x1 ={n}= x2  uPred_holds n x2;
  uPred_weaken x1 x2 n1 n2 :
    x1  x2  n2  n1  validN n2 x2  uPred_holds n1 x1  uPred_holds n2 x2
Robbert Krebbers's avatar
Robbert Krebbers committed
11
}.
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

Robbert Krebbers's avatar
Robbert Krebbers committed
15
Instance uPred_equiv (M : cmraT) : Equiv (uPred M) := λ P Q,  x n,
Robbert Krebbers's avatar
Robbert Krebbers committed
16
  validN n x  P n x  Q n x.
Robbert Krebbers's avatar
Robbert Krebbers committed
17
Instance uPred_dist (M : cmraT) : Dist (uPred M) := λ n P Q,  x n',
Robbert Krebbers's avatar
Robbert Krebbers committed
18
  n' < n  validN n' x  P n' x  Q n' x.
Robbert Krebbers's avatar
Robbert Krebbers committed
19
20
21
Program Instance uPred_compl (M : cmraT) : Compl (uPred M) := λ c,
  {| uPred_holds n x := c (S n) n x |}.
Next Obligation. by intros M c x y n ??; simpl in *; apply uPred_ne with x. Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
22
Next Obligation.
23
  intros M c x1 x2 n1 n2 ????; simpl in *.
Robbert Krebbers's avatar
Robbert Krebbers committed
24
  apply (chain_cauchy c (S n2) (S n1)); eauto using uPred_weaken, cmra_valid_le.
Robbert Krebbers's avatar
Robbert Krebbers committed
25
Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
26
Instance uPred_cofe (M : cmraT) : Cofe (uPred M).
Robbert Krebbers's avatar
Robbert Krebbers committed
27
28
29
30
31
32
33
34
35
36
37
38
Proof.
  split.
  * intros P Q; split; [by intros HPQ n x i ??; apply HPQ|].
    intros HPQ x n ?; apply HPQ with (S n); auto.
  * intros n; split.
    + by intros P x i.
    + by intros P Q HPQ x i ??; symmetry; apply HPQ.
    + by intros P Q Q' HP HQ x i ??; transitivity (Q i x); [apply HP|apply HQ].
  * intros n P Q HPQ x i ??; apply HPQ; auto.
  * intros P Q x i ??; lia.
  * intros c n x i ??; apply (chain_cauchy c (S i) n); auto.
Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
39
40
41
42
43
Instance uPred_holds_ne {M} (P : uPred M) n : Proper (dist n ==> iff) (P n).
Proof. intros x1 x2 Hx; split; eauto using uPred_ne. Qed.
Instance uPred_holds_proper {M} (P : uPred M) n : Proper (() ==> iff) (P n).
Proof. by intros x1 x2 Hx; apply uPred_holds_ne, equiv_dist. Qed.
Definition uPredC (M : cmraT) : cofeT := CofeT (uPred M).
Robbert Krebbers's avatar
Robbert Krebbers committed
44
45

(** functor *)
Robbert Krebbers's avatar
Robbert Krebbers committed
46
Program Definition uPred_map {M1 M2 : cmraT} (f : M2  M1)
Robbert Krebbers's avatar
Robbert Krebbers committed
47
  `{! n, Proper (dist n ==> dist n) f, !CMRAPreserving f}
Robbert Krebbers's avatar
Robbert Krebbers committed
48
  (P : uPred M1) : uPred M2 := {| uPred_holds n x := P n (f x) |}.
49
Next Obligation. by intros M1 M2 f ?? P y1 y2 n ? Hy; simpl; rewrite <-Hy. Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
50
Next Obligation.
Robbert Krebbers's avatar
Robbert Krebbers committed
51
  by intros M1 M2 f ?? P y1 y2 n i ???; simpl; apply uPred_weaken; auto;
Robbert Krebbers's avatar
Robbert Krebbers committed
52
53
    apply validN_preserving || apply included_preserving.
Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
54
Instance uPred_map_ne {M1 M2 : cmraT} (f : M2  M1)
Robbert Krebbers's avatar
Robbert Krebbers committed
55
  `{! n, Proper (dist n ==> dist n) f, !CMRAPreserving f} :
Robbert Krebbers's avatar
Robbert Krebbers committed
56
  Proper (dist n ==> dist n) (uPred_map f).
Robbert Krebbers's avatar
Robbert Krebbers committed
57
58
59
Proof.
  by intros n x1 x2 Hx y n'; split; apply Hx; try apply validN_preserving.
Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
60
61
Definition uPredC_map {M1 M2 : cmraT} (f : M2 -n> M1) `{!CMRAPreserving f} :
  uPredC M1 -n> uPredC M2 := CofeMor (uPred_map f : uPredC M1  uPredC M2).
Robbert Krebbers's avatar
Robbert Krebbers committed
62
63
64
65
66
67
Lemma upredC_map_ne {M1 M2 : cmraT} (f g : M2 -n> M1)
    `{!CMRAPreserving f, !CMRAPreserving g} n :
  f ={n}= g  uPredC_map f ={n}= uPredC_map g.
Proof.
  by intros Hfg P y n' ??; simpl; rewrite (dist_le _ _ _ _(Hfg y)) by lia.
Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
68
69

(** logical entailement *)
Robbert Krebbers's avatar
Robbert Krebbers committed
70
Instance uPred_entails {M} : SubsetEq (uPred M) := λ P Q,  x n,
Robbert Krebbers's avatar
Robbert Krebbers committed
71
72
73
  validN n x  P n x  Q n x.

(** logical connectives *)
Robbert Krebbers's avatar
Robbert Krebbers committed
74
75
Program Definition uPred_const {M} (P : Prop) : uPred M :=
  {| uPred_holds n x := P |}.
Robbert Krebbers's avatar
Robbert Krebbers committed
76
Solve Obligations with done.
77
Instance uPred_inhabited M : Inhabited (uPred M) := populate (uPred_const True).
Robbert Krebbers's avatar
Robbert Krebbers committed
78

Robbert Krebbers's avatar
Robbert Krebbers committed
79
80
81
82
83
84
85
86
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 :=
  {| uPred_holds n x :=  x' n',
Robbert Krebbers's avatar
Robbert Krebbers committed
87
88
       x  x'  n'  n  validN n' x'  P n' x'  Q n' x' |}.
Next Obligation.
89
  intros M P Q x1' x1 n1 HPQ Hx1 x2 n2 ????.
Robbert Krebbers's avatar
Robbert Krebbers committed
90
91
92
  destruct (cmra_included_dist_l x1 x2 x1' n1) as (x2'&?&Hx2); auto.
  assert (x2' ={n2}= x2) as Hx2' by (by apply dist_le with n1).
  assert (validN n2 x2') by (by rewrite Hx2'); rewrite <-Hx2'.
Robbert Krebbers's avatar
Robbert Krebbers committed
93
  by apply HPQ, uPred_weaken with x2' n2, uPred_ne with x2.
Robbert Krebbers's avatar
Robbert Krebbers committed
94
95
96
Qed.
Next Obligation. naive_solver eauto 2 with lia. Qed.

Robbert Krebbers's avatar
Robbert Krebbers committed
97
98
99
100
101
102
Program Definition uPred_forall {M A} (P : A  uPred M) : uPred M :=
  {| uPred_holds n x :=  a, P a n x |}.
Solve Obligations with naive_solver eauto 2 using uPred_ne, uPred_weaken.
Program Definition uPred_exist {M A} (P : A  uPred M) : uPred M :=
  {| uPred_holds n x :=  a, P a n x |}.
Solve Obligations with naive_solver eauto 2 using uPred_ne, uPred_weaken.
Robbert Krebbers's avatar
Robbert Krebbers committed
103

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

Robbert Krebbers's avatar
Robbert Krebbers committed
108
109
Program Definition uPred_sep {M} (P Q : uPred M) : uPred M :=
  {| uPred_holds n x :=  x1 x2, x ={n}= x1  x2  P n x1  Q n x2 |}.
Robbert Krebbers's avatar
Robbert Krebbers committed
110
Next Obligation.
111
  by intros M P Q x y n (x1&x2&?&?&?) Hxy; exists x1, x2; rewrite <-Hxy.
Robbert Krebbers's avatar
Robbert Krebbers committed
112
113
Qed.
Next Obligation.
114
  intros M P Q x y n1 n2 Hxy ?? (x1&x2&Hx&?&?).
Robbert Krebbers's avatar
Robbert Krebbers committed
115
116
117
118
  assert ( x2', y ={n2}= x1  x2'  x2  x2') as (x2'&Hy&?).
  { rewrite ra_included_spec in Hxy; destruct Hxy as [z Hy].
    exists (x2  z); split; eauto using ra_included_l.
    apply dist_le with n1; auto. by rewrite (associative op), <-Hx, Hy. }
119
  exists x1, x2'; split_ands; auto.
Robbert Krebbers's avatar
Robbert Krebbers committed
120
  * apply uPred_weaken with x1 n1; auto.
Robbert Krebbers's avatar
Robbert Krebbers committed
121
    by apply cmra_valid_op_l with x2'; rewrite <-Hy.
Robbert Krebbers's avatar
Robbert Krebbers committed
122
  * apply uPred_weaken with x2 n1; auto.
Robbert Krebbers's avatar
Robbert Krebbers committed
123
124
125
    by apply cmra_valid_op_r with x1; rewrite <-Hy.
Qed.

Robbert Krebbers's avatar
Robbert Krebbers committed
126
127
Program Definition uPred_wand {M} (P Q : uPred M) : uPred M :=
  {| uPred_holds n x :=  x' n',
Robbert Krebbers's avatar
Robbert Krebbers committed
128
129
       n'  n  validN n' (x  x')  P n' x'  Q n' (x  x') |}.
Next Obligation.
130
  intros M P Q x1 x2 n1 HPQ Hx x3 n2 ???; simpl in *.
Robbert Krebbers's avatar
Robbert Krebbers committed
131
132
133
134
  rewrite <-(dist_le _ _ _ _ Hx) by done; apply HPQ; auto.
  by rewrite (dist_le _ _ _ n2 Hx).
Qed.
Next Obligation.
135
  intros M P Q x1 x2 n1 n2 ??? HPQ x3 n3 ???; simpl in *.
Robbert Krebbers's avatar
Robbert Krebbers committed
136
  apply uPred_weaken with (x1  x3) n3; auto using ra_preserving_r.
Robbert Krebbers's avatar
Robbert Krebbers committed
137
138
139
140
  apply HPQ; auto.
  apply cmra_valid_included with (x2  x3); auto using ra_preserving_r.
Qed.

Robbert Krebbers's avatar
Robbert Krebbers committed
141
142
143
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.
Robbert Krebbers's avatar
Robbert Krebbers committed
144
Next Obligation.
145
  intros M P x1 x2 [|n1] [|n2] ????; auto with lia.
Robbert Krebbers's avatar
Robbert Krebbers committed
146
  apply uPred_weaken with x1 n1; eauto using cmra_valid_S.
Robbert Krebbers's avatar
Robbert Krebbers committed
147
Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
148
149
Program Definition uPred_always {M} (P : uPred M) : uPred M :=
  {| uPred_holds n x := P n (unit x) |}.
150
Next Obligation. by intros M P x1 x2 n ? Hx; simpl in *; rewrite <-Hx. Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
151
Next Obligation.
Robbert Krebbers's avatar
Robbert Krebbers committed
152
  intros M P x1 x2 n1 n2 ????; eapply uPred_weaken with (unit x1) n1;
Robbert Krebbers's avatar
Robbert Krebbers committed
153
154
155
    auto using ra_unit_preserving, cmra_unit_valid.
Qed.

Robbert Krebbers's avatar
Robbert Krebbers committed
156
157
Program Definition uPred_own {M : cmraT} (a : M) : uPred M :=
  {| uPred_holds n x :=  a', x ={n}= a  a' |}.
158
Next Obligation. by intros M a x1 x2 n [a' Hx] ?; exists a'; rewrite <-Hx. Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
159
Next Obligation.
160
  intros M a x1 x n1 n2; rewrite ra_included_spec; intros [x2 Hx] ?? [a' Hx1].
Robbert Krebbers's avatar
Robbert Krebbers committed
161
162
  exists (a'  x2). by rewrite (associative op), <-(dist_le _ _ _ _ Hx1), Hx.
Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
163
164
Program Definition uPred_valid {M : cmraT} (a : M) : uPred M :=
  {| uPred_holds n x := validN n a |}.
Robbert Krebbers's avatar
Robbert Krebbers committed
165
166
Solve Obligations with naive_solver eauto 2 using cmra_valid_le.

Robbert Krebbers's avatar
Robbert Krebbers committed
167
168
Definition uPred_fixpoint {M} (P : uPred M  uPred M)
  `{!Contractive P} : uPred M := fixpoint P (uPred_const True).
Robbert Krebbers's avatar
Robbert Krebbers committed
169

Robbert Krebbers's avatar
Robbert Krebbers committed
170
171
172
Delimit Scope uPred_scope with I.
Bind Scope uPred_scope with uPred.
Arguments uPred_holds {_} _%I _ _.
Robbert Krebbers's avatar
Robbert Krebbers committed
173

Robbert Krebbers's avatar
Robbert Krebbers committed
174
175
176
177
178
179
180
Notation "'False'" := (uPred_const False) : uPred_scope.
Notation "'True'" := (uPred_const True) : uPred_scope.
Infix "∧" := uPred_and : uPred_scope.
Infix "∨" := uPred_or : uPred_scope.
Infix "→" := uPred_impl : uPred_scope.
Infix "★" := uPred_sep (at level 80, right associativity) : uPred_scope.
Infix "-★" := uPred_wand (at level 90) : uPred_scope.
Robbert Krebbers's avatar
Robbert Krebbers committed
181
Notation "∀ x .. y , P" :=
Robbert Krebbers's avatar
Robbert Krebbers committed
182
  (uPred_forall (λ x, .. (uPred_forall (λ y, P)) ..)) : uPred_scope.
Robbert Krebbers's avatar
Robbert Krebbers committed
183
Notation "∃ x .. y , P" :=
Robbert Krebbers's avatar
Robbert Krebbers committed
184
185
186
  (uPred_exist (λ x, .. (uPred_exist (λ y, P)) ..)) : uPred_scope.
Notation "▷ P" := (uPred_later P) (at level 20) : uPred_scope.
Notation "□ P" := (uPred_always P) (at level 20) : uPred_scope.
Robbert Krebbers's avatar
Robbert Krebbers committed
187
188

Section logic.
189
Context {M : cmraT}.
Robbert Krebbers's avatar
Robbert Krebbers committed
190
Implicit Types P Q : uPred M.
Robbert Krebbers's avatar
Robbert Krebbers committed
191

Robbert Krebbers's avatar
Robbert Krebbers committed
192
Global Instance uPred_preorder : PreOrder (() : relation (uPred M)).
Robbert Krebbers's avatar
Robbert Krebbers committed
193
Proof. split. by intros P x i. by intros P Q Q' HP HQ x i ??; apply HQ, HP. Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
194
Lemma uPred_equiv_spec P Q : P  Q  P  Q  Q  P.
Robbert Krebbers's avatar
Robbert Krebbers committed
195
196
197
198
199
200
201
Proof.
  split.
  * intros HPQ; split; intros x i; apply HPQ.
  * by intros [HPQ HQP]; intros x i ?; split; [apply HPQ|apply HQP].
Qed.

(** Non-expansiveness *)
Robbert Krebbers's avatar
Robbert Krebbers committed
202
Global Instance uPred_const_proper : Proper (iff ==> ()) (@uPred_const M).
Robbert Krebbers's avatar
Robbert Krebbers committed
203
Proof. intros P Q HPQ ???; apply HPQ. Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
204
205
Global Instance uPred_and_ne n :
  Proper (dist n ==> dist n ==> dist n) (@uPred_and M).
Robbert Krebbers's avatar
Robbert Krebbers committed
206
207
208
Proof.
  intros P P' HP Q Q' HQ; split; intros [??]; split; by apply HP || by apply HQ.
Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
209
210
211
212
Global Instance uPred_and_proper :
  Proper (() ==> () ==> ()) (@uPred_and M) := ne_proper_2 _.
Global Instance uPred_or_ne n :
  Proper (dist n ==> dist n ==> dist n) (@uPred_or M).
Robbert Krebbers's avatar
Robbert Krebbers committed
213
214
215
216
Proof.
  intros P P' HP Q Q' HQ; split; intros [?|?];
    first [by left; apply HP | by right; apply HQ].
Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
217
218
219
220
Global Instance uPred_or_proper :
  Proper (() ==> () ==> ()) (@uPred_or M) := ne_proper_2 _.
Global Instance uPred_impl_ne n :
  Proper (dist n ==> dist n ==> dist n) (@uPred_impl M).
Robbert Krebbers's avatar
Robbert Krebbers committed
221
222
223
Proof.
  intros P P' HP Q Q' HQ; split; intros HPQ x' n'' ????; apply HQ,HPQ,HP; auto.
Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
224
225
226
227
Global Instance uPred_impl_proper :
  Proper (() ==> () ==> ()) (@uPred_impl M) := ne_proper_2 _.
Global Instance uPred_sep_ne n :
  Proper (dist n ==> dist n ==> dist n) (@uPred_sep M).
Robbert Krebbers's avatar
Robbert Krebbers committed
228
229
Proof.
  intros P P' HP Q Q' HQ x n' ? Hx'; split; intros (x1&x2&Hx&?&?);
230
    exists x1, x2; rewrite  Hx in Hx'; split_ands; try apply HP; try apply HQ;
Robbert Krebbers's avatar
Robbert Krebbers committed
231
232
    eauto using cmra_valid_op_l, cmra_valid_op_r.
Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
233
234
235
236
Global Instance uPred_sep_proper :
  Proper (() ==> () ==> ()) (@uPred_sep M) := ne_proper_2 _.
Global Instance uPred_wand_ne n :
  Proper (dist n ==> dist n ==> dist n) (@uPred_wand M).
Robbert Krebbers's avatar
Robbert Krebbers committed
237
238
239
240
Proof.
  intros P P' HP Q Q' HQ x n' ??; split; intros HPQ x' n'' ???;
    apply HQ, HPQ, HP; eauto using cmra_valid_op_r.
Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
241
242
243
244
Global Instance uPred_wand_proper :
  Proper (() ==> () ==> ()) (@uPred_wand M) := ne_proper_2 _.
Global Instance uPred_eq_ne {A : cofeT} n :
  Proper (dist n ==> dist n ==> dist n) (@uPred_eq M A).
Robbert Krebbers's avatar
Robbert Krebbers committed
245
246
247
248
249
Proof.
  intros x x' Hx y y' Hy z n'; split; intros; simpl in *.
  * by rewrite <-(dist_le _ _ _ _ Hx), <-(dist_le _ _ _ _ Hy) by auto.
  * by rewrite (dist_le _ _ _ _ Hx), (dist_le _ _ _ _ Hy) by auto.
Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
250
251
252
253
Global Instance uPred_eq_proper {A : cofeT} :
  Proper (() ==> () ==> ()) (@uPred_eq M A) := ne_proper_2 _.
Global Instance uPred_forall_ne {A : cofeT} :
  Proper (pointwise_relation _ (dist n) ==> dist n) (@uPred_forall M A).
Robbert Krebbers's avatar
Robbert Krebbers committed
254
Proof. by intros n P1 P2 HP12 x n'; split; intros HP a; apply HP12. Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
255
256
Global Instance uPred_forall_proper {A : cofeT} :
  Proper (pointwise_relation _ () ==> ()) (@uPred_forall M A).
257
Proof. by intros P1 P2 HP12 x n'; split; intros HP a; apply HP12. Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
258
259
Global Instance uPred_exists_ne {A : cofeT} :
  Proper (pointwise_relation _ (dist n) ==> dist n) (@uPred_exist M A).
Robbert Krebbers's avatar
Robbert Krebbers committed
260
261
262
Proof.
  by intros n P1 P2 HP12 x n'; split; intros [a HP]; exists a; apply HP12.
Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
263
264
Global Instance uPred_exist_proper {A : cofeT} :
  Proper (pointwise_relation _ () ==> ()) (@uPred_exist M A).
265
266
267
Proof.
  by intros P1 P2 HP12 x n'; split; intros [a HP]; exists a; apply HP12.
Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
268
Global Instance uPred_later_contractive : Contractive (@uPred_later M).
Robbert Krebbers's avatar
Robbert Krebbers committed
269
270
271
272
Proof.
  intros n P Q HPQ x [|n'] ??; simpl; [done|].
  apply HPQ; eauto using cmra_valid_S.
Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
273
274
275
Global Instance uPred_later_proper :
  Proper (() ==> ()) (@uPred_later M) := ne_proper _.
Global Instance uPred_always_ne n: Proper (dist n ==> dist n) (@uPred_always M).
Robbert Krebbers's avatar
Robbert Krebbers committed
276
Proof. intros P1 P2 HP x n'; split; apply HP; eauto using cmra_unit_valid. Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
277
278
279
Global Instance uPred_always_proper :
  Proper (() ==> ()) (@uPred_always M) := ne_proper _.
Global Instance uPred_own_ne n : Proper (dist n ==> dist n) (@uPred_own M).
Robbert Krebbers's avatar
Robbert Krebbers committed
280
281
282
283
Proof.
  by intros a1 a2 Ha x n'; split; intros [a' ?]; exists a'; simpl; first
    [rewrite <-(dist_le _ _ _ _ Ha) by lia|rewrite (dist_le _ _ _ _ Ha) by lia].
Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
284
285
Global Instance uPred_own_proper :
  Proper (() ==> ()) (@uPred_own M) := ne_proper _.
Robbert Krebbers's avatar
Robbert Krebbers committed
286
287

(** Introduction and elimination rules *)
Robbert Krebbers's avatar
Robbert Krebbers committed
288
Lemma uPred_True_intro P : P  True%I.
Robbert Krebbers's avatar
Robbert Krebbers committed
289
Proof. done. Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
290
Lemma uPred_False_elim P : False%I  P.
Robbert Krebbers's avatar
Robbert Krebbers committed
291
Proof. by intros x n ?. Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
292
Lemma uPred_and_elim_l P Q : (P  Q)%I  P.
Robbert Krebbers's avatar
Robbert Krebbers committed
293
Proof. by intros x n ? [??]. Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
294
Lemma uPred_and_elim_r P Q : (P  Q)%I  Q.
Robbert Krebbers's avatar
Robbert Krebbers committed
295
Proof. by intros x n ? [??]. Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
296
Lemma uPred_and_intro R P Q : R  P  R  Q  R  (P  Q)%I.
Robbert Krebbers's avatar
Robbert Krebbers committed
297
Proof. intros HP HQ x n ??; split. by apply HP. by apply HQ. Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
298
Lemma uPred_or_intro_l P Q : P  (P  Q)%I.
Robbert Krebbers's avatar
Robbert Krebbers committed
299
Proof. by left. Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
300
Lemma uPred_or_intro_r P Q : Q  (P  Q)%I.
Robbert Krebbers's avatar
Robbert Krebbers committed
301
Proof. by right. Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
302
Lemma uPred_or_elim R P Q : P  R  Q  R  (P  Q)%I  R.
Robbert Krebbers's avatar
Robbert Krebbers committed
303
Proof. intros HP HQ x n ? [?|?]. by apply HP. by apply HQ. Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
304
Lemma uPred_impl_intro P Q R : (R  P)%I  Q  R  (P  Q)%I.
Robbert Krebbers's avatar
Robbert Krebbers committed
305
Proof.
Robbert Krebbers's avatar
Robbert Krebbers committed
306
  intros HQ x n ?? x' n' ????; apply HQ; naive_solver eauto using uPred_weaken.
Robbert Krebbers's avatar
Robbert Krebbers committed
307
Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
308
Lemma uPred_impl_elim P Q : ((P  Q)  P)%I  Q.
Robbert Krebbers's avatar
Robbert Krebbers committed
309
Proof. by intros x n ? [HQ HP]; apply HQ. Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
310
Lemma uPred_forall_intro P `(Q: A  uPred M): ( a, P  Q a)  P  ( a, Q a)%I.
311
Proof. by intros HPQ x n ?? a; apply HPQ. Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
312
Lemma uPred_forall_elim `(P : A  uPred M) a : ( a, P a)%I  P a.
Robbert Krebbers's avatar
Robbert Krebbers committed
313
Proof. intros x n ? HP; apply HP. Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
314
Lemma uPred_exist_intro `(P : A  uPred M) a : P a  ( a, P a)%I.
315
Proof. by intros x n ??; exists a. Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
316
Lemma uPred_exist_elim `(P : A  uPred M) Q : ( a, P a  Q)  ( a, P a)%I  Q.
317
Proof. by intros HPQ x n ? [a ?]; apply HPQ with a. Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
318
319

(* BI connectives *)
Robbert Krebbers's avatar
Robbert Krebbers committed
320
Lemma uPred_sep_elim_l P Q : (P  Q)%I  P.
Robbert Krebbers's avatar
Robbert Krebbers committed
321
322
Proof.
  intros x n Hvalid (x1&x2&Hx&?&?); rewrite Hx in Hvalid |- *.
Robbert Krebbers's avatar
Robbert Krebbers committed
323
  by apply uPred_weaken with x1 n; auto using ra_included_l.
Robbert Krebbers's avatar
Robbert Krebbers committed
324
Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
325
Global Instance uPred_sep_left_id : LeftId () True%I (@uPred_sep M).
Robbert Krebbers's avatar
Robbert Krebbers committed
326
327
328
Proof.
  intros P x n Hvalid; split.
  * intros (x1&x2&Hx&_&?); rewrite Hx in Hvalid |- *.
Robbert Krebbers's avatar
Robbert Krebbers committed
329
    apply uPred_weaken with x2 n; auto using ra_included_r.
330
  * by intros ?; exists (unit x), x; rewrite ra_unit_l.
Robbert Krebbers's avatar
Robbert Krebbers committed
331
Qed. 
Robbert Krebbers's avatar
Robbert Krebbers committed
332
Global Instance uPred_sep_commutative : Commutative () (@uPred_sep M).
Robbert Krebbers's avatar
Robbert Krebbers committed
333
334
Proof.
  by intros P Q x n ?; split;
335
    intros (x1&x2&?&?&?); exists x2, x1; rewrite (commutative op).
Robbert Krebbers's avatar
Robbert Krebbers committed
336
Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
337
Global Instance uPred_sep_associative : Associative () (@uPred_sep M).
Robbert Krebbers's avatar
Robbert Krebbers committed
338
339
Proof.
  intros P Q R x n ?; split.
340
  * intros (x1&x2&Hx&?&y1&y2&Hy&?&?); exists (x1  y1), y2; split_ands; auto.
Robbert Krebbers's avatar
Robbert Krebbers committed
341
    + by rewrite <-(associative op), <-Hy, <-Hx.
342
343
    + by exists x1, y1.
  * intros (x1&x2&Hx&(y1&y2&Hy&?&?)&?); exists y1, (y2  x2); split_ands; auto.
Robbert Krebbers's avatar
Robbert Krebbers committed
344
    + by rewrite (associative op), <-Hy, <-Hx.
345
    + by exists y2, x2.
Robbert Krebbers's avatar
Robbert Krebbers committed
346
Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
347
Lemma uPred_wand_intro P Q R : (R  P)%I  Q  R  (P - Q)%I.
Robbert Krebbers's avatar
Robbert Krebbers committed
348
349
Proof.
  intros HPQ x n ?? x' n' ???; apply HPQ; auto.
350
  exists x, x'; split_ands; auto.
Robbert Krebbers's avatar
Robbert Krebbers committed
351
  eapply uPred_weaken with x n; eauto using cmra_valid_op_l.
Robbert Krebbers's avatar
Robbert Krebbers committed
352
Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
353
Lemma uPred_wand_elim P Q : ((P - Q)  P)%I  Q.
Robbert Krebbers's avatar
Robbert Krebbers committed
354
355
356
Proof.
  by intros x n Hvalid (x1&x2&Hx&HPQ&?); rewrite Hx in Hvalid |- *; apply HPQ.
Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
357
Lemma uPred_sep_or P Q R : ((P  Q)  R)%I  ((P  R)  (Q  R))%I.
Robbert Krebbers's avatar
Robbert Krebbers committed
358
Proof.
359
360
  split; [by intros (x1&x2&Hx&[?|?]&?); [left|right]; exists x1, x2|].
  intros [(x1&x2&Hx&?&?)|(x1&x2&Hx&?&?)]; exists x1, x2; split_ands;
Robbert Krebbers's avatar
Robbert Krebbers committed
361
362
    first [by left | by right | done].
Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
363
Lemma uPred_sep_and P Q R : ((P  Q)  R)%I  ((P  R)  (Q  R))%I.
364
Proof. by intros x n ? (x1&x2&Hx&[??]&?); split; exists x1, x2. Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
365
Lemma uPred_sep_exist `(P : A  uPred M) Q :
Robbert Krebbers's avatar
Robbert Krebbers committed
366
367
  (( b, P b)  Q)%I  ( b, P b  Q)%I.
Proof.
368
369
  split; [by intros (x1&x2&Hx&[a ?]&?); exists a, x1, x2|].
  intros [a (x1&x2&Hx&?&?)]; exists x1, x2; split_ands; by try exists a.
Robbert Krebbers's avatar
Robbert Krebbers committed
370
Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
371
Lemma uPred_sep_forall `(P : A  uPred M) Q :
372
373
  (( a, P a)  Q)%I  ( a, P a  Q)%I.
Proof. by intros x n ? (x1&x2&Hx&?&?); intros a; exists x1, x2. Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
374
375

(* Later *)
Robbert Krebbers's avatar
Robbert Krebbers committed
376
Lemma uPred_later_weaken P : P  ( P)%I.
Robbert Krebbers's avatar
Robbert Krebbers committed
377
378
Proof.
  intros x [|n] ??; simpl in *; [done|].
Robbert Krebbers's avatar
Robbert Krebbers committed
379
  apply uPred_weaken with x (S n); auto using cmra_valid_S.
Robbert Krebbers's avatar
Robbert Krebbers committed
380
Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
381
Lemma uPred_lub P : ( P  P)%I  P.
Robbert Krebbers's avatar
Robbert Krebbers committed
382
383
Proof.
  intros x n ? HP; induction n as [|n IH]; [by apply HP|].
Robbert Krebbers's avatar
Robbert Krebbers committed
384
  apply HP, IH, uPred_weaken with x (S n); eauto using cmra_valid_S.
Robbert Krebbers's avatar
Robbert Krebbers committed
385
Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
386
Lemma uPred_later_impl P Q : ( (P  Q))%I  ( P   Q)%I.
Robbert Krebbers's avatar
Robbert Krebbers committed
387
388
389
390
Proof.
  intros x [|n] ? HPQ x' [|n'] ???; auto with lia.
  apply HPQ; auto using cmra_valid_S.
Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
391
Lemma uPred_later_and P Q : ( (P  Q))%I  ( P   Q)%I.
Robbert Krebbers's avatar
Robbert Krebbers committed
392
Proof. by intros x [|n]; split. Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
393
Lemma uPred_later_or P Q : ( (P  Q))%I  ( P   Q)%I.
Robbert Krebbers's avatar
Robbert Krebbers committed
394
Proof. intros x [|n]; simpl; tauto. Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
395
Lemma uPred_later_forall `(P : A  uPred M) : (  a, P a)%I  ( a,  P a)%I.
Robbert Krebbers's avatar
Robbert Krebbers committed
396
Proof. by intros x [|n]. Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
397
Lemma uPred_later_exist `(P : A  uPred M) : ( a,  P a)%I  (  a, P a)%I.
Robbert Krebbers's avatar
Robbert Krebbers committed
398
Proof. by intros x [|n]. Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
399
Lemma uPred_later_exist' `{Inhabited A} (P : A  uPred M) :
400
  (  a, P a)%I  ( a,  P a)%I.
Robbert Krebbers's avatar
Robbert Krebbers committed
401
402
Proof.
  intros x [|n]; split; try done.
403
  by destruct (_ : Inhabited A) as [a]; exists a.
Robbert Krebbers's avatar
Robbert Krebbers committed
404
Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
405
Lemma uPred_later_sep P Q : ( (P  Q))%I  ( P   Q)%I.
Robbert Krebbers's avatar
Robbert Krebbers committed
406
407
Proof.
  intros x n ?; split.
408
  * destruct n as [|n]; simpl; [by exists x, x|intros (x1&x2&Hx&?&?)].
Robbert Krebbers's avatar
Robbert Krebbers committed
409
410
    destruct (cmra_extend_op x x1 x2 n)
      as ([y1 y2]&Hx'&Hy1&Hy2); auto using cmra_valid_S; simpl in *.
411
    exists y1, y2; split; [by rewrite Hx'|by rewrite Hy1, Hy2].
Robbert Krebbers's avatar
Robbert Krebbers committed
412
  * destruct n as [|n]; simpl; [done|intros (x1&x2&Hx&?&?)].
413
    exists x1, x2; eauto using (dist_S (A:=M)).
Robbert Krebbers's avatar
Robbert Krebbers committed
414
415
416
Qed.

(* Always *)
Robbert Krebbers's avatar
Robbert Krebbers committed
417
Lemma uPred_always_necessity P : ( P)%I  P.
Robbert Krebbers's avatar
Robbert Krebbers committed
418
Proof.
Robbert Krebbers's avatar
Robbert Krebbers committed
419
  intros x n ??; apply uPred_weaken with (unit x) n;auto using ra_included_unit.
Robbert Krebbers's avatar
Robbert Krebbers committed
420
Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
421
Lemma uPred_always_intro P Q : ( P)%I  Q  ( P)%I  ( Q)%I.
Robbert Krebbers's avatar
Robbert Krebbers committed
422
423
424
425
Proof.
  intros HPQ x n ??; apply HPQ; simpl in *; auto using cmra_unit_valid.
  by rewrite ra_unit_idempotent.
Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
426
Lemma uPred_always_impl P Q : ( (P  Q))%I  (P  Q)%I.
Robbert Krebbers's avatar
Robbert Krebbers committed
427
428
429
430
Proof.
  intros x n ? HPQ x' n' ???.
  apply HPQ; auto using ra_unit_preserving, cmra_unit_valid.
Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
431
Lemma uPred_always_and P Q : ( (P  Q))%I  ( P   Q)%I.
Robbert Krebbers's avatar
Robbert Krebbers committed
432
Proof. done. Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
433
Lemma uPred_always_or P Q : ( (P  Q))%I  ( P   Q)%I.
Robbert Krebbers's avatar
Robbert Krebbers committed
434
Proof. done. Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
435
Lemma uPred_always_forall `(P : A  uPred M) : (  a, P a)%I  ( a,  P a)%I.
Robbert Krebbers's avatar
Robbert Krebbers committed
436
Proof. done. Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
437
Lemma uPred_always_exist `(P : A  uPred M) : (  a, P a)%I  ( a,  P a)%I.
Robbert Krebbers's avatar
Robbert Krebbers committed
438
Proof. done. Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
439
Lemma uPred_always_and_always_box P Q : ( P  Q)%I  ( P  Q)%I.
Robbert Krebbers's avatar
Robbert Krebbers committed
440
Proof.
441
  intros x n ? [??]; exists (unit x), x; simpl in *.
Robbert Krebbers's avatar
Robbert Krebbers committed
442
443
444
  by rewrite ra_unit_l, ra_unit_idempotent.
Qed.

Robbert Krebbers's avatar
Robbert Krebbers committed
445
(* Own *)
Robbert Krebbers's avatar
Robbert Krebbers committed
446
447
Lemma uPred_own_op (a1 a2 : M) :
  uPred_own (a1  a2)  (uPred_own a1  uPred_own a2)%I.
Robbert Krebbers's avatar
Robbert Krebbers committed
448
449
450
451
452
453
454
455
Proof.
  intros x n ?; split.
  * intros [z ?]; exists a1, (a2  z); split; [by rewrite (associative op)|].
    split. by exists (unit a1); rewrite ra_unit_r. by exists z.
  * intros (y1&y2&Hx&[z1 Hy1]&[z2 Hy2]); exists (z1  z2).
    rewrite (associative op), <-(commutative op z1), <-!(associative op), <-Hy2.
    by rewrite (associative op), (commutative op z1), <-Hy1.
Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
456
Lemma uPred_own_valid (a : M) : uPred_own a  uPred_valid a.
Robbert Krebbers's avatar
Robbert Krebbers committed
457
458
459
460
Proof.
  intros x n Hv [a' Hx]; simpl; rewrite Hx in Hv; eauto using cmra_valid_op_l.
Qed.

Robbert Krebbers's avatar
Robbert Krebbers committed
461
(* Fix *)
Robbert Krebbers's avatar
Robbert Krebbers committed
462
463
Lemma uPred_fixpoint_unfold (P : uPred M  uPred M) `{!Contractive P} :
  uPred_fixpoint P  P (uPred_fixpoint P).
Robbert Krebbers's avatar
Robbert Krebbers committed
464
465
Proof. apply fixpoint_unfold. Qed.
End logic.