reserve i,j,k for Nat;

theorem
  Euclid-Algorithm, Start-At(0,SCM) computes Euclid-Function
proof
  set q = Euclid-Algorithm;
  set p = Start-At(0,SCM);
  let x be set;
  DataPart p = {} by MEMSTR_0:20;
  then
A1: dom DataPart p = {};
  assume x in dom Euclid-Function;
  then consider i1,i2 being Integer such that
A2: i1 > 0 and
A3: i2 > 0 and
A4: x = (a,b) --> (i1,i2) by Th8;
  x =  (a .--> i1) +* (b .--> i2) by A4;
  then reconsider d = x as FinPartState of SCM;
  consider t being State of SCM such that
A5: p +* d c= t by PBOOLE:141;
  consider T being Instruction-Sequence of SCM
    such that
A6: q c= T by PBOOLE:145;
A7: dom d = { a, b } by A4,FUNCT_4:62;
  then
A8: b in dom d by TARSKI:def 2;
A9: a in dom d by A7,TARSKI:def 2;
A10: for t being State of SCM st p +* d c= t
  holds t.a = i1 & t.b = i2
  proof
    let t be State of SCM;
    assume
A11:    p +* d c= t;
    d c= p +* d by FUNCT_4:25;
    then
A12: d c= t by A11;
    hence t.a = d.a by A9,GRFUNC_1:2
      .= i1 by A4,AMI_3:10,FUNCT_4:63;
    thus t.b = d.b by A8,A12,GRFUNC_1:2
      .= i2 by A4,FUNCT_4:63;
  end;
A14: now
    assume dom p meets dom d;
    then consider x being object such that
A15: x in dom p and
A16: x in dom d by XBOOLE_0:3;
A17: x = IC SCM by A15,TARSKI:def 1;
    x = a or x = b by A7,A16,TARSKI:def 2;
    hence contradiction by A17,AMI_3:13;
  end;
  then
A18: p c= p +* d by FUNCT_4:32;
A19: IC SCM in dom p by TARSKI:def 1;
  dom p /\ dom d = {} by A14,XBOOLE_0:def 7;
  then
A20: not IC SCM in dom d by A19,XBOOLE_0:def 4;
    set A = { IC SCM, a,b }, C = 5;
A21: dom (p +* d) = dom( p +* d)
       .= dom  p \/ dom d by FUNCT_4:def 1
       .= {IC SCM} \/ dom DataPart p \/ dom d by A19,MEMSTR_0:24
       .= { IC SCM } \/ { a, b } by A4,A1,FUNCT_4:62
      .= A by ENUMSET1:2;
A22: dom p c= dom(p +* d) by A18,RELAT_1:11;
  IC(p +* d) = IC p by A20,FUNCT_4:11
    .= 0 by FUNCOP_1:72;
  then
A23: p +* d is 0-started by A22,A19;
  then
A24: t is 0-started by A5,MEMSTR_0:17;
A25: p +* d is q-autonomic
  proof
    set A = { IC SCM, a,b }, C = 5;
    let P,Q being Instruction-Sequence of SCM
    such that
A26: q c= P and
A27: q c= Q;
    let s1,s2 be State of SCM such that
A28: (p +* d) c= s1 and
A29: (p +* d) c= s2;
A30: s2.a = i1 & s2.b = i2 by A10,A29;
    let k;
    defpred P[Nat] means
     IC Comput(P,s1,$1) = IC Comput(Q,s2,$1) &
     Comput(P,s1,$1).a = Comput(Q,s2,$1).a &
     Comput(P,s1,$1).b = Comput(Q,s2,$1).b;
A31: Comput(P,s1,0) = s1 & Comput(Q,s2,0) = s2 by EXTPRO_1:2;
A32: s1 is 0-started by A23,A28,MEMSTR_0:17;
A33: dom( Comput(P,s1,k)) = the carrier of SCM by PARTFUN1:def 2
      .= dom( Comput(Q,s2,k)) by PARTFUN1:def 2;
A34: s2 is 0-started by A23,A29,MEMSTR_0:17;
A35: for i,j being Nat st P[4*i] & j<>0 & j<=4 holds P[4*i+j]
    proof
      let i,j be Nat;
      assume that
A36:  IC Comput(P,s1,4*i) = IC Comput(Q,s2,4*i) and
A37:  Comput(P,s1,4*i).a = Comput(Q,s2,4*i).a and
A38:  Comput(P,s1,4*i).b = Comput(Q,s2,4*i).b;
      assume
A39:    j <> 0 & j <= 4;
      then j = 0 or ... or j = 4;
      then
