In Haskell terminology, what are the monadic effects?

I feel like I have a good understanding of monads, but I'm not too sure about what are called "monadic effects"? Is this a monad score? Does it have anything to do with IO?

+3


source to share


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 information that is not contained in the part a

. For example, IO

this is very clear. The value IO Int

represents Int

with some IO

effects such as writing to a file or launching a rocket. The type value Maybe Int

is Int with the effect of possibly not actually containing Int

. The [Int]

effect is that you actually have multiple Int

s.



We call this effect because you can think of Monads and Applicatives as computation concepts with specific effects. For the Maybe

effects are that you can abort the computation prematurely, for []

you can break the computation.

+13


source


I'm going to try to talk to the question in several ways, and hopefully this will be helpful.

"Effect" (as in "side effects") refers to the behavior of a particular instance Monad

, for example, the monad State

expresses the effect of "stateful computation" with get

and put

. Monad transformer libraries such as mtl

can be thought of as ways to "create effects".

Without knowing the types (or actually reading the docs) for foo

and bar

here, we can't say anything about "monadic effects" going on here, although we can say quite a few other things about this code:

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

      



The block do

above is of the form type SomeMonad m=> m (a,b)

. This tuple (a,b)

that is "returned", and the way it can be passed on to another "efficient computation" using >>=

, is not what we are talking about when we talk about "effects".

Monadic effects always "happen" when you call run

them (for example, by calling runState

for State

).

In the case, IO

only the runtime has access to a specific function run

for IO

, so a non-existent function runIO

calls main

to run your program. For IO

"monadic effects" are truly the same as what in other languages ​​you call "side effects", i.e. Everything that can change the state of the world.

+3


source







All Articles