reserve S for non void non empty ManySortedSign,
  U0 for MSAlgebra over S;
reserve S for non void non empty ManySortedSign,
  X for ManySortedSet of the carrier of S,
  o for OperSymbol of S,
  b for Element of ([:the carrier' of S,{the
  carrier of S}:] \/ Union (coprod X))*;
reserve x for set;

theorem Th16:
  for S be non void non empty ManySortedSign, X be non-empty
  ManySortedSet of the carrier of S holds FreeGen(X) is free
proof
  let S be non void non empty ManySortedSign, X be non-empty ManySortedSet of
  the carrier of S;
  set D = DTConMSA(X), FA = FreeMSA(X), FG = FreeGen(X);
  let U1 be non-empty MSAlgebra over S, F be ManySortedFunction of FG,the
  Sorts of U1;
  set SA =the Sorts of FA, AR = the Arity of S, S1 = the Sorts of U1, O = the
  carrier' of S, RS = the ResultSort of S, DU = Union (the Sorts of U1);
  deffunc TermVal(Symbol of D) = pi(F,the Sorts of U1,$1);
  deffunc NTermVal(Symbol of D, FinSequence, FinSequence) = pi(@(X,$1),U1,$3);
  consider G being Function of TS(D), DU such that
A1: for t being Symbol of D st t in Terminals D holds G.(root-tree t) =
  TermVal(t) and
A2: for nt be Symbol of D, ts be FinSequence of TS(D) st nt ==> roots ts
  holds G.(nt-tree ts) = NTermVal(nt,roots ts,G * ts) from DTCONSTR:sch 8;
  deffunc F(object) = G | ((the Sorts of FA).$1);
  consider h be Function such that
A3: dom h = the carrier of S & for s be object st s in the carrier of S
  holds h.s = F(s) from FUNCT_1:sch 3;
  reconsider h as ManySortedSet of the carrier of S by A3,PARTFUN1:def 2
,RELAT_1:def 18;
  for s be object st s in dom h holds h.s is Function
  proof
    let s be object;
    assume s in dom h;
    then h.s = G | ((the Sorts of FA).s) by A3;
    hence thesis;
  end;
  then reconsider h as ManySortedFunction of the carrier of S by FUNCOP_1:def 6
;
  defpred P[set] means for s be SortSymbol of S st $1 in (the Sorts of FA).s
  holds (h.s).$1 in (the Sorts of U1).s;
A4: for nt being Symbol of D, ts being FinSequence of TS(D) st nt ==> roots
  ts & for t being DecoratedTree of the carrier of D st t in rng ts holds P[t]
  holds P[nt-tree ts]
  proof
    let nt be Symbol of D, ts be FinSequence of TS(D);
    assume that
A5: nt ==> roots ts and
A6: for t being DecoratedTree of the carrier of D st t in rng ts holds P[t];
    set p = G * ts, o = @(X,nt), ar = the_arity_of o, rs = the_result_sort_of
    o, OU = [:the carrier' of S,{the carrier of S}:] \/ Union (coprod (X qua
    ManySortedSet of the carrier of S)), rt = roots ts;
A7: [o,the carrier of S] = nt by A5,Def20;
    then
A8: [[o,the carrier of S],rt] in the Rules of D by A5,LANG1:def 1;
A9: [o,the carrier of S] = Sym(o,X);
    then
