big_op.v 50.3 KB
Newer Older
Robbert Krebbers's avatar
Robbert Krebbers committed
1
From iris.algebra Require Export big_op.
2
From iris.bi Require Import derived_laws_sbi plainly.
Robbert Krebbers's avatar
Robbert Krebbers committed
3
From stdpp Require Import countable fin_collections functions.
4
Set Default Proof Using "Type".
Robbert Krebbers's avatar
Robbert Krebbers committed
5
Import interface.bi derived_laws_bi.bi derived_laws_sbi.bi.
6

7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
(** A version of the separating big operator that ranges over two lists. This
version also ensures that both lists have the same length. Although this version
can be defined in terms of the unary using a [zip] (see [big_sepL2_alt]), we do
not define it that way to get better computational behavior (for [simpl]). *)
Fixpoint big_sepL2 {PROP : bi} {A B}
    (Φ : nat  A  B  PROP) (l1 : list A) (l2 : list B) : PROP :=
  match l1, l2 with
  | [], [] => emp
  | x1 :: l1, x2 :: l2 => Φ 0 x1 x2  big_sepL2 (λ n, Φ (S n)) l1 l2
  | _, _ => False
  end%I.
Instance: Params (@big_sepL2) 3.
Arguments big_sepL2 {PROP A B} _ !_ !_ /.
Typeclasses Opaque big_sepL2.

22
(* Notations *)
Ralf Jung's avatar
Ralf Jung committed
23
24
25
26
27
28
29
Notation "'[∗' 'list]' k ↦ x ∈ l , P" :=
  (big_opL bi_sep (λ k x, P) l) : bi_scope.
Notation "'[∗' 'list]' x ∈ l , P" :=
  (big_opL bi_sep (λ _ x, P) l) : bi_scope.

Notation "'[∗]' Ps" := (big_opL bi_sep (λ _ x, x) Ps) : bi_scope.

30
31
32
33
34
Notation "'[∗' 'list]' k ↦ x1 ; x2 ∈ l1 ; l2 , P" :=
  (big_sepL2 (λ k x1 x2, P) l1 l2) : bi_scope.
Notation "'[∗' 'list]' x1 ; x2 ∈ l1 ; l2 , P" :=
  (big_sepL2 (λ _ x1 x2, P) l1 l2) : bi_scope.

Ralf Jung's avatar
Ralf Jung committed
35
36
37
38
39
40
41
42
43
44
45
46
47
Notation "'[∧' 'list]' k ↦ x ∈ l , P" :=
  (big_opL bi_and (λ k x, P) l) : bi_scope.
Notation "'[∧' 'list]' x ∈ l , P" :=
  (big_opL bi_and (λ _ x, P) l) : bi_scope.

Notation "'[∧]' Ps" := (big_opL bi_and (λ _ x, x) Ps) : bi_scope.

Notation "'[∗' 'map]' k ↦ x ∈ m , P" := (big_opM bi_sep (λ k x, P) m) : bi_scope.
Notation "'[∗' 'map]' x ∈ m , P" := (big_opM bi_sep (λ _ x, P) m) : bi_scope.

Notation "'[∗' 'set]' x ∈ X , P" := (big_opS bi_sep (λ x, P) X) : bi_scope.

Notation "'[∗' 'mset]' x ∈ X , P" := (big_opMS bi_sep (λ x, P) X) : bi_scope.
48

49
(** * Properties *)
Robbert Krebbers's avatar
Robbert Krebbers committed
50
51
Section bi_big_op.
Context {PROP : bi}.
52
Implicit Types P Q : PROP.
Robbert Krebbers's avatar
Robbert Krebbers committed
53
Implicit Types Ps Qs : list PROP.
54
55
Implicit Types A : Type.

56
(** ** Big ops over lists *)
57
Section sep_list.
58
59
  Context {A : Type}.
  Implicit Types l : list A.
Robbert Krebbers's avatar
Robbert Krebbers committed
60
  Implicit Types Φ Ψ : nat  A  PROP.
61

Robbert Krebbers's avatar
Robbert Krebbers committed
62
  Lemma big_sepL_nil Φ : ([ list] ky  nil, Φ k y)  emp.
63
  Proof. done. Qed.
64
  Lemma big_sepL_nil' `{BiAffine PROP} P Φ : P  [ list] ky  nil, Φ k y.
Robbert Krebbers's avatar
Robbert Krebbers committed
65
  Proof. apply (affine _). Qed.
66
  Lemma big_sepL_cons Φ x l :
67
    ([ list] ky  x :: l, Φ k y)  Φ 0 x  [ list] ky  l, Φ (S k) y.
68
  Proof. by rewrite big_opL_cons. Qed.
69
  Lemma big_sepL_singleton Φ x : ([ list] ky  [x], Φ k y)  Φ 0 x.
70
71
  Proof. by rewrite big_opL_singleton. Qed.
  Lemma big_sepL_app Φ l1 l2 :
72
73
    ([ list] ky  l1 ++ l2, Φ k y)
     ([ list] ky  l1, Φ k y)  ([ list] ky  l2, Φ (length l1 + k) y).
74
75
  Proof. by rewrite big_opL_app. Qed.

76
77
  Lemma big_sepL_mono Φ Ψ l :
    ( k y, l !! k = Some y  Φ k y  Ψ k y) 
78
    ([ list] k  y  l, Φ k y)  [ list] k  y  l, Ψ k y.
79
  Proof. apply big_opL_forall; apply _. Qed.
80
81
  Lemma big_sepL_proper Φ Ψ l :
    ( k y, l !! k = Some y  Φ k y  Ψ k y) 
82
    ([ list] k  y  l, Φ k y)  ([ list] k  y  l, Ψ k y).
83
  Proof. apply big_opL_proper. Qed.
