3 ms·
> Monads are not about sequencing. They don't force applying functions > in an order, at least any more than regular function application does. Monads are ab
by jmillikin 4y ago
> Monads are not about sequencing. They don't force applying functions
> in an order, at least any more than regular function application does.
Monads are about sequencing. This is easier to visualize if you de-sugar them:
f = do
x1 <- m 1
x2 <- m 2
return (x1 + x2)
is an imperative form of
f = m 1 >>= (\x1 -> m 2 >>= (\x2 -> return (x1 + x2)))
You can see that there is no way to evaluate `x1` without first evaluating `m 1`, even if the result is a thunk.
> Haskell does not need them, they're just an interface and don't
> add anything to a language you can't do without them.
How would you recommend implementing do-notation without the Monad typeclass? Applicative functors aren't enough.
> The monad instance says IO (IO a) is "the same as" an IO a, so really
> any sequencing ability implied in IO (IO a) already pre-exists in IO a.
I don't see your point. That may be true of IO, but it's not true in general. `Maybe ()` and `Maybe (Maybe ())` are different types.
- simiones 4y ago> You can see that there is no way to evaluate `x1` without first evaluating `m 1`, even if the result is a thunk. Isn't this true of `f(g(x))` as well? You can't evaluate f(g(x)) without evaluating g(x) first, right?
- jmillikin 4y agoYep[0], and if you really wanted to, you could use that syntax to express the monadic interface: putStrLn :: TheWorld -> String -> (TheWorld, ()) main :: TheWorld -> (TheWorld, ()) main world = (part3, ()) where (part1, _) = putStrLn world "Hello" (part2, _) = putStrLn part1 ", " (part3, _) = putStrLn part2 "world!" It would be a frustrating user experience, but it's a valid and equivalent encoding to Haskell's `IO` type. [0] I'm ignoring lazy evaluation for now, but `f` could be implemented such that it returns before `g(x)` is evaluated. This is generally not relevant for purely functional code, but it can affect performance and exception-safety.
- afiori 4y agoLazy evaluation is exactly why Haskell has monads and Ocaml does not. the whole point of haskell was to see what happens if try to build a language with the strong requirement of lazy evaluation. If you remove it you also remove any need* for the IO monad * you can obviously use an IO like interface in a non-lazy language, but it is not necessary