]> matita.cs.unibo.it Git - helm.git/blob - helm/software/matita/nlibrary/sets/partitions.ma
- Bug fixed in definition of big_op.
[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
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 nrecord partition (A: setoid) : Type[1] ≝ 
29  { support: setoid;
30    indexes: qpowerclass support;
31    class: unary_morphism1 (setoid1_of_setoid support) (qpowerclass_setoid A);
32    inhabited: ∀i. i ∈ indexes → class i ≬ class i;
33    disjoint: ∀i,j. i ∈ indexes → j ∈ indexes → class i ≬ class j → i=j;
34    covers: big_union support ? ? (λx.class x) = full_set A
35  }. napply indexes; nqed.
36
37 naxiom daemon: False.
38
39 nlet rec partition_splits_card_map
40  A (P:partition A) n s (f:isomorphism ?? (Nat_ n) (indexes ? P))
41  (fi: ∀i. isomorphism ?? (Nat_ (s i)) (class ? P (iso_f ???? f i))) m index
42  on index : A ≝
43  match ltb m (s index) with
44   [ true ⇒ iso_f ???? (fi index) m
45   | false ⇒
46      match index with
47       [ O ⇒ (* dummy value: it could be an elim False: *) iso_f ???? (fi O) O
48       | S index' ⇒
49          partition_splits_card_map A P n s f fi (minus m (s index)) index']].  
50
51 naxiom big_union_preserves_iso:
52  ∀A,A',B,T,T',f.
53   ∀g: isomorphism A' A T' T.
54    big_union A B T f = big_union A' B T' (λx.f (iso_f ???? g x)).
55
56 naxiom le_to_lt_or_eq: ∀n,m. n ≤ m → n < m ∨ n = m.
57 alias symbol "eq" = "leibnitz's equality".
58 naxiom minus_canc: ∀n. O = minus n n.
59 naxiom lt_to_ltb_t: ∀n,m. ∀P: bool → CProp[0]. P true → n < m → P (ltb n m).
60 naxiom lt_to_ltb_f: ∀n,m. ∀P: bool → CProp[0]. P false → ¬ (n < m) → P (ltb n m).
61 naxiom lt_to_minus: ∀n,m. n < m →  S (minus (minus m n) (S O)) = minus m n.
62 naxiom not_lt_O: ∀n. ¬ (n < O).
63 naxiom minus_S: ∀n,m. m ≤ n → minus (S n) m = S (minus n m).
64 naxiom minus_lt_to_lt: ∀n,m,p. n < p → minus n m < p.
65 naxiom minus_O_n: ∀n. O = minus O n.
66 naxiom le_O_to_eq: ∀n. n ≤ O → n=O.
67 naxiom lt_to_minus_to_S: ∀n,m. m < n → ∃k. minus n m = S k.
68 naxiom ltb_t: ∀n,m. n < m → ltb n m = true. 
69
70 nlemma partition_splits_card:
71  ∀A. ∀P:partition A. ∀n,s.
72   ∀f:isomorphism ?? (Nat_ n) (indexes ? P).
73    (∀i. isomorphism ?? (Nat_ (s i)) (class ? P (iso_f ???? f i))) →
74     (isomorphism ?? (Nat_ (big_plus n (λi.λ_.s i))) (Full_set A)).
75  #A; #P; #Sn; ncases Sn
76   [ #s; #f; #fi;
77     ngeneralize in match (covers ? P) in ⊢ ?; *; #_; #H;
78     ngeneralize in match
79      (big_union_preserves_iso ??? (indexes A P) (Nat_ O) (λx.class ? P x) f) in ⊢ ?;
80      *; #K; #_; nwhd in K: (? → ? → %);
81     nelim daemon (* impossibile *)
82   | #n; #s; #f; #fi; napply mk_isomorphism
83   [ napply mk_unary_morphism
84      [ napply (λm.partition_splits_card_map A P (S n) s f fi m n)
85      | #a; #a'; #H; nrewrite < H; napply refl ]
86 ##| #y; #_;
87     ngeneralize in match (covers ? P) in ⊢ ?; *; #_; #Hc;
88     ngeneralize in match (Hc y I) in ⊢ ?; *; #index; *; #Hi1; #Hi2;
89     ngeneralize in match (f_sur ???? f ? Hi1) in ⊢ ?; *; #nindex; *; #Hni1; #Hni2;
90     ngeneralize in match (f_sur ???? (fi nindex) y ?) in ⊢ ?
91      [##2: napply (. #‡(†?));##[##3: napply Hni2 |##2: ##skip | nassumption]##]
92     *; #nindex2; *; #Hni21; #Hni22;
93     nletin xxx ≝ (plus match minus n nindex return λ_.nat with [ O ⇒ O | S nn ⇒ big_plus nn (λi.λ_.s (S (plus i nindex)))] nindex2);
94     napply (ex_intro … xxx); napply conj
95      [ nwhd in Hni1; nwhd; nelim daemon
96      | nwhd in ⊢ (???%?);
97        nchange in Hni1 with (nindex < S n);
98        ngeneralize in match (le_S_S_to_le … Hni1) in ⊢ ?;
99        nwhd in ⊢ (? → ???(???????%?)?);
100        napply (nat_rect_CProp0
101         (λx. nindex ≤ x →
102           partition_splits_card_map A P (S n) s f fi
103            (plus
104              match minus x nindex with
105               [ O ⇒ O | S nn ⇒ big_plus nn (λi.λ_.s (S (plus i nindex)))]
106              nindex2) x = y) ?? n)
107         [ #K; nrewrite < (minus_O_n nindex); nwhd in ⊢ (???(???????%?)?);
108           nwhd in ⊢ (???%?); nchange in Hni21 with (nindex2 < s nindex);
109           ngeneralize in match (le_O_to_eq … K) in ⊢ ?; #K';
110           ngeneralize in match Hni21 in ⊢ ?;
111           ngeneralize in match Hni22 in ⊢ ?;
112           nrewrite > K' in ⊢ (% → % → ?); #K1; #K2;
113           nrewrite > (ltb_t … K2);
114           nwhd in ⊢ (???%?); nassumption
115         | #n'; #Hrec; #HH; nelim (le_to_lt_or_eq … HH)
116            [##2: #K; nrewrite < K; nrewrite < (minus_canc nindex);
117             nwhd in ⊢ (???(???????%?)?);
118             (*???????*)
119          ##| #K; nwhd in ⊢ (???%?);
120              nrewrite > (minus_S n' nindex ?) [##2: napply le_S_S_to_le; nassumption]
121              ngeneralize in match (? :
122               match S (minus n' nindex) with [O ⇒ O | S nn ⇒ big_plus nn (λi.λ_.s (S (plus i nindex)))]
123               = big_plus (minus n' nindex) (λi.λ_.s (S (plus i nindex)))) in ⊢ ? [##2: napply refl]
124              #He; napply (eq_rect_CProp0_r ??
125               (λx.λ_.
126                 match ltb (plus x nindex2) (s (S n')) with
127                  [ true ⇒ iso_f ???? (fi (S n')) (plus x nindex2)
128                  | false ⇒ ?(*partition_splits_card_map A P (S n) s f fi
129                      (minus (plus x nindex2) (s (S n'))) n'*)
130                  ] = y)
131               ?? He);
132              ngeneralize in match (? :
133               ltb (plus (big_plus (minus n' nindex) (λi.λ_.s (S (plus i nindex)))) nindex2)
134                (s (S n')) = false) in ⊢ ?
135                [ #Hc; nrewrite > Hc; nwhd in ⊢ (???%?);
136                  nelim (le_to_lt_or_eq … (le_S_S_to_le … K))
137                   [
138                 ##| #E; ngeneralize in match Hc in ⊢ ?;
139                     nrewrite < E; nrewrite < (minus_canc nindex);
140                     nwhd in ⊢ (??(?%?)? → ?);
141                     nrewrite > E in Hni21; #E'; nchange in E' with (nindex2 < s n'); 
142                     ngeneralize in match Hni21 in ⊢ ?;
143                     
144                
145                 ngeneralize in match (? :
146                  minus (plus (big_plus (minus n' nindex) (λi.λ_.s (S (plus i nindex)))) nindex2)
147                   (s (S n'))
148                  =
149                   plus
150                    match minus n' nindex with
151                     [ O ⇒ O | S nn ⇒ big_plus nn (λi.λ_.s (S (plus i nindex)))] nindex2)
152                  in ⊢ ?
153                   [ #F; nrewrite > F; napply Hrec; napply le_S_S_to_le; nassumption
154                   | nelim (le_to_lt_or_eq … (le_S_S_to_le … K))
155                      [
156                    ##| #E; nrewrite < E; nrewrite < (minus_canc nindex); nnormalize;
157                        
158                        nwhd in ⊢ (???%);
159                      ]
160  
161              
162               nrewrite > He;
163              
164                
165               nnormalize in ⊢ (???%?);
166           
167        
168        
169        nelim (le_to_lt_or_eq … K)
170         [##2: #K'; nrewrite > K'; nrewrite < (minus_canc n); nnormalize;
171          napply (eq_rect_CProp0 nat nindex (λx:nat.λ_.partition_splits_card_map A P (S n) s f fi nindex2 x = y) ? n K');
172          nchange in Hni21 with (nindex2 < s nindex); ngeneralize in match Hni21 in ⊢ ?;
173          ngeneralize in match Hni22 in ⊢ ?;
174          nelim nindex
175           [ #X1; #X2; nwhd in ⊢ (??? % ?);
176             napply (lt_to_ltb_t ???? X2); #D; nwhd in ⊢ (??? % ?); nassumption
177           | #n0; #_; #X1; #X2; nwhd in ⊢ (??? % ?);
178             napply (lt_to_ltb_t ???? X2); #D; nwhd in ⊢ (??? % ?); nassumption]
179       ##| #K'; ngeneralize in match (lt_to_minus … K') in ⊢ ?; #K2;
180           napply (eq_rect_CProp0 ?? (λx.λ_.?) ? ? K2); (* uffa, ancora??? *)
181           nwhd in ⊢ (??? (???????(?%?)?) ?);
182           ngeneralize in match K' in ⊢ ?;
183           napply (nat_rect_CProp0
184            (λx. nindex < x →
185              partition_splits_card_map A P (S n) s f fi
186               (plus (big_op plus_magma_type (minus (minus x nindex) (S O))
187                 (λi.λ_.s (S (plus i nindex))) O) nindex2) x = y) ?? n)
188            [ #A; nelim (not_lt_O … A)
189            | #n'; #Hrec; #X; nwhd in ⊢ (???%?);
190              ngeneralize in match
191               (? : ¬ ((plus (big_op plus_magma_type (minus (minus (S n') nindex) (S O))
192                 (λi.λ_.s (S (plus i nindex))) O) nindex2) < s (S n'))) in ⊢ ?
193               [ #B1; napply (lt_to_ltb_f ???? B1); #B1'; nwhd in ⊢ (???%?);
194                 nrewrite > (minus_S n' nindex …) [##2: napply le_S_S_to_le; nassumption]
195                 ngeneralize in match (le_S_S_to_le … X) in ⊢ ?; #X';
196                 nelim (le_to_lt_or_eq … X')
197                  [##2: #X'';
198                   nchange in Hni21 with (nindex2 < s nindex); ngeneralize in match Hni21 in ⊢ ?;
199                   nrewrite > X''; nrewrite < (minus_canc n');
200                   nrewrite < (minus_canc (S O)); nnormalize in ⊢ (? → %);
201                   nelim n'
202                    [ #Y; nwhd in ⊢ (??? % ?);
203                      ngeneralize in match (minus_lt_to_lt ? (s (S O)) ? Y) in ⊢ ?; #Y';
204                      napply (lt_to_ltb_t … Y'); #H; nwhd in ⊢ (???%?);  
205                  
206                 nrewrite > (minus_S (minus n' nindex) (S O) …) [##2: 
207               
208               XXX;
209           
210           nelim n in f K' ⊢ ?
211            [ #A; nelim daemon;
212          
213        (* BEL POSTO DOVE FARE UN LEMMA *)
214        (* invariante: Hni1; altre premesse: Hni1, Hni22 *)
215        nelim n in ⊢ (% → ??? (????????%) ?)
216         [ #A (* decompose *)
217         | #index'; #Hrec; #K; nwhd in ⊢ (???%?);
218           nelim (ltb xxx (s (S index')));
219           #K1; nwhd in ⊢ (???%?)
220            [
221            
222            nindex < S index' + 1
223            +^{nindex} (s i) w < s (S index') 
224            S index' == nindex
225            
226            |
227            ]
228         ]
229      ]
230   | #x; #x'; nnormalize in ⊢ (? → ? → %);
231     nelim daemon
232   ]
233 nqed.
234
235 (************** equivalence relations vs partitions **********************)
236
237 ndefinition partition_of_compatible_equivalence_relation:
238  ∀A:setoid. compatible_equivalence_relation A → partition A.
239  #A; #R; napply mk_partition
240   [ napply (quotient ? R)
241   | napply Full_set
242   | #a; napply mk_qpowerclass
243      [ napply {x | R x a}
244      | #x; #x'; #H; nnormalize; napply mk_iff; #K; nelim daemon]
245 ##| #x; #_; nnormalize; napply (ex_intro … x); napply conj; napply refl
246   | #x; #x'; #_; #_; nnormalize; *; #x''; *; #H1; #H2; napply (trans ?????? H2);
247     napply sym; nassumption
248   | nnormalize; napply conj
249      [ #a; #_; napply I | #a; #_; napply (ex_intro … a); napply conj [ napply I | napply refl]##]
250 nqed.