reserve A for preIfWhileAlgebra;
reserve A for Euclidean preIfWhileAlgebra;
reserve X for non empty countable set;
reserve T for Subset of Funcs(X, INT);
reserve f for Euclidean ExecutionFunction of A, Funcs(X, INT), T;
reserve A for Euclidean preIfWhileAlgebra,
  X for non empty countable set,
   z for (Element of X),
  s,s9 for (Element of Funcs(X, INT)),
  T for Subset of Funcs(X, INT),
  f for Euclidean ExecutionFunction of A, Funcs(X, INT), T,
  v for INT-Variable of A,f,
  t for INT-Expression of A,f;
reserve i for Integer;
reserve b for (Element of X),
  g for Euclidean ExecutionFunction of A, Funcs(X, INT), Funcs(X, INT)\(b,0);

theorem Th52:
  for I being Element of A for i,n being Variable of g st (ex d
being Function st d.b = 0 & d.n = 1 & d.i = 2) & for s holds g.(s,I).n = s.n &
  g.(s,I).i = s.i holds g iteration_terminates_for I\; i+=1\; i leq n, g.(s, i
  leq n)
proof
  let I be Element of A;
  let i,n be Variable of g;
  given d being Function such that
A1: d.b = 0 and
A2: d.n = 1 and
A3: d.i = 2;
  set J = i+=1;
  set C = i leq n;
  set S = Funcs(X, INT);
  set h = g;
  assume
A4: for s holds g.(s,I).n = s.n & g.(s,I).i = s.i;
  deffunc F(Element of S) = In($1.n+1-$1.i, NAT);
  defpred R[Element of S] means $1.i <= $1.n;
  set T = S\(b,0);
A5: i <> n by A2,A3;
A6: for s being Element of S st R[s] holds (R[h.(s,I\;J\;C) qua Element of
  S] iff h.(s,I\;J\;C) in T) & F(h.(s,I\;J\;C) qua Element of S) < F(s)
  proof
    let s be Element of S;
    set s1 = h.(s, I);
    set q = h.(s, I\;J);
    set q1 = h.(q, C);
A7: q = h.(s1, J) by AOFA_000:def 29;
    s1.i = s.i by A4;
    then q.i = s.i+1 by A7,Th28;
    then
A8: q1.i = s.i+1 by A1,A3,Th35;
A9: q.i > q.n implies q1.b = 0 by Th35;
    assume R[s];
    then reconsider ni = s.n-s.i as Element of NAT by INT_1:3,XREAL_1:48;
A10: q1 = h.(s, I\;J\;C) by AOFA_000:def 29;
A11: q.i <= q.n implies q1.b = 1 by Th35;
    q1.i = q.i by A1,A3,Th35;
    hence
    R[h.(s,I\;J\;C) qua Element of S] iff h.(s,I\;J\;C) in T by A1,A2,A9,A11
,A10,Th2,Th35;
A12: F(s) = ni+1;
    s1.n = s.n by A4;
    then q.n = s.n by A5,A7,Th28;
    then q1.n = s.n by A1,A2,Th35;
    then F(q1 qua Element of S) = ni by A8;
    hence thesis by A10,A12,NAT_1:13;
  end;
  reconsider s1 = h.(s, C) as Element of S;
A13: s.i > s.n implies s1.b = 0 by Th35;
A14: s.i <= s.n implies s1.b = 1 by Th35;
  s1.i = s.i by A1,A3,Th35;
  then
A15: s1 in T iff R[s1] by A1,A2,A13,A14,Th2,Th35;
  h iteration_terminates_for I\;J\;C, s1 from AOFA_000:sch 3(A15,A6 );
  hence thesis;
end;
