Skip to content(if available)orjump to list(if available)

Monads are too powerful: The expressiveness spectrum

SchemaLoad

I tried learning Haskell for a decent chunk of time and could make some stuff, but despite trying to learn, I still could not tell you what a monad actually is. All the explanations for it seemed to make no sense.

valiant55

Forget all the academic definitions, at it's core a monad is a container or wrapper that adds additional functionality to a type.

oncallthrow

A monad is just a monoid in the category of endofunctors

hinkley

Unfortunately no one can tell you what a monad is. You have to experience it for yourself. - Haskell Morpheus

bananaflag

You should first understand what a typeclass and a Functor is.

null

[deleted]

gowld

The important thing to know first is that a monad is not a single thing like "Optional". "monad" is a pattern or "interface" (called a "typeclass" in Haskell), that has many implementations, (Optional, Either, List, State Transormer, IO (Input/Output), Logger, Continuation, etc). Sort of how "Visitor" pattern in C++/Java is not a single thing.

https://hackage.haskell.org/package/base-4.21.0.0/docs/Contr...

https://book.realworldhaskell.org/read/monads.html

A common metaphor for monad is "executable semicolons". They are effectively a way to add (structured) hook computations (that always returns a specific type of value) to run every time a "main" computation (akin to a "statement" in other languages) occurs "in" the monad.

It's sort of like a decorator in Python, but more structured. It lets you write a series of simple computational steps (transforming values), and then "dress them up" / "clean them up" by adding a specific computation to run after each step.

Ryder123

This makes SchemaLoad's comment perfectly clear.

(but do I appreciate the effort you put into your reply - reading that monad's are more like interfaces is new information to me, and might help down the road)

PaulHoule

I'd argue the exact opposite. Compared to what you can do if you can write compilers anything that involves composing functions is weak beer and most monad examples cover computational pipelines as opposed to computational graphs. It's like that Graham book On Lisp, it's a really fun book but then you realize that screwing around with functions and macros doesn't hold a candle to what you learn from the Dragon Book.

fn-mote

> screwing around with functions and macros doesn't hold a candle to what you learn from the Dragon Book

This depends a lot on what you mean. My first take is that the more you know about macros the more you realize what they can do.

I don’t know what your takeaway from the Dragon Book was, but writing DSLs using macros feels very usefully powerful to me.

I think you are undervaluing modern macros.

taeric

I maintain that the big advantage of the On Lisp approach is that all of that is available without having to write a new compiler.

Granted, I also don't have as heavy an attachment to pure functional as most people seem to build. Don't get me wrong, wanton nonsense is nonsensical. But that is just as true in immutable contexts.

PaulHoule

What I found remarkable about that book is that 80% of what is in it can be done with functions and no macros, mostly you can rewrite the examples in Python except for the coroutines but Python already has coroutines. It also irks me that the I don’t think the explanation of coroutines in Scheme is very clear but it’s become the dominant one you find in the net and I can’t find a better one.

As for ‘compiler’ you also don’t need to go all the way to bare metal, some runtime like WASM or the JVM which is more civilized is a good target these days.

taeric

Totally fair. I think a lot of the things we used to do in the name of efficiency has been completely lost in the progress of time. Largely from the emergence and refinement of JIT compilers, I think?

That is, a lot of why you would go with macros in the past was to avoid the expense of function calls. Right? We have so far left the world of caring about function call overhead for most projects, that it is hard to really comprehend.

Coroutines still strike me as a hard one to really grok. I remember reading them in Knuth's work and originally thinking it was a fancy way of saying what we came to call functions and methods. I think without defining threads first, defining a coroutine is really hard to nail down. And too many of us take understanding of threads as a given. Despite many of us (myself not immune) having a bad understanding of threads.

andersmurphy

Yeah, I've had fun using macros to create optimised functions at runtime (inline caching effectively) and/or generate code that is more friendly to the JVM JIT.

Also, there's always plenty of use for doing work at compile time.

In some sense they can also be seen as a better code generation.

veqq

But lisp programs are compilers. That's the whole point of lisp and macros. Your functions can happily emit assembly direction.

instig007

> if you can write compilers anything that involves composing functions is weak beer

> screwing around with functions and macros doesn't hold a candle to what you learn from the Dragon Book.

---

So, what is it that you learn from that book that's a revelation for you compared to the weak beer of composable effect systems?

bionhoward

And here I thought it was a pedantic word for “data box”

jcmontx

Haskell looks a heck lot like F#, even more than Ocaml if you ask me

whycombinetor

Yes. For the same reason that the Yoneda lemma and the Cayley theorem are almost meaningless tautologies once you fully understand what they're saying. "Every small thing (of a certain type) is able to be expressed as a subcase of a bigger thing that contains every single possible subcase in existence." Well no shit.

IshKebab

Interesting, but it seems like he kind of proved himself wrong? Monads are the only option he presented that are sufficiently powerful for normal programs.

bokumo

I don't think you're being fair to Chris Penner. He ends his blog post with: "It may take me another 5 years to finally finish it, but at some point we'll continue this journey and explore how we can sequence effects using the hierarchy of Category classes instead." Emphasis by me.

So while it is true, that what he has described so far is not sufficiently powerful for normal programs, he has clearly stated that there are more abstractions between Applicative and Monad to explore than what he has presented so far.

z3ratul163071

[flagged]

dang

Please don't do this here.