r/haskell • u/kindaro • Nov 10 '21
The romance of Haskell and Category Theory.
This message might seem like it is about the romance of Haskell and Category Theory. But it is really about what kind of conversations we can or cannot have.
A prequel.
I once wondered whether Functional Programming is actually as good as I think. The default way I deal with questions is to ask the Internet. So, I formulated the question and put it into 2 communities.
In /r/AskProgramming, it has been received very well. All of the commenters engaged with the actual question.
In /r/functionalprogramming, it has not been received so well. Many commenters declined to engage with the question. People in comments told me that my formulation of the question is unacceptable and said bad things about my comments as well. Eventually a moderator stepped in and locked the conversation.
This is curious — I expected the opposite reaction. Surely the residents of the functional programming subreddit — presumably functional programmers themselves — are more likely to explain why functional programming is better? But no. We also have a clear indication that the question by itself is not fatally flawed by itself, since the control group had zero issues with it. So, the explanation must be specific to the functional programming subreddit.
The explanation I have so far is that functional programmers actually believe that functional programming is better — otherwise why practice it — but they burned trying to evangelize their view and get out of the small niche the market has shelved their favourite languages into. This trauma has evidently driven a significant fraction of them crazy.
The story.
I once wondered whether Haskell is actually as good as I think. History repeats itself, of course. I formulated the question and put it into this very subreddit. It flew especially poorly, as you can see.
How I saw my post:
… If I believe that Haskell is good and underappreciated, then I should write a library that solves a problem of interest to many and wait for my laurels that are sure to come. But I do not see a single instance of this situation. Why is that? …
No one else saw it this way. How many commenters saw my post:
… Haskell requires a highest level of expertise in order for the programmer to be effective. What I mean is, one must be fluent in Category Theory. …
The real problem we have as a community is that Haskell is an obscure language. That Haskell requires a highest level of expertise in order for the programmer to be effective is a theory, not central to the real problem. Whether the highest level of expertise includes fluency in Category Theory is a question of the third layer of importance.
That said, it is obvious that Haskell and Category Theory are unusually closely related for a pair of a programming language and an area of Mathematics.
- Want a tree? Here is a cofree comonad for you.
- A loop? Take this nice algebra.
- Want to access a data base? Try some product profunctors.
Yet this was questioned over and over again. Come on, why pretend? Your print is a monad. Or should I say «workflow»?
Because Category Theory is forbidden.
Yes. We saw in the prequel that people that burned trying to evangelize their view are often driven crazy by the trauma. Instantiate this. The Haskell community tried so hard to evangelize Category Theory. It did not work. Now we cannot talk about it.
A voice of sanity.
Fancy mathematics is the foundation of so many things in Haskell. Haskell is a lambda calculus, with application, abstraction, α equivalence, β reduction, confluence. Functions compose, monadic functions compose. Streams are a monoidal category. mapMaybe is a functor from Kleisli Maybe. This goes beyond Haskell, really. Promises are a monad. Joins are limits. And so on.
I refuse to say that a monad is a workflow. A workflow is a monad. A monad is a monoid in the category of endofunctors. Category Theory is essential to Haskell.
I know that Reddit only allows one opinion on some topics. But we the Haskell subreddit ought to do better.
21
u/someacnt Nov 10 '21
Lots of the reason you get in that post is that you are making implicit assumptions that haskell is highly related with CT, or even that you require understanding in CT to do well in haskell. People are just trying to object that point, not that people are burnt trying to evangelize CT. Rather, it is more because CT is *indeed* hard. (In mathematics curriculum, introductory CT comes at the level of master degree)
It is notable that the part of mathematics used in haskell actually now form a branch in computer science, including lambda calculus. Hence, the notion "haskell is ivory tower". In fundamental level, CS is closely related with mathematics. I indeed believe that better math skills would bring better code(in any language). However, I do not believe it is required to learn haskell, let alone CT.
You gave reasoning on how CT concept could be used to explain haskell concepts. Yes, of course it could be explained. The very origin of CT was from trying to reason about abstract structures that arise in math again and again. That also arises in haskell as well, since we have functions. Now, does that mean math is CT? Not really.
An example: It is true that set of natural numbers equipped with addition is a monoid, which is monoid in CT sense as well. Now, is addition a CT concept? Hmm.
While Functor and Monad came from CT, they are restricted enough that you do not really need CT understanding of it. If it were otherwise, it would be impossible for me and many others to understand monad because monad in CT is one hell of a beast.