 reserve x,y,z for object, X for set,
         i,k,n,m for Nat,
         R for Relation,
         P for finite Relation,
         p,q for FinSequence;

theorem Th6:
  for X be non empty set, A be non empty finite Subset of X
    for P be Function of X,bool X st
      for x st x in X holds card (P.x)=n holds
   card {F where F is Element of (k+1)-tuples_on X:
                 F.1 in A & for i st i in Seg k holds F.(i+1) in P.(F.i)}
     =
   card A * (n |^ k)
proof
   let X be non empty set;
   let A be non empty finite Subset of X;
   let P be Function of X,bool X such that
       A1:for x st x in X holds card (P.x)=n;
   defpred D[Function,Nat] means $1.1 in A & for i st i in Seg $2 holds
     $1.(i+1) in P.($1.i);
   defpred R[Nat] means card {F where F is Element of ($1+1)-tuples_on X:
     D[F,$1]} = card A * (n |^ $1);
   A2: dom P = X by FUNCT_2:def 1;
   A3: for k st R[k] holds R[k+1]
   proof
     let k;
     set k1=k+1,k2=k1+1;
     set F2={F where F is Element of k2-tuples_on X: D[F,k1]};
     set F1={F where F is Element of k1-tuples_on X: D[F,k]};
     defpred P[object,object] means
for f be FinSequence st $2=f holds f|k1 =$1;
     assume A4:R[k];
     then reconsider F1 as finite set;
     consider PP be Relation such that
       A5: for x,y being object
holds [x,y] in PP iff x in F1 & y in F2 & P[x,y]
       from RELAT_1:sch 1;
     for x st x in F1 holds card Im(PP,x) = n
     proof
       defpred FF[object,object] means
       for f be FinSequence st f=$1 holds f.k2=$2;
       let x;
       assume x in F1;
       then consider xx be Element of k1-tuples_on X such that
             A6: x=xx
         and A7: D[xx,k];
A8:for y being object st y in Im(PP,x)
   ex z being object st z in P.(xx.k1) & FF[y,z]
       proof
         let y being object such that
             A9:y in Im(PP,x);
         A10: [x,y] in PP by RELAT_1:169,A9;
         then y in F2 by A5;
         then consider yy be Element of (k1+1)-tuples_on X such that
               A11: y=yy
           and A12: D[yy,k1];
         take z=yy.(k1+1);
         A13: yy.(k1+1) in P.(yy.k1) by FINSEQ_1:3,A12;
         yy|k1=xx by A11, A10,A5,A6;
         hence thesis by FINSEQ_1:3,FUNCT_1:49,A13,A11;
       end;
       consider ff be Function of Im(PP,x),P.(xx.k1) such that
A14:for z being object st z in Im(PP,x) holds FF[z,ff.z]
from FUNCT_2:sch 1(A8);
       A15:len xx = k1 by CARD_1:def 7;
       k1 in Seg k1 by FINSEQ_1:3;
       then k1 in dom xx by A15,FINSEQ_1:def 3;
       then A16: xx.k1 in rng xx by FUNCT_1:def 3;
       then A17: card (P.(xx.k1)) = n by A1;
       A18: P.(xx.k1) in rng P by A16,A2,FUNCT_1:def 3;
       P.(xx.k1) c= rng ff
       proof
         let z be object such that
           A19: z in P.(xx.k1);
         reconsider Z=z as Element of X by A18,A19;
         set xz= xx^<*Z*>;
         A20: len xz=len xx+1 by FINSEQ_2:16;
         A21:xz|k1=xx by FINSEQ_5:23,A15;
         A22: ( P[xx,xz])& xx in F1 by FINSEQ_5:23,A15, A7;
         reconsider xz as Element of (k1+1)-tuples_on X by A20,A15,FINSEQ_2:92;
         A23: for i st i in Seg k1 holds xz.(i+1) in P.(xz.i)
         proof
           let i;
           assume A24: i in Seg k1;
           then A25: 1 <= i by FINSEQ_1:1;
           A26: i <=k1 by A24,FINSEQ_1:1;
           per cases by A26,NAT_1:8;
             suppose A27: i <=k;
               1<=1+i by NAT_1: 11;
               then i+1 in dom xx by A27,XREAL_1:6,A15, FINSEQ_3:25;
               then A28: xz.(i+1)=xx.(i+1) by FINSEQ_1:def 7;
               i<k1 by A27,NAT_1: 13;
               then A29: i in dom xx by A25,A15, FINSEQ_3:25;
               xx.(i+1) in P.(xx.i) by A27,A25,FINSEQ_1:1,A7;
               hence thesis by A28, A29,FINSEQ_1:def 7;
             end;
             suppose A30:i = k1;
               then xz.i=xx.i by A21, FINSEQ_1:3,FUNCT_1:49;
               hence thesis by A30,FINSEQ_1:42, A15,A19;
             end;
         end;
         1 <= k1 by NAT_1:11;
         then 1 in dom xx by FINSEQ_3:25,A15;
         then xz.1=xx.1 by FINSEQ_1:def 7;
         then xz in F2 by A23, A7;
         then A31: [xx,xz] in PP by A5,A22;
         then A32:xz in Im(PP,xx) by RELAT_1:169;
         ex d be object st d in P.(xx.k1) & FF[xz,d]
