A Lightning Tour of Haskell - PowerPoint PPT Presentation

1 / 80
About This Presentation
Title:

A Lightning Tour of Haskell

Description:

e.g. f (g x) Using Haskell: The Hugs Interpreter. A linked list. 1. 3. 5. 1 : 3 : 5 : ... Defining Functions. insert :: Ord a = a - [a] - [a] insert x ... – PowerPoint PPT presentation

Number of Views:76
Avg rating:3.0/5.0
Slides: 81
Provided by: johnh58
Category:

less

Transcript and Presenter's Notes

Title: A Lightning Tour of Haskell


1
A Lightning Tour of Haskell
  • Lecture 1,
  • Designing and Using Combinators
  • John Hughes

2
Using Haskell The Hugs Interpreter
3
Using Haskell The Hugs Interpreter
A module is loaded.
4
Using Haskell The Hugs Interpreter
A module is loaded.
Type an expression at the prompt.
5
Using Haskell The Hugs Interpreter
A module is loaded.
Type an expression at the prompt.
The value is printed.
6
Using Haskell The Hugs Interpreter
A function call with two arguments. No brackets!
Brackets are only for grouping e.g. f (g x)
7
Using Haskell The Hugs Interpreter
A linked list
1
3
5
1 3 5
8
Defining Functions
insert Ord a a - a - a insert x
insert x (yxs) xy
yinsert x xs
9
Defining Functions
Type signature. Optional!
insert Ord a a - a - a insert x
insert x (yxs) xy
yinsert x xs
10
Defining Functions
Type signature. Optional!
Ignore for now
insert Ord a a - a - a insert x
insert x (yxs) xy
yinsert x xs
11
Defining Functions
Type signature. Optional!
Ignore for now
Type of first argument a
insert Ord a a - a - a insert x
insert x (yxs) xy
yinsert x xs
12
Defining Functions
Type signature. Optional!
Ignore for now
Type of first argument a
insert Ord a a - a - a insert x
insert x (yxs) xy
yinsert x xs
Type of second argument list of as
13
Defining Functions
Type signature. Optional!
Ignore for now
Type of first argument a
Type of result
insert Ord a a - a - a insert x
insert x (yxs) xy
yinsert x xs
Type of second argument list of as
14
Defining Functions
Type signature. Optional!
Ignore for now
Type of first argument a
Type of result
insert Ord a a - a - a insert x
insert x (yxs) xy
yinsert x xs
Type of second argument list of as
What is a?
A type variable which can stand for any
type. This function is polymorphic.
15
Defining Functions
insert Ord a a - a - a insert x
insert x (yxs) xy
yinsert x xs
Definition by pattern matching case analysis
on the form of the arguments.
16
Defining Functions
insert Ord a a - a - a insert x
insert x (yxs) xy
yinsert x xs
Definition by pattern matching case analysis
on the form of the arguments.
Guards define conditions for an equation to
apply.
17
Defining Functions
We build a new structure as the
result purely functional.
insert Ord a a - a - a insert x
insert x (yxs) xy
yinsert x xs
Definition by pattern matching case analysis
on the form of the arguments.
Guards define conditions for an equation to
apply.
18
Defining Data Types
data Tree a Node a (Tree a) (Tree a) Leaf
deriving Show
19
Defining Data Types
Type name
data Tree a Node a (Tree a) (Tree a) Leaf
deriving Show
20
Defining Data Types
Type name
Type parameter
data Tree a Node a (Tree a) (Tree a) Leaf
deriving Show
  • Types may take parameters.
  • Enables us to define polymorphic functions which
    work on a tree with any type of labels.

21
Defining Data Types
Type name
Type parameter
data Tree a Node a (Tree a) (Tree a) Leaf
deriving Show
Constants start with upper case, variables with
lower
  • Types may take parameters.
  • Enables us to define polymorphic functions which
    work on a tree with any type of labels.

