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

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

Post on 13-Dec-2015

217 views

Category:

Documents

5 download

Embed Size (px)

TRANSCRIPT

<ul><li>Slide 1</li></ul> <p>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 =&gt; 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] =&gt; [1, 3, 5, 7, 9] ints = [1, 2..] take 10 ints =&gt; [1,2,3,4,5,6,7,8,9,10] squares = map (^2) ints take 3 squares =&gt; [1, 3, 9] numsFrom n = n : numsFrom (n+1) ones = 1 : ones"&gt; 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 -&gt; Mayb"&gt; 18 Error Conditions Often handled using the Maybe type defined in the Standard Prelude data Maybe a = Just a | Nothing doQuery :: Query -&gt; DB -&gt; Maybe Record r :: Maybe Record r = case doQuery db q1 of Nothing -&gt; Nothing Just r1 -&gt; case do Query db (q2 r1) of Nothing -&gt; Nothing Just r2 -&gt; case doQuery db (q3 r2) of Nothing -&gt; Nothing Just r3 -&gt;... Which can get quite tedious after a while There has to be a better way Slide 19 19 And there is... thenMB :: Maybe a -&gt; (a -&gt; Maybe b) -&gt; Maybe b mB `thenMB` f = case mB of Nothing -&gt; Nothing Just a -&gt; f a returnMB a = Just a r = doQuery q1 db `thenMB` \r1 -&gt; doQuery (q2 r1) db `thenMB` \r2 -&gt; doQuery (q3 r2) db `thenMB` \r3 -&gt; 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 -&gt; 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 &gt;&gt;= :: m a -&gt; (a -&gt; m b) -&gt; m b return :: a -&gt; m a So for our Database example we will have something like instance Monad Maybe where (&gt;&gt;=) = thenMB return = returnMB doQuery q1 db &gt;&gt;= \r1 -&gt; doQuery (q2 r1) db &gt;&gt;= \r2 -&gt; doQuery (q3 r2) db &gt;&gt;= \r3 -&gt; 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 -&gt; SomeState -&gt; (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 -&gt; (a, s) parm -&gt; StateT SomeState res Slide 22 22 State Example addRec :: Record -&gt; DB -&gt; (Bool,DB) addRec :: Record -&gt; 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 &amp;&amp; bool2 &amp;&amp; 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 -&gt; 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 -&gt; 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 </p>