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 Th58:
  for w being ManySortedFunction of X, (the carrier of S) --> NAT
  for o being OperSymbol of S, p being ArgumentSeq of Sym(o,X)
  for q being FinSequence st dom q = dom p &
  for i being Nat, t being Element of Free(S,X) st i in dom p & t = p.i
  holds q.i = #(t,w)
  holds #(Sym(o,X)-tree p, w) = Sym(o,(the carrier of S)-->NAT)-tree q
  proof
    let w be ManySortedFunction of X, (the carrier of S) --> NAT;
    let o be OperSymbol of S, p be ArgumentSeq of Sym(o,X);
    let q be FinSequence such that
A1: dom q = dom p and
A2: for i being Nat, t being Element of Free(S,X) st i in dom p & t = p.i
    holds q.i = #(t,w);
    reconsider t = Sym(o,X)-tree p as Term of S,X;
    deffunc R(set,set) = root-tree [w.$2.$1,$2];
    deffunc F(OperSymbol of S,FinSequence)
    = Sym($1,(the carrier of S)-->NAT)-tree($2);
A3: S-Terms X = Union the Sorts of FreeMSA X by MSATERM:13;
    then t is Element of Free(S,X) by MSAFREE3:31;
    then consider G being ManySortedSet of S-Terms X such that
A4: #(t,w) = G.t and
A5: (for s being SortSymbol of S, x being Element of X.s holds
    G.root-tree [x,s] = R(x,s)) and
A6: for o being OperSymbol of S, p being ArgumentSeq of Sym(o,X) holds
    G.(Sym(o,X)-tree p) = F(o,G*p) by Def15;
    rng p c= dom G
    proof
      let y be object; assume y in rng p;
      then consider x being object such that
A7:   x in dom p & y = p.x by FUNCT_1:def 3;
      reconsider x as Nat by A7;
      y is Term of S, X by A7,MSATERM:22;
      then y in S-Terms X;
      hence thesis by PARTFUN1:def 2;
    end;
    then
A8: dom (G*p) = dom p by RELAT_1:27;
    now
      let x be object; assume
A9:   x in dom p;
      then reconsider i = x as Nat;
      reconsider t = p.i as Term of S,X by A9,MSATERM:22;
      reconsider t as Element of Free(S,X) by A3,MSAFREE3:31;
      thus q.x = #(t,w) by A2,A9 .= G.t by A5,A6,Th55
      .= (G*p).x by A9,FUNCT_1:13;
    end;
    then q = G*p by A1,A8;
    hence #(Sym(o,X)-tree p, w) = Sym(o,(the carrier of S)-->NAT)-tree q
    by A4,A6;
  end;
