upred_big_op.v 10.6 KB
Newer Older
1
From iris.algebra Require Export upred list.
2
From iris.prelude Require Import gmap fin_collections functions.
3
Import uPred.
4

5 6
(** * Big ops over lists *)
(* These are the basic building blocks for other big ops *)
7 8 9 10 11 12 13 14
Fixpoint uPred_big_and {M} (Ps : list (uPred M)) : uPred M:=
  match Ps with [] => True | P :: Ps => P  uPred_big_and Ps end%I.
Instance: Params (@uPred_big_and) 1.
Notation "'Π∧' Ps" := (uPred_big_and Ps) (at level 20) : uPred_scope.
Fixpoint uPred_big_sep {M} (Ps : list (uPred M)) : uPred M :=
  match Ps with [] => True | P :: Ps => P  uPred_big_sep Ps end%I.
Instance: Params (@uPred_big_sep) 1.
Notation "'Π★' Ps" := (uPred_big_sep Ps) (at level 20) : uPred_scope.
15

16 17
(** * Other big ops *)
(** We use a type class to obtain overloaded notations *)
18
Definition uPred_big_sepM {M} `{Countable K} {A}
19 20
    (m : gmap K A) (Φ : K  A  uPred M) : uPred M :=
  uPred_big_sep (curry Φ <$> map_to_list m).
21
Instance: Params (@uPred_big_sepM) 6.
22 23
Notation "'Π★{map' m } Φ" := (uPred_big_sepM m Φ)
  (at level 20, m at level 10, format "Π★{map  m }  Φ") : uPred_scope.
24

25
Definition uPred_big_sepS {M} `{Countable A}
26
  (X : gset A) (Φ : A  uPred M) : uPred M := uPred_big_sep (Φ <$> elements X).
27
Instance: Params (@uPred_big_sepS) 5.
28 29
Notation "'Π★{set' X } Φ" := (uPred_big_sepS X Φ)
  (at level 20, X at level 10, format "Π★{set  X }  Φ") : uPred_scope.
30

31
(** * Persistence of lists of uPreds *)
32
Class PersistentL {M} (Ps : list (uPred M)) :=
33
  persistentL : Forall PersistentP Ps.
34
Arguments persistentL {_} _ {_}.
35

36
(** * Properties *)
37 38 39 40 41
Section big_op.
Context {M : cmraT}.
Implicit Types Ps Qs : list (uPred M).
Implicit Types A : Type.

42
(** ** Big ops over lists *)
43
Global Instance big_and_proper : Proper (() ==> ()) (@uPred_big_and M).
44
Proof. by induction 1 as [|P Q Ps Qs HPQ ? IH]; rewrite /= ?HPQ ?IH. Qed.
45
Global Instance big_sep_proper : Proper (() ==> ()) (@uPred_big_sep M).
46
Proof. by induction 1 as [|P Q Ps Qs HPQ ? IH]; rewrite /= ?HPQ ?IH. Qed.
47

48
Global Instance big_and_ne n : Proper (dist n ==> dist n) (@uPred_big_and M).
49
Proof. by induction 1 as [|P Q Ps Qs HPQ ? IH]; rewrite /= ?HPQ ?IH. Qed.
50
Global Instance big_sep_ne n : Proper (dist n ==> dist n) (@uPred_big_sep M).
51 52
Proof. by induction 1 as [|P Q Ps Qs HPQ ? IH]; rewrite /= ?HPQ ?IH. Qed.

53
Global Instance big_and_mono' : Proper (Forall2 () ==> ()) (@uPred_big_and M).
54
Proof. by induction 1 as [|P Q Ps Qs HPQ ? IH]; rewrite /= ?HPQ ?IH. Qed.
55
Global Instance big_sep_mono' : Proper (Forall2 () ==> ()) (@uPred_big_sep M).
56 57
Proof. by induction 1 as [|P Q Ps Qs HPQ ? IH]; rewrite /= ?HPQ ?IH. Qed.

58
Global Instance big_and_perm : Proper (() ==> ()) (@uPred_big_and M).
59 60
Proof.
  induction 1 as [|P Ps Qs ? IH|P Q Ps|]; simpl; auto.
