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;

theorem Th8:
  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;
  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);
A13:  P+*while=0(a,I) +* while=0(a,I) = P+*while=0(a,I);
      sk1= Comput(P +* while=0(a,I), Initialize sk,
       (LifeSpan(P +* while=0(a,I) +* I,Initialize sk) + 2)) by Def1;
      then IC sk1 = 0 by A11,A10,Th4,A13;
      hence StepWhile=0(a,I,P,s).((k+1)+1)= Comput(P1, s1,m) by A12,Th7;
    end;
    hence P[k+1];
  end;
A14: P[ 0]
  proof
    assume 0+1 <= m;
    take n=(LifeSpan(P +* while=0(a,I)+* I,Initialize s) + 2);
    thus thesis by Th6;
  end;
A15: for k being Nat holds P[k] from NAT_1:sch 2(A14,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 Def1;
      hence thesis by Th1;
    end;
    suppose
  m<>0;
      then consider i being Nat such that
A16:  m=i+1 by NAT_1:6;
      reconsider m,i as Element of NAT by ORDINAL1:def 12;
      set sm=StepWhile=0(a,I,P,s).m;
      set si=StepWhile=0(a,I,P,s).i;
      i < m by A16,NAT_1:13;
      then F(i) <> 0 by A6;
      then
A17:  si.a = 0 by A3;
A18:  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,Def1;
      then sm is 0-started by A18,A17,Th4,A2;
      then
A19:  Start-At(0,SCM+FSA) c= sm by MEMSTR_0:29;
      set p=(LifeSpan(P+*while=0(a,I)+* I,Initialize s) + 3);
      set sm1=Initialize sm;
      consider n being Nat such that
A20:  sm = Comput(P1, s1,n) by A15,A16;
A21:  sm1=sm by A19,FUNCT_4:98;
      sm.a <> 0 by A3,A5;
      then while=0(a,I) is_halting_on sm,P by Th1;
      then P +* while=0(a,I) halts_on Initialize sm by SCMFSA7B:def 7;
      then P +* while=0(a,I) halts_on Initialize sm;
      then P1 halts_on sm1;
      then consider j being Nat such that
A22:  CurInstr(P1,Comput(P1,sm,j))
        = halt SCM+FSA by A21;
A23: 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 A20,A22,A23;
      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;
