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:4
  ex q st p = (p|n)^q
proof
  now
    per cases;
    suppose
      n > len p;
      then Segm len p c= Segm n by NAT_1:39;
      then
A1:   p|n=p by RELAT_1:68;
      p^{}=p;
      hence thesis by A1;
    end;
    suppose
      n <= len p;
      then reconsider n1=len p-n as Element of NAT by NAT_1:21;
      defpred P[Nat] means for k st k= len p-$1 holds ex q st p=(p|k)^q;
A2:   for m be Nat st P[m] holds P[m+1]
      proof
        let m be Nat such that
A3:     P[m];
        let k such that
A4:     k = len p-(m+1);
        consider q such that
A5:     p=(p|(k+1))^q by A3,A4;
        Segm k c= Segm(k+1) by NAT_1:39,11;
        then
A6:     (p|(k+1))|k =p|k by RELAT_1:74;
         len p-m<=len p-0 by XREAL_1:10;
        then len (p | (k+1)) = k+1 by Th51,A4;
        then p|(k+1)=(p|(k+1))|k^<%(p|(k+1)).k%> by Th53;
        then p=(p|k)^(<%(p|(k+1)).k%>^q) by A5,A6,Th25;
        hence thesis;
      end;
      p|(len p-0)=p & p^{}=p;
      then
A7:   P[0];
A8:      for m be Nat holds P[m] from NAT_1:sch 2(A7,A2);
      n=len p-n1;
      hence thesis by A8;
    end;
  end;
  hence thesis;
end;
