reserve i,j,x,y for object,
  f,g for Function;
reserve T,T1 for finite Tree,
  t,p for Element of T,
  t1 for Element of T1;

theorem
  for e1,e2 being finite DecoratedTree, x being set,
      k being Element of NAT, p being DTree-yielding FinSequence st
    <*k*> in dom e1 & e1 = x-tree p
  ex q being DTree-yielding FinSequence
  st e1 with-replacement (<*k*>,e2) = x-tree q &
    len q = len p & q.(k+1) = e2 &
  for i being Nat st i in dom p & i <> k+1 holds q.i = p.i
proof
  let e1,e2 be finite DecoratedTree, x be set, k be Element of NAT,
      p be DTree-yielding FinSequence such that
A1: <*k*> in dom e1 and
A2: e1 = x-tree p;
  consider j being Nat, T being DecoratedTree, w being Node of T
  such that
A3: j < len p and
  T = p.(j+1) and
A4: <*k*> = <*j*>^w by A1,A2,TREES_4:11;
  consider pp being DTree-yielding FinSequence such that
A5: p = pp and
A6: dom(x-tree p) = tree doms pp by TREES_4:def 4;
A7: dom doms pp = dom p by A5,TREES_3:37;
  deffunc F(Nat) = IFEQ($1,k+1,e2,p.$1);
  set o = <*k*>;
  consider q being FinSequence such that
A8: len q = len p and
A9: for i being Nat st i in dom q holds q.i = F(i) from FINSEQ_1:sch 2;
A10: dom q = Seg len p by A8,FINSEQ_1:def 3;
A11: dom q = dom p by A8,FINSEQ_3:29;
  now
    let x;
    assume
A12: x in dom q;
    then reconsider i = x as Nat;
A13: i = k+1 or i <> k+1;
    q.i = IFEQ(i,k+1,e2,p.i) by A9,A12;
    then q.i = e2 or q.i = p.i by A13,FUNCOP_1:def 8;
    hence q.x is DecoratedTree by A11,A12,TREES_3:24;
  end;
  then reconsider q as DTree-yielding FinSequence by TREES_3:24;
  consider qq being DTree-yielding FinSequence such that
A14: q = qq and
A15: dom(x-tree q) = tree doms qq by TREES_4:def 4;
A16: len doms pp = len p by A5,TREES_3:38
    .= len doms qq by A8,A14,TREES_3:38;
A17: dom p = Seg len p by FINSEQ_1:def 3;
A18: now
    let i be Nat;
    assume that
A19: i in dom doms pp and
A20: i <> k+1;
A21: q.i = IFEQ(i,k+1,e2,p.i) by A9,A10,A17,A7,A19
      .= p.i by A20,FUNCOP_1:def 8;
    reconsider pii = p.i as DecoratedTree by A7,A19,TREES_3:24;
    thus (doms pp).i = dom pii by A5,A7,A19,FUNCT_6:22
      .= (doms qq).i by A11,A14,A7,A19,A21,FUNCT_6:22;
  end;
  reconsider dqq = doms qq as Tree-yielding FinSequence;
  take q;
  <*j*> = <*k*> by A4,FINSEQ_1:88;
  then
A22: j = <*k*>.1
    .= k;
  then 1 <= k+1 & k+1 <= len p by A3,NAT_1:11,13;
  then
A23: k+1 in dom p by FINSEQ_3:25;
  then k+1 in Seg len p by FINSEQ_1:def 3;
  then
A24: q.(k+1) = IFEQ(k+1,k+1,e2,p.(k+1)) by A9,A10
    .= e2 by FUNCOP_1:def 8;
  then (doms qq).(k+1) = dom e2 by A11,A23,A14,FUNCT_6:22;
  then
A25: dom(x-tree q) = dom e1 with-replacement (o, dom e2) by A2,A23,A15,A6,A16
,A7,A18,Th13;
  for f being FinSequence of NAT st f in dom e1 with-replacement (o, dom
  e2) holds not o is_a_prefix_of f & (x-tree q).f = e1.f or ex r being
  FinSequence of NAT st r in dom e2 & f = o^ r & (x-tree q).f = e2.r
  proof
    reconsider o9 = o as Element of dom e1 by A1;
    let f be FinSequence of NAT;
    assume
