Presentation is loading. Please wait.

Presentation is loading. Please wait.

0 PROGRAMMING IN HASKELL Chapter 3 - Types and Classes.

Similar presentations


Presentation on theme: "0 PROGRAMMING IN HASKELL Chapter 3 - Types and Classes."— Presentation transcript:

1 0 PROGRAMMING IN HASKELL Chapter 3 - Types and Classes

2 1 What is a Type? A type is a name for a collection of related values. For example, in Haskell the basic type TrueFalse Bool contains the two logical values:

3 2 Type Errors Applying a function to one or more arguments of the wrong type is called a type error. > 1 + False Error 1 is a number and False is a logical value, but + requires two numbers.

4 3 Types in Haskell zIf evaluating an expression e would produce a value of type t, then e has type t, written e :: t zEvery well formed expression has a type, which can be automatically calculated at compile time using a process called type inference.

5 4 zAll type errors are found at compile time, which makes programs safer and faster by removing the need for type checks at run time. zIn Hugs, the :type command calculates the type of an expression, without evaluating it: > not False True > :type not False not False :: Bool

6 5 Basic Types Haskell has a number of basic types, including: Bool - logical values Char - single characters Integer - arbitrary-precision integers Float - floating-point numbers String - strings of characters Int - fixed-precision integers Like primitive types in Java.

7 6 List Types [False,True,False] :: [Bool] [’a’,’b’,’c’,’d’] :: [Char] In general: A list is sequence of values of the same type: [t] is the type of lists with elements of type t. Lists are built into Haskell. They are like Java generics. The elements must all be the same type. E.g. [Bool] is not a list with one element. It is the type of a list of Bools.

8 7 zThe type of a list says nothing about its length. [False,True] :: [Bool] [False,True,False] :: [Bool] [[ ' a ' ],[ ' b ', ' c ' ]] :: [[Char]] Note: zThe type of the elements is unrestricted. For example, we can have lists of lists: -- Try it. > :t [False, True] [False,True] :: [Bool] > :t [False,True,False] [False,True,False] :: [Bool] > :t [['a'],['b', 'c']] [['a'],['b','c']] :: [[Char]] [[Char]] is the type of a list whose elements are of type [Char], i.e., whose elements are lists of Chars.

9 8 Tuple Types A tuple is a sequence of values of different types: (False,True) :: (Bool,Bool) (False, ' a ',True) :: (Bool,Char,Bool) In general: (t1,t2,…,tn) is the type of n-tuples whose ith components have type ti for any i in 1…n. This is also built into Haskell. A tuple is like the instance variables of an object—or a struct in C++. But the positions do not have names.

10 9 zThe type of a tuple encodes its size: (False,True) :: (Bool,Bool) (False,True,False) :: (Bool,Bool,Bool) ( ' a ',(False, ' b ' )) :: (Char,(Bool,Char)) (True,[ ' a ', ' b ' ]) :: (Bool,[Char]) Note: zThe type of the components is unrestricted: --Try some of these. > :t (True,['a', 'b']) (True,['a','b']) :: (Bool,[Char])

11 10 Function Types not :: Bool  Bool concat:: [[a]]  [a] In general: A function is a mapping from values of one type to values of another type: t1  t2 is the type of functions that map values of type t1 to values to type t2. --Try some of these. > :t not not :: Bool -> Bool > :t concat concat :: [[a]] -> [a] Do you understand the last one? In it a is a type variable. It can be any type.

12 11  The arrow  is typed at the keyboard as ->. zThe argument and result types are unrestricted. For example, functions with multiple arguments or results are possible using lists or tuples: Note: add :: (Int,Int)  Int add (x,y) = x+y zeroto :: Int  [Int] zeroto n = [0..n] {- Put these two lines in a file. Load it. Ask for the type of add. -} > :t add add :: (Int,Int) -> Int {- Comment out the type declaration and reload. -} > :t add add :: Num a => (a,a) -> a

13 12 Functions with multiple arguments are also possible by returning functions as results: add ' :: Int  (Int  Int) add ' x y = x+y Curried Functions add’ takes an integer x and returns a function add'x. add'x takes an integer y and returns the result x+y. This is the first strange (and important!) Haskell feature. It’s important to understand it. When executed, add' takes the first argument, x, and on the fly constructs a function of one argument, add'x, that adds x to whatever the second argument is. In add'x, x is a constant. add' 4 5 would construct add'4, the function that adds 4 to its argument. All Haskell functions have one argument.

