-- Quotients of algebras by ideals: the quotient algebra, its projection,
-- and the lift of a homomorphism that kills the ideal, in particular an
-- ideal generated by finitely many elements the homomorphism sends to zero.
-- Part of Cumulus, the Grace17 mathematics library.
-- Copyright (c) 2026 Grant Passmore.
-- Released under the Apache License 2.0. See LICENSE in the repository root.
-- Authors: Grant Passmore

module Grace.Cumulus.Algebra.Algebras.Quotient
import Grace.Cumulus.Algebra.Algebras.Basic
import Grace.Cumulus.Algebra.Ring

-- The definitions.

-- The projection of the ring of A onto its quotient by J.
def algebraProjection (A J : Set) : Set :=
  quotientProjection(algebraCarrier(A), quotientRelation(Algebra.ring(A), J))

-- The quotient algebra: the quotient ring with the projected structure map.
def quotientAlgebra (A J : Set) : Set :=
  Algebra.mk(Algebra.base(A), quotientRing(Algebra.ring(A), J),
    compose(algebraProjection(A, J), Algebra.map(A),
      Ring.carrier(Algebra.base(A)), algebraCarrier(A),
      Ring.carrier(quotientRing(Algebra.ring(A), J))))

-- The homomorphism a quotient inherits from one that kills the ideal.
def algebraLift (f A B J : Set) : Set :=
  quotientMap(f, algebraCarrier(A), algebraCarrier(B),
    quotientRelation(Algebra.ring(A), J))

-- The projections of the quotient algebra.
theorem quotientAlgebra.baseOf (A J : Set)
  shows Algebra.base(quotientAlgebra(A, J)) = Algebra.base(A)
proof
  simp [quotientAlgebra, Algebra.mk.base]

theorem quotientAlgebra.ringOf (A J : Set)
  shows Algebra.ring(quotientAlgebra(A, J)) = quotientRing(Algebra.ring(A), J)
proof
  simp [quotientAlgebra, Algebra.mk.ring]

theorem quotientAlgebra.mapOf (A J : Set)
  shows Algebra.map(quotientAlgebra(A, J)) =
    compose(algebraProjection(A, J), Algebra.map(A),
      Ring.carrier(Algebra.base(A)), algebraCarrier(A),
      Ring.carrier(quotientRing(Algebra.ring(A), J)))
proof
  simp [quotientAlgebra, Algebra.mk.map]

theorem quotientAlgebra.carrierOf (A J : Set)
  shows algebraCarrier(quotientAlgebra(A, J)) =
    Ring.carrier(quotientRing(Algebra.ring(A), J))
proof
  unfold algebraCarrier
  rw quotientAlgebra.ringOf

section Quotients
variable (A J : Set)
hypothesis algebra : Algebra(A)
hypothesis ideal : Ideal(Algebra.ring(A), J)

-- The projection: a ring homomorphism onto the quotient, sending x to its
-- class, and killing exactly the ideal.
theorem algebraProjection.ring_hom
  shows algebraProjection(A, J) :
    Algebra.ring(A) →+* quotientRing(Algebra.ring(A), J)
proof
  have ringRing := Algebra.comm_ring with algebra
  have projection := quotientRing.projection_hom with ringRing, ideal
  unfold algebraProjection
  unfold algebraCarrier
  exact projection

theorem algebraProjection.function
  shows algebraProjection(A, J) : algebraCarrier(A) →
    Ring.carrier(quotientRing(Algebra.ring(A), J))
proof
  obtain ⟨source, target, function, rest⟩ :=
    algebraProjection.ring_hom with algebra, ideal
  unfold algebraCarrier
  exact function

theorem algebraProjection.value (x : Set)
  requires member : x ∈ algebraCarrier(A)
  shows algebraProjection(A, J)(x) =
    ⟦x⟧[algebraCarrier(A), quotientRelation(Algebra.ring(A), J)]
proof
  have ringRing := Algebra.comm_ring with algebra
  have equivalence := quotientRelation.equivalence with ringRing, ideal
  unfold algebraCarrier at member
  unfold algebraProjection
  unfold algebraCarrier
  exact quotientProjection.value with equivalence, member

theorem algebraProjection.kills (x : Set)
  requires member : x ∈ J
  shows algebraProjection(A, J)(x) =
    Ring.zero(quotientRing(Algebra.ring(A), J))
