]> matita.cs.unibo.it Git - helm.git/blob - helm/software/matita/nlibrary/sets/partitions.ma
d87ccd3a9c5e6a1b20aa224d5708d7e936aa69b8
[helm.git] / helm / software / matita / nlibrary / sets / partitions.ma
1 (**************************************************************************)
2 (*       ___                                                              *)
3 (*      ||M||                                                             *)
4 (*      ||A||       A project by Andrea Asperti                           *)
5 (*      ||T||                                                             *)
6 (*      ||I||       Developers:                                           *)
7 (*      ||T||         The HELM team.                                      *)
8 (*      ||A||         http://helm.cs.unibo.it                             *)
9 (*      \   /                                                             *)
10 (*       \ /        This file is distributed under the terms of the       *)
11 (*        v         GNU General Public License Version 2                  *)
12 (*                                                                        *)
13 (**************************************************************************)
14
15 include "sets/sets.ma".
16 include "nat/plus.ma".
17 include "nat/compare.ma".
18 include "nat/minus.ma".
19 include "datatypes/pairs.ma".
20
21 alias symbol "eq" = "setoid eq".
22 alias symbol "eq" = "setoid1 eq".
23 alias symbol "eq" = "setoid eq".
24 alias symbol "eq" = "setoid1 eq".
25 alias symbol "eq" = "setoid eq".
26 alias symbol "eq" = "setoid1 eq".
27 alias symbol "eq" = "setoid eq".
28 alias symbol "eq" = "setoid1 eq".
29 alias symbol "eq" = "setoid eq".
30 alias symbol "eq" = "setoid1 eq".
31 alias symbol "eq" = "setoid eq".
32 alias symbol "eq" = "setoid1 eq".
33 alias symbol "eq" = "setoid eq".
34 nrecord partition (A: setoid) : Type[1] ≝ 
35  { support: setoid;
36    indexes: qpowerclass support;
37    class: unary_morphism1 (setoid1_of_setoid support) (qpowerclass_setoid A);
38    inhabited: ∀i. i ∈ indexes → class i ≬ class i;
39    disjoint: ∀i,j. i ∈ indexes → j ∈ indexes → class i ≬ class j → i=j;
40    covers: big_union support ? ? (λx.class x) = full_set A
41  }. napply indexes; nqed.
42
43 naxiom daemon: False.
44
45 nlet rec iso_nat_nat_union (s: nat → nat) m index on index : pair nat nat ≝
46  match ltb m (s index) with
47   [ true ⇒ mk_pair … index m
48   | false ⇒
49      match index with
50       [ O ⇒ (* dummy value: it could be an elim False: *) mk_pair … O O
51       | S index' ⇒ iso_nat_nat_union s (minus m (s index)) index']].
52
53 alias symbol "eq" = "leibnitz's equality".
54 naxiom plus_n_O: ∀n. plus n O = n.
55 naxiom ltb_t: ∀n,m. n < m → ltb n m = true.
56 naxiom ltb_f: ∀n,m. ¬ (n < m) → ltb n m = false.
57 naxiom ltb_cases: ∀n,m. (n < m ∧ ltb n m = true) ∨ (¬ (n < m) ∧ ltb n m = false).
58 naxiom minus_canc: ∀n. minus n n = O.
59 naxiom ad_hoc9: ∀a,b,c. a < b + c → a - b < c.
60 naxiom ad_hoc10: ∀a,b,c. a - b = c → a = b + c.
61 naxiom ad_hoc11: ∀a,b. a - b ≤ S a - b.
62 naxiom ad_hoc12: ∀a,b. b ≤ a → S a - b - (a - b) = S O.
63 naxiom ad_hoc13: ∀a,b. b ≤ a → (O + (a - b)) + b = a.
64 naxiom ad_hoc14: ∀a,b,c,d,e. c ≤ a → a - c = b + d + e → a = b + (c + d) + e.
65 naxiom not_lt_to_le: ∀a,b. ¬ (a < b) → b ≤ a.
66 naxiom split_big_plus:
67   ∀n,m,f. m ≤ n →
68    big_plus n f = big_plus m (λi,p.f i ?) + big_plus (n - m) (λi.λp.f (i + m) ?).
69  nelim daemon.
70 nqed.
71
72 ntheorem iso_nat_nat_union_char:
73  ∀n:nat. ∀s: nat → nat. ∀m:nat. m < big_plus (S n) (λi.λ_.s i) →
74   let p ≝ iso_nat_nat_union s m n in
75    m = big_plus (n - fst … p) (λi.λ_.s (S (i + fst … p))) + snd … p ∧
76     fst … p ≤ n ∧ snd … p < s (fst … p).
77  #n; #s; nelim n
78   [ #m; nwhd in ⊢ (??% → let p ≝ % in ?); nwhd in ⊢ (??(??%) → ?);
79     nrewrite > (plus_n_O (s O)); #H; nrewrite > (ltb_t … H); nnormalize;
80     napply conj [ napply conj [ napply refl | napply le_n ] ##| nassumption ]
81 ##| #n'; #Hrec; #m; nwhd in ⊢ (??% → let p ≝ % in ?); #H;
82     ncases (ltb_cases m (s (S n'))); *; #H1; #H2; nrewrite > H2;
83     nwhd in ⊢ (let p ≝ % in ?); nwhd
84      [ napply conj [napply conj
85         [ nwhd in ⊢ (????(?(?%(λ_.λ_:(??%).?))%)); nrewrite > (minus_canc n'); napply refl
86         | nnormalize; napply le_n]
87       ##| nnormalize; nassumption ]
88    ##| nchange in H with (m < s (S n') + big_plus (S n') (λi.λ_.s i));
89        ngeneralize in match (Hrec (m - s (S n')) ?) in ⊢ ?
90         [##2: napply ad_hoc9; nassumption] *; *; #Hrec1; #Hrec2; #Hrec3; napply conj
91         [##2: nassumption
92         |napply conj
93          [napply (eq_rect_CProp0_r ?? (λx.λ_. m = x + snd … (iso_nat_nat_union s (m - s (S n')) n')) ??
94            (split_big_plus
95             (S n' - fst … (iso_nat_nat_union s (m - s (S n')) n'))
96             (n' - fst … (iso_nat_nat_union s (m - s (S n')) n'))
97             (λi.λ_.s (S (i + fst … (iso_nat_nat_union s (m - s (S n')) n'))))?))
98            [##2: napply ad_hoc11]
99           napply (eq_rect_CProp0_r ?? (λx.λ_. ? = ? + big_plus x (λ_.λ_:? < x.?) + ?)
100            ?? (ad_hoc12 n' (fst … (iso_nat_nat_union s (m - s (S n')) n')) ?))
101             [##2: nassumption]
102           nwhd in ⊢ (???(?(??%)?));
103           nrewrite > (ad_hoc13 n' (fst … (iso_nat_nat_union s (m - s (S n')) n')) ?)
104            [##2: nassumption]
105           napply ad_hoc14 [ napply not_lt_to_le; nassumption ]
106           nwhd in ⊢ (???(?(??%)?));
107           napply (eq_rect_CProp0_r ?? (λx.λ_. ? = x + ?) ??
108            (plus_n_O (big_plus (n' - fst … (iso_nat_nat_union s (m - s (S n')) n'))
109             (λi.λ_.s (S (i + fst … (iso_nat_nat_union s (m - s (S n')) n')))))));
110           nassumption
111         | napply le_S; nassumption ]##]##]##]
112 nqed.
113
114
115 nlet rec partition_splits_card_map
116  A (P:partition A) n s (f:isomorphism ?? (Nat_ n) (indexes ? P))
117  (fi: ∀i. isomorphism ?? (Nat_ (s i)) (class ? P (iso_f ???? f i))) m index
118  on index : A ≝
119  match ltb m (s index) with
120   [ true ⇒ iso_f ???? (fi index) m
121   | false ⇒
122      match index with
123       [ O ⇒ (* dummy value: it could be an elim False: *) iso_f ???? (fi O) O
124       | S index' ⇒
125          partition_splits_card_map A P n s f fi (minus m (s index)) index']].  
126
127 naxiom big_union_preserves_iso:
128  ∀A,A',B,T,T',f.
129   ∀g: isomorphism A' A T' T.
130    big_union A B T f = big_union A' B T' (λx.f (iso_f ???? g x)).
131
132 naxiom le_to_lt_or_eq: ∀n,m. n ≤ m → n < m ∨ n = m.
133 alias symbol "eq" = "leibnitz's equality".
134 naxiom lt_to_ltb_t: ∀n,m. ∀P: bool → CProp[0]. P true → n < m → P (ltb n m).
135 naxiom lt_to_ltb_f: ∀n,m. ∀P: bool → CProp[0]. P false → ¬ (n < m) → P (ltb n m).
136 naxiom lt_to_minus: ∀n,m. n < m →  S (minus (minus m n) (S O)) = minus m n.
137 naxiom not_lt_O: ∀n. ¬ (n < O).
138 naxiom minus_S: ∀n,m. m ≤ n → minus (S n) m = S (minus n m).
139 naxiom minus_lt_to_lt: ∀n,m,p. n < p → minus n m < p.
140 naxiom minus_O_n: ∀n. O = minus O n.
141 naxiom le_O_to_eq: ∀n. n ≤ O → n=O.
142 naxiom lt_to_minus_to_S: ∀n,m. m < n → ∃k. minus n m = S k.
143 naxiom not_lt_plus: ∀n,m. ¬ (plus n m < n).
144 naxiom lt_to_lt_plus: ∀n,m,l. n < m → n < m + l.
145 naxiom S_plus: ∀n,m. S (n + m) = n + S m.
146 naxiom big_plus_ext: ∀n,f,f'. (∀i,p. f i p = f' i p) → big_plus n f = big_plus n f'.
147 naxiom ad_hoc1: ∀n,m,l. n + (m + l) = l + (n + m).
148 naxiom assoc: ∀n,m,l. n + m + l = n + (m + l).
149 naxiom lt_canc: ∀n,m,p. n < m → p + n < p + m.
150 naxiom ad_hoc2: ∀a,b. a < b → b - a - (b - S a) = S O.
151 naxiom ad_hoc3: ∀a,b. b < a → S (O + (a - S b) + b) = a.
152 naxiom ad_hoc4: ∀a,b. a - S b ≤ a - b.
153 naxiom ad_hoc5: ∀a. S a - a = S O.
154 naxiom ad_hoc6: ∀a,b. b ≤ a → a - b + b = a.
155 naxiom ad_hoc7: ∀a,b,c. a + (b + O) + c - b = a + c.
156 naxiom ad_hoc8: ∀a,b,c. ¬ (a + (b + O) + c < b).
157 naxiom ltb_elim_CProp0: ∀n,m. ∀P: bool → CProp[0]. (n < m → P true) → (¬ (n < m) → P false) → P (ltb n m).               
158
159 nlemma partition_splits_card_output:
160  ∀A. ∀P:partition A. ∀n,s.
161   ∀f:isomorphism ?? (Nat_ (S n)) (indexes ? P).
162    ∀fi:∀i. isomorphism ?? (Nat_ (s i)) (class ? P (iso_f ???? f i)).
163     ∀x. x ∈ Nat_ (big_plus (S n) (λi.λ_.s i)) →
164      ∃i1.∃i2. partition_splits_card_map A P (S n) s f fi x n = iso_f ???? (fi i1) i2.
165  #A; #P; #n; #s; #f; #fi;
166  nelim n in ⊢ (? → % → ??(λ_.??(λ_.???(????????%)?)))
167   [ #x; nnormalize in ⊢ (% → ?); nrewrite > (plus_n_O (s O)); nchange in ⊢ (% → ?) with (x < s O);
168     #H; napply (ex_intro … O); napply (ex_intro … x); nwhd in ⊢ (???%?);
169     nrewrite > (ltb_t … H); nwhd in ⊢ (???%?); napply refl
170   | #n'; #Hrec; #x; #Hx; nwhd in ⊢ (??(λ_.??(λ_.???%?))); nwhd in Hx; nwhd in Hx: (??%);
171     nelim (ltb_cases x (s (S n'))); *; #K1; #K2; nrewrite > K2; nwhd in ⊢ (??(λ_.??(λ_.???%?)))
172      [ napply (ex_intro … (S n')); napply (ex_intro … x); napply refl
173      | napply (Hrec (x - s (S n')) ?); nwhd; nrewrite < (minus_S x (s (S n')) ?)
174        [ napply ad_hoc9; nassumption | napply not_lt_to_le; nassumption ]##]
175 nqed.
176
177 nlemma partition_splits_card:
178  ∀A. ∀P:partition A. ∀n,s.
179   ∀f:isomorphism ?? (Nat_ n) (indexes ? P).
180    (∀i. isomorphism ?? (Nat_ (s i)) (class ? P (iso_f ???? f i))) →
181     (isomorphism ?? (Nat_ (big_plus n (λi.λ_.s i))) (Full_set A)).
182  #A; #P; #Sn; ncases Sn
183   [ #s; #f; #fi;
184     ngeneralize in match (covers ? P) in ⊢ ?; *; #_; #H;
185     ngeneralize in match
186      (big_union_preserves_iso ??? (indexes A P) (Nat_ O) (λx.class ? P x) f) in ⊢ ?;
187      *; #K; #_; nwhd in K: (? → ? → %);
188     nelim daemon (* impossibile *)
189   | #n; #s; #f; #fi; napply mk_isomorphism
190   [ napply mk_unary_morphism
191      [ napply (λm.partition_splits_card_map A P (S n) s f fi m n)
192      | #a; #a'; #H; nrewrite < H; napply refl ]
193 ##| #y; #_;
194     ngeneralize in match (covers ? P) in ⊢ ?; *; #_; #Hc;
195     ngeneralize in match (Hc y I) in ⊢ ?; *; #index; *; #Hi1; #Hi2;
196     ngeneralize in match (f_sur ???? f ? Hi1) in ⊢ ?; *; #nindex; *; #Hni1; #Hni2;
197     ngeneralize in match (f_sur ???? (fi nindex) y ?) in ⊢ ?
198      [##2: napply (. #‡(†?));##[##3: napply Hni2 |##2: ##skip | nassumption]##]
199     *; #nindex2; *; #Hni21; #Hni22;
200     nletin xxx ≝ (plus (big_plus (minus n nindex) (λi.λ_.s (S (plus i nindex)))) nindex2);
201     napply (ex_intro … xxx); napply conj
202      [ nwhd in Hni1; nwhd; nwhd in ⊢ (?(? %)%);
203        nchange with (? < plus (s n) (big_plus n ?));
204        nelim (le_to_lt_or_eq … (le_S_S_to_le … Hni1))
205         [##2: #E; nrewrite < E; nrewrite < (minus_canc nindex);
206           nwhd in ⊢ (?%?); nrewrite < E; napply lt_to_lt_plus; nassumption
207         | #L; nrewrite > (split_big_plus n (S nindex) (λm.λ_.s m) L);
208           nrewrite > (split_big_plus (n - nindex) (n - S nindex) (λi.λ_.s (S (i+nindex))) ?)
209            [ ngeneralize in match (big_plus_ext (n - S nindex)
210               (λi,p.s (S (i+nindex))) (λi,p.s (i + S nindex)) ?) in ⊢ ?
211                [ #E;
212                  napply (eq_rect_CProp0_r ??
213                   (λx:nat.λ_. x + big_plus (n - nindex - (n - S nindex))
214                    (λi,p.s (S (i + (n - S nindex)+nindex))) + nindex2 <
215                    s n + (big_plus (S nindex) (λi,p.s i) +
216                     big_plus (n - S nindex) (λi,p. s (i + S nindex)))) ? ? E);
217                  nrewrite > (ad_hoc1 (s n) (big_plus (S nindex) (λi,p.s i))
218                   (big_plus (n - S nindex) (λi,p. s (i + S nindex))));
219                  napply (eq_rect_CProp0_r
220                   ?? (λx.λ_.x < ?) ?? (assoc
221                   (big_plus (n - S nindex) (λi,p.s (i + S nindex)))
222                   (big_plus (n - nindex - (n - S nindex))
223                    (λi,p.s (S (i + (n - S nindex)+nindex))))
224                   nindex2));
225                  napply lt_canc;
226                  nrewrite > (ad_hoc2 … L); nwhd in ⊢ (?(?%?)?);
227                  nrewrite > (ad_hoc3 … L);
228                  napply (eq_rect_CProp0_r ?? (λx.λ_.x < ?) ?? (assoc …));
229                  napply lt_canc; nnormalize in ⊢ (?%?); nwhd in ⊢ (??%);
230                  napply lt_to_lt_plus; nassumption
231              ##|##2: #i; #_; nrewrite > (S_plus i nindex); napply refl]
232          ##| napply ad_hoc4]##]
233    ##| nwhd in ⊢ (???%?);
234        nchange in Hni1 with (nindex < S n);
235        ngeneralize in match (le_S_S_to_le … Hni1) in ⊢ ?;
236        nwhd in ⊢ (? → ???(???????%?)?);
237        napply (nat_rect_CProp0
238         (λx. nindex ≤ x →
239           eq_rel (carr A) (eq A)
240           (partition_splits_card_map A P (S n) s f fi
241            (plus
242              (big_plus (minus x nindex) (λi.λ_:i < minus x nindex.s (S (plus i nindex))))
243              nindex2) x) y) ?? n)
244         [ #K; nrewrite < (minus_O_n nindex); nwhd in ⊢ (???(???????%?)?);
245           nwhd in ⊢ (???%?); nchange in Hni21 with (nindex2 < s nindex);
246           ngeneralize in match (le_O_to_eq … K) in ⊢ ?; #K';
247           ngeneralize in match Hni21 in ⊢ ?;
248           ngeneralize in match Hni22 in ⊢ ?;
249           nrewrite > K' in ⊢ (% → % → ?); #K1; #K2;
250           nrewrite > (ltb_t … K2);
251           nwhd in ⊢ (???%?); nassumption
252         | #n'; #Hrec; #HH; nelim (le_to_lt_or_eq … HH)
253            [##2: #K; nrewrite < K; nrewrite < (minus_canc nindex);
254             nwhd in ⊢ (???(???????%?)?);
255             nrewrite > K;
256             nwhd in ⊢ (???%?); nrewrite < K; nrewrite > (ltb_t … Hni21);
257             nwhd in ⊢ (???%?); nassumption
258          ##| #K; ngeneralize in match (le_S_S_to_le … K) in ⊢ ?; #K';
259              nwhd in ⊢ (???%?);
260              ngeneralize in match (?:
261               ¬ (big_plus (S n' - nindex) (λi,p.s (S (i+nindex))) + nindex2 < s (S n'))) in ⊢ ?
262               [ #N; nrewrite > (ltb_f … N); nwhd in ⊢ (???%?);
263                 ngeneralize in match (Hrec K') in ⊢ ?; #Hrec';
264                 napply (eq_rect_CProp0_r ??
265                  (λx,p. eq_rel (carr A) (eq A) (partition_splits_card_map A P (S n) s f fi
266                   (big_plus x ? + ? - ?) n') y) ?? (minus_S n' nindex K'));
267                 nrewrite > (split_big_plus (S (n' - nindex)) (n' - nindex)
268                  (λi,p.s (S (i+nindex))) (le_S ?? (le_n ?)));
269                 nrewrite > (ad_hoc5 (n' - nindex));
270                 nnormalize in ⊢ (???(???????(?(?(??%)?)?)?)?);
271                 nrewrite > (ad_hoc6 … K');
272                 nrewrite > (ad_hoc7 (big_plus (n' - nindex) (λi,p.s (S (i+nindex))))
273                  (s (S n')) nindex2);
274                 nassumption
275               | nrewrite > (minus_S … K');
276                 nrewrite > (split_big_plus (S (n' - nindex)) (n' - nindex)
277                  (λi,p.s (S (i+nindex))) (le_S ?? (le_n ?)));
278                 nrewrite > (ad_hoc5 (n' - nindex));
279                 nnormalize in ⊢ (?(?(?(??%)?)?));
280                 nrewrite > (ad_hoc6 … K');
281                 napply ad_hoc8]##]##]##]
282 ##| #x; #x'; nnormalize in ⊢ (? → ? → %); #Hx; #Hx';
283     nelim (partition_splits_card_output A P n s f fi x Hx); #i1x; *; #i2x; #Ex;
284     nelim (partition_splits_card_output A P n s f fi x' Hx'); #i1x'; *; #i2x'; #Ex';
285     ngeneralize in match (? :
286      iso_f ???? fi i1x(* ≬ iso_f ???? (fi i1x'))*)) in ⊢ ?;
287     #E; napply (f_inj ???? (fi i1x));
288     
289     nelim n in ⊢ (% → % → (???(????????%)(????????%)) → ?)
290      [ nnormalize in ⊢ (% → % → ?); nrewrite > (plus_n_O (s O));
291        nchange in ⊢ (% → ?) with (x < s O);
292        nchange in ⊢ (? → % → ?) with (x' < s O);
293        #H1; #H2; nwhd in ⊢ (???%% → ?);
294        nrewrite > (ltb_t … H1); nrewrite > (ltb_t … H2); nwhd in ⊢ (???%% → ?);
295        napply f_inj; nassumption
296      | #n'; #Hrec; #Hx; #Hx'; nwhd in ⊢ (???%% → ?);
297   ]
298 nqed.
299
300 (************** equivalence relations vs partitions **********************)
301
302 ndefinition partition_of_compatible_equivalence_relation:
303  ∀A:setoid. compatible_equivalence_relation A → partition A.
304  #A; #R; napply mk_partition
305   [ napply (quotient ? R)
306   | napply Full_set
307   | napply mk_unary_morphism1
308      [ #a; napply mk_qpowerclass
309         [ napply {x | R x a}
310         | #x; #x'; #H; nnormalize; napply mk_iff; #K; nelim daemon]
311    ##| #a; #a'; #H; napply conj; #x; nnormalize; #K [ nelim daemon | nelim daemon]##]
312 ##| #x; #_; nnormalize; napply (ex_intro … x); napply conj; napply refl
313   | #x; #x'; #_; #_; nnormalize; *; #x''; *; #H1; #H2; napply (trans ?????? H2);
314     napply sym; nassumption
315   | nnormalize; napply conj
316      [ #a; #_; napply I | #a; #_; napply (ex_intro … a); napply conj [ napply I | napply refl]##]
317 nqed.