+nlemma intersect_ok: ∀A. binary_morphism1 (ext_powerclass_setoid A) (ext_powerclass_setoid A) (ext_powerclass_setoid A).
+ #A; @
+ [ #S; #S'; @
+ [ napply (S ∩ S')
+ | #a; #a'; #Ha;
+ nwhd in ⊢ (? ? ? % %); @; *; #H1; #H2; @
+ [##1,2: napply (. Ha^-1‡#); nassumption;
+ ##|##3,4: napply (. Ha‡#); nassumption]##]
+ ##| #a; #a'; #b; #b'; #Ha; #Hb; nwhd; @; #x; nwhd in ⊢ (% → %); #H
+ [ alias symbol "invert" = "setoid1 symmetry".
+ alias symbol "refl" = "refl".
+alias symbol "prop2" = "prop21".
+napply (. ((#‡Ha^-1)‡(#‡Hb^-1))); nassumption
+ | napply (. ((#‡Ha)‡(#‡Hb))); nassumption ]##]
+nqed.
+
+(* unfold if intersect, exposing fun21 *)
+alias symbol "hint_decl" = "hint_decl_Type1".
+unification hint 0 ≔
+ A : setoid, B,C : ext_powerclass A ⊢
+ pc A (fun21 …
+ (mk_binary_morphism1 …
+ (λS,S':qpowerclass_setoid A.mk_qpowerclass ? (S ∩ S') (mem_ok' ? (intersect_ok ? S S')))
+ (prop21 … (intersect_ok A)))
+ B
+ C)
+ ≡ intersect ? (pc ? B) (pc ? C).
+
+nlemma test: ∀A:setoid. ∀U,V:qpowerclass A. ∀x,x':setoid1_of_setoid A. x=x' → x ∈ U ∩ V → x' ∈ U ∩ V.
+ #A; #U; #V; #x; #x'; #H; #p; napply (. (H^-1‡#)); nassumption.
+nqed.
+*)
+
+ndefinition image: ∀A,B. (carr A → carr B) → Ω^A → Ω^B ≝
+ λA,B:setoid.λf:carr A → carr B.λSa:Ω^A.
+ {y | ∃x. x ∈ Sa ∧ eq_rel (carr B) (eq0 B) (f x) y}.
+
+ndefinition counter_image: ∀A,B. (carr A → carr B) → Ω^B → Ω^A ≝
+ λA,B,f,Sb. {x | ∃y. y ∈ Sb ∧ f x = y}.
+
+(******************* compatible equivalence relations **********************)
+
+nrecord compatible_equivalence_relation (A: setoid) : Type[1] ≝
+ { rel:> equivalence_relation A;
+ compatibility: ∀x,x':A. x=x' → rel x x'
+ }.
+
+ndefinition quotient: ∀A. compatible_equivalence_relation A → setoid.
+ #A; #R; @ A R;
+nqed.
+
+(******************* first omomorphism theorem for sets **********************)
+
+ndefinition eqrel_of_morphism:
+ ∀A,B. A ⇒_0 B → compatible_equivalence_relation A.
+ #A; #B; #f; @
+ [ @ [ napply (λx,y. f x = f y) ] /2/;
+##| #x; #x'; #H; nwhd; alias symbol "prop1" = "prop1".
+napply (.= (†H)); // ]
+nqed.
+
+ndefinition canonical_proj: ∀A,R. A ⇒_0 (quotient A R).
+ #A; #R; @
+ [ napply (λx.x) | #a; #a'; #H; napply (compatibility … R … H) ]
+nqed.
+
+ndefinition quotiented_mor:
+ ∀A,B.∀f:A ⇒_0 B.(quotient … (eqrel_of_morphism … f)) ⇒_0 B.
+ #A; #B; #f; @ [ napply f ] //.
+nqed.
+
+nlemma first_omomorphism_theorem_functions1:
+ ∀A,B.∀f: unary_morphism A B.
+ ∀x. f x = quotiented_mor … (canonical_proj … (eqrel_of_morphism … f) x).
+//. nqed.
+
+alias symbol "eq" = "setoid eq".
+ndefinition surjective ≝
+ λA,B.λS: ext_powerclass A.λT: ext_powerclass B.λf:A ⇒_0 B.
+ ∀y. y ∈ T → ∃x. x ∈ S ∧ f x = y.
+
+ndefinition injective ≝
+ λA,B.λS: ext_powerclass A.λf:A ⇒_0 B.
+ ∀x,x'. x ∈ S → x' ∈ S → f x = f x' → x = x'.
+
+nlemma first_omomorphism_theorem_functions2:
+ ∀A,B.∀f:A ⇒_0 B.
+ surjective … (Full_set ?) (Full_set ?) (canonical_proj ? (eqrel_of_morphism … f)).
+/3/. nqed.
+
+nlemma first_omomorphism_theorem_functions3:
+ ∀A,B.∀f:A ⇒_0 B.
+ injective … (Full_set ?) (quotiented_mor … f).
+ #A; #B; #f; nwhd; #x; #x'; #Hx; #Hx'; #K; nassumption.
+nqed.
+
+nrecord isomorphism (A, B : setoid) (S: ext_powerclass A) (T: ext_powerclass B) : Type[0] ≝
+ { iso_f:> A ⇒_0 B;
+ f_closed: ∀x. x ∈ S → iso_f x ∈ T;
+ f_sur: surjective … S T iso_f;
+ f_inj: injective … S iso_f
+ }.
+
+
+(*
+nrecord isomorphism (A, B : setoid) (S: qpowerclass A) (T: qpowerclass B) : CProp[0] ≝
+ { iso_f:> unary_morphism A B;
+ f_closed: ∀x. x ∈ pc A S → fun1 ?? iso_f x ∈ pc B T}.
+
+
+ncheck (λA:?.
+ λB:?.
+ λS:?.
+ λT:?.
+ λxxx:isomorphism A B S T.
+ match xxx
+ return λxxx:isomorphism A B S T.
+ ∀x: carr A.
+ ∀x_72: mem (carr A) (pc A S) x.
+ mem (carr B) (pc B T) (fun1 A B ((λ_.?) A B S T xxx) x)
+ with [ mk_isomorphism _ yyy ⇒ yyy ] ).
+
+ ;
+ }.
+*)
+
+(* Set theory *)
+
+nlemma subseteq_intersection_l: ∀A.∀U,V,W:Ω^A.U ⊆ W ∨ V ⊆ W → U ∩ V ⊆ W.
+#A; #U; #V; #W; *; #H; #x; *; /2/.
+nqed.
+
+nlemma subseteq_union_l: ∀A.∀U,V,W:Ω^A.U ⊆ W → V ⊆ W → U ∪ V ⊆ W.
+#A; #U; #V; #W; #H; #H1; #x; *; /2/.
+nqed.
+
+nlemma subseteq_intersection_r: ∀A.∀U,V,W:Ω^A.W ⊆ U → W ⊆ V → W ⊆ U ∩ V.
+/3/. nqed.
+
+nlemma cupC : ∀S. ∀a,b:Ω^S.a ∪ b = b ∪ a.
+#S a b; @; #w; *; nnormalize; /2/; nqed.
+
+nlemma cupID : ∀S. ∀a:Ω^S.a ∪ a = a.
+#S a; @; #w; ##[*; //] /2/; nqed.
+
+(* XXX Bug notazione \cup, niente parentesi *)
+nlemma cupA : ∀S.∀a,b,c:Ω^S.a ∪ b ∪ c = a ∪ (b ∪ c).
+#S a b c; @; #w; *; /3/; *; /3/; nqed.
+
+ndefinition Empty_set : ∀A.Ω^A ≝ λA.{ x | False }.
+
+notation "∅" non associative with precedence 90 for @{ 'empty }.
+interpretation "empty set" 'empty = (Empty_set ?).
+
+nlemma cup0 :∀S.∀A:Ω^S.A ∪ ∅ = A.
+#S p; @; #w; ##[*; //| #; @1; //] *; nqed.