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 Th37:
  for s being State of SCMPDS,I being halt-free shiftable
Program of SCMPDS, a be Int_position, i be Integer,n be Nat,X be set
  st s.DataLoc(s.a,i) < 0 & not DataLoc(s.a,i) in X & n > 0 &
  a <> DataLoc(s.a,i) &
(for t be State of SCMPDS,Q st (for x be Int_position st x in X
holds t.x=s.x) & t.a=s.a holds IExec(I,Q,Initialize t).a=t.a &
IExec(I,Q,Initialize t).DataLoc(s.a,i)=t.DataLoc(s.a,i) &
 I is_closed_on t,Q & I is_halting_on t,Q &
   for y be Int_position
st y in X holds IExec(I,Q,Initialize t).y=t.y)
 holds for-up(a,i,n,I) is_closed_on s,P
 & for-up(a,i,n,I) is_halting_on s,P
proof
  let s be State of SCMPDS,I be halt-free shiftable Program of SCMPDS, a be
  Int_position, i be Integer,n be Nat,X be set;
  set b=DataLoc(s.a,i);
  set FOR=for-up(a,i,n,I), pFOR=stop FOR,
  pI=stop I;
  set i1=(a,i)>=0_goto (card I+3), i2=AddTo(a,i,n), i3=goto -(card I+2);
  assume
A1: s.b < 0;
  defpred P[Nat] means
   for t be State of SCMPDS,Q st -t.b <= $1 & (
for x be Int_position st x in X holds t.x=s.x) & t.a=s.a
 holds FOR is_closed_on t,Q & FOR is_halting_on t,Q;
  assume
A2: not b in X;
  assume
A3: n > 0;
  assume
A4: a <> b;
  assume
A5: for t be State of SCMPDS,Q st (for x be Int_position st x in X holds t
.x=s.x) & t.a=s.a holds IExec(I,Q,Initialize t).a=t.a &
IExec(I,Q,Initialize t).b=t.b
 & I is_closed_on t,Q
  & I is_halting_on t,Q & for y be Int_position st y in X
   holds IExec(I,Q,Initialize t).y=t.y;
A6: for k be Nat st P[k] holds P[k + 1]
  proof
    let k be Nat;
    assume
A7: P[k];
      let t be State of SCMPDS,Q;
      assume
A8:   -t.b <= k+1;
      assume
A9:  for x be Int_position st x in X holds t.x=s.x;
      assume
A10:  t.a=s.a;
      per cases;
      suppose
        t.b >= 0;
        hence FOR is_closed_on t,Q & FOR is_halting_on t,Q by A10,Th33;
      end;
      suppose
A11:    t.b < 0;
        set t2 = Initialize t, t3 = Initialize t,
        Q2 = Q +* stop I,
        Q3 = Q +* pFOR,
        t4 = Comput(Q3,t3,1), Q4 = Q3;
A12:    stop I c= Q2 by FUNCT_4:25;
A13:    FOR = i1 ';' (I ';' i2 ';' i3) by Th2;
A14:    Comput(Q3, t3,(0 qua Nat)+1) = Following(Q3,
Comput(Q3,t3,0)) by EXTPRO_1:3
          .= Exec(i1,t3) by A13,SCMPDS_6:11;
        for a holds t2.a = t4.a by A14,SCMPDS_2:57;
        then
A15:    DataPart t2 = DataPart t4 by SCMPDS_4:8;
A16:    IExec(I,Q,Initialize t).b=t.b by A5,A9,A10;
        -(-n) > 0 by A3;
        then -n < 0;
        then -n <= -1 by INT_1:8;
        then
A17:    -n-t.b <= -1-t.b by XREAL_1:9;
        -t.b-1 <= k by A8,XREAL_1:20;
        then
A18:    -n-t.b <= k by A17,XXREAL_0:2;
A19:    I is_closed_on t,Q by A5,A9,A10;
        then
A20:    I is_closed_on t2,Q2 by SCMPDS_6:24;
A21:    not b in dom Start-At(0,SCMPDS) by SCMPDS_4:18;
        set m2=LifeSpan(Q2,t2), t5= Comput(Q4, t4,m2), Q5 = Q4,
         l1= (card I +
1);
A22:    IC t3 = 0 by MEMSTR_0:def 11;
        set m3=m2 +1;
        set t6= Comput(Q3, t3,m3), Q6 = Q3;
        card I + 1 < card I + 3 by XREAL_1:6;
        then
