
theorem lemgcdii:
for R being EuclidianRing
for a,b being Element of R
for g being a_gcd of a,b ex r,s being Element of R st g = a * r + b * s
proof
let R be EuclidianRing, a,b be Element of R, g be a_gcd of a,b;
per cases;
suppose a is zero;
  then g is_associated_to b by lemgcd0;
  then consider c being Element of R such that
  A: c is unital & b * c = g by GCD_1:18;
  take 0.R, c;
  thus thesis by A;
  end;
suppose S: a is non zero;
set f = the DegreeFunction of R;
defpred P[Nat] means
  ex d being Element of R st $1 = f.d & d in {a,b}-Ideal & {d}-Ideal <> {0.R};
A: ex k being Nat st P[k]
   proof
   {a,b}-Ideal is principal by IDEAL_1:def 28; then
   consider d being Element of R such that
   A1: {d}-Ideal = {a,b}-Ideal;
   A3: now assume A4: {d}-Ideal = {0.R};
       a <> 0.R & a in {d}-Ideal by S,A1,IDEAL_1:68;
       hence contradiction by A4,TARSKI:def 1;
       end;
   take f.d;
   thus thesis by A1,A3,IDEAL_1:66;
   end;
consider k being Nat such that
B: P[k] & for n being Nat st P[n] holds k <= n from NAT_1:sch 5(A);
consider d being Element of R such that
H1: f.d = k & d in {a,b}-Ideal & {d}-Ideal <> {0.R} by B;
H2: {d}-Ideal = the set of all d*r where r is Element of R &
    {a,b}-Ideal = the set of all a*r + b*s where r,s is Element of R
    by IDEAL_1:64,IDEAL_1:65; then
consider s,t being Element of R such that H3: d = a * s + b * t by H1;
d is a_gcd of a,b
    proof
    B0: d <> 0.R by H1,IDEAL_1:48; then
    consider q,r being Element of R such that
    B1: a = q * d + r & (r = 0.R or f.r < f.d) by INT_3:def 9;
    now assume B2: r <> 0.R;
      B4: now assume B5: {r}-Ideal = {0.R};
          r in {r}-Ideal by IDEAL_1:66;
          hence contradiction by B2,B5,TARSKI:def 1;
          end;
      r = a - q * (a * s + b * t) by H3,B1,VECTSP_2:2
       .= a + (-q) * (a * s + b * t) by VECTSP_1:9
       .= a + ((-q) * (a * s) + (-q) * (b * t)) by VECTSP_1:def 2
       .= (a + (-q) * (a * s)) + (-q) * (b * t) by RLVECT_1:def 3
       .= (a * 1.R) + a * ((-q) * s) + (-q) * (b * t) by GROUP_1:def 3
       .= a * (1.R + (-q) * s) + (-q) * (b * t) by VECTSP_1:def 2
       .= a * (1.R + (-q) * s) + b * ((-q) * t) by GROUP_1:def 3; then
      r in {a,b}-Ideal by H2;
      hence contradiction by B1,B2,B4,B,H1;
      end; then
    B2: d divides a by B1,GCD_1:def 1;
    consider q,r being Element of R such that
    B1: b = q * d + r & (r = 0.R or f.r < f.d) by B0,INT_3:def 9;
    now assume B2: r <> 0.R;
      B4: now assume B5: {r}-Ideal = {0.R};
          r in {r}-Ideal by IDEAL_1:66;
          hence contradiction by B2,B5,TARSKI:def 1;
          end;
      r = b - q * (a * s + b * t) by H3,B1,VECTSP_2:2
       .= b + (-q) * (a * s + b * t) by VECTSP_1:9
       .= b + ((-q) * (b * t) + (-q) * (a * s)) by VECTSP_1:def 2
       .= (b + (-q) * (b * t)) + (-q) * (a * s) by RLVECT_1:def 3
       .= (b * 1.R) + b * ((-q) * t) + (-q) * (a * s) by GROUP_1:def 3
       .= b * (1.R + (-q) * t) + (-q) * (a * s) by VECTSP_1:def 2
       .= b * (1.R + (-q) * t) + a * ((-q) * s) by GROUP_1:def 3; then
      r in {a,b}-Ideal by H2;
      hence contradiction by B1,B2,B4,B,H1;
      end; then
    B3: d divides b by B1,GCD_1:def 1;
    now let x be Element of R;
      assume B4: x divides a & x divides b; then
      consider c being Element of R such that
      B5: x * c = a * s by GCD_1:def 1,GCD_1:7;
      consider e being Element of R such that
      B6: x * e = b * t by B4,GCD_1:def 1,GCD_1:7;
      x * (c + e) = x * c + x * e by VECTSP_1:def 2;
      hence x divides d by H3,B6,B5,GCD_1:def 1;
      end;
    hence thesis by B2,B3,RING_4:def 10;
    end; then
H3: {g}-Ideal = {d}-Ideal by RING_2:21,RING_4:49;
H5: {d}-Ideal c= {a,b}-Ideal by H1,IDEAL_1:67;
g in {a,b}-Ideal by H3,H5,IDEAL_1:66;
hence thesis by H2;
end;
end;
