

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


data Nat = Zero | Succ Nat
There is recursion in the datatype equation. We are defining the datatype Nat
Main> :t Zero Zero :: Nat Main> :t Succ Zero Succ Zero :: Nat Main>
To apply the constructor Succ - we must have a previously constructed Nat to apply to it. What objects have type Nat ?? N at = {Zero, SuccZero, Succ(SuccZero), · · · } This representation goes back to Peano, an Italian logician and mathemati- cian. In Haskell, we have the most refined form of equality.
instance Eq Nat where Zero == Zero = True Zero == Succ n = False Succ n == Zero = False Succ n == Succ m = m == n
Main> :t (==) (==) :: Eq a => a -> a -> Bool
By default, we also get not equal to
:t (/=) (/=) :: Eq a => a -> a -> Bool Main>
If we had said data Nat = Zero |Succ Nat deriving Eq Haskell would have derived an equivalent == function to one we have writ- ten. Succ Zero == Succ (Succ Zero) ; Zero == Succ Zero ; False
Nat terms have a tree structure. We can define our own +++ as : (+++) :: N at → N at → N at m +++ zero = m m +++ (Succ n) = Succ (n +++ m)
Theorem 1. ∀m : N at.m +++ (succ 0) = succ m
Proof. Choose an arbitrary m and show m +++ (succ 0) = succ m. Starting with LHS
m +++ (succ 0) <<defn of +++>> = succ (m +++ zero) = succ m
Similarly, we can check if zero is same as our notion of 0 in mathematics
Theorem 2. ∀m : N at.zero +++ m = m
Proof by induction on m..
Consider again inductively defined data type. data Nat = Zero | Succ Nat deriving Eq The induction principle for Nat. (as is in the book)
Case (Zero): P (Zero) holds Case (succ n): Assuming P (n) holds, show P (succ n) holds.
What is P ?? P :: N at → Bool - P is a property of natural numbers. Another way of thinking this is :
(P (0) ∧ ∀m : N at.P (m) ⇒ P (m + 1)) ⇒ ∀m : N at.P (m)
Theorem 3. ∀m, n : N at.n +++ m = m +++ n