reserve x for set,
  m,n for Nat,
  a,b for Int_position,
  i,j,k for Instruction of SCMPDS,
  s,s1,s2 for State of SCMPDS,
  k1,k2 for Integer,
  loc,l for Nat,
  I,J,K for Program of SCMPDS;
reserve P,P1,P2,Q for Instruction-Sequence of SCMPDS;

theorem Th52:
  for s being 0-started State of SCMPDS,sp,cv,result,pp,pD be Nat,
  f be FinSequence of NAT st s.intpos sp > sp & cv < result & s.intpos pp=pD &
  s.intpos sp+result < pp & pp <pD & pD < s.intpos pD & s.DataLoc(s.intpos sp,
result)=0 & len f = s.DataLoc(s.intpos sp,cv) & for k be Nat st k <
len f holds f.(k+1)=s.DataLoc(s.intpos pD,k)
 holds IExec(for-down(intpos sp,cv,
1, AddTo(intpos sp,result,intpos pD,0) ';'
AddTo(intpos pp,0,1)),P,s).DataLoc(s.
  intpos sp,result)=Sum f
proof
  let s be 0-started State of SCMPDS,
      sp,cv,fr,pp,pD be Nat, f be FinSequence
  of NAT;
  set BP=intpos sp, PD=intpos pD, PP=intpos pp;
  assume that
A1: s.BP > sp and
A2: cv < fr and
A3: s.PP=pD and
A4: s.BP+fr <pp and
A5: pp < pD and
A6: pD < s.PD and
A7: s.DataLoc(s.BP,fr)=0 and
A8: len f = s.DataLoc(s.BP,cv) and
A9: for k be Nat st k < len f holds f.(k+1)=s.DataLoc(s.PD,k);
  reconsider n=s.BP as Element of NAT by A1,INT_1:3;
A10: n+fr < pD by A4,A5,XXREAL_0:2;
  set i2= AddTo(BP,fr,PD,0), i3= AddTo(PP,0,1), I= i2 ';' i3, FD= for-down(BP,
  cv,1,I), a=DataLoc(s.BP,fr);
  defpred P[Nat] means
   for t be 0-started State of SCMPDS,f be FinSequence of
NAT st t.BP=s.BP & t.PP=pD & pD < t.PD & len f = t.DataLoc(t.BP,cv) & len f=$1
  & for k be Nat st k < len f holds f.(k+1)=t.DataLoc(t.PD,k) holds
  IExec(FD,Q,t).a=Sum f+t.a;
  n <= n+fr by NAT_1:11;
  then
A11: sp < n+fr by A1,XXREAL_0:2;
A12: n+fr > n+cv by A2,XREAL_1:6;
  then n+cv < pp by A4,XXREAL_0:2;
  then
A13: n+cv < pD by A5,XXREAL_0:2;
A14: now
    let k be Nat;
    assume
A15: P[k];
    now
      let t be 0-started State of SCMPDS,f be FinSequence of NAT;
      let Q be Instruction-Sequence of SCMPDS;
      assume that
A16:  t.BP=s.BP and
A17:  t.PP=pD and
A18:  pD < t.PD and
A19:  len f = t.DataLoc(t.BP,cv) and
A20:  len f=k+1 and
A21:  for i be Nat st i < len f holds f.(i+1)=t.DataLoc(t.PD,i );
A22:  f is FinSequence of REAL by FINSEQ_2:24, NUMBERS:19;
A23:  now
        set Dv=DataLoc(t.BP,cv);
        let u be 0-started State of SCMPDS;
        let U be Instruction-Sequence of SCMPDS;
A24:    Initialize u = u by MEMSTR_0:44;
        assume that
A25:    for x be Int_position st x in {BP,PP} holds u.x=t.x and
A26:    u.BP=t.BP;
        set t0=Initialize u, t1=Exec(i2, t0);
A27:    DataLoc(t0.BP,fr)=DataLoc(n,fr) by A16,A26,SCMPDS_5:15
          .=intpos(n+fr) by SCMP_GCD:1;
        then
A28:    |.t0.BP+fr.| = n+fr by XTUPLE_0:1;
        then
