fin_collections.v 7.26 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
Require Import Permutation prelude.relations prelude.listset.
Require Export prelude.numbers prelude.collections.
Robbert Krebbers's avatar
Robbert Krebbers committed
8

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

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

17
Global Instance elements_proper: Proper (() ==> ()) (elements (C:=C)).
Robbert Krebbers's avatar
Robbert Krebbers committed
18
19
Proof.
  intros ?? E. apply NoDup_Permutation.
20
21
22
  * apply NoDup_elements.
  * apply NoDup_elements.
  * intros. by rewrite !elem_of_elements, E.
Robbert Krebbers's avatar
Robbert Krebbers committed
23
Qed.
24
Global Instance collection_size_proper: Proper (() ==> (=)) (@size C _).
25
Proof. intros ?? E. apply Permutation_length. by rewrite E. Qed.
26
Lemma size_empty : size ( : C) = 0.
Robbert Krebbers's avatar
Robbert Krebbers committed
27
Proof.
28
  unfold size, collection_size. simpl.
29
  rewrite (elem_of_nil_inv (elements )); [done |].
30
  intro. rewrite elem_of_elements. solve_elem_of.
Robbert Krebbers's avatar
Robbert Krebbers committed
31
Qed.
32
Lemma size_empty_inv (X : C) : size X = 0  X  .
Robbert Krebbers's avatar
Robbert Krebbers committed
33
Proof.
34
  intros. apply equiv_empty. intro. rewrite <-elem_of_elements.
35
  rewrite (nil_length_inv (elements X)). by rewrite elem_of_nil. done.
Robbert Krebbers's avatar
Robbert Krebbers committed
36
Qed.
37
Lemma size_empty_iff (X : C) : size X = 0  X  .
38
Proof. split. apply size_empty_inv. intros E. by rewrite E, size_empty. Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
39
40
Lemma size_non_empty_iff (X : C) : size X  0  X  .
Proof. by rewrite size_empty_iff. Qed.
41
Lemma size_singleton (x : A) : size {[ x ]} = 1.
Robbert Krebbers's avatar
Robbert Krebbers committed
42
Proof.
43
  change (length (elements {[ x ]}) = length [x]).
Robbert Krebbers's avatar
Robbert Krebbers committed
44
  apply Permutation_length, NoDup_Permutation.
45
  * apply NoDup_elements.
46
  * apply NoDup_singleton.
47
48
  * intros. by rewrite elem_of_elements,
      elem_of_singleton, elem_of_list_singleton.
Robbert Krebbers's avatar
Robbert Krebbers committed
49
50
51
Qed.
Lemma size_singleton_inv X x y : size X = 1  x  X  y  X  x = y.
Proof.
52
  unfold size, collection_size. simpl. rewrite <-!elem_of_elements.
53
  generalize (elements X). intros [|? l]; intro; simplify_equality'.
54
  rewrite (nil_length_inv l), !elem_of_list_singleton by done. congruence.
Robbert Krebbers's avatar
Robbert Krebbers committed
55
Qed.
56
Lemma collection_choose_or_empty X : ( x, x  X)  X  .
57
Proof.
58
59
60
  destruct (elements X) as [|x l] eqn:HX; [right|left].
  * apply equiv_empty. intros x. by rewrite <-elem_of_elements, HX, elem_of_nil.
  * exists x. rewrite <-elem_of_elements, HX. by left.
61
Qed.
62
63
64
65
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.
66
Lemma size_pos_elem_of X : 0 < size X   x, x  X.
67
Proof.
68
69
  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
70
Qed.
71
Lemma size_1_elem_of X : size X = 1   x, X  {[ x ]}.
Robbert Krebbers's avatar
Robbert Krebbers committed
72
Proof.
73
74
75
76
  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
77
78
79
Qed.
Lemma size_union X Y : X  Y    size (X  Y) = size X + size Y.
Proof.
80
  intros [E _]. unfold size, collection_size. simpl. rewrite <-app_length.
Robbert Krebbers's avatar
Robbert Krebbers committed
81
  apply Permutation_length, NoDup_Permutation.
82
83
84
85
  * apply NoDup_elements.
  * apply NoDup_app; repeat split; try apply NoDup_elements.
    intros x. rewrite !elem_of_elements. esolve_elem_of.
  * intros. rewrite elem_of_app, !elem_of_elements. solve_elem_of.
Robbert Krebbers's avatar
Robbert Krebbers committed
86
Qed.
87
88
Instance elem_of_dec_slow (x : A) (X : C) : Decision (x  X) | 100.
Proof.
89
  refine (cast_if (decide_rel () x (elements X)));
90
    by rewrite <-(elem_of_elements _).
