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;