A29:    t1.PP=t0.PP by A3,A5,A7,A27,SCMPDS_2:49
          .=u.PP by SCMPDS_5:15;
        PP in {BP,PP} by TARSKI:def 2;
        then
A30:    t1.PP=pD by A17,A25,A29;
        then t1.PP+(0 qua Nat) <> sp by A4,A5,A11,XXREAL_0:2;
        then |.t1.PP+(0 qua Nat).| <> sp by A30,ABSVALUE:def 1;
        then
A31:    DataLoc(t1.PP,0) <> BP by XTUPLE_0:1;
A32:    t1.BP=t0.BP by A1,A7,A27,A28,SCMPDS_2:49
          .=u.BP by SCMPDS_5:15;
        thus
A33:    IExec(I,U,u).BP=Exec(i3, t1).BP by A24,SCMPDS_5:42
          .=u.BP by A32,A31,SCMPDS_2:48;
        Dv=intpos(n+cv) by A16,SCMP_GCD:1;
        then
A34:    |.t.BP+cv.| = n+cv by XTUPLE_0:1;
        then |.t0.BP+fr.| <> |.t.BP+cv.| by A2,A28;
        then
A35:    DataLoc(t0.BP,fr) <> Dv by XTUPLE_0:1;
A36:    DataLoc(t1.PP,0)=intpos (pD+(0 qua Nat)) by A30,SCMP_GCD:1;
        then |.t1.PP+(0 qua Nat).|=pD+(0 qua Nat) by XTUPLE_0:1;
        then |.t1.PP+(0 qua Nat).| <> |.t.BP+cv.|
         by A4,A5,A12,A34,XXREAL_0:2;
        then
A37:    DataLoc(t1.PP,0) <> Dv by XTUPLE_0:1;
        thus IExec(I,U,u).Dv=Exec(i3, t1).Dv by A24,SCMPDS_5:42
          .=t1.Dv by A37,SCMPDS_2:48
          .=t0.Dv by A35,SCMPDS_2:49
          .=u.Dv by SCMPDS_5:15;
        thus I is_closed_on u, U & I is_halting_on u, U by SCMPDS_6:20,21;
A38:    IExec(I,U,Initialize u).PP=Exec(i3, t1).PP by SCMPDS_5:42
          .=u.PP by A3,A6,A29,A36,SCMPDS_2:48;
          let y be Int_position;
          assume
A39:      y in {BP,PP};
          per cases by A39,TARSKI:def 2;
          suppose
            y=BP;
            hence IExec(I,U,u).y=u.y by A33;
          end;
          suppose
            y=PP;
            hence IExec(I,U,u).y=u.y by A38,MEMSTR_0:44;
          end;
      end;
A40:  a=intpos(n+fr) by SCMP_GCD:1;
      set t0=t, t1=Exec(i2, t0);
      set j=AddTo(BP,cv,-1), s2=IExec(I ';'j,Q,t), P2 = Q,
      g=Del(f,1);
      set It=IExec(I,Q,t);
  DataLoc(t0.BP,fr) =intpos(n+fr) by A16,SCMP_GCD:1;
      then
A41:  |.t0.BP+fr.| = n+fr by XTUPLE_0:1;
A42:  t1.BP =t.BP by A1,A7,A16,SCMPDS_2:49;
      t1.PP =t.PP by A3,A5,A7,A16,SCMPDS_2:49;
      then
A43:  DataLoc(t1.PP,0)=intpos (pD+(0 qua Nat)) by A17,SCMP_GCD:1;
      then
A44:  |.t1.PP+(0 qua Nat).|=pD+(0 qua Nat) by XTUPLE_0:1;
      then |.t1.PP+(0 qua Nat).| <> sp by A4,A5,A11,XXREAL_0:2;
      then
A45:  DataLoc(t1.PP,0) <> BP by XTUPLE_0:1;
A46:  It.BP=Exec(i3, t1).BP by SCMPDS_5:42
        .=t.BP by A42,A45,SCMPDS_2:48;
      then
A47:  DataLoc(It.BP,cv)=intpos(n+cv) by A16,SCMP_GCD:1;
      then