91
92
93
94
Defined.
Global Program Instance collection_subseteq_dec_slow (X Y : C) :
    Decision (X  Y) | 100 :=
  match decide_rel (=) (size (X  Y)) 0 with
95
  | left E1 => left _ | right E1 => right _
96
97
  end.
Next Obligation.
98
99
  intros x Ex; apply dec_stable; intro. destruct (proj1 (elem_of_empty x)).
  apply (size_empty_inv _ E1). by rewrite elem_of_difference.
100
101
Qed.
Next Obligation.
102
103
  intros E2. destruct E1. apply size_empty_iff, equiv_empty. intros x.
  rewrite elem_of_difference. intros [E3 ?]. by apply E2 in E3.
104
Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
105
Lemma size_union_alt X Y : size (X  Y) = size X + size (Y  X).
106
Proof.
Robbert Krebbers's avatar
Robbert Krebbers committed
107
108
109
  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
110
111
Qed.
Lemma subseteq_size X Y : X  Y  size X  size Y.
112
Proof. intros. rewrite (union_difference X Y), size_union_alt by done. lia. Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
113
Lemma subset_size X Y : X  Y  size X < size Y.
Robbert Krebbers's avatar
Robbert Krebbers committed
114
Proof.
Robbert Krebbers's avatar
Robbert Krebbers committed
115
116
117
118
  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
119
Qed.
120
Lemma collection_wf : wf (strict (@subseteq C _)).
121
Proof. apply (wf_projected (<) size); auto using subset_size, lt_wf. Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
122
Lemma collection_ind (P : C  Prop) :
123
  Proper (() ==> iff) P 
124
  P   ( x X, x  X  P X  P ({[ x ]}  X))   X, P X.
Robbert Krebbers's avatar
Robbert Krebbers committed
125
Proof.
Robbert Krebbers's avatar
Robbert Krebbers committed
126
127
  intros ? Hemp Hadd. apply well_founded_induction with ().
  { apply collection_wf. }
128
  intros X IH. destruct (collection_choose_or_empty X) as [[x ?]|HX].
Robbert Krebbers's avatar
Robbert Krebbers committed
129
130
131
  * 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
132
133
134
Qed.
Lemma collection_fold_ind {B} (P : B  C  Prop) (f : A  B  B) (b : B) :
  Proper ((=) ==> () ==> iff) P 
135
  P b   ( x X r, x  X  P r X  P (f x r) ({[ x ]}  X)) 
136
   X, P (collection_fold f b X) X.
Robbert Krebbers's avatar
Robbert Krebbers committed
137
138
Proof.
  intros ? Hemp Hadd.
139
  cut ( l, NoDup l   X, ( x, x  X  x  l)  P (foldr f b l) X).
140
141
  { intros help ?. apply help; [apply NoDup_elements|].
    symmetry. apply elem_of_elements. }
Robbert Krebbers's avatar
Robbert Krebbers committed
142
  induction 1 as [|x l ?? IH]; simpl.
143
  * intros X HX. setoid_rewrite elem_of_nil in HX.
Robbert Krebbers's avatar
Robbert Krebbers committed
144
    rewrite equiv_empty. done. esolve_elem_of.
145
  * intros X HX. setoid_rewrite elem_of_cons in HX.
Robbert Krebbers's avatar
Robbert Krebbers committed
146
147
    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
148
Qed.
149
150
Lemma collection_fold_proper {B} (R : relation B) `{!Equivalence R}
    (f : A  B  B) (b : B) `{!Proper ((=) ==> R ==> R) f}
151
    (Hf :  a1 a2 b, R (f a1 (f a2 b)) (f a2 (f a1 b))) :
152
  Proper (() ==> R) (collection_fold f b : C  B).
153
Proof. intros ?? E. apply (foldr_permutation R f b); auto. by rewrite E. Qed.
154
155
Global Instance set_Forall_dec `(P : A  Prop)
  `{ x, Decision (P x)} X : Decision (set_Forall P X) | 100.
156
157
Proof.
  refine (cast_if (decide (Forall P (elements X))));
158
    abstract (unfold set_Forall; setoid_rewrite <-elem_of_elements;
159
      by rewrite <-Forall_forall).
160
Defined.
161
162
Global Instance set_Exists_dec `(P : A  Prop) `{ x, Decision (P x)} X :
  Decision (set_Exists P X) | 100.
163
164
Proof.
  refine (cast_if (decide (Exists P (elements X))));
165
    abstract (unfold set_Exists; setoid_rewrite <-elem_of_elements;
166
      by rewrite <-Exists_exists).
167
Defined.
168
Global Instance rel_elem_of_dec `{ x y, Decision (R x y)} x X :
169
  Decision (elem_of_upto R x X) | 100 := decide (set_Exists (R x) X).
Robbert Krebbers's avatar
Robbert Krebbers committed
170
End fin_collection.