Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Haskell terminology, what are monadic effects?

Tags:

haskell

I feel I have a good understanding of monads, but I'm not too sure what is referred to by 'monadic effects'? Is this the evaluation of a monad? Does it have something to do with IO?

like image 538
Chris Stryczynski Avatar asked Jul 11 '17 22:07

Chris Stryczynski


2 Answers

If you have a value of type M a with M a Monad (or Applicative for applicative effects), then by effects we mean the information that is not contained in the a part. For example with IO it is very clear. A value of IO Int is an Int with some IO effects like writing to a file or firing missiles. A value of type Maybe Int is an Int with the effect of maybe actually not containing an Int. For [Int] the effect is, that you actually have multiple Ints.

We call this an effect because you can think of Monads and Applicatives as notions of computation with certain effects. For Maybe the effects are that you can abort the computation prematurely, for [] you can split the computation.

like image 71
Julia Path Avatar answered Nov 15 '22 07:11

Julia Path


I'm going to try to talk at the question several ways, and hopefully it's helpful.

An "effect" (as in "side-effects") refer to the behaviors of a specific instance of Monad, so e.g. the State monad expresses the effect of "stateful computation" with get and put. Monad transformer libraries like mtl can be thought of as ways of "composing effects".

Without knowing the types (or in fact reading the docs) for foo and bar here, we can't say anything about what "monadic effects" are happening here, even though we can say quite a few other things about this code:

do a <- fmap bar $ foo x
   b <- baz
   return (a,b)

The do block above has a type of the form SomeMonad m=> m (a,b). That tuple (a,b) that is "returned", and the way it can be passed to another "effectful computation" with >>=, is not what we're talking about when we talk about "effects".

Monadic effects always actually "happen" when you run them (by calling runState for State for instance).

In the case of IO only the runtime has access to the particular run function for IO, so the nonexistent runIO function calls main to run your program. For IO the "monadic effects" are truly the same as what in other languages you'd call "side-effects", i.e. just about anything that might change the state of the world.

like image 24
jberryman Avatar answered Nov 15 '22 07:11

jberryman