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
  for x,y,z being Variable of g st ex d being Function st d.b = 0 & d.x
  = 1 & d.y = 2 & d.z = 3 holds while(y gt 0, z:=x\;z%=y\;x:=y\;y:=z)
  is_terminating_wrt g, {s: s.x > s.y & s.y >= 0}
proof
  set S = Funcs(X, INT);
  set T = S\(b,0);
  let x,y,z be Variable of g;
  set P = {s: s.x > s.y & s.y >= 0};
  given d being Function such that
A1: d.b = 0 and
A2: d.x = 1 and
A3: d.y = 2 and
A4: d.z = 3;
  set C = y gt 0, I = z:=x\;z%=y\;x:=y\;y:=z;
A5: P is_invariant_wrt C,g
  proof
    let s;
    set s1 = g.(s,C);
    assume s in P;
    then
A6: ex s9 st s9 = s & s9.x > s9.y & s9.y >= 0;
A7: s1.y = s.y by A1,A3,Th38;
    s1.x = s.x by A1,A2,Th38;
    hence thesis by A6,A7;
  end;
A8: now
    let s;
    assume s in P;
    then ex s9 st s9 = s & s9.x > s9.y & s9.y >= 0;
    then reconsider m = s.y as Element of NAT by INT_1:3;
    assume g.(g.(s,I),C) in T;
    then g.(g.(s,I),C).b <> 0 by Th2;
    then
A9: g.(s,I).y > 0 by Th38;
A10: g.(s,I).x = s.y by A1,A2,A3,A4,Lm1;
A11: g.(s,I).y = s.x mod s.y by A1,A2,A3,A4,Lm1;
    then m <> 0 by A9,INT_1:def 10;
    then g.(s,I).x > g.(s,I).y by A11,A10,NEWTON:65;
    hence g.(s,I) in P by A9;
  end;
A12: now
    let s;
    set s1 = g.(s,C);
A13: s.y <= 0 implies s1.b = 0 by Th38;
    assume g.(s,C) in P;
    then ex s9 st s9 = g.(s,C) & s9.x > s9.y & s9.y >= 0;
    then reconsider m = s1.y as Element of NAT by INT_1:3;
    s.y > 0 implies s1.b = 1 by Th38;
    then s1 in T iff m > 0 by A13,Th2,Th38;
    hence g iteration_terminates_for I\;C, g.(s,C) by A1,A2,A3,A4,Lm1;
  end;
  C is_terminating_wrt g by AOFA_000:104;
  hence thesis by A5,A8,A12,AOFA_000:107,118;
end;
