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
  for N, result being read-write Int-Location st N <> result for n being
Element of NAT st n = s.N
 holds IExec(Fusc_macro(N,result),p,s).result = Fusc n
  & IExec(Fusc_macro(N,result),p,s).N = n
proof
  let N, result be read-write Int-Location such that
A1: N <> result;
  set i0 = SubFrom(result, result);
  set rem2 = 3-rdRWNotIn {N, result};
  set aux = 2-ndRWNotIn {N, result};
  set next = 1-stRWNotIn {N, result};
  set I3i0 = rem2 := 2;
  set I3i1 = Divide(aux, rem2);
  set I3I2I0 = Macro AddTo(next, result);
  set I3I2I1 = Macro AddTo(result, next);
  reconsider I3I2 = if=0 ( rem2, I3I2I0, I3I2I1 )
     as really-closed Program of SCM+FSA;
  reconsider I = I3i0 ";" I3i1 ";" I3I2 as
    really-closed MacroInstruction of SCM+FSA;
  let n be Element of NAT such that
A2: n = s.N;
A3: next <> rem2 by SCMFSA_M:26;
A4: aux <> next by SCMFSA_M:26;
  set I3 = while>0 ( aux, I );
  deffunc U(Element of product the_Values_of SCM+FSA) = |.$1.aux.|;
  set i2 = aux := N;
  set i1 = next := intloc 0;
  set t = IExec(i0 ";" i1 ";" i2,p,s),
      q = p;
  set It = Initialized t;
  set SWt = StepWhile>0(aux,I,q,It),
      PWt = q +* while>0(aux,I);
  defpred X[Nat] means ex au, ne, re being Nat st SWt.$1
.aux = au & SWt.$1.next = ne & SWt.$1.result = re & SWt.$1.N = n & Fusc n = ne
  * Fusc au + re * Fusc (au+1);
  consider f being Function of product the_Values_of SCM+FSA,NAT such
  that
A5: for x being Element of product the_Values_of SCM+FSA holds f.x
  = U(x) from FUNCT_2:sch 4;
A6: N in {N, result} by TARSKI:def 2;
  then
A7: N <> next by SCMFSA_M:25;
A8: result in {N, result} by TARSKI:def 2;
  then
A9: aux <> result by SCMFSA_M:25;
A10: result <> rem2 by A8,SCMFSA_M:25;
A11: next <> result by A8,SCMFSA_M:25;
A12: N <> rem2 by A6,SCMFSA_M:25;
A13: N <> aux by A6,SCMFSA_M:25;
A14: aux <> rem2 by SCMFSA_M:26;
A15: for u being State of SCM+FSA,h st
 ex au, ne, re being Nat st u
.aux = au & u.next = ne & u.result = re & u.N = n & Fusc n = ne * Fusc au + re
* Fusc (au+1) ex au1, ne1, re1 being Nat
 st IExec(I,h,u).aux = au1 &
IExec(I,h,u).next = ne1 & IExec(I,h,u).result = re1 & IExec(I,h,u).N = n &
Fusc n
  = ne1 * Fusc au1 + re1 * Fusc (au1+1) & au1 = u.aux div 2
  proof
    let u be State of SCM+FSA, h;
    given au, ne, re being Nat such that
A16: u.aux = au and
A17: u.next = ne and
A18: u.result = re and
A19: u.N = n and
A20: Fusc n = ne * Fusc au + re * Fusc (au+1);
A21: (Initialized IExec(I3i0 ";" I3i1,h,u)).next = IExec(I3i0 ";" I3i1,h,u).
    next by SCMFSA_M:37
      .= Exec(I3i1, IExec(I3i0,h,u)).next by SCMFSA6C:6
      .= IExec(I3i0,h,u).next by A4,A3,SCMFSA_2:67
      .= ne by A17,SCMFSA7B:3,SCMFSA_M:26;
