1-- Algebras over a commutative ring: a commutative ring with a structure map2-- from the base ring, their homomorphisms f : A →ₐ B, which are ring3-- homomorphisms commuting with the structure maps, and isomorphisms.4-- Part of Cumulus, the Grace17 mathematics library.5-- Copyright (c) 2026 Grant Passmore.6-- Released under the Apache License 2.0. See LICENSE in the repository root.7-- Authors: Grant Passmore89module Grace.Cumulus.Algebra.Algebras.Basic10import Grace.Cumulus.Algebra.Ring1112-- The definitions.1314-- An algebra over its base: the base ring, the ring, and the structure map15-- from the base into the ring.16structure Algebra17| base ring map : Set18where19 base_ring : CommRing(base)20 ring_ring : CommRing(ring)21 structure_map : map : base →+*ring2223def algebraCarrier (A : Set) : Set:= Ring.carrier(Algebra.ring(A))2425-- A homomorphism of algebras over one base: a ring homomorphism that26-- commutes with the structure maps.27predicate AlgebraHom (f A B : Set) :=28 Algebra(A) ∧ Algebra(B) ∧ Algebra.base(A) = Algebra.base(B) ∧29 RingHom(f, Algebra.ring(A), Algebra.ring(B)) ∧30 (∀ a ∈ Ring.carrier(Algebra.base(A)),31 f(Algebra.map(A)(a)) = Algebra.map(B)(a))3233predicate AlgebraIso (f A B : Set) :=34 AlgebraHom(f, A, B) ∧35 BijectiveBetween(f, algebraCarrier(A), algebraCarrier(B))3637notationguard"_ : _ →ₐ _"=> AlgebraHom38notationguard"_ : _ ≃ₐ _"=> AlgebraIso3940-- Restriction of scalars: an algebra over B is an algebra over K along a41-- homomorphism K →+* B.42def restrictScalars (A K ι : Set) : Set:=43 Algebra.mk(K, Algebra.ring(A),44 compose(Algebra.map(A), ι, Ring.carrier(K), Ring.carrier(Algebra.base(A)),45 algebraCarrier(A)))4647-- The members: what an algebra provides. The ring and the images of base48-- elements are instances, so that `typecheck`, `ring`, and the notation49-- place them without a destructuring of the algebra each time.50section Members51variable (A : Set)52hypothesis algebra : Algebra(A)5354instance Algebra.comm_ring55shows CommRing(Algebra.ring(A))56proof57exact algebra.right.right.left5859theorem Algebra.base_comm_ring60shows CommRing(Algebra.base(A))61proof62exact algebra.right.left6364theorem Algebra.structure_hom65shows Algebra.map(A) : Algebra.base(A) →+* Algebra.ring(A)66proof67exact algebra.right.right.right6869instance Algebra.map_mem (a : Set)70requires member : a ∈ Ring.carrier(Algebra.base(A))71shows Algebra.map(A)(a) ∈ Ring.carrier(Algebra.ring(A))72proof73have structureMap := Algebra.structure_hom with algebra74exact RingHom.map_mem with structureMap, member7576theorem Algebra.map_function77shows Algebra.map(A) : Ring.carrier(Algebra.base(A)) → algebraCarrier(A)78proof79obtain⟨source, target, function, rest⟩:= Algebra.structure_hom with algebra80unfold algebraCarrier81exact function8283end Members8485-- The projections of the restriction of scalars.86theorem restrictScalars.base (A K ι : Set)87shows Algebra.base(restrictScalars(A, K, ι)) = K88proof89simp [restrictScalars, Algebra.mk.base]9091theorem restrictScalars.ring (A K ι : Set)92shows Algebra.ring(restrictScalars(A, K, ι)) = Algebra.ring(A)93proof94simp [restrictScalars, Algebra.mk.ring]9596theorem restrictScalars.map (A K ι : Set)97shows Algebra.map(restrictScalars(A, K, ι)) =98 compose(Algebra.map(A), ι, Ring.carrier(K), Ring.carrier(Algebra.base(A)),99 algebraCarrier(A))100proof101simp [restrictScalars, Algebra.mk.map]102103theorem restrictScalars.carrier (A K ι : Set)104shows algebraCarrier(restrictScalars(A, K, ι)) = algebraCarrier(A)105proof106unfold algebraCarrier107rw restrictScalars.ring108109-- The parts of a homomorphism: its source and target algebras, the110-- common base, the underlying ring homomorphism and function, and the111-- commutation with the structure maps.112theorem AlgebraHom.source (f A B : Set)113requires hom : f : A →ₐ B114shows Algebra(A)115proof116exact hom.left117118theorem AlgebraHom.target (f A B : Set)119requires hom : f : A →ₐ B120shows Algebra(B)121proof122exact hom.right.left123124theorem AlgebraHom.base_eq (f A B : Set)125requires hom : f : A →ₐ B126shows Algebra.base(A) = Algebra.base(B)127proof128exact hom.right.right.left129130theorem AlgebraHom.ring_hom (f A B : Set)131requires hom : f : A →ₐ B132shows f : Algebra.ring(A) →+* Algebra.ring(B)133proof134exact hom.right.right.right.left135136theorem AlgebraHom.function (f A B : Set)137requires hom : f : A →ₐ B138shows f : algebraCarrier(A) → algebraCarrier(B)139proof140obtain⟨source, target, function, rest⟩:= AlgebraHom.ring_hom with hom141unfold algebraCarrier142exact function143144theorem AlgebraHom.map_base (f A B a : Set)145requires hom : f : A →ₐ B146requires member : a ∈ Ring.carrier(Algebra.base(A))147shows f(Algebra.map(A)(a)) = Algebra.map(B)(a)148proof149exact hom.right.right.right.right with a, member150151-- The category: identities and composites of homomorphisms.152theorem AlgebraHom.identity (A : Set)153requires algebra : Algebra(A)154shows identity(algebraCarrier(A)) : A →ₐ A155proof156have ringRing := Algebra.comm_ring with algebra157unfold AlgebraHom158refine⟨algebra, algebra, rfl, ?_, ?_⟩159 . unfold algebraCarrier160exact RingHom.id with ringRing161 . intros a aIn162have image := Algebra.map_mem with algebra, aIn163unfold algebraCarrier164exact identity.apply with image165166theorem AlgebraHom.compose (f g A B C : Set)167requires first : f : A →ₐ B168requires second : g : B →ₐ C169shows compose(g, f, algebraCarrier(A), algebraCarrier(B), algebraCarrier(C)) :170 A →ₐ C171proof172obtain⟨algebraA, algebraB, sameBase, homF, commuteF⟩:= first173obtain⟨algebraB2, algebraC, sameBase2, homG, commuteG⟩:= second174have ringRing := Algebra.comm_ring with algebraA175have ringHom := RingHom.comp with ringRing, homF, homG176have functionF := AlgebraHom.function with first177have functionG := AlgebraHom.function with second178unfold AlgebraHom179refine⟨algebraA, algebraC, ?_, ?_, ?_⟩180 . rw sameBase181exact sameBase2182 . unfold algebraCarrier183exact ringHom184 . intros a aIn185have image : Algebra.map(A)(a) ∈ algebraCarrier(A)186unfold algebraCarrier187exact Algebra.map_mem with algebraA, aIn188rw compose.apply with functionF, functionG, image189rw AlgebraHom.map_base with first, aIn190rw sameBase at aIn191exact AlgebraHom.map_base with second, aIn192193-- Isomorphisms: from a two-sided inverse, their inverses, composites,194-- and identities.195196-- Two homomorphisms inverse to each other on the carriers are isomorphisms.197theorem AlgebraIso.of_inverse (f g A B : Set)198requires forward : f : A →ₐ B199requires backward : g : B →ₐ A200requiresleft : ∀ x ∈ algebraCarrier(A), g(f(x)) = x201requiresright : ∀ y ∈ algebraCarrier(B), f(g(y)) = y202shows f : A ≃ₐ B203proof204have functionF := AlgebraHom.function with forward205have functionG := AlgebraHom.function with backward206have bijective := Bijective.of_two_sided_inverse with functionF, functionG,207left, right208unfold AlgebraIso209exact⟨forward, bijective⟩210211theorem AlgebraIso.inverse (f A B : Set)212requires iso : f : A ≃ₐ B213shows converse(f, algebraCarrier(A), algebraCarrier(B)) : B ≃ₐ A214proof215obtain⟨hom, bijective⟩:= iso216obtain⟨algebraA, algebraB, sameBase, ringHom, commute⟩:= hom217have ringRingA := Algebra.comm_ring with algebraA218have ringRingB := Algebra.comm_ring with algebraB219unfold algebraCarrier at bijective220have ringIso : f : A.ring ≃+* B.ring221unfold RingIso222exact⟨ringHom, bijective⟩223have inverseIso := RingIso.symm with ringRingA, ringRingB, ringIso224have inverseHom := RingIso.hom_of with inverseIso225unfold AlgebraIso226unfold algebraCarrier227constructor228 . unfold AlgebraHom229refine⟨algebraB, algebraA, ?_, inverseHom, ?_⟩230 . rw sameBase231 . intros a aIn232rw← sameBase at aIn233have image := Algebra.map_mem with algebraA, aIn234have forward := commute with a, aIn235rw← forward236exact Bijective.inverse_apply with bijective, image237 . exact converse.bijective with bijective238239-- The inverse of an isomorphism, named on the carriers.240def algebraInverse (f A B : Set) : Set:=241 converse(f, algebraCarrier(A), algebraCarrier(B))242243theorem AlgebraIso.inverse_hom (f A B : Set)244requires iso : f : A ≃ₐ B245shows algebraInverse(f, A, B) : B →ₐ A246proof247have inverse := AlgebraIso.inverse with iso248unfold algebraInverse249exact inverse.left250251theorem AlgebraIso.compose (f g A B C : Set)252requires first : f : A ≃ₐ B253requires second : g : B ≃ₐ C254shows compose(g, f, algebraCarrier(A), algebraCarrier(B), algebraCarrier(C)) :255 A ≃ₐ C256proof257obtain⟨homF, bijectiveF⟩:= first258obtain⟨homG, bijectiveG⟩:= second259have hom := AlgebraHom.compose with homF, homG260have bijective := compose.bijective with bijectiveF, bijectiveG261unfold AlgebraIso262exact⟨hom, bijective⟩263264theorem AlgebraIso.identity (A : Set)265requires algebra : Algebra(A)266shows identity(algebraCarrier(A)) : A ≃ₐ A267proof268have hom := AlgebraHom.identity with algebra269have bijective := identity.bijective at algebraCarrier(A)270unfold AlgebraIso271exact⟨hom, bijective⟩272273-- Isomorphisms compose, as existence statements.274theorem AlgebraIso.exists_compose (A B C : Set)275requires first : ∃ e, e : A ≃ₐ B276requires second : ∃ f, f : B ≃ₐ C277shows∃ g, g : A ≃ₐ C278proof279obtain⟨e, eIso⟩:= first280obtain⟨f, fIso⟩:= second281have composite := AlgebraIso.compose with eIso, fIso282witness compose(f, e, algebraCarrier(A), algebraCarrier(B), algebraCarrier(C))283exact composite284285-- Restriction of scalars is an algebra, and homomorphisms over B are286-- homomorphisms over K.287theorem restrictScalars.algebra (A K ι : Set)288requires algebra : Algebra(A)289requires base : CommRing(K)290requires scalars : ι : K →+* Algebra.base(A)291shows Algebra(restrictScalars(A, K, ι))292proof293have ringRing := Algebra.comm_ring with algebra294have structureMap := Algebra.structure_hom with algebra295have composite := RingHom.comp with base, scalars, structureMap296unfold restrictScalars297unfold algebraCarrier298exact Algebra.intro with base, ringRing, composite299300theorem AlgebraHom.restrict (f A B K ι : Set)301requires hom : f : A →ₐ B302requires base : CommRing(K)303requires scalars : ι : K →+* Algebra.base(A)304shows f : restrictScalars(A, K, ι) →ₐ restrictScalars(B, K, ι)305proof306obtain⟨algebraA, algebraB, sameBase, ringHom, commute⟩:= hom307have scalarsB : ι : K →+* Algebra.base(B)308rw← sameBase309exact scalars310have restrictedA := restrictScalars.algebra with algebraA, base, scalars311have restrictedB := restrictScalars.algebra with algebraB, base, scalarsB312obtain⟨sourceK, targetA, functionI, restI⟩:= scalars313obtain⟨sourceK2, targetB, functionIB, restIB⟩:= scalarsB314have functionA := Algebra.map_function with algebraA315have functionB := Algebra.map_function with algebraB316unfold AlgebraHom317refine⟨restrictedA, restrictedB, ?_, ?_, ?_⟩318 . rw [restrictScalars.base, restrictScalars.base]319 . rw [restrictScalars.ring, restrictScalars.ring]320exact ringHom321 . intros a aIn322rw restrictScalars.base at aIn323rw [restrictScalars.map, restrictScalars.map]324have image := RingHom.map_mem with scalars, aIn325rw compose.apply with functionI, functionA, aIn326rw compose.apply with functionIB, functionB, aIn327exact AlgebraHom.map_base with hom, image328329theorem AlgebraIso.restrict (f A B K ι : Set)330requires iso : f : A ≃ₐ B331requires base : CommRing(K)332requires scalars : ι : K →+* Algebra.base(A)333shows f : restrictScalars(A, K, ι) ≃ₐ restrictScalars(B, K, ι)334proof335obtain⟨hom, bijective⟩:= iso336have restricted := AlgebraHom.restrict with hom, base, scalars337unfold AlgebraIso338rw [restrictScalars.carrier, restrictScalars.carrier]339exact⟨restricted, bijective⟩340341-- A homomorphism into an algebra is one into any algebra with the same342-- base, ring, and structure map values.343theorem AlgebraHom.transfer (f X A B : Set)344requires hom : f : X →ₐ B345requires algebra : Algebra(A)346requires base : Algebra.base(B) = Algebra.base(A)347requiresring : Algebra.ring(B) = Algebra.ring(A)348requires map : ∀ a ∈ Ring.carrier(Algebra.base(A)),349 Algebra.map(B)(a) = Algebra.map(A)(a)350shows f : X →ₐ A351proof352obtain⟨algebraX, algebraB, sameBase, ringHom, commute⟩:= hom353unfold AlgebraHom354refine⟨algebraX, algebra, ?_, ?_, ?_⟩355 . rw sameBase356exact base357 . rw←ring358exact ringHom359 . intros a aIn360have value := commute with a, aIn361rw [sameBase, base] at aIn362rw value363exact map with a, aIn364