by A31,RELAT_1:169,A6,A8;
         then A33: dom ff = Im(PP,xx) by FUNCT_2:def 1,A6;
         ff.xz = xz.(k1+1) by A31,RELAT_1:169,A14,A6
              .= z by FINSEQ_1:42,A15;
         hence thesis by A33,A32,FUNCT_1:def 3;
       end;
       then A34:P.(xx.k1) = rng ff;
       per cases;
         suppose A35:P.(xx.k1) is empty;
           Im(PP,x) is empty
           proof
             assume Im(PP,x) is non empty;
             then consider d be object such that
               A36:d in Im(PP,x);
             ex z being object st z in P.(xx.k1) & FF[d,z] by A36,A8;
             hence contradiction by A35;
           end;
           hence thesis by A35, A16,A1;
         end;
         suppose A37: P.(xx.k1) is non empty;
           A38: ff is one-to-one
           proof
             let x1,x2 be object;
             assume that
                   A39: x1 in dom ff
               and A40: x2 in dom ff
               and A41: ff.x1=ff.x2;
             A42: [xx,x1] in PP by A39,RELAT_1:169,A6;
             then x1 in F2 by A5;
             then consider f1 be Element of (k1+1)-tuples_on X such that
               A43: x1=f1
             and D[f1,k1];
             A44: len f1 = k1+1 by CARD_1 :def 7;
             f1|k1 = xx by A42,A43,A5;
             then A45: f1 = xx^<*f1.k2*> by A44, FINSEQ_3:55;
             A46: [xx,x2] in PP by A40,RELAT_1:169,A6;
             then x2 in F2 by A5;
             then consider f2 be Element of (k1+1)-tuples_on X such that
                   A47: x2=f2
               and D[f2,k1];
             A48: len f2=k1+1 by CARD_1: def 7;
             f2|k1 = xx by A46,A47,A5;
             then A49: f2 = xx^<*f2.k2*> by A48, FINSEQ_3:55;
             f1.k2 =ff.x1 by A39,A14,A43;
             hence thesis by A45,A49, A40,A14,A47,A41, A43;
           end;
         dom ff = Im(PP,x) by A37,FUNCT_2:def 1;
         hence thesis by A38,A34,WELLORD2:def 4,CARD_1:5,A17;
        end;
      end;
      then A50:card PP = card (PP| (dom PP\F1)) +`n*`card F1 by SIMPLEX1:1;
      dom PP c= F1
      proof
        let y be object;
        assume y in dom PP;
        then ex z being object st [y,z] in PP by XTUPLE_0:def 12;
        hence thesis by A5;
      end;
      then dom PP\F1={} by XBOOLE_1:37;
      then card (PP| (dom PP\F1))=0;
      then card PP = n*`card F1 by A50,CARD_2:18;
      then A51:card PP = n * card F1 by Lm1
          .= (card A)*(n*(n|^k)) by A4
          .= (card A)*(n|^k1) by NEWTON:6;
      A52:for f2 be Element of k2-tuples_on X st D[f2,k1] holds
          [f2|k1,f2] in PP
      proof
        let f2 be Element of k2-tuples_on X such that
            A53: D[f2,k1];
        A54:f2 in F2 by A53;
        set f1=f2|k1;
        A55:P[f1,f2];
        len f2 = k2 & k1 < k2 by CARD_1:def 7, NAT_1:13;
        then len f1 = k1 by FINSEQ_1:59;
        then reconsider f1 as Element of k1-tuples_on X by FINSEQ_2:92;
        A56: for i st i in Seg k holds f1.(i+1) in P.(f1.i)
        proof
          let i;
          set i1=i+1;
          assume A57: i in Seg k;
          then A58: 1<= i by FINSEQ_1:1;
          A59: i <= k by A57,FINSEQ_1:1;
          then i <k1 by NAT_1:13;
          then A60: f2.i1 in P.(f2.i) & f2.i=f1.i
            by A58,FINSEQ_1:1,A53,FUNCT_1 :49;
          1<=i1 by NAT_1:11;
          then i1 in Seg k1 by A59,XREAL_1:6,FINSEQ_1:1;
          hence thesis by A60,FUNCT_1:49;
        end;
        1<= k1 by NAT_1:11;
        then 1 in Seg k1;
        then D[f1,k] by A56,FUNCT_1:49, A53;
        then f1 in F1;
        hence thesis by A54,A55,A5;
      end;
      per cases;
        suppose n=0;
          then A61:n|^k1=0 by NAT_1:11,NEWTON:11;
          then A62:PP is empty by A51;
          F2 is empty
          proof
            assume F2 is non empty;
            then consider x2 be object such that
              A63:x2 in F2;
            ex f2 be Element of k2-tuples_on X st x2=f2 & D[f2,k1] by A63;
            hence contradiction by A52,A62;
          end;
        hence thesis by A61;
      end;
      suppose A64:n>0;
        defpred PR[object,object] means for x,y st $1=[x,y] holds $2=y;
A65:for y being object st y in PP ex z being object st z in F2 & PR[y,z]
        proof
          let y be object;
          assume A66:y in PP;
          then consider y1,y2 be object such that
            A67: y=[y1,y2] by RELAT_1:def 1;
          take y2;
          thus thesis by XTUPLE_0:1,A66,A67,A5;
        end;
        consider pr be Function of PP,F2 such that
A68:for y being object st y in PP holds PR[y,pr.y] from FUNCT_2:sch 1(A65);
        A69:pr is one-to-one
        proof
          let x1,x2 be object;
          assume that
                A70: x1 in dom pr
            and A71: x2 in dom pr
            and A72: pr.x1=pr.x2;
          consider y1,z1 be object such that
            A73:x1=[y1,z1] by A70,RELAT_1:def 1;
          A74: pr.x1=z1 by A68,A70,A73;
          consider y2,z2 be object such that
            A75:x2=[y2,z2] by A71,RELAT_1:def 1;
          A76: pr.x2 =z2 by A68,A71,A75;
          z1 in F2 by A73,A70,A5;
          then consider f1 be Element of k2-tuples_on X such that
                A77: z1=f1
            and D[f1,k1];
          f1|k1 = y1 by A73,A70,A5,A77;
          hence thesis by A71,A5,A75,A72,A73, A74,A76,A77;
        end;
        F2 is non empty
        proof
          n|^k1>0 by NEWTON:83,A64;
          then PP is non empty by A51, XREAL_1:129;
          then consider x being object such that
            A78:x in PP;
          ex y,z being object st x=[y,z] by RELAT_1:def 1,A78;
          hence thesis by A78,A5;
        end;
        then A79:dom pr = PP by FUNCT_2:def 1;
        F2 c= rng pr
        proof
          let x2 be object;
          assume x2 in F2;
          then consider f2 be Element of k2-tuples_on X such that
                A80: x2=f2
            and A81: D[f2,k1];
          [f2|k1,f2] in PP & pr.[f2|k1,f2] = f2 by A81,A52,A68;
          hence thesis by A79,FUNCT_1:def 3,A80;
        end;
        then F2 = rng pr;
        hence thesis by A69,A79,WELLORD2:def 4,A51,CARD_1:5;
      end;
    end;
    A82: R[0]
    proof
      deffunc P(object) = <*$1*>;
      set F0={F where F is Element of (0+1)-tuples_on X: D[F,0]};
      A83:for x being object st x in A holds P(x) in F0
      proof
        let x be object;
        assume A84:x in A;
        A85: len P(x) = 1 by FINSEQ_1:39;
        rng P(x) ={x} by FINSEQ_1:38;
        then P(x) is FinSequence of X by A84,ZFMISC_1:31,FINSEQ_1:def 4;
        then reconsider Px=P(x) as Element of (0+1)-tuples_on X
          by A85,FINSEQ_2:133;
        D[Px,0] by A84;
        hence thesis;
      end;
      consider f be Function of A,F0 such that
A86: for x being object st x in A holds f.x = P(x) from FUNCT_2:sch 2(A83);
      P(the Element of A) in F0 by A83;
      then A87:dom f = A by FUNCT_2:def 1;
      F0 c= rng f
      proof
        let x be object;
        assume x in F0;
        then A88:ex F be Element of (0+1)-tuples_on X st x=F & D[F,0];
        then consider y be Element of X such that
          A89: x=<*y*> by FINSEQ_2:97;
        f.y = x by A88,A89,A86;
        hence thesis by A88,A89,A87,FUNCT_1:def 3;
      end;
      then A91: rng f = F0;
      A92:f is one-to-one
      proof
        let x1,x2 be object;
        assume that
              A93: x1 in dom f & x2 in dom f
          and A94: f.x1=f.x2;
        A95: f.x1=P(x1) & f.x2=P(x2) by A93,A86;
        P(x1).1=x1;
        hence thesis by A95,FINSEQ_1:40,A94;
      end;
      n|^0 =1 by NEWTON:4;
      hence thesis by A91,WELLORD2:def 4,A87,A92,CARD_1:5;
    end;
    for k holds R[k] from NAT_1:sch 2(A82,A3);
    hence thesis;
end;
