Hacker News new | ask | show | jobs
by eximius 3797 days ago
"No, you don't have to learn monads."

Ha! This is ironic because a present discussion over some syntactic sugar (maybe more) to make Rust error handling feel more part of the language and more ergonamic may be ad-hoc monads. Not that the programmer needs to know that - it's just something that should happen to play nice in the rest of the Rust type ecosystem

1 comments

I was under the impression that `Option<T>` and `Result<T, E>` were monads.
What makes monads in Haskell interesting is that someone identified them as a pattern, generalized their usage, and then syntax was added to the language that lets you compose them in a natural manner. Sequential execution itself "is a monad", but in most languages we express that using something as simple as ";" or "\n": Haskell effectively generalized the idea of statements to support anything that is monadic, which is why identifying something as a monad ends up becoming so interesting in Haskell as opposed to many other languages.
And Haskell gave up rich control flow (break, continue, early return) by doing so. If you trace through what having those statements means for monads as a first-class concept, you find that the situation becomes very much not that simple.
I've never heard this argument before. Very interesting.
MONSIEUR JOURDAIN: Oh, really? So when I say: "x := x+1" and "throw new Exception” is that monadic?

PHILOSOPHY MASTER: Most clearly.

MONSIEUR JOURDAIN: Well, what do you know about that! These forty years now I’ve been using monads in programming without knowing it!

    -------------------------------
With many apologies to Molière, many things are monadic including state and exceptions. Most programming languages introduce them as first-class core concepts rather than as monad instances. Hence you can use them innocently, without realising that they are monadic.
Yes but the Rust type system cannot express monads, so they aren't monads in the type sense (they don't implement a specific Monad trait).
Fun Trivia Fact: Rust's type system was[1] Turing complete. So you could actually have monads. You just wouldn't want to.

1: The program that proved this no longer compiles, so we're not sure if the type system is or is not at the moment.

I would love to see this program. Maybe even spend some time trying to get it to compile.
I think this is still possible with associated types. You can imitate higher kinded types with associated types, just that you wouldn't want to.

I recall someone posting a Monad or Collection HKT example when associated types were proposed.