A26: f in dom e1 with-replacement (o, dom e2);
    per cases by A26,Th10;
    suppose
A27:  not o9 is_a_prefix_of f;
A28:  (x-tree q).f = e1.f
      proof
        per cases by A15,A25,A26,TREES_3:def 15;
        suppose
A29:      f = {};
          hence (x-tree q).f = x by TREES_4:def 4
            .= e1.f by A2,A29,TREES_4:def 4;
        end;
        suppose
          ex w being Nat, u being FinSequence st w < len(
          dqq) & u in (dqq).(w+1) & f = <*w*>^u;
          then consider w be Nat, u being FinSequence such that
A30:      w < len(doms q) and
A31:      u in (doms q).(w+1) and
A32:      f = <*w*>^u by A14;
          reconsider u as FinSequence of NAT by A32,FINSEQ_1:36;
          reconsider w as Element of NAT by ORDINAL1:def 12;
          reconsider ww = <*w*> as FinSequence of NAT;
A33:      w+1 <> k+1 by A27,A32,TREES_1:1;
A34:      ex pp being DTree-yielding FinSequence st p = pp & dom(x -tree p
          ) = tree doms pp by TREES_4:def 4;
A35:      w < len q by A30,TREES_3:38;
          then
A36:      (x-tree q)|<*w*> = q.(w+1) by TREES_4:def 4;
          1 <= w+1 & w+1 <= len p by A8,A35,NAT_1:11,13;
          then
A37:      w+1 in dom p by FINSEQ_3:25;
          then reconsider pw1 = p.(w+1) as DecoratedTree by TREES_3:24;
A38:      q.(w+1) = IFEQ(w+1,k+1,e2,p.(w+1)) by A9,A10,A17,A37
            .= p.(w+1) by A33,FUNCOP_1:def 8;
          w+1 in dom doms p by A37,TREES_3:37;
          then
A39:      (dom(x-tree p))|<*w*> = (doms p).(w+1) by A34,Th11
            .= dom pw1 by A37,FUNCT_6:22
            .= (doms q).(w+1) by A11,A37,A38,FUNCT_6:22;
          w+1 in dom doms q by A11,A37,TREES_3:37;
          then (dom(x-tree q))|<*w*> = (doms q).(w+1) by A14,A15,Th11;
          hence (x-tree q).f = ((x-tree q)|ww).u by A31,A32,TREES_2:def 10
            .= ((x-tree p)|ww).u by A8,A35,A36,A38,TREES_4:def 4
            .= e1.f by A2,A31,A32,A39,TREES_2:def 10;
        end;
      end;
      assume o is_a_prefix_of f or (x-tree q).f <> e1.f;
      hence thesis by A27,A28;
    end;
    suppose
      ex t1 being Element of dom e2 st f=o9^t1;
      then consider r being Element of dom e2 such that
A40:  f = o^r;
      assume o is_a_prefix_of f or (x-tree q).f <> e1.f;
A41:  (x-tree q)|o = q.(k+1) by A8,A3,A22,TREES_4:def 4;
      reconsider r as FinSequence of NAT;
      take r;
      thus
A42:  r in dom e2;
      thus f = o^r by A40;
      r in (dom(x-tree q))|o by A1,A25,A42,TREES_1:33;
      hence thesis by A24,A40,A41,TREES_2:def 10;
    end;
  end;
  hence e1 with-replacement (o,e2) = x-tree q by A1,A25,TREES_2:def 11;
  thus len q = len p by A8;
  thus q.(k+1) = e2 by A24;
  let i be Nat;
  assume i in dom p;
  then q.i = IFEQ(i,k+1,e2,p.i) by A9,A10,A17;
  hence thesis by FUNCOP_1:def 8;
end;
