natBase Case n1 = Zero:
Inductive Case:
to_int (mul_nat (Succ n) n2)
≡ to_int (plus_nat n2 (mul_nat n n2)) (* eval mul_nat *)
≡ (to_int n2) + (to_int (mul_nat n n2)) (* plus_nat theorem *)
≡ (to_int n2) + (to_int n) * (to_int n2) (* IH *)
≡ (1 + (to_int n)) * (to_int n2) (* distributive property *)
≡ (to_int (Succ n)) * (to_int n2) (* reverse eval to_int *)For any inductive type of the form:
The principle of induction for type t is:
For all x : t, P(x) if:
v : b, P(C₀ v), andx : t, v : b₁, P(x) ⇒ P(C₁(v,x))Examples:
nat: ∀ n : nat, P(n) if P(Zero) and ∀ m, P(m) ⇒ P(Succ m)'a list: ∀ ℓ : 'a list, P(ℓ) ifP([]) and ∀ x : 'a, ∀ ℓ : 'a list, P(ℓ) ⇒ P(x::ℓ)Claim: ∀ ℓ : int list, (sum ℓ) ≡ ∑i ℓi
Base Case: (sum []) ≡ 0, ∑i []i = 0.
Inductive case: let ℓ' = x::ℓ, then
(sum x::ℓ) ≡ x + (sum ℓ)
≡ x + ∑i ℓi
≡ ∑i ℓ’i.
To avoid doubt about “properties of lists” we can state properties using code:
let rec append l1 l2 = match l1 with
| [] -> l2
| h::t -> h::(append t l2)
let rec sum = function [] -> 0
| h::t -> h + (sum t)Claim: ∀ℓ₁ : int list:
∀ℓ₂ : int list,
sum (append ℓ₁ ℓ₂) ≡ (sum ℓ₁) + (sum ℓ₂)Base Case: ℓ₁ = [].
cs2041.org