reserve x,y,z for object, X,Y for set,
  i,k,n for Nat,
  p,q,r,s for FinSequence,
  w for FinSequence of NAT,
  f for Function;

theorem Th57:
  for p, q being Tree-yielding FinSequence for T1,T2 be Tree holds
  tree(p^<*T1*>^q) = tree(p^<*T2*>^q) with-replacement(<*len p*>,T1)
proof
  let p, q be Tree-yielding FinSequence, T1, T2 be Tree;
  set w1 = p^<*T1*>^q, W1 = tree(w1), w2 = p^<*T2*>^q, W2 = tree(w2),
  W = W2 with-replacement(<*len p*>, T1);
A1: len <*T1*> = 1 by FINSEQ_1:40;
A2: len <*T2*> = 1 by FINSEQ_1:40;
A3: len (p^<*T1*>) = len p + 1 by A1,FINSEQ_1:22;
A4: len w1 = len (p^<*T1*>)+len q by FINSEQ_1:22;
A5: len (p^<*T2*>) = len p + 1 by A2,FINSEQ_1:22;
A6: len w2 = len (p^<*T2*>)+len q by FINSEQ_1:22;
  len p+1 <= len p+1+len q by NAT_1:11;
  then
A7: len p < len p+1+len q by NAT_1:13;
  then
A8: w2.(len p+1) in rng w2 by A5,A6,Lm3;
  rng w2 is constituted-Trees by Def9;
  then
A9: {} in w2.(len p+1) by A8,TREES_1:22;
  <*len p*>^{} = <*len p*> by FINSEQ_1:34;
  then
A10: <*len p*> in W2 by A5,A6,A7,A9,Def15;
  let r be FinSequence of NAT;
  thus r in W1 implies r in W
  proof
    assume r in W1;
    then
A11: r = {} or ex n,s st n < len w1 & s in w1.(n+1) & r = <*n*>^s by Def15;
    now
      given n, s such that
A12:  n < len w1 and
A13:  s in w1.(n+1) and
A14:  r = <*n*>^s;
      reconsider s as FinSequence of NAT by A14,FINSEQ_1:36;
A15:  n <= len p or n >= len p+1 by NAT_1:13;
A16:  now
        assume
A17:    n < len p;
        then
A18:    n+1 in dom p by Lm3;
A19:    dom p c= dom (p^<*T2*>) by FINSEQ_1:26;
A20:    dom p c= dom (p^<*T1 *>) by FINSEQ_1:26;
A21:    (p^<*T2*>).(n+1) = p.(n+1) by A18,FINSEQ_1:def 7;
A22:    (p^<*T1*>).(n+1) = p.(n+1) by A18,FINSEQ_1:def 7;
A23:    w2.(n+1) = p.(n+1) by A18,A19,A21,FINSEQ_1:def 7;
        w1.(n+1) = p.(n+1) by A18,A20,A22,FINSEQ_1:def 7;
        then
A24:    r in W2 by A3,A4,A5,A6,A12,A13,A14,A23,Def15;
        not <*len p*> is_a_prefix_of <*n*>^s by A17,TREES_1:50;
        then not <*len p*> is_a_proper_prefix_of <*n*>^s;
        hence thesis by A10,A14,A24,TREES_1:def 9;
      end;
A25:  now
        assume
A26:    n = len p;
        then
A27:    (p^<*T1*>).(n+1) = T1 by FINSEQ_1:42;
        n < len p+1 by A26,NAT_1:13;
        then n+1 in dom (p^<*T1*>) by A3,Lm3;
        then
A28:    w1.(n+1) = T1 by A27,FINSEQ_1:def 7;
        s = s;
        hence thesis by A10,A13,A14,A26,A28,TREES_1:def 9;
      end;
      now
        assume that
A29:    n >= len p+1 and
A30:    n < len p+1+len q;
A31:    n+1 >= len p+1+1 by A29,XREAL_1:7;
A32:    n+1 <= len p+1+len q by A30,NAT_1:13;
        then
A33:    w1.(n+1) = q.(n+1-(len p+1)) by A3,A31,FINSEQ_1:23;
        w2.(n+1) = q.(n+1-(len p+1)) by A5,A31,A32,FINSEQ_1:23;
        then
A34:    r in W2 by A3,A4,A5,A6,A12,A13,A14,A33,Def15;
        len p <> n by A29,NAT_1:13;
        then not <*len p*> is_a_prefix_of <*n*>^s by TREES_1:50;
        then not <*len p*> is_a_proper_prefix_of <*n*>^s;
        hence thesis by A10,A14,A34,TREES_1:def 9;
      end;
      hence thesis by A3,A12,A15,A16,A25,FINSEQ_1:22,XXREAL_0:1;
    end;
    hence thesis by A11,TREES_1:22;
  end;
  assume r in W;
  then
A35: r in W2 & not <*len p*> is_a_proper_prefix_of r or
  ex s being FinSequence of NAT st s in T1 & r = <*len p*>^s
  by A10,TREES_1:def 9;
  assume
A36: not r in W1;
  then
A37: r <> {} by Def15;
A38: (p^<*T1*>).(len p+1) = T1 by FINSEQ_1:42;
A39: len p < len p+1 by NAT_1:13;
  len p+1 <= len p+1+len q by NAT_1:11;
  then
A40: len p < len w2 by A5,A6,NAT_1:13;
  len p+1 in dom (p^<*T1*>) by A3,A39,Lm3;
  then
A41: w1.(len p+1) = T1 by A38,FINSEQ_1:def 7;
  then consider n, s such that
A42: n < len w2 and
A43: s in w2.(n+1) and
A44: r = <*n*>^s by A3,A4,A5,A6,A35,A36,A37,A40,Def15;
  reconsider s as FinSequence of NAT by A44,FINSEQ_1:36;
A45: n = len p implies s = {} by A3,A4,A5,A6,A35,A36,A41,A42,A44,Def15,
TREES_1:10;
  {} in T1 by TREES_1:22;
  then n <> len p by A3,A4,A5,A6,A36,A41,A42,A44,A45,Def15;
  then n < len p or n > len p & 1<=1 by XXREAL_0:1;
  then 1 <= 1+n & 1+n = n+1 & n+1 <= len p & w1 = p^(<*T1*>^q) &
  w2 = p^(<*T2*>^q) or len p+1 < n+1 & n+1 <= len w1
  by A3,A4,A5,A6,A42,FINSEQ_1:32,NAT_1:11,13,XREAL_1:6;
  then w1.(n+1) = p.(n+1) & w2.(n+1) = p.(n+1) or
  w1.(n+1) = q.(n+1-(len p+1)) & w2.(n+1) = q.(n+1-(len p+1))
  by A3,A4,A5,A6,Lm1,FINSEQ_1:24;
  hence contradiction by A3,A4,A5,A6,A36,A42,A43,A44,Def15;
end;