A40:  j = 1 or ... or j = 4 by A39;
      per cases by A2,A3,A34,A27,A30,Lm4;
      suppose
A41:    IC Comput(Q,s2,4*i) = 0;
A42:    ( Comput(P,s1,4*i+1)).a = Comput(P,s1,4*i).a by A26,A36,A41,Th2
          .= ( Comput(Q,s2,4*i+1)).a by A27,A37,A41,Th2;
A43:    ( Comput(P,s1,4*i+1)).dl.2 = Comput(P,s1,4*i).b by A26,A36,A41,Th2
          .= ( Comput(Q,s2,4*i+1)).dl.2 by A27,A38,A41,Th2;
A44:    ( Comput(P,s1,4*i+1)).b = Comput(P,s1,4*i).b by A26,A36,A41,Th2
          .= ( Comput(Q,s2,4*i+1)).b by A27,A38,A41,Th2;
A45:    4*i + 1 + 1 = 4*i + (1 + 1);
A46:    (4*i+2)+1 = 4*i+(2+1);
A47:    IC Comput(Q,s2,4*i+1) = 1 by A27,A41,Th2;
        then
A48:    IC Comput(Q,s2,4*i+2) = 2 by A27,A45,Th3;
        then
A49:    IC Comput(Q,s2,4*i+3) = 3 by A27,A46,Th4;
A50:    IC Comput(P,s1,4*i+1) = 1 by A26,A36,A41,Th2;
        then
A51:    ( Comput(P,s1,4*i+2)).dl.2 = ( Comput(P,s1,4*i+1)).dl.2 by A26,A45,Th3
          .= ( Comput(Q,s2,4*i+2)).dl.2 by A27,A45,A47,A43,Th3;
A52:    ( Comput(P,s1,4*i+2)).b = ( Comput(P,s1,4*i
+1)).a mod (
        Comput(P,s1,4*i+1)).b by A26,A45,A50,Th3
          .= ( Comput(Q,s2,4*i+2)).b by A27,A45,A47,A42,A44,Th3;
A53:    IC Comput(P,s1,4*i+2) = 2 by A26,A45,A50,Th3;
        then
A54:    IC Comput(P,s1,4*i+3) = 3 by A26,A46,Th4;
A55:    ( Comput(P,s1,4*i+2)).a = ( Comput(P,s1,4*i+1)).a div (
        Comput(P,s1,4*i+1)).b by A26,A45,A50,Th3
          .= ( Comput(Q,s2,4*i+2)).a by A27,A45,A47,A42,A44,Th3;
A56:    4*i + 3 + 1 = 4*i + (3 + 1);
A57:    ( Comput(P,s1,4*i+3)).a = ( Comput(P,s1,4*i+2)).dl.2 by A26,A46,A53,Th4
          .= ( Comput(Q,s2,4*i+3)).a by A27,A46,A48,A51,Th4;
A58:    ( Comput(P,s1,4*i+3)).b = ( Comput(P,s1,4*i+2)).b by A26,A46,A53,Th4
          .= ( Comput(Q,s2,4*i+3)).b by A27,A46,A48,A52,Th4;
        ( Comput(P,s1,4*i+3)).b <= 0 or ( Comput(P,s1,4*i+3)).b > 0;
        then
        IC Comput(P,s1,4*i+4) = 4 & IC Comput(Q,s2,4*i+4) = 4 or
        IC Comput(P,s1,4*i+4) = 0 & IC Comput(Q,s2,4*i+4) = 0
          by A26,A27,A56,A54,A49,A58,Th5;
        hence IC Comput(P,s1,4*i+j) = IC Comput(Q,s2,4*i+j)
           by A40,A50,A27,A41,Th2,A26,A45,Th3,A48,A54,A46,Th4;
        ( Comput(P,s1,4*i+4)).a = ( Comput(P,s1,4*i+3)).a by A26,A56,A54,Th5
          .= ( Comput(Q,s2,4*i+4)).a by A27,A56,A49,A57,Th5;
        hence
        Comput(P,s1,4*i+j).a = Comput(Q,s2,4*i+j).a by A40,A42,A55,A57;
        ( Comput(P,s1,4*i+4)).b = ( Comput(P,s1,4*i+3)).b by A26,A56,A54,Th5
          .= ( Comput(Q,s2,4*i+4)).b by A27,A56,A49,A58,Th5;
        hence thesis by A40,A44,A52,A58;
      end;
      suppose
A59:    IC Comput(Q,s2,4*i) = 4;
        then P halts_at IC Comput(P,s1,4*i)
          by A26,A36,Lm3;
        then
