+include "hints_declaration.ma".
+
+alias symbol "hint_decl" = "hint_decl_Type2".
+unification hint 0 ≔ A ⊢ carr1 (mk_setoid1 (Ω^A) (eq1 (powerclass_setoid A))) ≡ Ω^A.
+
+(************ SETS OVER SETOIDS ********************)
+
+include "logic/cprop.ma".
+
+nrecord qpowerclass (A: setoid) : Type[1] ≝
+ { pc:> Ω^A; (* qui pc viene dichiarato con un target preciso...
+ forse lo si vorrebbe dichiarato con un target più lasco
+ ma la sintassi :> non lo supporta *)
+ mem_ok': ∀x,x':A. x=x' → (x ∈ pc) = (x' ∈ pc)
+ }.
+
+notation > "𝛀 ^ term 90 A" non associative with precedence 70
+for @{ 'qpowerclass $A }.
+
+notation "Ω term 90 A \atop ≈" non associative with precedence 70
+for @{ 'qpowerclass $A }.
+
+interpretation "qpowerclass" 'qpowerclass a = (qpowerclass a).
+
+ndefinition Full_set: ∀A. 𝛀^A.
+ #A; @[ napply A | #x; #x'; #H; napply refl1]
+nqed.
+ncoercion Full_set: ∀A. qpowerclass A ≝ Full_set on A: setoid to qpowerclass ?.
+
+ndefinition qseteq: ∀A. equivalence_relation1 (𝛀^A).
+ #A; @
+ [ napply (λS,S'. S = S')
+ | #S; napply (refl1 ? (seteq A))
+ | #S; #S'; napply (sym1 ? (seteq A))
+ | #S; #T; #U; napply (trans1 ? (seteq A))]
+nqed.
+
+ndefinition qpowerclass_setoid: setoid → setoid1.
+ #A; @
+ [ napply (qpowerclass A)
+ | napply (qseteq A) ]
+nqed.
+
+unification hint 0 ≔ A ⊢
+ carr1 (mk_setoid1 (𝛀^A) (eq1 (qpowerclass_setoid A)))
+≡ qpowerclass A.
+
+ncoercion pc' : ∀A.∀x:qpowerclass_setoid A. Ω^A ≝ pc
+on _x : (carr1 (qpowerclass_setoid ?)) to (Ω^?).
+
+nlemma mem_ok: ∀A. binary_morphism1 (setoid1_of_setoid A) (qpowerclass_setoid A) CPROP.
+ #A; @
+ [ napply (λx,S. x ∈ S)
+ | #a; #a'; #b; #b'; #Ha; *; #Hb1; #Hb2; @; #H;
+ ##[ napply Hb1; napply (. (mem_ok' …)); ##[##3: napply H| napply Ha^-1;##]
+ ##| napply Hb2; napply (. (mem_ok' …)); ##[##3: napply H| napply Ha;##]
+ ##]
+ ##]
+nqed.
+
+unification hint 0 ≔ A:setoid, x, S;
+ SS ≟ (pc ? S),
+ TT ≟ (mk_binary_morphism1 ???
+ (λx:setoid1_of_setoid ?.λS:qpowerclass_setoid ?. x ∈ S)
+ (prop21 ??? (mem_ok A)))
+
+ (*-------------------------------------*) ⊢
+ fun21 ? ? ? TT x S
+ ≡ mem A SS x.
+
+nlemma subseteq_ok: ∀A. binary_morphism1 (qpowerclass_setoid A) (qpowerclass_setoid A) CPROP.
+ #A; @
+ [ napply (λS,S'. S ⊆ S')
+ | #a; #a'; #b; #b'; *; #Ha1; #Ha2; *; #Hb1; #Hb2; @; #H
+ [ napply (subseteq_trans … a)
+ [ nassumption | napply (subseteq_trans … b); nassumption ]
+ ##| napply (subseteq_trans … a')
+ [ nassumption | napply (subseteq_trans … b'); nassumption ] ##]
+nqed.
+
+unification hint 0 ≔ A,a,a'
+ (*-----------------------------------------------------------------*) ⊢
+ eq_rel ? (eq A) a a' ≡ eq_rel1 ? (eq1 (setoid1_of_setoid A)) a a'.
+
+nlemma intersect_ok: ∀A. 𝛀^A → 𝛀^A → 𝛀^A.
+ #A; #S; #S'; @ (S ∩ S');
+ #a; #a'; #Ha; @; *; #H1; #H2; @
+ [##1,2: napply (. Ha^-1‡#); nassumption;
+##|##3,4: napply (. Ha‡#); nassumption]
+nqed.
+
+alias symbol "hint_decl" = "hint_decl_Type1".
+unification hint 1 ≔
+ A : setoid, B,C : qpowerclass A ⊢
+ pc A (mk_qpowerclass ? (B ∩ C) (mem_ok' ? (intersect_ok ? B C)))
+ ≡ intersect ? (pc ? B) (pc ? C).
+
+nlemma intersect_ok': ∀A. binary_morphism1 (powerclass_setoid A) (powerclass_setoid A) (powerclass_setoid A).
+ #A; @ (λS,S'. S ∩ S');
+ #a; #a'; #b; #b'; *; #Ha1; #Ha2; *; #Hb1; #Hb2; @; #x; nnormalize; *; #Ka; #Kb; @
+ [ napply Ha1; nassumption
+ | napply Hb1; nassumption
+ | napply Ha2; nassumption
+ | napply Hb2; nassumption]
+nqed.
+
+alias symbol "hint_decl" = "hint_decl_Type1".
+unification hint 0 ≔
+ A : Type[0], B,C : powerclass A ⊢
+ fun21 …
+ (mk_binary_morphism1 …
+ (λS,S'.S ∩ S')
+ (prop21 … (intersect_ok' A))) B C
+ ≡ intersect ? B C.
+
+ndefinition prop21_mem :
+ ∀A,C.∀f:binary_morphism1 (setoid1_of_setoid A) (qpowerclass_setoid A) C.
+ ∀a,a':setoid1_of_setoid A.
+ ∀b,b':qpowerclass_setoid A.a = a' → b = b' → f a b = f a' b'.
+#A; #C; #f; #a; #a'; #b; #b'; #H1; #H2; napply prop21; nassumption;
+nqed.
+
+interpretation "prop21 mem" 'prop2 l r = (prop21_mem ??????? l r).
+
+nlemma intersect_ok'':
+ ∀A. binary_morphism1 (qpowerclass_setoid A) (qpowerclass_setoid A) (qpowerclass_setoid A).
+ #A; @ (intersect_ok A); nlapply (prop21 … (intersect_ok' A)); #H;
+ #a; #a'; #b; #b'; #H1; #H2; napply H; nassumption;
+nqed.
+
+unification hint 1 ≔
+ A:?, B,C : 𝛀^A ⊢
+ fun21 …
+ (mk_binary_morphism1 …
+ (λS,S':qpowerclass_setoid A.S ∩ S')
+ (prop21 … (intersect_ok'' A))) B C
+ ≡ intersect ? B C.
+
+
+
+
+nlemma test: ∀U.∀A,B:qpowerclass U. A ∩ B = A →
+ ∀x,y. x=y → x ∈ A → y ∈ A ∩ B.
+ #U; #A; #B; #H; #x; #y; #K; #K2; napply (. #‡(?));
+##[ nchange with (A ∩ B = ?);
+ napply (prop21 ??? (mk_binary_morphism1 … (λS,S'.S ∩ S') (prop21 … (intersect_ok' U))) A A B B ##);
+ #H; napply H;
+ nassumption;
+nqed.
+
+(*
+nlemma intersect_ok: ∀A. binary_morphism1 (qpowerclass_setoid A) (qpowerclass_setoid A) (qpowerclass_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".
+ 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 : qpowerclass 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) (eq 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. unary_morphism A B → compatible_equivalence_relation A.
+ #A; #B; #f; @
+ [ @
+ [ napply (λx,y. f x = f y)
+ | #x; napply refl | #x; #y; napply sym | #x; #y; #z; napply trans]
+##| #x; #x'; #H; nwhd; alias symbol "prop1" = "prop1".
+napply (.= (†H)); napply refl ]
+nqed.
+
+ndefinition canonical_proj: ∀A,R. unary_morphism A (quotient A R).
+ #A; #R; @
+ [ napply (λx.x) | #a; #a'; #H; napply (compatibility … R … H) ]
+nqed.
+
+ndefinition quotiented_mor:
+ ∀A,B.∀f:unary_morphism A B.
+ unary_morphism (quotient … (eqrel_of_morphism … f)) B.
+ #A; #B; #f; @
+ [ napply f | #a; #a'; #H; nassumption]
+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).
+ #A; #B; #f; #x; napply refl;
+nqed.
+
+ndefinition surjective ≝
+ λA,B.λS: qpowerclass A.λT: qpowerclass B.λf:unary_morphism A B.
+ ∀y. y ∈ T → ∃x. x ∈ S ∧ f x = y.
+
+ndefinition injective ≝
+ λA,B.λS: qpowerclass A.λf:unary_morphism A B.
+ ∀x,x'. x ∈ S → x' ∈ S → f x = f x' → x = x'.
+
+nlemma first_omomorphism_theorem_functions2:
+ ∀A,B.∀f: unary_morphism A B.
+ surjective … (Full_set ?) (Full_set ?) (canonical_proj ? (eqrel_of_morphism … f)).
+ #A; #B; #f; nwhd; #y; #Hy; @ y; @ I ; napply refl;
+ (* bug, prova @ I refl *)
+nqed.
+
+nlemma first_omomorphism_theorem_functions3:
+ ∀A,B.∀f: unary_morphism A B.
+ injective … (Full_set ?) (quotiented_mor … f).
+ #A; #B; #f; nwhd; #x; #x'; #Hx; #Hx'; #K; nassumption.
+nqed.