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 Th49:
  for s being 0-started State of SCMPDS,n be Nat
   st s.GBP=0 & s.intpos 2=n & s.intpos 3=0
   holds IExec(for-down(GBP,2,1, Load AddTo(GBP,3,1)),P,s).intpos 3
       =n
proof
  set i= AddTo(GBP,3,1), I= Load i, FD= for-down(GBP,2,1, I), a=intpos 3;
  let s be 0-started State of SCMPDS,n be Nat;
  assume that
A1: s.GBP=0 and
A2: s.intpos 2=n and
A3: s.a=0;
  defpred P[Nat] means
   for s be 0-started State of SCMPDS st s.intpos 2=$1 & s.GBP=0
    holds IExec(FD,P,s).a=$1+s.a;
A4: now
    let k be Nat;
    assume
A5: P[k];
    now
      let s be 0-started State of SCMPDS;
      assume that
A6:   s.intpos 2=k+1 and
A7:   s.GBP=0;
      GBP <> DataLoc(s.GBP,2) by A6,A7,SCMP_GCD:1;
      then
A8:   not DataLoc(s.GBP,2) in {GBP} by TARSKI:def 1;
A9:  now
        set cv=DataLoc(s.GBP,2);
        let t be 0-started State of SCMPDS;
        let Q;
A10:   Initialize t = t by MEMSTR_0:44;
        assume that
        for x be Int_position st x in {GBP} holds t.x=s.x and
A11:    t.GBP=s.GBP;
        set t0=Initialize t;
        t0.GBP=0 by A7,A11,SCMPDS_5:15;
        then
A12:    DataLoc(t0.GBP,3)=intpos(0 qua Nat+3) by SCMP_GCD:1;
        then
A13:    cv <> DataLoc(t0.GBP,3) by A6,A7,AMI_3:10,SCMP_GCD:1;
        thus
A14:    IExec(I,Q,t).GBP=Exec(AddTo(GBP,3,1),t0).GBP by A10,SCMPDS_5:40
          .=t0.GBP by A12,AMI_3:10,SCMPDS_2:48
          .=t.GBP by SCMPDS_5:15;
        thus IExec(I,Q,t).cv=Exec(AddTo(GBP,3,1),t0).cv by A10,SCMPDS_5:40
          .=t0.cv by A13,SCMPDS_2:48
          .=t.cv by SCMPDS_5:15;
        thus I is_closed_on t,Q & I is_halting_on t,Q by SCMPDS_6:20,21;
          let y be Int_position;
          assume
          y in {GBP};
          then y=GBP by TARSKI:def 1;
          hence IExec(I,Q,t).y=t.y by A14;
      end;
      set j=AddTo(GBP,2,-1), s0=s, s1=IExec(I,P,s),
          s2=IExec(I ';' j,P,s), l2=intpos 2, P2 = P;
A15:  DataLoc(s0.GBP,3)=intpos(0 qua Nat+3) by A7,SCMP_GCD:1;
A16:  s1.GBP=Exec(i, s0).GBP by SCMPDS_5:40
        .=0 by A7,A15,AMI_3:10,SCMPDS_2:48;
      then
A17:  DataLoc(s1.GBP,2)=intpos(0 qua Nat+2) by SCMP_GCD:1;
A18:  s2.l2=Exec(j, s1).l2 by SCMPDS_5:41
        .=s1.l2+ -1 by A17,SCMPDS_2:48
        .=Exec(i, s0).l2+-1 by SCMPDS_5:40
        .=s0.l2+ -1 by A15,AMI_3:10,SCMPDS_2:48
        .=k by A6;
A19:  s2.a=Exec(j, s1).a by SCMPDS_5:41
        .=s1.a by A17,AMI_3:10,SCMPDS_2:48
        .=Exec(i, s0).a by SCMPDS_5:40
        .=s.a+1 by A15,SCMPDS_2:48;
A20:  s2.GBP=Exec(j, s1).GBP by SCMPDS_5:41
        .=0 by A16,A17,AMI_3:10,SCMPDS_2:48;
A21: (Initialize s2).intpos 2=s2.intpos 2 by SCMPDS_5:15;
A22: (Initialize s2).a=s2.a by SCMPDS_5:15;
A23: (Initialize s2).GBP=s2.GBP by SCMPDS_5:15;
      DataLoc(s.GBP,2)=intpos(0 qua Nat+2) by A7,SCMP_GCD:1;
      hence IExec(FD,P,s).a =IExec(FD,P2,Initialize s2).a
       by A6,A7,A8,A9,Th47
        .=k+s2.a by A5,A18,A20,A21,A22,A23
        .=k+1+s.a by A19;
    end;
    hence P[k+1];
  end;
A24: P[0]
  proof
    let s be 0-started State of SCMPDS;
    assume that
A25: s.intpos 2=0 and
A26: s.GBP=0;
A27: Initialize s = s by MEMSTR_0:44;
    DataLoc(s.GBP,2)=intpos((0 qua Nat)+2) by A26,SCMP_GCD:1;
    hence thesis by A25,Th45,A27;
  end;
  for k be Nat holds P[k] from NAT_1:sch 2(A24,A4);
  hence IExec(FD,P,s).a=n+(0 qua Nat) by A1,A2,A3
    .=n;
end;
