reserve
  a,b for object, I,J for set, f for Function, R for Relation,
  i,j,n for Nat, m for (Element of NAT),
  S for non empty non void ManySortedSign,
  s,s1,s2 for SortSymbol of S,
  o for OperSymbol of S,
  X for non-empty ManySortedSet of the carrier of S,
  x,x1,x2 for (Element of X.s), x11 for (Element of X.s1),
  T for all_vars_including inheriting_operations free_in_itself
  (X,S)-terms MSAlgebra over S,
  g for Translation of Free(S,X),s1,s2,
  h for Endomorphism of Free(S,X);
reserve
  r,r1,r2 for (Element of T),
  t,t1,t2 for (Element of Free(S,X));
reserve
  Y for infinite-yielding ManySortedSet of the carrier of S,
  y,y1 for (Element of Y.s), y11 for (Element of Y.s1),
  Q for all_vars_including inheriting_operations free_in_itself
  (Y,S)-terms MSAlgebra over S,
  q,q1 for (Element of Args(o,Free(S,Y))),
  u,u1,u2 for (Element of Q),
  v,v1,v2 for (Element of Free(S,Y)),
  Z for non-trivial ManySortedSet of the carrier of S,
  z,z1 for (Element of Z.s),
  l,l1 for (Element of Free(S,Z)),
  R for all_vars_including inheriting_operations free_in_itself
  (Z,S)-terms MSAlgebra over S,
  k,k1 for Element of Args(o,Free(S,Z));
reserve c,c1,c2 for set, d,d1 for DecoratedTree;
reserve
  w for (Element of Args(o,T)),
  p,p1 for Element of Args(o,Free(S,X));
reserve C for (context of x), C1 for (context of y), C9 for (context of z),
  C11 for (context of x11), C12 for (context of y11), D for context of s,X;
reserve
  S9 for sufficiently_rich non empty non void ManySortedSign,
  s9 for SortSymbol of S9,
  o9 for s9-dependent OperSymbol of S9,
  X9 for non-trivial ManySortedSet of the carrier of S9,
  x9 for (Element of X9.s9);
reserve h1 for x-constant Homomorphism of Free(S,X), T,
  h2 for y-constant Homomorphism of Free(S,Y), Q;
reserve
  s2 for s1-reachable SortSymbol of S,
  g1 for Translation of Free(S,Y),s1,s2,
  g for Translation of Free(S,X),s1,s2;

theorem Th117:
  for xi being Element of dom t st t.xi = [x,s]
  holds the_sort_of t1 = s implies
  t with-replacement (xi,t1) is Element of Free(S,X), the_sort_of t
  proof
    let xi be Element of dom t;
    assume Z0: t.xi = [x,s];
    assume Z1: the_sort_of t1 = s;
    defpred P[Element of Free(S,X)] means
    for xi being Element of dom $1
    for x1,t st $1.xi = [x1,s] & t = $1 holds $1 with-replacement (xi,t1)
    is Element of Free(S,X), the_sort_of t;
A1: P[x11-term]
    proof
      let xi be Element of dom(x11-term);
      dom(x11-term) = {{}} by TREES_1:29;
      then
A1:   xi = <*>NAT;
      let x1,t; assume (x11-term).xi = [x1,s];
      then [x11,s1] = [x1,s];
      then
A2:   s1 = s & x11 = x1 by XTUPLE_0:1;
      (x11-term) with-replacement(xi, t1) in
      (the Sorts of Free(S,X)).the_sort_of t1 by A1,SORT;
      hence thesis by A2,Z1,SORT;
    end;
A3: for o,p st for t st t in rng p holds P[t] holds P[o-term p]
    proof
      let o,p; assume
A4:   for t st t in rng p holds P[t];
      let xi being Element of dom (o-term p);
      let x1,t; assume
A5:   (o-term p).xi = [x1,s] & t = o-term p;
A6:   dom(o-term p) = tree doms p & doms p is Tree-yielding by TREES_4:10;
      then per cases by TREES_3:def 15;
      suppose xi = {};
        then (o-term p).xi = [o,the carrier of S] by TREES_4:def 4;
        then s in the carrier of S = s by A5,XTUPLE_0:1;
        hence (o-term p) with-replacement (xi,t1)
        is Element of Free(S,X), the_sort_of t;
      end;
      suppose
        ex n being Nat, w being FinSequence st
        n < len doms p & w in (doms p).(n+1) & xi = <*n*>^w;
        then consider w being FinSequence, n being Nat such
        that
B1:     n < len doms p & w in (doms p).(n+1) & xi = <*n*>^w;
        1 <= n+1 <= len doms p by B1,NAT_1:12,13;
        then
B2:     n+1 in dom doms p = dom p by FINSEQ_3:25,TREES_3:37;
        then
B3:     p/.(n+1) = p.(n+1) in rng p & (doms p).(n+1) = dom (p.(n+1))
        by FUNCT_1:def 3,PARTFUN1:def 6,FUNCT_6:def 2;
        reconsider w as Element of dom (p/.(n+1)) by B1,B3;
B4:     n < len p by B1,TREES_3:38;
        then (p/.(n+1)).w = [x1,s] by A5,B1,B3,TREES_4:12;
        then (p/.(n+1)) with-replacement (w,t1) is
        Element of Free(S,X), the_sort_of (p/.(n+1)) by B3,A4;
        then (p/.(n+1)) with-replacement (w,t1) is
        Element of Free(S,X), (the_arity_of o)/.(n+1) by B2,Th4A;
        then reconsider q = p+*(n+1, (p/.(n+1)) with-replacement (w,t1)) as
        Element of Args(o,Free(S,X)) by MSUALG_6:7;
        (Sym(o,X)-tree p) with-replacement (xi,t1) = Sym(o,X)-tree q
        by B1,B3,B4,A6,Th123;
        then (o-term p) with-replacement (xi,t1)
        is Element of Free(S,X), the_sort_of (o-term q) by SORT;
        then (o-term p) with-replacement (xi,t1)
        is Element of Free(S,X), the_result_sort_of o by Th8;
        hence (o-term p) with-replacement (xi,t1)
        is Element of Free(S,X), the_sort_of t by A5,Th8;
      end;
    end;
    P[t] from TermInd(A1,A3);
    hence t with-replacement (xi,t1) is Element of Free(S,X), the_sort_of t
    by Z0;
  end;
