reserve i,n,m for Nat;

theorem Th15:
for xseq,yseq be FinSequence of REAL m st
 len xseq = len yseq + 1 & xseq| (len yseq) = yseq holds
   ex v be Element of REAL m st v=xseq.(len xseq) & Sum xseq = Sum yseq + v
proof
   let xseq,yseq be FinSequence of REAL m;
   assume A1: len xseq  = len yseq + 1 & xseq| (len yseq) = yseq;
A2:len xseq= len (accum xseq) & xseq.1=(accum xseq).1 &
   (for k being Nat st 1<=k & k<len xseq holds
     (accum xseq).(k+1)=(accum xseq)/.k+xseq/.(k+1)) by EUCLID_7:def 10;
A3:Sum xseq=(accum xseq).(len xseq) by A1,EUCLID_7:def 11;
   set g = accum xseq;
   set i= len yseq;
   per cases;
   suppose A4: i= 0; then
    reconsider v = xseq.(len xseq) as Element of REAL m
      by A1,A3,EUCLID_7:def 10;
    take v;
    Sum yseq = 0*m by A4,EUCLID_7:def 11;
    hence v = xseq.(len xseq) & Sum xseq = Sum yseq + v
      by A1,A2,A3,A4,RVSUM_1:16;
   end;
   suppose A5: i <> 0;
A6: len yseq=len (accum yseq) & yseq.1=(accum yseq).1
    & (for k being Nat st 1<=k & k<len yseq holds
        (accum yseq).(k+1)=(accum yseq)/.k + yseq/.(k+1)) by EUCLID_7:def 10;
A7: Sum yseq = (accum yseq).i by A5,EUCLID_7:def 11;
    set g0 = accum yseq;
A8: i <= len g by A1,A2,NAT_1:11; then
A9: len(g|i) = i by FINSEQ_1:59;
A10: for k be Nat st 1 <= k & k <= len(g|i) holds (g|i).k = g0.k
    proof
     defpred PP[Nat] means
      1 <= $1 & $1 <= len(g|i) implies (g|i).$1 = g0.$1;
A11:  PP[0];
A12: now let k be Nat;
      assume A13: PP[k];
      now assume A14: 1 <= k+1 & k+1 <= len(g|i); then
A15:   k+1 <= len yseq by A8,FINSEQ_1:59; then
       k+1 in Seg i by A14; then
A16:   (g|i).(k+1) = g.(k+1) & xseq.(k+1) = (xseq|Seg i).(k+1) by FUNCT_1:49;
A17:   k < len yseq by A15,NAT_1:13;
       now assume A18: k<>0; then
A19:    1 <= k by NAT_1:14; then
A20:    k in Seg i by A17;
        len yseq <= len xseq by A1,NAT_1:12; then
A21:    k < len xseq by A17,XXREAL_0:2; then
        g/.k = g.k by A2,A19,FINSEQ_4:15; then
        g/.k = (g|i).k by A20,FUNCT_1:49; then
A22:    g/.k = g0/.k by A6,A17,A19,A13,A14,FINSEQ_4:15,NAT_1:13;
        k+1 <= len xseq by A15,A1,NAT_1:12; then
        xseq/.(k+1) = xseq.(k+1) by A14,FINSEQ_4:15; then
        xseq/.(k+1) = yseq/.(k+1) by A1,A14,A9,A16,FINSEQ_4:15; then
        (g/.k)+(xseq/.(k+1)) = g0.(k+1) by A6,A17,A18,A22,NAT_1:14;
        hence (g|i).(k+1) = g0.(k+1) by A2,A16,A18,A21,NAT_1:14;
       end;
       hence (g|i).(k+1) = g0.(k+1) by A1,A6,A16,EUCLID_7:def 10;
      end;
      hence PP[k+1];
     end;
     thus for k be Nat holds PP[k] from NAT_1:sch 2(A11,A12);
    end;
    1 <= i by A5,NAT_1:14; then
    i in Seg i; then
A23:i in dom(g|i) by A9,FINSEQ_1:def 3; then
    i in dom g by RELAT_1:57; then
    g.i = g/.i by PARTFUN1:def 6; then
    (g|i).i = g/.i by A23,FUNCT_1:47; then
A24:g0.i = g/.i by A6,A9,A10,FINSEQ_1:14;
    dom xseq = Seg (i+1) by A1,FINSEQ_1:def 3; then
    xseq.(len xseq) in rng xseq by A1,FINSEQ_1:4,FUNCT_1:3; then
    reconsider v=xseq.(len xseq) as Element of REAL m;
    take v;
    thus v=xseq.(len xseq);
A25:i < len xseq by A1,NAT_1:13;
    1<=i+1 by NAT_1:11; then
    g/.i + xseq/.(i+1) = Sum yseq + v by A7,A24,A1,FINSEQ_4:15;
    hence Sum xseq = Sum yseq + v by A1,A25,A2,A3,A5,NAT_1:14;
   end;
end;