A23:    l1 in dom FOR by Th31;
        set m5=m3+1+1, t8= Comput(Q3, t3,m5), Q8 = Q3;
        set t7= Comput(Q3, t3,m3+1), Q7 = Q3;
A24:    IExec(I,Q,Initialize t).a=t.a by A5,A9,A10;
        set l2=(card I+2);
A25:     0 in dom pFOR by COMPOS_1:36;
        card I + 2 < card I + 3 by XREAL_1:6;
        then
A26:    l2 in dom FOR by Th31;
A27:    pFOR c= Q3 by FUNCT_4:25;
        FOR c= pFOR by AFINSQ_1:74;
        then
A28:    FOR c= Q3 by A27,XBOOLE_1:1;
        Shift(I,1) c= FOR by Lm3;
        then
A29:    Shift(I,1) c= Q4 by A28,XBOOLE_1:1;
        I is_halting_on t,Q by A5,A9,A10;
        then
A30:    Q2 halts_on t2 by SCMPDS_6:def 3;
        Q2 +* stop I halts_on Initialize t2 by A30;
        then
A31:    I is_halting_on t2,Q2 by SCMPDS_6:def 3;
        not a in dom Start-At(0,SCMPDS) by SCMPDS_4:18;
        then t3.DataLoc(t3.a,i)= t3.b by A10,FUNCT_4:11
          .= t.b by A21,FUNCT_4:11;
        then
A32:    IC t4 = 0+1 by A22,A11,A14,SCMPDS_2:57;
        then
A33:    IC t5=l1 by A12,A31,A20,A15,A29,Th16;
A34: Q6/.IC t6 = Q6.IC t6 by PBOOLE:143;
A35:    t6=t5 by EXTPRO_1:4;
        then
A36:    CurInstr(Q6,t6)
           =Q3.l1 by A12,A31,A20,A32,A15,A29,Th16,A34
          .=FOR.l1 by A23,A28,GRFUNC_1:2
          .=i2 by Th32;
A37:    t7 = Following(Q3,t6) by EXTPRO_1:3
          .= Exec(i2,t6) by A36;
        then
A38:    IC t7= IC t6 + 1 by SCMPDS_2:48
          .= card I+(1+1) by A33,A35;
        then
A39:    CurInstr(Q7,t7)=Q3.l2 by PBOOLE:143
          .=FOR.l2 by A28,A26,GRFUNC_1:2
          .=i3 by Th32;
A40:    t8 = Following(Q3,t7) by EXTPRO_1:3
          .= Exec(i3,t7) by A39;
        then IC t8=ICplusConst(t7,(0 qua Nat)-(card I+2)) by SCMPDS_2:54
          .= 0 by A38,Th1;
        then
A41:    Initialize t8=t8 by MEMSTR_0:46;
A42:    DataPart Comput(Q2, t2,m2) = DataPart t5 by A12,A31,A20,A32,A15,A29
,Th16;
        then
A43:    t5.a= Comput(Q2, t2,m2).a by SCMPDS_4:8
          .=s.a by A10,A24,A30,EXTPRO_1:23;
        then DataLoc(t6.a,i)=b by EXTPRO_1:4;
        then t7.a=t6.a by A4,A37,SCMPDS_2:48
          .=s.a by A43,EXTPRO_1:4;
        then
A44:    t8.a=s.a by A40,SCMPDS_2:54;
A45:    now
          let x be Int_position;
          assume
A46:      x in X;
          t5.x= Comput(Q2, t2,m2).x by A42,SCMPDS_4:8
            .=IExec(I,Q,Initialize t).x by A30,EXTPRO_1:23
            .=t.x by A5,A9,A10,A46
            .=s.x by A9,A46;
          then t7.x=s.x by A2,A43,A35,A37,A46,SCMPDS_2:48;
          hence t8.x=s.x by A40,SCMPDS_2:54;
        end;
