4 ms·
This is a nice explanation about how category theory relates to programming, but it doesn't explain how mathematical rigor helps us in this area. Making a rough
by skybrian 14y ago
This is a nice explanation about how category theory relates to programming, but it doesn't explain how mathematical rigor helps us in this area. Making a rough analogy with function composition and unix pipes seems sufficient to get the point across to most programmers. I suppose you could actually write generic code that works with more than one category but that seems like unnecessary and unhelpful abstraction, compared to just writing the code twice in more concrete terms.
- tikhonj 14y agoThere are several advantages to writing code at that level of abstraction. It's a great way to encourage code reuse, even in fairly disparate domains. Haskell has some of the most versatile libraries of any language I've used by using mathematical abstractions like this. As a concrete example, there are functions like foldM which is like a normal fold except it can handle errors, state, nondeterminism or even parsing! The abstractions are well-understood, which makes it easier to reason about. You get a bunch of theorems and conclusions for free just by formulating your program in mathematical terms. You can then easily take advantage of these to simplify, verify or optimize your code. For example, if you know that your data type forms a valid functor, you can always rewrite fmap a . fmap b as fmap (a . b), turning two passes into one! If you wrote a map-like function separately for each different type, taking advantage of this pattern would be more difficult. You are also restricted in what you can do--any code that is generic across all categories can only use composition. This leaves you less room to make mistakes. If you write a function just for strings, you can accidentally add characters or always return an empty string or any number of string-specific behaviors. If you write a function against some abstraction like a functor, you cannot make any of those mistakes--there is simply no way to formulate them for all functors. These abstractions are just a reification of a common pattern across a bunch of domains. Making the pattern explicit makes it easier to see and use and makes your code simpler.
- skybrian 14y agoThis sounds promising, but it's far too abstract to be convincing. Can you give a example of a useful program where you need to do any of these things?
- tome 14y agoPretty much any non-trivial Haskell program will take advantage of things like this.
- freyrs3 14y agoSure, look at most Haskell libraries. A lot of them expose some of their APIs as monads, arrows, functors, and kleisli categories. Some good examples: hakyll, Pandoc, pipes, conduits, warp, scotty, or anything by Edward Kmett ( https://github.com/ekmett/ https://github.com/ekmett/ )
- skybrian 14y agoThanks, but that's far too much to take in at once. How about just one simple example? I did skim through them, so to be specific: hakyll, Pandoc, warp, and scotty are what I'd call practical programs, though they approximate more popular programs written in other languages, so there's the question of why someone who wasn't already a Haskell programmer should choose them. Pipes, conduits, and most of the stuff by Kmett appear to be libraries implementing other abstractions that themselves would have be justified by use in a practical program.
- freyrs3 14y ago> Pipes, conduits, and most of the stuff by Kmett appear to be libraries implementing other abstractions that themselves would have be justified by use in a practical program. I'm not sure I understand what you mean by "practical programming". This may just be a cultural difference but one of the big ideas of Haskell is using a library whose "core calculus" is provably correct and then combining them in ways that preserve that correctness. So I would argue that the justification of the libraries is not in the practical ( if I understand your usage of the word?) usage but in the thoroughness and compositional semantics that they provide. That's where the category theory inspired patterns ( zippers, monads, arrows, etc ) become important.
- skybrian 14y ago