A60:    Comput(P,s1,4*i+j) = Comput(P,s1,4*i) by EXTPRO_1:20,NAT_1:11;
        Q halts_at IC Comput(Q,s2,4*i) by A27,A59,Lm3;
        hence thesis by A36,A37,A38,A60,EXTPRO_1:20,NAT_1:11;
      end;
    end;
   reconsider k as Element of NAT by ORDINAL1:def 12;
    ( Comput(P,s1,0)).IC SCM = IC s1 by EXTPRO_1:2
      .= 0 by A32
      .= IC s2 by A34
      .= ( Comput(Q,s2,0)).IC SCM by EXTPRO_1:2;
    then
A61: P[ 0] by A10,A28,A30,A31;
A62: 4 > 0;
    P[k] from NAT_D:sch 2(A61,A62,A35);
   hence thesis by A21,A33,GRFUNC_1:31;
  end;
  take d;
  thus x = d;
A63: p +* d is q-halted
  proof
    reconsider i19 = i1, i29 = i2 as Element of NAT by A2,A3,INT_1:3;
    let t be State of SCM;
    assume
A64:  p +* d c= t;
    let P be Instruction-Sequence of SCM such that
A65:  q c= P;
    set t9 = Comput(P,t,4);
A66: t.b = i2 by A10,A64;
A67: t is 0-started & t.a = i1 by A23,A10,A64,MEMSTR_0:17;
    per cases by XXREAL_0:1;
    suppose
      i1 > i2;
      then ex k st P halts_at IC Comput(P,t,k)
       by A3,A65,A67,A66,Lm6;
      hence thesis by EXTPRO_1:16;
    end;
    suppose
A68:  i1 = i2;
A69:  i1 mod i2 = i19 mod i29
        .= 0 by A68,NAT_D:25;
A70:  t9 = Comput(P,t,4*(0+1));
      t = Comput(P,t,4*0) by EXTPRO_1:2;
      then t9.b = t.a mod t.b by A2,A3,A65,A67,A66,A70,Lm5;
      then IC t9 = 4 by A2,A3,A65,A67,A66,A69,A70,Lm4;
      then P halts_at IC t9 by A65,Lm3;
      hence thesis by EXTPRO_1:16;
    end;
    suppose
A71:  i1 < i2;
A72:  t9 = Comput(P,t,4*(0+1));
A73:  t = Comput(P,t,4*0) by EXTPRO_1:2;
      i1 mod i2 = i19 mod i29
        .= i19 by A71,NAT_D:24;
      then
A74:  t9.b = i1 by A2,A3,A65,A67,A66,A73,A72,Lm5;
      then IC t9 = 0 by A2,A3,A65,A67,A66,A72,Lm4;
      then
A75:  t9 is 0-started;
      t9.a = i2 by A2,A3,A65,A67,A66,A73,A72,Lm5;
      then consider k0 being Nat such that
A76:  P halts_at IC Comput(P,t9,k0) by A2,A71,A74,A75,A65,Lm6;
      P halts_at IC Comput(P,t,k0+4) by A76,EXTPRO_1:4;
      hence thesis by EXTPRO_1:16;
    end;
  end;
  thus
  p +* d is Autonomy of q by A25,A63,EXTPRO_1:def 12;
  then
A77: Result(q,p+*d) = Result(T,t)|dom (p+*d)
          by A6,A5,EXTPRO_1:def 13;
  a in the carrier of SCM;
  then
A78: a in dom Result(T,t) by PARTFUN1:def 2;
A79: d.a = i1 by A4,AMI_3:10,FUNCT_4:63;
A80: d.b = i2 by A4,FUNCT_4:63;
A81: d c= p +* d by FUNCT_4:25;
A82: dom d c= dom(p +* d) by A81,RELAT_1:11;
A83: d c= t by A81,A5;
A84: dom d = { a, b } by A4,FUNCT_4:62;
  then
A85:  b in dom d by TARSKI:def 2;
A86: t.b = i2 by A83,A80,A85,GRFUNC_1:2;
A87: a in dom d by A84,TARSKI:def 2;
  t.a = i1 by A83,A79,A87,GRFUNC_1:2;
  then
A88: (Result(T,t)).a = i1 gcd i2
      by A2,A3,A24,A86,Th7,A6;
  dom(a .--> (i1 gcd i2)) c= dom d by A84,ZFMISC_1:7;
   then
A90: dom(a .--> (i1 gcd i2)) c= dom(p +* d) by A82;
   a .--> (i1 gcd i2) c= (Result(T,t))|dom(p +* d)
        by A90,A78,A88,FUNCT_4:85,RELAT_1:151;
  hence Euclid-Function.d c= Result(q,p+* d) by A77,A2,A3,A4,Th9;
end;
