upred_big_op.v 9.09 KB
Newer Older
1
From algebra Require Export upred.
2
From prelude Require Import gmap fin_collections.
3

4
5
(** * Big ops over lists *)
(* These are the basic building blocks for other big ops *)
6
7
8
9
10
11
12
13
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.
14

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

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

(** * Always stability for lists *)
31
32
33
34
35
36
37
38
39
40
Class AlwaysStableL {M} (Ps : list (uPred M)) :=
  always_stableL : Forall AlwaysStable Ps.
Arguments always_stableL {_} _ {_}.

Section big_op.
Context {M : cmraT}.
Implicit Types Ps Qs : list (uPred M).
Implicit Types A : Type.

(* Big ops *)
41
Global Instance big_and_proper : Proper (() ==> ()) (@uPred_big_and M).
42
Proof. by induction 1 as [|P Q Ps Qs HPQ ? IH]; rewrite /= ?HPQ ?IH. Qed.
43
Global Instance big_sep_proper : Proper (() ==> ()) (@uPred_big_sep M).
44
Proof. by induction 1 as [|P Q Ps Qs HPQ ? IH]; rewrite /= ?HPQ ?IH. Qed.
45
46
47
48
49
50
51
52
53
54
55
56
57

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

Global Instance big_and_mono' : Proper (Forall2 () ==> ()) (@uPred_big_and M).
Proof. by induction 1 as [|P Q Ps Qs HPQ ? IH]; rewrite /= ?HPQ ?IH. Qed.
Global Instance big_sep_mono' : Proper (Forall2 () ==> ()) (@uPred_big_sep M).
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
63
  - by rewrite IH.
  - by rewrite !assoc (comm _ P).
  - etransitivity; 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
70
  - by rewrite IH.
  - by rewrite !assoc (comm _ P).
  - etransitivity; eauto.
71
Qed.
72

73
Lemma big_and_app Ps Qs : (Π (Ps ++ Qs))%I  (Π Ps  Π Qs)%I.
74
Proof. by induction Ps as [|?? IH]; rewrite /= ?left_id -?assoc ?IH. Qed.
75
Lemma big_sep_app Ps Qs : (Π★ (Ps ++ Qs))%I  (Π★ Ps  Π★ Qs)%I.
76
Proof. by induction Ps as [|?? IH]; rewrite /= ?left_id -?assoc ?IH. Qed.
77
78
79
80
81
82
83
84
85
86

