External Effects Internal Effects Testing and Properties Administrivia
1
Software System Design and Implementation
Effects and IO Monad Practice
Curtis Millar
CSE, UNSW (and Data61) 1 July 2020
External Effects Internal Effects Testing and Properties Administrivia
2
The IO Type
A procedure that performs some side effects, returning a result of type a is written as IO a.
External Effects Internal Effects Testing and Properties Administrivia
The IO Type
A procedure that performs some side effects, returning a result of type a is written as IO a.
World interpretation
IO a is an abstract type. But we can think of it as a function: RealWorld -> (RealWorld, a)
(that¡¯s how it¡¯s implemented in GHC)
3
External Effects Internal Effects Testing and Properties Administrivia
The IO Type
A procedure that performs some side effects, returning a result of type a is written as IO a.
World interpretation
IO a is an abstract type. But we can think of it as a function: RealWorld -> (RealWorld, a)
(that¡¯s how it¡¯s implemented in GHC)
(>>=) :: IO a -> (a -> IO b) -> IO b pure ::a->IOa
getChar :: IO Char
readLine :: IO String
putStrLn :: String -> IO ()
4
External Effects Internal Effects Testing and Properties Administrivia
Two-player Tic-Tac-Toe
Example
5
Simple two-player Tic-Tac-Toe game
Done in editor
External Effects Internal Effects Testing and Properties Administrivia
6
newtype State s a = State (s -> (s, a))
State Monads
External Effects Internal Effects Testing and Properties Administrivia
newtype State s a = State (s -> (s, a))
State Monad
get :: State s s
put :: s -> State s ()
modify :: (s -> s) -> State s ()
State Monads
7
External Effects Internal Effects Testing and Properties Administrivia
State Monads
newtype State s a = State (s -> (s, a))
State Monad
get :: State s s
put :: s -> State s ()
modify :: (s -> s) -> State s ()
Here we use a monadic interface to simplify the passing of our state around, so that we don¡¯t need to manually plumb data around.
8
External Effects Internal Effects Testing and Properties Administrivia
Tic-Tac-Toe A.I
Example
Adding A.Is for Tic-Tac-Toe
9
Done in editor
External Effects Internal Effects Testing and Properties Administrivia
10
QuickChecking Monads
QuickCheck lets us test IO (and ST) using this special property monad interface:
monadicIO :: PropertyM IO () -> Property
pre :: Bool -> PropertyM IO ()
assert :: Bool -> PropertyM IO ()
run :: IO a -> PropertyM IO a
External Effects Internal Effects Testing and Properties Administrivia
Testing a Tic-Tac-Toe A.I
Example
Testing A.Is for Tic-Tac-Toe
11
Done in editor
External Effects Internal Effects
Testing and Properties Administrivia
12
Homework
External Effects Internal Effects
Testing and Properties Administrivia
13
1 Next week is flexibility week
Homework
External Effects Internal Effects
Testing and Properties Administrivia
14
Homework
1 Next week is flexibility week
2 Last week¡¯s quiz is due on Friday. Make sure you submit your answers.
External Effects Internal Effects
Testing and Properties
Administrivia
15
1 Next week is flexibility week
Homework
2 Last week¡¯s quiz is due on Friday. Make sure you submit your answers.
3 The fourth programming exercise is due by the start if my next lecture (in 14 days).
External Effects Internal Effects
Testing and Properties
Administrivia
16
1 Next week is flexibility week
Homework
2 Last week¡¯s quiz is due on Friday. Make sure you submit your answers.
3 The fourth programming exercise is due by the start if my next lecture (in 14 days).
4 This week¡¯s quiz is also up, it¡¯s due Friday week (in 16 days).
External Effects Internal Effects Testing and Properties Administrivia
17
Consultations
Poll on Piazza to register interest. Will not run if there are no votes.
External Effects Internal Effects Testing and Properties Administrivia
18
Consultations
Poll on Piazza to register interest. Will not run if there are no votes. Tomorrow, 9am to 11am on Blackboard Collaborate.
External Effects Internal Effects Testing and Properties Administrivia
19
Consultations
Poll on Piazza to register interest. Will not run if there are no votes. Tomorrow, 9am to 11am on Blackboard Collaborate.
Link on course website & Piazza.
External Effects Internal Effects Testing and Properties Administrivia
20
Consultations
Poll on Piazza to register interest. Will not run if there are no votes. Tomorrow, 9am to 11am on Blackboard Collaborate.
Link on course website & Piazza.
Make sure to join the queue on Hopper. Be ready to share your screen with REPL (ghci or stack repl) and editor set up.