A22: (Initialized IExec(I3i0 ";" I3i1,h,u)).aux = IExec(I3i0 ";" I3i1,h,u).
    aux by SCMFSA_M:37
      .= Exec(I3i1, IExec(I3i0,h,u)).aux by SCMFSA6C:6
      .= IExec(I3i0,h,u).aux div IExec(I3i0,h,u).rem2 by A14,SCMFSA_2:67
      .= u.aux div IExec(I3i0,h,u).rem2 by SCMFSA7B:3,SCMFSA_M:26
      .= u.aux div 2 by SCMFSA7B:3;
A23: (Initialized IExec(I3i0 ";" I3i1,h,u)).result = IExec(I3i0 ";" I3i1,h,u)
    .result by SCMFSA_M:37
      .= Exec(I3i1, IExec(I3i0,h,u)).result by SCMFSA6C:6
      .= IExec(I3i0,h,u).result by A9,A10,SCMFSA_2:67
      .= re by A10,A18,SCMFSA7B:3;
A24: (Initialized IExec(I3i0 ";" I3i1,h,u)).N = IExec(I3i0 ";" I3i1,h,u).N by
SCMFSA_M:37
      .= Exec(I3i1, IExec(I3i0,h,u)).N by SCMFSA6C:6
      .= IExec(I3i0,h,u).N by A12,A13,SCMFSA_2:67
      .= n by A12,A19,SCMFSA7B:3;
A25: IExec(I3i0 ";" I3i1,h,u).rem2 = Exec(I3i1, IExec(I3i0,h,u)).rem2 by
SCMFSA6C:6
      .= IExec(I3i0,h,u).aux mod IExec(I3i0,h,u).rem2 by SCMFSA_2:67
      .= u.aux mod IExec(I3i0,h,u).rem2 by SCMFSA7B:3,SCMFSA_M:26
      .= u.aux mod 2 by SCMFSA7B:3;
    per cases;
    suppose
A26:  au is even;
      reconsider ne1 = ne + re as Element of NAT by ORDINAL1:def 12;
      reconsider au1 = u.aux div 2 as Element of NAT by A16,INT_1:3,55;
      take au1, ne1, re;
      consider k being Nat such that
A27:  au = 2*k by A26,ABIAN:def 2;
A28:  u.aux mod 2 = (2*k + 0) mod 2 by A16,A27
        .= 0 mod 2 by NAT_D:21
        .= 0 by NAT_D:26;
      IExec(I,h,u).aux = IExec(I3I2,h,IExec(I3i0 ";" I3i1,h,u)).aux
       by SCMFSA6C:1
        .= IExec(I3I2I0,h,IExec(I3i0 ";" I3i1,h,u)).aux by A25,A28,SCMFSA8B:18
        .= (Exec(AddTo(next, result),
                   Initialized IExec(I3i0 ";" I3i1,h,u))).aux
      by SCMFSA6C:5
        .= u.aux div 2 by A4,A22,SCMFSA_2:64;
      hence IExec(I,h,u).aux = au1;
      thus IExec(I,h,u).next = IExec(I3I2,h,IExec(I3i0 ";" I3i1,h,u)).next by
