The Meaning of Monad in MonadTrans
parsonsmatt.org
parsonsmatt.org
* You have monads in your language:
- Optional, Future, Promise, List, Observable, etc.
* Your language's type system can't refer to them generally. This is why you don't see the word Monad in your language. If it could, you'd see: - Optional<A> implements Monad<A>
- Future<A> implements Monad<A>
- etc.
* Why is this important? It means you get to reuse the same (familiar!) machinery even when faced with a new library: - Turning a List<Future<A>> into a Future<List<A>> uses the same few standard library functions as:
- turning a Vector<Arbitrary<A>> into an Arbitrary<Vector<A>>.
* So what's the limitation of Monads? They only compose with their own concrete type: - Optional compose with Optionals.
- Futures compose with Futures.
- What if you want both, e.g. a FutureOptional<A>?
* Monad Transformers let you stack different types of Monads on top of each other, e.g: - FutureT <Optional, A>
- OptionalT <Future, A>
- EitherT <L, ReaderT<E, State <S>>, R>
* This means you can write functions which use features from any of those monads, but importantly, you can reuse any code written for only one of those monads: - An Optional<A> cannot become a Future<A>, but
- both Optional<A> and Future<A> can trivially become FutureT <Optional, A>>
- This is called lifting.In a language like Java (and presumably many others) a generic type argument is implicitly universally quantified. Optional<A> implementing Monad<A> would therefore imply that Optional<B> implements Monad<B>.
The problem for why you can't define "monad" as an interface in Java is because you can't refer to the type constructor (due to the lack of HKT, as you yourself explained). That means that you can't even define "map" generically. You can define it for Optional, or List:
class Optional<A> {
...
Optional<B> map(Function<A, B> f) { ... }
...
}
class List<A> {
...
List<B> map(Function<A, B> f) { ... }
}
but you can't extract map into a Monad interface because you'd have to write the return type as "T<B>" or something and that's not possible (for a non-concrete T).I think I’d want write out the function for a transformation from a list of futures to a future containing a list, give it a name, and explain what it does. This is waiting for all the futures to settle, right? And you have to explain what happens when any of them fails.
And indeed, this is a standard library function [1].
Deriving it instead of naming and explaining it will make it harder to understand any code using the derivation because you can’t look up what Promise.all does. If the code and explanation is written in terms of Arbitrary instead of Future then the explanation will be difficult to write and difficult to read. Defining it for an arbitrary datatype instead of a List would complicate the explanation too.
The lesson of monad tutorials is that there are some levels of abstraction that are usually not worth it because they’re too hard to explain. Explaining how async works is difficult enough.
[1] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
(But if you use these abstractions extensively, maybe Haskell is for you?)
To the contrary! Because I’ve already used that function (traverse) a million times and know exactly how to write it myself I understand exactly what it does without any knowledge about the details of your Future or Optional or whatever. True and meaningful abstraction. Which is truly an amazing super power!
Then this becomes a question of what sort of common language we should expect of programmers. That's always going to depend on context.
Here are some people trying and mostly failing to explain what 'traverse' does:
https://stackoverflow.com/questions/7460809/can-someone-expl...
It honestly provides so much for free when using a new library.
Learning Haskell has helped me to cut a good amount of BS from technologies (and engineers too). Haskell needs Monads, Transformers and more things that I don't understand yet because sometimes operations need to be sequenced. And on top of it because it's not possible to perform any non-deterministic (IO) action without telling the world about it, as any non-deterministic behaviour is wrapped in IO.
I also find it interesting that Haskell feels lot more polymorphic that every statically typed OO language I know, when polymorphism is one of the pillars of OO.
That said, if a feature is really hard to figure out and it’s a constant pain point for newcomers … maybe it isn’t worth it …
No other language seem to bother with monads to the extent Haskell does.
Haskell is not a discovery of something that already existed. It isn't the only programming language out there. There are dozens of examples of other languages that people use more easily. The problem was something invented, not something that has to do with the underlying principles of programming.
If there are easier ways of teaching calculus, maybe those should be used. Actually that is happening now as youtube videos and web pages help visualize concepts.
If most people are confused why they're in a programming language, maybe it's time to admit that a language is influential but not made for general purpose productivity.
Quite sure a lot of people hate C++'s template programming.
> They’re simple enough that they can be explained in a short article, so that causes people to write lots of articles about them.
Or ... there are a lot of articles because people keep finding new ones to read because they still don't get it after reading the previous 100 articles.
The extremes of template meta programming are self imposed by people being fancy while writing libraries and the value is questionable. On top of that no one thinks that C++ templates are the ideal way to do complex meta programming.
That's a bit of an understatement, seeing as it's... I mean is it even possible to write a non-trivial application without accidentally implementing a monad?
Most people write monads all the time, and then their head explodes when someone calls it by its name.
I agree. It’s very important.
It wasn't invented as a set of restrictions, it was just just discovered and formalized.
That’s a bit too casual of a dismissal of an active, major branch of philosophy of mathematics which holds that all mathematics is invented. For fun, and what I take as evidence supporting that argument, look up Michael Penn on YouTube to see how nonstandard analysis works and how you can define the operations of calculus in vastly different ways than you might have learned in school.
The main difference between calculus and monads is that the former traditionally belongs to the domain of analysis and the latter to algebra. That monads haven’t taken off to the same degree is a matter of luck. The vast majority of mathematics has found no use whatsoever outside of pure mathematics.
Finally, I want to say that I think it’s a mistake to think of monads as some set of restrictions that Haskell’s designers put in place to keep people out, like some twisted “you must be this tall to ride the rollercoaster” gatekeeping. They discovered an algebraic pattern in a bunch of commonly used data types and decided to make it into a type class. For them, it solved a major problem of the language at the time: how to deal with effects in a language that uses lazy evaluation.
This is the problem. You keep talking about "philosophy of mathematics" and I'm talking about programming.
Finally, I want to say that I think it’s a mistake to think of monads as some set of restrictions that Haskell’s designers put in place to keep people out, like some twisted “you must be this tall to ride the rollercoaster” gatekeeping.
I don't know if anyone thinks that. It just isn't useful to write programs with an arbitrary albatross around your neck because someone else is obsessed with philosophy and set theory.
You made a specific claim that calculus was discovered but that monads are invented. Are you walking back that claim? Fine.
It just isn't useful to write programs with an arbitrary albatross around your neck because someone else is obsessed with philosophy and set theory.
I've tried to engage with you in good faith but now it appears that you're just concern trolling. The article is an educational piece about monad transformers. Nowhere is it implied that you, specifically, need to use Haskell in your work. If you can't see why monads are useful, why not ask about that?
No I didn't, I said haskell was invented.
Are you walking back that claim? Fine.
Quote me where I said that.
I've tried to engage with you in good faith
You haven't engaged in the thing that matters: programming. You went off on tangents about philosophy and monads and set theory and pure mathematics, when my whole point is that they don't help the vast majority of programmers write programs.
Nowhere is it implied that you, specifically, need to use Haskell in your work.
I don't. I tried it and it was interesting but not useful.
If you can't see why monads are useful, why not ask about that?
My point is about the larger issue that after 30 years, people are still trying to explain step one of this language. In that time entire other languages have exploded and withered. At some point the expectations that this is healthy needs to be looked at. I like that haskell exists, but it isn't overall a good tool for making software.
Haskell doesn't impose monads on a more fundamental notion of computation that other languages expose directly, which is how I interpreted your comment; rather, other languages force a particular monad on you at the language level, whereas Haskell lets you choose your monad and even mix and match different monads that suit your program. This does introduce some complexity in exchange for the additional flexibility, and people may reasonably differ on whether that flexibility is worth it, but the complexity results from exposing something more ‘fundamental’, according to our current theories of computer science, that other languages hide from you.
You say that, but they only seem to come up when dealing with haskell.
This is similar to structured loops and function calls (not GOTO), which are in computations that we care about, regardless of the implementing programming languages. Some languages do not name and reason about them (e.g., Assembly, Turing Machines).
While it is possible to program without raising the level of discourse, this is discouraged (considered harmful), as it lacks safety, hurts productivity (developer velocity/experience), and scales worse.
> > when you start considering effectful computation at a deep level, monads come up pretty quickly
> You say that, but they only seem to come up when dealing with haskell.
"when you start considering structured computation at a deep level, loops and function calls come up pretty quickly"
"You say that, but structured loops only seem to come up when dealing with high level languages."
This situation, where a higher level programming language delivers more power (in this case, scales with better developer velocity/experience) but a lower level programmer resists, has been observed many times. Paul Graham calls this the Blub Paradox [1].
What's so great about Lisp? And if Lisp is so great, why doesn't everyone use it?...
I'll begin with a shockingly controversial statement: programming languages vary in power.
... But when our hypothetical Blub programmer looks in the other direction, up the power continuum, he doesn't realize he's looking up. What he sees are merely weird languages. He probably considers them about equivalent in power to Blub, but with all this other hairy stuff thrown in as well. Blub is good enough for him, because he thinks in Blub.
And using a powerful high level language is "The Secret Weapon" behind the success of Viaweb [1]. Haskell is also a secret weapon for those who know how to use it.[1]: http://www.paulgraham.com/avg.html "Beating the Averages"
our hypothetical Blub programmer
This is all patronizing rationalization to believe that people aren't picking someone else's favorite language because they "just aren't smart enough to get it".
Programming is hard enough without constantly trying to make languages themselves a silver bullet. Fancy macros, fance meta programming, 'pure mathematics' etc. never equals productivity over the long term. Library writers can get away with it, but simplicity wins overall because people can focus on making programs and making tools surrounding the language.
Lisp and Haskell were influential, but that doesn't make them good tools by modern standards. They had ideas that made it into other programming languages and that's enough. They did their jobs. Haskell has major programs with controlling order of execution because it pretends it can be abstracted away.
"The Secret Weapon" behind the success of Viaweb
Haskell users always talk about the same handful of programs that no one would have heard about if they weren't made in haskell.
Meanwhile 90% of software that people actually use is basically made in C++, python and javascript. The languages aren't perfect, but when people sit down to program they can move past the language and actually write software.
You share good company with this observation. Philip Wadler (who put the monads into Haskell) often talks about functional languages having been discovered, rather than invented.
Haskell is System F (The polymorphic lambda calculus) with the Hindley-Milner type system. (Both Hindley and Milner independently discovered it.)
Curry and Howard observed that this type system corresponds to 1st order logic - types are propositions and programs are proofs, e.g.
modus ponens:
P implies Q
P is true
Therefore Q must also be true.
H-M 'App' rule:
f has type P -> Q
x has type P
Therefore f(x) has type Q.
You don't have to dig far into Haskell to find the 'discovered' stuff.The Haskell of today is not the Haskell of 1998. It evolves constantly. More than any other general purpose language dares to. Some people don't like that. Some do. It's fun to be on the bleeding edge, for me at least.
I simply wouldn't program if I couldn't use Haskell. I'd maybe clock in and roll my face on the keyboard enough to get paid, but I wouldn't allow my brain to be molded by other languages, lest I become dumb.
Python used to be good for this but it's had somewhat of a complexity explosion. What's the best now? Scheme?
I may start to look into it as some point for curiosity, but it would be nice if i'd knew that it would be at least a bit useful somewhere.
On a more personal note, learning Haskell is more like learning FP and basic type theory, both of which are transferrable skills to most languages. How "useful" is debatable. Maps and filters you can use almost everywhere, ADTs are lacking in most languages, going into things like optics or type lambdas engineers will start feeling you are crazy but that's not even scratching the surface of PL research
[This is a half-joking note. Many real-world programs are written in Haskell and OCaml, but they really are the minority]
Native support for ADTs is lacking, but most languages can cobble them together out of OO constructs, and they're so useful that it's often worth the inconvenience.
Hiring was actually Haskell’s strong side. I managed to hire 6 engineers in only one month. There were more than 50 applicants to the position. Just the idea of using Haskell in production is attractive to many.
It’s been a great experience overall. I worked at a python company before this one and it’s been refreshing not having unexpected runtime errors. Moving money around requires more robustness than your typical app.
https://discourse.haskell.org/t/hasura-migrating-to-rust/662...
Some monads are also handy for structuring certain kind of code---for example, the non-determinism monad can be handy for writing searches, etc.
I was recently writing a Unger-parser-like natural language parser and I wanted to compose functions that took the parse results from one parse function and then parse something else. I noticed that every time I wanted to pass the results to another function I had to do something with the leftover tokens and also coallate the error messages that might have been building up. What if I used monads to automatically handle the resulting tokens and error messages each time I reached for another parse function? I wrote a monadic binding function that handled the tokens and error messages for each parse function, and then I was able to happily compose my parsing functions in a much more clean and concise way.
Monads are great anytime you want to do something more along with your function composition other than just passing one piece of data directly from one function to another.
"And then you say... What if I used a different kind of composition? Also function composition, but with some kind of flair, something additional. If I redefine composition, I have this one additional degree of freedom in defining composition. If I use this additional degree of freedom, I am using a monad." - Bartosz Milewski [1]
Various monads basically help you manage things like configuration parameters that affect your computations. Your code can all be deterministic, and yet using a monad can help you write cleaner code.
Just because the IO monad is used for impure actions doesn't mean that monads in general are used for impure actions.
[0] There are a few builtins that have side-effects: `input` and `inputs` (which consume inputs), and then there's `stderr` and `debug` (which emit output to `stderr`). Other than this, every jq program represents a pure transformation of its input.