:: An Explicit Construction of Free Modules in Mizar
::  by Yasushige Watase
:: 
:: Received August 2, 2026
:: Copyright (c) 2026 Association of Mizar Users
::           (Stowarzyszenie Uzytkownikow Mizara, Bialystok, Poland).
:: This code can be distributed under the GNU General Public Licence
:: version 3.0 or later, or the Creative Commons Attribution-ShareAlike
:: License version 4.0 or later, subject to the binding interpretation
:: detailed in file COPYING.interpretation.
:: See COPYING.GPL and COPYING.CC-BY-SA for the full text of these
:: licenses, or see http://www.gnu.org/licenses/gpl.html and
:: http://creativecommons.org/licenses/by-sa/4.0/.

environ

 vocabularies TARSKI, FUNCSDOM, VECTSP_1, STRUCT_0, XBOOLE_0, VECTSP_2,
      SUBSET_1, FUNCT_1, RELAT_1, FUNCT_2, CLASSES2, FUNCOP_1, NUMBERS, NAT_1,
      ARYTM_3, ALGSTR_0, RLVECT_1, CARD_3, FINSEQ_1, XXREAL_0, SUPINF_2, MOD_3,
      BINOP_1, ZFMISC_1, PARTFUN1, ARYTM_1, MESFUNC1, LMOD_7, CARD_1, FINSEQ_3,
      FINSET_1, POLYNOM1, ALGSEQ_1, QC_LANG1, LOPBAN_1, UPROOTS, ORDINAL4,
      RMOD_2, XCMPLX_0, GROUP_6, GROUP_4, PRELAMB, RFINSEQ, MSSUBFAM, SETWISEO,
      FINSUB_1, RLVECT_3, ALGSTR_1, LMOD_XX1, RLVECT_2, FUNCT_5, MCART_1,
      RLSUB_1, VALUED_1, MOD_2, LMODFREE;
 notations TARSKI, XBOOLE_0, ZFMISC_1, SUBSET_1, RELAT_1, FUNCT_1, ORDINAL1,
      RELSET_1, PARTFUN1, MCART_1, FUNCT_2, BINOP_1, DOMAIN_1, FUNCT_3,
      FUNCOP_1, FINSET_1, FINSUB_1, SETWISEO, CARD_1, FUNCT_5, NUMBERS,
      XCMPLX_0, XXREAL_0, NAT_D, FINSEQ_1, FINSEQOP, FINSEQ_3, RFINSEQ,
      STRUCT_0, ALGSTR_0, RLVECT_1, VECTSP_1, ALGSTR_1, FUNCSDOM, VECTSP_2,
      VECTSP_4, VECTSP_6, VECTSP_7, MOD_2, POLYNOM1, MOD_4, ZMODUL01, PENCIL_2;
 constructors FUNCT_5, RELSET_1, SETWISEO, NAT_D, FINSEQOP, FINSEQ_3, RFINSEQ,
      VECTSP_6, GRCAT_1, MOD_2, MOD_4, RING_2, PENCIL_2;
 registrations ORDINAL1, STRUCT_0, RELSET_1, SUBSET_1, FUNCT_2, FUNCOP_1,
      VECTSP_1, VECTSP_4, NAT_1, FUNCT_1, CARD_1, FINSET_1, FINSEQ_1, RELAT_1,
      XREAL_0, RLVECT_1, NEWTON04, FVSUM_1, INT_1, VECTSP_7;
 requirements SUBSET, REAL, BOOLE, NUMERALS, ARITHM;


begin :: Function spaces and free modules

 reserve R for Ring;
 reserve X for set;
 reserve o,x for object;
 reserve a,b for Element of the carrier of R;
 reserve M,N for LeftMod of R;
 reserve i for Nat;

definition
  let R,X,N;
  func 0_Funcs(X,N) -> Element of Funcs(X,the carrier of N) equals
:: LMODFREE:def 1
  X --> 0.N;
end;

registration
  let R,X,N;
  cluster Funcs(X,the carrier of N) -> non empty;
end;

