The goal of this lecture note is to formalize Euclid’s division lemma.
For any positive natural number d (the divisor) and for any natural number n (the dividend), there exists a natural number q (the quotient) and a natural number r (the remainder) such that r < d and n = q * d + r hold.
The proof is by induction on n:
Base case: (n = O)
q = 0 and r = 0 do the job since 0 < d and 0 = 0 * d + 0 hold.
Induction step: (n = S n')
Under the induction hypothesis that for a given n', there exist q' and r' such that r' < d and n' = q' * d + r' hold, we need to show that there exist q and r such that r < d and S n' = q * d + r hold.
Since r' < d, either r' < d - 1 (r' is way smaller than d) or r' = d - 1 (r' just precedes d):
QED.
Formalize the above proof in tCPA.
Hints:
(Concretely, r' < d implies S r' <= d which implies S r' < d \/ S r' = d which gives rise to the proof by cases.)
Here is an alternative statement of Euclid’s division lemma: for any natural number d and for any natural number n, there exists a natural number q and a natural number r such that r < S d and n = q * S d + r.
More precisely, Euclid’s division lemma states that q and r are unique. Formalize and prove this unicity, starting from this accompanying resource file that formalizes the uniqueness proof in Wikipedia:
Lemma uniqueness :
forall n d : nat,
forall q1 r1 : nat,
n = q1 * S d + r1 ->
r1 < S d ->
forall q2 r2 : nat,
n = q2 * S d + r2 ->
r2 < S d ->
q1 = q2 /\ r1 = r2.
(Apologies for spoiling the fun of stating this lemma, but it is critical to start off simply and correctly.)
Prove the following lemma as a corollary of (either version of) Euclid’s division lemma:
Lemma even_or_odd :
forall n : nat,
exists q : nat,
n = 2 * q \/ n = S (2 * q).
Implement at least one function quorem of type nat -> nat -> option (nat * nat) that, given a natural number n (the dividend) and a positive natural number d (the divisor), computes a pair of natural numbers (q, r) where q is the quotient and r is the remainder of dividing n by d. (The option type accounts for division by zero.)
At least one of your implementations should be using structural recursion over n the way the proof above is using structural induction over the dividend.