54 points · by snaky · 10y ago · 74 comments
On the other hand I don't think it's actually very relevant. Category theory in the context of haskell is a source of ideas, not a tool for formal proofs. Stream fusion and related constructions such as free monads are wrong if you take seq and undefined into account (and don't even start with actual impure features such as unsafePerformIO). So the "real" category Hask would tell you that these constructions don't exist.
The category that people seem to think of as Hask seems to consist of the total functions modulo the obvious observational equivalence. This is very well behaved and I'd argue that it's a great source of ideas for practical programming patterns.
My thought as well.
It seems Haskell gets criticism from both ends of the spectrum: it's either too academic to be practical or too practical to be academic.
EDIT: Actually I want to restate my criticism. The blog post does outline a useful programme of research but it is unfortunately overshadowed by Andrej's grandstanding, which is a great shame.
How about “its abstractions leak in ways the community doesn't want to address”?
And “too practical to be academic” is bullshit. There's nothing more practical than an elegant language whose actual semantics matches the way you think.
To get both the good parts of strictness and the good parts of laziness, you need call-by-push-value: http://www.cs.bham.ac.uk/~pbl/cbpv.html. But, if that's overkill, it's easier to get the benefits of laziness in a primarily strict language, than the other way around.
He says that the more fruitful connection between Haskell and category theory goes in the reverse direction. Rather than trying to define a category Hask, which would contain all Haskell programs including exceptions, "undefined", out of memory, and so on, and then see if there are any general theorems in category theory which would help us understand Haskell, we should do it the other way around. Look at a particular small Haskell program, consider all the different categories it could be interpreted in, and see if this gives us a better understanding of category theory!
1. Only strict functions (f ⊥ = ⊥)
2. Only total functions (x ≠ ⊥ -> f x ≠ ⊥)
But otherwise, the notion that Hask is not a category is nothing new.
I'm not entirely sure. Read the official Haskell page on the subject: https://wiki.haskell.org/Hask
> In order to make Hask a category, we define two functions f and g as the same morphism if f x = g x for all x. Thus undef1 and undef2 are different values, but the same morphism in Hask.
If I understand the passage correctly, they are defining the morphism in a way that makes Hask a category. I think the author of the original post interpreted it the same way.
Which is exactly what is addressed by the wiki page... you get different behavior when you use "seq" on two different things which are the same morphism. The wiki page even uses almost the same example.
While this is definitely a problem in some Haskell code bases, you will also see your fair share of Haskell code which is closer to purely functional code or closer to straightforward imperative code.
I can tolerate abstraction leaks when the cost of using the non-leaky version is prohibitive. For example, I wouldn't want a language that constantly reminds me that memory allocation can fail. “Memory is infinite” is just too nice an abstraction, and I'm not going to let the fact it isn't true get in the way.
But I can't live with a mismatch between the language's intended and actual semantics. If the Haskell language, libraries and community promise me that Hask is morally the category of sets, then it better be morally the category of sets! Of course, this promise is impossible to fulfill in a language without a typefully delimited terminating subset, so Haskellers better scale down their ambitions to something actually realizable.
> I think the problem you are talking about is the fact that Haskell code tends to have lots of abstractions built on top of other abstractions.
No, I don't mind high abstraction towers - I actually like them, in fact! What I hate is climbing them down because some abstraction leaked somewhere.
> you will also see your fair share of Haskell code which is closer to purely functional code or closer to straightforward imperative code.
Since when is imperative code straightforward? Reasoning about purely functional programs: high school algebra, plus induction over datatypes. Reasoning about imperative programs: Hoare logic. The facts speak for themselves.
In any case, this has nothing to do with functional vs. imperative. It's just “please don't impose cognitive dissonance on me!”
Can you give an example?
Do we, as Haskell programmers, need to care? No. If you want to learn Haskell, forget Category theory, until you want to learn the theoretical underpinnings of it. Focus on learning how Haskell ACTUALLY works, not how mathematicians want it to work.
In short, learning category theory will likely help you learn Haskell about as much as learning lambda calculus will help you learn Lisp. Maybe a bit less.
Yes please. Please Don't Learn Category Theory to Learn Haskell: http://jozefg.bitbucket.org/posts/2013-10-14-please-dont-lea...
> learning category theory will likely help you learn Haskell about as much as learning lambda calculus will help you learn Lisp. Maybe a bit less
Much, much less, I would have said
Category Theory isn't very useful for learning the theoretical underpinnings of the language itself.
But sadly this paper is pulled out all the time by people who I suspect don't understand what is going on, to justify sleight of hand & dodgy reasoning which has little to do with the result in the paper. (Not claiming that's what's happening in the above comment! But I bristle a bit when I see this paper come up in amateur/Haskell circles.)
One possible fix is to read the call-by-push-value papers: http://www.cs.bham.ac.uk/~pbl/cbpv.html. The danger is that they might want to switch to a strict language.
Or some of us live in the real world where we get work done with useful but imperfect tools, and are pretty aware of where our abstractions run risk of breaking down in practice.
* Need to enable a few extensions.
EDIT: Also, I forgot, I haven't actually seen Haskellers use ImpredicativeTypes much. Are you really sure that the predicative subset of System F (just RankN types) has no set models?