# Using Types Using Types Slides thanks to Mark Jones Expressions Have Types: The type of an expression tells you what kind of value you might expect to see if you evaluate that expression In Haskell, read :: as has type Examples: 1 :: Int, 'a' :: Char, True :: Bool, 1.2 :: Float,

You can even ask GHCI for the type of an expression: :t expr 2 Type Errors: Prelude> 'a' && True :26:1: Couldn't match expected type `Bool' with actual type `Char' In the first argument of `(&&)', namely 'a' In the expression: 'a' && True In an equation for `it': it = 'a' && True

Prelude> odd 1 + 2 :29:7: No instance for (Num Bool) arising from a use of `+' Possible fix: add an instance declaration for (Num Bool) In the expression: odd 1 + 2 In an equation for `it': it = odd 1 + 2 3 Pairs:

A pair packages two values into one (1, 2) ('a', 'z') (True, False) Components can have different types (1, 'z') ('a', False) (True, 2) The type of a pair whose first component is of type

A and second component is of type B is written (A,B) What are the types of the pairs above? 4 Operating on Pairs: There are built-in functions for extracting the first and second component of a pair: fst (True, 2) = True snd (0, 7) = 7 Is the following property true?

For any pair p, (fst p, snd p) = p 5 Lists: Lists can be used to store zero or more elements, in sequence, in a single value: [] [1, 2, 3] ['a', 'z']

[True, True, False] All of the elements in a list must have the same type The type of a list whose elements are of type A is written as [A] What are the types of the lists above? 6 Overloading Some expressions can have more than one

type Examples 23 [] fx=x<3 f x = show x ++ is the answer One way to get these is overloading Three important causes of overloading Numbers Num