84
  Lemma big_sepL_submseteq `{BiAffine PROP} (Φ : A  PROP) l1 l2 :
Robbert Krebbers's avatar
Robbert Krebbers committed
85
    l1 + l2  ([ list] y  l2, Φ y)  [ list] y  l1, Φ y.
Robbert Krebbers's avatar
Robbert Krebbers committed
86
87
88
  Proof.
    intros [l ->]%submseteq_Permutation. by rewrite big_sepL_app sep_elim_l.
  Qed.
89

90
91
  Global Instance big_sepL_mono' :
    Proper (pointwise_relation _ (pointwise_relation _ ()) ==> (=) ==> ())
Robbert Krebbers's avatar
Robbert Krebbers committed
92
           (big_opL (@bi_sep PROP) (A:=A)).
93
  Proof. intros f g Hf m ? <-. apply big_opL_forall; apply _ || intros; apply Hf. Qed.
94
  Global Instance big_sepL_id_mono' :
95
    Proper (Forall2 () ==> ()) (big_opL (@bi_sep PROP) (λ _ P, P)).
96
  Proof. by induction 1 as [|P Q Ps Qs HPQ ? IH]; rewrite /= ?HPQ ?IH. Qed.
97

98
  Lemma big_sepL_emp l : ([ list] ky  l, emp) @{PROP} emp.
Robbert Krebbers's avatar
Robbert Krebbers committed
99
100
  Proof. by rewrite big_opL_unit. Qed.

101
102
103
104
  Lemma big_sepL_lookup_acc Φ l i x :
    l !! i = Some x 
    ([ list] ky  l, Φ k y)  Φ i x  (Φ i x - ([ list] ky  l, Φ k y)).
  Proof.
105
106
107
    intros Hli. rewrite -(take_drop_middle l i x) // big_sepL_app /=.
    rewrite Nat.add_0_r take_length_le; eauto using lookup_lt_Some, Nat.lt_le_incl.
    rewrite assoc -!(comm _ (Φ _ _)) -assoc. by apply sep_mono_r, wand_intro_l.
108
109
  Qed.

Robbert Krebbers's avatar
Robbert Krebbers committed
110
  Lemma big_sepL_lookup Φ l i x `{!Absorbing (Φ i x)} :
111
    l !! i = Some x  ([ list] ky  l, Φ k y)  Φ i x.
Robbert Krebbers's avatar
Robbert Krebbers committed
112
  Proof. intros. rewrite big_sepL_lookup_acc //. by rewrite sep_elim_l. Qed.
113

Robbert Krebbers's avatar
Robbert Krebbers committed
114
  Lemma big_sepL_elem_of (Φ : A  PROP) l x `{!Absorbing (Φ x)} :
115
    x  l  ([ list] y  l, Φ y)  Φ x.
116
117
118
  Proof.
    intros [i ?]%elem_of_list_lookup; eauto using (big_sepL_lookup (λ _, Φ)).
  Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
119

Robbert Krebbers's avatar
Robbert Krebbers committed
120
  Lemma big_sepL_fmap {B} (f : A  B) (Φ : nat  B  PROP) l :
121
    ([ list] ky  f <$> l, Φ k y)  ([ list] ky  l, Φ k (f y)).
122
  Proof. by rewrite big_opL_fmap. Qed.
123
124

  Lemma big_sepL_sepL Φ Ψ l :
125
126
    ([ list] kx  l, Φ k x  Ψ k x)
     ([ list] kx  l, Φ k x)  ([ list] kx  l, Ψ k x).
127
  Proof. by rewrite big_opL_opL. Qed.
128

129
130
131
  Lemma big_sepL_and Φ Ψ l :
    ([ list] kx  l, Φ k x  Ψ k x)
     ([ list] kx  l, Φ k x)  ([ list] kx  l, Ψ k x).
Robbert Krebbers's avatar
Robbert Krebbers committed
132
  Proof. auto using and_intro, big_sepL_mono, and_elim_l, and_elim_r. Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
133

134
  Lemma big_sepL_persistently `{BiAffine PROP} Φ l :
135
    <pers> ([ list] kx  l, Φ k x)  [ list] kx  l, <pers> (Φ k x).
136
  Proof. apply (big_opL_commute _). Qed.
137

138
  Lemma big_sepL_forall `{BiAffine PROP} Φ l :
139
    ( k x, Persistent (Φ k x)) 
Ralf Jung's avatar
Ralf Jung committed
140
    ([ list] kx  l, Φ k x)  ( k x, l !! k = Some x  Φ k x).
141
142
143
  Proof.
    intros HΦ. apply (anti_symm _).
    { apply forall_intro=> k; apply forall_intro=> x.
Robbert Krebbers's avatar
Robbert Krebbers committed
144
145
      apply impl_intro_l, pure_elim_l=> ?; by apply: big_sepL_lookup. }
    revert Φ HΦ. induction l as [|x l IH]=> Φ HΦ; [by auto using big_sepL_nil'|].
146
    rewrite big_sepL_cons. rewrite -persistent_and_sep; apply and_intro.
147
    - by rewrite (forall_elim 0) (forall_elim x) pure_True // True_impl.
148
149
150
151
    - rewrite -IH. apply forall_intro=> k; by rewrite (forall_elim (S k)).
  Qed.

  Lemma big_sepL_impl Φ Ψ l :
Robbert Krebbers's avatar
Robbert Krebbers committed
152
    ([ list] kx  l, Φ k x) -
153
     ( k x, l !! k = Some x  Φ k x - Ψ k x) -
Robbert Krebbers's avatar
Robbert Krebbers committed
154
    [ list] kx  l, Ψ k x.
155
  Proof.
Robbert Krebbers's avatar
Robbert Krebbers committed
156
157
    apply wand_intro_l. revert Φ Ψ. induction l as [|x l IH]=> Φ Ψ /=.
    { by rewrite sep_elim_r. }
158
    rewrite intuitionistically_sep_dup -assoc [( _  _)%I]comm -!assoc assoc.
Robbert Krebbers's avatar
Robbert Krebbers committed
159
160
    apply sep_mono.
    - rewrite (forall_elim 0) (forall_elim x) pure_True // True_impl.
161
      by rewrite intuitionistically_elim wand_elim_l.
Robbert Krebbers's avatar
Robbert Krebbers committed
162
    - rewrite comm -(IH (Φ  S) (Ψ  S)) /=.
163
      apply sep_mono_l, affinely_mono, persistently_mono.
Robbert Krebbers's avatar
Robbert Krebbers committed
164
      apply forall_intro=> k. by rewrite (forall_elim (S k)).
165
166
  Qed.

167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
  Lemma big_sepL_delete Φ l i x :
    l !! i = Some x 
    ([ list] ky  l, Φ k y)
     Φ i x  [ list] ky  l, if decide (k = i) then emp else Φ k y.
  Proof.
    intros. rewrite -(take_drop_middle l i x) // !big_sepL_app /= Nat.add_0_r.
    rewrite take_length_le; last eauto using lookup_lt_Some, Nat.lt_le_incl.
    rewrite decide_True // left_id.
    rewrite assoc -!(comm _ (Φ _ _)) -assoc. do 2 f_equiv.
    - apply big_sepL_proper=> k y Hk. apply lookup_lt_Some in Hk.
      rewrite take_length in Hk. by rewrite decide_False; last lia.
    - apply big_sepL_proper=> k y _. by rewrite decide_False; last lia.
  Qed.

  Lemma big_sepL_delete' `{!BiAffine PROP} Φ l i x :
    l !! i = Some x 
    ([ list] ky  l, Φ k y)  Φ i x  [ list] ky  l,  k  i   Φ k y.
  Proof.
    intros. rewrite big_sepL_delete //. (do 2 f_equiv)=> k y.
    rewrite -decide_emp. by repeat case_decide.
  Qed.