proof
  have ringRing := Algebra.comm_ring with algebra
  have inCarrier := Ideal.mem_carrier at Algebra.ring(A), J, x with *
  have inAlgebra : x ∈ algebraCarrier(A)
    unfold algebraCarrier
    exact inCarrier
  have value := algebraProjection.value with algebra, ideal, inAlgebra
  have criterion := quotientRing.class_eq_zero_iff with ringRing, ideal,
    inCarrier
  rw [value, quotientRing.zeroOf]
  unfold algebraCarrier
  exact criterion.backward with member

theorem algebraProjection.zero_iff (x : Set)
  requires member : x ∈ algebraCarrier(A)
  shows algebraProjection(A, J)(x) =
      Ring.zero(quotientRing(Algebra.ring(A), J)) ↔
    x ∈ J
proof
  have ringRing := Algebra.comm_ring with algebra
  have value := algebraProjection.value with algebra, ideal, member
  rw [value, quotientRing.zeroOf]
  unfold algebraCarrier at member
  unfold algebraCarrier
  exact quotientRing.class_eq_zero_iff with ringRing, ideal, member

-- The quotient algebra, with the projection as a surjective homomorphism
-- onto it.
theorem quotientAlgebra.algebra
  shows Algebra(quotientAlgebra(A, J))
proof
  have baseRing := Algebra.base_comm_ring with algebra
  have ringRing := Algebra.comm_ring with algebra
  have structureMap := Algebra.structure_hom with algebra
  have quotientRing := quotientRing.comm_ring with ringRing, ideal
  have projection := quotientRing.projection_hom with ringRing, ideal
  have composite := RingHom.comp with baseRing, structureMap, projection
  unfold quotientAlgebra
  unfold algebraProjection
  unfold algebraCarrier
  exact Algebra.intro with baseRing, quotientRing, composite

theorem algebraProjection.hom
  shows algebraProjection(A, J) : A →ₐ quotientAlgebra(A, J)
proof
  have quotient := quotientAlgebra.algebra with algebra, ideal
  have mapFunction := Algebra.map_function with algebra
  have projFunction := algebraProjection.function with algebra, ideal
  have projection := algebraProjection.ring_hom with algebra, ideal
  unfold AlgebraHom
  refine ⟨algebra, quotient, ?_, ?_, ?_⟩
  . symm
    exact quotientAlgebra.baseOf at A, J
  . rw quotientAlgebra.ringOf
    exact projection
  . intros a ha
    rw quotientAlgebra.mapOf
    symm
    exact compose.apply with mapFunction, projFunction, ha

theorem algebraProjection.surjective
  shows SurjectiveBetween(algebraProjection(A, J), algebraCarrier(A),
    algebraCarrier(quotientAlgebra(A, J)))
proof
  have ringRing := Algebra.comm_ring with algebra
  have onto := quotientRing.projection_surjective with ringRing, ideal
  rw quotientAlgebra.carrierOf
  unfold algebraProjection
  unfold algebraCarrier
  exact onto

-- Every element of the quotient is the class of an element of A.
theorem quotientAlgebra.representative (x : Set)
  requires member : x ∈ algebraCarrier(quotientAlgebra(A, J))
  shows ∃ y ∈ algebraCarrier(A), x = algebraProjection(A, J)(y)
proof
  have onto := algebraProjection.surjective with algebra, ideal
  obtain ⟨y, yIn, related⟩ := onto.right with x, member
  have value := FunctionBetween.apply_value with onto.left, yIn, related
  witness y
  refine ⟨yIn, ?_⟩
  rw value

-- The lift. A homomorphism killing J has J inside its kernel, hence
-- respects the congruence modulo J; its lift sends the class of x to
-- f(x), is a homomorphism out of the quotient, and factors f through
-- the projection.
theorem AlgebraHom.kernel_of_killed (f B : Set)
  requires hom : f : A →ₐ B
  requires killed : ∀ x ∈ J, f(x) = Ring.zero(Algebra.ring(B))
  shows J ⊆ ringKernel(f, Ring.carrier(Algebra.ring(A)),
    Ring.zero(Algebra.ring(B)))
proof
  intros x hx
  have inCarrier := Ideal.mem_carrier at Algebra.ring(A), J, x with *
  have vanishes := killed with x, hx
  exact ringKernel.membership.backward with ⟨inCarrier, vanishes⟩