SCMFSA6C:1
        .= IExec(I3I2I0,h,IExec(I3i0 ";" I3i1,h,u)).next by A25,A28,SCMFSA8B:18
        .= (Exec(AddTo(next, result),
         Initialized IExec(I3i0 ";" I3i1,h,u))).next by SCMFSA6C:5
        .= ne1 by A21,A23,SCMFSA_2:64;
      thus IExec(I,h,u).result = IExec(I3I2,h,IExec(I3i0 ";" I3i1,h,u)).result
       by SCMFSA6C:1
        .= IExec(I3I2I0,h,IExec(I3i0 ";" I3i1,h,u)).result
         by A25,A28,SCMFSA8B:18
        .= (Exec(AddTo(next, result),
             Initialized IExec(I3i0 ";" I3i1,h,u))).result by SCMFSA6C:5
        .= re by A11,A23,SCMFSA_2:64;
      thus IExec(I,h,u).N = IExec(I3I2,h,IExec(I3i0 ";" I3i1,h,u)).N
       by SCMFSA6C:1
        .= IExec(I3I2I0,h,IExec(I3i0 ";" I3i1,h,u)).N by A25,A28,SCMFSA8B:18
        .= (Exec(AddTo(next, result),
        Initialized IExec(I3i0 ";" I3i1,h,u))).N by SCMFSA6C:5
        .= n by A7,A24,SCMFSA_2:64;
      au1 = k by A16,A27,NAT_D:18;
      hence Fusc n = ne1 * Fusc au1 + re * Fusc (au1+1) by A20,A27,PRE_FF:20;
      thus thesis;
    end;
    suppose
A29:  au is odd;
      reconsider re1 = ne + re as Element of NAT by ORDINAL1:def 12;
      reconsider au1 = u.aux div 2 as Element of NAT by A16,INT_1:3,55;
      take au1, ne, re1;
      consider k being Nat such that
A30:  au = 2*k +1 by A29,ABIAN:9;
A31:  u.aux mod 2 = 1 mod 2 by A16,A30,NAT_D:21
        .= 1 by NAT_D:24;
      IExec(I,h,u).aux = IExec(I3I2,h,IExec(I3i0 ";" I3i1,h,u)).aux
       by SCMFSA6C:1
        .= IExec(I3I2I1,h,IExec(I3i0 ";" I3i1,h,u)).aux by A25,A31,SCMFSA8B:18
        .= (Exec(AddTo(result, next),
         Initialized IExec(I3i0 ";" I3i1,h,u))).aux
      by SCMFSA6C:5
        .= u.aux div 2 by A9,A22,SCMFSA_2:64;
      hence IExec(I,h,u).aux = au1;
      thus IExec(I,h,u).next = IExec(I3I2,h,IExec(I3i0 ";" I3i1,h,u)).next by
SCMFSA6C:1
        .= IExec(I3I2I1,h,IExec(I3i0 ";" I3i1,h,u)).next by A25,A31,SCMFSA8B:18
        .= (Exec(AddTo(result, next),
        Initialized IExec(I3i0 ";" I3i1,h,u))).
      next by SCMFSA6C:5
        .= ne by A11,A21,SCMFSA_2:64;
      thus IExec(I,h,u).result
       = IExec(I3I2,h,IExec(I3i0 ";" I3i1,h,u)).result by SCMFSA6C:1
        .= IExec(I3I2I1,h,IExec(I3i0 ";" I3i1,h,u)).result
         by A25,A31,SCMFSA8B:18
        .= (Exec(AddTo(result, next),
        Initialized IExec(I3i0 ";" I3i1,h,u))).
      result by SCMFSA6C:5
        .= re1 by A21,A23,SCMFSA_2:64;
      thus IExec(I,h,u).N = IExec(I3I2,h,IExec(I3i0 ";" I3i1,h,u)).N
       by SCMFSA6C:1
        .= IExec(I3I2I1,h,IExec(I3i0 ";" I3i1,h,u)).N by A25,A31,SCMFSA8B:18
        .= (Exec(AddTo(result, next),
        Initialized IExec(I3i0 ";" I3i1,h,u))).N by SCMFSA6C:5
        .= n by A1,A24,SCMFSA_2:64;
      au1 = 2*k div 2 by A16,A30,NAT_2:26
        .= k by NAT_D:18;
      hence Fusc n = ne * Fusc au1 + re1 * Fusc (au1+1) by A20,A30,PRE_FF:19;
      thus thesis;
    end;
  end;
A32: It.intloc 0 = 1 by SCMFSA_M:9;
A33: for k being Nat st X[k] holds X[k+1]
  proof
    let k be Nat;
    given au, ne, re being Nat such that