189
190
191
192
  Lemma big_sepL_replicate l P :
    [] replicate (length l) P  [ list] y  l, P.
  Proof. induction l as [|x l]=> //=; by f_equiv. Qed.

193
  Global Instance big_sepL_nil_persistent Φ :
194
    Persistent ([ list] kx  [], Φ k x).
195
  Proof. simpl; apply _. Qed.
196
  Global Instance big_sepL_persistent Φ l :
197
    ( k x, Persistent (Φ k x))  Persistent ([ list] kx  l, Φ k x).
198
  Proof. revert Φ. induction l as [|x l IH]=> Φ ? /=; apply _. Qed.
199
  Global Instance big_sepL_persistent_id Ps :
200
    TCForall Persistent Ps  Persistent ([] Ps).
201
  Proof. induction 1; simpl; apply _. Qed.
202

203
204
205
  Global Instance big_sepL_nil_affine Φ :
    Affine ([ list] kx  [], Φ k x).
  Proof. simpl; apply _. Qed.
206
207
208
  Global Instance big_sepL_affine Φ l :
    ( k x, Affine (Φ k x))  Affine ([ list] kx  l, Φ k x).
  Proof. revert Φ. induction l as [|x l IH]=> Φ ? /=; apply _. Qed.
209
210
  Global Instance big_sepL_affine_id Ps : TCForall Affine Ps  Affine ([] Ps).
  Proof. induction 1; simpl; apply _. Qed.
211
End sep_list.
212

213
Section sep_list_more.
214
215
  Context {A : Type}.
  Implicit Types l : list A.
Robbert Krebbers's avatar
Robbert Krebbers committed
216
  Implicit Types Φ Ψ : nat  A  PROP.
217
218
219
  (* Some lemmas depend on the generalized versions of the above ones. *)

  Lemma big_sepL_zip_with {B C} Φ f (l1 : list B) (l2 : list C) :
Robbert Krebbers's avatar
Robbert Krebbers committed
220
    ([ list] kx  zip_with f l1 l2, Φ k x)
Robbert Krebbers's avatar
Robbert Krebbers committed
221
     ([ list] kx  l1, if l2 !! k is Some y then Φ k (f x y) else emp).
222
  Proof.
Robbert Krebbers's avatar
Robbert Krebbers committed
223
224
225
    revert Φ l2; induction l1 as [|x l1 IH]=> Φ [|y l2] //=.
    - by rewrite big_sepL_emp left_id.
    - by rewrite IH.
226
  Qed.
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
End sep_list_more.

Lemma big_sepL2_alt {A B} (Φ : nat  A  B  PROP) l1 l2 :
  ([ list] ky1;y2  l1; l2, Φ k y1 y2)
    length l1 = length l2   [ list] k  y  zip l1 l2, Φ k (y.1) (y.2).
Proof.
  apply (anti_symm _).
  - apply and_intro.
    + revert Φ l2. induction l1 as [|x1 l1 IH]=> Φ -[|x2 l2] /=;
        auto using pure_intro, False_elim.
      rewrite IH sep_elim_r. apply pure_mono; auto.
    + revert Φ l2. induction l1 as [|x1 l1 IH]=> Φ -[|x2 l2] /=;
        auto using pure_intro, False_elim.
      by rewrite IH.
  - apply pure_elim_l=> /Forall2_same_length Hl. revert Φ.
    induction Hl as [|x1 l1 x2 l2 _ _ IH]=> Φ //=. by rewrite -IH.
Qed.