61 62
  - by rewrite IH.
  - by rewrite !assoc (comm _ P).
63
  - etrans; eauto.
64
Qed.
65
Global Instance big_sep_perm : Proper (() ==> ()) (@uPred_big_sep M).
66 67
Proof.
  induction 1 as [|P Ps Qs ? IH|P Q Ps|]; simpl; auto.
68 69
  - by rewrite IH.
  - by rewrite !assoc (comm _ P).
70
  - etrans; eauto.
71
Qed.
72

73
Lemma big_and_app Ps Qs : Π (Ps ++ Qs)  (Π Ps  Π Qs).
74
Proof. by induction Ps as [|?? IH]; rewrite /= ?left_id -?assoc ?IH. Qed.
75
Lemma big_sep_app Ps Qs : Π★ (Ps ++ Qs)  (Π★ Ps  Π★ Qs).
76
Proof. by induction Ps as [|?? IH]; rewrite /= ?left_id -?assoc ?IH. Qed.
77

78
Lemma big_and_contains Ps Qs : Qs `contains` Ps  Π Ps  Π Qs.
79
Proof.
80
  intros [Ps' ->]%contains_Permutation. by rewrite big_and_app and_elim_l.
81
Qed.
82
Lemma big_sep_contains Ps Qs : Qs `contains` Ps  Π★ Ps  Π★ Qs.
83
Proof.
84
  intros [Ps' ->]%contains_Permutation. by rewrite big_sep_app sep_elim_l.
85 86
Qed.

87
Lemma big_sep_and Ps : Π★ Ps  Π Ps.
88
Proof. by induction Ps as [|P Ps IH]; simpl; auto with I. Qed.
89

90
Lemma big_and_elem_of Ps P : P  Ps  Π Ps  P.
91
Proof. induction 1; simpl; auto with I. Qed.
92
Lemma big_sep_elem_of Ps P : P  Ps  Π★ Ps  P.
93 94
Proof. induction 1; simpl; auto with I. Qed.

95
(** ** Big ops over finite maps *)
96 97 98
Section gmap.
  Context `{Countable K} {A : Type}.
  Implicit Types m : gmap K A.
99
  Implicit Types Φ Ψ : K  A  uPred M.
100

101
  Lemma big_sepM_mono Φ Ψ m1 m2 :
102
    m2  m1  ( x k, m2 !! k = Some x  Φ k x  Ψ k x) 
103
    Π★{map m1} Φ  Π★{map m2} Ψ.
104
  Proof.
105
    intros HX HΦ. trans (Π★{map m2} Φ)%I.
106
    - by apply big_sep_contains, fmap_contains, map_to_list_contains.
107
    - apply big_sep_mono', Forall2_fmap, Forall_Forall2.
108
      apply Forall_forall=> -[i x] ? /=. by apply HΦ, elem_of_map_to_list.
109
  Qed.
110 111 112 113 114 115 116
  Lemma big_sepM_proper Φ Ψ m1 m2 :
    m1  m2  ( x k, m1 !! k = Some x  m2 !! k = Some x  Φ k x  Ψ k x) 
    Π★{map m1} Φ  Π★{map m2} Ψ.
  Proof.
    intros [??] ?; apply (anti_symm ()); apply big_sepM_mono;
      eauto using equiv_entails, equiv_entails_sym, lookup_weaken.
  Qed.
117 118 119 120 121

  Global Instance big_sepM_ne m n :
    Proper (pointwise_relation _ (pointwise_relation _ (dist n)) ==> (dist n))
           (uPred_big_sepM (M:=M) m).
  Proof.
122
    intros Φ1 Φ2 HΦ. apply big_sep_ne, Forall2_fmap.
123
    apply Forall_Forall2, Forall_true=> -[i x]; apply HΦ.
124
  Qed.
125
  Global Instance big_sepM_proper' m :
126
    Proper (pointwise_relation _ (pointwise_relation _ ()) ==> ())
127
           (uPred_big_sepM (M:=M) m).
128
  Proof. intros Φ1 Φ2 HΦ. by apply big_sepM_proper; intros; last apply HΦ. Qed.
129
  Global Instance big_sepM_mono' m :
130
    Proper (pointwise_relation _ (pointwise_relation _ ()) ==> ())
131
           (uPred_big_sepM (M:=M) m).
132
  Proof. intros Φ1 Φ2 HΦ. by apply big_sepM_mono; intros; last apply HΦ. Qed.
133

134
  Lemma big_sepM_empty Φ : Π★{map } Φ  True.
135
  Proof. by rewrite /uPred_big_sepM map_to_list_empty. Qed.
136
  Lemma big_sepM_insert Φ (m : gmap K A) i x :
137
    m !! i = None  Π★{map <[i:=x]> m} Φ  (Φ i x  Π★{map m} Φ).
138
  Proof. intros ?; by rewrite /uPred_big_sepM map_to_list_insert. Qed.
139
  Lemma big_sepM_singleton Φ i x : Π★{map {[i := x]}} Φ  (Φ i x).
140 141 142 143
  Proof.
    rewrite -insert_empty big_sepM_insert/=; last auto using lookup_empty.
    by rewrite big_sepM_empty right_id.
  Qed.
144

145
  Lemma big_sepM_sepM Φ Ψ m :
146
    Π★{map m} (λ i x, Φ i x  Ψ i x)  (Π★{map m} Φ  Π★{map m} Ψ).
147
  Proof.
Robbert Krebbers's avatar
Robbert Krebbers committed
148 149
    rewrite /uPred_big_sepM.
    induction (map_to_list m) as [|[i x] l IH]; csimpl; rewrite ?right_id //.
150
    by rewrite IH -!assoc (assoc _ (Ψ _ _)) [(Ψ _ _  _)%I]comm -!assoc.
151
  Qed.
152
  Lemma big_sepM_later Φ m :  Π★{map m} Φ  Π★{map m} (λ i x,  Φ i x).
153
  Proof.
Robbert Krebbers's avatar
Robbert Krebbers committed
154 155 156
    rewrite /uPred_big_sepM.
    induction (map_to_list m) as [|[i x] l IH]; csimpl; rewrite ?later_True //.
    by rewrite later_sep IH.
157
  Qed.
158 159
End gmap.

160
(** ** Big ops over finite sets *)
161 162 163
Section gset.
  Context `{Countable A}.
  Implicit Types X : gset A.
164
  Implicit Types Φ : A  uPred M.
165

166
  Lemma big_sepS_mono Φ Ψ X Y :
167
    Y  X  ( x, x  Y  Φ x  Ψ x)  Π★{set X} Φ  Π★{set Y} Ψ.
168
  Proof.
169
    intros HX HΦ. trans (Π★{set Y} Φ)%I.
170
    - by apply big_sep_contains, fmap_contains, elements_contains.
171
    - apply big_sep_mono', Forall2_fmap, Forall_Forall2.
172
      apply Forall_forall=> x ? /=. by apply HΦ, elem_of_elements.
173
  Qed.
174 175 176 177 178 179
  Lemma big_sepS_proper Φ Ψ X Y :
    X  Y  ( x, x  X  x  Y  Φ x  Ψ x)  Π★{set X} Φ  Π★{set Y} Ψ.
  Proof.
    intros [??] ?; apply (anti_symm ()); apply big_sepS_mono;
      eauto using equiv_entails, equiv_entails_sym.
  Qed.
180 181 182 183

  Lemma big_sepS_ne X n :
    Proper (pointwise_relation _ (dist n) ==> dist n) (uPred_big_sepS (M:=M) X).
  Proof.
184
    intros Φ1 Φ2 HΦ. apply big_sep_ne, Forall2_fmap.
185
    apply Forall_Forall2, Forall_true=> x; apply HΦ.
186
  Qed.
187
  Lemma big_sepS_proper' X :
188
    Proper (pointwise_relation _ () ==> ()) (uPred_big_sepS (M:=M) X).
189
  Proof. intros Φ1 Φ2 HΦ. apply big_sepS_proper; naive_solver. Qed.
190
  Lemma big_sepS_mono' X :
191
    Proper (pointwise_relation _ () ==> ()) (uPred_big_sepS (M:=M) X).
192
  Proof. intros Φ1 Φ2 HΦ. apply big_sepS_mono; naive_solver. Qed.
193

194
  Lemma big_sepS_empty Φ : Π★{set } Φ  True.
195
  Proof. by rewrite /uPred_big_sepS elements_empty. Qed.
196

197
  Lemma big_sepS_insert Φ X x :
198
    x  X  Π★{set {[ x ]}  X} Φ  (Φ x  Π★{set X} Φ).
199
  Proof. intros. by rewrite /uPred_big_sepS elements_union_singleton. Qed.
200 201 202 203 204 205 206 207 208 209 210 211 212
  Lemma big_sepS_insert' (Ψ : A  uPred M  uPred M) Φ X x P :
    x  X 
    Π★{set {[ x ]}  X} (λ y, Ψ y (<[x:=P]> Φ y))
     (Ψ x P  Π★{set X} (λ y, Ψ y (Φ y))).
  Proof.
    intros. rewrite big_sepS_insert // fn_lookup_insert.
    apply sep_proper, big_sepS_proper; auto=> y ??.
    by rewrite fn_lookup_insert_ne; last set_solver.
  Qed.
  Lemma big_sepS_insert'' Φ X x P :
    x  X  Π★{set {[ x ]}  X} (<[x:=P]> Φ)  (P  Π★{set X} Φ).
  Proof. apply (big_sepS_insert' (λ y, id)). Qed.

213
  Lemma big_sepS_delete Φ X x :
214
    x  X  Π★{set X} Φ  (Φ x  Π★{set X  {[ x ]}} Φ).
215
  Proof.
216 217
    intros. rewrite -big_sepS_insert; last set_solver.
    by rewrite -union_difference_L; last set_solver.
218
  Qed.
219

220
  Lemma big_sepS_singleton Φ x : Π★{set {[ x ]}} Φ  (Φ x).
221
  Proof. intros. by rewrite /uPred_big_sepS elements_singleton /= right_id. Qed.
222

223
  Lemma big_sepS_sepS Φ Ψ X :
224
    Π★{set X} (λ x, Φ x  Ψ x)  (Π★{set X} Φ  Π★{set X} Ψ).
225
  Proof.
Robbert Krebbers's avatar
Robbert Krebbers committed
226 227
    rewrite /uPred_big_sepS.
    induction (elements X) as [|x l IH]; csimpl; first by rewrite ?right_id.
228
    by rewrite IH -!assoc (assoc _ (Ψ _)) [(Ψ _  _)%I]comm -!assoc.
229 230
  Qed.

231
  Lemma big_sepS_later Φ X :  Π★{set X} Φ  Π★{set X} (λ x,  Φ x).
232
  Proof.
Robbert Krebbers's avatar
Robbert Krebbers committed
233 234 235
    rewrite /uPred_big_sepS.
    induction (elements X) as [|x l IH]; csimpl; first by rewrite ?later_True.
    by rewrite later_sep IH.
236
  Qed.
237
End gset.
238

239
(** ** Persistence *)
240
Global Instance big_and_persistent Ps : PersistentL Ps  PersistentP (Π Ps).
241
Proof. induction 1; apply _. Qed.
242
Global Instance big_sep_persistent Ps : PersistentL Ps  PersistentP (Π★ Ps).
243 244
Proof. induction 1; apply _. Qed.

245
Global Instance nil_persistent : PersistentL (@nil (uPred M)).
246
Proof. constructor. Qed.
247
Global Instance cons_persistent P Ps :
248
  PersistentP P  PersistentL Ps  PersistentL (P :: Ps).
249
Proof. by constructor. Qed.
250 251
Global Instance app_persistent Ps Ps' :
  PersistentL Ps  PersistentL Ps'  PersistentL (Ps ++ Ps').
252
Proof. apply Forall_app_2. Qed.
253
Global Instance zip_with_persistent {A B} (f : A  B  uPred M) xs ys :
254
  ( x y, PersistentP (f x y))  PersistentL (zip_with f xs ys).
255 256 257
Proof.
  unfold PersistentL=> ?; revert ys; induction xs=> -[|??]; constructor; auto.
Qed.
258
End big_op.