A34: SWt.k.aux = au and
A35: SWt.k.next = ne and
A36: SWt.k.result = re and
A37: SWt.k.N = n and
A38: Fusc n = ne * Fusc au + re * Fusc (au+1);
A39: SWt.k.intloc 0 = 1 by A32,Th33;
    per cases;
    suppose
A40:  SWt.k.aux > 0;
      consider au1, ne1, re1 being Nat such that
A41:  IExec(I,PWt,SWt.k).aux = au1 and
A42:  IExec(I,PWt,SWt.k).next = ne1 and
A43:  IExec(I,PWt,SWt.k).result = re1 and
A44:  IExec(I,PWt,SWt.k).N = n and
A45:  Fusc n = ne1 * Fusc au1 + re1 * Fusc (au1+1) and
      au1 = SWt.k.aux div 2 by A15,A34,A35,A36,A37,A38;
      take au1, ne1, re1;
A46:  DataPart SWt.(k+1) = DataPart IExec(I,PWt,SWt.k) by A39,A40,Th32;
      hence SWt.(k+1).aux = au1 by A41,SCMFSA_M:2;
      thus SWt.(k+1).next = ne1 by A46,A42,SCMFSA_M:2;
      thus SWt.(k+1).result = re1 by A46,A43,SCMFSA_M:2;
      thus SWt.(k+1).N = n by A46,A44,SCMFSA_M:2;
      thus thesis by A45;
    end;
    suppose
A47:  SWt.k.aux <= 0;
      take au, ne, re;
A48:  DataPart SWt.(k+1) = DataPart SWt.k by A47,Th31;
      hence SWt.(k+1).aux = au by A34,SCMFSA_M:2;
      thus SWt.(k+1).next = ne by A35,A48,SCMFSA_M:2;
      thus SWt.(k+1).result = re by A36,A48,SCMFSA_M:2;
      thus SWt.(k+1).N = n by A37,A48,SCMFSA_M:2;
      thus thesis by A38;
    end;
  end;
  t.intloc 0 = 1 by SCMFSA6B:11;
  then
A49: DataPart t = DataPart It by SCMFSA_M:19;
A50: X[0]
  proof
    take au = n;
    take ne = 1;
    take re = 0;
A51: SWt.0 = It by SCMFSA_9:def 5;
    hence SWt.0.aux = t.aux by A49,SCMFSA_M:2
      .= Exec(i2, IExec(i0 ";" i1,p,s)).aux by SCMFSA6C:6
      .= IExec(i0 ";" i1,p,s).N by SCMFSA_2:63
      .= Exec(i1, Exec(i0, Initialized s)).N by SCMFSA6C:8
      .= Exec(i0, Initialized s).N by A7,SCMFSA_2:63
      .= (Initialized s).N by A1,SCMFSA_2:65
      .= au by A2,SCMFSA_M:37;
    thus SWt.0.next = t.next by A49,A51,SCMFSA_M:2
      .= Exec(i2, IExec(i0 ";" i1,p,s)).next by SCMFSA6C:6
      .= IExec(i0 ";" i1,p,s).next by A4,SCMFSA_2:63
      .= Exec(i1, Exec(i0, Initialized s)).next by SCMFSA6C:8
      .= Exec(i0, Initialized s).intloc 0 by SCMFSA_2:63
      .= (Initialized s).intloc 0 by SCMFSA_2:65
      .= ne by SCMFSA_M:9;
    thus SWt.0.result = t.result by A49,A51,SCMFSA_M:2
      .= Exec(i2, IExec(i0 ";" i1,p,s)).result by SCMFSA6C:6
      .= IExec(i0 ";" i1,p,s).result by A9,SCMFSA_2:63
      .= Exec(i1, Exec(i0, Initialized s)).result by SCMFSA6C:8
      .= Exec(i0, Initialized s).result by A11,SCMFSA_2:63
      .= (Initialized s).result - (Initialized s).result by SCMFSA_2:65
      .= re;
    thus SWt.0.N = t.N by A49,A51,SCMFSA_M:2
      .= Exec(i2, IExec(i0 ";" i1,p,s)).N by SCMFSA6C:6
      .= IExec(i0 ";" i1,p,s).N by A13,SCMFSA_2:63
      .= Exec(i1, Exec(i0, Initialized s)).N by SCMFSA6C:8
      .= Exec(i0, Initialized s).N by A7,SCMFSA_2:63
      .= (Initialized s).N by A1,SCMFSA_2:65
      .= n by A2,SCMFSA_M:37;
    thus thesis;
  end;
