fin_collections.v 7.49 KB
Newer Older
Robbert Krebbers's avatar
Robbert Krebbers committed
1
(* Copyright (c) 2012-2015, 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
8
From Coq Require Import Permutation.
From stdpp Require Import relations listset.
From stdpp Require Export numbers collections.
Robbert Krebbers's avatar
Robbert Krebbers committed
9

10
11
12
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
13
14
15

Section fin_collection.
Context `{FinCollection A C}.
16
Implicit Types X Y : C.
Robbert Krebbers's avatar
Robbert Krebbers committed
17

18
19
Lemma fin_collection_finite X : set_finite X.
Proof. by exists (elements X); intros; rewrite elem_of_elements. Qed.
20
21
22
23
24
25
26
27

Instance elem_of_dec_slow (x : A) (X : C) : Decision (x  X) | 100.
Proof.
  refine (cast_if (decide_rel () x (elements X)));
    by rewrite <-(elem_of_elements _).
Defined.

(** * The [elements] operation *)
28
Global Instance elements_proper: Proper (() ==> ()) (elements (C:=C)).
Robbert Krebbers's avatar
Robbert Krebbers committed
29
30
Proof.
  intros ?? E. apply NoDup_Permutation.
31
32
33
  - apply NoDup_elements.
  - apply NoDup_elements.
  - intros. by rewrite !elem_of_elements, E.
Robbert Krebbers's avatar
Robbert Krebbers committed
34
Qed.
35

36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
Lemma elements_empty : elements ( : C) = [].
Proof.
  apply elem_of_nil_inv; intros x.
  rewrite elem_of_elements, elem_of_empty; tauto.
Qed.
Lemma elements_union_singleton (X : C) x :
  x  X  elements ({[ x ]}  X)  x :: elements X.
Proof.
  intros ?; apply NoDup_Permutation.
  { apply NoDup_elements. }
  { by constructor; rewrite ?elem_of_elements; try apply NoDup_elements. }
  intros y; rewrite elem_of_elements, elem_of_union, elem_of_singleton.
  by rewrite elem_of_cons, elem_of_elements.
Qed.
Lemma elements_singleton x : elements {[ x ]} = [x].
Proof.
  apply Permutation_singleton. by rewrite <-(right_id  () {[x]}),
53
    elements_union_singleton, elements_empty by set_solver.
54
55
56
57
58
59
60
Qed.
Lemma elements_contains X Y : X  Y  elements X `contains` elements Y.
Proof.
  intros; apply NoDup_contains; auto using NoDup_elements.
  intros x. rewrite !elem_of_elements; auto.
Qed.

61
(** * The [size] operation *)
62
Global Instance collection_size_proper: Proper (() ==> (=)) (@size C _).
63
Proof. intros ?? E. apply Permutation_length. by rewrite E. Qed.
64

65
Lemma size_empty : size ( : C) = 0.
66
Proof. unfold size, collection_size. simpl. by rewrite elements_empty. Qed.
67
Lemma size_empty_inv (X : C) : size X = 0  X  .
Robbert Krebbers's avatar
Robbert Krebbers committed
68
Proof.
69
70
  intros; apply equiv_empty; intros x; rewrite <-elem_of_elements.
  by rewrite (nil_length_inv (elements X)), ?elem_of_nil.
Robbert Krebbers's avatar
Robbert Krebbers committed
71
Qed.
72
Lemma size_empty_iff (X : C) : size X = 0  X  .
73
Proof. split. apply size_empty_inv. by intros ->; rewrite size_empty. Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
74
75
Lemma size_non_empty_iff (X : C) : size X  0  X  .
Proof. by rewrite size_empty_iff. Qed.
76

77
Lemma collection_choose_or_empty X : ( x, x  X)  X  .
78
Proof.
79
  destruct (elements X) as [|x l] eqn:HX; [right|left].
80
81
  - apply equiv_empty; intros x. by rewrite <-elem_of_elements, HX, elem_of_nil.
  - exists x. rewrite <-elem_of_elements, HX. by left.
82
Qed.
83
84
85
86
Lemma collection_choose X : X     x, x  X.
Proof. intros. by destruct (collection_choose_or_empty X). Qed.
Lemma collection_choose_L `{!LeibnizEquiv C} X : X     x, x  X.
Proof. unfold_leibniz. apply collection_choose. Qed.
87
Lemma size_pos_elem_of X : 0 < size X   x, x  X.
88
Proof.
89
90
  intros Hsz. destruct (collection_choose_or_empty X) as [|HX]; [done|].
  contradict Hsz. rewrite HX, size_empty; lia.
Robbert Krebbers's avatar
Robbert Krebbers committed
91
Qed.
92
93
94
95
96
97
98
99
100

Lemma size_singleton (x : A) : size {[ x ]} = 1.
Proof. unfold size, collection_size. simpl. by rewrite elements_singleton. Qed.
Lemma size_singleton_inv X x y : size X = 1  x  X  y  X  x = y.
Proof.
  unfold size, collection_size. simpl. rewrite <-!elem_of_elements.
  generalize (elements X). intros [|? l]; intro; simplify_eq/=.
  rewrite (nil_length_inv l), !elem_of_list_singleton by done; congruence.
Qed.
101
Lemma size_1_elem_of X : size X = 1   x, X  {[ x ]}.
Robbert Krebbers's avatar
Robbert Krebbers committed
102
Proof.
103
104
  intros E. destruct (size_pos_elem_of X); auto with lia.
  exists x. apply elem_of_equiv. split.
105
  - rewrite elem_of_singleton. eauto using size_singleton_inv.
106
  - set_solver.
Robbert Krebbers's avatar
Robbert Krebbers committed
107
Qed.
108

Robbert Krebbers's avatar
Robbert Krebbers committed
109
Lemma size_union X Y : X  Y  size (X  Y) = size X + size Y.
Robbert Krebbers's avatar
Robbert Krebbers committed
110
Proof.
Robbert Krebbers's avatar
Robbert Krebbers committed
111
  intros. unfold size, collection_size. simpl. rewrite <-app_length.
Robbert Krebbers's avatar
Robbert Krebbers committed
112
  apply Permutation_length, NoDup_Permutation.
113
114
  - apply NoDup_elements.
  - apply NoDup_app; repeat split; try apply NoDup_elements.
115
    intros x; rewrite !elem_of_elements; set_solver.
116
  - intros. by rewrite elem_of_app, !elem_of_elements, elem_of_union.
Robbert Krebbers's avatar
Robbert Krebbers committed
117
118
Qed.
Lemma size_union_alt X Y : size (X  Y) = size X + size (Y  X).
119
Proof.
120
121
122
  rewrite <-size_union by set_solver.
  setoid_replace (Y  X) with ((Y  X)  X) by set_solver.
  rewrite <-union_difference, (comm ()); set_solver.
Robbert Krebbers's avatar
Robbert Krebbers committed
123
Qed.
124

Robbert Krebbers's avatar
Robbert Krebbers committed
125
Lemma subseteq_size X Y : X  Y  size X  size Y.
126
Proof. intros. rewrite (union_difference X Y), size_union_alt by done. lia. Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
127
Lemma subset_size X Y : X  Y  size X < size Y.
Robbert Krebbers's avatar
Robbert Krebbers committed
128
Proof.
129
  intros. rewrite (union_difference X Y) by set_solver.
Robbert Krebbers's avatar
Robbert Krebbers committed
130
131
132
  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
133
Qed.
134
135

(** * Induction principles *)
136
Lemma collection_wf : wf (strict (@subseteq C _)).
137
Proof. apply (wf_projected (<) size); auto using subset_size, lt_wf. Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
138
Lemma collection_ind (P : C  Prop) :
139
  Proper (() ==> iff) P 
140
  P   ( x X, x  X  P X  P ({[ x ]}  X))   X, P X.
Robbert Krebbers's avatar
Robbert Krebbers committed
141
Proof.
Robbert Krebbers's avatar
Robbert Krebbers committed
142
143
  intros ? Hemp Hadd. apply well_founded_induction with ().
  { apply collection_wf. }
144
  intros X IH. destruct (collection_choose_or_empty X) as [[x ?]|HX].
145
146
  - rewrite (union_difference {[ x ]} X) by set_solver.
    apply Hadd. set_solver. apply IH; set_solver.
147
  - by rewrite HX.
Robbert Krebbers's avatar
Robbert Krebbers committed
148
Qed.
149
150

(** * The [collection_fold] operation *)
Robbert Krebbers's avatar
Robbert Krebbers committed
151
152
Lemma collection_fold_ind {B} (P : B  C  Prop) (f : A  B  B) (b : B) :
  Proper ((=) ==> () ==> iff) P 
153
  P b   ( x X r, x  X  P r X  P (f x r) ({[ x ]}  X)) 
154
   X, P (collection_fold f b X) X.
Robbert Krebbers's avatar
Robbert Krebbers committed
155
156
Proof.
  intros ? Hemp Hadd.
157
  cut ( l, NoDup l   X, ( x, x  X  x  l)  P (foldr f b l) X).
158
159
  { intros help ?. apply help; [apply NoDup_elements|].
    symmetry. apply elem_of_elements. }
Robbert Krebbers's avatar
Robbert Krebbers committed
160
  induction 1 as [|x l ?? IH]; simpl.
161
  - intros X HX. setoid_rewrite elem_of_nil in HX.
162
    rewrite equiv_empty. done. set_solver.
163
  - intros X HX. setoid_rewrite elem_of_cons in HX.
164
165
    rewrite (union_difference {[ x ]} X) by set_solver.
    apply Hadd. set_solver. apply IH. set_solver.
Robbert Krebbers's avatar
Robbert Krebbers committed
166
Qed.
167
168
Lemma collection_fold_proper {B} (R : relation B) `{!Equivalence R}
    (f : A  B  B) (b : B) `{!Proper ((=) ==> R ==> R) f}
169
    (Hf :  a1 a2 b, R (f a1 (f a2 b)) (f a2 (f a1 b))) :
170
  Proper (() ==> R) (collection_fold f b : C  B).
171
Proof. intros ?? E. apply (foldr_permutation R f b); auto. by rewrite E. Qed.
172
173

(** * Decision procedures *)
174
175
Global Instance set_Forall_dec `(P : A  Prop)
  `{ x, Decision (P x)} X : Decision (set_Forall P X) | 100.
176
177
Proof.
  refine (cast_if (decide (Forall P (elements X))));
178
    abstract (unfold set_Forall; setoid_rewrite <-elem_of_elements;
179
      by rewrite <-Forall_forall).
180
Defined.
181
182
Global Instance set_Exists_dec `(P : A  Prop) `{ x, Decision (P x)} X :
  Decision (set_Exists P X) | 100.
183
184
Proof.
  refine (cast_if (decide (Exists P (elements X))));
185
    abstract (unfold set_Exists; setoid_rewrite <-elem_of_elements;
186
      by rewrite <-Exists_exists).
187
Defined.
Robbert Krebbers's avatar
Robbert Krebbers committed
188
End fin_collection.