Re:Haskell is not category theory
by hackandthink on Sunday February 05, 2023 @09:30AM
(#34665172)
"you don't have a function a: X x Y -> Z, you've got a function a: X -> (Y => Z)"
Haskell has products. What's the problem with f':
ghci> f x y = x + y
ghci> f' (x,y) = x + y
ghci> f 1 2
3
ghci> f' (1,2)
3
ghci> :t f'
f' :: Num a => (a, a) -> a
ghci> :t f
f :: Num a => a -> a -> a
Say you have a multivariate function a: X x Y -> Z, and a functor F. This would give you a function F(a): F(X x Y) -> F(Z). Now this is not really multivariate any more as F(X x Y) is not a product, but products behave quite well in category theory so this is not usually that big a problem, and it turns out that quite a few functors are 'continuous' which means they preserve 'limits' and a product is one such limit, in which case F(X) x F(Y) ~= F(X x Y). In particular all right adjoint functors have this property, which is why adjointness is such an important concept.
Now in Haskell things get slightly more difficult, because you don't have a function a: X x Y -> Z, you've got a function a: X -> (Y => Z). Where Y => Z is an object representing all maps from Y to Z (such an object may or may not exist for any particular category). And to use currying you need something like (X * Y) -> Z and X -> (Y => Z) to be isomorphic. So you don't just need F(X x Y) = F(X) x F(Y), which is fairly innocuous, you need to deal with monoidal categories and monoidal functions between them, which is a heck of a lot more complex. This is where the Applicative stuff comes from.
Now some of the stuff you don't get as easily in Haskell is the stuff relying on basic properties of sum and product types, such as
No clue how you'd state that in Haskell. And the following where you can interpret X + Y as an 'Either X Y'.Sure you could probably write a function
but it's not that obvious that it should exist, whereas in category theory it's one of the most obvious constructions imaginable and its existence is clear from the definition. And in Haskell you wouldn't think about using products so the first property is probably barely used anywhere (there's probably a right inverse for zip somewhere: unzip :: [(a,b)] -> ([a],[b]) but Haskell generally doesn't seem to like functions with multiple outputs.