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 Th47:
  canonical_homomorphism A2 is_epimorphism Free(S,X),A2 &
  for s being SortSymbol of S, t being Element of A2,s holds
  (canonical_homomorphism A2).s.t = t
  proof set A = A2;
A1: FreeMSA X = Free(S,X) by MSAFREE3:31;
    reconsider G = FreeGen X as GeneratorSet of Free(S,X) by MSAFREE3:31;
    set f = canonical_homomorphism A;
A2: f is_homomorphism Free(S,X),A & f||G = id G by Def10;
    defpred P[set] means for s being SortSymbol of S
    st $1 in (the Sorts of A).s
    holds f.s.$1 = $1;
A3: for s being SortSymbol of S, v being Element of X.s
    holds P[root-tree [v,s]]
    proof
      let s be SortSymbol of S;
      let v be Element of X.s;
      reconsider t = root-tree [v,s] as Term of S,X by MSATERM:4;
      let r be SortSymbol of S; assume
A4:   root-tree [v,s] in (the Sorts of A).r;
      the Sorts of A is MSSubset of Free(S,X) by Def6; then
      (the Sorts of A).r c= (the Sorts of Free(S,X)).r by PBOOLE:def 2,def 18;
      then root-tree [v,s] in (FreeSort X).r by A4,A1; then
      root-tree [v,s] in FreeSort(X, r) by MSAFREE:def 11; then
A5:   the_sort_of t = r & s = the_sort_of t by MSATERM:def 5,14;
      root-tree [v,s] in FreeGen(s, X) by MSAFREE:def 15; then
A6:   root-tree [v,s] in (FreeGen X).s by MSAFREE:def 16;
A7:   (id FreeGen X).s = id ((FreeGen X).s) by MSUALG_3:def 1;
      (f.s)|((FreeGen X).s).root-tree [v,s] = (f.s).root-tree [v,s]
      by A6,FUNCT_1:49; then
A8:   (f.s).root-tree [v,s] = (id((FreeGen X).s)).root-tree [v,s]
      by A2,A7,MSAFREE:def 1 .= root-tree [v,s] by A6,FUNCT_1:18;
      thus thesis by A5,A8;
    end;
A9: 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);
      assume
A10:   for t being Term of S,X st t in rng p holds P[t];
      let s be SortSymbol of S;
      assume
A11:   [o,the carrier of S]-tree p in (the Sorts of A).s;
      Sym(o,X) ==> roots p & p is FinSequence of TS DTConMSA X
      by MSATERM:def 1,21; then
A12:   DenOp(o, X).p = Sym(o,X)-tree p & (FreeOper X).o = DenOp(o, X)
      by MSAFREE:def 12,def 13;
A13:   p is Element of Args(o, Free(S,X)) by A1,INSTALG1:1;
      reconsider t = Sym(o,X)-tree p as Term of S,X;
      the Sorts of A is MSSubset of Free(S,X) by Def6; then
A14:   (the Sorts of A).s c= (the Sorts of Free(S,X)).s by PBOOLE:def 2,def 18;
A15:   the_sort_of t = the_result_sort_of o &
      FreeSort(X,s) = (the Sorts of Free(S,X)).s
      by A1,MSATERM:20,MSAFREE:def 11; then
A16:   s = the_result_sort_of o by A11,A14,MSATERM:def 5;
A17:   Den(o,Free(S,X)).p in (the Sorts of A).the_result_sort_of o
      by A11,A12,A1,A15,A14,MSATERM:def 5; then
A18:   p in Args(o,A) & Den(o,A).p = Den(o,Free(S,X)).p
      by A13,Def8;
      reconsider q = p as Element of Args(o, A) by A17,A13,Def8;
      reconsider p0 = p as Element of Args(o, Free(S,X)) by A1,INSTALG1:1;
A19:   dom q = dom the_arity_of o & dom(f#p0) = dom the_arity_of o &
      Args(o,A) = product ((the Sorts of A)*the_arity_of o) &
      dom ((the Sorts of A)*the_arity_of o) = dom the_arity_of o
      by MSUALG_3:6,PRALG_2:3;
      now
        let i be Nat; assume
A20:     i in dom the_arity_of o; then
A21:     (the_arity_of o)/.i = (the_arity_of o).i & p0.i in rng p
        by A19,FUNCT_1:def 3,PARTFUN1:def 6;
A22:     p0.i in ((the Sorts of A)*the_arity_of o).i by A19,A20,CARD_3:9; then
A23:     p0.i in (the Sorts of A).((the_arity_of o).i) by A20,FUNCT_1:13;
        p0.i is Element of (the Sorts of A).((the_arity_of o)/.i)
        by A21,A22,A20,FUNCT_1:13;
        then p0.i is Term of S,X by Th42;
        hence q.i = (f.((the_arity_of o)/.i)).(p0.i) by A10,A21,A23;
      end; then
      f#p0 = q by A19,MSUALG_3:24;
      hence thesis by A16,A12,A1,A18,Def10,MSUALG_3:def 7;
    end;
A24: for t being Term of S,X holds P[t] from MSATERM:sch 1(A3,A9);
    thus f is_epimorphism Free(S,X),A
    proof
      thus f is_homomorphism Free(S,X), A by Def10;
      let x; assume x in the carrier of S; then
      reconsider s = x as SortSymbol of S;
      thus rng(f.x) c= (the Sorts of A).x;
      let y be object; assume
   y in (the Sorts of A).x; then
      reconsider t = y as Element of (the Sorts of A).s;
      t is Term of S,X by Th42; then
A25:   f.s.t = t by A24;
      the Sorts of A is MSSubset of Free(S,X) by Def6; then
      (the Sorts of A).s c= (the Sorts of Free(S,X)).s
      by PBOOLE:def 2,def 18; then
      t in (the Sorts of Free(S,X)).s & dom(f.s) = (the Sorts of Free(S,X)).s
      by FUNCT_2:def 1;
      hence thesis by A25,FUNCT_1:def 3;
    end;
    let s be SortSymbol of S;
    let t be Element of (the Sorts of A).s;
    t is Term of S,X by Th42;
    hence f.s.t = t by A24;
  end;
