5 ms·
I think that monads are hard for non-functional programmers to grasp for two reasons: They take a different way of thinking, and "monad" is the wrong name. "T
by AnimalMuppet 13y ago
I think that monads are hard for non-functional programmers to grasp for two reasons: They take a different way of thinking, and "monad" is the wrong name.
"The name is wrong?" the Haskell crowd replies. "Not at all! Look at the function signatures, and look at the definition of a monad in abstract algebra! It is a monad!"
Well, any function with the signature a -> a -> a is a magma (per abstract algebra), and those are all over the place in Haskell. But we don't hear much about magmas in Haskell. We do hear a lot about monads. Why? Because monads are magic in Haskell, and magmas are not.
But here's the thing. Monads are magic for reasons that have to do with programming in Haskell, but that really don't have much to do with abstract algebra. That's why it's a bad name. It's like I told you that something was "an enumerable set of bricks" rather than "it's a three bedroom house". "An enumerable set of bricks" is in fact an accurate description, but it is not a useful one.
- thinkpad20 13y agoI agree to an extent, but I think it's a minority opinion. I had a similar point to make about monoids, and would rather see them called "addables" or something similar. Yes, yes, I know, they're not just about adding things... but a great majority of times, `mappend` is used as an adding operation, or something similar (union of sets, concatenating strings, etc). But I didn't receive much support for this view, because "look at the definition of a monoid in abstract algebra! It is a monoid!" It's this kind of thing that makes Haskell so cool, but also makes it so unlikely that it will ever see real widespread adoption. Haskell is a language based heavily on mathematical theory. There's more of a focus on expressing mathematical concepts and hewing to theoretical correctness than on being practical. Why are there no (real) exceptions in Haskell? Because you can do it with monads. Why aren't there member variables on objects? Because you can do it with functions (or better yet, lenses! a shiny new abstraction to learn!). Why is there no stack trace when a runtime error occurs? This one I don't have a good answer to, but I think it has to do with laziness, which has its roots in theory as well. I love Haskell and have written several large projects in it. I write it almost every day, currently in the middle of a project with ~1,500 lines of code written over the last month or so. But a lot of it feels like bending over backwards to allow features that would be completely basic in other languages, because of the thing I was talking about above: the starting point is the theory, and the practical comes after. The names of the types are just the tip of the iceberg on this one. There are many great and practical libraries for Haskell (some of which this post's author has written), and many are incredibly powerful, expressive and performant, so it's wrong to say that Haskell isn't "for the real world". But it's really a whole different philosophy than 90% of other languages. All that said I'm not sure I can come up with a better name for Monad. Context? Pipe? Container? Nothing really springs to mind. It's sort of its own ball of wax.
- tel 13y agoI think Haskell as a community favors more learning over having edges to abstractions. You could call Monoids "Addables" and it would make intuitive sense more quickly, but it would (a) face edge cases later (multiplication is a monoid, as is "take the leftmost argument") and (b) not link to a larger literature of abstract algebra. That turns a lot of people off, as you mention, since they want the names of things in their language to provide immediate (partial) intuition as to what's going on. The upshot is that reading Haskell expects more background literature in abstract algebra (or at least a willingness to read the docs for a while to learn a new vocabulary) but suffers fewer intuition breakdowns. I think that's a general theme of the style of Haskell programming—suffer few intuition breakdowns. It's reinforced by purity, equational reasoning, sophisticated types, top-level annotation style, some parts of laziness, etc. Altogether these pieces fit together to form something unique and interesting. So that tradeoff of immediate understandability is painful. The community is battling it by (increasingly) building more and more public documentation. Without that tradeoff Haskell wouldn't be as unique and specialized, though.
- thinkpad20 13y agoYeah, I agree. Haskell has an almost unmatched capacity for abstraction and high-level thinking. This can be a double-edged sword, but it has made Haskell pretty much in a class of its own, and a very special language.
- aninhumer 13y agoI'm not sure how helpful intuitive names are in practice, especially when the real concept has nuances that it doesn't capture. What do you get by renaming Monoid "Addable" that you don't get from the first line of a tutorial saying "Monoids are things that can be added"? Then, once you've got the intuition, it's much easier to absorb the line further down which says "oh, but they also need to be associative and have a zero element" because you already have a separate name to file these new properties under. It also provides a precise name for further discussion. Also, on the topic of names for Monads, the key difficulty there is that there are at least two distinct ways of thinking about them which can be applied in different cases. Some Monads are best described by their join (List, Maybe etc.) and some are best described by their bind (IO, State etc.).
- tel 13y agoTypes like (a -> a -> a) which are associative show up everywhere and are often named as Semigroups—there's a (somewhat) popular library which provides all kinds of hooks into the abstract algebra in that space. When we also have that (a -> a -> a) has a zero then we call it a Monoid and that typeclass is built-in. In short, there's a continual tension between wanting to reify the entire abstract algebraic hierarchy and the weight and arbitrariness of doing such a thing. To resolve this, the community tends to favor typeclasses which have enough law structure to determine uniquely what the semantics of that algebraic pattern ought to be. For Monad that is absolutely the case. There is wonderful algebraic structure to monads and thus they are named. For Magma... not so much. So you've got an operator, eh?
- monista 13y agoAnother misconseption is that talking about monads people usually mean 'magic' monads, in first place IO, but also State and more, while other monads like Maybe are not so magic, and common things like lists are also monads, though they don't look magic at all.
- nilkn 13y agoI'm not sure I agree with your point about the name. Practically speaking, what should it be called? Most proposals I've seen rely on some sort of analogy which doesn't hold true in general.
- freyrs3 13y agoMagmas aren't mentioned much in Haskell because they don't have much structure to them, it's just a closed binary operation and typically we'd just refer to them as being subsumed by a Semigroup(oid) or something more complicated. Monads on the otherhand are "magical" in some sense because they all this rich structure to them that we can use to model computations of all kinds in a very abstract and useful way. As for the name "monad", it's an arbitrary name that comes froms mathematics but I argue it's as good a term as any until the English language comes up with first-class way of talking about the relationships between natural transformations! The abstract algebra names are great because they force a different mode of thinking than the "nominative adjective" style of naming things in OO languages tends to prefer.
- AnimalMuppet 13y agoFair point. I say that monad is the wrong name, but I don't yet have a better candidate...
- freyrs3 13y agoI understand what you're saying. My point was largely that I don't think there can be a better name, the English language doesn't have terms that describe abstract concepts like monads well. That's why mathematicians invent new arbitrary words that have a precise meaning tied to a very specific set of definitions and rules.
- jfischoff 13y agoThere are no useful laws for magmas. Monads have a fair amount of structure for something so simple.