]> matita.cs.unibo.it Git - helm.git/blob - helm/software/matita/nlibrary/sets/partitions.ma
Release 0.5.9.
[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" (instance 7) = "setoid1 eq".
22 nrecord partition (A: setoid) : Type[1] ≝ 
23  { support: setoid;
24    indexes: ext_powerclass support;
25    class: unary_morphism1 (setoid1_of_setoid support) (ext_powerclass_setoid A);
26    inhabited: ∀i. i ∈ indexes → class i ≬ class i;
27    disjoint: ∀i,j. i ∈ indexes → j ∈ indexes → class i ≬ class j → i = j;
28    covers: big_union support ? indexes (λx.class x) = full_set A
29  }.
30  
31 naxiom daemon: False.
32
33 nlet rec iso_nat_nat_union (s: nat → nat) m index on index : pair nat nat ≝
34  match ltb m (s index) with
35   [ true ⇒ mk_pair … index m
36   | false ⇒
37      match index with
38       [ O ⇒ (* dummy value: it could be an elim False: *) mk_pair … O O
39       | S index' ⇒ iso_nat_nat_union s (minus m (s index)) index']].
40
41 alias symbol "eq" = "leibnitz's equality".
42 naxiom plus_n_O: ∀n. n + O = n.
43 naxiom plus_n_S: ∀n,m. n + S m = S (n + m).
44 naxiom ltb_t: ∀n,m. n < m → ltb n m = true.
45 naxiom ltb_f: ∀n,m. ¬ (n < m) → ltb n m = false.
46 naxiom ltb_cases: ∀n,m. (n < m ∧ ltb n m = true) ∨ (¬ (n < m) ∧ ltb n m = false).
47 naxiom minus_canc: ∀n. minus n n = O.
48 naxiom ad_hoc9: ∀a,b,c. a < b + c → a - b < c.
49 naxiom ad_hoc10: ∀a,b,c. a - b = c → a = b + c.
50 naxiom ad_hoc11: ∀a,b. a - b ≤ S a - b.
51 naxiom ad_hoc12: ∀a,b. b ≤ a → S a - b - (a - b) = S O.
52 naxiom ad_hoc13: ∀a,b. b ≤ a → (O + (a - b)) + b = a.
53 naxiom ad_hoc14: ∀a,b,c,d,e. c ≤ a → a - c = b + d + e → a = b + (c + d) + e.
54 naxiom ad_hoc15: ∀a,a',b,c. a=a' → b < c → a + b < c + a'.
55 naxiom ad_hoc16: ∀a,b,c. a < c → a < b + c.
56 naxiom not_lt_to_le: ∀a,b. ¬ (a < b) → b ≤ a.
57 naxiom le_to_le_S_S: ∀a,b. a ≤ b → S a ≤ S b.
58 naxiom minus_S: ∀n. S n - n = S O.
59 naxiom ad_hoc17: ∀a,b,c,d,d'. a+c+d=b+c+d' → a+d=b+d'.
60 naxiom split_big_plus:
61   ∀n,m,f. m ≤ n →
62    big_plus n f = big_plus m (λi,p.f i ?) + big_plus (n - m) (λi.λp.f (i + m) ?).
63  nelim daemon.
64 nqed.
65 naxiom big_plus_preserves_ext:
66  ∀n,f,f'. (∀i,p. f i p = f' i p) → big_plus n f = big_plus n f'.
67
68 ntheorem iso_nat_nat_union_char:
69  ∀n:nat. ∀s: nat → nat. ∀m:nat. m < big_plus (S n) (λi.λ_.s i) →
70   let p ≝ iso_nat_nat_union s m n in
71    m = big_plus (n - fst … p) (λi.λ_.s (S (i + fst … p))) + snd … p ∧
72     fst … p ≤ n ∧ snd … p < s (fst … p).
73  #n; #s; nelim n
74   [ #m; nwhd in ⊢ (??% → let p ≝ % in ?); nwhd in ⊢ (??(??%) → ?);
75     nrewrite > (plus_n_O (s O)); #H; nrewrite > (ltb_t … H); nnormalize; @
76     [ @ [ napply refl | napply le_n ] ##| nassumption ]
77 ##| #n'; #Hrec; #m; nwhd in ⊢ (??% → let p ≝ % in ?); #H;
78     ncases (ltb_cases m (s (S n'))); *; #H1; #H2; nrewrite > H2;
79     nwhd in ⊢ (let p ≝ % in ?); nwhd
80      [ napply conj [napply conj
81         [ nwhd in ⊢ (???(?(?%(λ_.λ_:(??%).?))%)); nrewrite > (minus_canc n'); napply refl
82         | nnormalize; napply le_n]
83       ##| nnormalize; nassumption ]
84    ##| nchange in H with (m < s (S n') + big_plus (S n') (λi.λ_.s i));
85        nlapply (Hrec (m - s (S n')) ?)
86         [ napply ad_hoc9; nassumption] *; *; #Hrec1; #Hrec2; #Hrec3; @
87         [##2: nassumption
88         |@
89          [nrewrite > (split_big_plus …); ##[##2:napply ad_hoc11;##|##3:##skip]
90           nrewrite > (ad_hoc12 …); ##[##2: nassumption]
91           nwhd in ⊢ (???(?(??%)?));
92           nrewrite > (ad_hoc13 …);##[##2: nassumption]
93           napply ad_hoc14 [ napply not_lt_to_le; nassumption ]
94           nwhd in ⊢ (???(?(??%)?));
95           nrewrite > (plus_n_O …);
96           nassumption;
97         ##| napply le_S; nassumption ]##]##]##]
98 nqed.
99
100 ntheorem iso_nat_nat_union_pre:
101  ∀n:nat. ∀s: nat → nat.
102   ∀i1,i2. i1 ≤ n → i2 < s i1 →
103    big_plus (n - i1) (λi.λ_.s (S (i + i1))) + i2 < big_plus (S n) (λi.λ_.s i).
104  #n; #s; #i1; #i2; #H1; #H2;
105  nrewrite > (split_big_plus (S n) (S i1) (λi.λ_.s i) ?)
106   [##2: napply le_to_le_S_S; nassumption]
107  napply ad_hoc15
108   [ nwhd in ⊢ (???(?%?));
109     napply big_plus_preserves_ext; #i; #_;
110     nrewrite > (plus_n_S i i1); napply refl
111   | nrewrite > (split_big_plus (S i1) i1 (λi.λ_.s i) ?) [##2: napply le_S; napply le_n]
112     napply ad_hoc16; nrewrite > (minus_S i1); nnormalize; nrewrite > (plus_n_O (s i1) …);
113     nassumption ]
114 nqed.
115     
116 ntheorem iso_nat_nat_union_uniq:
117  ∀n:nat. ∀s: nat → nat.
118   ∀i1,i1',i2,i2'. i1 ≤ n → i1' ≤ n → i2 < s i1 → i2' < s i1' →
119    big_plus (n - i1) (λi.λ_.s (S (i + i1))) + i2 = big_plus (n - i1') (λi.λ_.s (S (i + i1'))) + i2' →
120     i1 = i1' ∧ i2 = i2'.
121  #n; #s; #i1; #i1'; #i2; #i2'; #H1; #H1'; #H2; #H2'; #E;
122  nelim daemon.
123 nqed.
124
125 nlemma partition_splits_card:
126  ∀A. ∀P:partition A. ∀n,s.
127   ∀f:isomorphism ?? (Nat_ n) (indexes ? P).
128    (∀i. isomorphism ?? (Nat_ (s i)) (class ? P (iso_f ???? f i))) →
129     (isomorphism ?? (Nat_ (big_plus n (λi.λ_.s i))) (Full_set A)).
130 #A; #P; #Sn; ncases Sn
131   [ #s; #f; #fi;
132     nlapply (covers ? P); *; #_; #H;
133     (*
134     nlapply
135      (big_union_preserves_iso ??? (indexes A P) (Nat_ O) (λx.class ? P x) f);
136      *; #K; #_; nwhd in K: (? → ? → %);*)
137     nelim daemon (* impossibile *)
138   | #n; #s; #f; #fi; @
139   [ @
140      [ napply (λm.let p ≝ (iso_nat_nat_union s m n) in iso_f ???? (fi (fst … p)) (snd … p))
141      | #a; #a'; #H; nrewrite < H; napply refl ]
142 ##| #x; #Hx; nwhd; napply I
143 ##| #y; #_;
144     nlapply (covers ? P); *; #_; #Hc;
145     nlapply (Hc y I); *; #index; *; #Hi1; #Hi2;
146     nlapply (f_sur ???? f ? Hi1); *; #nindex; *; #Hni1; #Hni2;
147     nlapply (f_sur ???? (fi nindex) y ?)
148      [ alias symbol "refl" = "refl".
149 alias symbol "prop1" = "prop11".
150 alias symbol "prop2" = "prop21 mem".
151 napply (. #‡(†?));##[##2: napply Hni2 |##1: ##skip | nassumption]##]
152     *; #nindex2; *; #Hni21; #Hni22;
153     nletin xxx ≝ (plus (big_plus (minus n nindex) (λi.λ_.s (S (plus i nindex)))) nindex2);
154     @ xxx; @
155      [ napply iso_nat_nat_union_pre [ napply le_S_S_to_le; nassumption | nassumption ]
156    ##| nwhd in ⊢ (???%%); napply (.= ?) [##3: nassumption|##skip]
157        nlapply (iso_nat_nat_union_char n s xxx ?)
158         [napply iso_nat_nat_union_pre [ napply le_S_S_to_le; nassumption | nassumption]##]
159        *; *; #K1; #K2; #K3;
160        nlapply
161         (iso_nat_nat_union_uniq n s nindex (fst … (iso_nat_nat_union s xxx n))
162           nindex2 (snd … (iso_nat_nat_union s xxx n)) ?????)
163         [##6: *; #E1; #E2; nrewrite > E1; nrewrite > E2; napply refl
164         |##5: napply le_S_S_to_le; nassumption
165         |##*: nassumption]##]
166 ##| #x; #x'; nnormalize in ⊢ (? → ? → %); #Hx; #Hx'; #E;
167     ncut(∀i1,i2,i1',i2'. i1 ∈ Nat_ (S n) → i1' ∈ Nat_ (S n) → i2 ∈ Nat_ (s i1) → i2' ∈ Nat_ (s i1') → eq_rel (carr A) (eq A) (fi i1 i2) (fi i1' i2') → i1=i1' ∧ i2=i2');
168     ##[ #i1; #i2; #i1'; #i2'; #Hi1; #Hi1'; #Hi2; #Hi2'; #E;
169        nlapply(disjoint … P (f i1) (f i1') ???)
170        [##2,3: napply f_closed; nassumption
171        |##1: @ (fi i1 i2); @;
172          ##[ napply f_closed; nassumption ##| alias symbol "refl" = "refl1".
173 napply (. E‡#);
174              nwhd; napply f_closed; nassumption]##]
175       #E'; ncut(i1 = i1'); ##[ napply (f_inj … E'); nassumption; ##]
176       #E''; nrewrite < E''; @; 
177       ##[ @;
178       ##| nrewrite < E'' in E; #E'''; napply (f_inj … E''')
179              [ nassumption | nrewrite > E''; nassumption ]##]##]
180    ##] #K;
181     nelim (iso_nat_nat_union_char n s x Hx); *; #i1x; #i2x; #i3x;
182     nelim (iso_nat_nat_union_char n s x' Hx'); *; #i1x'; #i2x'; #i3x';
183     nlapply (K … E)  
184      [##1,2: nassumption;
185      ##|##3,4:napply le_to_le_S_S; nassumption; ##]
186     *; #K1; #K2;
187     napply (eq_rect_CProp0_r ?? (λX.λ_.? = X) ?? i1x');
188     napply (eq_rect_CProp0_r ?? (λX.λ_.X = ?) ?? i1x);
189     nrewrite > K1; nrewrite > K2; napply refl ]
190 nqed.
191
192 (************** equivalence relations vs partitions **********************)
193
194 ndefinition partition_of_compatible_equivalence_relation:
195  ∀A:setoid. compatible_equivalence_relation A → partition A.
196  #A; #R; napply mk_partition
197   [ napply (quotient ? R)
198   | napply Full_set
199   | napply mk_unary_morphism1
200      [ #a; napply mk_ext_powerclass
201         [ napply {x | R x a}
202         | #x; #x'; #H; nnormalize; napply mk_iff; #K; nelim daemon]
203    ##| #a; #a'; #H; napply conj; #x; nnormalize; #K [ nelim daemon | nelim daemon]##]
204 ##| #x; #_; nnormalize; napply (ex_intro … x); napply conj; napply refl
205   | #x; #x'; #_; #_; nnormalize; *; #x''; *; #H1; #H2; napply (trans ?????? H2);
206     napply sym; nassumption
207   | nnormalize; napply conj
208      [ #a; #_; napply I | #a; #_; napply (ex_intro … a); napply conj [ napply I | napply refl]##]
209 nqed.