reserve m,n for Element of NAT,
  i,j for Instruction of SCMPDS,
  I for Program
  of SCMPDS,
  a for Int_position;
reserve Q,U,P for Instruction-Sequence of SCMPDS;

theorem Th17:
  for s being 0-started State of SCMPDS,I being halt-free shiftable
Program of SCMPDS, a,b,c be Int_position,i,d be Integer st
s.a=d &
s.b > 0 & s.c > 0 & s.DataLoc(d,i)=s.b-s.c &
 (for t be 0-started State of SCMPDS,Q st t.b >
0 & t.c > 0 & t.a=d & t.DataLoc(d,i)=t.b-t.c & t.b<>t.c
 holds IExec(I,Q,t).a=d &
I is_closed_on t,Q & I is_halting_on t,Q
 & (t.b > t.c implies IExec(I,Q,t).b=t.b-t.c
& IExec(I,Q,t).c = t.c) & (t.b <= t.c
 implies IExec(I,Q,t).c = t.c-t.b & IExec(I,Q,t)
  .b=t.b) & IExec(I,Q,t).DataLoc(d,i)=
  IExec(I,Q,t).b-IExec(I,Q,t).c)
   holds IExec(while<>0(a,i,I),P,s).b = s.b gcd s.c
   & IExec(while<>0(a,i,I),P,s).c = s.b gcd s.c
proof
  let s be 0-started State of SCMPDS,
      I be halt-free shiftable Program of SCMPDS, a,b,c
  be Int_position, i,d be Integer;
  set ci=DataLoc(s.a,i);
  consider f be Function of product the_Values_of SCMPDS,NAT such that
A1: for s be State of SCMPDS holds (s.b = s.c implies f.s =0) & (s.b <>
  s.c implies f.s=max(|.s.b.|,|.s.c.|)) by Th2;
  deffunc F(State of SCMPDS) = f.$1;
  set s1=IExec(while<>0(a,i,I),P,s), ss= s1;
  defpred P[set] means
   ex t be 0-started State of SCMPDS st t=$1 & t.b > 0 & t.c > 0
    & t.b gcd t.c =s.b gcd s.c & t.DataLoc(d,i)=t.b-t.c;
  assume that
A2: s.a=d and
A3: s.b > 0 and
A4: s.c > 0 and
A5: s.DataLoc(d,i)=s.b-s.c;
  assume
A6: for t be 0-started State of SCMPDS,Q
   st t.b > 0 & t.c > 0 & t.a=d & t.DataLoc(d,
i)=t.b-t.c & t.b<>t.c holds IExec(I,Q,t).a=d
 & I is_closed_on t,Q & I is_halting_on
t,Q & (t.b > t.c implies IExec(I,Q,t).b=t.b-t.c &
IExec(I,Q,t).c = t.c)
 & (t.b <= t.c implies IExec(I,Q,t).c = t.c-t.b & IExec(I,Q,t).b=t.b)
   & IExec(I,Q,t).DataLoc(d,i)=IExec(I,Q,t).b-IExec(I,Q,t).c;
A7: now
    let t be 0-started State of SCMPDS, Q;
    assume that
A8: P[t] and
A9: t.a=s.a and
A10: t.ci <> 0;
    set It=IExec(I,Q,t), t2=Initialize It, t1=t;
    set x=IExec(I,Q,t).b, y=IExec(I,Q,t).c;
    consider v be State of SCMPDS such that
A11: v=t and
A12: v.b > 0 and
A13: v.c > 0 and
A14: v.b gcd v.c =s.b gcd s.c and
A15: v.DataLoc(d,i)=v.b-v.c by A8;
A16: t.b > 0 by A11,A12;
A17: t.c > 0 by A11,A13;
A18: t.DataLoc(d,i)=v.b-v.c by A11,A15
      .=t.b-v.c by A11
      .=t.b-t.c by A11;
    then
A19: t.b > t.c implies IExec(I,Q,t).b=t.b-t.c &
IExec(I,Q,t).c = t.c
 by A2,A6,A9,A17;
A20: t.b<>t.c by A2,A10,A18;
    hence IExec(I,Q,t).a=t.a by A2,A6,A9,A16,A17,A18;
    thus I is_closed_on t,Q & I is_halting_on t,Q by A2,A6,A9,A16,A17,A18,A20;
A21: t.b <= t.c implies IExec(I,Q,t).c =t.c-t.b &
IExec(I,Q,t).b=t.b
 by A2,A6,A9,A16,A18,A20;
A22: now
      per cases;
      suppose
A23:    t.b > t.c;
        then t.b-t.c > 0 by XREAL_1:50;
        hence x > 0 by A2,A6,A9,A17,A18,A23;
        thus y > 0 by A2,A6,A9,A17,A18,A23;
        thus x gcd y = t.b gcd t.c by A16,A17,A19,A21,PREPOWER:97;
A24:    x=t.b-t.c by A2,A6,A9,A17,A18,A23;
        hereby
A25:      max(t.b,t.c)=t.b by A23,XXREAL_0:def 10;
          per cases by XXREAL_0:16;
          suppose
            max(x,y) = x;
            hence max(x,y) < max(t.b,t.c) by A17,A24,A25,XREAL_1:44;
          end;
          suppose
            max(x,y) = y;
            hence max(x,y) < max(t.b,t.c) by A2,A6,A9,A17,A18,A23,A25;
          end;
        end;
      end;
      suppose
