Definition of prime number and two theorems of Euclid
Function:
(defun divides (x y) (declare (xargs :guard t)) (and (acl2-numberp x) (not (= x 0)) (acl2-numberp y) (integerp (/ y x))))
Theorem:
(defthm divides-leq (implies (and (> x 0) (> y 0) (divides x y)) (<= x y)) :rule-classes nil)
Theorem:
(defthm divides-minus (implies (divides x y) (divides x (- y))) :rule-classes nil)
Theorem:
(defthm divides-sum (implies (and (divides x y) (divides x z)) (divides x (+ y z))) :rule-classes nil)
Theorem:
(defthm divides-product (implies (and (integerp z) (divides x y)) (divides x (* y z))) :rule-classes nil)
Theorem:
(defthm divides-transitive (implies (and (divides x y) (divides y z)) (divides x z)) :rule-classes nil)
Theorem:
(defthm divides-self (implies (and (acl2-numberp x) (not (= x 0))) (divides x x)))
Theorem:
(defthm divides-0 (implies (and (acl2-numberp x) (not (= x 0))) (divides x 0)))
Theorem:
(defthm divides-mod-equal (implies (and (real/rationalp a) (real/rationalp b) (real/rationalp n) (not (= n 0))) (iff (divides n (- a b)) (= (mod a n) (mod b n)))) :rule-classes nil)
Theorem:
(defthm divides-mod-0 (implies (and (acl2-numberp a) (acl2-numberp n) (not (= n 0))) (iff (divides n a) (= (mod a n) 0))) :rule-classes nil)Our definition of primality is based on the following function, which computes the least divisor of a natural number
Function:
(defun least-divisor (k n) (declare (xargs :guard t)) (if (and (integerp n) (integerp k) (< 1 k) (<= k n)) (if (divides k n) k (least-divisor (1+ k) n)) nil))
Theorem:
(defthm least-divisor-divides (implies (and (integerp n) (integerp k) (< 1 k) (<= k n)) (and (integerp (least-divisor k n)) (divides (least-divisor k n) n) (<= k (least-divisor k n)) (<= (least-divisor k n) n))) :rule-classes nil)
Theorem:
(defthm least-divisor-is-least (implies (and (integerp n) (integerp k) (< 1 k) (<= k n) (integerp d) (divides d n) (<= k d)) (<= (least-divisor k n) d)) :rule-classes nil)
Function:
(defun primep (n) (declare (xargs :guard t)) (and (integerp n) (>= n 2) (equal (least-divisor 2 n) n)))
Theorem:
(defthm primep-gt-1 (implies (primep p) (and (integerp p) (>= p 2))) :rule-classes :forward-chaining)
Theorem:
(defthm primep-no-divisor (implies (and (primep p) (integerp d) (divides d p) (> d 1)) (= d p)) :rule-classes nil)
Theorem:
(defthm primep-least-divisor (implies (and (integerp n) (>= n 2)) (primep (least-divisor 2 n))) :rule-classes nil)Our formulation of the infinitude of the set of primes is based on a function that returns a prime that is greater than its argument:
Function:
(defun fact (n) (declare (xargs :guard (natp n))) (if (zp n) 1 (* n (fact (1- n)))))
Function:
(defun greater-prime (n) (declare (xargs :guard (natp n))) (least-prime-divisor (1+ (fact n))))
Theorem:
(defthm greater-prime-divides (divides (greater-prime n) (1+ (fact n))) :rule-classes nil)
Theorem:
(defthm divides-fact (implies (and (integerp n) (integerp k) (<= 1 k) (<= k n)) (divides k (fact n))))
Theorem:
(defthm not-divides-fact-plus1 (implies (and (integerp n) (integerp k) (< 1 k) (<= k n)) (not (divides k (1+ (fact n))))) :rule-classes nil)
Theorem:
(defthm infinitude-of-primes (implies (integerp n) (and (primep (greater-prime n)) (> (greater-prime n) n))) :rule-classes nil)Our main theorem of Euclid depends on the properties of the greatest common divisor, which we define according to Euclid's algorithm.
Function:
(defun gcd-nat (x y) (declare (xargs :guard (and (natp x) (natp y)))) (if (zp x) y (if (zp y) x (if (<= x y) (gcd-nat x (- y x)) (gcd-nat (- x y) y)))))
Function:
(defun gcd (x y) (declare (xargs :guard (and (integerp x) (integerp y)))) (gcd-nat (abs x) (abs y)))
Theorem:
(defthm gcd-nat-pos (implies (and (natp x) (natp y) (not (and (= x 0) (= y 0)))) (> (gcd-nat x y) 0)) :rule-classes nil)
Theorem:
(defthm gcd-pos (implies (and (integerp x) (integerp y) (not (and (= x 0) (= y 0)))) (and (integerp (gcd x y)) (> (gcd x y) 0))) :rule-classes nil)
Theorem:
(defthm divides-gcd-nat (implies (and (natp x) (natp y)) (and (or (= x 0) (divides (gcd-nat x y) x)) (or (= y 0) (divides (gcd-nat x y) y)))) :rule-classes nil)
Theorem:
(defthm gcd-divides (implies (and (integerp x) (integerp y)) (and (or (= x 0) (divides (gcd x y) x)) (or (= y 0) (divides (gcd x y) y)))) :rule-classes nil)It remains to be shown that the gcd of
r*x + s*y. We construct the coefficients
Function:
(defun r-nat (x y) (declare (xargs :guard (and (natp x) (natp y)))) (if (zp x) 0 (if (zp y) 1 (if (<= x y) (- (r-nat x (- y x)) (s-nat x (- y x))) (r-nat (- x y) y)))))
Function:
(defun s-nat (x y) (declare (xargs :guard (and (natp x) (natp y)))) (if (zp x) 1 (if (zp y) 0 (if (<= x y) (s-nat x (- y x)) (- (s-nat (- x y) y) (r-nat (- x y) y))))))
Theorem:
(defthm r-s-nat (implies (and (natp x) (natp y)) (= (+ (* (r-nat x y) x) (* (s-nat x y) y)) (gcd-nat x y))) :rule-classes nil)
Function:
(defun r (x y) (declare (xargs :guard (and (integerp x) (integerp y)))) (if (< x 0) (- (r-nat (abs x) (abs y))) (r-nat (abs x) (abs y))))
Function:
(defun s (x y) (declare (xargs :guard (and (integerp x) (integerp y)))) (if (< y 0) (- (s-nat (abs x) (abs y))) (s-nat (abs x) (abs y))))
Theorem:
(defthm gcd-linear-combination (implies (and (integerp x) (integerp y)) (= (+ (* (r x y) x) (* (s x y) y)) (gcd x y))) :rule-classes nil)
Theorem:
(defthm divides-gcd (implies (and (integerp x) (integerp y) (integerp d) (not (= d 0)) (divides d x) (divides d y)) (divides d (gcd x y))))
Theorem:
(defthm gcd-prime (implies (and (primep p) (integerp a)) (equal (gcd p a) (if (divides p a) p 1))))The main theorem:
Theorem:
(defthm euclid (implies (and (primep p) (integerp a) (integerp b) (not (divides p a)) (not (divides p b))) (not (divides p (* a b)))) :rule-classes nil)