You need to be able to "wrap" values and then also "wrap" functions in the way you expect. That's literally it.
Btw, the list monad example is stupid imo and borderline misleading. The promise/nullable/Either examples are better. you "wrap" a function by putting it as the only value in a list, and "map" pretty much acts as your function wrapper, but technically this you need to jump through a couple hoops to make it monadic, and I'm just not sure the metaphor is helpful here
I might have some inaccuracies in how I state this since I’m not from a functional programming background, but I think of monads as an abstraction of chaining functions over a value such that each returned value can specify what further transformations it supports, and particularly in such a way that generic transformations (like sequence reversal) can easily be applied, and errors or empty values can be accounted for within the control flow.
I think sometimes people call this a “fluent API”, but I would never call it “a generalization of callbacks” or of promises.
Anyway there’s a much more mathematically precise way of stating it but this is my intuitive caveman way of thinking about it.
(I'm trying very hard not to fall into the trying-to-explain-monads trap!)
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.
(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)
Typeclasses are a distraction, the point is computation ignoring annoying contexty stuff (file not found errors, null on failure, etc) and there's dozens of examples in literally every language ever.
Not all problems are solved with a technical definition.
Example is best for illustration. I'll use a made-up syntax.
// 'Maybe' is a monad wrapping any value type T that can be null.
class Maybe<T> {
// The value wrapped by the monadic value
value: T;
// A constructor to make a Maybe monadic value from a plain value T.
// This is called 'unit' or 'return' in Haskell, or 'lift' in other languages.
// It's really a constructor.
static wrap(v: T) Maybe<T> {
return new Maybe { value = v }
}
// then() applies fn on the unwrapped value. fn returns a Maybe<T>.
// This is called 'bind' in Haskell, or 'flatMap' in other languages.
then(fn: (T) => Maybe<T>) Maybe<T> {
return this.value == null ? Maybe.wrap(null) : fn(this.value);
}
}
That's it! That's all to to monad. You can use the same pattern to build other monadic types, like List<T>, Promise<T>, IO<T>, as long as the wrap() and then() functions are built accordingly. Back to this example, to use it, let a = Maybe<int>.wrap(4) // construct a monadic value
let b = a.then(x => Maybe<int>.wrap(x + 1)) // add 1 to it
let c = Maybe<int>.wrap(null) // construct a null monadic value
let d = c.then(x => Maybe<int>.wrap(x + 1)) // safely handle null; d is null
let e = Maybe<int>.wrap(5)
.then(x => Maybe<int>.wrap(x + 1))
.then(x => Maybe<int>.wrap(x * 2)) // chain the calls
let f = Maybe<float>.wrap(5.0) // The same Maybe on a different type
.then(x => Maybe<float>.wrap(null))
.then(x => Maybe<float>.wrap(x * 2)) // chain the calls; safely handle nullNan-in served tea. He poured his visitor’s cup full, and then kept on pouring.
The professor watched the overflow until he no longer could restrain himself. “It is overfull. No more will go in!”
“Like this cup,” Nan-in said, “you are full of your own opinions and speculations. How can I show you a Monad unless you first empty your cup?”
A monad is a function that can be combined with other functions.
It's a closure (or functor to the cool kids) that can be bound and arranged into a more complex composite closure without a specification of any actual value to operate on.
It's a lazy operation declaration that can operate over a class of types rather than a specific type (though a type is a class of types with just a single type so this is more a note on potential rather than necessary utility) that can be composed and manipulated in languages like Haskell to easily create large declarative blocks of code that are very easy to understand and lend themselves easily to abstract proofs about execution.
You've probably used them or a pattern like them in your code without realizing it.
I can’t even make stuff with fundamental groups.
In that case, everything runs within the effect monad and then no one would ever really need to learn what a monad is, just that some calls are effectful (like reading a file or throwing an exception).
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.
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.
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.
> 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?
1. I free solo a bunch of junk in vanilla javascript with state flowing hither and thither until I'm out of coffee
2. I test the exact behaviors(s) I wanted to make possible in the GUI I just wrote.
3. The framework whitelists only the event chains from my test.
4. For any blacklisted event chains, the user gets a Youtube video screencast of the whitelisted test so they can learn the correct usage of my GUI.
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.
Haskell and ML make up one of the major language families. They are more like each other than they are like other languages. Inspired by the lambda calculus. Strong static typing with type inference. A succinct math-like syntax that emphasizes pattern matching.
Haskell goes further with syntactic sugar and tries to be almost equation-like:
a = 5
f = \x -> x + 1
g x = x + 2
f $ g a
SML: val a = 5
val f = fn x => x + 1
fun g x = x + 2
f (g a)
But F# like Haskell makes no distinction between values and functions: let a = 5
let f = fun x -> x + 1
let g x = x + 2
f (g a)
The use of indent-based blocks is another Haskell-ish influence on F#. But now we're awfully close to bikeshedding.