Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Monad for tracking side-effects

Tags:

haskell

monads

In Haskell, we have the IO monad to deal with side-effects, although, it is not able to expressively track side-effects, you don't really know what type of side effect is really happening:

main :: IO ()

In PureScript, we have the Eff monad, where you are able to know what type of side-effects happen according to the type signature:

main :: forall e. Eff (fs :: FS, trace :: Trace, process :: Process | e) Unit

Here it is clearly that the main function has use of the filesystem, of tracing messages to the console and is able to handle the current process, where we have a specific module Control.Monad.Eff for dealing with side effects, and submodules such as Control.Monad.Eff.Random and Control.Monad.Eff.Console.

Taking by example the following:

module RandomExample where

import Prelude

import Control.Monad.Eff
import Control.Monad.Eff.Random (random)
import Control.Monad.Eff.Console (print)

printRandom :: forall e. Eff (console :: CONSOLE, random :: RANDOM | e) Unit
printRandom = do
  n <- random
  print n

This is so much more specific than using just "Hey, here happens a side-effect, that's it, no more that you need to know!". I've being looking through the web and I didn't see a monad complete enough to track side-effects.

Is there a specific monad in Haskell, like Eff, for tracking side effects?

Thanks in advance.

like image 320
Marcelo Camargo Avatar asked Aug 13 '15 11:08

Marcelo Camargo


1 Answers

There are a couple of libraries that define similar effect systems for Haskell.

I have worked some with extensible-effects, and found it quite easy to add restricted IO, eg STDIO, FileIO, effects. The lack of compiler support makes is slightly less nice to use.

If you want to try it out you can find inspiration in existing effects for the extensible-effects framework: http://hackage.haskell.org/packages/#cat:Effect

There seems to be a version of extensible-effects that doesn't use Typeable to track effects: http://hackage.haskell.org/package/effin. That should make it nicer to write new effects.

like image 139
adamse Avatar answered Oct 20 '22 22:10

adamse