10 ms·
Computational Category Theory in Python III: Monoids, Groups, and Preorders
- chestervonwinch 7y agoI suggest to the author that they rephrase the title to something like "...Theory in Python Part 3..." to make it clear that this is the third installment in a series, not specific to Python version 3.
- philzook 7y agoInteresting point, that hadn't occurred to me. Luckily that won't be a problem for the other parts. I updated it to III which hopefully should make it a little more clear. The title is so long already!
- pansa2 7y agoYes. I was thinking, “I’ve never seen Python 3 written as Python III before”.
- lr4444lr 7y agoCan someone explain to me what Category theory actually contributes to mathematics beyond the contents of abstract algebra, graph theory, and set theory? Because every interesting result I've seen just looks like it comes from those other branches with some different terminology. Does it actually lead to any unique or synergistic results that the others fail to produce with their own axioma?
- empath75 7y agoWhat is set theory useful for beyond the contents of arithmetic, algebra, calculus, etc... Category theory is useful for studying mathematical structures in general — by its nature, it’s results are going to be fairly abstract and general, but that doesn’t make it useless.
- hyperbovine 7y agoThere's no doubting the important role of category theory in modern mathematics. Less clear to me is why programmers seem so keen on learning category theory. I studied a fair amount of abstract algebra in grad school and never once have I found it useful in my day to day life as a programer. At best, I can say that if you've mastered category theory then you know how to think abstractly and modularize complicated concepts. But there are many less circuitous routes to obtaining those skills that don't involve understanding what a monad is. The moment I'm reading a programming tutorial that starts talking about monads and monoids is the moment I go read some other tutorial that actually teaches a useful skill.
- Tomczik76 7y agoI suggest this article https://www.quantamagazine.org/with-category-theory-mathematics-escapes-from-equality-20191010/ https://www.quantamagazine.org/with-category-theory-mathemat...
- brachi 7y agoThank you. I'm finding it fascinating.
- deleted 7y ago[deleted]
- Koshkin 7y ago“Diagram chasing”, which is the standard method of reasoning in Category Theory, turns out to be very useful in other branches of mathematics, such as topology. (By the way, do not underestimate the power of abstract algebra, it’s widely used in the rest of mathematics, and its applicability to computing is more obvious.)
- contravariant 7y agoAmong other things it contributes a precise definition of the notion of products and sums of algebraic spaces, as well as generalizations and proofs of their basic properties.
- philzook 7y agoFrom a programming perspective I think there are a couple contributions: - Category theory has a number of very intuitive looking graphical notations which nevertheless translate to very exact algebraic expressions. This is pretty dang nice. Category theory is a road to a very principled formulation of things that are already done in dataflow languages, tensor diagrams, and UML and things like that. These graphical languages are a reasonable user facing interface. - Empirically in mathematics, category theory has been found to be a flexible and "simple" language to describe and interconnect very disparate things. In programming terms that means that it is a plausible source of inspiration for consistent interfaces that can do the same. - Point free DSLs are really nice to work with as library/compiler writer. It makes optimizations way easier to recognize and implement correctly. Named references are phantom links attaching different points in a syntax tree and make it hard to manipulate them correctly. In slightly different (and vague) terms, category theory gives a methodology to turn abstract syntax graphs into trees by making all links very explicit. I can't comment much on what category theory contributes to mathematics, but I think there are similar points. I think category theory can make precise analogies between extremely disparate fields like logic, set theory, and linear algebra. I think categorical intuitions lead to clean definitions when specialized to some particular field. Combinatory categorical logic does make some aspects of logic less spooky.
- afpx 7y agoCan you give an example of some of these, because I'd love to learn more.
- askthereception 7y agoIt's more helpful to view category theory as a language. Most undergraduates learn mathematics in the language of sets. If they're doing algebra, then the underlying structures of their groups, rings, etc. are always sets. Same for topology and logic (e.g. classical model theory). An extreme example of this set-theoretic thinking is defining the real numbers in terms of Dedekind cuts. Category theory, very slowly, changes this perspective. If you study this for years, you will come to realise there is more to mathematics than sets. And your way of thinking will shift. You will learn to ask different questions. For example, instead of defining things in terms of their underlying set (often breaking symmetries), you will ask 'does it have a universal property?' and so on. Note, there are still tons of reasons to use sets / ZFC, even when working with categories mostly. But I do not want to get into that and it's irrelevant for the point I'm trying to make. Final note: the 'language of sets', i.e. the language of undergraduate mathematics, is very different from set theory. 'Set theory' is like the language talking about itself. The same goes for pure category theory.
- occamrazor 7y agoVery good point. To add on this, even category theory is often taught in the language of sets and classes (“A category is a class of objects with a set Hom(X,Y) of morphisms for each pair of objects, etc.”) It is possible to use categories as basic building blocks instead if sets but, in my anecdotal experience, this us not what the majority of graduate programs in Mathematics do. It will be interesting to see whether this will change in the next 20 yea.
- spekcular 7y agoThey don't do it for good reason: set theory is basically a strictly better foundation for mathematics, and you have to ape all the set theoretic constructions when doing various things anyway (e.g. constructing the real numbers), so it doesn't buy you anything. This issue has been litigated extensively, and in my view successfully, by Harvey Friedman on the Foundations of Mathematics mailing list, if you want to check its archives.
- jacobolus 7y ago
- WoahNoun 7y agoMany results in algebraic geometry about rational points on curves are impossible to prove without category theory. Here's a pop-sci kind of take on it from quanta that I like. The "bridge" between areas of mathematics they talk about are functors between categories. Also many proofs of the theorems you encounter in an undergrad level abstract algebra course are much easier using categories. Also, just from the tautological stand-point, if you can prove something about multiple types of mathematical objects at once using category theory, then you don't need to prove it for each of those objects individually. https://www.quantamagazine.org/amazing-math-bridge-extended-beyond-fermats-last-theorem-20200406/ https://www.quantamagazine.org/amazing-math-bridge-extended-...
- spekcular 7y agoNone of those results would be impossible to prove without category theory, and all are easily translatable to non-category theoretic language. The use of categories doesn't provide any substantive input, it's just a useful shorthand. (I might have to make an allowance for the use of derived categories here, but I think I could still pull it off by being sufficiently concrete.) Further, none of the proofs from an undergrad abstract algebra are going to be "easier" with categories. (Show me one?) At the end of the day, you have to prove the same propositions. Categories just give a different and sometimes more convenient language.
- WoahNoun 7y agoI'd love to see a proof of Fermat's or Mazur/Tate without using any category theory. I suppose you could define lifts between categories by being explicit about the morphisms and functors but that's just category theory without the name. As for the second part, it's not that one single proof or another is easier with category theory. But the entire presentation is very elegant. Thinking of Aluffi Chapter 0 where he uses universal property extensively to simplify a lot of proofs. My personal favorite presentation of Algebra, but everyone has an opinion here.
- spekcular 7y ago
- mikorym 7y agoCategory theory actually replaces set theory in the chronology of things. So more or less the history of mathematics was something like: Geometry -> Analysis -> Set Theory -> Category Theory Number theorists should not be discounted; they are a separate split after analysis and graph theory is technically more like number theory than category theory. Later on they come back and join category theory again. This is my own opinion and I say it because of the methods that they like to use. You have bridges between them in the form of things like algebraic geometry or functional analysis. Analysis also includes calculus and I would say set theory includes topology. Now, I think your question was a sort of general question about why one should do category theory, but I think the best way is to answer it in two ways. A third point is added as an afterthought. I) Does it lead to synergistic results? Yes, for example Cayley's theorem is an instance of the Yoneda embedding. You can embed any group injectively into the permutation group of the underlying set. But Yoneda is a bunch of other things at the same time too. So by doing the correct generalisation, you solve all the specifications thereof. And then sometimes when people say category theory is foundational, they mean that: Within a special type of category called a topos, you can do all of mathematics. [1] II) Why should we use category theory in computer science? This is maybe the more relevant question for the audience and I would say we aren't using it. So yes, there are these monad like things in functional programming and functional programming has monoids (a monad is a higher dimensional monoid). But they don't use much of the mathematics that Grothendieck invented and also I haven't seen many posts on functional programming that mention Lawvere. My opinion is that we still haven't implemented the real insights of category theory and that functional programming is actually the extension into practice of maybe Church's lambda calculus, not category theory. In the same way that quantum mechanics has waited 50 years to start using category theory I think computer science doesn't use exactly category theory. I guess my point is that there is a difference between ideas percolating down and ideas being applied over. One area where I would like to see more formal methods being used is reactive programming. So for example, why can't your programming language be specified entirely in category theory? But I think the problem is not that category theory cannot be applied, it is that category theory is percolating to other fields; those fields are only glancing at the mathematics; and a lot of mathematicians can't program at all for that matter. I don't say this as a type of lament or apologetically; I think it's just a matter of time and the right commitment. This is my own opinion and I would like to be disproven by top researchers that are experts in both category theory and functional programming. I also think it is that we are not always aware of which work is important until it has become important. And said differently: I think Excel probably has a very interesting reactive framework and I have never seen anything about that posted on HN. So the fact that no-one is mentioning it doesn't mean that it doesn't exist. (Could Excel's reactivity be undocumented?) III) Does it end at category theory? This is not really a question, but more a footnote. I asked my supervisor this once and he answered (rather surprisingly quickly): Probably not, and probably it will look different. For example, a lot of category theorists have not even heard of something like the Langlands Project [2] and the Langlands group seem to also be looking for unifications of theories too. I also asked my supervisor about whether he knew the Langlands project and he just said "No," and he didn't seem to want to say anything else. I have read of lone mathematicians that have their own unique way of approaching mathematics. So it is more a case of: Take what you need, see where you can get. Personally, I would prefer to linger a while with category theory as you can get further with less than in something like traditional analysis (where you have perhaps a thousand rules for integrals). [1] You can do all of mathematics in set theory and you can do set theory in category theory. A topos is like a generalised set theory where functions have taken preference as the route of abstraction. Topos theory is also the reason why I say that classical mathematics includes in it constructive mathematics, but that is another topic that seems to have a somewhat emotive history. The bottom line is that the logic you use to spend your money at the supermarket is not going to change when someone discovers something new in mathematics. [2] https://en.wikipedia.org/wiki/Langlands_program https://en.wikipedia.org/wiki/Langlands_program
- eli_gottlieb 7y agoEvery branch of mathematics formalizes and studies some underlying phenomena. For example, in calculus, it's rates of change: their division into instantaneous changes (derivatives) and their accumulation into changes over time or space (integrals). In topology, it's properties like continuity and connectedness. Of course, all mathematics is abstraction, and all abstractions are leaky. Calculus just chooses not to study non-differentiable and non-integrable functions. Topology chooses not to study discontinuous transformations, and abstracts away the spatial metric. In category theory, the property under study is compositionality: the property that given a path from A->B, and another from B->C, you have a path A->C. Of course, anyone who's ever written a computer program knows that this is not as guaranteed as it sounds. All abstractions are leaky, and if your "paths" are functions, in the programming sense, then the second one could throw an exception, leaving you high and dry with the intuitive transitive property being straight-up wrong. Likewise, if your "paths" are mathematical functions, composition can preserve continuity (f . g for continuous f and g is continuous), but if either one is discontinuous, the result will fail to be continuous. So category theory abstracts away the "exceptions" and "discontinuities" by ruling them illegal: in a category of topological spaces and continuous functions, everything, by construction, preserves continuity. We thus have an entire field of mathematics devoted to asking and answering the question: "what sorts of properties are preserved under transitive composition in what sorts of settings?"
- spekcular 7y agoYou have basically the right idea, despite what some category theory boosters on this site will say. It's a very useful language for algebraic topology, algebraic geometry, and related fields, to the point where working in those subjects without its terminology would be exceedingly clumsy. Otherwise, its value is inversely proportional to your distance to those areas, more or less.
- kmill 7y agoIt makes certain kinds of representation theory easier to deal with. Rather than just group homomorphisms to automorphisms of a vector space (or ring homomorphisms to endomorphisms of a vector space) you can more generally study functors from a category to Vect, the category of vector spaces. This point of view has proven useful in the study of knot invariants like the Jones polynomial. In this case, there is a category of framed tangles and a functor to something called the representation category of quantum sl2, which is a fancy kind of category of vector spaces.
- heavenlyblue 7y agoOk, the basic question: what is dom and cod and why does this article spend the time explaining the basic concepts but never gives a definition of the only obviously unknown concept?
- philzook 7y agoSorry, this is the third part of a series. I think I said what those were in the first part but I'm not sure dom and cod are the domain and codomain of a morphism. They are functions that given a morphism will return the object at the head or tail of the morphism. I don't think that these articles are a very good self contained explanation of category theory. At the moment, I've been aiming at a supplement written in concrete python to something more thorough.
- philzook 7y agoFrom a programming persepctive, I could recommend Bartosz Milewski's series https://bartoszmilewski.com/2014/10/28/category-theory-for-programmers-the-preface/ https://bartoszmilewski.com/2014/10/28/category-theory-for-p... RydeHeard and Burstall - http://www.cs.man.ac.uk/~david/categories/book/book.pdf http://www.cs.man.ac.uk/~david/categories/book/book.pdf Milewski, Fong, and Spivak's course - http://brendanfong.com/programmingcats.html http://brendanfong.com/programmingcats.html From a mathematical perspective: MacLane, Awodey's book, Conceptual Mathematics by Lawvere and Schanuel.
- heavenlyblue 7y agoWhat’s the point of using the short names? You could call them domain and codomain and then everyone could Google them
- tom_mellior 7y agoMore generally, I think all the naming could be improved. Why not write out "domain" and "codomain"? Why abbreviate "identity" as "idd", is that standard anywhere? Why use "mplus" and "mzero"? The latter is as in Haskell, I believe, but there they must do it that way because they can't overload the identifiers. Inside Python classes you don't have that restriction.
- philzook 7y agoHey, author here! Neato! I'm on HN!
- carapace 7y agoI haven't read all the way through yet but this seems pretty great. Some rough edges, sure, but it's accessible, fairly comprehensive, and concise. Have you seen "Multi-Level Languages are Generalized Arrows" by Adam Megacz? https://arxiv.org/abs/1007.2885 https://arxiv.org/abs/1007.2885
- philzook 7y agoThanks! I haven't seen this one before. I'll check it out!
- heavenlyblue 7y agoThis article doesn’t properly use self within python classes. On top of that the author couldn’t even apply a code formatter to the code so there’s a bunch of missing/extra spaces. It also doesn’t even try to make the given code applicable to real problems. It seems as if someone writing an article of how to use functions.partial while never mentioning when those partials could be practically used. All in all, 0 respect for the reader; seems like a badly typed retelling of a university course.
- yowlingcat 7y agoNot a bad attempt at all, but it doesn't showcase how expressive things could be. What would be really cool (and arguably Pythonic) could would be to use Python's operator overloading to what these abstract entities would look like, in the same way that the the builtin `set` is used.
- seemslegit 7y agoOne day I might read something that will undo my conviction that category theory is the mathematical equivalent of literary critical theory. But alas, it is not this day.
- dr_kiszonka 7y ago(I realize that I am probably too late to get an answer.) Besides Category Theory, what other topics are worth reading about to broaden one's CS background? I write a lot of code without formal CS training so I was hoping to expose myself to interesting, preferably "mind-blowing" topics without having to take a course or go through the whole SICP.
- philzook 7y agoAuthor here. I don't recommend category theory. There are lower hanging more mind blowing fruit. I'm having a hard time thinking about a blog posts worth of content that is mind blowing. Mind blows tend to take more investment than that. I'd recommend checking out an algorithms course https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-006-introduction-to-algorithms-fall-2011/ https://ocw.mit.edu/courses/electrical-engineering-and-compu... This one is very good. Has free videos. I'd recommend giving functional programming a try. I'm an FP fiend. I found SICP tough going though. I'd recommend giving low level programming a try. Learning prolog is pretty mind blowing http://www.learnprolognow.org/ http://www.learnprolognow.org/ Digital Circuitry is pretty mind blowing https://www.nand2tetris.org/ https://www.nand2tetris.org/ Making interpreters can be pretty mind blowing https://craftinginterpreters.com/contents.html https://craftinginterpreters.com/contents.html