A52: for k being Nat holds X[k] from NAT_1:sch 2(A50, A33);
  for k being Nat holds f.(SWt.(k+1)) < f.(SWt.k) or SWt.k.aux
  <= 0
  proof
    let k be Nat;
    consider au, ne, re being Nat such that
A53: SWt.k.aux = au and
A54: SWt.k.next = ne & SWt.k.result = re & SWt.k.N = n & Fusc n = ne *
    Fusc au + re * Fusc (au+1) by A52;
A55: f.(SWt.k) = |.SWt.k.aux.| by A5
      .= au by A53,ABSVALUE:def 1;
    now
      consider au1, ne1, re1 being Nat such that
A56:  IExec(I,PWt,SWt.k).aux = au1 and
      IExec(I,PWt,SWt.k).next = ne1 and
      IExec(I,PWt,SWt.k).result = re1 and
      IExec(I,PWt,SWt.k).N = n and
      Fusc n = ne1 * Fusc au1 + re1 * Fusc (au1+1) and
A57:  au1 = SWt.k.aux div 2 by A15,A53,A54;
      assume
A58:  au > 0;
      SWt.k.intloc 0 = 1 by A32,Th33;
      then DataPart SWt.(k+1) = DataPart IExec(I,PWt,SWt.k) by A53,A58,Th32;
      then
A59:  SWt.(k+1).aux = au1 by A56,SCMFSA_M:2;
      f.(SWt.(k+1)) = |.SWt.(k+1).aux.| by A5
        .= au1 by A59,ABSVALUE:def 1;
      hence f.(SWt.(k+1)) < f.(SWt.k) by A53,A55,A58,A57,INT_1:56;
    end;
    hence thesis by A53;
  end;
  then
A60: WithVariantWhile>0 aux,I,It,q;
  then consider k being Nat such that
A61: ExitsAtWhile>0(aux,I,q,It) = k and
A62: (StepWhile>0(aux,I,q,It).k).aux <= 0 and
  for i being Nat st SWt.i.aux <= 0 holds k <= i and
  DataPart (Comput(q +* while>0(aux,I),(Initialize It),
   (LifeSpan(q +* while>0(aux,I),Initialize It))))
    = DataPart StepWhile>0(aux,I,q,It).k
  by Def6;
A63: DataPart IExec(I3,q,t) = DataPart SWt.k by A60,A61,Th36;
  consider au, ne, re being Nat such that
A64: SWt.k.aux = au and
  SWt.k.next = ne and
A65: SWt.k.result = re and
A66: SWt.k.N = n and
A67: Fusc n = ne * Fusc au + re * Fusc (au+1) by A52;
A68: au = 0 by A62,A64;
  I3 is_halting_on It,q by A60,Th28;
  then
A69: I3 is_halting_on t,q by A49,SCMFSA8B:5;
  hence IExec(Fusc_macro(N,result),p,s).result = IExec(I3,q,t).result by
SFMASTR1:7
    .= Fusc n by A65,A67,A68,A63,PRE_FF:15,SCMFSA_M:2;
  thus IExec(Fusc_macro(N,result),p,s).N = IExec(I3,q,t).N by A69,SFMASTR1:7
    .= n by A66,A63,SCMFSA_M:2;
end;
