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

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 Bβ†’RBB \to R^B.

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

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 AA is generated by elements of AA (the inc) constructor, the operations of an Abelian group (which we write with _+_), together with a scalar multiplication operation R→RA→RAR \to R^A \to R^A. The equations imposed are precisely those necessary to make _+_ into an abelian group, and the scalar multiplication into an action by RR. 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
  Β·-add-r : βˆ€ x y z β†’ x Β· (y + z)   ≑ x Β· y + x Β· z
  Β·-add-l : βˆ€ 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 RAR^A 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
I’ll leave the definition of the group, Abelian group, and RR-module structures on RAR^A in this <details> tag, since they’re not particularly interesting. For every operation and law, we simply use the corresponding constructors.
open Module-on using (_⋆_ ; ⋆-id ; ⋆-add-r ; ⋆-add-l ; ⋆-assoc)
open make-group

Group-on-free-mod : βˆ€ {β„“β€²} {A : Type β„“β€²} β†’ Group-on (Free-mod A)
Group-on-free-mod = to-group-on Ξ» where
  .group-is-set β†’ squash
  .unit   β†’ 0m
  .mul    β†’ _+_
  .inv    β†’ neg
  .assoc  β†’ +-assoc
  .invl   β†’ +-invl
  .invr x β†’ +-comm x (neg x) βˆ™ +-invl _
  .idl    β†’ +-idl

Free-mod-ab-group : βˆ€ {β„“β€²} {A : Type β„“β€²} β†’ AbGroup _
Free-mod-ab-group {A = A} .object =
  el (Free-mod A) squash ,
  Group-on-free-mod
Free-mod-ab-group .witness = +-comm


Module-on-free-mod
  : βˆ€ {β„“β€²} {A : Type β„“β€²}
  β†’ Module-on R (Free-mod-ab-group {A = A})
Module-on-free-mod ._⋆_ = _Β·_
Module-on-free-mod .⋆-id = Β·-id
Module-on-free-mod .⋆-add-r = Β·-add-r
Module-on-free-mod .⋆-add-l = Β·-add-l
Module-on-free-mod .⋆-assoc = Β·-assoc

Free-Mod : βˆ€ {β„“β€²} β†’ Type β„“β€² β†’ Module (β„“ βŠ” β„“β€²) R
Free-Mod x .fst = Free-mod-ab-group {A = x}
Free-Mod x .snd = Module-on-free-mod

open Functor
fold-free-mod
  : βˆ€ {β„“ β„“β€²} {A : Type β„“} {G : AbGroup β„“β€²} (N : Module-on R G)
  β†’ (A β†’ AbGrp.β‚€ G)
  β†’ Linear-map (Free-Mod A) (_ , N) Rings.id
fold-free-mod {A = A} {G} N f = go-linear module fold-free-mod where
  private module N = Module (_ , N)

The endless constructors of Free-mod are powerless in the face of a function from the generators into the underlying type of an actual RR-module NN. Each operation is mapped to the corresponding operation on NN, so the path constructors are also handled by the witnesses that NN 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.Gβ‚€
  go (inc x) = f x
  go (x Β· y) = x N.⋆ go y
  go (x + y) = go x N.+ go y
  go (neg x) = N.G.inverse (go x)
  go 0m      = N.G.unit
  go (+-comm x y i)    = N.G.commutative {go x} {go y} i
  go (+-assoc x y z i) = N.G.associative {go x} {go y} {go z} (~ i)
  go (+-invl x i)      = N.G.inversel {go x} i
  go (+-idl x i)       = N.G.idl {go x} i
  go (Β·-id x i)        = N.⋆-id (go x) i
  go (Β·-add-r x y z i) = N.⋆-add-r x (go y) (go z) i
  go (Β·-add-l x y z i) = N.⋆-add-l 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.G.has-is-set (go a) (go b) (Ξ» i β†’ go (p i)) (Ξ» i β†’ go (q i)) i j

  go-linear : Linear-map (Free-Mod A) (G , N) Rings.id
  go-linear .map = go
  go-linear .linear r m s n = refl

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

To prove that free modules have the expected universal property, it remains to show that if f=g∘incf = g\circ{\mathrm{inc}}, then fold(f)=g{\mathrm{fold}}(f) = g. 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-module R)
  go .free x = Free-Mod ∣ x ∣
  go .unit x = Free-mod.inc
  go .universal {y = y} = fold-free-mod (y .snd)
  go .commutes f = refl
  go .unique {y = y} {f = f} {g = g} p =
    Linear-map-path (funext (Free-elim-prop.elim m)) where
    open Free-elim-prop
    module g = Linear-map g
    module y = Module y
    fold = fold-free-mod (y .snd) f .map
    m : Free-elim-prop (Ξ» a β†’ fold-free-mod (y .snd) f .map a ≑ g .map a)
    m .has-is-prop x = hlevel!
    m .P-Β· x y p =
      x y.⋆ fold y   β‰‘βŸ¨ ap (x y.⋆_) p βŸ©β‰‘
      x y.⋆ g .map y β‰‘βŸ¨ g.linear-simple _ _ βŸ©β‰‘
      g.map (x · y)  ∎
    m .P-0m = sym g.has-group-hom.pres-id
    m .P-+ x y p q =
      fold x y.+ fold y   β‰‘βŸ¨ apβ‚‚ y._+_ p q βŸ©β‰‘
      g.map x y.+ g.map y β‰‘Λ˜βŸ¨ g.has-group-hom.pres-⋆ _ _ βŸ©β‰‘Λ˜
      g.map (x + y)       ∎
    m .P-neg x p =
      y.G.inverse (fold x)  β‰‘βŸ¨ ap y.G.inverse p βŸ©β‰‘
      y.G.inverse (g.map x) β‰‘Λ˜βŸ¨ g.has-group-hom.pres-inv βŸ©β‰‘Λ˜
      g.map (neg x)         ∎
    m .P-inc x = p $β‚š x

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 {β„“β€² = β„“β€²})

  1. truncated, higherβ†©οΈŽ