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,m being Variable of g st ex d being Function st d.b = 0 & d.x
= 1 & d.y = 2 & d.m = 3 for s being Element of Funcs(X, INT) for n being Nat st
n = s.m holds g.(s, y:=1\; while(m gt 0, if-then(m is_odd, y*=x)\; m/=2\; x*=x
  ) ).y = (s.x)|^n
proof
  set S = Funcs(X, INT);
  set T = S\(b,0);
A1: g complies_with_if_wrt T by AOFA_000:def 32;
  let x,y,m be Variable of g;
  given d being Function such that
A2: d.b = 0 and
A3: d.x = 1 and
A4: d.y = 2 and
A5: d.m = 3;
  defpred R[Element of S] means $1.m > 0;
  set C = m gt 0;
  let s be Element of Funcs(X, INT);
  let n be Nat;
  defpred P[Element of S] means (s.x)|^n = ($1.y)*(($1.x)to_power($1.m)) & $1.
  m >= 0;
  deffunc F(Element of S) = In($1.m, NAT);
  set I = if-then(m is_odd, y*=x);
  set J = I\; m/=2\; x*=x;
  set s0 = g.(s, y:=1);
A6: m <> y by A4,A5;
  then
A7: s0.m = s.m by Th25;
A8: for s being Element of S st P[s] holds P[g.(s,C)] & (g.(s,C) in T iff R
  [g.(s,C)])
  proof
    let s be Element of S such that
A9: P[s];
    set s1 = g.(s, C);
A10: s1.x = s.x by A2,A3,Th38;
    s1.m = s.m by A2,A5,Th38;
    hence P[g.(s,C)] by A2,A4,A9,A10,Th38;
A11: s.m <= 0 implies s1.b = 0 by Th38;
    s.m > 0 implies s1.b = 1 by Th38;
    hence thesis by A11,Th2,Th38;
  end;
A12: s0.y = 1 by Th25;
  set fs = g.(s0, while(C,J));
  set s1 = g.(s0,C);
  assume
A13: n = s.m;
A14: (fs.x) to_power 0 = 1 by POWER:24;
A15: m <> x by A3,A5;
A16: for s being Element of S st R[s] holds (R[g.(s,J\;C)] iff g.(s,J\;C) in
  T) & F(g.(s,J\;C)) < F(s)
  proof
    let s be Element of S such that
A17: s.m > 0;
A18: F(s) = s.m by A17,INT_1:3,SUBSET_1:def 8;
    set q1 = g.(s,I);
    set q0 = g.(s, m is_odd);
    set sJ = g.(s,J);
    set sC = g.(sJ,C);
A19: g.(s,J\;C) = sC by AOFA_000:def 29;
A20: sJ.m <= 0 implies sC.b = 0 by Th38;
    sJ.m > 0 implies sC.b = 1 by Th38;
    hence R[g.(s,J\;C)] iff g.(s,J\;C) in T by A20,A19,Th2,Th38;
    set q2 = g.(q1,m/=2);
    set q3 = g.(q2,x*=x);
A21: q1 = g.(q0, y*=x) or q1 = g.(q0, EmptyIns A) by A1;
    q2 = g.(s,I\;m/=2) by AOFA_000:def 29;
    then q3 = g.(s,J) by AOFA_000:def 29;
    then
A22: sJ.m = q2.m by A15,Th33
      .= (q1.m) div 2 by Th45
      .= (q0.m) div 2 by A6,A21,Th33,AOFA_000:def 28
      .= (s.m) div 2 by A2,A5,Th49;
A23: sC.m = sJ.m by A2,A5,Th38;
    then sC.m in NAT by A17,A22,INT_1:3,61;
    then F(sC) = sC.m by SUBSET_1:def 8;
    hence thesis by A17,A23,A19,A22,A18,INT_1:56;
  end;
  set q = s;