22
Defining Data Types
Node and Leaf are alternative forms of Tree.
data Tree a Node a (Tree a) (Tree a) Leaf
deriving Show
23
Defining Data Types
Node and Leaf are alternative forms of Tree.
data Tree a Node a (Tree a) (Tree a) Leaf
deriving Show
Types of the components.
24
Defining Data Types
Node and Leaf are alternative forms of Tree.
data Tree a Node a (Tree a) (Tree a) Leaf
deriving Show
Types of the components.
Ignore for now.
25
Defining Data Types
data Tree a Node a (Tree a) (Tree a) Leaf
deriving Show
26
Defining Data Types
data Tree a Node a (Tree a) (Tree a) Leaf
deriving Show
Type Tree Integer
27
Tree Insertion
insertTree Ord a a - Tree a - Tree
a insertTree x Leaf Node x Leaf Leaf insertTree
x (Node y l r) x l) r x y Node y l (insertTree x r)
xy Node y l r
Pattern matching works as for lists.
28
Overloading
  • Polymorphic functions use the same definition at
    each type.
  • Overloaded functions may have a different
    definition at each type.

Class name.
Read a is a type in class Eq, if it has the
following methods.
class Eq a where () a - a - Bool
(/) a - a - Bool x/y not (xy)
Class methods and types.
Default definition.
29
The Class Hierarchy
Read Type a in class Eq is also in class Ord,
if it provides the following methods
class Eq a Ord a where ( a -
Bool
30
Instance Declarations
instance Eq Integer where xy primitive
instance Eq a Eq a where
True xxs yys x y xs ys
Provided a is in class Eq, then a is in class
Eq, with the method definition given.
31
Types of Overloaded Functions
a may be any type in class Ord.
insert Ord a a - a - a insert x
insert x (yxs) xy
yinsert x xs
Because insert uses a method from class Ord.
32
Show and Read
class Show a where show a - String
class Read a where read String - a
These are simplifications there are more methods
in reality.
read . show id (usually)
33
Derived Instances
data Tree a Node a (Tree a) (Tree a) Leaf
deriving Show
Constructs a default instance of class Show.
Works for many standard classes.
Main show (Node 1 Leaf (Node 2 Leaf Leaf)) "Node
1 Leaf (Node 2 Leaf Leaf)"
34
Multi-Parameter Classes
Define relations between classes.
class Collection c a where empty c add
a - c - c member a - c - Bool
c is a collection with elements of type a.
instance Eq a Collection a a where
empty add () member elem
instance Ord a Collection (Tree a) a where
empty Leaf add insertTree member
elemTree
35
Functional Dependencies
A functional dependency
class Collection c a c - a where empty
c add a - c - c member a - c -
Bool
  • Declares that c determines a there can be only
    one instance for each type c.
  • Helps the type-checker resolve ambiguities
    (tremendously).

add x (add y empty) -- x and y must be the same
type.
36
Side Effects in Haskell
  • Suppose
  • tick String - Integer
  • reads an integer n from a file with given name,
  • writes n1 back to the file
  • returns n

Then tick tick might be False!
Cannot replace equals by equals. Not purely
functional!
37
Haskells Solution
Performs I/O and delivers a String.
Side effects are recorded in the type!
readFile String - IO String writeFile
String - String - IO ()
So the type of tick is tick String - IO
Int and tick tick is ill-typed.
IO is a monad -- more later!
38
The do notation
I/O actions may be combined in sequence.
tick String - IO Integer tick f
do contents contents writeFile f (show (n1)) return n
39
The do notation
I/O actions may be combined in sequence.
Type IO String
tick String - IO Integer tick f
do contents contents writeFile f (show (n1)) return n
Type String
Scope of contents
40
The do notation
I/O actions may be combined in sequence.
tick String - IO Integer tick f
do contents contents writeFile f (show (n1)) return n
Local declaration
Scope
41
The do notation
I/O actions may be combined in sequence.
tick String - IO Integer tick f
do contents contents writeFile f (show (n1)) return n
Type IO ()
No need to bind a name to the result.
42
The do notation
I/O actions may be combined in sequence.
tick String - IO Integer tick f
do contents contents writeFile f (show (n1)) return n
Type IO Integer Defines result of tick.
return a - IO a
43
IO a Action Yeilding an a
44
IO a Action Yeilding an a
Action at the prompt is performed.
The action returned by tick has a side-effect.
Yields a different Integer each time it is
performed.
45
IO a Action Yeilding an a
twice1 IO a - (IO a, IO a) twice1 c (c,c)
Result is not an action! Therefore not performed.
Next call reveals no side-effects occurred.
46
IO a Action Yeilding an a
twice2 IO a - IO (a,a) twice2 a do x a return (x,x) twice3 IO a - IO
(a,a) twice3 a do x The same action can be performed many times.
47
References
Variables in Haskell cannot be updated --
references can.
newIORef a - IO (IORef a) readIORef IORef
a - IO a writeIORef IORef a - a - IO ()
Reference operations have side-effects -- hence
IO type.
48
Example Destructive List Insertion
Updateable tail.
data RList a Nil Cons a (IORef (RList
a)) insertRList Ord a a - IORef (RList a)
- IO () insertRList x xs do cell readIORef xs case cell of Nil - do
new (Cons x new) Cons y xs' x do
new (Cons x new) xy - insertRList x xs'
Must read the list cell.
Create new cell and update old.
case is inline pattern matching.
49
Encapsulated Side Effects
  • IORefs can only be updated at the top level.
  • Can we use references internally to define a pure
    function?
  • Example
  • removeDuplicates Hashable a a - a

Array operations resemble reference ones.
Use a hash table internally to make comparison
fast.
No IO type no externally visible side-effects!
50
Encapsulation The ST Monad
newSTRef a - ST s (STRef s a) readSTRef
STRef s a - ST s a writeSTRef STRef s a - a
- ST s ()
Similar family of operations.
51
Encapsulation The ST Monad
newSTRef a - ST s (STRef s a) readSTRef
STRef s a - ST s a writeSTRef STRef s a - a
- ST s ()
s ties together the type of a reference and
the action which uses it.
52
Encapsulation The ST Monad
newSTRef a - ST s (STRef s a) readSTRef
STRef s a - ST s a writeSTRef STRef s a - a
- ST s ()
The encapsulation function runST (forall s .
ST s a) - a
53
Encapsulation The ST Monad
newSTRef a - ST s (STRef s a) readSTRef
STRef s a - ST s a writeSTRef STRef s a - a
- ST s ()
The encapsulation function runST (forall s .
ST s a) - a
Result type free from ST pure.
54
Encapsulation The ST Monad
newSTRef a - ST s (STRef s a) readSTRef
STRef s a - ST s a writeSTRef STRef s a - a
- ST s ()
The encapsulation function runST (forall s .
ST s a) - a
Each use binds a fresh variable s labels
references created, guarantees used only here.
Result type free from ST pure.
55
Encapsulation The ST Monad
newSTRef a - ST s (STRef s a) readSTRef
STRef s a - ST s a writeSTRef STRef s a - a
- ST s ()
The encapsulation function runST (forall s .
ST s a) - a
The argument of runST must be polymorphic in s.
This is a rank 2 type. Cannot be inferred
-- must be declared.
56
Overloading Side-Effects
Why should we choose between IO and ST when we
want side-effects?
class Monad m RefMonad m r m - r where
newRef a - m (r a) readRef r a - m a
writeRef r a - a - m ()
instance RefMonad IO IORef
instance RefMonad (ST s) (STRef s)
57
Overloading Side-Effects
Why should we choose between IO and ST when we
want side-effects?
class Monad m RefMonad m r m - r where
newRef a - m (r a) readRef r a - m a
writeRef r a - a - m ()
Partial type application. Plug in ST s and STRef
s for m and r in the types
instance RefMonad IO IORef
instance RefMonad (ST s) (STRef s)
58
Overloading Side-Effects
Why should we choose between IO and ST when we
want side-effects?
class Monad m RefMonad m r m - r where
newRef a - ST s (STRef s a) readRef
STRef s a - ST s a writeRef STRef s a -
a - ST s ()
instance RefMonad IO IORef
instance RefMonad (ST s) (STRef s)
59
Overloading Side-Effects
Why should we choose between IO and ST when we
want side-effects?
class Monad m RefMonad m r m - r where
newRef a - m (r a) readRef r a - m a
writeRef r a - a - m ()
Example
data RList r a Nil Cons a (r (RList r
a)) insertRList (Ord a, RefMonad m r) a
- r (RList r a) - m ()
60
Higher-Order Functions
  • Functions are values in Haskell.
  • Program skeletons take functions as parameters.

takeWhile (a - Bool) - a - a takeWhile
p takeWhile p (xxs) p x
xtakeWhile p xs otherwise
Takes a prefix of a list, satisfying a predicate.
61
Denoting Functions
62
Denoting Functions
below a b b 63
Denoting Functions
below a b b takeWhile (a - Bool) - a - a takeWhile
p takeWhile p (xxs) p x
xtakeWhile p xs otherwise
64
Denoting Functions
below a b b takeWhile (a - Bool) - a - a takeWhile
p takeWhile p (xxs) below 10 x
xtakeWhile p xs otherwise
A partial function application.
65
More Ways to Denote Functions
below a b b
  • takeWhile (below 10) 1,5,9,15,20

  • 66
    More Ways to Denote Functions
    below a b b
  • takeWhile (below 10) 1,5,9,15,20
  • takeWhile (\b - b
    Lambda expression. Function definition in place.
    67
    More Ways to Denote Functions
    below a b b
  • takeWhile (below 10) 1,5,9,15,20
  • takeWhile (\b - b
    Lambda expression. Function definition in place.
    f b b
    68
    More Ways to Denote Functions
    below a b b
  • takeWhile (below 10) 1,5,9,15,20
  • takeWhile (\b - b
  • takeWhile (
    Lambda expression. Function definition in place.
    f b b
    Partial operator application -- argument replaces
    missing operand.
    69
    Lazy Evaluation
    • Expressions are evaluated only when their value
      is really needed!
    • Function arguments, data structure components,
      are held unevaluated until their value is used.

    from n n from (n1)
    70
    Lazy Recursive Definitions
    nats 0 map (1) nats
    71
    A Confession
    This program doesnt work!
    tick String - IO Integer tick f
    do contents contents writeFile f (show (n1)) return n
    72
    A Confession
    This program doesnt work!
    The file is only opened here, it is read
    when contents is needed.
    tick String - IO Integer tick f
    do contents contents writeFile f (show (n1)) return n
    73
    A Confession
    This program doesnt work!
    The file is only opened here, it is read
    when contents is needed.
    tick String - IO Integer tick f
    do contents contents writeFile f (show (n1)) return n
    Not needed yet! n isnt needed
    74
    A Confession
    This program doesnt work!
    The file is only opened here, it is read
    when contents is needed.
    tick String - IO Integer tick f
    do contents contents writeFile f (show (n1)) return n
    Not needed yet! n isnt needed
    Not needed until after the file is opened for
    writing!
    75
    A Confession
    This program doesnt work!
    The file is only opened here, it is read
    when contents is needed.
    tick String - IO Integer tick f
    do contents contents writeFile f (show (n1)) return n
    Not needed yet! n isnt needed
    Not needed until after the file is opened for
    writing!
    So readFile sees an empty file!
    76
    A Confession
    This program doesnt work!
    tick String - IO Integer tick f
    do contents contents n seq writeFile f (show
    (n1)) return n
    77
    A Confession
    This program doesnt work!
    tick String - IO Integer tick f
    do contents contents n seq writeFile f (show
    (n1)) return n
    seq evaluates its first argument, then returns
    its second.
    78
    A Confession
    This program doesnt work!
    tick String - IO Integer tick f
    do contents contents n seq writeFile f (show
    (n1)) return n
    seq evaluates its first argument, then returns
    its second.
    Backquotes turn a function name into
    an operator.
    79
    Time for a Demo
    80
    Course Home Page
    www.cs.chalmers.se/rjmh/Combinators Begin with
    some finger exercises in Haskell.
  • Write a Comment
    User Comments (0)
    About PowerShow.com