4 ms·
Start with a functor--a parameterized type with fmap. Maybe t, Either e t, [t], IO t, State s t, and so on. Consider a monad m and a function, a -> m b. Freque
by cynicalkane 10y ago
Start with a functor--a parameterized type with fmap. Maybe t, Either e t, [t], IO t, State s t, and so on.
Consider a monad m and a function, a -> m b. Frequently you want to feed another monadic value, m a, into that function.
A monad gives you a function, >>=, that lets you take m a and feed it into a -> m b.
So any function that does IO, that looks like a -> IO b, you can take an IO a and plug it into a -> IO b. Any function that manipulates a state, you can take State a and plug it into a -> State b. This is the heart of a monad: gluing monadic functions together.
The meaning of >>= depends on the monad you're in. You also need return :: a -> m a, which can take a thing and put it in a monadic value.
There are monad laws also. A lot of times there's several ways to manipulate monads to do something, and the laws are needed to make sure they're all equivalent. For everyday monad use the laws aren't terribly important to remember, and GHC's linter will helpfully point out when you can use them.
That's my attempt at a short explanation. The "monad tutorial problem" has been weighing on my mind. The only way I ever understood monads was by reading and rereading the definition and examples. Tutorials never taught me anything.
- slmyers 10y agoI'm still slightly confused. The author suggests that a monad is composed of 3 functions. It seems to me you've shown 2 of these functions. a -> m b >>= What is the third function? Did I misunderstand you?
- gizmo686 10y agoThe author is mistaken. There are only two functions necessary for a monad: >>= and return Also, the type of return is `a -> m a`. The type you cite `a -> m b` is a parameter to >>= (which has type `m a -> (a -> m b) -> m b. The author might be refering to a quirk of the Haskell monad interface, which has to additional functions: ">>" which can be easily defined in terms of >>=. I assume this is part of the interface so users can override it with a more efficient implementation, but every implementation I have seen just uses the default one. "fail" which is an error handler. This is being depreciated as a function of Monads, and moved into its own MonadFail typeclass. This also has a default implementation that just throws an exception.
- acomar 10y ago> The author is mistaken. There are only two functions necessary for a monad: >>= and return Probably because monads are frequently described as triples -- two functions and a functor. The author seems to have reduced that to three functions (presumably fmap) without realizing that it isn't sufficient.
- rdnetto 10y agoI suspect the third function is the one for running the monad. It's different for each one, so it's not part of the type class, but you do need it to use a given monad. (Except IO, though even it has unsafePerformIO)
- gizmo686 10y agoWhat is the function for running Maybe, or List?