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