reserve p,p1,p2,h for Instruction-Sequence of SCM+FSA;
reserve k, l, n for Nat,
  j for Integer,
  i,i1 for Instruction of SCM+FSA;
reserve s, s1, s2 for State of SCM+FSA,
  a for read-write Int-Location,
  b for Int-Location,
  I, J for MacroInstruction of SCM+FSA,
  Ig for good MacroInstruction of SCM+FSA,
  i, j, k, m, n for Nat;

theorem Th27: :: SCMFSA_9:42, corrected
  for I being really-closed MacroInstruction of SCM+FSA holds
  ProperBodyWhile>0 a,I,s,p & WithVariantWhile>0 a,I,s,p implies
  while>0(a,I) is_halting_on s,p
proof let I be really-closed MacroInstruction of SCM+FSA;
  assume
A1: for k being Nat st StepWhile>0(a,I,p,s).k.a > 0 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);
  defpred S[Nat] means StepWhile>0(a,I,p,s).$1.a <= 0;
  given f being Function of product the_Values_of SCM+FSA,NAT such that
A2: for k being Nat holds (f.(StepWhile>0(a,I,p,s).(k+1)) < f.(
  StepWhile>0(a,I,p,s).k) or (StepWhile>0(a,I,p,s).k).a <= 0 );
  deffunc F(Nat) = f.(StepWhile>0(a,I,p,s).$1);
A3: for k holds F(k+1) < F(k) or S[k] by A2;
  consider m being Nat such that
A4: S[m] and
A5: for n st S[n] holds m <= n from NAT_1:sch 18(A3);
  defpred P[Nat] means $1+1 <= m implies
   ex k st StepWhile>0(a,I,p,s).($1+1)=Comput(p1,s1,k);
A6: now
    let k be Nat;
    assume
A7: P[k];
    now
      set sk1=StepWhile>0(a,I,p,s).(k+1);
      set sk=StepWhile>0(a,I,p,s).k,
          pk = p +* while>0(a,I);
      assume
A8:   (k+1)+ 1 <= m;
      k + 0 < k+ (1+ 1) by XREAL_1:6;
      then k < m by A8,XXREAL_0:2;
      then
A9:   sk.a > 0 by A5;
      (k+1)+ 0 < (k+ 1)+ 1 by XREAL_1:6;
      then consider n being Nat such that
A10:  sk1 = Comput(p1,s1,n) by A7,A8,XXREAL_0:2;
A11:  sk1 = Comput(pk +* while>0(a,I),(Initialize sk),
      (LifeSpan(pk +* I,Initialize sk) + 2))
by SCMFSA_9:def 5;
      take m=n +(LifeSpan(pk +* I,Initialize sk1) + 2);
      I is_halting_on sk,pk by A1,A9;
      then IC sk1 = 0 by A11,A9,SCMFSA_9:42;
      hence StepWhile>0(a,I,p,s).((k+1)+1)=Comput(p1,s1,m) by A10,SCMFSA_9:45;
    end;
    hence P[k+1];
  end;
A12: P[0]
  proof
    assume 0+1 <= m;
    take n=(LifeSpan(p +* while>0(a,I) +* I,Initialize s) + 2);
    thus thesis by SCMFSA_9:44;
  end;
A13: for k being Nat holds P[k] from NAT_1:sch 2(A12,A6);
    per cases;
    suppose
      m=0;
      then s.a <= 0 by A4,SCMFSA_9:def 5;
      hence thesis by SCMFSA_9:38;
    end;
    suppose
A14:  m<>0;
      set ii=(LifeSpan(p+* while>0(a,I) +* I,Initialize s) + 2);
      set sm=StepWhile>0(a,I,p,s).m,
          pm = p +* while>0(a,I);
      set sm1=Initialize sm,
          pm1 = pm +* while>0(a,I);
      consider i being Nat such that
A15:  m=i+1 by A14,NAT_1:6;
      reconsider i as Element of NAT by ORDINAL1:def 12;
      set si=StepWhile>0(a,I,p,s).i,
          psi = p +* while>0(a,I);
A16:  sm= Comput(psi +* while>0(a,I),(Initialize si),
      (LifeSpan(psi +* I,Initialize si) + 2))
      by A15,SCMFSA_9:def 5;
      m=i+1 by A15;
      then consider n being Nat such that
A17:  sm = Comput(p1,s1,n) by A13;
      i < m by A15,NAT_1:13;
      then
A18:  si.a > 0 by A5;
      then I is_halting_on si,psi by A1;
      then
      IC sm = 0 by A16,A18,SCMFSA_9:42;
      then Start-At(0,SCM+FSA) c= sm by MEMSTR_0:30;
      then
A19:  sm1=sm by FUNCT_4:98;
      while>0(a,I) is_halting_on sm,pm by A4,SCMFSA_9:38;
      then pm1 halts_on sm1 by SCMFSA7B:def 7;
      then consider j being Nat such that
A20:  CurInstr(pm,Comput(pm,sm,j)) = halt SCM+FSA by A19;
A21: 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,A20,A21;
      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;