A48:  |.It.BP+cv.|=n+cv by XTUPLE_0:1;
      then pD <> |.It.BP+cv.| by A4,A5,A12,XXREAL_0:2;
      then
A49:  PD <> DataLoc(It.BP,cv) by XTUPLE_0:1;
A50:  f.((0 qua Nat)+1) =t0.DataLoc(t0.PD,0) by A20,A21;
      n+fr <> |.It.BP+cv.| by A2,A48;
      then
A51:  a <> DataLoc(It.BP,cv) by A40,XTUPLE_0:1;
A52:  It.a=Exec(i3, t1).a by SCMPDS_5:42
        .=t1.a by A6,A7,A43,SCMPDS_2:48
        .=t.a + f.1 by A16,A50,SCMPDS_2:49;
A53:  s2.a=Exec(j, It).a by SCMPDS_5:41
        .=f.1+t.a by A51,A52,SCMPDS_2:48;
      n+cv <> sp by A1,NAT_1:11;
      then |.n+cv.| <> sp by ABSVALUE:def 1;
      then
A54:  DataLoc(t.BP,cv) <> BP by A16,XTUPLE_0:1;
A55:  s2.PD=Exec(j, It).PD by SCMPDS_5:41
        .=It.PD by A49,SCMPDS_2:48
        .=Exec(i3, t1).PD by SCMPDS_5:42
        .=t1.PD+1 by A43,SCMPDS_2:48
        .=t.PD+1 by A6,A7,A16,SCMPDS_2:49;
      then t.PD < s2.PD by XREAL_1:29;
      then
A56:  pD < s2.PD by A18,XXREAL_0:2;
      1 <= k+1 by NAT_1:11;
      then 1 in Seg (k+1) by FINSEQ_1:1;
      then
A57:  1 in dom f by A20,FINSEQ_1:def 3;
      then
A58:  len g +1=len f by WSIERP_1:def 1;
A59:   s2.PD = (Initialize s2).PD by SCMPDS_5:15;
A60:   s2.DataLoc(s2.BP,cv) = (Initialize s2).DataLoc(s2.BP,cv) by SCMPDS_5:15;
A61:   s2.PP = (Initialize s2).PP by SCMPDS_5:15;
A62:   s2.BP = (Initialize s2).BP by SCMPDS_5:15;
A63:
    for k be Nat st k < len g
     holds g.(k+1)=(Initialize s2).DataLoc((Initialize s2).PD,k)
 proof
        reconsider m=t.PD as Element of NAT by A18,INT_1:3;
        let i be Nat;
        set SD=DataLoc((Initialize s2).PD,i);
        assume
        i < len g;
        then
A64:    i+1 < len g+1 by XREAL_1:6;
A65:   s2.SD = (Initialize s2).SD by SCMPDS_5:15;
        SD =intpos(m+(1+i)) by A55,A59,SCMP_GCD:1;
        then
A66:    |.s2.PD+i.| = m+(1+i) by A59,XTUPLE_0:1;
        then |.t1.PP+(0 qua Nat).| <> |.s2.PD+i.| by A18,A44,NAT_1:11;
        then
A67:    DataLoc(t1.PP,0) <> SD by A59,XTUPLE_0:1;
        m <= m+(1+i) by NAT_1:11;
        then |.t0.BP+fr.|<> |.s2.PD+i.|by A10,A18,A41,A66,XXREAL_0:2;
        then
A68:    DataLoc(t0.BP,fr)<> SD by A59,XTUPLE_0:1;
        n+cv < m by A13,A18,XXREAL_0:2;
        then |.s2.PD+i.| <> |.It.BP+cv.| by A48,A66,NAT_1:11;
        then
A69:    SD <> DataLoc(It.BP,cv) by A59,XTUPLE_0:1;
A70:    s2.SD=Exec(j, It).SD by SCMPDS_5:41
          .=It.SD by A69,SCMPDS_2:48
          .=Exec(i3, t1).SD by SCMPDS_5:42
          .=t1.SD by A67,SCMPDS_2:48
          .=t.SD by A68,SCMPDS_2:49;
        (0 qua Nat)+1 <= i+1 by XREAL_1:6;
        hence g.(i+1)=f.(i+1+1) by A57,WSIERP_1:def 1
          .=t.DataLoc(t.PD,i+1) by A21,A58,A64
          .=(Initialize s2).SD by A66,A70,A65,SCMPDS_5:15;
      end;
      |.t0.BP+fr.| <> n+cv by A2,A41;
      then
