reserve S for non empty non void ManySortedSign;
reserve X for non-empty ManySortedSet of S;
reserve x,y,z for set, i,j for Nat;
reserve
  A0 for (X,S)-terms non-empty MSAlgebra over S,
  A1 for all_vars_including (X,S)-terms MSAlgebra over S,
  A2 for all_vars_including inheriting_operations (X,S)-terms MSAlgebra over S,
  A for all_vars_including inheriting_operations free_in_itself
  (X,S)-terms MSAlgebra over S;

theorem Th56:
  for w being ManySortedFunction of X, (the carrier of S) --> NAT
  ex h being ManySortedFunction of Free(S,X), TermAlg S st
  h is_homomorphism Free(S,X), TermAlg S &
  for s being SortSymbol of S, t being Element of Free(S,X),s
  holds #(t,w) = h.s.t
  proof
    let w be ManySortedFunction of X, (the carrier of S) --> NAT;
A1: TermAlg S = Free(S, (the carrier of S)-->NAT) by MSAFREE3:31;
    reconsider G = FreeGen X as non-empty GeneratorSet of Free(S,X)
    by MSAFREE3:31;
    deffunc F(SortSymbol of S,Element of G.$1)
    = root-tree [w.$1.($2.{})`1, $1];
A2: for s being SortSymbol of S, x being Element of G.s holds
    F(s,x) in (the Sorts of TermAlg S).s
    proof
      let s be SortSymbol of S, x be Element of G.s;
      G.s = FreeGen(s,X) by MSAFREE:def 16;
      then consider y such that
A3:   y in X.s & x = root-tree [y,s] by MSAFREE:def 15;
      x .{} = [y,s] by A3,TREES_4:3;
      then reconsider x1 = (x .{})`1 as Element of X.s by A3;
A4:   w.s.x1 in ((the carrier of S)-->NAT).s;
      then reconsider t = F(s,x) as Term of S, (the carrier of S)-->NAT
      by MSATERM:4;
      t in FreeSort((the carrier of S)-->NAT, the_sort_of t) by MSATERM:def 5;
      then t in FreeSort((the carrier of S)-->NAT,s) by A4;
      hence thesis by MSAFREE:def 11;
    end;
    consider f being ManySortedFunction of G, the Sorts of TermAlg S such that
A5: for s being SortSymbol of S for x being Element of G.s holds f.s.x = F(s,x)
    from Sch2(A2);
    FreeGen X is free & FreeMSA X = Free(S, X) by MSAFREE3:31;
    then consider h being ManySortedFunction of Free(S,X), TermAlg S such that
A6: h is_homomorphism Free(S,X), TermAlg S & h||G = f;
    set t = the Element of Free(S, X);
    consider F being ManySortedSet of S-Terms X such that
A7: #(t,w) = F.t &
    (for s being SortSymbol of S, x being Element of X.s holds
    F.root-tree [x,s] = root-tree [w.s.x,s]) &
    for o being OperSymbol of S, p being ArgumentSeq of Sym(o,X) holds
    F.(Sym(o,X)-tree p) = Sym(o,(the carrier of S)-->NAT)-tree(F*p) by Def15;
    take h; thus h is_homomorphism Free(S,X), TermAlg S by A6;
    let s be SortSymbol of S, t be Element of Free(S,X),s;
    defpred P[DecoratedTree] means
    for s being SortSymbol of S
    st $1 in (the Sorts of Free(S,X)).s holds #($1,w) = h.s.$1;
A8: for s being SortSymbol of S, x being Element of X.s
    holds P[root-tree [x,s]]
    proof
      let s be SortSymbol of S, x be Element of X.s;
      reconsider t = root-tree [x,s] as Term of S,X by MSATERM:4;
      let s1 be SortSymbol of S;
      assume root-tree [x,s] in (the Sorts of Free(S,X)).s1;
      then t in (the Sorts of FreeMSA X).s1 by MSAFREE3:31;
      then s = the_sort_of t & t in FreeSort(X, s1)
      by MSAFREE:def 11,MSATERM:14;
      then
A9:   s = s1 by MSATERM:def 5;
      t in FreeGen(s,X) by MSAFREE:def 15;
      then reconsider q = t as Element of G.s by MSAFREE:def 16;
      t is Element of FreeMSA X by MSATERM:13;
      then t is Element of Free(S,X) by MSAFREE3:31;
      hence #(root-tree [x,s],w) = F.t by A7,Th55
      .= root-tree [w.s.x,s] by A7
      .= root-tree [w.s.[x,s]`1,s]
      .= F(s,q) by TREES_4:3
      .= f.s.t by A5
      .= ((h.s)|(G.s)).q by A6,MSAFREE:def 1
      .= h.s1.(root-tree [x,s]) by A9,FUNCT_1:49;
    end;
A10: for o being OperSymbol of S, p being ArgumentSeq of Sym(o,X) st
    for t being Term of S,X st t in rng p holds P[t]
    holds P[[o,the carrier of S]-tree p]
    proof
      let o be OperSymbol of S;
      let p be ArgumentSeq of Sym(o,X) such that
A11:   for t being Term of S,X st t in rng p holds P[t];
      let s be SortSymbol of S;
      reconsider t = Sym(o,X)-tree p as Term of S,X;
      assume [o, the carrier of S]-tree p in (the Sorts of Free(S,X)).s;
      then t in (the Sorts of FreeMSA X).s by MSAFREE3:31;
      then t in FreeSort(X,s) by MSAFREE:def 11;
      then the_sort_of t = s & t.{} = Sym(o,X) by TREES_4:def 4,MSATERM:def 5;
      then
A12:   s = the_result_sort_of o by MSATERM:17;
      FreeMSA X = Free(S,X) by MSAFREE3:31;
      then reconsider q = p as Element of Args(o,Free(S,X)) by INSTALG1:1;
      rng p c= dom F
      proof
        let y be object; assume y in rng p;
        then consider x being object such that
A13:     x in dom p & y = p.x by FUNCT_1:def 3;
        reconsider x as Nat by A13;
        p.x is Term of S,X & dom F = S-Terms X
        by A13,MSATERM:22,PARTFUN1:def 2;
        hence thesis by A13;
      end; then
A14:   dom(F*p) = dom p & dom(h#q) = dom the_arity_of o &
      dom q = dom the_arity_of o by RELAT_1:27,MSUALG_3:6;
      now
        let x be object; assume
A15:     x in dom p;
        then reconsider i = x as Nat;
        reconsider t = p.i as Term of S,X by A15,MSATERM:22;
        t is Element of FreeMSA X by MSATERM:13;
        then reconsider u = t as Element of Free(S,X) by MSAFREE3:31;
        reconsider s = (the_arity_of o)/.i as SortSymbol of S;
A16:     t in rng p by A15,FUNCT_1:def 3;
        the_sort_of t = s by A15,MSATERM:23;
        then t in FreeSort(X,s) by MSATERM:def 5;
        then t in (the Sorts of FreeMSA X).s by MSAFREE:def 11;
        then
A17:     t in (the Sorts of Free(S,X)).s by MSAFREE3:31;
        thus (F*p).x = F.t by A15,FUNCT_1:13
        .= #(u,w) by A7,Th55
        .= h.s.u by A11,A16,A17
        .= (h#q).x by A15,MSUALG_3:def 6;
      end; then
A18:   F*p = h#q by A14;
      t is Element of FreeMSA X by MSATERM:13;
      then t is Element of Free(S,X) by MSAFREE3:31;
      hence #([o, the carrier of S]-tree p, w) = F.t by Th55,A7
      .= Sym(o,(the carrier of S)-->NAT)-tree (F*p) by A7
      .= Den(o,Free(S,(the carrier of S)-->NAT)).(h#q) by A18,A1,Th13
      .= h.s.(Den(o,Free(S,X)).q) by A1,A6,A12
      .= h.s.([o, the carrier of S]-tree p) by Th13;
    end;
A19: for t being Term of S,X holds P[t] from MSATERM:sch 1(A8,A10);
    t is Element of (the Sorts of Free(S,X)).s;
    then t is Element of FreeMSA X by MSAFREE3:31;
    then t is Term of S,X by MSATERM:13;
    hence #(t,w) = h.s.t by A19;
  end;
