fin_collections.v 7.03 KB
Newer Older
1 2 3 4 5
(* Copyright (c) 2012, Robbert Krebbers. *)
(* 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.
Require Export collections numbers listset.
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 15 16 17 18

Section fin_collection.
Context `{FinCollection A C}.

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

26
Lemma size_empty : size ( : C) = 0.
Robbert Krebbers's avatar
Robbert Krebbers committed
27
Proof.
28 29 30
  unfold size, collection_size. simpl.
  rewrite (elem_of_nil_inv (elements )).
  * done.
31
  * intro. rewrite <-elements_spec. solve_elem_of.
Robbert Krebbers's avatar
Robbert Krebbers committed
32
Qed.
33
Lemma size_empty_inv (X : C) : size X = 0  X  .
Robbert Krebbers's avatar
Robbert Krebbers committed
34 35
Proof.
  intros. apply equiv_empty. intro. rewrite elements_spec.
36
  rewrite (nil_length (elements X)). by rewrite elem_of_nil. done.
Robbert Krebbers's avatar
Robbert Krebbers committed
37
Qed.
38 39 40 41
Lemma size_empty_iff (X : C) : size X = 0  X  .
Proof. split. apply size_empty_inv. intros E. by rewrite E, size_empty. Qed.
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
42

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

61
Lemma elem_of_or_empty X : ( x, x  X)  X  .
Robbert Krebbers's avatar
Robbert Krebbers committed
62
Proof.
63 64 65 66 67
  destruct (elements X) as [|x xs] eqn:E.
  * right. apply equiv_empty. intros x Ex.
    by rewrite elements_spec, E, elem_of_nil in Ex.
  * left. exists x. rewrite elements_spec, E.
    by constructor.
Robbert Krebbers's avatar
Robbert Krebbers committed
68
Qed.
69 70 71 72 73 74 75 76

Lemma choose X : X     x, x  X.
Proof.
  destruct (elem_of_or_empty X) as [[x ?]|?].
  * by exists x.
  * done.
Qed.
Lemma size_pos_choose X : 0 < size X   x, x  X.
77
Proof.
78 79 80
  intros E1. apply choose.
  intros E2. rewrite E2, size_empty in E1.
  by apply (Lt.lt_n_0 0).
Robbert Krebbers's avatar
Robbert Krebbers committed
81
Qed.
82
Lemma size_1_choose X : size X = 1   x, X  {[ x ]}.
Robbert Krebbers's avatar
Robbert Krebbers committed
83 84
Proof.
  intros E. destruct (size_pos_choose X).
85
  * rewrite E. auto with arith.
86
  * exists x. apply elem_of_equiv. split.
87 88
    + intro. rewrite elem_of_singleton.
      eauto using size_singleton_inv.
89
    + solve_elem_of.
Robbert Krebbers's avatar
Robbert Krebbers committed
90 91 92 93
Qed.

Lemma size_union X Y : X  Y    size (X  Y) = size X + size Y.
Proof.
94
  intros [E _]. unfold size, collection_size. simpl. rewrite <-app_length.
Robbert Krebbers's avatar
Robbert Krebbers committed
95
  apply Permutation_length, NoDup_Permutation.
96
  * apply elements_nodup.
97
  * apply NoDup_app; repeat split; try apply elements_nodup.
98
    intros x. rewrite <-!elements_spec. esolve_elem_of.
99
  * intros. rewrite elem_of_app, <-!elements_spec. solve_elem_of.
Robbert Krebbers's avatar
Robbert Krebbers committed
100
Qed.
101 102

Instance elem_of_dec_slow (x : A) (X : C) : Decision (x  X) | 100.
103
Proof.
104 105 106 107 108 109 110 111 112 113 114 115 116 117
  refine (cast_if (decide_rel () x (elements X)));
    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.
  intros x Ex; apply dec_stable; intro.
  destruct (proj1 (elem_of_empty x)).
  apply (size_empty_inv _ E1).
  by rewrite elem_of_difference.
118
Qed.
119 120 121 122 123
Next Obligation.
  intros E2. destruct E1.
  apply size_empty_iff, equiv_empty. intros x.
  rewrite elem_of_difference. intros [E3 ?].
  by apply E2 in E3.
124
Qed.
125 126

Lemma size_union_alt X Y : size (X  Y) = size X + size (Y  X).
Robbert Krebbers's avatar
Robbert Krebbers committed
127
Proof.
128 129 130
  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
131 132 133
Qed.

Lemma subseteq_size X Y : X  Y  size X  size Y.
134
Proof.
135
  intros. rewrite (union_difference X Y), size_union_alt by done. lia.
136
Qed.
137
Lemma subset_size X Y : X  Y  size X < size Y.
Robbert Krebbers's avatar
Robbert Krebbers committed
138
Proof.
139 140 141 142
  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
143 144
Qed.

145 146 147
Lemma collection_wf : wf (@subset C _).
Proof. apply well_founded_lt_compat with size, subset_size. Qed.

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

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

191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
Global Instance cforall_dec `(P : A  Prop)
  `{ x, Decision (P x)} X : Decision (cforall P X) | 100.
Proof.
  refine (cast_if (decide (Forall P (elements X))));
    abstract (unfold cforall; setoid_rewrite elements_spec;
      by rewrite <-Forall_forall).
Defined.

Global Instance cexists_dec `(P : A  Prop) `{ x, Decision (P x)} X :
  Decision (cexists P X) | 100.
Proof.
  refine (cast_if (decide (Exists P (elements X))));
    abstract (unfold cexists; setoid_rewrite elements_spec;
      by rewrite <-Exists_exists).
Defined.

207 208
Global Instance rel_elem_of_dec `{ x y, Decision (R x y)} x X :
  Decision (elem_of_upto R x X) | 100 := decide (cexists (R x) X).
Robbert Krebbers's avatar
Robbert Krebbers committed
209
End fin_collection.