upred.v 54.8 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;
Robbert Krebbers's avatar
Robbert Krebbers committed
8
  uPred_ne n x1 x2 : uPred_holds n x1  x1 {n} x2  uPred_holds n x2;
9
10
  uPred_mono n x1 x2 : uPred_holds n x1  x1  x2  uPred_holds n x2;
  uPred_closed n1 n2 x : uPred_holds n1 x  n2  n1  {n2} x  uPred_holds n2 x
Robbert Krebbers's avatar
Robbert Krebbers committed
11
}.
12
Arguments uPred_holds {_} _ _ _ : simpl never.
Robbert Krebbers's avatar
Robbert Krebbers committed
13
14
Add Printing Constructor uPred.
Instance: Params (@uPred_holds) 3.
Robbert Krebbers's avatar
Robbert Krebbers committed
15

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

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

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

55
Instance uPred_ne' {M} (P : uPred M) n : Proper (dist n ==> iff) (P n).
Robbert Krebbers's avatar
Robbert Krebbers committed
56
Proof. intros x1 x2 Hx; split; eauto using uPred_ne. 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
64
  `{!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.
65
66
67
Next Obligation. naive_solver eauto using uPred_mono, included_preserving. Qed.
Next Obligation. naive_solver eauto using uPred_closed, validN_preserving. Qed.

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

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

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

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

124
125
Hint Resolve uPred_ne uPred_mono uPred_closed : uPred_def.

Robbert Krebbers's avatar
Robbert Krebbers committed
126
(** logical connectives *)
127
Program Definition uPred_const_def {M} (φ : Prop) : uPred M :=
128
  {| uPred_holds n x := φ |}.
Robbert Krebbers's avatar
Robbert Krebbers committed
129
Solve Obligations with done.
130
131
132
133
134
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.

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

137
Program Definition uPred_and_def {M} (P Q : uPred M) : uPred M :=
Robbert Krebbers's avatar
Robbert Krebbers committed
138
  {| uPred_holds n x := P n x  Q n x |}.
139
Solve Obligations with naive_solver eauto 2 with uPred_def.
140
141
142
143
144
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
145
  {| uPred_holds n x := P n x  Q n x |}.
146
Solve Obligations with naive_solver eauto 2 with uPred_def.
147
148
149
150
151
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
152
  {| uPred_holds n x :=  n' x',
Robbert Krebbers's avatar
Robbert Krebbers committed
153
       x  x'  n'  n  {n'} x'  P n' x'  Q n' x' |}.
Robbert Krebbers's avatar
Robbert Krebbers committed
154
Next Obligation.
Robbert Krebbers's avatar
Robbert Krebbers committed
155
156
  intros M P Q n1 x1' x1 HPQ Hx1 n2 x2 ????.
  destruct (cmra_included_dist_l n1 x1 x2 x1') as (x2'&?&Hx2); auto.
157
  assert (x2' {n2} x2) as Hx2' by (by apply dist_le with n1).
158
  assert ({n2} x2') by (by rewrite Hx2'); rewrite -Hx2'.
159
  eauto using uPred_ne.
Robbert Krebbers's avatar
Robbert Krebbers committed
160
Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
161
Next Obligation. intros M P Q [|n] x1 x2; auto with lia. Qed.
162
Next Obligation. intros M P Q [|n1] [|n2] x; auto with lia. Qed.
163
164
165
166
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
167

168
Program Definition uPred_forall_def {M A} (Ψ : A  uPred M) : uPred M :=
169
  {| uPred_holds n x :=  a, Ψ a n x |}.
170
Solve Obligations with naive_solver eauto 2 with uPred_def.
171
172
173
174
175
176
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 :=
177
  {| uPred_holds n x :=  a, Ψ a n x |}.
178
Solve Obligations with naive_solver eauto 2 with uPred_def.
179
180
181
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
182

183
Program Definition uPred_eq_def {M} {A : cofeT} (a1 a2 : A) : uPred M :=
184
  {| uPred_holds n x := a1 {n} a2 |}.
185
Solve Obligations with naive_solver eauto 2 using (dist_le (A:=A)).
186
187
188
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
189

190
Program Definition uPred_sep_def {M} (P Q : uPred M) : uPred M :=
191
  {| uPred_holds n x :=  x1 x2, x {n} x1  x2  P n x1  Q n x2 |}.
Robbert Krebbers's avatar
Robbert Krebbers committed
192
Next Obligation.
Robbert Krebbers's avatar
Robbert Krebbers committed
193
  by intros M P Q n x y (x1&x2&?&?&?) Hxy; exists x1, x2; rewrite -Hxy.
Robbert Krebbers's avatar
Robbert Krebbers committed
194
195
Qed.
Next Obligation.
196
197
198
199
200
201
202
203
  intros M P Q n x y (x1&x2&Hx&?&?) [z Hy].
  exists x1, (x2  z); split_and?; eauto using uPred_mono, cmra_included_l.
  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
204
Qed.
205
206
207
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
208

209
Program Definition uPred_wand_def {M} (P Q : uPred M) : uPred M :=
Robbert Krebbers's avatar
Robbert Krebbers committed
210
  {| uPred_holds n x :=  n' x',
Robbert Krebbers's avatar
Robbert Krebbers committed
211
       n'  n  {n'} (x  x')  P n' x'  Q n' (x  x') |}.
Robbert Krebbers's avatar
Robbert Krebbers committed
212
Next Obligation.
Robbert Krebbers's avatar
Robbert Krebbers committed
213
  intros M P Q n1 x1 x2 HPQ Hx n2 x3 ???; simpl in *.
Robbert Krebbers's avatar
Robbert Krebbers committed
214
  rewrite -(dist_le _ _ _ _ Hx) //; apply HPQ; auto.
Robbert Krebbers's avatar
Robbert Krebbers committed
215
  by rewrite (dist_le _ _ _ _ Hx).
Robbert Krebbers's avatar
Robbert Krebbers committed
216
217
Qed.
Next Obligation.
218
219
  intros M P Q n x1 x2 HPQ ? n3 x3 ???; simpl in *.
  apply uPred_mono with (x1  x3);
220
    eauto using cmra_validN_included, cmra_preserving_r.
Robbert Krebbers's avatar
Robbert Krebbers committed
221
Qed.
222
Next Obligation. naive_solver. Qed.
223
224
225
226
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
227

228
Program Definition uPred_always_def {M} (P : uPred M) : uPred M :=
Ralf Jung's avatar
Ralf Jung committed
229
  {| uPred_holds n x := P n (core x) |}.
230
Next Obligation. by intros M P x1 x2 n ? Hx; rewrite /= -Hx. Qed.
231
232
Next Obligation. naive_solver eauto using uPred_mono, cmra_core_preserving. Qed.
Next Obligation. naive_solver eauto using uPred_closed, cmra_core_validN. Qed.
233
234
235
236
237
238
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 :=
239
240
  {| 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.
241
Next Obligation. intros M P [|n] x1 x2; eauto using uPred_mono. Qed.
242
Next Obligation.
243
  intros M P [|n1] [|n2] x; eauto using uPred_closed, cmra_validN_S with lia.
244
Qed.
245
246
247
248
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
249

250
Program Definition uPred_ownM_def {M : ucmraT} (a : M) : uPred M :=
Robbert Krebbers's avatar
Robbert Krebbers committed
251
  {| uPred_holds n x := a {n} x |}.
Robbert Krebbers's avatar
Robbert Krebbers committed
252
Next Obligation. by intros M a n x1 x2 [a' ?] Hx; exists a'; rewrite -Hx. Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
253
Next Obligation.
254
255
  intros M a n x1 x [a' Hx1] [x2 ->].
  exists (a'  x2). by rewrite (assoc op) Hx1.
Robbert Krebbers's avatar
Robbert Krebbers committed
256
Qed.
257
Next Obligation. naive_solver eauto using cmra_includedN_le. Qed.
258
259
260
261
262
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.

263
Program Definition uPred_valid_def {M : ucmraT} {A : cmraT} (a : A) : uPred M :=
Robbert Krebbers's avatar
Robbert Krebbers committed
264
  {| uPred_holds n x := {n} a |}.
265
Solve Obligations with naive_solver eauto 2 using cmra_validN_le.
266
267
268
269
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
270

271
272
273
274
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.
275
276
Notation "■ φ" := (uPred_const φ%C%type)
  (at level 20, right associativity) : uPred_scope.
Ralf Jung's avatar
Ralf Jung committed
277
Notation "x = y" := (uPred_const (x%C%type = y%C%type)) : uPred_scope.
278
Notation "x ⊥ y" := (uPred_const (x%C%type  y%C%type)) : uPred_scope.
Robbert Krebbers's avatar
Robbert Krebbers committed
279
280
281
Notation "'False'" := (uPred_const False) : uPred_scope.
Notation "'True'" := (uPred_const True) : uPred_scope.
Infix "∧" := uPred_and : uPred_scope.
282
Notation "(∧)" := uPred_and (only parsing) : uPred_scope.
Robbert Krebbers's avatar
Robbert Krebbers committed
283
Infix "∨" := uPred_or : uPred_scope.
284
Notation "(∨)" := uPred_or (only parsing) : uPred_scope.
Robbert Krebbers's avatar
Robbert Krebbers committed
285
286
Infix "→" := uPred_impl : uPred_scope.
Infix "★" := uPred_sep (at level 80, right associativity) : uPred_scope.
287
Notation "(★)" := uPred_sep (only parsing) : uPred_scope.
288
Notation "P -★ Q" := (uPred_wand P Q)
Robbert Krebbers's avatar
Robbert Krebbers committed
289
  (at level 99, Q at level 200, right associativity) : uPred_scope.
Robbert Krebbers's avatar
Robbert Krebbers committed
290
Notation "∀ x .. y , P" :=
291
  (uPred_forall (λ x, .. (uPred_forall (λ y, P)) ..)%I) : uPred_scope.
Robbert Krebbers's avatar
Robbert Krebbers committed
292
Notation "∃ x .. y , P" :=
293
  (uPred_exist (λ x, .. (uPred_exist (λ y, P)) ..)%I) : uPred_scope.
294
295
Notation "□ P" := (uPred_always P)
  (at level 20, right associativity) : uPred_scope.
296
297
Notation "▷ P" := (uPred_later P)
  (at level 20, right associativity) : uPred_scope.
Robbert Krebbers's avatar
Robbert Krebbers committed
298
Infix "≡" := uPred_eq : uPred_scope.
299
Notation "✓ x" := (uPred_valid x) (at level 20) : uPred_scope.
Robbert Krebbers's avatar
Robbert Krebbers committed
300

301
Definition uPred_iff {M} (P Q : uPred M) : uPred M := ((P  Q)  (Q  P))%I.
302
Instance: Params (@uPred_iff) 1.
303
304
Infix "↔" := uPred_iff : uPred_scope.

305
306
307
308
309
310
311
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").

312
Class TimelessP {M} (P : uPred M) := timelessP :  P  (P   False).
313
Arguments timelessP {_} _ {_}.
314

315
316
Class PersistentP {M} (P : uPred M) := persistentP : P   P.
Arguments persistentP {_} _ {_}.
Robbert Krebbers's avatar
Robbert Krebbers committed
317

318
319
320
321
322
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).
323
Ltac unseal := rewrite !unseal /=.
324
325

Section uPred_logic.
326
Context {M : ucmraT}.
327
Implicit Types φ : Prop.
Robbert Krebbers's avatar
Robbert Krebbers committed
328
Implicit Types P Q : uPred M.
329
Implicit Types A : Type.
330
331
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 *)
332
Arguments uPred_holds {_} !_ _ _ /.
333
Hint Immediate uPred_in_entails.
Robbert Krebbers's avatar
Robbert Krebbers committed
334

335
Global Instance: PreOrder (@uPred_entails M).
336
337
338
339
340
Proof.
  split.
  * by intros P; split=> x i.
  * by intros P Q Q' HP HQ; split=> x i ??; apply HQ, HP.
Qed.
341
Global Instance: AntiSymm () (@uPred_entails M).
342
Proof. intros P Q HPQ HQP; split=> x n; by split; [apply HPQ|apply HQP]. Qed.
343
Lemma equiv_spec P Q : P  Q  P  Q  Q  P.
Robbert Krebbers's avatar
Robbert Krebbers committed
344
Proof.
345
  split; [|by intros [??]; apply (anti_symm ())].
346
  intros HPQ; split; split=> x i; apply HPQ.
Robbert Krebbers's avatar
Robbert Krebbers committed
347
Qed.
348
Lemma equiv_entails P Q : P  Q  P  Q.
349
Proof. apply equiv_spec. Qed.
350
Lemma equiv_entails_sym P Q : Q  P  P  Q.
351
Proof. apply equiv_spec. Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
352
Global Instance entails_proper :
353
  Proper (() ==> () ==> iff) (() : relation (uPred M)).
Robbert Krebbers's avatar
Robbert Krebbers committed
354
Proof.
Robbert Krebbers's avatar
Robbert Krebbers committed
355
  move => P1 P2 /equiv_spec [HP1 HP2] Q1 Q2 /equiv_spec [HQ1 HQ2]; split; intros.
356
357
  - by trans P1; [|trans Q1].
  - by trans P2; [|trans Q2].
Robbert Krebbers's avatar
Robbert Krebbers committed
358
Qed.
359
Lemma entails_equiv_l (P Q R : uPred M) : P  Q  Q  R  P  R.
360
Proof. by intros ->. Qed.
361
Lemma entails_equiv_r (P Q R : uPred M) : P  Q  Q  R  P  R.
362
Proof. by intros ? <-. Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
363

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

(** Introduction and elimination rules *)
471
Lemma const_intro φ P : φ  P   φ.
472
Proof. by intros ?; unseal; split. Qed.
473
Lemma const_elim φ Q R : Q   φ  (φ  Q  R)  Q  R.
474
475
476
Proof.
  unseal; intros HQP HQR; split=> n x ??; apply HQR; first eapply HQP; eauto.
Qed.
477
Lemma and_elim_l P Q : (P  Q)  P.
478
Proof. by unseal; split=> n x ? [??]. Qed.
479
Lemma and_elim_r P Q : (P  Q)  Q.
480
Proof. by unseal; split=> n x ? [??]. Qed.
481
Lemma and_intro P Q R : P  Q  P  R  P  (Q  R).
482
Proof. intros HQ HR; unseal; split=> n x ??; by split; [apply HQ|apply HR]. Qed.
483
Lemma or_intro_l P Q : P  (P  Q).
484
Proof. unseal; split=> n x ??; left; auto. Qed.
485
Lemma or_intro_r P Q : Q  (P  Q).
486
Proof. unseal; split=> n x ??; right; auto. Qed.
487
Lemma or_elim P Q R : P  R  Q  R  (P  Q)  R.
488
Proof. intros HP HQ; unseal; split=> n x ? [?|?]. by apply HP. by apply HQ. Qed.
489
Lemma impl_intro_r P Q R : (P  Q)  R  P  (Q  R).
Robbert Krebbers's avatar
Robbert Krebbers committed
490
Proof.
491
  unseal; intros HQ; split=> n x ?? n' x' ????.
492
  apply HQ; naive_solver eauto using uPred_mono, uPred_closed.
Robbert Krebbers's avatar
Robbert Krebbers committed
493
Qed.
494
Lemma impl_elim P Q R : P  (Q  R)  P  Q  P  R.
495
Proof. by unseal; intros HP HP'; split=> n x ??; apply HP with n x, HP'. Qed.
496
Lemma forall_intro {A} P (Ψ : A  uPred M): ( a, P  Ψ a)  P  ( a, Ψ a).
497
Proof. unseal; intros HPΨ; split=> n x ?? a; by apply HPΨ. Qed.
498
Lemma forall_elim {A} {Ψ : A  uPred M} a : ( a, Ψ a)  Ψ a.
499
Proof. unseal; split=> n x ? HP; apply HP. Qed.
500
Lemma exist_intro {A} {Ψ : A  uPred M} a : Ψ a  ( a, Ψ a).
501
Proof. unseal; split=> n x ??; by exists a. Qed.
502
Lemma exist_elim {A} (Φ : A  uPred M) Q : ( a, Φ a  Q)  ( a, Φ a)  Q.
503
Proof. unseal; intros HΦΨ; split=> n x ? [a ?]; by apply HΦΨ with a. Qed.
504
Lemma eq_refl {A : cofeT} (a : A) : True  (a  a).
505
Proof. unseal; by split=> n x ??; simpl. Qed.
506
Lemma eq_rewrite {A : cofeT} a b (Ψ : A  uPred M) P
507
  {HΨ :  n, Proper (dist n ==> dist n) Ψ} : P  (a  b)  P  Ψ a  P  Ψ b.
508
Proof.
509
510
511
  unseal; intros Hab Ha; split=> n x ??. apply HΨ with n a; auto.
  - by symmetry; apply Hab with x.
  - by apply Ha.
512
Qed.
513
Lemma eq_equiv {A : cofeT} (a b : A) : True  (a  b)  a  b.
514
Proof.
515
  unseal=> Hab; apply equiv_dist; intros n; apply Hab with ; last done.
516
  apply cmra_valid_validN, ucmra_unit_valid.
517
Qed.
518
519
520
521
522
523
524
525
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.
526
527

(* Derived logical stuff *)
528
529
Lemma False_elim P : False  P.
Proof. by apply (const_elim False). Qed.
530
Lemma True_intro P : P  True.
Robbert Krebbers's avatar
Robbert Krebbers committed
531
Proof. by apply const_intro. Qed.
532
Lemma and_elim_l' P Q R : P  R  (P  Q)  R.
533
Proof. by rewrite and_elim_l. Qed.
534
Lemma and_elim_r' P Q R : Q  R  (P  Q)  R.
535
Proof. by rewrite and_elim_r. Qed.
536
Lemma or_intro_l' P Q R : P  Q  P  (Q  R).
537
Proof. intros ->; apply or_intro_l. Qed.
538
Lemma or_intro_r' P Q R : P  R  P  (Q  R).
539
Proof. intros ->; apply or_intro_r. Qed.
540
Lemma exist_intro' {A} P (Ψ : A  uPred M) a : P  Ψ a  P  ( a, Ψ a).
541
Proof. intros ->; apply exist_intro. Qed.
542
Lemma forall_elim' {A} P (Ψ : A  uPred M) : P  ( a, Ψ a)  ( a, P  Ψ a).
543
Proof. move=> HP a. by rewrite HP forall_elim. Qed.
544

545
Hint Resolve or_elim or_intro_l' or_intro_r'.
Robbert Krebbers's avatar
Robbert Krebbers committed
546
547
Hint Resolve and_intro and_elim_l' and_elim_r'.
Hint Immediate True_intro False_elim.
548

549
Lemma impl_intro_l P Q R : (Q  P)  R  P  (Q  R).
550
Proof. intros HR; apply impl_intro_r; rewrite -HR; auto. Qed.
551
Lemma impl_elim_l P Q : ((P  Q)  P)  Q.
552
Proof. apply impl_elim with P; auto. Qed.
553
Lemma impl_elim_r P Q : (P  (P  Q))  Q.
554
Proof. apply impl_elim with P; auto. Qed.
555
Lemma impl_elim_l' P Q R : P  (Q  R)  (P  Q)  R.
556
Proof. intros; apply impl_elim with Q; auto. Qed.
557
Lemma impl_elim_r' P Q R : Q  (P  R)  (P  Q)  R.
558
Proof. intros; apply impl_elim with P; auto. Qed.
559
Lemma impl_entails P Q : True  (P  Q)  P  Q.
560
Proof. intros HPQ; apply impl_elim with P; rewrite -?HPQ; auto. Qed.
561
Lemma entails_impl P Q : (P  Q)  True  (P  Q).
562
Proof. auto using impl_intro_l. Qed.
563

564
565
566
567
568
569
570
571
572
573
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