reserve k,n for Nat,
  x,y,z,y1,y2 for object,X,Y for set,
  f,g for Function;
reserve p,q,r,s,t for XFinSequence;
reserve D for set;

theorem :: CATALAN2:3
  n = dom p + k implies (p^q)|n = p^(q|k)
proof
  assume
A1: n = dom p + k;
  now
    per cases;
    suppose
A2:   n>=len (p^q);
      then n>=len p+len q by Def3;
      then Segm len q c= Segm k by NAT_1:39,A1,XREAL_1:8;
      then
A3:   q|k = q by RELAT_1:68;
      Segm len(p^q) c= Segm n by A2,NAT_1:39;
      hence thesis by A3,RELAT_1:68;
    end;
    suppose
A4:   n<len (p^q);
      then
A5:   len ((p^q)|n)=n by Th10;
      n<len p+len q by A4,Def3;
      then k < len q by A1,XREAL_1:6;
      then len (q|k)=k by Th10;
      then
A6:   len (p^(q|k))=len p + k by Def3;
      now
        let m be Nat such that
A7:     m in dom ((p^q)|n);
A8:     m < len ((p^q)|n) by A7,Lm1;then
        m <len (p^q) by A4,A5,XXREAL_0:2;
        then
A9:     m in len (p^q) by Lm1;
        m in n by A4,Th10,A7;
        then
A10:     m in dom (p^q) /\ n by A9,XBOOLE_0:def 4;
        then
A11:    ((p^q)|n).m=(p^q).m by FUNCT_1:48;
        now
          per cases;
          suppose
            m<len p;
            then m in dom p by Lm1;
            then (p^(q|k)).m=p.m & (p^q).m=p.m by Def3;
            hence ((p^q)|n).m=(p^(q|k)).m by A10,FUNCT_1:48;
          end;
          suppose
A12:        m>=len p;
            m < len (p^q) by A4,A5,A8,XXREAL_0:2;
            then
A13:        q.(m-len p)=(p^q).m by A12,Th17;
A14:        m-len p+len p< len (p^q) by A4,A5,A8,XXREAL_0:2;
A15:        m-len p is Nat by A12,NAT_1:21;
            len (p^q)=len p+len q by Def3;
            then m-len p<len q by A14,XREAL_1:6;
            then
A16:        m-len p in len q by A15,Lm1;
            m-len p < k by A1,A5,A14,A8,XREAL_1:6;
            then m-len p in Segm k by A15,NAT_1:44;
            then
A17:        m-len p in k/\dom q by A16,XBOOLE_0:def 4;
            (p^(q|k)).m=(q|k).(m-len p) by A1,A6,A5,A12,A8,Th17;
            hence ((p^q)|n).m=(p^(q|k)).m by A11,A13,A17,FUNCT_1:48;
          end;
        end;
        hence ((p^q)|n).m=(p^(q|k)).m;
      end;
      hence thesis by A6,A1,A4,Th10;
    end;
  end;
  hence thesis;
end;
