// ----------------------------------------------------------------- // gcd.txt: the greatest common divisor and the Euclidean algorithm // Concrete Abstractions: Theories and Algorithms // (c) 2020, Wolfgang Schreiner, RISC, JKU Linz, Austria // ----------------------------------------------------------------- // ----------------------------------------------------------------- // 1. the domain // ----------------------------------------------------------------- val N:ℕ; axiom notZero ⇔ N ≥ 1; type Num = ℕ[N]; val zero:Num = 0; fun checkZero(x:Num):Num = if x = zero then 1 else 0; pred isZero(x:Num) ⇔ checkZero(x) = 1; theorem twoNums ⇔ (∃x:Num.isZero(x)) ∧ (∃y:Num.¬isZero(y)); theorem zeroMin ⇔ ∀x:Num. isZero(x) ⇒ ∀y:Num. x ≤ y; theorem zeroMin0(x:Num) ⇔ isZero(x) ⇒ ∀y:Num. x ≤ y; theorem zeroMin1(x:Num) ⇔ isZero(x) ⇒ ∀y:Num. x < y; // ----------------------------------------------------------------- // 2. the theory // ----------------------------------------------------------------- // the predicate "m divides n" pred divides0(m:Num,n:Num) ⇔ ∃p:Num. m⋅p = n; pred divides1(m:Num,n:Num) ⇔ n = 0 ∨ (m ≠ 0 ∧ n%m = 0); theorem dividesEquiv(m:Num,n:Num) ⇔ divides0(m,n) ⇔ divides1(m,n); pred divides(m:Num,n:Num) ⇔ divides1(m,n); // the predicate "g is a gcd of m and n" pred isgcd1(g:Num,m:Num,n:Num) ⇔ // g is a common divisor of m and n divides(g,m) ∧ divides(g,n) ∧ // g is the greatest such value ∀g0:Num. divides(g0,m) ∧ divides(g0,n) ⇒ g0 ≤ g; // the same predicate defined with the help of the "max" quantifier pred isgcd2(g:Num,m:Num,n:Num) ⇔ g = max g:Num with divides(g,m) ∧ divides(g, n). g; // both definitions are equivalent theorem gcdEquiv(m:Num,n:Num) ⇔ ∀g:Num. isgcd1(g,m,n) ⇔ isgcd2(g,m,n); // therefore we may choose any definition pred isgcd(g:Num,m:Num,n:Num) ⇔ isgcd1(g,m,n); // when do m and n have a gcd? pred hasgcd(m:Num,n:Num) ⇔ m ≠ 0 ∨ n ≠ 0; // validation of problem specification theorem someInput() ⇔ ∃m:Num,n:Num. hasgcd(m,n); theorem notEveryInput() ⇔ ∃m:Num,n:Num. ¬hasgcd(m,n); theorem someOutput(m:Num,n:Num) ⇔ hasgcd(m,n) ⇒ ∃g:Num. isgcd(g,m,n); theorem notEveryOutput(m:Num,n:Num) ⇔ hasgcd(m,n) ⇒ ∃g:Num. ¬isgcd(g,m,n); theorem notTrivial() ⇔ ∃m:Num,n:Num. hasgcd(m,n) ∧ ∃g:Num. ¬isgcd(g,m,n); theorem uniqueOutput(m:Num,n:Num) ⇔ hasgcd(m,n) ⇒ ∀g1:Num,g2:Num. isgcd(g1,m,n) ∧ isgcd(g2,m,n) ⇒ g1 = g2; // the implicitly defined mathematical function whose result g is // a gcd of m and n (is there always one and only one such g?) fun gcd(m:Num,n:Num): Num requires hasgcd(m,n); ensures isgcd(result,m,n); = choose g:Num with isgcd(g,m,n); // ----------------------------------------------------------------- // 3. the knowledge derived from the theory // ----------------------------------------------------------------- // the core knowledge on which Euclid's algorithm is based // are these theorems valid (i.e., true for all m and n)? theorem gcd0(m:Num,n:Num) ⇔ m = 0 ∧ n ≠ 0 ⇒ gcd(m,n) = n; theorem gcd1(m:Num,n:Num) ⇔ m ≠ 0 ∧ n = 0 ⇒ gcd(m,n) = m; theorem gcd2(m:Num,n:Num) ⇔ n ≠ 0 ∧ n ≤ m ⇒ gcd(m,n) = gcd(m%n,n); theorem gcd3(m:Num,n:Num) ⇔ m ≠ 0 ∧ m ≤ n ⇒ gcd(m,n) = gcd(m,n%m); theorem gcd4(m:Num,n:Num) ⇔ n ≠ 0 ⇒ gcd(m,n) = gcd(n,m%n); // ----------------------------------------------------------------- // 4. the algorithm derived from the knowledge // ----------------------------------------------------------------- // Euclid's algorithm as a recursive function fun gcdf(m:Num,n:Num): Num requires hasgcd(m,n); // m ≠ 0 ∨ n ≠ 0 ensures isgcd(result,m,n); decreases m+n; = if m = 0 then n else if n = 0 then m else if n ≤ m then gcdf(m%n,n) else gcdf(m,n%m); // simplified version fun gcdf0(m:Num,n:Num): Num requires hasgcd(m,n); // m ≠ 0 ∨ n ≠ 0 ensures isgcd(result,m,n); decreases m+n,n; = if n = 0 then m else gcdf0(n,m%n); // Euclid's algorithm as an iterative procedure proc gcdp(m:Num,n:Num): Num requires hasgcd(m,n); // m ≠ 0 ∨ n ≠ 0 ensures isgcd(result,m,n); { var a:Num ≔ m; var b:Num ≔ n; while a ≠ 0 ∧ b ≠ 0 do invariant hasgcd(a,b); // a ≠ 0 ∨ b ≠ 0; invariant ∀r:Num. isgcd(r,a,b) ⇔ isgcd(r,m,n); decreases a+b; { if b ≤ a then a ≔ a%b; else b ≔ b%a; } return if a = 0 then b else a; } // simplified version proc gcdp0(m:Num,n:Num): Num requires hasgcd(m,n); ensures isgcd(result,m,n); { var a:Num ≔ m; var b:Num ≔ n; while b ≠ 0 do invariant hasgcd(a,b); // a ≠ 0 ∨ b ≠ 0; invariant ∀r:Num. isgcd(r,a,b) ⇔ isgcd(r,m,n); decreases a+b,b; { val c = a%b; a ≔ b; b ≔ c; } return a; } // Euclid's algorith as a transition system pred initial(a:Num,b:Num) ⇔ hasgcd(a,b); pred next(a:Num,b:Num,a0:Num,b0:Num) ⇔ (a ≥ b ∧ b > 0 ∧ a0 = a%b ∧ b0 = b) ∨ (b ≥ a ∧ a > 0 ∧ a0 = a ∧ b0 = b%a); proc gcds(m:Num,n:Num): Num requires initial(m,n); ensures isgcd(result,m,n); { var a:Num = m; var b:Num = n; var t:Array[N,Tuple[Num,Num]] = Array[N,Tuple[Num,Num]](⟨a,b⟩); var i:Num = 1; while a > 0 ∧ b > 0 do { choose a0:Num,b0:Num with next(a,b,a0,b0); a ≔ a0; b ≔ b0; t[i] ≔ ⟨a,b⟩; i ≔ i+1; } print t,i; return if a = 0 then b else a; } pred next0(a:Num,b:Num,a0:Num,b0:Num) ⇔ a0 = b ∧ b0 = a%b; proc gcds0(m:Num,n:Num): Num requires initial(m,n); ensures isgcd(result,m,n); { var a:Num = m; var b:Num = n; while b > 0 do { choose a0:Num,b0:Num with next0(a,b,a0,b0); a ≔ a0; b ≔ b0; } return a; } shared system GCD { var a:Num; var b:Num; invariant hasgcd(a,b); invariant gcd(a,b) = gcd(old_a,old_b); init(m:Num,n:Num) with hasgcd(m,n); ⇔ a0 = m ∧ b0 = n; action next1() ⇔ a ≥ b ∧ b > 0 ∧ a0 = a%b ∧ b0 = b; action next2() ⇔ b ≥ a ∧ a > 0 ∧ a0 = a ∧ b0 = b%a; } shared system GCD0 { var a:Num; var b:Num; var t:Array[N,Tuple[Num,Num]] = Array[N,Tuple[Num,Num]](⟨0,0⟩); var i:Num = 0; invariant hasgcd(a,b); invariant gcd(a,b) = gcd(old_a,old_b); init(m:Num,n:Num) with hasgcd(m,n); { a ≔ m; b ≔ n; t[i] = ⟨a,b⟩; i ≔ i+1; } action next1() with a ≥ b ∧ b > 0; { a ≔ a%b; t[i] = ⟨a,b⟩; i ≔ i+1; } action next2() with b ≥ a ∧ a > 0; { b ≔ b%a; t[i] = ⟨a,b⟩; i ≔ i+1; } action result() with a = 0 ∨ b = 0; { print (if a = 0 then b else a),t,i; } } // ----------------------------------------------------------------- // end of file // -----------------------------------------------------------------