reserve i, j, m, n for Nat,
  z, B0 for set,
  f, x0 for real-valued FinSequence;

theorem Th22:
  for g being FinSequence of REAL n, h being FinSequence of NAT,
      F being FinSequence of REAL n st
   h is increasing & rng h c= dom g & F=g*h &
   (for i being Element of NAT st i in dom g & not i in rng h holds g.i= 0*n)
   holds Sum g=Sum F
proof
  let g be FinSequence of REAL n, h be FinSequence of NAT,
      F be FinSequence of REAL n;
  assume that
A1: h is increasing and
A2: rng h c= dom g and
A3: F=g*h and
A4: for i being Element of NAT st i in dom g & not i in rng h holds g.i= 0*n;
A5: dom((h qua Relation)*(g qua Relation))=dom h by A2,RELAT_1:27;
  dom (g*h)=dom h by A2,RELAT_1:27;
  then
A6: dom F=Seg len h by A3,FINSEQ_1:def 3;
  then
A7: len F=len h by FINSEQ_1:def 3;
  dom h c=dom g
  proof
    let x be object;
    assume
A8: x in dom h;
    then reconsider nx=x as Element of NAT;
A9: h.x in rng h by A8,FUNCT_1:def 3;
    then reconsider nhx=h.x as Element of NAT;
A10: nx in Seg len h by A8,FINSEQ_1:def 3;
    then
A11: 1<=nx by FINSEQ_1:1;
A12: nx<=len h by A10,FINSEQ_1:1;
    then 1<=len h by A11,XXREAL_0:2;
    then 1 in Seg len h by FINSEQ_1:1;
    then 1 in dom h by FINSEQ_1:def 3;
    then h.1 in rng h by FUNCT_1:def 3;
    then h.1 in dom g by A2;
    then h.1 in Seg len g by FINSEQ_1:def 3;
    then 1<=h.1 by FINSEQ_1:1;
    then
A13: nx<=nhx by A1,A12,Th7;
    h.x in dom g by A2,A9;
    then h.x in Seg len g by FINSEQ_1:def 3;
    then nhx <=len g by FINSEQ_1:1;
    then nx <=len g by A13,XXREAL_0:2;
    then nx in Seg len g by A11,FINSEQ_1:1;
    hence x in dom g by FINSEQ_1:def 3;
  end;
  then dom h c= Seg len g by FINSEQ_1:def 3;
  then Seg len h c= Seg len g by FINSEQ_1:def 3;
  then
A14: len h <= len g by FINSEQ_1:5;
  per cases;
  suppose
A15: len F>0;
    then
A16: 0+1<=len F by NAT_1:13;
    then 1 in Seg len F by FINSEQ_1:1;
    then
A17: 1 in dom F by FINSEQ_1:def 3;
    then
A18: 1 in Seg len h by A3,A5,FINSEQ_1:def 3;
    then
A19: 1<=len h by FINSEQ_1:1;
    then len h in Seg len h by FINSEQ_1:1;
    then len h in dom h by FINSEQ_1:def 3;
    then h.(len h) in rng h by FUNCT_1:def 3;
    then h.(len h) in dom g by A2;
    then
A20: h.(len h) in Seg len g by FINSEQ_1:def 3;
    reconsider j=h.1 as Nat;
    dom((h qua Relation)*(g qua Relation))=dom h by A2,RELAT_1:27;
    then
A21: Seg len F=dom h by A3,FINSEQ_1:def 3;
    then
A22: len F=len h by FINSEQ_1:def 3;
A23: h.1 in rng h by A3,A5,A17,FUNCT_1:def 3;
    then
A24: h.1 in dom g by A2;
    then
A25: h.1 in Seg len g by FINSEQ_1:def 3;
    then
A26: 1<=h.1 by FINSEQ_1:1;
    then
A27: j-'1=j-1 by XREAL_1:233;
    Seg len g <> {} by A2,A23,FINSEQ_1:def 3;
    then 0<len g;
    then
A28: 0+1<=len g by NAT_1:13;
    then
A29: len g -'1=len g-1 by XREAL_1:233;
    then
A30: h.(len h)<=len g-'1+1 by A20,FINSEQ_1:1;
A31: 1<=j by A25,FINSEQ_1:1;
    set g4 = accum g;
