News for hackers. Stuff that matters.
Re:Escaping Hell with Monads (2017) by maxxxxx on Friday February 23, 2018 @06:40AM (#16445705)
How is error handling done if something goes wrong in one of the functions?
Re:Escaping Hell with Monads (2017) by js8 on Friday February 23, 2018 @07:37AM (#16445932)
It depends. In Haskell, functions do not fail in traditional sense - they just return indication of failure. So the return type of function can be, for instance, Either type (as someone already noted) which basically lets you have proper value and error value, one or the other.

And Either is also a monad. Typically, you would derive your own monad by somehow combining the two monads (for instance using a monad transformer) that would have a behavior you want.

In ordinary languages, the error handling is done ad hoc, in each situation the semantics can subtly differ. You can do that too but then you have to explicitly handle the fact that functions return Either in the function body (this would be somewhat similar to checked exceptions). Or you can define your own monad that prescribes the correct semantics of interaction between the monads.

Re:Escaping Hell with Monads (2017) by gmfawcett on Friday February 23, 2018 @10:47AM (#16446991)
> In Haskell, functions do not fail in traditional sense - they just return indication of failure.

That's too general a statement. Haskell functions certainly can fail in the traditional sense. As a trivial example, "head []" is a well-typed Haskell expression that will raise an exception when evaluated; if the exception is not caught (using a traditional exception handler), then it propagates to an error, and the program exits.

This article [1] about the many ways to handle errors in Haskell is a bit old, but as far as I know, all his points are still relevant. The conclusion (for me) is that if modern Haskell tends to use return-values on failure, it's due to convention (and good taste) among developers, and not due to a design constraint of the language.

[1] http://www.randomhacks.net/2007/03/10/haskell-8-ways-to-repo...

Re:Escaping Hell with Monads (2017) by pjmlp on Friday February 23, 2018 @07:52AM (#16445992)
You end up with something that F# community calls railroad programming, each monad works like a rail switch driving which of the functions actually gets called on it.

So if they are in an error state it is like a nop, until someone actually retrieves the error value.

Re:Escaping Hell with Monads (2017) by seanmcdirmid on Friday February 23, 2018 @09:06AM (#16446347)
This is what I don’t get about FP, in exchange for code that looks more elegant, you get something that is much more of a PITA to debug. If there is a problem, I often find getting rid of the indirected plumbing and directly exposing control flow to be the best way to find out what is going on when and where.
Re:Escaping Hell with Monads (2017) by pjmlp on Friday February 23, 2018 @10:10AM (#16446712)
It is only a PITA to debug on paper, not when using a graphical debugger.
Re:Escaping Hell with Monads (2017) by seanmcdirmid on Friday February 23, 2018 @10:15AM (#16446754)
How so? Visual studio still doesn’t reveal the right context when you actually find out the error has occurred.
Re:Escaping Hell with Monads (2017) by pjmlp on Friday February 23, 2018 @11:20AM (#16447245)
Re:Escaping Hell with Monads (2017) by seanmcdirmid on Friday February 23, 2018 @12:36PM (#16447834)
Nice! Tooling is a large part of my trouble with FP, better tooling would make it easier to debug, especially those complex signal networks that require different debugging paradigms from imperative languages.
Re:Escaping Hell with Monads (2017) by andrewprock on Friday February 23, 2018 @11:33AM (#16447357)
This suggest that it's difficult to reason about the code. That's the last thing I want.
Re:Escaping Hell with Monads (2017) by m1el on Friday February 23, 2018 @06:43AM (#16445714)
The answer to that is `Either` monad (similar to `Result` in Rust).
Re:Escaping Hell with Monads (2017) by thomasahle on Friday February 23, 2018 @07:14AM (#16445844)
You have to then combine multiple monads though, which isn't quite as easy iirc.
Re:Escaping Hell with Monads (2017) by loverofthings on Friday February 23, 2018 @08:20AM (#16446120)
Well, monad transformers make things easy but the abstraction impacts runtime performance :(