theorem AlgebraHom.respects_of_killed (f B : Set)
  requires hom : f : A →ₐ B
  requires killed : ∀ x ∈ J, f(x) = Ring.zero(Algebra.ring(B))
  shows Respects(f, algebraCarrier(A), quotientRelation(Algebra.ring(A), J))
proof
  have targetAlgebra := AlgebraHom.target with hom
  have sourceParts := CommRing.parts with (Algebra.comm_ring with algebra)
  have targetParts := CommRing.parts with (Algebra.comm_ring with targetAlgebra)
  have homParts := RingHom.parts with (AlgebraHom.ring_hom with hom)
  have kernel := AlgebraHom.kernel_of_killed with algebra, ideal, hom, killed
  rw quotientRelation.value
  unfold algebraCarrier
  exact IsRingHom.respects_ideal with sourceParts, targetParts, homParts, kernel

theorem algebraLift.class (f B x : Set)
  requires hom : f : A →ₐ B
  requires killed : ∀ x ∈ J, f(x) = Ring.zero(Algebra.ring(B))
  requires member : x ∈ algebraCarrier(A)
  shows algebraLift(f, A, B, J)(
    ⟦x⟧[algebraCarrier(A), quotientRelation(Algebra.ring(A), J)]) = f(x)
proof
  have ringRing := Algebra.comm_ring with algebra
  have function := AlgebraHom.function with hom
  have equivalence := quotientRelation.equivalence with ringRing, ideal
  have respects := AlgebraHom.respects_of_killed with algebra, ideal, hom,
    killed
  unfold algebraLift
  unfold algebraCarrier at function
  unfold algebraCarrier at respects
  unfold algebraCarrier at member
  unfold algebraCarrier
  exact quotientMap.class with function, equivalence, respects, member

theorem AlgebraHom.lift (f B : Set)
  requires hom : f : A →ₐ B
  requires killed : ∀ x ∈ J, f(x) = Ring.zero(Algebra.ring(B))
  shows algebraLift(f, A, B, J) : quotientAlgebra(A, J) →ₐ B
proof
  have quotient := quotientAlgebra.algebra with algebra, ideal
  have targetAlgebra := AlgebraHom.target with hom
  have sameBase := AlgebraHom.base_eq with hom
  have ringRing := Algebra.comm_ring with algebra
  have targetRing := Algebra.comm_ring with targetAlgebra
  have ringHom := AlgebraHom.ring_hom with hom
  have kernel := AlgebraHom.kernel_of_killed with algebra, ideal, hom, killed
  have descended := RingHom.quotient_hom with ringRing, targetRing, ringHom,
    ideal, kernel
  have mapFunction := Algebra.map_function with algebra
  have projFunction := algebraProjection.function with algebra, ideal
  unfold AlgebraHom
  refine ⟨quotient, targetAlgebra, ?_, ?_, ?_⟩
  . rw quotientAlgebra.baseOf
    exact sameBase
  . rw quotientAlgebra.ringOf
    unfold algebraLift
    unfold algebraCarrier
    exact descended
  . intros a ha
    rw quotientAlgebra.baseOf at ha
    have composed := compose.apply with mapFunction, projFunction, ha
    have mapped := FunctionBetween.apply_closed with mapFunction, ha
    have projected := algebraProjection.value with algebra, ideal, mapped
    have lifted := algebraLift.class with algebra, ideal, hom, killed, mapped
    rw [quotientAlgebra.mapOf, composed, projected, lifted]
    exact AlgebraHom.map_base with hom, ha

theorem AlgebraHom.lift_factors (f B x : Set)
  requires hom : f : A →ₐ B
  requires killed : ∀ x ∈ J, f(x) = Ring.zero(Algebra.ring(B))
  requires member : x ∈ algebraCarrier(A)
  shows algebraLift(f, A, B, J)(algebraProjection(A, J)(x)) = f(x)
proof
  rw algebraProjection.value with algebra, ideal, member
  exact algebraLift.class with algebra, ideal, hom, killed, member

-- Homomorphisms out of a quotient are determined by their composites with
-- the projection.
theorem AlgebraHom.quotient_ext (g h B : Set)
  requires first : g : quotientAlgebra(A, J) →ₐ B
  requires second : h : quotientAlgebra(A, J) →ₐ B
  requires agree : ∀ x ∈ algebraCarrier(A),
    g(algebraProjection(A, J)(x)) = h(algebraProjection(A, J)(x))
  shows g = h
