fin_collections.v 7.76 KB
Newer Older
Robbert Krebbers's avatar
Robbert Krebbers committed
1
(* Copyright (c) 2012-2014, Robbert Krebbers. *)
2
3
4
5
(* This file is distributed under the terms of the BSD license. *)
(** This file collects definitions and theorems on finite collections. Most
importantly, it implements a fold and size function and some useful induction
principles on finite collections . *)
6
7
Require Import Permutation ars listset.
Require Export numbers collections.
Robbert Krebbers's avatar
Robbert Krebbers committed
8

9
10
Definition collection_choose `{Elements A C} (X : C) : option A :=
  head (elements X).
11
12
13
Instance collection_size `{Elements A C} : Size C := length  elements.
Definition collection_fold `{Elements A C} {B}
  (f : A  B  B) (b : B) : C  B := foldr f b  elements.
Robbert Krebbers's avatar
Robbert Krebbers committed
14
15
16
17

Section fin_collection.
Context `{FinCollection A C}.

18
Global Instance elements_proper: Proper (() ==> ()) elements.
Robbert Krebbers's avatar
Robbert Krebbers committed
19
20
Proof.
  intros ?? E. apply NoDup_Permutation.
21
22
  * apply elements_nodup.
  * apply elements_nodup.
23
  * intros. by rewrite <-!elements_spec, E.
Robbert Krebbers's avatar
Robbert Krebbers committed
24
25
Qed.
Global Instance collection_size_proper: Proper (() ==> (=)) size.
26
Proof. intros ?? E. apply Permutation_length. by rewrite E. Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
27

28
Lemma size_empty : size ( : C) = 0.
Robbert Krebbers's avatar
Robbert Krebbers committed
29
Proof.
30
  unfold size, collection_size. simpl.
31
32
  rewrite (elem_of_nil_inv (elements )); [done |].
  intro. rewrite <-elements_spec. solve_elem_of.
Robbert Krebbers's avatar
Robbert Krebbers committed
33
Qed.
34
Lemma size_empty_inv (X : C) : size X = 0  X  .
Robbert Krebbers's avatar
Robbert Krebbers committed
35
36
Proof.
  intros. apply equiv_empty. intro. rewrite elements_spec.
37
  rewrite (nil_length_inv (elements X)). by rewrite elem_of_nil. done.
Robbert Krebbers's avatar
Robbert Krebbers committed
38
Qed.
39
Lemma size_empty_iff (X : C) : size X = 0  X  .
40
Proof. split. apply size_empty_inv. intros E. by rewrite E, size_empty. Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
41
42
Lemma size_non_empty_iff (X : C) : size X  0  X  .
Proof. by rewrite size_empty_iff. Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
43

44
Lemma size_singleton (x : A) : size {[ x ]} = 1.
Robbert Krebbers's avatar
Robbert Krebbers committed
45
Proof.
46
  change (length (elements {[ x ]}) = length [x]).
Robbert Krebbers's avatar
Robbert Krebbers committed
47
  apply Permutation_length, NoDup_Permutation.
48
49
  * apply elements_nodup.
  * apply NoDup_singleton.
50
51
  * intros.
    by rewrite <-elements_spec, elem_of_singleton, elem_of_list_singleton.
Robbert Krebbers's avatar
Robbert Krebbers committed
52
53
54
Qed.
Lemma size_singleton_inv X x y : size X = 1  x  X  y  X  x = y.
Proof.
55
  unfold size, collection_size. simpl. rewrite !elements_spec.
56
  generalize (elements X). intros [|? l]; intro; simplify_equality.
57
  rewrite (nil_length_inv l), !elem_of_list_singleton by done. congruence.
Robbert Krebbers's avatar
Robbert Krebbers committed
58
59
Qed.

60
Lemma collection_choose_Some X x : collection_choose X = Some x  x  X.
61
Proof.
62
63
  unfold collection_choose. destruct (elements X) eqn:E; intros;
    simplify_equality. rewrite elements_spec, E. by left.
64
Qed.
65
Lemma collection_choose_None X : collection_choose X = None  X  .
66
Proof.
67
68
  unfold collection_choose.
  destruct (elements X) eqn:E; intros; simplify_equality.
69
70
  apply equiv_empty. intros x. by rewrite elements_spec, E, elem_of_nil.