A24: x <> y by A3,A4;
A25: for s being Element of S st P[s] & s in T & R[s] holds P[g.(s,J)]
  proof
    let s be Element of S such that
A26: P[s] and
    s in T and
    R[s];
    reconsider sm = s.m as Element of NAT by A26,INT_1:3;
    s.m = ((s.m) div 2)*2+((s.m) mod 2) by NEWTON:66;
    then
A27: (q.x)|^n = (s.y)*(((s.x)to_power((sm div 2)*2))*((s.x)to_power(sm mod
    2))) by A26,FIB_NUM2:5
      .= (s.y)*((s.x)to_power(sm mod 2))*((s.x)to_power((sm div 2)*2))
      .= (s.y)*((s.x)to_power(sm mod 2))* (((s.x)to_power 2) to_power (sm
    div 2)) by NEWTON:9
      .= (s.y)*((s.x)to_power(sm mod 2))* (((s.x)*(s.x)) to_power (sm div 2)
    ) by NEWTON:81;
    set q1 = g.(s,I);
    set q0 = g.(s, m is_odd);
    set sJ = g.(s,J);
    set q2 = g.(q1,m/=2);
    set q3 = g.(q2,x*=x);
A28: q1 = g.(q0, y*=x) or q1 = g.(q0, EmptyIns A) by A1;
A29: q2.x = q1.x by A15,Th45
      .= q0.x by A24,A28,Th33,AOFA_000:def 28;
A30: q2.y = q1.y by A6,Th45;
A31: q0.y = s.y by A2,A4,Th49;
A32: q0.x = s.x by A2,A3,Th49;
    q2 = g.(s,I\;m/=2) by AOFA_000:def 29;
    then
A33: q3 = g.(s,J) by AOFA_000:def 29;
    then
A34: sJ.y = q2.y by A24,Th33;
A35: sm div 2 = s.m div 2;
A36: now
A37:  q0.b = (s.m) mod 2 by Th49;
      per cases by A35,A37,NAT_D:12;
      suppose
A38:    q0.b = 0;
        then q0 nin T by Th2;
        then q1 = g.(q0, EmptyIns A) by A1;
        then
A39:    q1.y = q0.y by AOFA_000:def 28;
A40:    (s.y)*1 = s.y;
        (s.x)to_power 0 = 1 by POWER:24;
        hence (s.y)*((s.x)to_power(sm mod 2)) = sJ.y by A34,A30,A31,A38,A39,A40
,Th49;
      end;
      suppose
A41:    q0.b = 1;
        then q0 in T;
        then q1 = g.(q0, y*=x) by A1;
        then
A42:    q1.y = (q0.y)*(q0.x) by Th33;
        (s.x)to_power 1 = s.x by POWER:25;
        hence (s.y)*((s.x)to_power(sm mod 2)) = sJ.y by A32,A34,A30,A31,A41,A42
,Th49;
      end;
    end;
    sJ.m = q2.m by A15,A33,Th33
      .= (q1.m) div 2 by Th45
      .= (q0.m) div 2 by A6,A28,Th33,AOFA_000:def 28
      .= (s.m) div 2 by A2,A5,Th49;
    hence thesis by A33,A29,A32,A36,A27,Th33;
  end;
A43: s0.m <= 0 implies s1.b = 0 by Th38;
  s0.m > 0 implies s1.b = 1 by Th38;
  then
A44: g.(s0,C) in T iff R[g.(s0,C)] by A43,Th2,Th38;
A45: g iteration_terminates_for J\;C, g.(s0,C) from AOFA_000:sch 3(A44, A16);
  s0.x = s.x by A24,Th25;
  then
A46: P[s0] by A13,A7,A12,POWER:41;
A47: P[g.(s0,while(C,J))] & not R[g.(s0,while(C,J))] from AOFA_000:sch 5(A46
  ,A45,A25,A8);
  then fs.m = 0;
  hence thesis by A47,A14,AOFA_000:def 29;
end;