A32: len g=len g4 by Def10;
A33: g.1=g4.1 by Def10;
A34: for i being Nat st 1<=i & i<len g holds g4.(i+1)=(g4/.i)+(g/.(i+1) )
      by Def10;
A35: Sum g=g4.(len g) by A7,A14,A15,Def11;
    set g2 = accum F;
A36: len F=len g2 by Def10;
A37: F.1=g2.1 by Def10;
A38: for i being Nat st 1<=i & i<len F holds g2.(i+1)=(g2/.i)+(F/.(i+1) )
       by Def10;
A39: Sum F=g2.(len F) by A15,Def11;
    defpred Q[Nat] means 1<=$1+1 & $1+1<j implies g4.($1+1)= 0*n;
A40: for k being Nat st Q[k] holds Q[k+1]
    proof
      let k be Nat;
      assume
A41:  Q[k];
      1<=k+1+1 & k+1+1<j implies g4.(k+1+1)= 0*n
      proof
        assume that
A42:    1<=k+1+1 and
A43:    k+1+1<j;
        per cases by A42,XXREAL_0:1;
        suppose
A44:      1<k+1+1;
          j in Seg len g by A24,FINSEQ_1:def 3;
          then
A45:      j<=len g by FINSEQ_1:1;
          then k+1+1<len g by A43,XXREAL_0:2;
          then
A46:      g/.(k+1+1)=g.(k+1+1) by A44,FINSEQ_4:15;
A47:      now
            assume k+1+1 in rng h;
            then consider x0 being object such that
A48:        x0 in dom h and
A49:        h.x0=k+1+1 by FUNCT_1:def 3;
            reconsider nx0=x0 as Element of NAT by A48;
A50:        x0 in Seg len h by A48,FINSEQ_1:def 3;
            then
A51:        nx0<=len h by FINSEQ_1:1;
            1<=nx0 by A50,FINSEQ_1:1;
            hence contradiction by A1,A43,A49,A51,Th6;
          end;
A52:      k+1<k+1+1 by XREAL_1:29;
          then k+1<j by A43,XXREAL_0:2;
          then
A53:      k+1<len g by A45,XXREAL_0:2;
          then
A54:      g4/.(k+1)=g4.(k+1) by A32,FINSEQ_4:15,NAT_1:11;
          k+1+1<=len g by A53,NAT_1:13;
          then k+1+1 in Seg len g by A42,FINSEQ_1:1;
          then k+1+1 in dom g by FINSEQ_1:def 3;
          then
A55:      g.(k+1+1)= 0*n by A4,A47;
          g4.(k+1+1)=(g4/.(k+1))+(g/.(k+1+1)) by A34,A53,NAT_1:11;
          hence g4.(k+1+1)= 0*n by A41,A43,A52,A55,A54,A46,EUCLID_4:1,NAT_1:11
,XXREAL_0:2;
        end;
        suppose
          1=k+1+1;
          hence g4.(k+1+1)= 0*n;
        end;
      end;
      hence Q[k+1];
    end;
    defpred P[Nat] means 1<=$1+1 & $1+1<=len g2 implies g4.(h.($1+1))=g2.($1+1
    );
A56: 1 in Seg len g by A28,FINSEQ_1:1;
    1<j implies 1 in dom g & not 1 in rng h
    proof
      assume
A57:  1<j;
      thus 1 in dom g by A56,FINSEQ_1:def 3;
      hereby
        assume 1 in rng h;
        then consider x0 being object such that
A58:    x0 in dom h and
A59:    h.x0=1 by FUNCT_1:def 3;
        reconsider nx0=x0 as Element of NAT by A58;
A60:    x0 in Seg len h by A58,FINSEQ_1:def 3;
        then
A61:    nx0<=len h by FINSEQ_1:1;
        1<=nx0 by A60,FINSEQ_1:1;
        hence contradiction by A1,A57,A59,A61,Th6;
      end;
    end;
    then
A62: Q[0] by A4,A33;
A63: for k being Nat holds Q[k] from NAT_1:sch 2(A62,A40);
A64: now
      per cases by A26,XXREAL_0:1;
      case
        1<j;
        then 1+1<=j by NAT_1:13;
        then
A65:    1+1-1<=j-1 by XREAL_1:9;
        then 1<=j-'1 by A31,XREAL_1:233;
        then