definition
  let R,X,N;
  func ADD(X,N) -> BinOp of Funcs(X,the carrier of N) means
:: LMODFREE:def 2
  for f,g being Element of Funcs(X,the carrier of N) holds
  it.(f,g) = (the addF of N).:(f,g);
end;

 reserve f,g,h for Element of Funcs(X, the carrier of N);

definition
  let R,X,N;
  let F be Function of [:the carrier of R,the carrier of N:], the carrier of N;
  let a be Element of the carrier of R;
  let f be Function of X,N;
  redefine func F[;](a,f) -> Element of Funcs(X, the carrier of N);
end;

::for x being object st x in X holds (it.(a,f)).x = (the lmult of N).(a,f.x)

definition
  let R,X,N;
  func LMULT(X,N) ->
  Function of [:the carrier of R,Funcs(X,the carrier of N):],
  Funcs(X,the carrier of N) means
:: LMODFREE:def 3
  for a being Element of the carrier of R, f being Element of
  Funcs(X,the carrier of N)
  holds
  for x being object st x in X holds (it.(a,f)).x = (the lmult of N).(a,f.x);
end;

definition
  let R,X,N;
  func Func_Mod(X,N) -> non empty ModuleStr over R equals
:: LMODFREE:def 4
  ModuleStr(# Funcs(X,the carrier of N),
              ADD(X,N), 0_Funcs(X,N), LMULT(X,N) #);
end;

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::: Funcs(X,R)/finite Support -> R-Module
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::$INSERT Module structure on function spaces & on finitely supported functions.

definition
  let R,X;
  func 0_Funcs(X,R) -> Element of Funcs(X,the carrier of R) equals
:: LMODFREE:def 5
  X --> 0.R;
end;

definition
  let R,X;
  func ADD(X,R) -> BinOp of Funcs(X,the carrier of R) means
:: LMODFREE:def 6

  for f,g being Element of Funcs(X,the carrier of R) holds
  it.(f,g) = (the addF of R).:(f,g);
end;

 reserve f,g,h for Element of Funcs(X,the carrier of R);

theorem :: LMODFREE:1
  for R,X,f,g,h holds h = ADD(X,R).(f,g) iff
  for x being object st x in X holds h.x = (the addF of R).(f.x,g.x);

definition
  let R,X;
  let F be Function of [:the carrier of R,the carrier of R:],
    the carrier of R;
  let a be Element of the carrier of R;
  let f be Function of X,R;
  redefine func F[;](a,f) -> Element of Funcs(X, the carrier of R);
end;

definition
  let R,X;
  func LMULT(X,R) ->
  Function of [:the carrier of R,Funcs(X,the carrier of R):],
  Funcs(X,the carrier of R) means
:: LMODFREE:def 7

  for a being Element of the carrier of R, f being Element of
  Funcs(X,the carrier of R)
  holds
  for x being object st x in X holds
  (it.(a,f)).x = (the multF of R).(a,f.x);
end;

theorem :: LMODFREE:2
    for R,X holds
    for a be Element of the carrier of R,
    f,h be Element of Funcs(X, the carrier of R) holds
    h = (LMULT(X,R)).(a,f) iff for x st x in X holds
    h.x = (the multF of R).(a,f.x);

definition
  let R,X;
  func fs_Funcs(X,R) -> Subset of Funcs(X,the carrier of R) equals
:: LMODFREE:def 8
  {x where x is Function of X,the carrier of R : x is finite-Support};
end;

registration
  let R,X;
  cluster fs_Funcs(X,R) -> non empty;
end;

registration
  let R,X;
  cluster finite-Support for Function of X,the carrier of R;
end;

theorem :: LMODFREE:3
  for f,g be finite-Support Function of X,the carrier of R holds
  ADD(X,R).[f,g] is finite-Support Function of X,the carrier of R;

theorem :: LMODFREE:4
  for a be Element of the carrier of R,
  g be finite-Support Function of X,the carrier of R holds
  LMULT(X,R).(a,g) is finite-Support Function of X,the carrier of R;

 reserve f,g,h for Element of fs_Funcs(X,R);

:: Construct the Module
definition
  let X be non empty set;
  let Y be non empty addLoopStr;
  func FuncAdd(X,Y) -> BinOp of Funcs(X,the carrier of Y) means
:: LMODFREE:def 9
  for f,g being Element of Funcs(X,the carrier of Y)
   holds it.(f,g) = (the addF of Y).:(f,g);
end;

definition
  let R,X;
  func fs_ADD(X,R) -> BinOp of fs_Funcs(X,R) equals
:: LMODFREE:def 10
  ADD(X,R)|[:fs_Funcs(X,R), fs_Funcs(X,R):];
end;

definition
  let R,X;
  func fs_LMULT(X,R) ->
  Function of [:the carrier of R,fs_Funcs(X,R):], fs_Funcs(X,R) equals
:: LMODFREE:def 11
  (LMULT(X,R))|[:the carrier of R,fs_Funcs(X,R):];
end;

definition
  let R,X;
  func 0_fs_Zero(X,R) -> Element of fs_Funcs(X,R) equals
:: LMODFREE:def 12
  0_Funcs(X,R);
end;

::$N The structure $\mathrm{FS\_Funcs}(X,R)$ forms a left $R$-module.
definition
  let R,X;
  func FS_Funcs(X,R) -> non empty ModuleStr over R equals
:: LMODFREE:def 13
  ModuleStr(# fs_Funcs(X,R), fs_ADD(X,R), 0_fs_Zero(X,R), fs_LMULT(X,R) #);
end;

theorem :: LMODFREE:5
   for v1,w1 be Element of Funcs(X,the carrier of R),
   v,w be Element of FS_Funcs(X,R) st v1=v & w1= w holds
   v + w = (ADD(X,R)).(v1,w1);

theorem :: LMODFREE:6
  FS_Funcs(X,R) is LeftMod of R;

registration
  let R,X;
  cluster FS_Funcs(X,R) -> Abelian add-associative right_zeroed left_zeroed
  right_complementable vector-distributive scalar-distributive
  scalar-associative scalar-unital;
end;

::$N The definition of Free Module $\mathcal{F}_{\mathrm{LMod}}(X)$
definition
  let R,X;
  func Free_LMod(X,R) -> non empty ModuleStr over R equals
:: LMODFREE:def 14
  FS_Funcs(X,R);
end;

registration
  let R,X;
  cluster Free_LMod(X,R) ->
    Abelian add-associative right_zeroed left_zeroed
    right_complementable vector-distributive
    scalar-distributive scalar-associative scalar-unital;
end;

begin :: Basis map and reconstruction
::::::::::::::::::::::::::::::::::::::::::::::::::::
:: Throughout this section, we assume that X is non empty.
:: The case X = {} is treated separately in the final section.
:: From this point onward, we also assume that R is non degenerated.
:: This assumption is essential for the basis construction below:
:: the equality Support iota(x,R) = {x} relies on
:: (iota(x,R)).x = 1.R <> 0.R.
:: Over a degenerated ring, every left module is trivial, and
:: IotaMap(X,R) is not one-to-one when card X >= 2.
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

reserve X for non empty set;
reserve R for non degenerated Ring;
reserve M,N for LeftMod of R;

definition
  let R,X;
  let f be Element of Free_LMod(X,R);
  func ~f -> Element of fs_Funcs(X,R) equals
:: LMODFREE:def 15
  f;
end;

definition
  let R,X;
  let f be Element of Free_LMod(X,R);
  func @f -> Element of Funcs(X,the carrier of R) equals
:: LMODFREE:def 16
  f;
end;

::$N the canonical generator $\iota_X(x)$
definition
  let X,R;
  let a be Element of X;
  func iota(a,R) -> Function of X, [#]R means
:: LMODFREE:def 17

  for x being object st x in X holds
  (x = a implies it.x = 1.R) & (x <> a implies it.x = 0.R);
end;

registration
  let X,R;
  let a be Element of X;
  cluster iota(a,R) -> (the carrier of R)-valued;
end;

registration
  let X,R;
  let a be Element of X;
  cluster iota(a,R) -> finite-Support;
end;

definition
  let R,X;
  let x be Element of X;
  func Iota(x,R) -> Element of Free_LMod(X,R) equals
:: LMODFREE:def 18
  iota(x,R);
end;

::$N the induced injection $\iota_X : X \longrightarrow \mathcal{F}_{\mathrm{LMod}}(X)$
definition
  let R,X;
  func IotaMap(X,R) -> Function of X, Free_LMod(X,R) means
:: LMODFREE:def 19
   for x be Element of X holds it.x = Iota(x,R);
 end;

registration
  let R,X;
  cluster IotaMap(X,R) -> one-to-one;
end;

definition
  let R,X;
  let x be Element of X;
  func ev_(x,R) -> Function of Free_LMod(X,R), R means
:: LMODFREE:def 20
  for f be Element of Free_LMod(X,R) holds it.f = @f.x;
 end;

definition
  let X,R;
  let f be Element of Free_LMod(X,R);
  func ev_iota(f) -> Function of X, Free_LMod(X,R) means
:: LMODFREE:def 21
  for x be Element of X holds it.x = (ev_(x,R).f)*(IotaMap(X,R).x);
 end;

registration
  let X,R;
  let f be Element of Free_LMod(X,R);
  cluster Support @f -> finite for set;
end;

registration
  let X,R;
  cluster 0.Free_LMod(X,R) -> zero;
  cluster non zero for Element of Free_LMod(X,R);
end;

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: We introduce the indexing constructions idxseq and F_idxseq
:: to represent finite formal sums by finite sequences:
:: idxseq(f)   = canFS(Support @f)  the support as a finite sequence
:: F_idxseq(f) = the FinSequence with
:: F_idxseq(f).i = (ev_(x,R).f) * Iota(x,R)  where x = idxseq(f).i
:: f = Sum F_idxseq(f)                  :: reconstruction, see Th15
:: (the numbering supports the proofs by induction on the length of
:: the sequence.)
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

definition
  let X,R;
  let f be Element of Free_LMod(X,R);
  func idxseq(f) -> FinSequence of X equals
:: LMODFREE:def 22
  canFS (Support @f);
end;

theorem :: LMODFREE:7
  for f be Element of Free_LMod(X,R) holds
    Support ev_iota(f) = Support @f;

definition
  let X,R;
  let s be FinSequence of the carrier of Free_LMod(X,R), x be Element of X;
  func ev_(s,x) -> FinSequence of the carrier of R means
:: LMODFREE:def 23

  dom it = dom s & for i be Nat st i in dom s holds it.i = ev_(x,R).(s/.i);
 end;

theorem :: LMODFREE:8
  for x be Element of X, f be Element of Free_LMod(X,R), a be Element of R
  holds @(a*f).x = a*@f.x;

theorem :: LMODFREE:9
  for x be Element of X, f,g be Element of Free_LMod(X,R) holds
  @(f+g).x = @f.x + @g.x &
  ev_(x,R).(f+g) = ev_(x,R).f + ev_(x,R).g;

theorem :: LMODFREE:10
  for x being Element of X holds ev_(x,R).(0.Free_LMod(X,R)) = 0.R;

theorem :: LMODFREE:11
  for N0 be Nat, F be FinSequence of the carrier of Free_LMod(X,R),
      x be Element of X st len F = N0+1 holds
    ev_(F,x) = (ev_(F|N0,x))^<*ev_(x,R).(F/.(len F))*>;

theorem :: LMODFREE:12
  for F be FinSequence of the carrier of Free_LMod(X,R), x be Element of X
  holds ev_(x,R).(Sum F) = Sum ev_(F,x);

definition
  let X,R;
  let f be Element of Free_LMod(X,R);
  func F_idxseq(f) -> FinSequence of the carrier of Free_LMod(X,R) means
:: LMODFREE:def 24

  len it = len idxseq(f) & for i be Nat st i in dom it holds
  it.i = (ev_iota(f)).((idxseq(f)).i);
end;

theorem :: LMODFREE:13
  for f be Element of  Free_LMod(X,R),x be Element of X
    st not x in Support @f holds 0.R = @(Sum(F_idxseq(f))).x;

theorem :: LMODFREE:14
   for f be Element of Free_LMod(X,R),x be Element of X st x in Support @f
   holds
   ex k be Nat st x = (canFS (Support @f)).k & k in dom (canFS (Support @f)) &
   ev_(F_idxseq(f),x).k = @f.x;

theorem :: LMODFREE:15
  for f be Element of Free_LMod(X,R),x be Element of X st x in Support @f
    holds @f.x = (@Sum(F_idxseq(f))).x;

::$MR Every element of $\mathrm{Free\_LMod}(X,R)$ is the sum of its canonical finite decomposition.

theorem :: LMODFREE:16
   for f be Element of Free_LMod(X,R) holds f = Sum(F_idxseq(f));

theorem :: LMODFREE:17
  for f,g be Element of Free_LMod(X,R)
  st Support @f misses Support @g holds
  Support @(f+g) = (Support @f) \/ (Support @g);

definition
  let X,R,N;
  let u be Function of X,N;
  func @u -> Element of Funcs(X,[#]N) equals
:: LMODFREE:def 25
  u;
end;

definition
  let X,R,N;
  let u be Function of X,N;
  func ^u -> Element of Func_Mod(X,N) equals
:: LMODFREE:def 26
  u;
end;

definition
  let X,R,N;
  let u be Element of Func_Mod(X,N);
  func ^u -> Function of X,N equals
:: LMODFREE:def 27
  u;
end;

begin :: Universal Mapping Property

 reserve B,B1,B2 for Element of Fin X;
 reserve f for Element of Free_LMod(X,R);
 reserve v for Function of X, the carrier of N;

definition
  let R be non degenerated Ring, X be non empty set, N be LeftMod of R;
  let v be Function of X, the carrier of N;
  let f be Element of Free_LMod(X,R);
  func TermFS(f,v) -> Function of X, the carrier of N means
:: LMODFREE:def 28

  for x being Element of X holds it.x = (@f.x) * (v.x);
end;

definition
  let R be non degenerated Ring, X be non empty set, N be LeftMod of R;
  let v be Function of X, the carrier of N;
  let f be Element of Free_LMod(X,R);
  let B be Element of Fin X;
  func PhiSet(v,f,B) -> Element of N equals
:: LMODFREE:def 29
  (the addF of N) $$ (B, TermFS(f,v));
end;

theorem :: LMODFREE:18
  for x being Element of X holds PhiSet(v,f,{.x.}) = (@f.x) * (v.x);

definition
  let R be non degenerated Ring, X be non empty set;
  let f be Element of Free_LMod(X,R);
  func SuppFin(f) -> Element of Fin X equals
:: LMODFREE:def 30
    Support @f;
end;

::$INSERT we define the induced homomorphism $\Phi(v):\mathrm{FS\_Funcs}(X,R)\to N$.
definition
  let R be non degenerated Ring, X be non empty set, N be LeftMod of R;
  let v be Function of X, the carrier of N;
  func Phi(v) -> Function of Free_LMod(X,R), the carrier of N means
:: LMODFREE:def 31

  for f being Element of Free_LMod(X,R) holds
  it.f = PhiSet(v,f,SuppFin(f));
end;

theorem :: LMODFREE:19
  for R be non degenerated Ring, X be non empty set, N be LeftMod of R,
      v be Function of X, the carrier of N, x be Element of X holds
    (Phi(v)).(Iota(x,R)) = v.x;

:: Additivity of Phi(v) can be intuitively understood
:: by reducing to the disjoint-support case.

theorem :: LMODFREE:20 :::: Th20:
  for R be non degenerated Ring, X be non empty set, N be LeftMod of R,
      v be Function of X, the carrier of N, f,g be Element of Free_LMod(X,R)
    st Support @f misses Support @g
    holds (Phi(v)).(f+g) = (Phi(v)).f + (Phi(v)).g;

theorem :: LMODFREE:21
   for R be non degenerated Ring, X be non empty set, N be LeftMod of R,
   v be Function of X, the carrier of N, f,g be Element of Free_LMod(X,R)
   holds (Phi(v)).(f+g) = (Phi(v)).f + (Phi(v)).g;

theorem :: LMODFREE:22
  for R be non degenerated Ring, X be non empty set, N be LeftMod of R,
      v be Function of X, the carrier of N, f be Element of Free_LMod(X,R),
      a be Element of R holds (Phi(v)).(a*f) = a * ((Phi(v)).f);

theorem :: LMODFREE:23 ::::::: PENCIL_2?
  for M be non empty addLoopStr
  for p be FinSequence of the carrier of M, k be Nat
    st k in dom p holds Del(p,k,k) = Del(p,k);

definition
  let R be Ring, X be set, M be LeftMod of R;
  let u be Function of X, the carrier of M;
  attr u is universal means
:: LMODFREE:def 32
  for N be LeftMod of R, v be Function of X, the carrier of N
  holds
  ex F be Homomorphism of (id R), M,N st F*u = v &
  for f1,f2 be Homomorphism of (id R), M,N st f1*u = v & f2*u = v
  holds f1 = f2;
end;

theorem :: LMODFREE:24
  for R be non degenerated Ring, X be non empty set, N be LeftMod of R,
      v be Function of X, the carrier of N
    holds Phi(v) is Homomorphism of (id R), Free_LMod(X,R), N;

theorem :: LMODFREE:25
   for R be non degenerated Ring, X be non empty set, N be LeftMod of R,
   v be Function of X, the carrier of N
   holds (Phi(v)) * (IotaMap(X,R)) = v;

registration
  let R be Ring, M,N be LeftMod of R;
  let h be Function of the carrier of M, the carrier of N;
  let s be FinSequence of the carrier of M;
  cluster h*s -> FinSequence-like (the carrier of N)-valued;
end;

theorem :: LMODFREE:26
   for D,E be non empty set,  h be Function of D,E,  p be FinSequence of D,
   d be Element of D holds h*(p^<*d*>) = (h*p)^<*h.d*>;

theorem :: LMODFREE:27
  for M,N being non empty add-associative right_zeroed right_complementable
    addLoopStr,
    h being Function of the carrier of M, the carrier of N,
    s being FinSequence of the carrier of M
    st h is additive
    holds h.(Sum s) = Sum(h*s);

::$MR The map $\mathrm{IotaMap}(X,R)$ satisfies the universal mapping property of the free module on $X$.
theorem :: LMODFREE:28
  for R be non degenerated Ring, X be non empty set
    holds IotaMap(X,R) is universal;

begin :: Linear Independence and Basis

 reserve a for Nat, p for FinSequence;

theorem :: LMODFREE:29
  for R be non degenerated Ring, X be non empty set,
    f be Element of Free_LMod(X,R) holds f in Lin(rng IotaMap(X,R));

theorem :: LMODFREE:30
  for R be non degenerated Ring, X be non empty set holds
    Lin rng IotaMap(X,R) = the ModuleStr of Free_LMod(X,R);

theorem :: LMODFREE:31
  for R be non degenerated Ring, X be non empty set holds
    rng IotaMap(X,R) is linearly-independent;

theorem :: LMODFREE:32
  for R be non degenerated Ring, X be non empty set holds
    rng IotaMap(X,R) is base;

::$N as a corollary of Th34, using the basis-theoretic definition of free modules.
theorem :: LMODFREE:33
  for R be non degenerated Ring, X be non empty set
  holds Free_LMod(X,R) is free;

begin

theorem :: LMODFREE:34
  FS_Funcs({},R) = TrivialLMod(R);

theorem :: LMODFREE:35
  Free_LMod({},R) is free;

theorem :: LMODFREE:36
  for R be non degenerated Ring, X be set
    holds Free_LMod(X,R) is free;

registration
  let R be non degenerated Ring, X be set;
  cluster Free_LMod(X,R) -> free;
end;