A10: (DenOp(o,X)).ts = nt-tree ts by A5,A7,Def12;
    dom (DenOp (o,X)) = ((FreeSort X)# * AR).o by FUNCT_2:def 1;
    then ts in dom (DenOp(o,X)) by A5,A7,A9,Th10;
    then
    rng (DenOp (o,X)) c= ((FreeSort X) * RS).o & nt-tree ts in rng (DenOp
    (o,X)) by A10,FUNCT_1:def 3,RELAT_1:def 19;
    then
A11: nt-tree ts in (SA * RS).o;
    set ppi = pi(o,U1,p);
A12: rng RS c= the carrier of S by RELAT_1:def 19;
A13: rng ar c= the carrier of S by FINSEQ_1:def 4;
    reconsider rt as Element of OU* by A8,ZFMISC_1:87;
A14: len rt = len ar by A8,Th5;
A15: dom rt = Seg len rt by FINSEQ_1:def 3;
    dom SA = the carrier of S by PARTFUN1:def 2;
    then
A16: dom(SA* ar) = dom ar by A13,RELAT_1:27;
A17: ar = AR.o by MSUALG_1:def 1;
    dom S1 = the carrier of S by PARTFUN1:def 2;
    then
A18: dom(S1* ar) = dom ar by A13,RELAT_1:27;
A19: dom rt = dom ts & Seg len ar = dom ar by FINSEQ_1:def 3,TREES_3:def 18;
A20: dom p = dom ts by FINSEQ_3:120;
    then
A21: dom p = dom (S1 * ar) by A18,A8,A15,A19,Th5;
A22: for x being object st x in dom (S1 * ar) holds p.x in (S1 * ar).x
    proof
      let x be object;
      assume
A23:  x in dom (S1 * ar);
      then reconsider n = x as Nat;
A24:  ts.n in rng ts by A18,A14,A15,A19,A23,FUNCT_1:def 3;
      rng ts c= TS D by FINSEQ_1:def 4;
      then reconsider t = ts.n as Element of TS(D) by A24;
A25:  p.n = G.(ts.n) by A21,A23,FINSEQ_3:120;
      ar.x in rng ar by A18,A23,FUNCT_1:def 3;
      then reconsider s = ar.x as SortSymbol of S by A13;
A26:  h.s = G | (SA.s) by A3;
      dom SA = the carrier of S by PARTFUN1:def 2;
      then
A27:  SA.s in rng SA by FUNCT_1:def 3;
      dom G = TS D by FUNCT_2:def 1
        .= union rng SA by Th11;
      then
A28:  dom (h.s) = SA.s by A26,A27,RELAT_1:62,ZFMISC_1:74;
      ts in ((FreeSort X)# * AR).o by A5,A7,A9,Th10;
      then ts in product ((FreeSort X) * ar) by A17,Th1;
      then ts.x in ((FreeSort X) * ar).x by A18,A16,A23,CARD_3:9;
      then
A29:  ts.x in (FreeSort X).(ar.x) by A18,A16,A23,FUNCT_1:12;
      then (h.s).t in S1.s by A6,A24;
      then G.t in S1.s by A29,A26,A28,FUNCT_1:47;
      hence thesis by A23,A25,FUNCT_1:12;
    end;
    dom S1 = the carrier of S by PARTFUN1:def 2;
    then
A30: dom (S1 *RS) = dom RS by A12,RELAT_1:27;
    let s be SortSymbol of S;
A31: dom Den(o,U1) = Args(o,U1) by FUNCT_2:def 1;
A32: dom RS = O by FUNCT_2:def 1;
    dom SA = the carrier of S by PARTFUN1:def 2;
    then dom (SA * RS) = dom RS by A12,RELAT_1:27;
    then nt-tree ts in SA.(RS.o) by A32,A11,FUNCT_1:12;
    then
A33: nt-tree ts in SA.rs by MSUALG_1:def 2;
    Args(o,U1) = (S1# * AR).o by MSUALG_1:def 4
      .= product (S1 * ar) by A17,Th1;
    then
A34: p in Args(o,U1) by A20,A18,A14,A15,A19,A22,CARD_3:9;
    then pi(o,U1,p) = Den(o,U1).p by Def21;
    then rng Den(o,U1) c= Result(o,U1) & ppi in rng Den(o,U1) by A34,A31,
FUNCT_1:def 3,RELAT_1:def 19;
    then
A35: ppi in Result(o,U1);
    assume
A36: nt-tree ts in SA.s;
A37: rs = s by A33,A36,XBOOLE_0:3,Th12;
    G.(nt-tree ts) = ppi by A2,A5;
    then
A38: ppi = (G | (SA.rs)).(nt-tree ts) by A33,FUNCT_1:49;
    Result(o,U1) = (S1 *RS).o by MSUALG_1:def 5
      .= S1.(RS.o) by A30,A32,FUNCT_1:12
      .= S1.rs by MSUALG_1:def 2;
    hence thesis by A3,A35,A38,A37;
  end;
A39: for t being Symbol of D st t in Terminals D holds P[root-tree t]
  proof
    let t be Symbol of D;
    assume
A40: t in Terminals D;
    then consider s be SortSymbol of S, x be set such that
    x in X.s and
A41: t = [x,s] by Th7;
    set E = {root-tree tt where tt is Symbol of D: tt in Terminals D & tt`2 =
    s}, a = root-tree t;
A42: t`2 = s by A41;
    then
A43: a in E by A40;
    let s1 be SortSymbol of S;
    reconsider f = F.s as Function of FG.s,S1.s;
A44: dom f = FG.s by FUNCT_2:def 1;
A45: E = FreeGen(s,X) by Th13;
    then FG.s = E by Def16;
    then
A46: rng f c= S1.s & f.a in rng f by A43,A44,FUNCT_1:def 3,RELAT_1:def 19;
    assume
A47: a in SA.s1;
A48: now
      a in (FreeSort X).s /\ (FreeSort X).s1 by A43,A45,A47,XBOOLE_0:def 4;
      then
A49:  (FreeSort X).s meets (FreeSort X).s1;
      assume s <> s1;
      hence contradiction by A49,Th12;
    end;
    h.s = G | (SA.s) by A3;
    then (h.s).a = G.a by A43,A45,FUNCT_1:49
      .= pi(F,S1,t) by A1,A40
      .= f.a by A40,A42,Def19;
    hence thesis by A46,A48;
  end;
A50: for t being DecoratedTree of the carrier of D st t in TS(D) holds P[t]
  from DTCONSTR:sch 7(A39,A4);
  for s be object st s in the carrier of S holds h.s is Function of (the
  Sorts of FA).s, (the Sorts of U1).s
  proof
    let x be object;
    assume x in the carrier of S;
    then reconsider s = x as SortSymbol of S;
A51: dom G = TS D by FUNCT_2:def 1
      .= union rng SA by Th11;
    dom SA = the carrier of S by PARTFUN1:def 2;
    then
A52: SA.s in rng SA by FUNCT_1:def 3;
A53: h.s = G | (SA.s) by A3;
    then
A54: dom (h.s) = SA.s by A51,A52,RELAT_1:62,ZFMISC_1:74;
A55: SA.s c= dom G by A51,A52,ZFMISC_1:74;
    rng (h.s) c= S1.s
    proof
      let a be object;
      assume a in rng (h.s);
      then consider T be object such that
A56:  T in dom (h.s) and
A57:  (h.s).T = a by FUNCT_1:def 3;
      reconsider T as Element of TS(D) by A55,A54,A56,FUNCT_2:def 1;
      T in SA.s by A53,A51,A52,A56,RELAT_1:62,ZFMISC_1:74;
      hence thesis by A50,A57;
    end;
    hence thesis by A54,FUNCT_2:def 1,RELSET_1:4;
  end;
  then reconsider h as ManySortedFunction of FA,U1 by PBOOLE:def 15;
  take h;
  thus h is_homomorphism FA,U1
  proof
    rng RS c= the carrier of S & dom SA = the carrier of S by PARTFUN1:def 2
,RELAT_1:def 19;
    then
A58: dom RS = O & dom (SA * RS) = dom RS by FUNCT_2:def 1,RELAT_1:27;
    let o be OperSymbol of S such that
    Args(o,FA) <> {};
    let x be Element of Args(o,FA);
    set rs = the_result_sort_of o, DA = Den(o,FA), D1 = Den(o,U1), OU = [:the
carrier' of S,{the carrier of S}:] \/ Union (coprod (X qua ManySortedSet of the
    carrier of S)), ar = the_arity_of o;
A59: ar = AR.o by MSUALG_1:def 1;
A60: Args(o,FA) = ((FreeSort X)# * AR).o by MSUALG_1:def 4;
    then reconsider p = x as FinSequence of TS(D) by Th8;
A61: Sym(o,X) ==> roots p by A60,Th10;
    then
A62: @(X,Sym(o,X)) = o by Def20;
A63: dom (G *p) = dom p by FINSEQ_3:120;
A64: x in ((FreeSort X)# * AR).o by A60;
A65: for a be object st a in dom p holds (G*p).a = (h#x).a
    proof
      rng ar c= the carrier of S & dom SA = the carrier of S by FINSEQ_1:def 4
,PARTFUN1:def 2;
      then
A66:  dom(SA* ar) = dom ar by RELAT_1:27;
      set rt = roots p;
      let a be object;
      assume
A67:  a in dom p;
      then reconsider n = a as Nat;
A68:  [[o,the carrier of S],rt] in the Rules of D by A61,LANG1:def 1;
      then reconsider rt as Element of OU* by ZFMISC_1:87;
A69:  len rt = len ar by A68,Th5;
A70:  (G*p).n = G.(x.n) & (h#x).n = (h.(ar/.n)).(x.n) by A63,A67,FINSEQ_3:120
,MSUALG_3:def 6;
A71:  h.(ar/.n) = G | (SA.(ar/.n)) by A3;
A72:  Seg len rt = dom rt by FINSEQ_1:def 3;
A73:  dom rt = dom p & Seg len ar = dom ar by FINSEQ_1:def 3,TREES_3:def 18;
      p in product((FreeSort X) * ar) by A64,A59,Th1;
      then
A74:  p.n in ((FreeSort X) * ar).n by A67,A66,A69,A72,A73,CARD_3:9;
      ((FreeSort X) * ar).n = SA.(ar.n) by A67,A66,A69,A72,A73,FUNCT_1:12
        .= SA.(ar/.n) by A67,A69,A72,A73,PARTFUN1:def 6;
      hence thesis by A70,A71,A74,FUNCT_1:49;
    end;
    dom (h#x) = dom ar by MSUALG_3:6;
    then
A75: G*p = h#x by A63,A65,FUNCT_1:2,MSUALG_3:6;
A76: h.rs = G | (SA.rs) by A3;
A77: dom (DenOp (o,X)) = ((FreeSort X)# * AR).o by FUNCT_2:def 1;
    (DenOp(o,X)).p = (Sym(o,X))-tree p by A61,Def12;
    then rng (DenOp (o,X)) c= ((FreeSort X) * RS).o & (Sym(o,X))-tree p in
    rng (DenOp (o,X)) by A60,A77,FUNCT_1:def 3,RELAT_1:def 19;
    then (Sym(o,X))-tree p in (SA * RS).o;
    then (Sym(o,X))-tree p in SA.(RS.o) by A58,FUNCT_1:12;
    then
A78: (Sym(o,X))-tree p in SA.rs by MSUALG_1:def 2;
    dom SA = the carrier of S by PARTFUN1:def 2;
    then
A79: SA.rs in rng SA by FUNCT_1:def 3;
    dom G = TS D by FUNCT_2:def 1
      .= union rng SA by Th11;
    then
A80: dom (h.rs) = SA.rs by A76,A79,RELAT_1:62,ZFMISC_1:74;
    DA = (FreeOper(X)).o by MSUALG_1:def 6
      .= DenOp(o,X) by Def13;
    then DA.x = (Sym(o,X))-tree p by A61,Def12;
    hence (h.rs).(DA.x) = G.((Sym(o,X))-tree p) by A78,A76,A80,FUNCT_1:47
      .= pi(@(X,Sym(o,X)),U1,G*p) by A2,A61
      .= D1.(h#x) by A62,A75,Def21;
  end;
  for x being object st x in the carrier of S holds (h || FG).x = F.x
  proof
    set hf = h || FG;
    let x be object;
    assume x in the carrier of S;
    then reconsider s = x as SortSymbol of S;
A81: dom (h.s) = SA.s by FUNCT_2:def 1;
A82: dom (hf.s) = FG.s by FUNCT_2:def 1;
A83: FG.s = FreeGen(s,X) by Def16;
A84: hf.s = (h.s) | (FG.s) by Def1;
A85: for a be object st a in FG.s holds (hf.s).a = (F.s).a
    proof
      let a be object;
A86:  h.s = G | (SA.s) by A3;
      assume
A87:  a in FG.s;
      then a in {root-tree t where t is Symbol of D: t in Terminals D & t`2 =
      s } by A83,Th13;
      then consider t be Symbol of D such that
A88:  a = root-tree t & t in Terminals D and
A89:  t`2 = s;
      thus (hf.s).a = (h.s).a by A84,A82,A87,FUNCT_1:47
        .= G.a by A81,A83,A87,A86,FUNCT_1:47
        .= pi(F,S1,t) by A1,A88
        .= (F.s).a by A88,A89,Def19;
    end;
    dom (F.s) = FG.s by FUNCT_2:def 1;
    hence thesis by A82,A85,FUNCT_1:2;
  end;
  hence thesis;
end;
