reserve n,m,k for Nat,
  x,y for set,
  r for Real;
reserve C,D for non empty finite set,
  a for FinSequence of bool D;

theorem Th23:
  for F be PartFunc of D,REAL, A be RearrangmentGen of C st F is
  total & card C = card D holds Rlor(F,A), FinS(F,D) are_fiberwise_equipotent
proof
  let F be PartFunc of D,REAL, B be RearrangmentGen of C;
  set fd = FinS(F,D), p = Rlor(F,B), mf = MIM(fd), b = Co_Gen B, h = CHI(b,C);
A1: len h = len b & len mf = len fd by RFINSEQ:def 2,RFUNCT_3:def 6;
  assume
A2: F is total & card C = card D;
  then
A3: rng p = rng fd by Th22;
A4: dom p = C by A2,Th20;
  then reconsider p as finite PartFunc of C,REAL by FINSET_1:10;
A5: len mf = len h by A2,Th11;
A6: dom fd = Seg len fd & dom b = Seg len b by FINSEQ_1:def 3;
  now
    let x be object;
    now
      per cases;
      case
A7:     not x in rng fd;
        then fd"{x}={} by Lm1;
        hence card (fd"{x}) = card(p"{x}) by A3,A7,Lm1;
      end;
      case
A8:     x in rng fd;
        defpred P[Nat] means $1 in dom fd & fd.$1 = x;
A9:     ex n be Nat st P[n] by A8,FINSEQ_2:10;
        consider mi be Nat such that
A10:    P[mi] & for n be Nat st P[n] holds mi<=n from NAT_1:sch 5(A9
        );
        reconsider r=x as Real by A10;
A11:    1<=mi by A10,FINSEQ_3:25;
        then max(0,mi-1)=mi-1 by FINSEQ_2:4;
        then reconsider m1 = mi -1 as Element of NAT by FINSEQ_2:5;
A12:    m1+1=mi;
A13:    for n be Nat st P[n] holds n<=len fd by FINSEQ_3:25;
        consider ma be Nat such that
A14:    P[ma] & for n be Nat st P[n] holds n<=ma from NAT_1:sch 6(A13
        ,A9);
A15:    ma<=len fd by A14,FINSEQ_3:25;
A16:    Seg ma \ Seg m1 = fd"{x}
        proof
          thus Seg ma \ Seg m1 c= fd"{x}
          proof
            let y be object;
            assume
A17:        y in Seg ma \ Seg m1;
            then reconsider l=y as Element of NAT;
            reconsider o=fd.l as Real;
A18:        y in Seg ma by A17,XBOOLE_0:def 5;
            then
A19:        1<=l by FINSEQ_1:1;
A20:        l<=ma by A18,FINSEQ_1:1;
            then l<=len fd by A15,XXREAL_0:2;
            then
A21:        l in dom fd by A19,FINSEQ_3:25;
            not y in Seg m1 by A17,XBOOLE_0:def 5;
            then l<1 or m1<l by FINSEQ_1:1;
            then mi<l+1 by A18,FINSEQ_1:1,XREAL_1:19;
            then
A22:        mi<=l by NAT_1:13;
            now
              per cases;
              case
                l=ma;
                then o in {x} by A14,TARSKI:def 1;
                hence thesis by A21,FUNCT_1:def 7;
              end;
              case
                l<>ma;
                then l<ma by A20,XXREAL_0:1;
                then
A23:            o>=r by A14,A21,RFINSEQ:19;
                now
                  per cases;
                  case
                    l=mi;
                    then o in {x} by A10,TARSKI:def 1;
                    hence thesis by A21,FUNCT_1:def 7;
                  end;
                  case
                    l<>mi;
                    then mi<l by A22,XXREAL_0:1;
                    then r>=o by A10,A21,RFINSEQ:19;
                    then r=o by A23,XXREAL_0:1;
                    then o in {x} by TARSKI:def 1;
                    hence thesis by A21,FUNCT_1:def 7;
                  end;
                end;
                hence thesis;
              end;
            end;
            hence thesis;
          end;
          let y be object;
          assume
A24:      y in fd"{x};
          then
A25:      y in dom fd by FUNCT_1:def 7;
          reconsider l=y as Element of NAT by A24;
A26:      1<=l by A25,FINSEQ_3:25;
          fd.y in {x} by A24,FUNCT_1:def 7;
          then
