123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- {-
- Types Summer School 2007
- Bertinoro
- Aug 19 - 31, 2007
- Agda
- Ulf Norell
- -}
- -- This is where the fun begins.
- -- Unleashing datatypes, pattern matching and recursion.
- module Datatypes where
- {-
- Simple datatypes.
- -}
- -- Let's define natural numbers.
- data Nat : Set where
- zero : Nat
- suc : Nat -> Nat
- -- A simple function.
- pred : Nat -> Nat
- pred zero = zero
- pred (suc n) = n
- -- Now let's do recursion.
- _+_ : Nat -> Nat -> Nat
- zero + m = m
- suc n + m = suc (n + m)
- infixl 60 _+_
- -- An aside on infix operators:
- -- Any name containing _ can be used as a mixfix operator.
- -- The arguments simply go in place of the _. For instance:
- data Bool : Set where
- true : Bool
- false : Bool
- if_then_else_ : {A : Set} -> Bool -> A -> A -> A
- if true then x else y = x
- -- if false then x else y = y
- if_then_else_ false x y = y
- {-
- Parameterised datatypes
- -}
- data List (A : Set) : Set where
- [] : List A
- _::_ : A -> List A -> List A
- -- The parameters are implicit arguments to the constructors.
- nil : (A : Set) -> List A
- nil A = [] {A}
- map : {A B : Set} -> (A -> B) -> List A -> List B
- map f [] = []
- map f (x :: xs) = f x :: map f xs
- {-
- Empty datatypes
- -}
- -- A very useful guy is the empty datatype.
- data False : Set where
- -- When pattern matching on an element of an empty type, something
- -- interesting happens:
- elim-False : {A : Set} -> False -> A
- elim-False () -- Look Ma, no right hand side!
- -- The pattern () is called an absurd pattern and matches elements
- -- of an empty type.
- {-
- What's next?
- -}
- -- The Curry-Howard isomorphism.
- -- CurryHoward.agda
|