News for hackers. Stuff that matters.
Re:Are Monads a Waste of Time? by pdpi on Tuesday February 20, 2018 @09:51AM (#16420684)
That's not quite right.

Let's define an operator `|> = (flip $)`, which has a type signature `a -> (a -> b) -> b`, which, by your reasoning, should force sequencing too. Except it doesn't: Laziness is a bitch to work with, and `a |> f |> g` evaluates to `g(f(a))`, and g is evaluated first, which then evaluates f(a) on demand. If you consider `1 |> (+1) |> const 4`, this will evaluate to 4 without ever performing the addition.

Instead, let's compare this to the monadic equivalent. `(return 1) >>= return . (+ 1) >>= return . (const 4)`. The sequence that's being forced here isn't that (+ 1) is evaluated, it's that the `return` in `return 1` is evaluated (producing a 1) before `return . (+ 1)` is ever touched, and that _that_ return is evaluated (presumably producing an unevaluated thunk for (1 + 1)) before `return . (const 4)` is touched.

In IO world, this corresponds to guaranteeing that the IO operations happen in sequence irrespective of the order in which the pure computations they wrap are evaluated in.

Re:Are Monads a Waste of Time? by tome on Tuesday February 20, 2018 @09:57AM (#16420742)
> the `return` in `return 1` is evaluated (producing a 1) before `return . (+ 1)` is ever touched, and that _that_ return is evaluated (presumably producing an unevaluated thunk for (1 + 1)) before `return . (const 4)` is touched.

That's not quite right either. The former returns need never be evaluated.

    > undefined >>= (return . (const 4)) :: Identity Int
    Identity 4
Re:Are Monads a Waste of Time? by pdpi on Tuesday February 20, 2018 @09:59AM (#16420767)
Right — I should've said "that's the mechanism through which the implementation of `>>=` can force sequencing". `>>=` for Identity doesn't, most of the other common ones do, in some sense or another.
Re:Are Monads a Waste of Time? by tome on Tuesday February 20, 2018 @10:03AM (#16420812)
Right! `>>=` can force sequencing. It doesn't for Identity, Writer.Lazy or State.Lazy, but it can. OK, but also Arrow and Monoid can enforce a (different sort of) sequencing relationship.

Neither does monad force a sequencing relationship nor is it the only thing than can force a sequencing relationship so I can only conclude that "What a Monad does is to force sequencing by creating a data dependency" is indeed one of those misconceptions!

Re:Are Monads a Waste of Time? by lmm on Wednesday February 21, 2018 @05:00AM (#16428287)
That's just Haskell's crazy evaluation semantics though, not something that applies to monads in general.
Re:Are Monads a Waste of Time? by tome on Wednesday February 21, 2018 @05:31AM (#16428429)
Indeed. But bad_user made a claim about monads in general ("What a Monad does is to force sequencing by creating a data dependency.") so all I need to do to refute it is give an example of a particular monad where the claim doesn't hold.
Re:Are Monads a Waste of Time? by lmm on Wednesday February 21, 2018 @06:23AM (#16428661)
Monads force a denotation of sequencing. Of course you can write a language implementation where the sequencing of operation does not represent the sequencing of denotation (just as you can write a "compiler" that compiles all programs to Hello World), but, well, that's on you.
Re:Are Monads a Waste of Time? by tome on Wednesday February 21, 2018 @01:08PM (#16431508)
> Monads force a denotation of sequencing

Aha! Now we're on to something.