(** ** Big ops over two lists *)
Section sep_list2.
  Context {A B : Type}.
  Implicit Types Φ Ψ : nat  A  B  PROP.

  Lemma big_sepL2_nil Φ : ([ list] ky1;y2  []; [], Φ k y1 y2)  emp.
  Proof. done. Qed.
  Lemma big_sepL2_nil' `{BiAffine PROP} P Φ : P  [ list] ky1;y2  [];[], Φ k y1 y2.
  Proof. apply (affine _). Qed.

  Lemma big_sepL2_cons Φ x1 x2 l1 l2 :
    ([ list] ky1;y2  x1 :: l1; x2 :: l2, Φ k y1 y2)
     Φ 0 x1 x2  [ list] ky1;y2  l1;l2, Φ (S k) y1 y2.
  Proof. done. Qed.
  Lemma big_sepL2_cons_inv_l Φ x1 l1 l2 :
    ([ list] ky1;y2  x1 :: l1; l2, Φ k y1 y2) -
     x2 l2',  l2 = x2 :: l2'  
              Φ 0 x1 x2  [ list] ky1;y2  l1;l2', Φ (S k) y1 y2.
  Proof.
    destruct l2 as [|x2 l2]; simpl; auto using False_elim.
    by rewrite -(exist_intro x2) -(exist_intro l2) pure_True // left_id.
  Qed.
  Lemma big_sepL2_cons_inv_r Φ x2 l1 l2 :
    ([ list] ky1;y2  l1; x2 :: l2, Φ k y1 y2) -
     x1 l1',  l1 = x1 :: l1'  
              Φ 0 x1 x2  [ list] ky1;y2  l1';l2, Φ (S k) y1 y2.
  Proof.
    destruct l1 as [|x1 l1]; simpl; auto using False_elim.
    by rewrite -(exist_intro x1) -(exist_intro l1) pure_True // left_id.
  Qed.

  Lemma big_sepL2_singleton Φ x1 x2 :
    ([ list] ky1;y2  [x1];[x2], Φ k y1 y2)  Φ 0 x1 x2.
  Proof. by rewrite /= right_id. Qed.

  Lemma big_sepL2_length Φ l1 l2 :
    ([ list] ky1;y2  l1; l2, Φ k y1 y2) -  length l1 = length l2 .
  Proof. by rewrite big_sepL2_alt and_elim_l. Qed.

  Lemma big_sepL2_app Φ l1 l2 l1' l2' :
    ([ list] ky1;y2  l1; l1', Φ k y1 y2) -
    ([ list] ky1;y2  l2; l2', Φ (length l1 + k) y1 y2) -
    ([ list] ky1;y2  l1 ++ l2; l1' ++ l2', Φ k y1 y2).
  Proof.
    apply wand_intro_r. revert Φ l1'. induction l1 as [|x1 l1 IH]=> Φ -[|x1' l1'] /=.
    - by rewrite left_id.
    - rewrite left_absorb. apply False_elim.
    - rewrite left_absorb. apply False_elim.
    - by rewrite -assoc IH.
  Qed.
  Lemma big_sepL2_app_inv_l Φ l1' l1'' l2 :
    ([ list] ky1;y2  l1' ++ l1''; l2, Φ k y1 y2) -
     l2' l2'',  l2 = l2' ++ l2''  
                ([ list] ky1;y2  l1';l2', Φ k y1 y2) 
                ([ list] ky1;y2  l1'';l2'', Φ (length l1' + k) y1 y2).
  Proof.
    rewrite -(exist_intro (take (length l1') l2))
      -(exist_intro (drop (length l1') l2)) take_drop pure_True // left_id.
    revert Φ l2. induction l1' as [|x1 l1' IH]=> Φ -[|x2 l2] /=;
       [by rewrite left_id|by rewrite left_id|apply False_elim|].
    by rewrite IH -assoc.
  Qed.
  Lemma big_sepL2_app_inv_r Φ l1 l2' l2'' :
    ([ list] ky1;y2  l1; l2' ++ l2'', Φ k y1 y2) -
     l1' l1'',  l1 = l1' ++ l1''  
                ([ list] ky1;y2  l1';l2', Φ k y1 y2) 
                ([ list] ky1;y2  l1'';l2'', Φ (length l2' + k) y1 y2).
  Proof.
    rewrite -(exist_intro (take (length l2') l1))
      -(exist_intro (drop (length l2') l1)) take_drop pure_True // left_id.
    revert Φ l1. induction l2' as [|x2 l2' IH]=> Φ -[|x1 l1] /=;
       [by rewrite left_id|by rewrite left_id|apply False_elim|].
    by rewrite IH -assoc.
  Qed.

  Lemma big_sepL2_mono Φ Ψ l1 l2 :
    ( k y1 y2, l1 !! k = Some y1  l2 !! k = Some y2  Φ k y1 y2  Ψ k y1 y2) 
    ([ list] k  y1;y2  l1;l2, Φ k y1 y2)  [ list] k  y1;y2  l1;l2, Ψ k y1 y2.
  Proof.
    intros H. rewrite !big_sepL2_alt. f_equiv. apply big_sepL_mono=> k [y1 y2].
    rewrite lookup_zip_with=> ?; simplify_option_eq; auto.
  Qed.
  Lemma big_sepL2_proper Φ Ψ l1 l2 :
    ( k y1 y2, l1 !! k = Some y1  l2 !! k = Some y2  Φ k y1 y2  Ψ k y1 y2) 
    ([ list] k  y1;y2  l1;l2, Φ k y1 y2)  [ list] k  y1;y2  l1;l2, Ψ k y1 y2.
  Proof.
    intros; apply (anti_symm _);
      apply big_sepL2_mono; auto using equiv_entails, equiv_entails_sym.
  Qed.

  Global Instance big_sepL2_ne n :
    Proper (pointwise_relation _ (pointwise_relation _ (pointwise_relation _ (dist n)))
      ==> (=) ==> (=) ==> (dist n))
           (big_sepL2 (PROP:=PROP) (A:=A) (B:=B)).
  Proof.
    intros Φ1 Φ2 HΦ x1 ? <- x2 ? <-. rewrite !big_sepL2_alt. f_equiv.
    f_equiv=> k [y1 y2]. apply HΦ.
  Qed.
  Global Instance big_sepL2_mono' :
    Proper (pointwise_relation _ (pointwise_relation _ (pointwise_relation _ ()))
      ==> (=) ==> (=) ==> ())
           (big_sepL2 (PROP:=PROP) (A:=A) (B:=B)).
  Proof. intros f g Hf l1 ? <- l2 ? <-. apply big_sepL2_mono; intros; apply Hf. Qed.
  Global Instance big_sepL2_proper' :
    Proper (pointwise_relation _ (pointwise_relation _ (pointwise_relation _ ()))
      ==> (=) ==> (=) ==> ())
           (big_sepL2 (PROP:=PROP) (A:=A) (B:=B)).
  Proof. intros f g Hf l1 ? <- l2 ? <-. apply big_sepL2_proper; intros; apply Hf. Qed.

  Lemma big_sepL2_lookup_acc Φ l1 l2 i x1 x2 :
    l1 !! i = Some x1  l2 !! i = Some x2 
    ([ list] ky1;y2  l1;l2, Φ k y1 y2) 
    Φ i x1 x2  (Φ i x1 x2 - ([ list] ky1;y2  l1;l2, Φ k y1 y2)).
  Proof.
    intros Hl1 Hl2. rewrite big_sepL2_alt. apply pure_elim_l=> Hl.
    rewrite {1}big_sepL_lookup_acc; last by rewrite lookup_zip_with; simplify_option_eq.
    by rewrite pure_True // left_id.
  Qed.

  Lemma big_sepL2_lookup Φ l1 l2 i x1 x2 `{!Absorbing (Φ i x1 x2)} :
    l1 !! i = Some x1  l2 !! i = Some x2 
    ([ list] ky1;y2  l1;l2, Φ k y1 y2)  Φ i x1 x2.
  Proof. intros. rewrite big_sepL2_lookup_acc //. by rewrite sep_elim_l. Qed.

  Lemma big_sepL2_fmap_l {A'} (f : A  A') (Φ : nat  A'  B  PROP) l1 l2 :
    ([ list] ky1;y2  f <$> l1; l2, Φ k y1 y2)
     ([ list] ky1;y2  l1;l2, Φ k (f y1) y2).
  Proof.
    rewrite !big_sepL2_alt fmap_length zip_with_fmap_l zip_with_zip big_sepL_fmap.
    by f_equiv; f_equiv=> k [??].
  Qed.
  Lemma big_sepL2_fmap_r {B'} (g : B  B') (Φ : nat  A  B'  PROP) l1 l2 :
    ([ list] ky1;y2  l1; g <$> l2, Φ k y1 y2)
     ([ list] ky1;y2  l1;l2, Φ k y1 (g y2)).
  Proof.
    rewrite !big_sepL2_alt fmap_length zip_with_fmap_r zip_with_zip big_sepL_fmap.
    by f_equiv; f_equiv=> k [??].
  Qed.

  Lemma big_sepL2_sepL2 Φ Ψ l1 l2 :
    ([ list] ky1;y2  l1;l2, Φ k y1 y2  Ψ k y1 y2)
     ([ list] ky1;y2  l1;l2, Φ k y1 y2)  ([ list] ky1;y2  l1;l2, Ψ k y1 y2).
  Proof.
    rewrite !big_sepL2_alt big_sepL_sepL !persistent_and_affinely_sep_l.
    rewrite -assoc (assoc _ _ (<affine> _)%I). rewrite -(comm bi_sep (<affine> _)%I).
    rewrite -assoc (assoc _ _ (<affine> _)%I) -!persistent_and_affinely_sep_l.
    by rewrite affinely_and_r persistent_and_affinely_sep_l idemp.
  Qed.

  Lemma big_sepL2_and Φ Ψ l1 l2 :
    ([ list] ky1;y2  l1;l2, Φ k y1 y2  Ψ k y1 y2)
     ([ list] ky1;y2  l1;l2, Φ k y1 y2)  ([ list] ky1;y2  l1;l2, Ψ k y1 y2).
  Proof. auto using and_intro, big_sepL2_mono, and_elim_l, and_elim_r. Qed.

  Lemma big_sepL2_persistently `{BiAffine PROP} Φ l1 l2 :
    <pers> ([ list] ky1;y2  l1;l2, Φ k y1 y2)
     [ list] ky1;y2  l1;l2, <pers> (Φ k y1 y2).
  Proof.
    by rewrite !big_sepL2_alt persistently_and persistently_pure big_sepL_persistently.
  Qed.

  Lemma big_sepL2_impl Φ Ψ l1 l2 :
    ([ list] ky1;y2  l1;l2, Φ k y1 y2) -
     ( k x1 x2,
      l1 !! k = Some x1  l2 !! k = Some x2  Φ k x1 x2 - Ψ k x1 x2) -
    [ list] ky1;y2  l1;l2, Ψ k y1 y2.
  Proof.
    apply wand_intro_l. revert Φ Ψ l2.
    induction l1 as [|x1 l1 IH]=> Φ Ψ [|x2 l2] /=; [by rewrite sep_elim_r..|].
    rewrite intuitionistically_sep_dup -assoc [( _  _)%I]comm -!assoc assoc.
    apply sep_mono.
    - rewrite (forall_elim 0) (forall_elim x1) (forall_elim x2) !pure_True // !True_impl.
      by rewrite intuitionistically_elim wand_elim_l.
    - rewrite comm -(IH (Φ  S) (Ψ  S)) /=.
      apply sep_mono_l, affinely_mono, persistently_mono.
      apply forall_intro=> k. by rewrite (forall_elim (S k)).
  Qed.

  Global Instance big_sepL2_nil_persistent Φ :
    Persistent ([ list] ky1;y2  []; [], Φ k y1 y2).
  Proof. simpl; apply _. Qed.
  Global Instance big_sepL2_persistent Φ l1 l2 :
    ( k x1 x2, Persistent (Φ k x1 x2)) 
    Persistent ([ list] ky1;y2  l1;l2, Φ k y1 y2).
  Proof. rewrite big_sepL2_alt. apply _. Qed.

  Global Instance big_sepL2_nil_affine Φ :
    Affine ([ list] ky1;y2  []; [], Φ k y1 y2).
  Proof. simpl; apply _. Qed.
  Global Instance big_sepL2_affine Φ l1 l2 :
    ( k x1 x2, Affine (Φ k x1 x2)) 
    Affine ([ list] ky1;y2  l1;l2, Φ k y1 y2).
  Proof. rewrite big_sepL2_alt. apply _. Qed.
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
End sep_list2.

Section and_list.
  Context {A : Type}.
  Implicit Types l : list A.
  Implicit Types Φ Ψ : nat  A  PROP.

  Lemma big_andL_nil Φ : ([ list] ky  nil, Φ k y)  True.
  Proof. done. Qed.
  Lemma big_andL_nil' P Φ : P  [ list] ky  nil, Φ k y.
  Proof. by apply pure_intro. Qed.
  Lemma big_andL_cons Φ x l :
    ([ list] ky  x :: l, Φ k y)  Φ 0 x  [ list] ky  l, Φ (S k) y.
  Proof. by rewrite big_opL_cons. Qed.
  Lemma big_andL_singleton Φ x : ([ list] ky  [x], Φ k y)  Φ 0 x.
  Proof. by rewrite big_opL_singleton. Qed.
  Lemma big_andL_app Φ l1 l2 :
    ([ list] ky  l1 ++ l2, Φ k y)
     ([ list] ky  l1, Φ k y)  ([ list] ky  l2, Φ (length l1 + k) y).
  Proof. by rewrite big_opL_app. Qed.

  Lemma big_andL_mono Φ Ψ l :
    ( k y, l !! k = Some y  Φ k y  Ψ k y) 
    ([ list] k  y  l, Φ k y)  [ list] k  y  l, Ψ k y.
  Proof. apply big_opL_forall; apply _. Qed.
  Lemma big_andL_proper Φ Ψ l :
    ( k y, l !! k = Some y  Φ k y  Ψ k y) 
    ([ list] k  y  l, Φ k y)  ([ list] k  y  l, Ψ k y).
  Proof. apply big_opL_proper. Qed.
  Lemma big_andL_submseteq (Φ : A  PROP) l1 l2 :
    l1 + l2  ([ list] y  l2, Φ y)  [ list] y  l1, Φ y.
  Proof.
    intros [l ->]%submseteq_Permutation. by rewrite big_andL_app and_elim_l.
  Qed.

  Global Instance big_andL_mono' :
    Proper (pointwise_relation _ (pointwise_relation _ ()) ==> (=) ==> ())
           (big_opL (@bi_and PROP) (A:=A)).
  Proof. intros f g Hf m ? <-. apply big_opL_forall; apply _ || intros; apply Hf. Qed.
477
  Global Instance big_andL_id_mono' :
478
    Proper (Forall2 () ==> ()) (big_opL (@bi_and PROP) (λ _ P, P)).
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
  Proof. by induction 1 as [|P Q Ps Qs HPQ ? IH]; rewrite /= ?HPQ ?IH. Qed.

  Lemma big_andL_lookup Φ l i x `{!Absorbing (Φ i x)} :
    l !! i = Some x  ([ list] ky  l, Φ k y)  Φ i x.
  Proof.
    intros. rewrite -(take_drop_middle l i x) // big_andL_app /=.
    rewrite Nat.add_0_r take_length_le;
      eauto using lookup_lt_Some, Nat.lt_le_incl, and_elim_l', and_elim_r'.
  Qed.

  Lemma big_andL_elem_of (Φ : A  PROP) l x `{!Absorbing (Φ x)} :
    x  l  ([ list] y  l, Φ y)  Φ x.
  Proof.
    intros [i ?]%elem_of_list_lookup; eauto using (big_andL_lookup (λ _, Φ)).
  Qed.

  Lemma big_andL_fmap {B} (f : A  B) (Φ : nat  B  PROP) l :
    ([ list] ky  f <$> l, Φ k y)  ([ list] ky  l, Φ k (f y)).
  Proof. by rewrite big_opL_fmap. Qed.

  Lemma big_andL_andL Φ Ψ l :
    ([ list] kx  l, Φ k x  Ψ k x)
     ([ list] kx  l, Φ k x)  ([ list] kx  l, Ψ k x).
  Proof. by rewrite big_opL_opL. Qed.

  Lemma big_andL_and Φ Ψ l :
    ([ list] kx  l, Φ k x  Ψ k x)
     ([ list] kx  l, Φ k x)  ([ list] kx  l, Ψ k x).
  Proof. auto using and_intro, big_andL_mono, and_elim_l, and_elim_r. Qed.

  Lemma big_andL_persistently Φ l :
510
    <pers> ([ list] kx  l, Φ k x)  [ list] kx  l, <pers> (Φ k x).
511
512
  Proof. apply (big_opL_commute _). Qed.

513
  Lemma big_andL_forall `{BiAffine PROP} Φ l :
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
    ([ list] kx  l, Φ k x)  ( k x, l !! k = Some x  Φ k x).
  Proof.
    apply (anti_symm _).
    { apply forall_intro=> k; apply forall_intro=> x.
      apply impl_intro_l, pure_elim_l=> ?; by apply: big_andL_lookup. }
    revert Φ. induction l as [|x l IH]=> Φ; [by auto using big_andL_nil'|].
    rewrite big_andL_cons. apply and_intro.
    - by rewrite (forall_elim 0) (forall_elim x) pure_True // True_impl.
    - rewrite -IH. apply forall_intro=> k; by rewrite (forall_elim (S k)).
  Qed.

  Global Instance big_andL_nil_persistent Φ :
    Persistent ([ list] kx  [], Φ k x).
  Proof. simpl; apply _. Qed.
  Global Instance big_andL_persistent Φ l :
    ( k x, Persistent (Φ k x))  Persistent ([ list] kx  l, Φ k x).
  Proof. revert Φ. induction l as [|x l IH]=> Φ ? /=; apply _. Qed.
End and_list.
532

533
(** ** Big ops over finite maps *)
534
535
536
Section gmap.
  Context `{Countable K} {A : Type}.
  Implicit Types m : gmap K A.
Robbert Krebbers's avatar
Robbert Krebbers committed
537
  Implicit Types Φ Ψ : K  A  PROP.
538

Robbert Krebbers's avatar
Robbert Krebbers committed
539
540
541
542
  Lemma big_sepM_mono Φ Ψ m :
    ( k x, m !! k = Some x  Φ k x  Ψ k x) 
    ([ map] k  x  m, Φ k x)  [ map] k  x  m, Ψ k x.
  Proof. apply big_opM_forall; apply _ || auto. Qed.
543
544
  Lemma big_sepM_proper Φ Ψ m :
    ( k x, m !! k = Some x  Φ k x  Ψ k x) 
545
    ([ map] k  x  m, Φ k x)  ([ map] k  x  m, Ψ k x).
546
  Proof. apply big_opM_proper. Qed.
547
  Lemma big_sepM_subseteq `{BiAffine PROP} Φ m1 m2 :
Robbert Krebbers's avatar
Robbert Krebbers committed
548
549
    m2  m1  ([ map] k  x  m1, Φ k x)  [ map] k  x  m2, Φ k x.
  Proof. intros. by apply big_sepL_submseteq, map_to_list_submseteq. Qed.
550

551
552
  Global Instance big_sepM_mono' :
    Proper (pointwise_relation _ (pointwise_relation _ ()) ==> (=) ==> ())
Robbert Krebbers's avatar
Robbert Krebbers committed
553
554
           (big_opM (@bi_sep PROP) (K:=K) (A:=A)).
  Proof. intros f g Hf m ? <-. apply big_sepM_mono=> ???; apply Hf. Qed.
555

Robbert Krebbers's avatar
Robbert Krebbers committed
556
  Lemma big_sepM_empty Φ : ([ map] kx  , Φ k x)  emp.
557
  Proof. by rewrite big_opM_empty. Qed.
558
  Lemma big_sepM_empty' `{BiAffine PROP} P Φ : P  [ map] kx  , Φ k x.
Robbert Krebbers's avatar
Robbert Krebbers committed
559
  Proof. rewrite big_sepM_empty. apply: affine. Qed.
560

561
  Lemma big_sepM_insert Φ m i x :
562
    m !! i = None 
563
    ([ map] ky  <[i:=x]> m, Φ k y)  Φ i x  [ map] ky  m, Φ k y.
564
  Proof. apply big_opM_insert. Qed.
565

566
  Lemma big_sepM_delete Φ m i x :
567
    m !! i = Some x 
568
    ([ map] ky  m, Φ k y)  Φ i x  [ map] ky  delete i m, Φ k y.
569
  Proof. apply big_opM_delete. Qed.
570

571
572
573
574
575
576
577
578
579
580
581
582
583
  Lemma big_sepM_insert_2 Φ m i x :
    TCOr ( x, Affine (Φ i x)) (Absorbing (Φ i x)) 
    Φ i x - ([ map] ky  m, Φ k y) - [ map] ky  <[i:=x]> m, Φ k y.
  Proof.
    intros Ha. apply wand_intro_r. destruct (m !! i) as [y|] eqn:Hi; last first.
    { by rewrite -big_sepM_insert. }
    assert (TCOr (Affine (Φ i y)) (Absorbing (Φ i x))).
    { destruct Ha; try apply _. }
    rewrite big_sepM_delete // assoc.
    rewrite (sep_elim_l (Φ i x)) -big_sepM_insert ?lookup_delete //.
    by rewrite insert_delete.
  Qed.

584
585
586
587
588
589
590
  Lemma big_sepM_lookup_acc Φ m i x :
    m !! i = Some x 
    ([ map] ky  m, Φ k y)  Φ i x  (Φ i x - ([ map] ky  m, Φ k y)).
  Proof.
    intros. rewrite big_sepM_delete //. by apply sep_mono_r, wand_intro_l.
  Qed.

Robbert Krebbers's avatar
Robbert Krebbers committed
591
  Lemma big_sepM_lookup Φ m i x `{!Absorbing (Φ i x)} :
592
    m !! i = Some x  ([ map] ky  m, Φ k y)  Φ i x.
Robbert Krebbers's avatar
Robbert Krebbers committed
593
  Proof. intros. rewrite big_sepM_lookup_acc //. by rewrite sep_elim_l. Qed.
594

Robbert Krebbers's avatar
Robbert Krebbers committed
595
  Lemma big_sepM_lookup_dom (Φ : K  PROP) m i `{!Absorbing (Φ i)} :
Robbert Krebbers's avatar
Robbert Krebbers committed
596
597
    is_Some (m !! i)  ([ map] k_  m, Φ k)  Φ i.
  Proof. intros [x ?]. by eapply (big_sepM_lookup (λ i x, Φ i)). Qed.
598

599
  Lemma big_sepM_singleton Φ i x : ([ map] ky  {[i:=x]}, Φ k y)  Φ i x.
600
  Proof. by rewrite big_opM_singleton. Qed.
601

Robbert Krebbers's avatar
Robbert Krebbers committed
602
  Lemma big_sepM_fmap {B} (f : A  B) (Φ : K  B  PROP) m :
603
    ([ map] ky  f <$> m, Φ k y)  ([ map] ky  m, Φ k (f y)).
604
  Proof. by rewrite big_opM_fmap. Qed.
605

Robbert Krebbers's avatar
Robbert Krebbers committed
606
607
608
  Lemma big_sepM_insert_override Φ m i x x' :
    m !! i = Some x  (Φ i x  Φ i x') 
    ([ map] ky  <[i:=x']> m, Φ k y)  ([ map] ky  m, Φ k y).
609
  Proof. apply big_opM_insert_override. Qed.
610

Robbert Krebbers's avatar
Robbert Krebbers committed
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
  Lemma big_sepM_insert_override_1 Φ m i x x' :
    m !! i = Some x 
    ([ map] ky  <[i:=x']> m, Φ k y) 
      (Φ i x' - Φ i x) - ([ map] ky  m, Φ k y).
  Proof.
    intros ?. apply wand_intro_l.
    rewrite -insert_delete big_sepM_insert ?lookup_delete //.
    by rewrite assoc wand_elim_l -big_sepM_delete.
  Qed.

  Lemma big_sepM_insert_override_2 Φ m i x x' :
    m !! i = Some x 
    ([ map] ky  m, Φ k y) 
      (Φ i x - Φ i x') - ([ map] ky  <[i:=x']> m, Φ k y).
  Proof.
    intros ?. apply wand_intro_l.
    rewrite {1}big_sepM_delete //; rewrite assoc wand_elim_l.
    rewrite -insert_delete big_sepM_insert ?lookup_delete //.
  Qed.

Robbert Krebbers's avatar
Robbert Krebbers committed
631
  Lemma big_sepM_fn_insert {B} (Ψ : K  A  B  PROP) (f : K  B) m i x b :
632
    m !! i = None 
633
634
       ([ map] ky  <[i:=x]> m, Ψ k y (<[i:=b]> f k))
     (Ψ i x b  [ map] ky  m, Ψ k y (f k)).
635
  Proof. apply big_opM_fn_insert. Qed.
636

Robbert Krebbers's avatar
Robbert Krebbers committed
637
  Lemma big_sepM_fn_insert' (Φ : K  PROP) m i x P :
638
    m !! i = None 
639
    ([ map] ky  <[i:=x]> m, <[i:=P]> Φ k)  (P  [ map] ky  m, Φ k).
640
  Proof. apply big_opM_fn_insert'. Qed.
641

642
643
644
645
646
647
  Lemma big_sepM_union Φ m1 m2 :
    m1 ## m2 
    ([ map] ky  m1  m2, Φ k y)
     ([ map] ky  m1, Φ k y)  ([ map] ky  m2, Φ k y).
  Proof. apply big_opM_union. Qed.

648
  Lemma big_sepM_sepM Φ Ψ m :
649
    ([ map] kx  m, Φ k x  Ψ k x)
650
     ([ map] kx  m, Φ k x)  ([ map] kx  m, Ψ k x).
651
  Proof. apply big_opM_opM. Qed.
652

653
654
655
  Lemma big_sepM_and Φ Ψ m :
    ([ map] kx  m, Φ k x  Ψ k x)
     ([ map] kx  m, Φ k x)  ([ map] kx  m, Ψ k x).
Robbert Krebbers's avatar
Robbert Krebbers committed
656
  Proof. auto using and_intro, big_sepM_mono, and_elim_l, and_elim_r. Qed.
657

658
  Lemma big_sepM_persistently `{BiAffine PROP} Φ m :
659
    (<pers> ([ map] kx  m, Φ k x))  ([ map] kx  m, <pers> (Φ k x)).
660
  Proof. apply (big_opM_commute _). Qed.
661

662
  Lemma big_sepM_forall `{BiAffine PROP} Φ m :
663
    ( k x, Persistent (Φ k x)) 
Ralf Jung's avatar
Ralf Jung committed
664
    ([ map] kx  m, Φ k x)  ( k x, m !! k = Some x  Φ k x).
665
666
667
  Proof.
    intros. apply (anti_symm _).
    { apply forall_intro=> k; apply forall_intro=> x.
Robbert Krebbers's avatar
Robbert Krebbers committed
668
669
      apply impl_intro_l, pure_elim_l=> ?; by apply: big_sepM_lookup. }
    induction m as [|i x m ? IH] using map_ind; auto using big_sepM_empty'.
670
    rewrite big_sepM_insert // -persistent_and_sep. apply and_intro.
671
    - rewrite (forall_elim i) (forall_elim x) lookup_insert.
672
      by rewrite pure_True // True_impl.
673
    - rewrite -IH. apply forall_mono=> k; apply forall_mono=> y.
674
675
      apply impl_intro_l, pure_elim_l=> ?.
      rewrite lookup_insert_ne; last by intros ?; simplify_map_eq.
676
      by rewrite pure_True // True_impl.
677
678
679
  Qed.

  Lemma big_sepM_impl Φ Ψ m :
Robbert Krebbers's avatar
Robbert Krebbers committed
680
    ([ map] kx  m, Φ k x) -
681
     ( k x, m !! k = Some x  Φ k x - Ψ k x) -
Robbert Krebbers's avatar
Robbert Krebbers committed
682
    [ map] kx  m, Ψ k x.
683
  Proof.
Robbert Krebbers's avatar
Robbert Krebbers committed
684
685
    apply wand_intro_l. induction m as [|i x m ? IH] using map_ind.
    { by rewrite sep_elim_r. }
686
    rewrite !big_sepM_insert // intuitionistically_sep_dup.
687
    rewrite -assoc [( _  _)%I]comm -!assoc assoc. apply sep_mono.
Robbert Krebbers's avatar
Robbert Krebbers committed
688
    - rewrite (forall_elim i) (forall_elim x) pure_True ?lookup_insert //.
689
      by rewrite True_impl intuitionistically_elim wand_elim_l.
Robbert Krebbers's avatar
Robbert Krebbers committed
690
    - rewrite comm -IH /=.
691
      apply sep_mono_l, affinely_mono, persistently_mono, forall_mono=> k.
Robbert Krebbers's avatar
Robbert Krebbers committed
692
693
694
      apply forall_mono=> y. apply impl_intro_l, pure_elim_l=> ?.
      rewrite lookup_insert_ne; last by intros ?; simplify_map_eq.
      by rewrite pure_True // True_impl.
695
  Qed.
696

697
  Global Instance big_sepM_empty_persistent Φ :
698
    Persistent ([ map] kx  , Φ k x).
699
  Proof. rewrite /big_opM map_to_list_empty. apply _. Qed.
700
  Global Instance big_sepM_persistent Φ m :
701
    ( k x, Persistent (Φ k x))  Persistent ([ map] kx  m, Φ k x).
702
  Proof. intros. apply big_sepL_persistent=> _ [??]; apply _. Qed.
703

704
705
706
  Global Instance big_sepM_empty_affine Φ :
    Affine ([ map] kx  , Φ k x).
  Proof. rewrite /big_opM map_to_list_empty. apply _. Qed.
707
708
  Global Instance big_sepM_affine Φ m :
    ( k x, Affine (Φ k x))  Affine ([ map] kx  m, Φ k x).
709
  Proof. intros. apply big_sepL_affine=> _ [??]; apply _. Qed.
710
711
End gmap.

712
(** ** Big ops over finite sets *)
713
714
715
Section gset.
  Context `{Countable A}.
  Implicit Types X : gset A.
Robbert Krebbers's avatar
Robbert Krebbers committed
716
  Implicit Types Φ : A  PROP.
717

Robbert Krebbers's avatar
Robbert Krebbers committed
718
719
720
721
  Lemma big_sepS_mono Φ Ψ X :
    ( x, x  X  Φ x  Ψ x) 
    ([ set] x  X, Φ x)  [ set] x  X, Ψ x.
  Proof. intros. apply big_opS_forall; apply _ || auto. Qed.
722
723
  Lemma big_sepS_proper Φ Ψ X :
    ( x, x  X  Φ x  Ψ x) 
724
    ([ set] x  X, Φ x)  ([ set] x  X, Ψ x).
725
  Proof. apply big_opS_proper. Qed.
726
  Lemma big_sepS_subseteq `{BiAffine PROP} Φ X Y :
Robbert Krebbers's avatar
Robbert Krebbers committed
727
728
    Y  X  ([ set] x  X, Φ x)  [ set] x  Y, Φ x.
  Proof. intros. by apply big_sepL_submseteq, elements_submseteq. Qed.
729

730
  Global Instance big_sepS_mono' :
Robbert Krebbers's avatar
Robbert Krebbers committed
731
732
     Proper (pointwise_relation _ () ==> (=) ==> ()) (big_opS (@bi_sep PROP) (A:=A)).
  Proof. intros f g Hf m ? <-. by apply big_sepS_mono. Qed.
733

Robbert Krebbers's avatar
Robbert Krebbers committed
734
  Lemma big_sepS_empty Φ : ([ set] x  , Φ x)  emp.
735
  Proof. by rewrite big_opS_empty. Qed.
736
  Lemma big_sepS_empty' `{!BiAffine PROP} P Φ : P  [ set] x  , Φ x.
Robbert Krebbers's avatar
Robbert Krebbers committed
737
  Proof. rewrite big_sepS_empty. apply: affine. Qed.
738

739
  Lemma big_sepS_insert Φ X x :
740
    x  X  ([ set] y  {[ x ]}  X, Φ y)  (Φ x  [ set] y  X, Φ y).
741
  Proof. apply big_opS_insert. Qed.
742

Robbert Krebbers's avatar
Robbert Krebbers committed
743
  Lemma big_sepS_fn_insert {B} (Ψ : A  B  PROP) f X x b :
744
    x  X 
745
746
       ([ set] y  {[ x ]}  X, Ψ y (<[x:=b]> f y))
     (Ψ x b  [ set] y  X, Ψ y (f y)).
747
  Proof. apply big_opS_fn_insert. Qed.
748

749
  Lemma big_sepS_fn_insert' Φ X x P :
750
    x  X  ([ set] y  {[ x ]}  X, <[x:=P]> Φ y)  (P  [ set] y  X, Φ y).
751
  Proof. apply big_opS_fn_insert'. Qed.
752

Robbert Krebbers's avatar
Robbert Krebbers committed
753
  Lemma big_sepS_union Φ X Y :
754
    X ## Y 
Robbert Krebbers's avatar
Robbert Krebbers committed
755
    ([ set] y  X  Y, Φ y)  ([ set] y  X, Φ y)  ([ set] y  Y, Φ y).
756
  Proof. apply big_opS_union. Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
757

758
  Lemma big_sepS_delete Φ X x :
759
    x  X  ([ set] y  X, Φ y)  Φ x  [ set] y  X  {[ x ]}, Φ y.
760
  Proof. apply big_opS_delete. Qed.
761

Robbert Krebbers's avatar
Robbert Krebbers committed
762
763
  Lemma big_sepS_elem_of Φ X x `{!Absorbing (Φ x)} :
    x  X  ([ set] y  X, Φ y)  Φ x.
Robbert Krebbers's avatar
Robbert Krebbers committed
764
  Proof. intros. rewrite big_sepS_delete //. by rewrite sep_elim_l. Qed.
765

766
767
768
769
770
771
772
  Lemma big_sepS_elem_of_acc Φ X x :
    x  X 
    ([ set] y  X, Φ y)  Φ x  (Φ x - ([ set] y  X, Φ y)).
  Proof.
    intros. rewrite big_sepS_delete //. by apply sep_mono_r, wand_intro_l.
  Qed.

773
  Lemma big_sepS_singleton Φ x : ([ set] y  {[ x ]}, Φ y)  Φ x.
774
  Proof. apply big_opS_singleton. Qed.
775

Robbert Krebbers's avatar
Robbert Krebbers committed
776
777
778
  Lemma big_sepS_filter' (P : A  Prop) `{ x, Decision (P x)} Φ X :
    ([ set] y  filter P X, Φ y)
     ([ set] y  X, if decide (P y) then Φ y else emp).
779
780
781
782
783
784
  Proof.
    induction X as [|x X ? IH] using collection_ind_L.
    { by rewrite filter_empty_L !big_sepS_empty. }
    destruct (decide (P x)).
    - rewrite filter_union_L filter_singleton_L //.
      rewrite !big_sepS_insert //; last set_solver.
Robbert Krebbers's avatar
Robbert Krebbers committed
785
      by rewrite decide_True // IH.
786
    - rewrite filter_union_L filter_singleton_not_L // left_id_L.
Robbert Krebbers's avatar
Robbert Krebbers committed
787
      by rewrite !big_sepS_insert // decide_False // IH left_id.
788
789
  Qed.

Robbert Krebbers's avatar
Robbert Krebbers committed
790
  Lemma big_sepS_filter_acc' (P : A  Prop) `{ y, Decision (P y)} Φ X Y :
791
792
    ( y, y  Y  P y  y  X) 
    ([ set] y  X, Φ y) -
Robbert Krebbers's avatar
Robbert Krebbers committed
793
794
      ([ set] y  Y, if decide (P y) then Φ y else emp) 
      (([ set] y  Y, if decide (P y) then Φ y else emp) - [ set] y  X, Φ y).
795
796
797
  Proof.