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
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 ()
2
External Effects Internal Effects Testing and Properties Administrivia
Two-player Tic-Tac-Toe
Example
3
Simple two-player Tic-Tac-Toe game
Done in editor
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.
4
External Effects Internal Effects Testing and Properties Administrivia
Tic-Tac-Toe A.I
Example
Adding A.Is for Tic-Tac-Toe
5
Done in editor
External Effects Internal Effects Testing and Properties Administrivia
6
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
7
Done in editor
External Effects Internal Effects
Testing and Properties
Administrivia
8
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
9
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.