Functors & IO Prof. Tom Austin San Jos State University - - PowerPoint PPT Presentation

functors io
SMART_READER_LITE
LIVE PREVIEW

Functors & IO Prof. Tom Austin San Jos State University - - PowerPoint PPT Presentation

CS 252: Advanced Programming Language Principles Functors & IO Prof. Tom Austin San Jos State University Review: Add 1 to each element in a list of numbers The Functor typeclass class Functor f where fmap :: (a -> b) -> f a


slide-1
SLIDE 1

CS 252: Advanced Programming Language Principles

  • Prof. Tom Austin

San José State University

Functors & IO

slide-2
SLIDE 2

Review:

Add 1 to each element in a list of numbers

slide-3
SLIDE 3

The Functor typeclass

class Functor f where

fmap :: (a -> b) -> f a -> f b map :: (a -> b) -> [a] -> [b]

Compare fmap to map:

slide-4
SLIDE 4

A functor is something that can be mapped over.

slide-5
SLIDE 5

Box analogy for functors

slide-6
SLIDE 6

Maps as functors

instance Functor [] where fmap = map

slide-7
SLIDE 7

Prelude> map (+1) [1,2,3] [2,3,4] Prelude> fmap (+1) [1,2,3] [2,3,4] Prelude> fmap (+1) [] [] Prelude> fmap (+1) $ Just 3 Just 4 Prelude> fmap (+1) $ Nothing Nothing

slide-8
SLIDE 8

Maybe as a functor

instance Functor Maybe where fmap f (Just x) = Just (f x) fmap f Nothing = Nothing

slide-9
SLIDE 9

Either type

data Either a b = Left a | Right b deriving (Eq,Ord,Read,Show)

slide-10
SLIDE 10

Prelude> fmap (+1) $ Right 20 Right 21 Prelude> fmap (+1) $ Left 20 Left 20

???

slide-11
SLIDE 11

Either type

data Either a b = Left a | Right b deriving (Eq,Ord,Read,Show)

Error type

slide-12
SLIDE 12

Either type

data Either a b = Left a | Right b deriving (Eq,Ord,Read,Show)

Expected type

slide-13
SLIDE 13

Either as a functor

instance Functor (Either a) where fmap f (Right x) = Right (f x) fmap f (Left x) = Left x

slide-14
SLIDE 14

Haskell Input/Output

slide-15
SLIDE 15

Side effects & monads

  • Haskell avoids side effects

–Inevitable in real programs

  • Monads

–related to functors –used to compartmentalize side effects

slide-16
SLIDE 16

Haskell Input/Output

  • Why does main have this type?

main :: IO ()

  • Why does getLine have this type?

getLine :: IO String

slide-17
SLIDE 17

Hello world in Haskell

main = putStrLn "hello"

We can call other functions that perform file I/O, but their type will also include an IO somewhere

slide-18
SLIDE 18

Do syntax

main = do putStrLn "Who goes there?" name <- getLine putStrLn $ "Welcome, " ++ name Pulling data out of an IO "box"

slide-19
SLIDE 19

A more complex example

main = do line <- getLine if null line then return () else do putStrLn $ reverseWords line main reverseWords = unwords . map reverse . words

The unit type

Ah! Something familiar.

slide-20
SLIDE 20

No

slide-21
SLIDE 21

Haskell's return: the single worst named keyword in any language ever made.

slide-22
SLIDE 22

Haskell's return

  • unrelated to return in other languages
  • better names: "wrap" or "box":

return puts a value in a "box" <- gets contents of a "box"

slide-23
SLIDE 23

Haskell's return

*Main> :t () () :: () *Main> :t (return ()) (return ()) :: Monad m => m ()

We'll come back to Monads later

slide-24
SLIDE 24

Is Io a Functor?

main = do line <- fmap (++"!!!") getLine putStrLn line

fmap appends the string "!!!" to the input from getLine.

slide-25
SLIDE 25

Functor IO

instance Functor IO where fmap f action = do result <- action return (f result)

Take the value out

  • f its box

Apply f to result, then put the value back in the box

slide-26
SLIDE 26

Functor Laws

(or at least strong suggestions)

slide-27
SLIDE 27

Functor Law #1

If we map the id function over a functor, the functor that we get back should be the same as the original functor. Prelude> fmap id (Just 3) Just 3 Prelude> fmap id Nothing Nothing Prelude> fmap id [1,2,3] [1,2,3]

slide-28
SLIDE 28

Functor Law #2

Composing two functions and then mapping the resulting function over a functor should be the same as first mapping one function over the functor and then mapping the other one. More formally written: fmap (f . g) = fmap f . fmap g

slide-29
SLIDE 29

The functor laws are not enforced, but they make your code easier to reason about.

slide-30
SLIDE 30

Lab: Functors

Add support for fmap to the Tree type. Download functors.lhs from the course website.