14 13 zadd and add’ produce the same final result, but add takes its two arguments at the same time, whereas add’ takes them one at a time: Note: zFunctions that take their arguments one at a time are called curried functions, celebrating the work of Haskell Curry on such functions. add :: (Int,Int)  Int add’ :: Int  (Int  Int) -- Try it. > (+) 3 4 7 > :t (+) (+) :: Num a => a -> a -> a > :t (+)3 (3 +) :: Num a => a -> a > f 4 where f = (+) 3 7 {- Or type g = (+) 3 into your file and reload. They try: -} > :t g > g 4

15 14 zFunctions with more than two arguments can be curried by returning nested functions: mult :: Int  (Int  (Int  Int)) mult x y z = x*y*z mult takes an integer x and returns a function mult x, which in turn takes an integer y and returns a function mult x y, which finally takes an integer z and returns the result x*y*z.

16 15 Why is Currying Useful? Curried functions are more flexible than functions on tuples, because useful functions can often be made by partially applying a curried function. For example: add’ 1 :: Int  Int take 5 :: [Int]  [Int] drop 5 :: [Int]  [Int]

17 16 Currying Conventions  The arrow  associates to the right. Int  Int  Int  Int To avoid excess parentheses when using curried functions, two simple conventions are adopted: Means Int  (Int  (Int  Int)).

18 17 zAs a consequence, it is then natural for function application to associate to the left. mult x y z Means ((mult x) y) z. Unless tupling is explicitly required, all functions in Haskell are normally defined in curried form.

19 18 Polymorphic Functions A function is called polymorphic (“of many forms”) if its type contains one or more type variables. length :: [a]  Int for any type a, length takes a list of values of type a and returns an integer.

20 19 zType variables can be instantiated to different types in different circumstances: Note: zType variables must begin with a lower-case letter, and are usually named a, b, c, etc. > length [False,True] 2 > length [1,2,3,4] 4 a = Bool a = Int

21 20 zMany of the functions defined in the standard prelude are polymorphic. For example: fst :: (a,b)  a head :: [a]  a take :: Int  [a]  [a] zip :: [a]  [b]  [(a,b)] id :: a  a Polymorphic functions don’t care what the type is. They operate the same way in all cases.

22 21 Overloaded Functions A polymorphic function is called overloaded if its type contains one or more class constraints. sum :: Num a  [a]  a for any numeric type a, sum takes a list of values of type a and returns a value of type a.

23 22 zConstrained type variables can be instantiated to any types that satisfy the constraints: Note: > sum [1,2,3] 6 > sum [1.1,2.2,3.3] 6.6 > sum [’a’,’b’,’c’] ERROR Char is not a numeric type a = Int a = Float Overloaded functions operate differently depending on the type. E.g., floating arithmetic or integer arithmetic.

24 23 Num - Numeric types Eq - Equality types Ord - Ordered types zHaskell has a number of type classes, including: zFor example: (+) :: Num a  a  a  a (==) :: Eq a  a  a  Bool (<) :: Ord a  a  a  Bool

25 24 Hints and Tips zWhen defining a new function in Haskell, it is useful to begin by writing down its type; zWithin a script, it is good practice to state the type of every new function defined; zWhen stating the types of polymorphic functions that use numbers, equality or orderings, take care to include the necessary class constraints. Normally I don’t bother writing down the types of functions I define. I let Haskell tell me. But Haskell is very picky about Num types.

26 25 Exercises [’a’,’b’,’c’] (’a’,’b’,’c’) [(False,’0’),(True,’1’)] ([False,True],[’0’,’1’]) [tail,init,reverse] What are the types of the following values?(1)

27 26 second xs = head (tail xs) swap (x,y) = (y,x) pair x y = (x,y) double x = x*2 palindrome xs = reverse xs == xs twice f x = f (f x) What are the types of the following functions?(2) Check your answers using Hugs.(3)


Download ppt "0 PROGRAMMING IN HASKELL Chapter 3 - Types and Classes."

Similar presentations


Ads by Google