A47:    t5.b= Comput(Q2, t2,m2).b by A42,SCMPDS_4:8
          .=t.b by A16,A30,EXTPRO_1:23;
        t8.b=t7.b by A40,SCMPDS_2:54
          .=t.b+n by A43,A47,A35,A37,SCMPDS_2:48;
        then
A48:    -t8.b=-n-t.b;
        then
A49:    FOR is_closed_on t8,Q8 by A7,A44,A45,A18;
        now
          let k be Nat;
          per cases;
          suppose
            k < m5;
            then k <= m3+1 by INT_1:7;
            then
A50:        k <= m3 or k=m3+1 by NAT_1:8;
            hereby
              per cases by A50,NAT_1:8;
              suppose
A51:            k <= m2;
                hereby
                  per cases;
                  suppose
                    k=0;
                    hence IC Comput(Q3, t3,k) in dom pFOR by A25,A22;
                  end;
                  suppose
                    k<>0;
                    then consider kn be Nat such that
A52:                k=kn+1 by NAT_1:6;
                    reconsider kn as Nat;
                    reconsider lm = IC Comput(Q2, t2,kn) as
Nat;
                    kn < k by A52,XREAL_1:29;
                    then kn < m2 by A51,XXREAL_0:2;
                    then IC Comput(Q2, t2,kn) + 1 = IC Comput(
Q4,t4,kn)
                    by A12,A31,A20,A32,A15,A29,Th14;
                    then
A53:                IC Comput(Q3, t3,k)= lm +1 by A52,EXTPRO_1:4;
                    IC Comput(Q2, t2,kn) in dom pI by A19,SCMPDS_6:def 2;
                    then lm < card pI by AFINSQ_1:66;
                    then lm < card I+1 by COMPOS_1:55;
                    then
A54:                lm+1 <= card I +1 by INT_1:7;
                    card I + 1 < card I + 4 by XREAL_1:6;
                    then lm+1 < card I +4 by A54,XXREAL_0:2;
                    then lm+1 < card pFOR by Lm2;
                    hence IC Comput(Q3, t3,k) in dom pFOR by A53,AFINSQ_1:66;
                  end;
                end;
              end;
              suppose
A55:            k=m3;
                l1 in dom pFOR by A23,COMPOS_1:62;
                hence
                IC Comput(Q3, t3,k) in dom pFOR by A12,A31,A20,A32,A15,A29,A35
,A55,Th16;
              end;
              suppose
                k=m3+1;
                hence IC Comput(Q3, t3,k) in dom pFOR by A38,A26,COMPOS_1:62;
              end;
            end;
          end;
          suppose
            k >= m5;
            then consider nn be Nat such that
A56:        k=m5+nn by NAT_1:10;
            reconsider nn as Nat;
            Comput(Q3,t3,k)=
            Comput(Q8 +* pFOR, Initialize t8,nn) by A41,A56,EXTPRO_1:4;
            hence IC Comput(Q3, t3,k) in dom pFOR by A49,SCMPDS_6:def 2;
          end;
        end;
        hence FOR is_closed_on t,Q by SCMPDS_6:def 2;
A57:  Q8 +* pFOR = Q3;
        FOR is_halting_on t8,Q8 by A7,A44,A45,A18,A48;
        then Q3 halts_on t8 by A41,A57,SCMPDS_6:def 3;
        then Q3 halts_on t3 by EXTPRO_1:22;
        hence FOR is_halting_on t,Q by SCMPDS_6:def 3;
      end;
  end;
  reconsider nn=-s.b as Element of NAT by A1,INT_1:3;
A58: P[0]
  proof
    let t be State of SCMPDS, Q;
    assume
    -t.b <= 0;
    then -t.b <= -(0 qua Nat);
    then
A59: t.b >= 0 by XREAL_1:24;
    assume
    for x be Int_position st x in X holds t.x=s.x;
    assume
    t.a=s.a;
    hence thesis by A59,Th33;
  end;
  for k being Nat holds P[k] from NAT_1:sch 2(A58,A6);
  then
A60: P[nn];
  for x be Int_position st x in X holds s.x=s.x;
  hence ::thesis
   for-up(a,i,n,I) is_closed_on s,P
   & for-up(a,i,n,I) is_halting_on s,P by A60;
end;
