reserve P,Q for Instruction-Sequence of SCM+FSA;
reserve m, n for Nat;
reserve f for FinSeq-Location,
  c for Int-Location;
reserve s for State of SCM+FSA,
  I for MacroInstruction of SCM+FSA,
  a for read-write Int-Location;
reserve i,k,m,n for Nat;
reserve s for State of SCM+FSA,
  I for MacroInstruction of SCM+FSA,
  a for read-write Int-Location;

theorem Th19:
  for I being really-closed MacroInstruction of SCM+FSA,
      a being read-write Int-Location,
s being State of SCM+FSA st (for k being Nat
 holds
  I is_halting_on StepWhile>0(a,I,P,s).k,P+*while>0(a,I)) &
  ex f being Function of
  product the_Values_of SCM+FSA,NAT st (for k being Nat holds (f.(
StepWhile>0(a,I,P,s).(k+1)) < f.(StepWhile>0(a,I,P,s).k) or
 f.(StepWhile>0(a,I,P,s).k
  ) = 0) & ( f.(StepWhile>0(a,I,P,s).k)=0 iff
   (StepWhile>0(a,I,P,s).k).a <= 0 ))
  holds while>0(a,I) is_halting_on s,P
proof
  let I be really-closed MacroInstruction of SCM+FSA,
      a be read-write Int-Location, s be State of
  SCM+FSA;
  set D = Int-Locations \/ FinSeq-Locations;
  assume
A1: for k being Nat holds
     I is_halting_on StepWhile>0(a,I,P,s).k,P+*while>0(a,I);
  set s1 = Initialize s,
      P1 = P +* while>0(a,I);
A2:  P1 +* while>0(a,I) = P1;
  given f being Function of product the_Values_of SCM+FSA,NAT such that
A3: for k being Nat
 holds (f.(StepWhile>0(a,I,P,s).(k+1)) < f.(StepWhile>0
(a,I,P,s).k) or f.(StepWhile>0(a,I,P,s).k) = 0) &
 ( f.(StepWhile>0(a,I,P,s).k)=0
 iff
  (StepWhile>0(a,I,P,s).k).a <= 0 );
  deffunc F(Nat) = f.(StepWhile>0(a,I,P,s).$1);
A4: for k being Nat holds F(k+1) < F(k) or F(k) = 0 by A3;
  consider m being Nat such that
A5: F(m)=0 and
A6: for n being Nat st F(n) =0 holds m <= n from NAT_1:sch 17(A4);
  defpred P[Nat] means $1+1 <= m implies ex k st StepWhile>0(a,I,P,s)
  .($1+1)= Comput(P1, s1,k);
A7: now
    let k be Nat;
    assume
A8: P[k];
    now
      set sk1=StepWhile>0(a,I,P,s).(k+1);
      set sk=StepWhile>0(a,I,P,s).k;
      assume
A9:   (k+1)+ 1 <= m;
      k + 0 < k+ (1+ 1) by XREAL_1:6;
      then k < m by A9,XXREAL_0:2;
      then F(k) <> 0 by A6;
      then
A10:  sk.a > 0 by A3;
A11:  I is_halting_on sk,P+*while>0(a,I) by A1;
      (k+1)+ 0 < (k+ 1)+ 1 by XREAL_1:6;
      then consider n being Nat such that
A12:  sk1 = Comput(P1, s1,n) by A8,A9,XXREAL_0:2;
      take m=n +(LifeSpan(P +* while>0(a,I) +* I,
      Initialize sk1) + 2);
      sk1= Comput(P +* while>0(a,I), (Initialize sk),
      (LifeSpan(P +* while>0(a,I) +* I,Initialize sk) + 2)) by Def2;
      then IC sk1 = 0 by A11,A10,Th15,A2;
      hence StepWhile>0(a,I,P,s).((k+1)+1)= Comput(P1, s1,m) by A12,Th18;
    end;
    hence P[k+1];
  end;
A13: P[ 0]
  proof
    assume 0+1 <= m;
    take n=(LifeSpan(P+*while>0(a,I)+* I,Initialize s) + 2);
    thus thesis by Th17;
  end;
A14: for k being Nat holds P[k] from NAT_1:sch 2(A13,A7);
  now
    per cases;
    suppose
      m=0;
      then (StepWhile>0(a,I,P,s).0).a <= 0 by A3,A5;
      then s.a <= 0 by Def2;
      hence thesis by Th12;
    end;
    suppose
A15:  m<>0;
      set p=(LifeSpan(P+*while>0(a,I)+* I,Initialize s) + 3);
      set sm=StepWhile>0(a,I,P,s).m;
      set sm1=Initialize sm;
      consider i being Nat such that
A16:  m=i+1 by A15,NAT_1:6;
      reconsider i as Element of NAT by ORDINAL1:def 12;
      consider n being Nat such that
A17:  sm = Comput(P1, s1,n) by A14,A16;
      set si=StepWhile>0(a,I,P,s).i;
      i < m by A16,NAT_1:13;
      then F(i) <> 0 by A6;
      then
A18:  si.a > 0 by A3;
A19:  I is_halting_on si,P+*while>0(a,I) by A1;
      sm= Comput(P +* while>0(a,I), (Initialize si),
      (LifeSpan(P +* while>0(a,I) +* I,Initialize si) + 2))
       by A16,Def2;
      then sm is 0-started by A19,A18,Th15,A2;
   then Start-At(0,SCM+FSA) c= sm by MEMSTR_0:29;
   then
A20:  sm1=sm by FUNCT_4:98;
      sm.a <= 0 by A3,A5;
      then while>0(a,I) is_halting_on sm,P1 by Th12;
      then P1 +* while>0(a,I) halts_on Initialize sm by SCMFSA7B:def 7;
      then P1 halts_on sm1;
      then consider j being Nat such that
A21:  CurInstr(P1,Comput(P1,sm,j))
        = halt SCM+FSA by A20;
A22: Comput(P1,s1,n+j)
 = Comput(P1,Comput(P1,s1,n),j) by EXTPRO_1:4;
      CurInstr(P1,Comput(P1,s1,n+j))
       = halt SCM+FSA by A17,A21,A22;
      then P1 halts_on s1 by EXTPRO_1:29;
      hence while>0(a,I) is_halting_on s,P by SCMFSA7B:def 7;
    end;
  end;
  hence thesis;
end;