A27:      fd.l=x by TARSKI:def 1;
          then mi<=l by A10,A25;
          then mi<l+1 by NAT_1:13;
          then m1<l or l<1 by XREAL_1:19;
          then
A28:      not l in Seg m1 by FINSEQ_1:1;
          l<=ma by A14,A25,A27;
          then l in Seg ma by A26,FINSEQ_1:1;
          hence thesis by A28,XBOOLE_0:def 5;
        end;
A29:    1<=ma by A14,FINSEQ_3:25;
A30:    mi<=ma by A10,A14;
        m1<=mi by XREAL_1:43;
        then
A31:    m1<=ma by A30,XXREAL_0:2;
        then Seg m1 c= Seg ma by FINSEQ_1:5;
        then
A32:    card(fd"{x}) = card(Seg ma) - card(Seg m1) by A16,CARD_2:44
          .= ma - card(Seg m1) by FINSEQ_1:57
          .= ma - m1 by FINSEQ_1:57;
A33:    mi<=len fd by A10,FINSEQ_3:25;
        then 1<=len fd by A11,XXREAL_0:2;
        then
A34:    1 in dom fd by FINSEQ_3:25;
        now
          per cases;
          case
A35:        mi=1;
            b.ma = p"{x}
            proof
              thus b.ma c= p"{x}
              proof
                defpred P[Nat] means $1 in dom b & $1<=ma implies
                for c be Element of C st c in b.$1 holds c in p"{x};
                let y be object;
                assume
A36:            y in b.ma;
                b.ma c= C by A5,A1,A6,A14,Lm5;
                then reconsider cy = y as Element of C by A36;
A37:            for n st P[n] holds P[n+1]
                proof
                  let n;
                  assume
A38:              P[n];
                  assume that
A39:              n+1 in dom b and
A40:              n+1<=ma;
A41:              n+1<=len b by A39,FINSEQ_3:25;
                  then
A42:              n<=len b -1 by XREAL_1:19;
A43:              n<len b by A41,NAT_1:13;
                  let c be Element of C;
                  assume
A44:              c in b.(n+1);
A45:              n<=len b by A41,NAT_1:13;
                  now
                    per cases;
                    case
                      n=0;
                      then p.c = x by A2,A10,A35,A44,Th21;
                      then p.c in {x} by TARSKI:def 1;
                      hence thesis by A4,FUNCT_1:def 7;
                    end;
                    case
A46:                  n<>0;
                      then
A47:                  0+1<n+1 by XREAL_1:6;
A48:                  0+1<=n by A46,NAT_1:13;
                      then b.n c= b.(n+1) by A42,Def2;
                      then
A49:                  b.(n+1) = b.(n+1) \/ b.n by XBOOLE_1:12
                        .= (b.(n+1) \ b.n) \/ b.n by XBOOLE_1:39;
                      now
                        per cases by A44,A49,XBOOLE_0:def 3;
                        case
                          c in b.(n+1) \ b.n;
                          then
A50:                      p.c = fd.(n+1) by A2,A43,A48,Th21;
                          now
                            per cases;
                            case
                              n+1=ma;
                              then p.c in {x} by A14,A50,TARSKI:def 1;
                              hence thesis by A4,FUNCT_1:def 7;
                            end;
                            case
                              n+1<>ma;
                              then n+1<ma by A40,XXREAL_0:1;
                              then
A51:                          p.c>=r by A5,A1,A6,A14,A39,A50,RFINSEQ:19;
                              r>=p.c by A5,A1,A6,A10,A35,A39,A47,A50,RFINSEQ:19
;
                              then r=p.c by A51,XXREAL_0:1;
                              then p.c in {x} by TARSKI:def 1;
                              hence thesis by A4,FUNCT_1:def 7;
                            end;
                          end;
                          hence thesis;
                        end;
                        case
                          c in b.n;
                          hence thesis by A38,A40,A45,A48,FINSEQ_3:25,NAT_1:13;
                        end;
                      end;
                      hence thesis;
                    end;
                  end;
                  hence thesis;
                end;
A52:            P[ 0 ] by FINSEQ_3:25;
A53:            for n holds P[n] from NAT_1:sch 2(A52,A37);
                ma in dom b by A5,A1,A14,FINSEQ_3:29;
                then cy in p"{x} by A36,A53;
                hence thesis;
              end;
              let y be object;
              assume
A54:          y in p"{x};
              then reconsider cy = y as Element of C;
              p.cy in {x} by A54,FUNCT_1:def 7;
              then
A55:          p.cy = x by TARSKI:def 1;
              defpred Q[Nat] means $1 in dom b & ma<$1 & cy in b.$1;
              assume
A56:          not y in b.ma;
A57:          ex n be Nat st Q[n]
              proof
                take n=len b;
                len b <> 0 by Th4;
                then 0+1<=len b by NAT_1:13;
                hence n in dom b by FINSEQ_3:25;
A58:            b.n= C by Th3;
                now
                  assume n <= ma;
                  then ma=len b by A5,A1,A15,XXREAL_0:1;
                  then cy in b.ma by A58;
                  hence contradiction by A56;
                end;
                hence thesis by A58;
              end;
              consider mm be Nat such that
A59:          Q[mm] & for n be Nat st Q[n] holds mm<=n from NAT_1:
              sch 5(A57 );
              1<=mm by A59,FINSEQ_3:25;
              then max(0,mm-1)=mm-1 by FINSEQ_2:4;
              then reconsider 1m = mm - 1 as Element of NAT by FINSEQ_2:5;
              ma+1<=mm by A59,NAT_1:13;
              then
A60:          ma<=1m by XREAL_1:19;
              then
A61:          1<=1m by A29,XXREAL_0:2;
A62:          mm in dom fd by A5,A1,A59,FINSEQ_3:29;
A63:          mm<=len b by A59,FINSEQ_3:25;
A64:          mm<mm+1 by NAT_1:13;
              then 1m<mm by XREAL_1:19;
              then
A65:          1m<len b by A63,XXREAL_0:2;
              1m<=mm by A64,XREAL_1:19;
              then 1m<=len b by A63,XXREAL_0:2;
              then
A66:          1m in dom b by A61,FINSEQ_3:25;
              now
                per cases;
                case
                  ma=1m;
                  then cy in b.(1m+1) \ b.(1m) by A56,A59,XBOOLE_0:def 5;
                  then p.cy = fd.mm by A2,A61,A65,Th21;
                  hence contradiction by A14,A55,A59,A62;
                end;
                case
                  ma <> 1m;
                  then
A67:              ma < 1m by A60,XXREAL_0:1;
                  now
                    assume cy in b.(1m);
                    then mm<=1m by A59,A66,A67;
                    then mm - 1m <=0 by XREAL_1:47;
                    hence contradiction;
                  end;
                  then cy in b.(1m+1) \ b.(1m) by A59,XBOOLE_0:def 5;
                  then p.cy = fd.mm by A2,A61,A65,Th21;
                  hence contradiction by A14,A55,A59,A62;
                end;
              end;
              hence contradiction;
            end;
            hence card(p"{x}) = card(fd"{x}) by A5,A1,A29,A15,A32,A35,Def1;
          end;
          case
A68:        mi<>1;
            then 1<mi by A11,XXREAL_0:1;
            then
A69:        1<=m1 by A12,NAT_1:13;
A70:        m1<=len fd by A33,A12,NAT_1:13;
            then
A71:        m1 in dom b by A5,A1,A69,FINSEQ_3:25;
            then
A72:        b.m1 c= b.ma by A5,A1,A6,A14,A31,Th2;
            b.ma c= C by A5,A1,A6,A14,Lm5;
            then reconsider bma = b.ma, bm1 = b.m1 as finite set by A72;
            b.ma \ b.m1 = p"{x}
            proof
              thus b.ma \ b.m1 c= p"{x}
              proof
                defpred P[Nat] means $1 in dom b & mi<=$1 & $1<=ma
implies for c be Element of C st c in b.$1 \ b.m1 holds c in p"{x};
                let y be object;
A73:            b.ma c= C by A5,A1,A6,A14,Lm5;
A74:            for n st P[n] holds P[n+1]
                proof
                  let n;
                  assume
A75:              P[n];
                  assume that
A76:              n+1 in dom b and
A77:              mi<=n+1 and
A78:              n+1<=ma;
                  let c be Element of C;
                  n+1<=len b by A76,FINSEQ_3:25;
                  then
A79:              n<len b by NAT_1:13;
                  assume
A80:              c in b.(n+1) \ b.m1;
                  now
                    per cases;
                    case
A81:                  n+1=mi;
                      then p.c = fd.(n+1) by A2,A69,A79,A80,Th21;
                      then p.c in {x} by A10,A81,TARSKI:def 1;
                      hence thesis by A4,FUNCT_1:def 7;
                    end;
                    case
                      mi <> n+1;
                      then
A82:                  mi<n+1 by A77,XXREAL_0:1;
                      then mi<=n by NAT_1:13;
                      then
A83:                  1<=n by A11,XXREAL_0:2;
                      n<=ma by A78,NAT_1:13;
                      then
A84:                  n<=len b by A5,A1,A15,XXREAL_0:2;
                      then n<=n+1 & n in dom b by A83,FINSEQ_3:25,NAT_1:11;
                      then b.n c= b.(n+1) by A76,Th2;
                      then
A85:                  b.(n+1) \ b.m1 = (b.(n+1) \/ b .n) \ b.m1 by XBOOLE_1:12
                        .= ((b.(n+1) \b.n) \/ b.n) \ b.m1 by XBOOLE_1:39
                        .= (b.(n+1) \ b.n)\ b.m1 \/ (b.n \ b.m1) by XBOOLE_1:42
;
                      now
                        per cases by A80,A85,XBOOLE_0:def 3;
                        case
                          c in (b.(n+1) \ b.n) \ b.m1;
                          then c in b.(n+1) \ b.n by XBOOLE_0:def 5;
                          then
A86:                      p.c = fd.(n+1) by A2,A79,A83,Th21;
                          now
                            per cases;
                            case
                              n+1=ma;
                              then p.c in {x} by A14,A86,TARSKI:def 1;
                              hence thesis by A4,FUNCT_1:def 7;
                            end;
                            case
                              n+1<>ma;
                              then n+1<ma by A78,XXREAL_0:1;
                              then
A87:                          p.c>=r by A5,A1,A6,A14,A76,A86,RFINSEQ:19;
                              r>=p.c by A5,A1,A6,A10,A76,A82,A86,RFINSEQ:19;
                              then r=p.c by A87,XXREAL_0:1;
                              then p.c in {x} by TARSKI:def 1;
                              hence thesis by A4,FUNCT_1:def 7;
                            end;
                          end;
                          hence thesis;
                        end;
                        case
                          c in b.n \ b.m1;
                          hence thesis by A75,A78,A82,A83,A84,FINSEQ_3:25
,NAT_1:13;
                        end;
                      end;
                      hence thesis;
                    end;
                  end;
                  hence thesis;
                end;
A88:            P[ 0 ];
A89:            for n holds P[n] from NAT_1:sch 2(A88,A74);
                assume
A90:            y in b.ma \ b.m1;
                then y in b.ma;
                then reconsider cy = y as Element of C by A73;
                ma in dom b by A5,A1,A14,FINSEQ_3:29;
                then cy in p"{x} by A30,A90,A89;
                hence thesis;
              end;
              let y be object;
              assume
A91:          y in p"{x};
              then reconsider cy = y as Element of C;
              p.cy in {x} by A91,FUNCT_1:def 7;
              then
A92:          p.cy = x by TARSKI:def 1;
              assume
A93:          not y in b.ma \ b.m1;
              now
                per cases by A93,XBOOLE_0:def 5;
                case
A94:              not y in b.ma;
                  defpred Q[Nat] means $1 in dom b & ma<$1 & cy in b.$1;
A95:              ex n be Nat st Q[n]
                  proof
                    take n=len b;
                    len b <> 0 by Th4;
                    then 0+1<=len b by NAT_1:13;
                    hence n in dom b by FINSEQ_3:25;
A96:                b.n= C by Th3;
                    now
                      assume n <= ma;
                      then ma=len b by A5,A1,A15,XXREAL_0:1;
                      then cy in b.ma by A96;
                      hence contradiction by A94;
                    end;
                    hence thesis by A96;
                  end;
                  consider mm be Nat such that
A97:              Q [mm] & for n be Nat st Q[n] holds mm<=n from
                  NAT_1:sch 5(A95);
                  1<=mm by A97,FINSEQ_3:25;
                  then max(0,mm-1)=mm-1 by FINSEQ_2:4;
                  then reconsider 1m = mm - 1 as Element of NAT by FINSEQ_2:5;
                  ma+1<=mm by A97,NAT_1:13;
                  then
A98:              ma<=1m by XREAL_1:19;
                  then
A99:              1<=1m by A29,XXREAL_0:2;
A100:             mm in dom fd by A5,A1,A97,FINSEQ_3:29;
A101:             mm<=len b by A97,FINSEQ_3:25;
A102:             mm<mm+1 by NAT_1:13;
                  then 1m<mm by XREAL_1:19;
                  then
A103:             1m<len b by A101,XXREAL_0:2;
                  1m<=mm by A102,XREAL_1:19;
                  then 1m<=len b by A101,XXREAL_0:2;
                  then
A104:             1m in dom b by A99,FINSEQ_3:25;
                  now
                    per cases;
                    case
                      ma=1m;
                      then cy in b.(1m+1) \ b.(1m) by A94,A97,XBOOLE_0:def 5;
                      then p.cy = fd.mm by A2,A99,A103,Th21;
                      hence contradiction by A14,A92,A97,A100;
                    end;
                    case
                      ma <> 1m;
                      then
A105:                 ma < 1m by A98,XXREAL_0:1;
                      now
                        assume cy in b.(1m);
                        then mm<=1m by A97,A104,A105;
                        then mm - 1m <=0 by XREAL_1:47;
                        hence contradiction;
                      end;
                      then cy in b.(1m+1) \ b.(1m) by A97,XBOOLE_0:def 5;
                      then p.cy = fd.mm by A2,A99,A103,Th21;
                      hence contradiction by A14,A92,A97,A100;
                    end;
                  end;
                  hence contradiction;
                end;
                case
A106:             y in b.m1;
                  defpred Q[Nat] means $1 in dom b & $1<=m1 & cy in b.$1;
A107:             ex n be Nat st Q[n] by A71,A106;
                  consider mm be Nat such that
A108:             Q [mm] & for n be Nat st Q[n] holds mm<=n from
                  NAT_1:sch 5(A107);
A109:             1<=mm by A108,FINSEQ_3:25;
                  then max(0,mm-1)=mm-1 by FINSEQ_2:4;
                  then reconsider 1m=mm-1 as Element of NAT by FINSEQ_2:5;
A110:             mm<=len b by A108,FINSEQ_3:25;
A111:             mm in dom fd by A5,A1,A108,FINSEQ_3:29;
                  now
                    per cases;
                    case
                      mm=1;
                      then p.cy = fd.1 by A2,A108,Th21;
                      then mi<=1 by A10,A34,A92;
                      hence contradiction by A11,A68,XXREAL_0:1;
                    end;
                    case
                      mm<>1;
                      then 1<mm by A109,XXREAL_0:1;
                      then 1+1<=mm by NAT_1:13;
                      then
A112:                 1<=1m by XREAL_1:19;
A113:                 mm<mm+1 by NAT_1:13;
                      then
A114:                 1m<=mm by XREAL_1:19;
                      then 1m<=len b by A110,XXREAL_0:2;
                      then
A115:                 1m in dom b by A112,FINSEQ_3:25;
A116:                 1m<mm by A113,XREAL_1:19;
                      then
A117:                 1m<len b by A110,XXREAL_0:2;
                      1m<=m1 by A108,A114,XXREAL_0:2;
                      then not cy in b.1m by A108,A116,A115;
                      then cy in b.(1m+1) \ b.1m by A108,XBOOLE_0:def 5;
                      then p.cy = fd.(1m+1) by A2,A112,A117,Th21;
                      then mi<=mm by A10,A92,A111;
                      then m1+1<=m1 by A108,XXREAL_0:2;
                      then 1<=m1-m1 by XREAL_1:19;
                      then 1<=0;
                      hence contradiction;
                    end;
                  end;
                  hence contradiction;
                end;
              end;
              hence contradiction;
            end;
            hence card(p"{x}) = card(bma) - card(bm1) by A72,CARD_2:44
              .= ma - card(bm1) by A5,A1,A29,A15,Def1
              .= card (fd"{x}) by A5,A1,A32,A69,A70,Def1;
          end;
        end;
        hence card (fd"{x}) = card(p"{x});
      end;
    end;
    hence card Coim(fd,x) = card Coim(p,x);
  end;
  hence thesis by CLASSES1:def 10;
end;