A26:    t.b <= t.c;
        hence x > 0 by A2,A6,A9,A16,A18,A20;
        t.b < t.c by A20,A26,XXREAL_0:1;
        then t.c-t.b > 0 by XREAL_1:50;
        hence y > 0 by A2,A6,A9,A16,A18,A20,A26;
        thus x gcd y =t.b gcd t.c by A16,A17,A19,A21,PREPOWER:97;
A27:    y=t.c-t.b by A2,A6,A9,A16,A18,A20,A26;
A28:    x=t.b by A2,A6,A9,A16,A18,A20,A26;
        hereby
A29:      max(t.b,t.c)=t.c by A26,XXREAL_0:def 10;
          per cases by XXREAL_0:16;
          suppose
            max(x,y) = y;
            hence max(x,y) < max(t.b,t.c) by A16,A27,A29,XREAL_1:44;
          end;
          suppose
            max(x,y) = x;
            hence max(x,y) < max(t.b,t.c) by A20,A26,A28,A29,XXREAL_0:1;
          end;
        end;
      end;
    end;
    thus F(t2) < F(t1)
    proof
      t1.b <> t.c by A20;
      then t1.b <> t1.c;
      then
A30:  F(t1)=max(|.t1.b.|,|.t1.c.|) by A1
        .=max(|.t.b.|,|.t1.c.|)
        .=max(|.t.b.|,|.t.c.|)
        .=max( t.b,|.t.c.|) by A16,ABSVALUE:def 1
        .=max(t.b,t.c) by A17,ABSVALUE:def 1;
      then F(t1) >= t.b by XXREAL_0:25;
      then
A31:  F(t1) > 0 by A16;
      per cases;
      suppose
        t2.b=t2.c;
        hence thesis by A1,A31;
      end;
      suppose
        t2.b<>t2.c;
        then F(t2)=max(|.t2.b.|,|.t2.c.|) by A1
          .=max(|.x.|,|.t2.c.|) by SCMPDS_5:15
          .=max(|.x.|,|.y.|) by SCMPDS_5:15
          .=max( x,|.y.|) by A22,ABSVALUE:def 1
          .=max(x,y) by A22,ABSVALUE:def 1;
        hence thesis by A22,A30;
      end;
    end;
A32: IExec(I,Q,t).DataLoc(d,i)= IExec(I,Q,t).b-IExec(I,Q,t).c
         by A2,A6,A9,A16,A17,A18,A20;
    thus P[Initialize It]
    proof
      take u=Initialize It;
      thus u=Initialize It;
      thus u.b > 0 & u.c > 0 by A22,SCMPDS_5:15;
      thus u.b gcd u.c =It.b gcd u.c by SCMPDS_5:15
        .=t.b gcd t.c by A22,SCMPDS_5:15
        .=v.b gcd t.c by A11
        .=s.b gcd s.c by A11,A14;
      thus u.DataLoc(d,i)=x-y by A32,SCMPDS_5:15
        .=u.b-y by SCMPDS_5:15
        .=u.b-u.c by SCMPDS_5:15;
    end;
  end;
A33: for t be 0-started State of SCMPDS st P[t] holds
   F(t)=0 iff t.ci = 0
  proof
    let t be 0-started State of SCMPDS;
    assume P[t];
    then consider v be State of SCMPDS such that
A34: v=t and
A35: v.b > 0 and
    v.c > 0 and
    v.b gcd v.c =s.b gcd s.c and
A36: v.DataLoc(d,i)=v.b-v.c;
A37: t.ci=v.b-v.c by A2,A34,A36
      .=t.b-v.c by A34
      .=t.b-t.c by A34;
    hereby
      assume
A38:  F(t)=0;
      now
        assume t.b <> t.c;
        then (t).b <> t.c;
        then (t).b <> (t).c;
        then
A39:    F(t)=max(|.(t).b.|,|.(t).c.|) by A1
          .=max(|.t.b.|,|.(t).c.|)
          .=max(|.t.b.|,|.t.c.|);
        t.b > 0 by A34,A35;
        then |.t.b.| > 0 by COMPLEX1:47;
        hence contradiction by A38,A39,XXREAL_0:25;
      end;
      hence t.ci=0 by A37;
    end;
   thus t.ci=0 implies F(t)=0 by A1,A37;
  end;
A40: P[s] by A3,A4,A5;
A41: F(Initialize ss)=0 & P[Initialize ss] from WhileNEnd(A33,A40,A7);
  then consider w be 0-started State of SCMPDS such that
A42: w=Initialize ss and
A43: w.b > 0 and
  w.c > 0 and
A44: w.b gcd w.c =s.b gcd s.c and
A45: w.DataLoc(d,i)=w.b-w.c;
A46:  (Initialize ss).ci = ss.ci by SCMPDS_5:15;
A47:  (Initialize ss).b = ss.b by SCMPDS_5:15;
A48:  (Initialize ss).c = ss.c by SCMPDS_5:15;
A49: w.b-w.c =s1.ci by A2,A42,A45,SCMPDS_5:15
    .=0 by A33,A41,A46;
  then
A50: |.w.b.|=|.w.b.| gcd |.w.c.| by NAT_D:32
    .=s.b gcd s.c by A44,INT_2:34;
  thus IExec(while<>0(a,i,I),P,s).b=ss.b
    .=s.b gcd s.c by A42,A43,A50,A47,ABSVALUE:def 1;
  thus IExec(while<>0(a,i,I),P,s).c =ss.c
    .=s.b gcd s.c by A42,A43,A49,A50,A48,ABSVALUE:def 1;
end;