Qed.
71
Lemma elem_of_or_empty X : ( x, x  X)  X  .
Robbert Krebbers's avatar
Robbert Krebbers committed
72
Proof.
73
74
75
76
77
78
79
80
  destruct (collection_choose X) eqn:?;
    eauto using collection_choose_Some, collection_choose_None.
Qed.
Lemma collection_choose_is_Some X : X    is_Some (collection_choose X).
Proof.
  destruct (collection_choose X) eqn:?.
  * rewrite elem_of_equiv_empty. split; eauto using collection_choose_Some.
  * split. intros []; eauto using collection_choose_None. by intros [??].
Robbert Krebbers's avatar
Robbert Krebbers committed
81
Qed.
82
Lemma not_elem_of_equiv_empty X : X    ( x, x  X).
83
Proof.
84
85
  destruct (elem_of_or_empty X) as [?|E]; [esolve_elem_of |].
  setoid_rewrite E. setoid_rewrite elem_of_empty. naive_solver.
86
Qed.
87
Lemma size_pos_elem_of X : 0 < size X   x, x  X.
88
Proof.
89
90
  intros E1. apply not_elem_of_equiv_empty. intros E2.
  rewrite E2, size_empty in E1. lia.
Robbert Krebbers's avatar
Robbert Krebbers committed
91
Qed.
92
Lemma size_1_elem_of X : size X = 1   x, X  {[ x ]}.
Robbert Krebbers's avatar
Robbert Krebbers committed
93
Proof.
94
95
96
97
  intros E. destruct (size_pos_elem_of X); auto with lia.
  exists x. apply elem_of_equiv. split.
  * rewrite elem_of_singleton. eauto using size_singleton_inv.
  * solve_elem_of.
Robbert Krebbers's avatar
Robbert Krebbers committed
98
99
100
101
Qed.

Lemma size_union X Y : X  Y    size (X  Y) = size X + size Y.
Proof.
102
  intros [E _]. unfold size, collection_size. simpl. rewrite <-app_length.
Robbert Krebbers's avatar
Robbert Krebbers committed
103
  apply Permutation_length, NoDup_Permutation.
104
  * apply elements_nodup.
105
  * apply NoDup_app; repeat split; try apply elements_nodup.
106
    intros x. rewrite <-!elements_spec. esolve_elem_of.
107
  * intros. rewrite elem_of_app, <-!elements_spec. solve_elem_of.
Robbert Krebbers's avatar
Robbert Krebbers committed
108
Qed.
109
110
111

Instance elem_of_dec_slow (x : A) (X : C) : Decision (x  X) | 100.
Proof.
112
  refine (cast_if (decide_rel () x (elements X)));
113
114
115
116
117
118
119
120
121
    by rewrite (elements_spec _).
Defined.
Global Program Instance collection_subseteq_dec_slow (X Y : C) :
    Decision (X  Y) | 100 :=
  match decide_rel (=) (size (X  Y)) 0 with
  | left E1 => left _
  | right E1 => right _
  end.
Next Obligation.
122
123
  intros x Ex; apply dec_stable; intro. destruct (proj1 (elem_of_empty x)).
  apply (size_empty_inv _ E1). by rewrite elem_of_difference.
124
125
Qed.
Next Obligation.
126
127
  intros E2. destruct E1. apply size_empty_iff, equiv_empty. intros x.
  rewrite elem_of_difference. intros [E3 ?]. by apply E2 in E3.
128
129
Qed.

Robbert Krebbers's avatar
Robbert Krebbers committed
130
Lemma size_union_alt X Y : size (X  Y) = size X + size (Y  X).
131
Proof.
Robbert Krebbers's avatar
Robbert Krebbers committed
132
133
134
  rewrite <-size_union by solve_elem_of.
  setoid_replace (Y  X) with ((Y  X)  X) by esolve_elem_of.
  rewrite <-union_difference, (commutative ()); solve_elem_of.
Robbert Krebbers's avatar
Robbert Krebbers committed
135
136
137
Qed.

