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));

theorem Th124:
  for xi,w being FinSequence of NAT
  for p,q being Tree-yielding FinSequence
  for d,t being Tree
  st i < len p & xi = <*i*>^w & d = p.(i+1) &
     q = p+*(i+1,d with-replacement (w, t)) & xi in tree p
  holds (tree p) with-replacement (xi, t) = tree q
  proof
    let xi,w be FinSequence of NAT;
    let p,q be Tree-yielding FinSequence;
    let d,t be Tree;
    assume Z0: i < len p;
    assume Z1: xi = <*i*>^w;
    assume Z2: d = p.(i+1);
    assume Z3: q = p+*(i+1,d with-replacement (w, t));
    assume Z4: xi in tree p;
    consider j being Nat, u being FinSequence such that
A2: j < len p & u in p.(j+1) & xi = <*j*>^u by Z1,Z4,TREES_3:def 15;
A3: i = xi.1 = j & w = u by Z1,A2,FINSEQ_1:41,HILBERT2:2;
    dom p = dom q by Z3,FUNCT_7:30;
    then
A4: len p = len q by FINSEQ_3:29;
    1 <= i+1 <= len p by Z0,NAT_1:12,13;
    then
AA: i+1 in dom p by FINSEQ_3:25;
    then
A7: q.(i+1) = d with-replacement(w, t) by Z3,FUNCT_7:31;
    let n be FinSequence of NAT;
    hereby assume n in (tree p) with-replacement (xi, t);
      then per cases by Z4,TREES_1:def 9;
      suppose
A8:     n in tree p & not xi is_a_proper_prefix_of n;
        then per cases by TREES_3:def 15;
        suppose n = {};
          hence n in tree q by TREES_1:22;
        end;
        suppose ex j being Nat, u being FinSequence st
          j < len p & u in p.(j+1) & n = <*j*>^u;
          then consider j being Nat, u being FinSequence such that
A9:       j < len p & u in p.(j+1) & n = <*j*>^u;
          per cases;
          suppose j+1 <> i+1;
            then q.(j+1) = p.(j+1) by Z3,FUNCT_7:32;
            hence n in tree q by A4,A9,TREES_3:def 15;
          end;
          suppose
B1:         j = i;
            xi = n or not xi c= n by A8,XBOOLE_0:def 8;
            then
B2:         not w is_a_proper_prefix_of u
            by Z1,A9,B1,HILBERT2:2,FINSEQ_6:13,XBOOLE_0:def 8;
            u is Element of d by Z2,A9,B1;
            then u in d with-replacement(w,t) by A2,A3,Z2,B2,TREES_1:def 9;
            hence n in tree q by A4,A7,A9,B1,TREES_3:def 15;
          end;
        end;
      end;
      suppose ex r being FinSequence of NAT st r in t & n = xi^r;
        then consider r being FinSequence of NAT such that
A1:     r in t & n = xi^r;
        w^r in d with-replacement (w,t) by A1,A2,A3,Z2,TREES_1:def 9;
        then <*i*>^(w^r) in tree q by Z0,A4,A7,TREES_3:def 15;
        hence n in tree q by Z1,A1,FINSEQ_1:32;
      end;
    end;
    assume n in tree q;
    then per cases by TREES_3:def 15;
    suppose n = {};
      hence thesis by TREES_1:22;
    end;
    suppose ex i st ex w being FinSequence st
      i < len q & w in q.(i+1) & n = <*i*>^w;
      then consider j being Nat, u being FinSequence such that
C1:   j < len q & u in q.(j+1) & n = <*j*>^u;
      per cases;
      suppose
C5:     i+1 <> j+1; then
        q.(j+1) = p.(j+1) & i <> j by Z3,FUNCT_7:32;
        then
C3:     n in tree p by A4,C1,TREES_3:def 15;
        not <*i*> c= <*j*>^u by C5,TREES_1:50;
        then not xi c< n by Z1,C1,Lem8,XBOOLE_0:def 8;
        hence thesis by Z4,C3,TREES_1:def 9;
      end;
      suppose
C4:     i = j;
        then reconsider u as Element of d with-replacement(w,t)
        by AA,C1,Z3,FUNCT_7:31;
        per cases by A2,A3,Z2,TREES_1:def 9;
        suppose u in d & not w c< u;
          then n in tree p & not xi c< n
          by Z1,Z2,A4,C1,C4,TREES_1:49,TREES_3:def 15;
          hence thesis by Z4,TREES_1:def 9;
        end;
        suppose ex r being FinSequence of NAT st r in t & u = w^r;
          then consider r being FinSequence of NAT such that
C5:       r in t & u = w^r;
          n = xi^r by Z1,C1,C4,C5,FINSEQ_1:32;
          hence thesis by Z4,C5,TREES_1:def 9;
        end;
      end;
    end;
  end;
