open import Algebra.Ring.Commutative
open import Algebra.Ring.Module
open import Algebra.Group.Ab
open import Algebra.Prelude
open import Algebra.Group
open import Algebra.Ring

open import Data.Fin.Product
open import Data.Fin.Base

import Algebra.Ring.Module.Multilinear

module Algebra.Ring.Module.Free {β} (R : Ring β) where


# Free modulesπ

For a finite set of generators, we can define free modules very directly: for example, using vectors. For infinite sets, this definition does not work as well: rings do not admit infinite sums, so we would need a way to βtameβ the vectors so only a finite amount of information needs to be summed at a time. The usual definition, of being zero in all but finitely many indices, is problematic since we can not (unless the indexing set admits decidable equality) define the map

Note: even if is not, strictly speaking, built on an underlying (sub)set of functions into we will stick to the exponential notation for definiteness.

private module R = Ring-on (R .snd)

infixr 30 _Β·_
infixl 25 _+_


Fortunately, free objects have a very straightforward definition in type theory: they are initial objects in certain categories of algebras, so they can be presented as1 inductive types.

data Free-mod {β'} (A : Type β') : Type (β β β') where
inc  : A β Free-mod A

_+_ : Free-mod A β Free-mod A β Free-mod A
neg : Free-mod A β Free-mod A
0m  : Free-mod A

_Β·_  : β R β β Free-mod A β Free-mod A


The free module on is generated by elements of (the inc) constructor, the operations of an Abelian group (which we write with _+_), together with a scalar multiplication operation The equations imposed are precisely those necessary to make _+_ into an abelian group, and the scalar multiplication into an action by We also add a squash constructor, since modules must have an underlying set.

  +-comm  : β x y   β x + y β‘ y + x
+-assoc : β x y z β x + (y + z) β‘ (x + y) + z
+-invl  : β x     β neg x + x β‘ 0m
+-idl   : β x     β 0m + x β‘ x

Β·-id       : β x     β R.1r Β· x      β‘ x
Β·-distribl : β x y z β x Β· (y + z)   β‘ x Β· y + x Β· z
Β·-distribr : β x y z β (x R.+ y) Β· z β‘ x Β· z + y Β· z
Β·-assoc    : β x y z β x Β· y Β· z     β‘ (x R.* y) Β· z

squash : is-set (Free-mod A)


In passing, we define a record to package together the data of a predicate on free modules: as long as it is prop-valued, we can prove something for all of by treating the group operations, the ring action, and the generators.

record Free-elim-prop {β' β''} {A : Type β'} (P : Free-mod A β Type β'')
: Type (β β β' β β'') where
no-eta-equality
field
has-is-prop : β x β is-prop (P x)
P-0m  : P 0m
P-neg : β x β P x β P (neg x)
P-inc : β x β P (inc x)
P-Β·   : β x y β P y β P (x Β· y)
P-+   : β x y β P x β P y β P (x + y)

elim : β x β P x

  elim (inc x) = P-inc x
elim (x Β· y) = P-Β· x y (elim y)
elim (x + y) = P-+ x y (elim x) (elim y)
elim (neg x) = P-neg x (elim x)
elim 0m = P-0m
elim (+-comm x y i) =
is-propβpathp (Ξ» j β has-is-prop (+-comm x y j))
(P-+ x y (elim x) (elim y)) (P-+ y x (elim y) (elim x)) i
elim (+-assoc x y z i) =
is-propβpathp (Ξ» j β has-is-prop (+-assoc x y z j))
(P-+ _ _ (elim x) (P-+ _ _ (elim y) (elim z)))
(P-+ _ _ (P-+ _ _ (elim x) (elim y)) (elim z)) i
elim (+-invl x i) =
is-propβpathp (Ξ» j β has-is-prop (+-invl x j))
(P-+ _ _ (P-neg _ (elim x)) (elim x)) P-0m i
elim (+-idl x i) =
is-propβpathp (Ξ» j β has-is-prop (+-idl x j))
(P-+ _ _ P-0m (elim x)) (elim x) i
elim (Β·-id x i)  =
is-propβpathp (Ξ» j β has-is-prop (Β·-id x j))
(P-Β· R.1r _ (elim x)) (elim x) i
elim (Β·-distribl x y z i) =
is-propβpathp (Ξ» j β has-is-prop (Β·-distribl x y z j))
(P-Β· x _ (P-+ _ _ (elim y) (elim z)))
(P-+ _ _ (P-Β· x _ (elim y)) (P-Β· x _ (elim z))) i
elim (Β·-distribr x y z i) =
is-propβpathp (Ξ» j β has-is-prop (Β·-distribr x y z j ))
(P-Β· (x R.+ y) _ (elim z))
(P-+ _ _ (P-Β· x _ (elim z)) (P-Β· y _ (elim z))) i
elim (Β·-assoc x y z i) =
is-propβpathp (Ξ» j β has-is-prop (Β·-assoc x y z j))
(P-Β· x (y Β· z) (P-Β· y _ (elim z)))
(P-Β· (x R.* y) z (elim z)) i
elim (squash x y p q i j) =
is-propβsquarep (Ξ» i j β has-is-prop (squash x y p q i j))
(Ξ» _ β elim x) (Ξ» j β elim (p j)) (Ξ» j β elim (q j)) (Ξ» _ β elim y) i j

Iβll leave the definition of the group, Abelian group, and structures on in this <details> tag, since theyβre not particularly interesting. For every operation and law, we simply use the corresponding constructors.
open Module-on hiding (_+_)
open make-module hiding (_+_)

Module-on-free-mod
: β {β'} (A : Type β')
β Module-on R (Free-mod A)
Module-on-free-mod A = to-module-on mk module Module-on-free-mod where
mk : make-module R (Free-mod A)
mk .has-is-set = squash
mk .make-module._+_ = _+_
mk .inv = neg
mk .0g = 0m
mk .make-module.+-assoc = Free-mod.+-assoc
mk .make-module.+-invl = Free-mod.+-invl
mk .make-module.+-idl = Free-mod.+-idl
mk .make-module.+-comm = Free-mod.+-comm
mk ._β_ = _Β·_
mk .β-distribl = Free-mod.Β·-distribl
mk .β-distribr = Free-mod.Β·-distribr
mk .β-assoc x y z = Free-mod.Β·-assoc x y z
mk .β-id = Free-mod.Β·-id

Free-Mod : β {β'} β Type β' β Module R (β β β')
Free-Mod T = to-module (Module-on-free-mod.mk T)

open Functor

fold-free-mod
: β {β β'} {A : Type β} (N : Module R β')
β (A β β N β)
β Linear-map (Free-Mod A) N
fold-free-mod {A = A} N f = go-linear module fold-free-mod where
private module N = Module-on (N .snd)


The endless constructors of Free-mod are powerless in the face of a function from the generators into the underlying type of an actual Each operation is mapped to the corresponding operation on so the path constructors are also handled by the witnesses that is an actual module. This function, annoying though it may be to write, is definitionally a linear map β saving us a bit of effort.

  -- Rough:
go : Free-mod A β β N β
go (inc x) = f x
go (x Β· y) = x N.β go y
go (x + y) = go x N.+ go y
go (neg x) = N.- (go x)
go 0m      = N.0g
go (+-comm x y i)       = N.+-comm {go x} {go y} i
go (+-assoc x y z i)    = N.+-assoc {go x} {go y} {go z} i
go (+-invl x i)         = N.+-invl {go x} i
go (+-idl x i)          = N.+-idl {go x} i
go (Β·-id x i)           = N.β-id (go x) i
go (Β·-distribl x y z i) = N.β-distribl x (go y) (go z) i
go (Β·-distribr x y z i) = N.β-distribr x y (go z) i
go (Β·-assoc x y z i)    = N.β-assoc x y (go z) i
go (squash a b p q i j) =
N.has-is-set (go a) (go b) (Ξ» i β go (p i)) (Ξ» i β go (q i)) i j

go-linear : Linear-map (Free-Mod A) N
go-linear .map = go
go-linear .lin .linear r s t = refl

{-# DISPLAY fold-free-mod.go = fold-free-mod #-}
{-# DISPLAY fold-free-mod.go-linear = fold-free-mod #-}

open Free-elim-prop

equal-on-basis
: β {βb βg} {T : Type βb} (M : Module R βg)
β {f g : Linear-map (Free-Mod T) M}
β ((x : T) β f .map (inc x) β‘ g .map (inc x))
β f β‘ g
equal-on-basis M {f} {g} p =
ext $Free-elim-prop.elim Ξ» where .has-is-prop x β M .fst .is-tr _ _ .P-0m β f.pres-0 β sym g.pres-0 .P-neg x Ξ± β f.pres-neg Β·Β· ap M.-_ Ξ± Β·Β· sym g.pres-neg .P-inc β p .P-Β· x y Ξ± β f.pres-β _ _ Β·Β· ap (x M.β_) Ξ± Β·Β· sym (g.pres-β _ _) .P-+ x y Ξ± Ξ² β f.pres-+ _ _ Β·Β· apβ M._+_ Ξ± Ξ² Β·Β· sym (g.pres-+ _ _) where module f = Linear-map f module g = Linear-map g module M = Module-on (M .snd) Extensional-linear-map-free : β {βb βg βr} {T : Type βb} {M : Module R βg} β β¦ ext : Extensional (T β β M β) βr β¦ β Extensional (Linear-map (Free-Mod T) M) βr Extensional-linear-map-free {M = M} β¦ ext β¦ = injectionβextensional! {f = Ξ» m x β m .map (inc x)} (Ξ» p β equal-on-basis M (happly p)) ext Extensional-hom-free : β {β' βr} {T : Type β'} {M : Module R (β β β')} β β¦ ext : Extensional (T β β M β) βr β¦ β Extensional (R-Mod.Hom (Free-Mod T) M) βr Extensional-hom-free {M = M} β¦ ef β¦ = injectionβextensional! {f = Ξ» m x β m # (inc x)} (Ξ» {f} {g} p β let it = equal-on-basis M {homβlinear-map f} {homβlinear-map g} (happly p) in ext (happly (ap map it))) ef instance extensionality-linear-map-free : β {βb βg} {T : Type βb} {M : Module R βg} β Extensionality (Linear-map (Free-Mod T) M) extensionality-linear-map-free = record { lemma = quote Extensional-linear-map-free } extensionality-hom-free : β {β'} {T : Type β'} {M : Module R (β β β')} β Extensionality (R-Mod.Hom {βm = β β β'} (Free-Mod T) M) extensionality-hom-free = record { lemma = quote Extensional-hom-free }  To prove that free modules have the expected universal property, it remains to show that if then Since weβre eliminating into a proposition, all we have to handle are the operation constructors, which is.. inductive, but manageable. Iβll leave the computation here if youβre interested: open make-left-adjoint make-free-module : β {β'} β make-left-adjoint (Forget-module R (β β β')) make-free-module {β'} = go where go : make-left-adjoint (Forget-structure (R-Mod-structure R)) go .free x = Free-Mod β£ x β£ go .unit x = Free-mod.inc go .universal {y = y} f = linear-mapβhom (fold-free-mod {β = β β β'} y f) go .commutes f = refl go .unique {y = y} {f = f} {g = g} p = reext! p  After that calculation, we can β¨ just β¨ conclude that Free-module has the right universal property: that is, we can rearrange the proof above into the form of a functor and an adjunction. Free-module : β {β'} β Functor (Sets (β β β')) (R-Mod R (β β β')) Free-module {β' = β'} = make-left-adjoint.to-functor (make-free-module {β' = β'}) Freeβ£Forget : β {β'} β Free-module {β'} β£ Forget-module R (β β β') Freeβ£Forget {β'} = make-left-adjoint.to-left-adjoint (make-free-module {β' = β'})  equal-on-basis' : β {βb βg} {T : Type βb} {G : Type βg} (M : Module-on R G) β (let module M = Module-on M) β {f : Free-mod T β G} β (β r x y β f (r Β· x + y) β‘ r M.β f x M.+ f y) β {g : Free-mod T β G} β (β r x y β g (r Β· x + y) β‘ r M.β g x M.+ g y) β ((x : T) β f (inc x) β‘ g (inc x)) β f β‘ g equal-on-basis' M l1 l2 p = ap map$
equal-on-basis (el _ (Module-on.has-is-set M) , M)
{f = record { lin = record { linear = l1 } }}
{g = record { lin = record { linear = l2 } }}
p

module _ (cring : is-commutative-ring R) where
open Algebra.Ring.Module.Multilinear R cring

multilinear-extension
: β {n} {ββ}
{ββ : Fin (suc n) β Level} {Ms : (i : Fin (suc n)) β Type (ββ i)} {N : Module R ββ}
β (f : ArrαΆ  Ms β N β)
β Multilinear-map (suc n) (Ξ» i β Free-Mod (Ms i)) N
multilinear-extension {zero} {N = N} f = 1-linear-map (fold-free-mod N f)
multilinear-extension {suc n} f = Uncurry.from $fold-free-mod _ Ξ» x β multilinear-extension (f x) multi-equal-on-bases : β {n} {ββ} {ββ : Fin n β Level} {Ms : (i : Fin n) β Type (ββ i)} {N : Module R ββ} β {f g : Multilinear-map n (Ξ» i β Free-Mod (Ms i)) N} β (β (as : Ξ αΆ Ms) β applyαΆ (f .map) (mapβ (Ξ» _ β inc) as) β‘ applyαΆ (g .map) (mapβ (Ξ» _ β inc) as)) β f β‘ g multi-equal-on-bases {n = zero} p = Multilinear-map-path (p tt) multi-equal-on-bases {n = suc n} {f = f} {g} p = Uncurry.injective$ equal-on-basis _ Ξ» x β
multi-equal-on-bases Ξ» as β
p (x , as)