CoRecursive: Coding Stories - Tech Talk: Bartosz Milewski on Category Theory

Episode Date: August 15, 2019

Tech Talks are in-depth technical discussions. Today Adam talks to Bartosz Milewski. He is the author of a famous blog series, lecture series and now book on Category Theory for programmers. The world... of functional programming is rife with terminology imported from abstract algebra and Category Theory. In fact, it may be one of the most valid criticisms of functional programming is the use of Category-Theoretic terminology that can be unwelcoming to newcomers. Category theory can also be a tool to teach us to see software development in a different light and it can teach us to build better software. Bartosz is also just an interesting person, if you haven't heard of him yet, you are in for a treat. Bartosz's Website Blog Series Book Lecture Series https://corecursive.com/035-bartosz-milewski-category-theory/

Transcript
Discussion (0)
Starting point is 00:00:00 It will sort of make you a higher level programmer, you know, it's like being able to lift yourself above your program. It's like, otherwise, you're just like a little ant working in an anthill, right? Hello, this is Adam Gordon-Bell. That was Bartosz Milewski. He is the author of a famous blog series and also lecture series and now book on category theory for programmers. The world of functional programming is rife with terminology imported from category theory. In fact, maybe one of the more valid criticisms of FP is that the use of this category theoretic terminology can be confusing and maybe off-putting for newcomers. Category theory itself, though, can be a tool to teach us to see software development in a different light,
Starting point is 00:00:58 and it can teach us to build better software. Bartosz is also just an interesting person to talk to, so if you haven't heard of him yet, I think you're in for a treat. Bartosz, thanks for joining me on the podcast. People who listen to this, they might hear different terms that come up over the course of episodes like functors and algebraic data types, applicatives, sum types, product types. Like, where do these terms come from? Oh, they come from category theory. And like, what is category theory?
Starting point is 00:01:35 Yeah, category theory, it's a branch of mathematics. It's a very, very abstract branch of mathematics. And really surprisingly, this extremely abstract branch of mathematics. And really surprisingly, this extremely abstract branch of mathematics has applications in programming, which is really a big surprise. I talked to some mathematicians and they were also surprised that it's like, when you are a mathematician and you want to study category theory. Usually, you first go through all other branches of mathematics. You have to be fluent in topology, in algebra, in analysis, all these things, group theory, because they use examples. When they teach category theory, they use examples that are familiar to mathematicians.
Starting point is 00:02:27 So, they use examples from every branch of mathematics. Because it's like a theory that abstracts over all other branches of mathematics. So, what does it have to do with programming? Well, because programming really is mathematics you know whether we know this or accept this or not it really is and it's all about structure and mathematics is all about structure i mean mathematics is just building structures from essentially from nothing. You know, you put some axioms and then you derive stuff from it and so on. But it all has tremendous structure, right? Everything is structured.
Starting point is 00:03:15 There's like this follows from that, you know, this you divide things, you categorize things, you say, you know, well, we have things like groups, we have things like monoids, we have things like measures, topology, open sets, and so on. So you categorize things, you say, like, you know, you have all kinds of sets, but there are some special sets like open sets and so on. And how do you characterize these things? Well, you characterize them by saying what properties they have. So how they interact with other things that you already know or other things of the same type. And this is very much like what we do in programming. We define things and then we describe them by how they interact with other things,
Starting point is 00:04:16 like in object-oriented programming. Okay, I mean, mostly category theory is used in functional programming, but object-oriented programming also has very rigid structure, right? So you do things like data hiding, for instance. What does it mean to hide your data? It means that you want to describe your object not by how it's implemented, but how it interacts with other objects, namely its methods, right? What are messages, like what kind of messages can you send to this object and how it will respond to these messages by sending other messages and so on. So this view of things, you know, that you have objects that interact with each other by sending messages, for instance, that's like the essence of category theory.
Starting point is 00:05:15 A category is just a bunch of objects and arrows between them. That's all. And this is like, this is perfect model for programming so there's an abstract branch of math that deals with objects and arrows and then somebody realized this looks a lot like what we're doing with software development is that kind of the view of it historically speaking no i don't know no but you know i were always, like, computer scientists were always interested in, like, what is programming, really, right? So, there are, like, these two schools of thought. The Turing school of thought that says, well, a computer is just a device that has, like, sort of like a printing head and reader and so on and it just moves the tape and infinite tape and moves it and so this is a very mechanical kind of approach and then there's this very
Starting point is 00:06:14 abstract approach in mathematics like what is computation because normally in math, you define things like functions and you say, well, function is just a value that is related to the argument of the function. So there's argument and there is value, you know, it's like function is defined and this is argument, this is value. Give me an argument, I'll give you a value. It doesn't ask you, you know, it's like, well, but how long will it take you to calculate this value, right? No, it's just there, right? But in computer science, you have to think about, well, if I want value, I'll have to like derive it from the argument somehow. And I have to go through some steps, right? And so I have to decompose this bigger question into smaller question and just answer every single smaller question and then combine
Starting point is 00:07:16 them into one big result, right? So being able to decompose bigger problems into smaller problems and then combine the solutions, that's essentially the description of, well, I don't know, depends on who you are. You will say, oh, that's a description of what I'm doing as a programmer. And the mathematician will say, oh, this is a description of what I'm doing as a mathematician. And, you know, a physicist will say, that's what I'm doing as a physicist and so on. physicist will say that's what i'm doing as a physicist and so on it's like everybody's doing this this is like the essence of all human activity that's interesting it makes sense to me that that's what i do as a programmer like if i'm given some requirements then you know i feel how they might break into like modules and then build those modules and then combine them back. I would have never
Starting point is 00:08:05 thought that that's what a mathematician does, but maybe I don't understand what mathematicians do. Well, when you're a mathematician, you also like divide your work into, okay, I have to prove this theorem first. And in order to prove this theorem, I have to prove this lemma. I have to define like a new maybe space or object in my space and so on. So it's like, yeah. And then if you have like a huge, huge problem, you want to split it into smaller problems. Like if you want to prove Fermat's last theorem, right? It's not just like you think for a moment and then you say oh i got it right no
Starting point is 00:08:46 you say hey you know i have to study first like elliptic curves maybe and there is this theorem in elliptic curves that i have to prove oh and somebody tried to prove it, but they failed because they couldn't prove a certain lemma. Maybe I can do this and so on. So you always split things into smaller pieces, right? And then sometimes when you work in a team, right, you have to split your work into individual tasks and so on. So like everything we humans do is always composable. So what does category theory bring to the table? Yeah, category theory essentially studies all the different ways in which things can be composed and decomposed.
Starting point is 00:09:39 That's like the goal of category theory. You know, it's like it says, says like what is the structure of things what is the structure structure is like what parts something has and how these parts interact right i mean we don't even ask ourselves these questions like what is structure right but but it's a obvious thing right but it is structure is that you can decompose something into smaller pieces and you can describe how they interact. That's structure, right? Otherwise, you have like one huge morass of stuff. Why does category theory seem to, or why is it more used in the world of functional programming?
Starting point is 00:10:20 Why not imperative programming or object-oriented programming? It's mostly because functional programming is much more restricted, less hacky, I would say. I mean, I came from imperative programming. I mean, I programmed in C++ for years. So, you know, I'm familiar with this stuff. I can do that, right? But things in imperative programming are not very well defined. It's like they don't really have
Starting point is 00:10:59 this nice mathematical structure. It's more of an expert system programming like there are rules you know you're not supposed to do that it's mostly like you're not supposed to do things right i get a lot of teaching of of uh imperative programming is don't do that don't do that and you know they slap you over the head you know, like, don't dereference this pointer. OK, you're not supposed to do that. Right. In functional programming, it's more like there is a mathematical structure behind it. And let's just stick to it. Right.
Starting point is 00:11:37 And if you have this mathematical structure, then you don't have to slap people over the head and say don't do that because it's impossible to do certain things right you mentioned before like turing machines versus lambda calculus i think so like no i mean computers really are imperative aren't they in their like execution so why do we need something that models things abstractly when they execute concretely step after step well because there is this other thing on the on the other side of the computer and that's the human right the human programmer okay so programming is not just about the computer it's about the interaction between a human and a computer. And the computer doesn't really care about how structured your code is. Why do we avoid go-tos? It's like computers love go-tos.
Starting point is 00:12:33 Give me go-tos. Yeah, I can execute them. It's like, right? I mean, it's like the processor has one of the basic instructions, jump, right? Yeah. Why don't we jump all is jump, right? Yeah. Why don't we jump all the time, right? So why do we avoid go-tos?
Starting point is 00:12:51 Not because computers don't like go-tos, right? It's because we humans, if we have too many go-tos, we just lose track of stuff and start making mistakes, right? So I don't think this is like the requirement, you know, for the computer architecture or the programming language should reflect the architecture of the computer. I think a computer language should reflect the architecture of the human mind. And the human mind works differently right i mean we have to do this thing with what we call understand things right so the computer doesn't
Starting point is 00:13:35 understand things but we have to right we have to understand things and understanding again it means that we have to like divide problems into smaller things give them names right we give them names like computer doesn't care what names you give like x1 x2 x3 they're just fine right why do we come up with these names for variables that you know it's like factory of lists or something like that right i read some story about how the like a library differs from like an amazon warehouse right so in a library you have like a dewey decimal system where people can locate where books are yeah in the amazon warehouse amazon has a very simple way of organizing things they just put things wherever and just remember where they put them because it's a computer.
Starting point is 00:14:26 They don't need it. Yeah. So, it seems like what you're saying is category theory is for people, not for computers. Is that the idea? Absolutely. Yeah. So, in category theory, there's like arrows and objects. What is that?
Starting point is 00:14:40 What are arrows and objects? Well, that's the wrong kind of question. You're not supposed to look inside objects or arrows. They are just like the basic thing. And I mean, they have certain properties, right? So you describe them not by what they are but how they behave so objects are really you can think of objects as being labels for beginnings and ends of arrows that's all because you have to say arrow goes from a to b right so this is why you need a and b so an arrow can go there. Yeah. So the narrow can connect that. Right. So you have objects as these end points for arrows and you have arrows that can be composed. And this is like, this is
Starting point is 00:15:34 what it's all about, about composition. So if you have an arrow from A to B and an arrow from B to C, then there is automatically an arrow from A to C, which is a composition of these arrows. And that's like the principle of category theory, that things are composable. That's it, we're done. And well, there's one more thing, you know, there has to be an identity arrow for every object, which means, you know, it's an arrow that when you compose with some other arrow, you get back that arrow again. So it's like multiplying by one or adding zero, right? That's an identity arrow.
Starting point is 00:16:13 So that's all the requirements, you know. You have to be able to compose arrows. There has to be a unit arrow for every object, the identity arrow. And also the composition has to be associative so that you don't have to parenthesis. Like, did I first compose these two and then compose it with a third one? Or did I do it in that different, right? So what is an example of a category where that has some meaning?
Starting point is 00:16:41 Well, so the category that we are using as programmers that's the category that underlies programming languages objects in that category are called types and arrows are called functions pure functions this is why it's so nice to talk about functional languages because in functional languages you have these pure functions right so you automatically are in category theory and in fact this is like maybe the best way of defining what you're doing as abstractly as a programmer that because like if you ask somebody what is a type what is a function you know it's it's really well people think of types as sets of values okay so that's one possible approach but sets form a category so it's like to the lowest approximation you can say well it's a category of sets and functions so i can think
Starting point is 00:17:41 about like my method like to string or show it just takes say the the set of things that's the set of integers and maps it to a set of strings it's that kind of the yes and it is the arrow that does that map yeah exactly and you can compose them right i mean you can first say two string and then you can say uppercase right and you have a composition of these two things you can say uppercase to string or something like that that's a new function that's a composition of these two and of course there is an identity function that well we don't even think much about, but it's a function that returns its argument without changing it. Yeah. So the identity on my integer example just returns the same integer. Yeah. Yeah. And so is the value there that category theory gives us sort of a
Starting point is 00:18:38 language to look at things from the outside, like to just look at the types and the mappings? Well, this is just sort of like the beginning because the category has more structure. You can add additional structure to a category or you can discover additional structure in this category. So for instance, in programming, we are dealing with data structures, right? So what are data structures? Again, you know, you have to start with some elementary types, right? And then from these types, you form more complex types. Like how do you define, you know, a structure, struct, right? I mean, you say, say well i'll put an int there and i will put a string there and maybe a boolean right so you are combining things together right so you know you take
Starting point is 00:19:35 several types which are objects in your category and you put them into one bigger type that combines them. And that's called a product in a category theory. Or you do things like creating a data structure in which I either have a string or an integer, like a union type or something. That also can be described in a category theory as a sum type and so on. So you get product types, you get sum types, and you can do algebra on them and, you know, you get algebraic data types.
Starting point is 00:20:11 Yeah, one critique that I sometimes hear about functional programming is that the terminology can be confusing, right? That it can be not friendly to newcomers, that we call it algebraic data types and some types and product types and do you think that's like a valid criticism no no i mean a name is a name you know it's like why should we invent i mean there are some cultures in programming that invent languages and libraries where they come up with weird names that are supposed to be easier to understand than the ones taken from mathematics. But then, you know, it's sort of like blocks you from going back to
Starting point is 00:21:02 mathematics and trying to learn what's the theory behind that, right? So you go to a mathematical paper and they use completely different language and you don't know, well, what is that? I don't know what a sum is, what our product is, right? So I don't know, why use a different language there and here? And anyways, like if I call something, you know, like in object-oriented programming, if I call something an object, right, that's so meaningful, right? Just try to define what you mean by an object in a programming language. Just because you took a word from English language that everybody thinks they understand, that doesn't mean that an object in C++, for instance, is immediately obvious.
Starting point is 00:21:51 Oh, that's an object. Oh, I know what an object is because I learned it when I was an infant, right? No. Yeah, and then when you have objects that are like nouns or like the factory builder you were talking about, like how? Yeah. How is that an object?
Starting point is 00:22:08 Yeah, exactly. People try to give them names like mappable, I think. Okay. So functor is mappable. And monad is bindable. Like, is that really easier to understand? Mappable is not bad. I don't think bindable like is that really easier to understand mappable is not bad i don't think bindable is good monoid could be like combinable maybe combinable yeah there we go see we've already improved things so you mentioned you mentioned um c++ so how did you get here from doing C++ development to writing about category theory?
Starting point is 00:22:50 Well, I was always interested in how do you write good programs, right? How do you make your programs reliable? And that made me interested in the theory behind programming and i was always interested in exploring the the boundaries like what is the hardest thing in c++ that you can think of well i guess um template metaprogramming right yeah template metaprogramming, right? Yeah. Template metaprogramming. So I got into template metaprogramming and it was fascinating because it was so different from regular programming because it's done at compile time. So there is no mutation, right? So how do you program without mutation?
Starting point is 00:23:41 Okay. So I started reading these books about template metaprogramming in c++ and it was really hard to understand and then i found out okay so they actually take all these ideas from functional programming you know some of these people are actually know haskell and they just translate it into c++ and say, I came up with this great thing. So I discovered this and I started my own franchise. It's like, oh, I started blogging about, oh, this is how you can do using template metaprogramming, this fancy thing here and fancy thing there. It's like, oh, okay, okay cool and people were amazed that you can do these
Starting point is 00:24:28 things in c++ right but it was really cheating you know it's like i was taking something from haskell that in haskell is just like a one-liner you know and um and translating it so eventually i i decided to cut the middleman and just go directly to Haskell and see how it works in Haskell. So it's a cheat code. Yeah. It's a way... So understanding functional programming
Starting point is 00:24:53 was like a shortcut for you to be able to understand template metaprogramming. Yes. Being able to think in Haskell but write in C++ was your advantage. Yeah. And then I even started talking to C++ programmers saying that even if you program in C++, it's a good idea to learn some Haskell and maybe use it as pseudocode. You know,
Starting point is 00:25:19 I'm like a lot of people from the C++, they don't like functional programming because of performance. Because it's true. I mean, performance, it depends on what kind of programming you're doing. But if you're doing like, you know, maybe string processing, maybe Haskell would not be the fastest language to do this. And if you start dealing with performance issues, then your Haskell code, I mean, you can optimize stuff, but then your Haskell code becomes uglier, not so clean. The clean code doesn't really perform very well. That's the problem. But, you know, it's like if you solve your problem first in haskell
Starting point is 00:26:06 and then uh you translate it into c++ you will probably get better quality code at the end and better performing and so how did you get from haskell to category theory. Well, because of the language, you know, because they use these terms, the functor, monad, monoid, and so on. So I was curious, where do these terms come from and what's the meaning of that? So I started looking into a mathematical foundation of that, i started looking into mathematical foundation of that trying to understand and then again you know it's like i i now i don't do much programming mostly just testing some ideas mostly testing the types do they work together and so on because even Haskell is too constraining. So there are certain ideas in math that are difficult to translate to Haskell. So it's again like, you know, finding the boundaries of what can be expressed in Haskell
Starting point is 00:27:18 and then going beyond them as category theory. There is a whole area in between, which is dependent types. So dependent types are very interesting. And I'm trying to learn Idris, which is a dependent type language and figure things out. Nice. Yeah. I've had a couple episodes about dependent types. Edwin Brady was on the podcast. Oh, okay. Yeah. Yeah. So, I think what you're saying is you went from writing C++, but thinking about it in Haskell to writing Haskell, but thinking about it in category theory? Yes.
Starting point is 00:28:01 So, is there like a notation or something for category theory? What do your thoughts look like? Is it in a mathematical notation a lot of my thoughts when i do them on paper on whiteboard they are diagrams yeah it's like you know arrows between dots that's a yeah that's how you work in category theory so it's purely visual now your thinking is actually all diagram based yeah and And it's very good for me because this is, you know, like different people have different types of thinking that they are. Some people are better at kind of algebraic thinking where they think in terms of symbols and formulas. I don't do that. I have a problem with that, but I am very visual and I like pictures. I draw lots of pictures and that helps me. Yeah, that's interesting. I don't know. Am I a visual thinker? I think I'm somewhat of like a
Starting point is 00:28:57 oral, like I think I think in like a soundtrack, you know what I mean? So, what's the most successful... What's been a success that's come out of applying category theory to software? I think the whole language Haskell is very solid because it's based on mathematical foundations and category theory in particular. So, I think... Well monads probably monads are like the most successful thing in functional programming because when people started working in haskell it was a purely functional language it is still a purely functional language but with a purely functional language you have this this problem of, well, so how do I print something? It's a side effect.
Starting point is 00:29:51 Okay. How do I get input from a file or from the user? Right. That's not a function. You know, it's like get character, get string. You know, it's not a function because every time you call it it returns a different value possibly right yeah so how do you describe this in terms of categories and so instead of like doing the easy way out like most other languages, you know, functional language. Haskell is probably the only, well,
Starting point is 00:30:25 there are some others, but the strict functional pure language, right? It's like even ML is cheating. So they started thinking, you know, how can we do this without cheating? Right? So that's a really hard problem. And because scientists like hard problems you know it's like unlike engineers who will try to find shortcuts and do it quickly because there is like deadline deadline pending right and their salary depends on it. And scientists are interested in, will there be a publication out of it, right? So if it's a hard problem, then the publication will be good, right? So they figured out this stuff,
Starting point is 00:31:16 and they found out that the way to do this is to use monads. And then once monads came into functional programming, they are now spreading to other languages. Yeah, definitely. I had Phil Wadler as a guest on the podcast who I think he did the implementation of bringing monads to Haskell.
Starting point is 00:31:41 Yes, yes, he did. It was Eugenio Moggi who first introduced monads into computer science, but it was very theoretical. And then Phil Wadler read his paper, talked to him and came up with something that was actually very practical and worked. Yeah. And an interesting thing about Phil was that he was saying that he thinks these mathematical concepts like, blame the calculus specifically, that these are like kind of innate to the universe. Maybe he wouldn't quite say it that way, but he thinks that they are. Yeah, he would. Yes.
Starting point is 00:32:24 I had discussions with him about this you know i totally disagree he's a platonist yeah a lot of mathematicians are platonists they believe that there are just things inherent like mathematics is built into the universe. I totally disagree. So what's your perspective on it? Why do you disagree? I think mathematics is something that is inherent to human beings. You know, that's, this is the way, the only way we can deal with our environment is because we have small brains, you know, it's like compared to the size of the universe, this is just like a tiny, tiny thing. And it evolved from apes that were trying to solve problems
Starting point is 00:33:13 like how to run away from a predator or how to kill an animal and eat it, you know, and start a fire and so on. So the way we deal with complexity is by dividing into smaller tasks, solving them, and then recombining the solutions. And that's what we do with everything. We just don't know how to deal with things that are not decomposable. I mean, one part of life is that, you know, every living being has to have some kind of model of the environment, right? So it has to create a simplified model of the environment. I mean, we humans have this sort of, we can even think about the model as opposed to reality, right?
Starting point is 00:34:07 We have a model of the environment in our brains and we know it's simplified, right? But the fact that you can take the environment, the universe, and simplify it, I mean, throwing away some things and decomposing into smaller things, that's just an amazing thing and people think well isn't that amazing that the universe is decomposable and i think no it's not amazing that the universe i mean it is amazing that this part of the universe that we live in the particular scales like the meter scale is decomposable right but like you can go like a 10 levels down to like micro scales and suddenly things are completely different right you know this is why we don't have life at the Planck scale because it's not decomposable you know at our level of universe things are nicely
Starting point is 00:35:08 decomposable and this is what makes life possible and that's humans yeah i would think that it's like has to do with where we evolved right like we have troubles understanding things at the quantum level because that's just foreign to us right the same way we have a hard time understanding how approaching the speed of light works it's just because humans never existed in a world where they went that fast so it doesn't fit in the model of our brain but humans never existed at atomic scales because it's impossible for life to exist at atomic scales why is it impossible why is it like why can't there be a life on the surface of the nucleus of hydrogen right why well what would it be made out of exactly right right oh i see yeah
Starting point is 00:35:57 just saying made of means decomposable right ah. There's nothing to compose or decompose. I see. Yeah, that makes sense. You had some talk that you sent the link to me where you posit that the world is not round or that the world is flat, I think, wasn't it? Well, it was tongue in cheek. Yeah. Could you explain that point?
Starting point is 00:36:24 Like, how is the world flat? Because people often confuse, well, Platonists will confuse the thing that we understand about the world with what the world is really, right? The ontology of the world. Because the world isn't really a sphere, I guess, right? Yeah, the earth is not a sphere. It's like, if you start arguing that the't really a sphere, I guess, right? Yeah, the earth is not a sphere. It's like if you start arguing that the world is a sphere, you're talking about some kind of ideal of a world, right? A model, right?
Starting point is 00:36:56 So, you can model the earth to some approximation as a sphere. And that's good, you know? But the earth is not a sphere and that's good you know it's like but the earth is not a sphere you have to understand the earth is not a sphere there is a tiny difference you know several kilometers you know in some places right so it's it has to do with your decomposition thought i think right because you're saying like for me to understand what the world looks like, I need to come up with some model and it has to fit inside my head, right? So, it has to get rid of some of the details.
Starting point is 00:37:34 This is what we call abstraction. Abstraction is getting rid of some details and getting a nice model out of it. So, yeah. So, I mean, you can describe the Earth as a sphere to some approximation and you can describe the Earth as flat mathematically. It's okay. You know, it's like there is a system of coordinates in which the Earth is flat, you know.
Starting point is 00:38:01 And it would be much, much harder to work with that system of coordinates because you would constantly have to do adjustments you know it's like you move towards the south pole for instance what we consider south pole you know and like your coordinates would just blow to infinity so this abstraction i think it so at the level of my code, right? Like I just have imperative code that runs on the computer. Like it does jumps and whatever. And then like I have my actual, like maybe I have my functional programming code. It's like a level up, right? It hides some details and then I'm trying to connect this all back. So then I think what you're saying is there's like category theory is like something that gets rid of some of these details even more.
Starting point is 00:38:47 So, it's like if you want to look at the flat, you can assume the world is flat when you're measuring a hundred meter race. Like that's fine, right? But then when you want to zoom out and go to the South Pole, maybe you want to assume it's a sphere. And then like at each level of detail, if you want to do a foundation for your house, like at the lowest level, you can't assume the world is flat. You have to actually know where the bumps and cracks are.
Starting point is 00:39:09 Yeah. So, a category theory is a way to hide some of the details so that we can fit it in our head. So, it's not something, I'm trying to understand your perspective. It's like, it's not something innate to the mathematical underpinnings of the world. It's a way that humans use to fit big, hairy concepts into their heads. Yeah, exactly. So, category theory is a very good description of how our minds work. It has nothing to do, well, maybe that's a harsh statement, but you should not confuse mathematical model with reality. And I hear this very often when, you know, especially talking to mathematicians, they say, you know, it's like, okay, so we have in this category, we have like this category described the world.
Starting point is 00:39:59 And inside this category, we have a model of the world, right? But the world is also a category. It's like, no, the world is not a category. The world is already, I mean, a category is already a model in our brain, right? Yeah. So, there is this problem occurs in mathematics and in physics you know it's like in physics you have quantum physics you know and you have an external observer
Starting point is 00:40:30 always you know it's like I never sat right with me yeah and you know I mean as a physicist you kind of get used to it but never really you know it's like you, that there's something wrong
Starting point is 00:40:46 with quantum mechanics at some level. There's always this classical observer that's observing quantum effects. So it means that like quantum theory is incomplete and every single theory that we have is incomplete. And it's Even true in mathematics, you know, there's an incompleteness theorem. Goodell's incompleteness. So, it's like, everything we do is incomplete. So, why should people learn category theory?
Starting point is 00:41:16 Why should people do anything? I don't know. Is it fun? Is it practical? Is it both? For a programmer, for instance, it doesn't make sense to learn category theory. Will it make you a better programmer? I think it will. It will sort of make you a higher level programmer.
Starting point is 00:41:38 You know, it's like being able to lift yourself above your program. It's like otherwise, you know, you're just like a little ant working in an anthill, right? And the only things you see are the things that are close around you, right? It's like, you're never able to like lift yourself above the anthill and see how it's related to the rest of the world and so on. And category theory provides these ways, these abstractions that otherwise you wouldn't even think of. You know, it connects things.
Starting point is 00:42:17 It says, well, a list is in a way, a list data structure is somehow similar to an optional in some ways. How is it similar? Well, because it's a monad, right? And unless you have this word monad, you know, and the description of what a monad is, you will never see the similarity between a list and optional or well you could just say it's a list of uh maximum one arguments like a list of max size one okay yeah you know good but maybe that would get you towards the concept of a monad i don't know yeah yeah yeah it probably would yeah if it became widely understood as a language where we talk about software development using these terminology like how would that influence
Starting point is 00:43:12 software development it would make us feel smarter because we'd have all these abstract algebra terms to throw around i think it's already changing the way we program i mean the development of programming and programming languages is sort of from goes from bottom up so we start working to solve particular problems right and then then we discover that there are similarities between these problems and that we could simplify our lives instead of redeveloping the same thing over and over and over maybe we can abstract over it right and a lot of programming languages evolve but every programming language really evolves this way that it just adds abstractions on top and And I saw this in C++, you know, I was always really happy when new features came to C++ because like,
Starting point is 00:44:12 oh, I can do more stuff. Right. But they were always like slapped on top of the existing stuff, you know, and it was like this really ugly syntax of templates, right? You know, it's like there were even problems like if you have a template inside a template, then the two greater than signs would collide and the compiler would think this is like, oh, this is the right shift.
Starting point is 00:44:38 You know, it's like, that's crazy, right? It just says, you know, this language was not developed to think at this this high level of abstraction right it was just added to it so there are new languages which let you program more abstractly which means that you can reuse your code that you can write code that will solve different kinds of problems using the same methods. I mean, you know, there's like this whole industry of patterns, right? There is the Gang of Four pattern book and so on. That was a great thing when it came out.
Starting point is 00:45:20 But it showed this tremendous weakness of programming languages that you have to describe these patterns in a book, right? And then ask the programmer, oh, here you have a particular problem, you know, write your code according to this book instead of use this library, right? Because we've solved this problem before, right? So, the gang of four solved a certain bunch of problems, right? But they could not express it in a language. What was a stumbling block that you hit or that people hit when trying to learn about category theory? Oh, the biggest stumbling block
Starting point is 00:46:05 is that mathematicians, they just don't explain things the way that's easy to understand. Let me describe it this way. I don't know. It's like when you read a mathematical paper or a book, you know, it's written in a certain style, you know.
Starting point is 00:46:30 Is it that it's written for mathematicians to understand, but not for programmers? I think it's a culture. It's a culture thing, you know. It's like, there is even, I mean, being a physicist, you know, I see the difference in culture between physics and mathematics. Like in physics, you know, we had this great guy, Feynman, who was like, he would always try to explain things in the simplest terms. And he loved giving talks to outsiders, trying to explain, you know, the quantum field theory, you know, stuff like this. And mathematicians don't do that. They write these abstract papers. And if you don't get it, you know, it's sort of like you're stupid because you don't get it.
Starting point is 00:47:22 You know, you shouldn't be reading this whereas feinman had the opposite like if i can't explain it it means i don't understand it you know if i can't explain it in simple terms i don't understand so and is this why you started your series explaining category theory for programmers yes yes. And because I thought there is this problem that mathematicians will use examples from other branches of mathematics. And so what if you don't know these other branches of mathematics? Does it mean that category theory is useless to you? No, it's not. It's just that, you know, you have to like rewrite category theory using different examples. And there are
Starting point is 00:48:07 plenty of examples in programming that I could use to explain concepts in category theory. And it turns out that these concepts are not that hard if you have the right examples. Yeah, that totally makes sense. It's interesting. Like, I guess one thing I never thought of before I started talking to you was, so there's this field called category theory. My understanding is that somebody noticed a whole bunch of similarities between a whole bunch of branches of math, right? And that's where it came from. But what you're saying is, I think that those similarities aren't to do with math. They're to do with the people who created the math.
Starting point is 00:48:47 Yeah, yes, exactly. And you see these things not only in mathematics, but also in physics, in programming. It's like in every area of human activity, you see the same patterns, the same structures. And so I sometimes am amazed, you know, it's like reading a paper in some area of mathematics and they are using the same ideas that I found in some other area of mathematics. And I'm thinking about, are these the only patterns that we humans are able to discern and we are just describing the same patterns over and over and over again in different contexts does that mean that there's things we'll never figure out because they're not part of our the patterns we can find we have figured some things out and there are so many other things that we haven't figured out. And we always think that like, we already understand like 99% of stuff and there's this 1% missing. And I think this is completely wrong. We understand the 0.001% if it even makes sense. Right. And there is so much stuff missing we just ignore
Starting point is 00:50:07 the stuff that we don't understand do you know what we're ignoring well yeah there are certain things that we know that are sort of beyond the scope of what we can do right now and we think well eventually we'll figure these things out but in physics obviously there is this humongous problem of we have general relativity and gravity on the one side and quantum mechanics on the other side and we just don't know how to connect that and we think well well maybe string theory maybe this maybe that you know it's like and the deeper we go into it, the more we understand that we don't understand stuff. And maybe these things just don't decompose, you know, it's like that's the obvious solution.
Starting point is 00:50:54 Okay. And if they don't decompose, then we can't really describe them. We can't have a model, right? It's like, does everything have to have a model? Can it be simplified? Why should it be? You can always come up with approximates for things, can't you? Why? Like how the globe is...
Starting point is 00:51:13 I don't know. Why? Because this is what's been happening in the history of humankind. But then you can also understand this as like, we are discovering things that are possible for us to understand and we just ignore everything else yeah i don't know a lot about physics but and this will probably get cut from the actual episode but like uh dark matter that doesn't seem to make sense like they're like okay the universe has to have so much mass to it and there's not enough. So let's just postulate something that weighs the rest of it. That seems like cheating, right? Yeah.
Starting point is 00:51:49 I mean, and a lot of people think it is cheating. Yeah. Crazy stuff. Yeah. There's dark matter. There's dark energy. Yeah. Maybe that's the difference between physicists and mathematicians.
Starting point is 00:51:59 I don't know. Well, physicists are very pragmatic, you know, if it works, it works. Okay. If I can calculate something and then test it, then it's great. In this way, physicists are more like programmers. If it works, let's ship it. So, what's next for you after category theory? I'm interested in homotopy type theory, but this is really hard.
Starting point is 00:52:27 Has to do with the quality. I know that much. So thank you so much for joining me. It was a lot of fun. Thank you. Thank you for having me. That was the show. It was so much fun for me to talk to Bartosz
Starting point is 00:52:39 and I hope it gives some context for why category theory is something interesting and something worth looking into. On the Slack channel, user Freemason has been sharing the results of building JavaScript tokenizers and parsers. And he was inspired by the episode we did about building your own interpreter. Also, Mark Weiss and Brandon Brown have been talking about programmatic music creation, which sounds pretty cool. If you like the podcast, make sure you subscribe and also follow the podcast and myself on Twitter. That's at Adam Gordon Bell and at co-recursive. I've been on a couple of different podcasts lately talking about functional programming and containers and distributed teams and a couple other topics. You can find a list of some of those episodes on my
Starting point is 00:53:24 website, which is adamgordonbell.com. Until next time, thank you for listening.

There aren't comments yet for this episode. Click on any sentence in the transcript to leave a comment.