A66:    j-'1-'1+1=j-'1-1+1 by XREAL_1:233
          .=j-'1;
A67:    g.j=g2.1 by A3,A5,A37,A17,FUNCT_1:13;
A68:    Q[j-'1-'1] by A63;
A69:    j in Seg len g by A24,FINSEQ_1:def 3;
        then
A70:    j<=len g by FINSEQ_1:1;
        1<=j by A69,FINSEQ_1:1;
        then
A71:    g/.j=g.j by A70,FINSEQ_4:15;
A72:    g2/.1=g2.1 by A36,A16,FINSEQ_4:15;
A73:    j-'1<j-'1+1 by XREAL_1:29;
        then
A74:    j-'1<len g by A27,A70,XXREAL_0:2;
        then g4/.(j-'1)=g4.(j-'1) by A32,A27,A65,FINSEQ_4:15;
        then g4.j= 0*n + g2/.1 by Def10,A27,A65,A73,A74,A68,A66,A71,A72,A67
          .=g2/.1 by EUCLID_4:1;
        hence P[0] by FINSEQ_4:15;
      end;
      case
        j=1;
        hence P[0] by A3,A5,A37,A33,A17,FUNCT_1:13;
      end;
    end;
A75: for k being Nat st P[k] holds P[k+1]
    proof
      let k be Nat;
      assume
A76:  P[k];
      1<=k+1+1 & k+1+1<=len g2 implies g4.(h.(k+1+1))=g2.(k+1+1)
      proof
        defpred R[Nat] means h.(k+1)<= $1+1 & $1+1<h.(k+1+1) implies g4.($1+1)
        = g2.(k+1);
        assume that
A77:    1<=k+1+1 and
A78:    k+1+1<=len g2;
A79:    k+1+1 in Seg len g2 by A77,A78,FINSEQ_1:1;
        then h.(k+1+1) in rng h by A36,A21,FUNCT_1:def 3;
        then h.(k+1+1) in dom g by A2;
        then
A80:    h.(k+1+1) in Seg len g by FINSEQ_1:def 3;
        then
A81:    1<=h.(k+1+1) by FINSEQ_1:1;
A82:    k+1<k+1+1 by XREAL_1:29;
        then
A83:    k+1<len g2 by A78,XXREAL_0:2;
        now
          per cases;
          case
A84:        1<=k;
            k<k+1 by XREAL_1:29;
            then
A85:        k<len h by A36,A22,A83,XXREAL_0:2;
            then k<=h.k by A1,A26,Th7;
            then
A86:        1<=h.k by A84,XXREAL_0:2;
            h.k<h.(k+1) by A1,A84,A85;
            hence R[0] by A86,XXREAL_0:2;
          end;
          case
            k<1;
            hence R[0] by A15,A36,A64,NAT_1:14;
          end;
        end;
        then
A87:    R[0];
A88:    h.(k+1+1)<=len g by A80,FINSEQ_1:1;
        1<=k+1 by NAT_1:11;
        then
A89:    h.(k+1)<h.(k+1+1) by A1,A36,A22,A83;
A90:    for k2 being Nat st R[k2] holds R[k2+1]
        proof
          let k2 be Nat;
          assume
A91:      R[k2];
          h.(k+1)<= k2+1+1 & k2+1+1<h.(k+1+1) implies g4.(k2+1+1)= g2.(k +1)
          proof
            assume that
A92:        h.(k+1)<= k2+1+1 and
A93:        k2+1+1<h.(k+1+1);
            per cases by A92,XXREAL_0:1;
            suppose
A94:          h.(k+1)< k2+1+1;
A95:          now
                assume k2+1+1 in rng h;
                then consider x0 being object such that
A96:            x0 in dom h and
A97:            h.x0=k2+1+1 by FUNCT_1:def 3;
                reconsider nx0=x0 as Element of NAT by A96;
A98:            x0 in Seg len h by A96,FINSEQ_1:def 3;
                then nx0<=len h by FINSEQ_1:1;
                then
