1 haskell kevin atkinson john simmons. 2 contents introduction type system variables functions...

Download 1 Haskell Kevin Atkinson John Simmons. 2 Contents Introduction Type System Variables Functions Module System I/O Conclusions

Post on 13-Dec-2015




5 download

Embed Size (px)


  • Slide 1

1 Haskell Kevin Atkinson John Simmons Slide 2 2 Contents Introduction Type System Variables Functions Module System I/O Conclusions Slide 3 3 History of Haskell Named for Haskell Curry Created by committee from FPCA conference, 1987 Needed a standard non-strict, purely functional language Latest version: Haskell 98 Slide 4 4 Goals of the Haskell design Suitability for teaching, research, and applications Complete description via the publication of a formal syntax and semantics Free availability Basis of ideas that enjoy a wide consensus Reduction of unnecessary diversity in functional programming languages Slide 5 5 Interpreters and Compilers GHC nhc98 HBC / HBI Hugs www.haskell.org/implementations.html Slide 6 6 Distinguishing features Lazy evaluation Stateless No exception system - errors are considered equivalent to _|_ Slide 7 7 Variants and extensions Parallel variants Object support Pattern guards Views Slide 8 8 The Haskell type system value :: Type Type inference Pre-defined types Integer, Real, String, etc. 9 Keywords type and data Enumerated types, renamed types, tuples (record types), lists Type classes ex. Equality types Inheritance ex. Equality => Ordered Kinds of types - verify type constructors * means a value type Slide 10 10 Thunks and ! Lazy evaluation: the computation needed to compute a field value is stored until needed in a thunk ! Will force immediate evaluation Can help constrain types Use with caution Slide 11 11 Variables No keywords to define Lists (constructor is : ) Arrays must open the Array module Infinite data structures Slide 12 12 Basic Stuff Due to layout rules Haskell syntax is rather elegant and generally easy to understand Functions similar to ML Pattern and Wildcards like ML but also more powerful Pattern Gards Case clause to pattern match inside of functions Let like ML to define bindings but also where as part of function and case clause syntax to declare binding after the expression Slide 13[1,2,3,4,5,6,7,8,9,10] [1,3.. 10] => [1, 3, 5, 7, 9] ints = [1, 2..] take 10 ints => [1,2,3,4,5,6,7,8,9,10] squares = map (^2) ints take 3 squares => [1, 3, 9] numsFrom n = n : numsFrom (n+1) ones = 1 : ones"> 13 More on Lists [ f x | x[1, 3, 9] numsFrom n = n : numsFrom (n+1) ones = 1 : ones Slide 14 Fibonacci fib = 1 : 1 : [ a+b | (a,b) DB -> Mayb"> 18 Error Conditions Often handled using the Maybe type defined in the Standard Prelude data Maybe a = Just a | Nothing doQuery :: Query -> DB -> Maybe Record r :: Maybe Record r = case doQuery db q1 of Nothing -> Nothing Just r1 -> case do Query db (q2 r1) of Nothing -> Nothing Just r2 -> case doQuery db (q3 r2) of Nothing -> Nothing Just r3 ->... Which can get quite tedious after a while There has to be a better way Slide 19 19 And there is... thenMB :: Maybe a -> (a -> Maybe b) -> Maybe b mB `thenMB` f = case mB of Nothing -> Nothing Just a -> f a returnMB a = Just a r = doQuery q1 db `thenMB` \r1 -> doQuery (q2 r1) db `thenMB` \r2 -> doQuery (q3 r2) db `thenMB` \r3 -> returnMB r3 And what we have is the mathematical notion of Monad which is formally defined as: return a `then` f === f a m `then` return === m m `then` (\a -> f a `then` h) === (m `then` f) `then` h Monads Slide 20 20 Haskell Monads In Haskell a Monad is a class defined something like: class Monad m where >>= :: m a -> (a -> m b) -> m b return :: a -> m a So for our Database example we will have something like instance Monad Maybe where (>>=) = thenMB return = returnMB doQuery q1 db >>= \r1 -> doQuery (q2 r1) db >>= \r2 -> doQuery (q3 r2) db >>= \r3 -> return r3 Which can be rewritten using Haskells do syntax do r1 21 Simulating State To simulate state when there is none simply pass in the state and return a copy of the new state. parm -> SomeState -> (res, SomeState) Functions that do so are known as State Transformers. We can model this notation in Haskell using a type synonym type StateT s a = s -> (a, s) parm -> StateT SomeState res Slide 22 22 State Example addRec :: Record -> DB -> (Bool,DB) addRec :: Record -> StateT DB Bool newDB :: StateT DB Bool newDB db = let (bool1,db1) = addRec rec1 db (bool2,db2) = addRec rec2 db1 (bool3,db3) = delRec rec3 db2 in (bool1 && bool2 && bool3) As you can see this could get quite tedious However, with Monads we can abstract the state passing details away. The details or a bit complicated, but the end result will look something like this: do boo11 23 I / O To Perform I/O A State Transformer Must be used, for example to get a character: getChar :: FileHandle -> StateT World Char But we must make sure each World is only passed to one function so a Monad is used But this is open to abuse so an ADT must be used: data IO a = IO (StateT World a) So putChar is now: getChar :: FileHandle -> IO Char The end result is that any function which uses I/O must return the IO type. And the only way IO can be executed is by declaring the function main :: IO (). Slide 24 24 Finally A Complete Program import System (getArgs) main :: IO () main = do args do fstr putStrLn "usage: wc fname" Slide 25 25 Hudak, P. et al (2000) A Gentle Introduction to Haskell Version 98 http://www.haskell.org/tutorial Jones, S.P. et al (1999) The Haskell 98 Report http://www.haskell.org/onlinereport Winstanley, Noel (1999) What the Hell are Monads? http://www.dcs.gla.ac.uk/~nww/Monad.html References