Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redefine IO to simplify debugging?

Tags:

haskell

monads

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?

like image 843
ErikR Avatar asked Mar 11 '12 16:03

ErikR


1 Answers

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.

like image 200
Jedai Avatar answered Oct 20 '22 20:10

Jedai