Docsity

Inductive Data Type - Lecture Notes | COSC 3015, Study notes of Computer Science

Toggle sidebar
bg1
COSC 3015: Lecture 7
Lecture given by Prof. Caldwell and scribed by Sunil Kothari
September 16, 2008
1 Inductive Datatype
data Nat = Zero | Succ Nat
There is recursion in the datatype equation. We are defining the datatype Nat
- and in doing so, use the data type Nat.
Zero and Succ are constructors for the data type.
Zero::Nat - is a constant of the type nat.
Succ:: Nat →Nat - is a constant that maps Nats to Nats
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 ?? Nat ={Zero, S uccZero, S ucc(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
1
pf3

Partial preview of the text

Download Inductive Data Type - Lecture Notes | COSC 3015 and more Study notes Computer Science in PDF only on Docsity!

COSC 3015: Lecture 7

Lecture given by Prof. Caldwell and scribed by Sunil Kothari

September 16, 2008

1 Inductive Datatype

data Nat = Zero | Succ Nat

There is recursion in the datatype equation. We are defining the datatype Nat

  • and in doing so, use the data type Nat. Zero and Succ are constructors for the data type. Zero::Nat - is a constant of the type nat. Succ:: Nat → Nat - is a constant that maps Nats to Nats

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..

1.1 Induction Principle

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