8
votes

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.

1
well there are all kinds of side-effects so which one do you want? - some of those have their own monads (State, ...) and many libs introduce some more (Yesod, ...) - but no I guess you cannot assemble them just like in PureScript - usually there are transformers and the monad-stack concept to do thisRandom Dev
A small nitpick: IO has effects, not side-effects. A side-effect would be something evil like a computation pretending to be pure, but doing IO via unsafePerformIO.Cactus

1 Answers

4
votes

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.