proof
  have ringRing := Algebra.comm_ring with algebra
  have gFunction := AlgebraHom.function with first
  have hFunction := AlgebraHom.function with second
  apply FunctionBetween.ext with gFunction, hFunction
  intros X hX
  rw quotientAlgebra.carrierOf at hX
  obtain ⟨x, hx, shape⟩ := quotientRing.representative with ringRing, ideal,
    hX
  have inAlgebra : x ∈ algebraCarrier(A)
    unfold algebraCarrier
    exact hx
  have projected := algebraProjection.value with algebra, ideal, inAlgebra
  have same := agree with x, inAlgebra
  rw projected at same
  unfold algebraCarrier at same
  rw shape
  exact same

end Quotients

-- Ideals generated by a set of elements: a homomorphism killing the
-- generators kills the ideal, so it lifts to the quotient, and the
-- projection kills each generator.
section Generated
variable (A G : Set)
hypothesis algebra : Algebra(A)
hypothesis generators : G ⊆ algebraCarrier(A)

theorem generatedIdeal.subset_kernel (f B : Set)
  requires hom : f : A →ₐ B
  requires killed : ∀ g ∈ G, f(g) = Ring.zero(Algebra.ring(B))
  shows ∀ x ∈ generatedIdeal(Algebra.ring(A), G),
    f(x) = Ring.zero(Algebra.ring(B))
proof
  have targetAlgebra := AlgebraHom.target with hom
  have ringRing := Algebra.comm_ring with algebra
  have targetRing := Algebra.comm_ring with targetAlgebra
  have ringHom := AlgebraHom.ring_hom with hom
  have kernelIdeal := ringKernel.ideal with ringRing, targetRing, ringHom
  have includes : G ⊆ ringKernel(f, Ring.carrier(Algebra.ring(A)),
      Ring.zero(Algebra.ring(B)))
    intros g hg
    have inCarrier := generators with g, hg
    unfold algebraCarrier at inCarrier
    have vanishes := killed with g, hg
    exact ringKernel.membership.backward with ⟨inCarrier, vanishes⟩
  have least := generatedIdeal.least with ringRing, kernelIdeal, includes
  intros x hx
  have inKernel := least with x, hx
  have data := ringKernel.membership.forward with inKernel
  exact data.right

theorem AlgebraHom.lift_generated (f B : Set)
  requires hom : f : A →ₐ B
  requires killed : ∀ g ∈ G, f(g) = Ring.zero(Algebra.ring(B))
  shows algebraLift(f, A, B, generatedIdeal(Algebra.ring(A), G)) :
    quotientAlgebra(A, generatedIdeal(Algebra.ring(A), G)) →ₐ B
proof
  have ringRing := Algebra.comm_ring with algebra
  have ideal := generatedIdeal.ideal at _, G with ringRing
  have killedAll := generatedIdeal.subset_kernel with algebra, generators, hom,
    killed
  exact AlgebraHom.lift with algebra, ideal, hom, killedAll

theorem AlgebraHom.lift_generated_factors (f B x : Set)
  requires hom : f : A →ₐ B
  requires killed : ∀ g ∈ G, f(g) = Ring.zero(Algebra.ring(B))
  requires member : x ∈ algebraCarrier(A)
  shows algebraLift(f, A, B, generatedIdeal(Algebra.ring(A), G))(
    algebraProjection(A, generatedIdeal(Algebra.ring(A), G))(x)) = f(x)
proof
  have ringRing := Algebra.comm_ring with algebra
  have ideal := generatedIdeal.ideal at _, G with ringRing
  have killedAll := generatedIdeal.subset_kernel with algebra, generators, hom,
    killed
  exact AlgebraHom.lift_factors with algebra, ideal, hom, killedAll, member

theorem algebraProjection.kills_generator (g : Set)
  requires member : g ∈ G
  shows algebraProjection(A, generatedIdeal(Algebra.ring(A), G))(g) =
    Ring.zero(quotientRing(Algebra.ring(A), generatedIdeal(Algebra.ring(A), G)))
proof
  have ringRing := Algebra.comm_ring with algebra
  have ideal := generatedIdeal.ideal at _, G with ringRing
  have bounded : G ⊆ Ring.carrier(Algebra.ring(A))
    unfold algebraCarrier at generators
    exact generators
  have contains := generatedIdeal.contains with ringRing, bounded
  have inIdeal := contains with g, member
  exact algebraProjection.kills with algebra, ideal, inIdeal

end Generated