A71:  DataLoc(t0.BP,fr)<> intpos(n+cv) by XTUPLE_0:1;
      |.t1.PP+(0 qua Nat).| <> n+cv by A4,A5,A12,A44,XXREAL_0:2;
      then
A72:  DataLoc(t1.PP,0) <> intpos(n+cv) by XTUPLE_0:1;
A73:  It.intpos(n+cv)=Exec(i3, t1).intpos(n+cv) by SCMPDS_5:42
        .=t1.intpos(n+cv) by A72,SCMPDS_2:48
        .=t.intpos(n+cv) by A71,SCMPDS_2:49
        .=k+1 by A16,A19,A20,SCMP_GCD:1;
      |.It.BP+cv.| <> sp by A1,A48,NAT_1:11;
      then
A74:  DataLoc(It.BP,cv) <> BP by XTUPLE_0:1;
A75:  s2.BP=Exec(j, It).BP by SCMPDS_5:41
        .=s.BP by A16,A46,A74,SCMPDS_2:48;
      then DataLoc(s2.BP,cv)=intpos(n+cv) by SCMP_GCD:1;
      then
A76:  s2.DataLoc(s2.BP,cv)=Exec(j, It).intpos(n+cv) by SCMPDS_5:41
        .=It.intpos(n+cv)+ -1 by A47,SCMPDS_2:48
        .=len g by A20,A58,A73;
      pp <> |.It.BP+cv.| by A2,A4,A48,XREAL_1:6;
      then
A77:  PP <> DataLoc(It.BP,cv) by XTUPLE_0:1;
A78:  s2.PP=Exec(j, It).PP by SCMPDS_5:41
        .=It.PP by A77,SCMPDS_2:48
        .=Exec(i3, t1).PP by SCMPDS_5:42
        .=t1.PP by A3,A6,A43,SCMPDS_2:48
        .=pD by A17,A3,A5,A7,A16,SCMPDS_2:49;
      1 <= len f by A20,NAT_1:11;
      then
A79:  1 in dom f by FINSEQ_3:25;
      n+cv <> pp by A2,A4,XREAL_1:6;
      then |.n+cv.| <> pp by ABSVALUE:def 1;
      then DataLoc(s.BP,cv) <> PP by XTUPLE_0:1;
      then not DataLoc(t.BP,cv) in {BP,PP} by A16,A54,TARSKI:def 2;
      hence IExec(FD,Q,t).a =IExec(FD,P2,Initialize s2).a
       by A19,A20,A54,A23,Th47
        .=Sum g+(Initialize s2).a
         by A15,A20,A75,A58,A76,A78,A56,A63,A59,A60,A61,A62
        .=Sum g+s2.a by SCMPDS_5:15
        .=Sum g+f.1+t.a by A53
        .=Sum f+t.a by A79,A22,WSIERP_1:20;
    end;
    hence P[k+1];
  end;
  now
    let t be 0-started State of SCMPDS,f be FinSequence of NAT;
    assume that
    t.BP=s.BP and
    t.PP=pD and
    pD < t.PD and
A80: len f = t.DataLoc(t.BP,cv) and
A81: len f=0 and
    for k be Nat st k < len f holds f.(k+1)=t.DataLoc(t.PD,k);
A82:  Initialize t = t by MEMSTR_0:44;
    f = <*>NAT by A81;
    hence IExec(FD,Q,t).a =Sum f + t.a by A80,Th45,A82,RVSUM_1:72;
  end;
  then
A83: P[0];
  for k be Nat holds P[k] from NAT_1:sch 2(A83,A14);
  hence IExec(FD,P,s).a=Sum f+(0 qua Nat) by A3,A6,A7,A8,A9
    .=Sum f;
end;