Lemma subseteq_size X Y : X  Y  size X  size Y.
138
Proof. intros. rewrite (union_difference X Y), size_union_alt by done. lia. Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
139
Lemma subset_size X Y : X  Y  size X < size Y.
Robbert Krebbers's avatar
Robbert Krebbers committed
140
Proof.
Robbert Krebbers's avatar
Robbert Krebbers committed
141
142
143
144
  intros. rewrite (union_difference X Y) by solve_elem_of.
  rewrite size_union_alt, difference_twice.
  cut (size (Y  X)  0); [lia |].
  by apply size_non_empty_iff, non_empty_difference.
Robbert Krebbers's avatar
Robbert Krebbers committed
145
146
Qed.

147
Lemma collection_wf : wf (strict (@subseteq C _)).
148
Proof. apply (wf_projected (<) size); auto using subset_size, lt_wf. Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
149

Robbert Krebbers's avatar
Robbert Krebbers committed
150
Lemma collection_ind (P : C  Prop) :
151
  Proper (() ==> iff) P 
152
  P   ( x X, x  X  P X  P ({[ x ]}  X))   X, P X.
Robbert Krebbers's avatar
Robbert Krebbers committed
153
Proof.
Robbert Krebbers's avatar
Robbert Krebbers committed
154
155
156
157
158
159
  intros ? Hemp Hadd. apply well_founded_induction with ().
  { apply collection_wf. }
  intros X IH. destruct (elem_of_or_empty X) as [[x ?]|HX].
  * rewrite (union_difference {[ x ]} X) by solve_elem_of.
    apply Hadd. solve_elem_of. apply IH. esolve_elem_of.
  * by rewrite HX.
Robbert Krebbers's avatar
Robbert Krebbers committed
160
161
162
163
Qed.

Lemma collection_fold_ind {B} (P : B  C  Prop) (f : A  B  B) (b : B) :
  Proper ((=) ==> () ==> iff) P 
164
  P b   ( x X r, x  X  P r X  P (f x r) ({[ x ]}  X)) 
165
   X, P (collection_fold f b X) X.
Robbert Krebbers's avatar
Robbert Krebbers committed
166
167
Proof.
  intros ? Hemp Hadd.
168
  cut ( l, NoDup l   X, ( x, x  X  x  l)  P (foldr f b l) X).
169
  { intros help ?. apply help. apply elements_nodup. apply elements_spec. }
Robbert Krebbers's avatar
Robbert Krebbers committed
170
  induction 1 as [|x l ?? IH]; simpl.
171
  * intros X HX. setoid_rewrite elem_of_nil in HX.
Robbert Krebbers's avatar
Robbert Krebbers committed
172
    rewrite equiv_empty. done. esolve_elem_of.
173
  * intros X HX. setoid_rewrite elem_of_cons in HX.
Robbert Krebbers's avatar
Robbert Krebbers committed
174
175
    rewrite (union_difference {[ x ]} X) by esolve_elem_of.
    apply Hadd. solve_elem_of. apply IH. esolve_elem_of.
Robbert Krebbers's avatar
Robbert Krebbers committed
176
177
Qed.

178
179
Lemma collection_fold_proper {B} (R : relation B) `{!Equivalence R}
    (f : A  B  B) (b : B) `{!Proper ((=) ==> R ==> R) f}
180
181
    (Hf :  a1 a2 b, R (f a1 (f a2 b)) (f a2 (f a1 b))) :
  Proper (() ==> R) (collection_fold f b).
182
Proof. intros ?? E. apply (foldr_permutation R f b); auto. by rewrite E. Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
183

184
185
Global Instance set_Forall_dec `(P : A  Prop)
  `{ x, Decision (P x)} X : Decision (set_Forall P X) | 100.
186
187
Proof.
  refine (cast_if (decide (Forall P (elements X))));
188
    abstract (unfold set_Forall; setoid_rewrite elements_spec;
189
      by rewrite <-Forall_forall).
190
Defined.
191
192
Global Instance set_Exists_dec `(P : A  Prop) `{ x, Decision (P x)} X :
  Decision (set_Exists P X) | 100.
193
194
Proof.
  refine (cast_if (decide (Exists P (elements X))));
195
    abstract (unfold set_Exists; setoid_rewrite elements_spec;
196
      by rewrite <-Exists_exists).
197
Defined.
198
Global Instance rel_elem_of_dec `{ x y, Decision (R x y)} x X :
199
  Decision (elem_of_upto R x X) | 100 := decide (set_Exists (R x) X).
Robbert Krebbers's avatar
Robbert Krebbers committed
200
End fin_collection.