# Induction on `nat`

What about inputs that are not integers?

``type nat = Zero | Succ of nat``
``````let rec to_int (n:nat) = match n with
| Zero -> 0
| Succ m -> 1 + (to_int m)``````
``````let rec plus_nat m n = match m with
| Zero -> n
| Succ m’ -> Succ (plus_nat m’ n)``````
``````let rec mul_nat m n = match m with
| Zero -> Zero
| Succ m’ -> plus_nat n (mul_nat m’ n)``````

## Generalizing Induction

Principle of induction for nat:

For all `x : nat`, `P(x)` if

• `P(Zero)` ( Base Case ) and
• `∀x, P(x) ⇒ P(Succ x)` ( Inductive Case )

E.g. Let’s prove that for all `n1 : nat`, `P(n1)`, where

`P(n1)` = ∀ `n2 : nat`, `to_int (plus_nat n1 n2) ≡ (to_int n1) + (to_int n2)`

Base Case: `n1 = Zero`.

E.g. Let’s prove that for all `n1 : nat`, `P(n1)`, where

`P(n1)` = ∀ `n2 : nat`, `to_int (plus_nat n1 n2) ≡ (to_int n1) + (to_int n2)`

Base Case: `n1 = Zero`.

Inductive Case:

``````to_int (plus_nat (Succ n) n2) ≡
to_int (Succ (plus_nat n n2)) ≡
1 + (to_int (plus_nat n n2)) ≡
1 + (to_int n) + (to_int n2) ≡
(to_int (Succ n)) + (to_int n2)``````

# `mul_nat`

``∀ n1 : nat, ∀n2 : nat, to_int (mul_nat n1 n2) ≡ (to_int n1) * (to_int n2)``

Base Case `n1 = Zero`:

``∀n2, to_int (mul_nat Zero n2) ≡ to_int Zero ≡ 0 ≡ (to_int Zero) * (to_int n2)``

Inductive Case:

``````to_int (mul_nat (Succ n) n2) ≡
to_int (plus_nat n2 (mul_nat n n2)) ≡
(to_int n2) + (to_int (mul_nat n n2)) ≡
(to_int n2) + (to_int n) * (to_int n2) ≡
(1 + (to_int n)) * (to_int n2) ≡
(to_int (Succ n)) * (to_int n2)``````

# Generalized Induction

For any inductive type of the form:

``````type t = C₀ of b (* b is some other type not referring to t *)
| C₁ of b₁*t
| C₂ of b₂ * t * t``````

The principle of induction for type `t` is:
For all `x : t`, `P(x)` if:

• `v : b`, `P(C₀ v)`, and
• `x : t, v : b₁`, `P(x) ⇒ P(C₁(v,x))`, and
• `x1, x2 : t, v : b₂`, `P(x1)` and `P(x2)``P(C₂ (v,x1,x2))`

`nat`: ∀ `n : nat`, `P(n)` if `P(Zero)` and `∀ m`, `P(m) ⇒ P(Succ m)`

`'a list`: ∀ `ℓ : 'a list'`, `P(ℓ)` if `P([])` and `∀ x ∈ 'a`, `∀ ℓ : 'a list`, `P(ℓ) => P(x::ℓ)`

# `cs2041.org`

// reveal.js plugins