A99:            nx0>=k+1+1 implies h.nx0>=h.(k+1+1) by A1,A77,Th6;
                1<=nx0 by A98,FINSEQ_1:1;
                then nx0<=k+1 implies h.nx0<=h.(k+1) by A1,A36,A22,A83,Th6;
                hence contradiction by A93,A94,A97,A99,NAT_1:13;
              end;
              h.(k+1+1) in rng h by A36,A21,A79,FUNCT_1:def 3;
              then h.(k+1+1) in dom g by A2;
              then h.(k+1+1) in Seg len g by FINSEQ_1:def 3;
              then h.(k+1+1)<=len g by FINSEQ_1:1;
              then
A100:         k2+1+1< len g by A93,XXREAL_0:2;
A101:         1<k2+1+1 by XREAL_1:29;
              then
A102:         g/.(k2+1+1)=g.(k2+1+1) by A100,FINSEQ_4:15;
A103:         k2+1<k2+1+1 by XREAL_1:29;
              then
A104:         k2+1<len g by A100,XXREAL_0:2;
              k2+1+1 in Seg len g by A100,A101,FINSEQ_1:1;
              then k2+1+1 in dom g by FINSEQ_1:def 3;
              then
A105:         g.(k2+1+1)= 0*n by A4,A95;
              k2+1< len g by A103,A100,XXREAL_0:2;
              then g4.(k2+1+1)=(g4/.(k2+1))+(g/.(k2+1+1)) by A34,NAT_1:11
                .=(g4/.(k2+1)) by A105,A102,EUCLID_4:1
                .= g4.(k2+1) by A32,A104,FINSEQ_4:15,NAT_1:11;
              hence g4.(k2+1+1)= g2.(k+1) by A91,A93,A94,NAT_1:13;
            end;
            suppose
              h.(k+1)=k2+1+1;
              hence g4.(k2+1+1)=g2.(k+1) by A76,A78,A82,NAT_1:11,XXREAL_0:2;
            end;
          end;
          hence R[k2+1];
        end;
A106:   for k2 being Nat holds R[k2] from NAT_1:sch 2(A87,A90);
        then
A107:   h.(k+1)<= h.(k+1+1)-'1-'1+1 & h.(k+1+1)-'1-'1+1<h.(k+1+1)
        implies g4.(h.(k+1+1)-'1-'1+1)= g2.(k+1);
        now
          per cases;
          case
A108:       1<=k;
            k<k+1 by XREAL_1:29;
            then
A109:       k<len h by A36,A22,A83,XXREAL_0:2;
            then k<=h.k by A1,A26,Th7;
            then
A110:       1<=h.k by A108,XXREAL_0:2;
A111:       1+h.(k+1)<=h.(k+1+1) by A89,NAT_1:13;
            then
A112:       h.(k+1)+1-1<=h.(k+1+1)-1 by XREAL_1:9;
            h.k<h.(k+1) by A1,A108,A109;
            then 1<h.(k+1) by A110,XXREAL_0:2;
            then 1+1<h.(k+1)+1 by XREAL_1:6;
            then 1+1<h.(k+1+1) by A111,XXREAL_0:2;
            then
A113:       1+1-1<h.(k+1+1)-1 by XREAL_1:9;
            then
A114:       h.(k+1+1)-'1=h.(k+1+1)-1 by XREAL_0:def 2;
            then
A115:       h.(k+1+1)-'1-'1=h.(k+1+1)-1-1 by A113,XREAL_1:233;
A116:       g/.(h.(k+1+1))=g.(h.(k+1+1)) by A81,A88,FINSEQ_4:15;
A117:       F.(k+1+1)=g.(h.(k+1+1)) by A3,A36,A21,A79,FUNCT_1:13;
A118:       k+1<=len g2 by A78,A82,XXREAL_0:2;
            h.(k+1+1)<h.(k+1+1)+1 by XREAL_1:29;
            then
A119:       h.(k+1+1)-1 <h.(k+1+1)+1-1 by XREAL_1:9;
            then
A120:       h.(k+1+1)-'1<len g by A88,A114,XXREAL_0:2;
            then
