]> matita.cs.unibo.it Git - helm.git/blob - helm/software/matita/nlibrary/sets/sets.ma
A very little bit of arithmetic.
[helm.git] / helm / software / matita / nlibrary / sets / sets.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 (******************* SETS OVER TYPES *****************)
16
17 include "logic/connectives.ma".
18
19 nrecord powerclass (A: Type[0]) : Type[1] ≝ { mem: A → CProp[0] }.
20
21 interpretation "mem" 'mem a S = (mem ? S a).
22 interpretation "powerclass" 'powerset A = (powerclass A).
23 interpretation "subset construction" 'subset \eta.x = (mk_powerclass ? x).
24
25 ndefinition subseteq ≝ λA.λU,V.∀a:A. a ∈ U → a ∈ V.
26 interpretation "subseteq" 'subseteq U V = (subseteq ? U V).
27
28 ndefinition overlaps ≝ λA.λU,V.∃x:A.x ∈ U ∧ x ∈ V.
29 interpretation "overlaps" 'overlaps U V = (overlaps ? U V).
30
31 ndefinition intersect ≝ λA.λU,V:Ω \sup A.{ x | x ∈ U ∧ x ∈ V }.
32 interpretation "intersect" 'intersects U V = (intersect ? U V).
33
34 ndefinition union ≝ λA.λU,V:Ω \sup A.{ x | x ∈ U ∨ x ∈ V }.
35 interpretation "union" 'union U V = (union ? U V).
36
37 ndefinition big_union ≝ λA.λT:Type[0].λf:T → Ω \sup A.{ x | ∃i. x ∈ f i }.
38
39 ndefinition big_intersection ≝ λA.λT:Type[0].λf:T → Ω \sup A.{ x | ∀i. x ∈ f i }.
40
41 ndefinition full_set: ∀A. Ω \sup A ≝ λA.{ x | True }.
42 (* bug dichiarazione coercion qui *)
43 (* ncoercion full_set : ∀A:Type[0]. Ω \sup A ≝ full_set on _A: Type[0] to (Ω \sup ?). *)
44
45 nlemma subseteq_refl: ∀A.∀S: Ω \sup A. S ⊆ S.
46  #A; #S; #x; #H; nassumption.
47 nqed.
48
49 nlemma subseteq_trans: ∀A.∀S,T,U: Ω \sup A. S ⊆ T → T ⊆ U → S ⊆ U.
50  #A; #S; #T; #U; #H1; #H2; #x; #P; napply H2; napply H1; nassumption.
51 nqed.
52
53 include "properties/relations1.ma".
54
55 ndefinition seteq: ∀A. equivalence_relation1 (Ω \sup A).
56  #A; napply mk_equivalence_relation1
57   [ napply (λS,S'. S ⊆ S' ∧ S' ⊆ S)
58   | #S; napply conj; napply subseteq_refl
59   | #S; #S'; *; #H1; #H2; napply conj; nassumption
60   | #S; #T; #U; *; #H1; #H2; *; #H3; #H4; napply conj; napply subseteq_trans;
61      ##[##2,5: nassumption |##1,4: ##skip |##*: nassumption]##]
62 nqed. 
63
64 include "sets/setoids1.ma".
65
66 ndefinition powerclass_setoid: Type[0] → setoid1.
67  #A; napply mk_setoid1
68   [ napply (Ω \sup A)
69   | napply seteq ]
70 nqed.
71
72 unification hint 0 (∀A. (λx,y.True) (carr1 (powerclass_setoid A)) (Ω \sup A)).
73
74 (************ SETS OVER SETOIDS ********************)
75
76 include "logic/cprop.ma".
77
78 nrecord qpowerclass (A: setoid) : Type[1] ≝
79  { pc:> Ω \sup A;
80    mem_ok': ∀x,x':A. x=x' → (x ∈ pc) = (x' ∈ pc) 
81  }.
82
83 ndefinition qseteq: ∀A. equivalence_relation1 (qpowerclass A).
84  #A; napply mk_equivalence_relation1
85   [ napply (λS,S':qpowerclass A. eq_rel1 ? (eq1 (powerclass_setoid A)) S S')
86   | #S; napply (refl1 ? (seteq A))
87   | #S; #S'; napply (sym1 ? (seteq A))
88   | #S; #T; #U; napply (trans1 ? (seteq A))]
89 nqed.
90
91 ndefinition qpowerclass_setoid: setoid → setoid1.
92  #A; napply mk_setoid1
93   [ napply (qpowerclass A)
94   | napply (qseteq A) ]
95 nqed.
96
97 unification hint 0 (∀A. (λx,y.True) (carr1 (qpowerclass_setoid A)) (qpowerclass A)).
98 ncoercion qpowerclass_hint: ∀A: setoid. ∀S: qpowerclass_setoid A. Ω \sup A ≝ λA.λS.S
99  on _S: (carr1 (qpowerclass_setoid ?)) to (Ω \sup ?). 
100
101 nlemma mem_ok: ∀A. binary_morphism1 (setoid1_of_setoid A) (qpowerclass_setoid A) CPROP.
102  #A; napply mk_binary_morphism1
103   [ napply (λx.λS: qpowerclass_setoid A. x ∈ S) (* CSC: ??? *)
104   | #a; #a'; #b; #b'; #Ha; #Hb; (* CSC: qui *; non funziona *)
105     nwhd; nwhd in ⊢ (? (? % ??) (? % ??)); napply mk_iff; #H
106      [ ncases Hb; #Hb1; #_; napply Hb1; napply (. (mem_ok' …))
107         [ nassumption | napply Ha^-1 | ##skip ]
108    ##| ncases Hb; #_; #Hb2; napply Hb2; napply (. (mem_ok' …))
109         [ nassumption | napply Ha | ##skip ]##]
110 nqed.
111
112 unification hint 0 (∀A,x,S. (λx,y.True) (fun21 ??? (mem_ok A) x S) (mem A S x)).
113   
114 nlemma subseteq_ok: ∀A. binary_morphism1 (qpowerclass_setoid A) (qpowerclass_setoid A) CPROP.
115  #A; napply mk_binary_morphism1
116   [ napply (λS,S': qpowerclass_setoid ?. S ⊆ S')
117   | #a; #a'; #b; #b'; *; #Ha1; #Ha2; *; #Hb1; #Hb2; napply mk_iff; #H
118      [ napply (subseteq_trans … a' a) (* anche qui, perche' serve a'? *)
119         [ nassumption | napply (subseteq_trans … a b); nassumption ]
120    ##| napply (subseteq_trans … a a') (* anche qui, perche' serve a'? *)
121         [ nassumption | napply (subseteq_trans … a' b'); nassumption ] ##]
122 nqed.
123
124 nlemma intersect_ok: ∀A. binary_morphism1 (qpowerclass_setoid A) (qpowerclass_setoid A) (qpowerclass_setoid A).
125  #A; napply mk_binary_morphism1
126   [ #S; #S'; napply mk_qpowerclass
127      [ napply (S ∩ S')
128      | #a; #a'; #Ha; nwhd in ⊢ (? ? ? % %); napply mk_iff; *; #H1; #H2; napply conj
129         [##1,2: napply (. (mem_ok' …)^-1) [##3,6: nassumption |##1,4: nassumption |##*: ##skip]
130       ##|##3,4: napply (. (mem_ok' …)) [##2,5: nassumption |##1,4: nassumption |##*: ##skip]##]##]
131  ##| #a; #a'; #b; #b'; #Ha; #Hb; nwhd; napply conj; #x; nwhd in ⊢ (% → %); #H
132       [ napply (. ((#‡Ha^-1)‡(#‡Hb^-1))); nassumption
133       | napply (. ((#‡Ha)‡(#‡Hb))); nassumption ]##]
134 nqed.
135
136 unification hint 0 (∀A.∀U,V.(λx,y.True) (fun21 ??? (intersect_ok A) U V) (intersect A U V)).
137
138 nlemma test: ∀A:setoid. ∀U,V:qpowerclass A. ∀x,x':setoid1_of_setoid A. x=x' → x ∈ U ∩ V → x' ∈ U ∩ V.
139  #A; #U; #V; #x; #x'; #H; #p;
140   (* CSC: senza la change non funziona! *)
141   nchange with (x' ∈ (fun21 ??? (intersect_ok A) U V));
142   napply (. (H^-1‡#)); nassumption.
143 nqed.
144
145 (*
146 (* qui non funziona una cippa *)
147 ndefinition image: ∀A,B. (carr A → carr B) → Ω \sup A → Ω \sup B ≝
148  λA,B:setoid.λf:carr A → carr B.λSa:Ω \sup A.
149   {y | ∃x. x ∈ Sa ∧ eq_rel (carr B) (eq B) ? ?(*(f x) y*)}.
150   ##[##2: napply (f x); ##|##3: napply y]
151  #a; #a'; #H; nwhd; nnormalize; (* per togliere setoid1_of_setoid *) napply (mk_iff ????);
152  *; #x; #Hx; napply (ex_intro … x)
153   [ napply (. (#‡(#‡#))); 
154
155 ndefinition counter_image: ∀A,B. (A → B) → Ω \sup B → Ω \sup A ≝
156  λA,B,f,Sb. {x | ∃y. y ∈ Sb ∧ f x = y}.
157 *)
158
159 (******************* compatible equivalence relations **********************)
160
161 nrecord compatible_equivalence_relation (A: setoid) : Type[1] ≝
162  { rel:> equivalence_relation A;
163    compatibility: ∀x,x':A. x=x' → eq_rel ? rel x x' (* coercion qui non va *)
164  }.
165
166 ndefinition quotient: ∀A. compatible_equivalence_relation A → setoid.
167  #A; #R; napply mk_setoid
168   [ napply A
169   | napply R]
170 nqed.
171
172 (******************* first omomorphism theorem for sets **********************)
173
174 ndefinition eqrel_of_morphism:
175  ∀A,B. unary_morphism A B → compatible_equivalence_relation A.
176  #A; #B; #f; napply mk_compatible_equivalence_relation
177   [ napply mk_equivalence_relation
178      [ napply (λx,y. f x = f y)
179      | #x; napply refl | #x; #y; napply sym | #x; #y; #z; napply trans]
180 ##| #x; #x'; #H; nwhd; napply (.= (†H)); napply refl ]
181 nqed.
182
183 ndefinition canonical_proj: ∀A,R. unary_morphism A (quotient A R).
184  #A; #R; napply mk_unary_morphism
185   [ napply (λx.x) | #a; #a'; #H; napply (compatibility ? R … H) ]
186 nqed.
187
188 ndefinition quotiented_mor:
189  ∀A,B.∀f:unary_morphism A B.
190   unary_morphism (quotient ? (eqrel_of_morphism ?? f)) B.
191  #A; #B; #f; napply mk_unary_morphism
192   [ napply f | #a; #a'; #H; nassumption]
193 nqed.
194
195 nlemma first_omomorphism_theorem_functions1:
196  ∀A,B.∀f: unary_morphism A B.
197   ∀x. f x = quotiented_mor ??? (canonical_proj ? (eqrel_of_morphism ?? f) x).
198  #A; #B; #f; #x; napply refl;
199 nqed.
200
201 ndefinition surjective ≝ λA,B.λf:unary_morphism A B. ∀y.∃x. f x = y.
202
203 ndefinition injective ≝ λA,B.λf:unary_morphism A B. ∀x,x'. f x = f x' → x = x'.
204
205 nlemma first_omomorphism_theorem_functions2:
206  ∀A,B.∀f: unary_morphism A B. surjective ?? (canonical_proj ? (eqrel_of_morphism ?? f)).
207  #A; #B; #f; nwhd; #y; napply (ex_intro … y); napply refl.
208 nqed.
209
210 nlemma first_omomorphism_theorem_functions3:
211  ∀A,B.∀f: unary_morphism A B. injective ?? (quotiented_mor ?? f).
212  #A; #B; #f; nwhd; #x; #x'; #H; nassumption.
213 nqed.
214
215 (************************** partitions ****************************)
216
217 nrecord partition (A: Type[0]) : Type[1] ≝ 
218  { index_set: setoid;
219    class: index_set → Ω \sup A;
220    disjoint: ∀i,j. ¬ (i = j) → ¬(class i ≬ class j);
221    covers: big_union ?? class = full_set A
222  }.
223
224 (*
225 nrecord has_card (A: Type[0]) (S: Ω \sup A) (n: nat) : Prop ≝
226  { f: ∀m:nat. m < n → S;
227    f_inj: injective ?? f;
228    f_sur: surjective ?? f
229  }.
230
231 nlemma subset_of_finite:
232  ∀A. ∃n. has_card ? (full_subset A) n → ∀S. ∃m. has_card ? S m.
233 nqed.
234
235 nlemma partition_splits_card:
236  ∀A. ∀P: partition A. ∀s: index_set P → nat.
237   (∀i. has_card ? (class i) = s i) →
238    has_card ? (full_subset A) (big_plus ? (λi. s i)).
239 nqed.
240 *)