I've been going through the slides at http://www.algorithm.com.au/downloads/talks/monads-are-not-scary/monads-are-not-scary-chak.pdf
and approx. 75% of the way through the deck there is the bullet point "Redefine IO to simplify debugging!"
This looks like a very intriguing idea! Can someone give a practical example of what the speaker is talking about?
Well, what if instead of IO, you used a monad that simulates I/O with a perfectly controlled and stipulated environment ? You could easily test those "IO" actions like they were pure functions. This is the idea behind for instance IOSpec, which goes even farther by allowing you to specify precisely what kind of effect you want to allow in your simulated IO, you could write :
myFunction :: a -> b -> IOSpec (Teletype :+: IORefS)
myFunction x y = do
...
putStr (...)
ref <- newIORef ...
...
(teletype allow for terminal functions, IORefS for references) And then test your function in a quickcheck property for instance (see the VM module and runIOSpec) with complete control over input and output or even step by step in GHCI. And if it works correctly, just change the import to introduce Test.IOSpec.Surrogate which redefine IOSpec f as a synonym for IO.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With