A121:       g4
/.(h.(k+1+1)-'1)=g4.((h.(k+1+1)-'1)) by A32,A113,A114,FINSEQ_4:15;
            g2.(k+1+1)=(g2/.(k+1))+(F/.(k+1+1)) by A36,A38,A83,NAT_1:11
              .= g4/.(h.(k+1+1)-'1) +(F/.(k+1+1)) by A107,A114,A115,A112,A119
,A121,A118,FINSEQ_4:15,NAT_1:11
              .= g4/.(h.(k+1+1)-'1) +g/.(h.(k+1+1)) by A36,A77,A78,A117,A116,
FINSEQ_4:15
              .=g4.(h.(k+1+1)-'1+1) by Def10,A113,A114,A120;
            hence g4.(h.(k+1+1))=g2.(k+1+1) by A114;
          end;
          case
            k<1;
            then
A122:       k=0 by NAT_1:14;
            then 1<len h by A36,A22,A78,NAT_1:13;
            then
A123:       h.1<h.(1+1) by A1;
            then
A124:       h.(1+1)-'1=h.(1+1)-1 by A31,XREAL_1:233,XXREAL_0:2;
            then
A125:       h.(k+1+1)-'1+1=h.(k+1+1) by A122;
A126:       F/.1=F.1 by A19,A22,FINSEQ_4:15;
            k+1<len F by A36,A78,NAT_1:13;
            then
A127:       g2.(k+1+1)=(g2/.(k+1))+(F/.(k+1+1)) by A38,NAT_1:11
              .=F/.1 +F/.(1+1) by A36,A37,A19,A22,A122,A126,FINSEQ_4:15;
A128:       F/.(1+1)=F.(1+1) by A36,A78,A122,FINSEQ_4:15;
            h.1+1<=h.(1+1) by A123,NAT_1:13;
            then h.1+1-1<=h.(1+1)-1 by XREAL_1:9;
            then
A129:       h.1<=h.(1+1)-'1 by A26,A123,XREAL_1:233,XXREAL_0:2;
            h.(1+1)<h.(1+1)+1 by XREAL_1:29;
            then
A130:       h.(1+1)-1<h.(1+1)+1-1 by XREAL_1:9;
            then
A131:       h.(1+1)-'1<h.(1+1) by A26,A123,XREAL_1:233,XXREAL_0:2;
A132:       1<h.(1+1) by A26,A123,XXREAL_0:2;
            then 1+1<=h.(1+1) by NAT_1:13;
            then
A133:       1+1-1<=h.(1+1)-1 by XREAL_1:9;
            then h.(1+1)-'1-'1=h.(1+1)-'1-1 by A124,XREAL_1:233;
            then
A134:       h.(1+1)-'1-'1+1=h.(1+1)-'1;
            1+1 in Seg len h by A36,A22,A78,A122,FINSEQ_1:1;
            then 1+1 in dom h by FINSEQ_1:def 3;
            then h.(1+1) in rng h by FUNCT_1:def 3;
            then h.(1+1) in dom g by A2;
            then
A135:       h.(1+1) in Seg len g by FINSEQ_1:def 3;
            then h.(1+1)<=len g by FINSEQ_1:1;
            then
A136:       g/.(h.(k+1+1)) =g.(h.(1+1)) by A122,A132,FINSEQ_4:15
              .=F.(1+1) by A3,A36,A21,A79,A122,FUNCT_1:13;
            h.(1+1)<=len g by A135,FINSEQ_1:1;
            then h.(1+1)-'1<len g4 by A32,A124,A130,XXREAL_0:2;
            then
A137:       g4/.(h.(k+1+1)-'1)=g4.((h.(1+1)-'1)) by A122,A124,A133,FINSEQ_4:15
              .=F.1 by A37,A106,A122,A134,A131,A129;
            h.(1+1)<=len g by A135,FINSEQ_1:1;
            then
A138:       h.(k+1+1)-'1<len g by A122,A125,NAT_1:13;
            1<=h.(k+1+1)-'1 by A122,A132,A125,NAT_1:13;
            then g4.(h.(k+1+1)-'1+1)=g4/.(h.(k+1+1)-'1) +g/.(h.(k+1+1))
              by Def10,A122,A124,A138
              .=F/.1 +F/.(1+1) by A19,A22,A128,A137,A136,FINSEQ_4:15;
            hence g4.(h.(k+1+1))=g2.(k+1+1) by A122,A124,A127;
          end;
        end;
        hence g4.(h.(k+1+1))=g2.(k+1+1);
      end;
      hence P[k+1];
    end;
    defpred R[Nat] means h.(len h)<= $1+1 & $1+1<=len g implies g4.($1+1)= g2.
    (len g2);
A139: P[0] by A64;
A140: for k being Nat holds P[k] from NAT_1:sch 2(A139,A75);
A141: for k being Nat st R[k] holds R[k+1]
    proof
      let k be Nat;
      assume
A142: R[k];
      h.(len h)<= k+1+1 & k+1+1<=len g implies g4.(k+1+1)= g2.(len g2)
      proof
        assume that
A143:   h.(len h)<= k+1+1 and
A144:   k+1+1<=len g;
        per cases by A143,XXREAL_0:1;
        suppose
A145:     h.(len h)<k+1+1;
A146:     now
            assume k+1+1 in rng h;
            then consider x being object such that
A147:       x in dom h and
A148:       h.x=k+1+1 by FUNCT_1:def 3;
            reconsider nx=x as Element of NAT by A147;
A149:       x in Seg len h by A147,FINSEQ_1:def 3;
            then
A150:       nx<=len h by FINSEQ_1:1;
            1<=nx by A149,FINSEQ_1:1;
            hence contradiction by A1,A145,A148,A150,Th6;
          end;
          1<=k+1+1 by NAT_1:11;
          then k+1+1 in Seg len g by A144,FINSEQ_1:1;
          then
A151:     k+1+1 in dom g by FINSEQ_1:def 3;
          k+1<k+1+1 by XREAL_1:29;
          then
A152:     k+1<len g by A144,XXREAL_0:2;
          then
A153:     g4.(k+1+1)=(g4/.(k+1))+(g/.(k+1+1)) by A34,NAT_1:11;
          1<=k+1 by NAT_1:11;
          then
A154:     g4/.(k+1)= g2.(len g2) by A32,A142,A145,A152,FINSEQ_4:15,NAT_1:13;
          1<k+1+1 by XREAL_1:29;
          then g/.(k+1+1)= g.(k+1+1) by A144,FINSEQ_4:15
            .=0*n by A4,A146,A151;
          hence g4.(k+1+1)= g2.(len g2) by A154,A153,EUCLID_4:1;
        end;
        suppose
A155:     h.(len h)=k+1+1;
A156:     len h-'1=len h -1 by A19,XREAL_1:233;
          1<=len h-'1+1 & len h-'1+1<=len g2 implies g4.(h.(len h-'1+1))
          =g2.(len h-'1+1) by A140;
          hence g4.(k+1+1)= g2.(len g2) by A36,A18,A21,A155,A156,FINSEQ_1:1
,def 3;
        end;
      end;
      hence R[k+1];
    end;
A157: 1<=h.(len h) by A20,FINSEQ_1:1;
    h.(len h)<=1 & 1<=len g implies g4.(0+1)= g2.(len g2)
    proof
      assume that
A158: h.(len h)<=1 and
      1<=len g;
      h.(len h)=1 by A157,A158,XXREAL_0:1;
      then len h<=1 by A1,A26,Th7;
      then
A159: len h=1 by A19,XXREAL_0:1;
      g2.1=g.(h.1) by A3,A5,A37,A17,FUNCT_1:13
        .=g.1 by A157,A158,A159,XXREAL_0:1;
      hence g4.(0+1)= g2.(len g2) by A6,A36,A33,A159,FINSEQ_1:def 3;
    end;
    then
A160: R[0];
    for k being Nat holds R[k] from NAT_1:sch 2(A160,A141);
    hence Sum g =Sum F by A36,A39,A35,A29,A30;
  end;
  suppose
A161: len F <=0;
    then
A162: Sum F= 0*n by Def11;
A163: dom g = Seg len g by FINSEQ_1:def 3;
    Seg len F = {} by A161;
    then dom F= {} by FINSEQ_1:def 3;
    then
A164: rng h= {} by A3,A5,RELAT_1:42;
A165: now
      let z be object;
      assume
A166: z in dom g;
      hence g.z = 0*n by A4,A164
        .= (len g |-> 0*n).z by A163,A166,FINSEQ_2:57;
    end;
    Seg len g = dom(len g |-> 0*n) by FUNCOP_1:13;
    then g = len g |-> 0*n by A163,A165,FUNCT_1:2;
    hence Sum g =Sum F by A162,Th21;
  end;
end;