Lemma big_and_contains Ps Qs : Qs `contains` Ps  (Π Ps)%I  (Π Qs)%I.
Proof.
  intros [Ps' ->]%contains_Permutation. by rewrite big_and_app uPred.and_elim_l.
Qed.
Lemma big_sep_contains Ps Qs : Qs `contains` Ps  (Π★ Ps)%I  (Π★ Qs)%I.
Proof.
  intros [Ps' ->]%contains_Permutation. by rewrite big_sep_app uPred.sep_elim_l.
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
99
Section gmap.
  Context `{Countable K} {A : Type}.
  Implicit Types m : gmap K A.
  Implicit Types P : K  A  uPred M.
100

101
102
103
  Lemma big_sepM_mono P Q m1 m2 :
    m2  m1  ( x k, m2 !! k = Some x  P k x  Q k x) 
    (Π★{map m1} P)  (Π★{map m2} Q).
104
  Proof.
105
106
107
108
    intros HX HP. transitivity (Π★{map m2} P)%I.
    - by apply big_sep_contains, fmap_contains, map_to_list_contains.
    - apply big_sep_mono', Forall2_fmap, Forall2_Forall.
      apply Forall_forall=> -[i x] ? /=. by apply HP, elem_of_map_to_list.
109
  Qed.
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135

  Global Instance big_sepM_ne m n :
    Proper (pointwise_relation _ (pointwise_relation _ (dist n)) ==> (dist n))
           (uPred_big_sepM (M:=M) m).
  Proof.
    intros P1 P2 HP. apply big_sep_ne, Forall2_fmap.
    apply Forall2_Forall, Forall_true=> -[i x]; apply HP.
  Qed.
  Global Instance big_sepM_proper m :
    Proper (pointwise_relation _ (pointwise_relation _ ()) ==> ())
           (uPred_big_sepM (M:=M) m).
  Proof.
    intros P1 P2 HP; apply equiv_dist=> n.
    apply big_sepM_ne=> k x; apply equiv_dist, HP.
  Qed.
  Global Instance big_sepM_mono' m :
    Proper (pointwise_relation _ (pointwise_relation _ ()) ==> ())
           (uPred_big_sepM (M:=M) m).
  Proof. intros P1 P2 HP. apply big_sepM_mono; intros; [done|apply HP]. Qed.

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

  Lemma big_sepM_sep P Q m :
    (Π★{map m} (λ i x, P i x  Q i x))%I  (Π★{map m} P  Π★{map m} Q)%I.
  Proof.
    rewrite /uPred_big_sepM. induction (map_to_list m); simpl;
      first by rewrite right_id.
    destruct a. rewrite IHl /= -!assoc. apply uPred.sep_proper; first done.
    rewrite !assoc [(_  Q _ _)%I]comm -!assoc. apply uPred.sep_proper; done.
  Qed.
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
End gmap.

(* Big ops over finite sets *)
Section gset.
  Context `{Countable A}.
  Implicit Types X : gset A.
  Implicit Types P : A  uPred M.

  Lemma big_sepS_mono P Q X Y :
    Y  X  ( x, x  Y  P x  Q x)  (Π★{set X} P)  (Π★{set Y} Q).
  Proof.
    intros HX HP. transitivity (Π★{set Y} P)%I.
    - by apply big_sep_contains, fmap_contains, elements_contains.
    - apply big_sep_mono', Forall2_fmap, Forall2_Forall.
      apply Forall_forall=> x ? /=. by apply HP, elem_of_elements.
  Qed.

  Lemma big_sepS_ne X n :
    Proper (pointwise_relation _ (dist n) ==> dist n) (uPred_big_sepS (M:=M) X).
  Proof.
    intros P1 P2 HP. apply big_sep_ne, Forall2_fmap.
    apply Forall2_Forall, Forall_true=> x; apply HP.
  Qed.
  Lemma big_sepS_proper X :
    Proper (pointwise_relation _ () ==> ()) (uPred_big_sepS (M:=M) X).
  Proof.
    intros P1 P2 HP; apply equiv_dist=> n.
    apply big_sepS_ne=> x; apply equiv_dist, HP.
  Qed.
  Lemma big_sepS_mono' X :
    Proper (pointwise_relation _ () ==> ()) (uPred_big_sepS (M:=M) X).
  Proof. intros P1 P2 HP. apply big_sepS_mono; naive_solver. Qed.

  Lemma big_sepS_empty P : (Π★{set } P)%I  True%I.
  Proof. by rewrite /uPred_big_sepS elements_empty. Qed.
  Lemma big_sepS_insert P X x :
    x  X  (Π★{set {[ x ]}  X} P)%I  (P x  Π★{set X} P)%I.
  Proof. intros. by rewrite /uPred_big_sepS elements_union_singleton. Qed.
  Lemma big_sepS_delete P X x :
    x  X  (Π★{set X} P)%I  (P x  Π★{set X  {[ x ]}} P)%I.
  Proof.
190
191
    intros. rewrite -big_sepS_insert; last set_solver.
    by rewrite -union_difference_L; last set_solver.
192
193
194
  Qed.
  Lemma big_sepS_singleton P x : (Π★{set {[ x ]}} P)%I  (P x)%I.
  Proof. intros. by rewrite /uPred_big_sepS elements_singleton /= right_id. Qed.
195
196
197
198
199
200
201
202
203

  Lemma big_sepS_sep P Q X :
    (Π★{set X} (λ x, P x  Q x))%I  (Π★{set X} P  Π★{set X} Q)%I.
  Proof.
    rewrite /uPred_big_sepS. induction (elements X); simpl;
      first by rewrite right_id.
    rewrite IHl -!assoc. apply uPred.sep_proper; first done.
    rewrite !assoc [(_  Q a)%I]comm -!assoc. apply uPred.sep_proper; done.
  Qed.
204
End gset.
205

206
207
208
(* Always stable *)
Local Notation AS := AlwaysStable.
Local Notation ASL := AlwaysStableL.
209
Global Instance big_and_always_stable Ps : ASL Ps  AS (Π Ps).
210
Proof. induction 1; apply _. Qed.
211
Global Instance big_sep_always_stable Ps : ASL Ps  AS (Π★ Ps).
212
213
214
215
216
217
218
219
220
221
222
Proof. induction 1; apply _. Qed.

Global Instance nil_always_stable : ASL (@nil (uPred M)).
Proof. constructor. Qed.
Global Instance cons_always_stable P Ps : AS P  ASL Ps  ASL (P :: Ps).
Proof. by constructor. Qed.
Global Instance app_always_stable Ps Ps' : ASL Ps  ASL Ps'  ASL (Ps ++ Ps').
Proof. apply Forall_app_2. Qed.
Global Instance zip_with_always_stable {A B} (f : A  B  uPred M) xs ys :
  ( x y, AS (f x y))  ASL (zip_with f xs ys).
Proof. unfold ASL=> ?; revert ys; induction xs=> -[|??]; constructor; auto. Qed.
223
End big_op.