1 (**************************************************************************)
4 (* ||A|| A project by Andrea Asperti *)
6 (* ||I|| Developers: *)
7 (* ||T|| The HELM team. *)
8 (* ||A|| http://helm.cs.unibo.it *)
10 (* \ / This file is distributed under the terms of the *)
11 (* v GNU General Public License Version 2 *)
13 (**************************************************************************)
15 include "sets/sets.ma".
16 include "nat/plus.ma".
17 include "nat/compare.ma".
18 include "nat/minus.ma".
20 alias symbol "eq" = "setoid eq".
21 alias symbol "eq" = "setoid1 eq".
22 alias symbol "eq" = "setoid eq".
23 alias symbol "eq" = "setoid1 eq".
24 alias symbol "eq" = "setoid eq".
25 alias symbol "eq" = "setoid1 eq".
26 alias symbol "eq" = "setoid eq".
27 alias symbol "eq" = "setoid1 eq".
28 alias symbol "eq" = "setoid eq".
29 alias symbol "eq" = "setoid1 eq".
30 alias symbol "eq" = "setoid eq".
31 alias symbol "eq" = "setoid1 eq".
32 nrecord partition (A: setoid) : Type[1] ≝
34 indexes: qpowerclass support;
35 class: unary_morphism1 (setoid1_of_setoid support) (qpowerclass_setoid A);
36 inhabited: ∀i. i ∈ indexes → class i ≬ class i;
37 disjoint: ∀i,j. i ∈ indexes → j ∈ indexes → class i ≬ class j → i=j;
38 covers: big_union support ? ? (λx.class x) = full_set A
39 }. napply indexes; nqed.
43 nlet rec partition_splits_card_map
44 A (P:partition A) n s (f:isomorphism ?? (Nat_ n) (indexes ? P))
45 (fi: ∀i. isomorphism ?? (Nat_ (s i)) (class ? P (iso_f ???? f i))) m index
47 match ltb m (s index) with
48 [ true ⇒ iso_f ???? (fi index) m
51 [ O ⇒ (* dummy value: it could be an elim False: *) iso_f ???? (fi O) O
53 partition_splits_card_map A P n s f fi (minus m (s index)) index']].
55 naxiom big_union_preserves_iso:
57 ∀g: isomorphism A' A T' T.
58 big_union A B T f = big_union A' B T' (λx.f (iso_f ???? g x)).
60 naxiom le_to_lt_or_eq: ∀n,m. n ≤ m → n < m ∨ n = m.
61 alias symbol "eq" = "leibnitz's equality".
62 naxiom minus_canc: ∀n. O = minus n n.
63 naxiom lt_to_ltb_t: ∀n,m. ∀P: bool → CProp[0]. P true → n < m → P (ltb n m).
64 naxiom lt_to_ltb_f: ∀n,m. ∀P: bool → CProp[0]. P false → ¬ (n < m) → P (ltb n m).
65 naxiom lt_to_minus: ∀n,m. n < m → S (minus (minus m n) (S O)) = minus m n.
66 naxiom not_lt_O: ∀n. ¬ (n < O).
67 naxiom minus_S: ∀n,m. m ≤ n → minus (S n) m = S (minus n m).
68 naxiom minus_lt_to_lt: ∀n,m,p. n < p → minus n m < p.
69 naxiom minus_O_n: ∀n. O = minus O n.
70 naxiom le_O_to_eq: ∀n. n ≤ O → n=O.
71 naxiom lt_to_minus_to_S: ∀n,m. m < n → ∃k. minus n m = S k.
72 naxiom ltb_t: ∀n,m. n < m → ltb n m = true.
73 naxiom ltb_f: ∀n,m. ¬ (n < m) → ltb n m = false.
74 naxiom plus_n_O: ∀n. plus n O = n.
75 naxiom not_lt_plus: ∀n,m. ¬ (plus n m < n).
76 naxiom lt_to_lt_plus: ∀n,m,l. n < m → n < m + l.
77 naxiom S_plus: ∀n,m. S (n + m) = n + S m.
78 naxiom big_plus_ext: ∀n,f,f'. (∀i,p. f i p = f' i p) → big_plus n f = big_plus n f'.
79 naxiom ad_hoc1: ∀n,m,l. n + (m + l) = l + (n + m).
80 naxiom assoc: ∀n,m,l. n + m + l = n + (m + l).
81 naxiom lt_canc: ∀n,m,p. n < m → p + n < p + m.
82 naxiom ad_hoc2: ∀a,b. a < b → b - a - (b - S a) = S O.
83 naxiom ad_hoc3: ∀a,b. b < a → S (O + (a - S b) + b) = a.
84 naxiom ad_hoc4: ∀a,b. a - S b ≤ a - b.
85 naxiom ad_hoc5: ∀a. S a - a = S O.
86 naxiom ad_hoc6: ∀a,b. b ≤ a → a - b + b = a.
87 naxiom ad_hoc7: ∀a,b,c. a + (b + O) + c - b = a + c.
88 naxiom ad_hoc8: ∀a,b,c. ¬ (a + (b + O) + c < b).
89 naxiom ltb_elim_CProp0: ∀n,m. ∀P: bool → CProp[0]. (n < m → P true) → (¬ (n < m) → P false) → P (ltb n m).
90 naxiom ltb_cases: ∀n,m. (n < m ∧ ltb n m = true) ∨ (¬ (n < m) ∧ ltb n m = false).
91 naxiom ad_hoc9: ∀a,b,c. a ≤ b + c → a - b ≤ c.
92 naxiom not_lt_to_le: ∀a,b. ¬ (a < b) → b ≤ a.
95 naxiom split_big_plus:
97 big_plus n f = big_plus m (λi,p.f i ?) + big_plus (n - m) (λi.λp.f (i + m) ?).
101 nlemma partition_splits_card_output:
102 ∀A. ∀P:partition A. ∀n,s.
103 ∀f:isomorphism ?? (Nat_ (S n)) (indexes ? P).
104 ∀fi:∀i. isomorphism ?? (Nat_ (s i)) (class ? P (iso_f ???? f i)).
105 ∀x. x ∈ Nat_ (big_plus (S n) (λi.λ_.s i)) →
106 ∃i1.∃i2. partition_splits_card_map A P (S n) s f fi x n = iso_f ???? (fi i1) i2.
107 #A; #P; #n; #s; #f; #fi;
108 nelim n in ⊢ (? → % → ??(λ_.??(λ_.???(????????%)?)))
109 [ #x; nnormalize in ⊢ (% → ?); nrewrite > (plus_n_O (s O)); nchange in ⊢ (% → ?) with (x < s O);
110 #H; napply (ex_intro … O); napply (ex_intro … x); nwhd in ⊢ (???%?);
111 nrewrite > (ltb_t … H); nwhd in ⊢ (???%?); napply refl
112 | #n'; #Hrec; #x; #Hx; nwhd in ⊢ (??(λ_.??(λ_.???%?))); nwhd in Hx; nwhd in Hx: (??%);
113 nelim (ltb_cases x (s (S n'))); *; #K1; #K2; nrewrite > K2; nwhd in ⊢ (??(λ_.??(λ_.???%?)))
114 [ napply (ex_intro … (S n')); napply (ex_intro … x); napply refl
115 | napply (Hrec (x - s (S n')) ?); nwhd; nrewrite < (minus_S x (s (S n')) ?)
116 [ napply ad_hoc9; nassumption | napply not_lt_to_le; nassumption ]##]
119 nlemma partition_splits_card:
120 ∀A. ∀P:partition A. ∀n,s.
121 ∀f:isomorphism ?? (Nat_ n) (indexes ? P).
122 (∀i. isomorphism ?? (Nat_ (s i)) (class ? P (iso_f ???? f i))) →
123 (isomorphism ?? (Nat_ (big_plus n (λi.λ_.s i))) (Full_set A)).
124 #A; #P; #Sn; ncases Sn
126 ngeneralize in match (covers ? P) in ⊢ ?; *; #_; #H;
128 (big_union_preserves_iso ??? (indexes A P) (Nat_ O) (λx.class ? P x) f) in ⊢ ?;
129 *; #K; #_; nwhd in K: (? → ? → %);
130 nelim daemon (* impossibile *)
131 | #n; #s; #f; #fi; napply mk_isomorphism
132 [ napply mk_unary_morphism
133 [ napply (λm.partition_splits_card_map A P (S n) s f fi m n)
134 | #a; #a'; #H; nrewrite < H; napply refl ]
136 ngeneralize in match (covers ? P) in ⊢ ?; *; #_; #Hc;
137 ngeneralize in match (Hc y I) in ⊢ ?; *; #index; *; #Hi1; #Hi2;
138 ngeneralize in match (f_sur ???? f ? Hi1) in ⊢ ?; *; #nindex; *; #Hni1; #Hni2;
139 ngeneralize in match (f_sur ???? (fi nindex) y ?) in ⊢ ?
140 [##2: napply (. #‡(†?));##[##3: napply Hni2 |##2: ##skip | nassumption]##]
141 *; #nindex2; *; #Hni21; #Hni22;
142 nletin xxx ≝ (plus (big_plus (minus n nindex) (λi.λ_.s (S (plus i nindex)))) nindex2);
143 napply (ex_intro … xxx); napply conj
144 [ nwhd in Hni1; nwhd; nwhd in ⊢ (?(? %)%);
145 nchange with (? < plus (s n) (big_plus n ?));
146 nelim (le_to_lt_or_eq … (le_S_S_to_le … Hni1))
147 [##2: #E; nrewrite < E; nrewrite < (minus_canc nindex);
148 nwhd in ⊢ (?%?); nrewrite < E; napply lt_to_lt_plus; nassumption
149 | #L; nrewrite > (split_big_plus n (S nindex) (λm.λ_.s m) L);
150 nrewrite > (split_big_plus (n - nindex) (n - S nindex) (λi.λ_.s (S (i+nindex))) ?)
151 [ ngeneralize in match (big_plus_ext (n - S nindex)
152 (λi,p.s (S (i+nindex))) (λi,p.s (i + S nindex)) ?) in ⊢ ?
154 napply (eq_rect_CProp0_r ??
155 (λx:nat.λ_. x + big_plus (n - nindex - (n - S nindex))
156 (λi,p.s (S (i + (n - S nindex)+nindex))) + nindex2 <
157 s n + (big_plus (S nindex) (λi,p.s i) +
158 big_plus (n - S nindex) (λi,p. s (i + S nindex)))) ? ? E);
159 nrewrite > (ad_hoc1 (s n) (big_plus (S nindex) (λi,p.s i))
160 (big_plus (n - S nindex) (λi,p. s (i + S nindex))));
161 napply (eq_rect_CProp0_r
162 ?? (λx.λ_.x < ?) ?? (assoc
163 (big_plus (n - S nindex) (λi,p.s (i + S nindex)))
164 (big_plus (n - nindex - (n - S nindex))
165 (λi,p.s (S (i + (n - S nindex)+nindex))))
168 nrewrite > (ad_hoc2 … L); nwhd in ⊢ (?(?%?)?);
169 nrewrite > (ad_hoc3 … L);
170 napply (eq_rect_CProp0_r ?? (λx.λ_.x < ?) ?? (assoc …));
171 napply lt_canc; nnormalize in ⊢ (?%?); nwhd in ⊢ (??%);
172 napply lt_to_lt_plus; nassumption
173 ##|##2: #i; #_; nrewrite > (S_plus i nindex); napply refl]
174 ##| napply ad_hoc4]##]
175 ##| nwhd in ⊢ (???%?);
176 nchange in Hni1 with (nindex < S n);
177 ngeneralize in match (le_S_S_to_le … Hni1) in ⊢ ?;
178 nwhd in ⊢ (? → ???(???????%?)?);
179 napply (nat_rect_CProp0
181 eq_rel (carr A) (eq A)
182 (partition_splits_card_map A P (S n) s f fi
184 (big_plus (minus x nindex) (λi.λ_:i < minus x nindex.s (S (plus i nindex))))
186 [ #K; nrewrite < (minus_O_n nindex); nwhd in ⊢ (???(???????%?)?);
187 nwhd in ⊢ (???%?); nchange in Hni21 with (nindex2 < s nindex);
188 ngeneralize in match (le_O_to_eq … K) in ⊢ ?; #K';
189 ngeneralize in match Hni21 in ⊢ ?;
190 ngeneralize in match Hni22 in ⊢ ?;
191 nrewrite > K' in ⊢ (% → % → ?); #K1; #K2;
192 nrewrite > (ltb_t … K2);
193 nwhd in ⊢ (???%?); nassumption
194 | #n'; #Hrec; #HH; nelim (le_to_lt_or_eq … HH)
195 [##2: #K; nrewrite < K; nrewrite < (minus_canc nindex);
196 nwhd in ⊢ (???(???????%?)?);
198 nwhd in ⊢ (???%?); nrewrite < K; nrewrite > (ltb_t … Hni21);
199 nwhd in ⊢ (???%?); nassumption
200 ##| #K; ngeneralize in match (le_S_S_to_le … K) in ⊢ ?; #K';
202 ngeneralize in match (?:
203 ¬ (big_plus (S n' - nindex) (λi,p.s (S (i+nindex))) + nindex2 < s (S n'))) in ⊢ ?
204 [ #N; nrewrite > (ltb_f … N); nwhd in ⊢ (???%?);
205 ngeneralize in match (Hrec K') in ⊢ ?; #Hrec';
206 napply (eq_rect_CProp0_r ??
207 (λx,p. eq_rel (carr A) (eq A) (partition_splits_card_map A P (S n) s f fi
208 (big_plus x ? + ? - ?) n') y) ?? (minus_S n' nindex K'));
209 nrewrite > (split_big_plus (S (n' - nindex)) (n' - nindex)
210 (λi,p.s (S (i+nindex))) (le_S ?? (le_n ?)));
211 nrewrite > (ad_hoc5 (n' - nindex));
212 nnormalize in ⊢ (???(???????(?(?(??%)?)?)?)?);
213 nrewrite > (ad_hoc6 … K');
214 nrewrite > (ad_hoc7 (big_plus (n' - nindex) (λi,p.s (S (i+nindex))))
217 | nrewrite > (minus_S … K');
218 nrewrite > (split_big_plus (S (n' - nindex)) (n' - nindex)
219 (λi,p.s (S (i+nindex))) (le_S ?? (le_n ?)));
220 nrewrite > (ad_hoc5 (n' - nindex));
221 nnormalize in ⊢ (?(?(?(??%)?)?));
222 nrewrite > (ad_hoc6 … K');
223 napply ad_hoc8]##]##]##]
224 ##| #x; #x'; nnormalize in ⊢ (? → ? → %); #Hx; #Hx';
225 nelim (partition_splits_card_output A P n s f fi x Hx); #i1x; *; #i2x; #Ex;
226 nelim (partition_splits_card_output A P n s f fi x' Hx'); #i1x'; *; #i2x'; #Ex';
227 ngeneralize in match (? :
228 iso_f ???? fi i1x(* ≬ iso_f ???? (fi i1x'))*)) in ⊢ ?;
229 #E; napply (f_inj ???? (fi i1x));
231 nelim n in ⊢ (% → % → (???(????????%)(????????%)) → ?)
232 [ nnormalize in ⊢ (% → % → ?); nrewrite > (plus_n_O (s O));
233 nchange in ⊢ (% → ?) with (x < s O);
234 nchange in ⊢ (? → % → ?) with (x' < s O);
235 #H1; #H2; nwhd in ⊢ (???%% → ?);
236 nrewrite > (ltb_t … H1); nrewrite > (ltb_t … H2); nwhd in ⊢ (???%% → ?);
237 napply f_inj; nassumption
238 | #n'; #Hrec; #Hx; #Hx'; nwhd in ⊢ (???%% → ?);
242 (************** equivalence relations vs partitions **********************)
244 ndefinition partition_of_compatible_equivalence_relation:
245 ∀A:setoid. compatible_equivalence_relation A → partition A.
246 #A; #R; napply mk_partition
247 [ napply (quotient ? R)
249 | napply mk_unary_morphism1
250 [ #a; napply mk_qpowerclass
252 | #x; #x'; #H; nnormalize; napply mk_iff; #K; nelim daemon]
253 ##| #a; #a'; #H; napply conj; #x; nnormalize; #K [ nelim daemon | nelim daemon]##]
254 ##| #x; #_; nnormalize; napply (ex_intro … x); napply conj; napply refl
255 | #x; #x'; #_; #_; nnormalize; *; #x''; *; #H1; #H2; napply (trans ?????? H2);
256 napply sym; nassumption
257 | nnormalize; napply conj
258 [ #a; #_; napply I | #a; #_; napply (ex_intro … a); napply conj [ napply I | napply refl]##]