字幕列表 影片播放 列印所有字幕 列印翻譯字幕 列印英文字幕 Alright, welcome to category theory lectures 好的,歡迎收看範疇論的課程 So, are we all programmers here? 所以,在座的都是工程師嗎? Is everyone a programmer, or are there people who are not programmers? 大家都是工程師,或者這邊有誰不是工程師嗎? If you're not a programmer, please raise your hand 如果你不是工程師,請舉起手 Okay, thats means, like, you don't write any programs? > I kind of learn as a hobby 好的,這代表你並不寫程式? > 我只是為了興趣 Okay well that's good enough 好的,這樣夠好了 okay and how many people here have some little bit of knowledge of Haskell, okay 好的,這邊有多少人有些Haskell的經驗 好的,哇 Oh Lots, that's, that's, excellent because I might be giving some examples mostly 噢很多,這真的、真的很棒,因為大多數時候我可能會提供這方面例子 just like you know, declarations, functions or something like that 就像你知道的,宣告、函數之類的 I'll explain everything but it's it's good to have a little bit of 我會解釋一切,不過如果能有些 understanding. So I'm not really teaching a programming language, it will be about 概念是好的。所以我不真的會教一個程式語言,這會是關於 category theory and category theory is like this most abstract, or well maybe 範疇論。範疇論是最抽象的,或 almost the most abstract part of mathematics, right 可能幾乎是數學最抽象的部份 so the question is why are we here? 所以問題是,我們為什麼在這裡? what does it have to do with programming, right? 是啊,這和寫程式有什麼關係呢? and a lot of programmers they hate math 而且很多程式設計師討厭數學 they finished learning math at college and now they 他們在大學修習完數學然後現在他們 are really happy now "for the rest of my life I will not have to touch it" 非常開心。「在我的下半生不需要再碰它了」 "I hate math" and so on, and you guys here come for, I dunno, punishment? 「我討厭數學」之類的,然後你們為了,難道是,被懲罰而來? Why do you think category theory might be important? 為什麼你們會覺得範疇論可能很重要? What do you think, do we do functional programming? 你們怎麼想的,大家用函數式編程嗎? Is category theory only relevant to functional programming or other branches 範疇論是只和函數式編程相關 還是也可能有助於 of programming would maybe profit from it too? 其它寫程式的方式? is there something more to offer? 它能提供更多嗎? Have you heard of functors, who's heard of functors? 你們聽過函子嗎,誰聽過函子? yeah, and who knows what functors are? 好,那誰知道函子是什麼? Uh huh, a few. That's good, that means i can actually explain stuff and you won't be totally 啊哈,有一些。 這很好,這代表我可以真的解釋些概念而你不會完全在 repeating what you already know 重複你已經會的 so I came to category theory through a long long twisted road 我沿著一條崎嶇小徑來到範疇論的世界 ok when I started programming I started programming in assembly language 我從組合語言開始學習程式 the lowest possible level, right, where you actually tell the computer exactly what to do 你在可能的最低階層詳盡的告訴電腦要做什麼 right down to "grab this thing from memory, put it into the register, use it as an 例如「從記憶體拿取這個東西,把它放進暫存器,把它當成 "address and then jump" and so on so this is very precisely telling the computer 位址然後跳轉」之類的。我非常精確地告訴電腦 what to do right this is this is a very imperative way of programming we start 要怎樣做,嗯。這是非常命令式的編程。我們 with this most imperative approach to programming and then sort of we drag 從最命令式的編程方法開始然後我們持續 this this approach to programming throughout our lives right and like we 一直這麼做,直到 have to unlearn at some point. And this approach to programming sort of in 某刻我們必須假裝忘記它。計算機科學中這樣的編程方式 computer science is related to Turing machines. A Turing machine is this kind of 和圖靈機有關。圖靈機是種 very primitive machine, it just stamps stuff on a paper tape, right 非常基本的機器,它只在一個紙帶上打印,是吧。 there is no high-level programming there it's just like this is 沒有高階編程,這就像是 the assembly language "read this number, put it back on tape, erase something from 組合語言: 「讀取這個數字,存回磁帶上,消除 the tape" and so on so this is this one approach towards programming 某些磁帶上的東西」等等。這是一種寫程式的方式 by the way, all these approaches to programming were invented before there were even 順帶一提,這些編程方式甚至在電腦出現之前就被發明了 computers, right and then there's the other approach to programming this came 然後有另一種編程方式,來自於 from mathematics the lambda calculus right, Alonzo Church and these guys 數學,即lambda演算,阿隆佐·邱奇和那些人 and that was like "what is possible to compute, right, 問「什麼是可計算的?」 "thinking about mathematics in terms of 「以東西會怎麼樣被實際被執行,變換,來 "how things can be actually executed in some way, transforming things", right 思考數學。」嗯 so these approaches to programming are not very practical 所以這些程式設計的技巧不是非常的實際 although people write programs in assembly language and it's possible but 雖然人們用組合語言寫程式而這是可行的但 they don't really scale, so this is why we came out with languages that offer higher levels of abstraction, 它們並不真正 是可擴展的,這是為什麼我們發明了提供更高層級抽象的語言 and so the next level abstraction was procedural programming 然後下個層級的抽象是程序式編程 what's characteristic of procedural programming is that you divide a big 程序式編程的特徵是你把一個大問題 problem into procedures and each procedure has its name, has a 分割程很多程序且每個程序都有名字、有 certain number of arguments 一定數量的參數 maybe it returns a value sometimes right 或者它可能傳回一個值,恩 not necessarily, maybe it's just for side effects and so on, but because you 也不一定,它可能只為了副作用之類的,但因為你 chop up your work into smaller pieces and you can like deal with bigger 把你的工作分成小片段 你可以處理更大的 problems right so this this kind of abstracting of things really helped in 問題。這樣的抽象對寫程式真的有幫助 in programming, right and then next people came up with this 然後接下來大家發現 idea of object-oriented programming right and that's even more abstract now you 物件導向這樣的點子。這甚至更抽象 have stuff that you are hiding inside objects and then you can compose these 現在你在物件內藏東西然後可以組合這些 objects right and once you program an object you don't have to look 物件對吧 然後一旦你寫了個物件你不必去探討物件 inside the object you can forget about the implementation right and and and 的內部 你可以忘了物件的實作然後 just look at the surface of the object which is the interface and then you can 只觀察物件的表面,介面 然後你可以 combine these objects without looking inside and you know you have the bigger picture 在不探討物件的內部下組合它們 然後你曉得你對整個程式有更深入了解了 and then you combine them into bigger objects and so you can see that there is 然後你再把它們組成更大的物件。所以你可以看到這裡有 a a certain idea there, right? and so it's a very important idea that if you 有個特定的點子,是吧?這是個非常重要的點子,如果你 want to deal with more complex problems you have to be able to 想處理更大的問題,你必須能 chop the bigger problem into smaller problems, right, 把大問題分割成更小的問題 solve them separately and then combine the solutions together 分別解決後把這些解組合起來 And there is a name for this, this is called composability, right. 這樣的行為有個詞:「可組合性」 So composability is something that really helps us in programming. 可組合性是一個大大地幫助我們寫程式的概念 What else helps us in programming? Abstraction, "abstraction" that comes from 還有什麼也是可以幫助我們寫程式的? 「抽象」。抽象這個詞來自 from a Greek word that means more or less the same as "subtraction", right which 一個或多或少代表「減法」的希臘文字 means "getting rid of details". If you want to hide details, you don't want them or you wanna say 意即「擺脫細節」。如果你想隱藏細節,你不想要它們或者你想說 "these things, they differ in some small details but for me they are the same" 「這些東西,它們在小細節上不同,但對我來說是他們一樣的」 "I don't care about the details" so, an object is in object-oriented programming 「我不在意細節」。所以,在物件導向中物件是 is something that hides the details, abstracts over some details right, and there are even these 用於隱藏細節、抽象於某些細節,甚至有這些 abstract data types that just expose the interface and you're not supposed to 只暴露介面的抽象資料型態而你不應該 know how they are implemented right? 知道它們是怎麼被實作的是吧 so when I first learned object-oriented programming I thought "this is like the best thing since sliced bread" 所以當我初次接觸到物件導向的時候覺得「這是開天闢地以來最棒的東西」 and I was a big proponent of object-oriented programming and together with this idea 然後我那時是個物件導向的擁護者,伴隨著 of abstracting things and and and composing things comes the idea of 抽象這樣點子和、和、和組合東西的是 reusabillity right so if i have these blocks that I have chopped up and 可重複使用性所以如果我有切割好的模塊 implemented, right, maybe I can rearrange them in different ways so once I 而且已被實作,或許我能用不同的方式排列它們 implemented something maybe I will use it in another problem to solve 一旦我實作了某個東西我以後可能會在不同的問題上用他 another problem I will have these building blocks, I will have lots of building 來解決那個問題。我會有這些模塊,我會有很多 blocks that hide their complexity and I will just juggle them and put them 隱藏複雜性的模塊然後我會把它們 in new constellations, right? so it seemed to me like this is really the promise of 揉合成不同形貌,對吧。所以當時我看來這真的是 object-oriented programming, I'm buying it! and I started programming object-oriented way 物件導向達到的承諾,我服了這套!我開始用 C++ 以物件導向的方式寫程式 using C++ and I became pretty good at C++ I think, you know, I wrote a lot of C++ code 然後我的 C++ 變得很強,我想。你知道的,我寫了很多 C++ and well it turns out that there is something wrong with this 但物件導向似乎有些地方不太對勁 object-oriented approach and it became more and more painfully obvious when people started 一旦人們開始撰寫並行和平行的程式,痛苦變得愈來愈明顯 writing concurrent code and parallel code, ok, so concurrency does not mix very well with 好的,所以並行並不能和 object-oriented programming. Why doesn't it? Because objects hide implementation 物件導向相處得很好。為什麼?因為物件導向隱藏了實作 and they hide exactly the wrong thing, which makes them not composable, ok? 而且它們正好隱藏了錯誤的東西,使得他們無法被組合,是吧。 They hide two things that are very important: 它們隱藏了兩件非常重要的事情: They hide mutation – they mutate some state inside, right? And we don't know about it, they hide it 它們隱藏了改寫──它們改寫了某些內部狀態,對吧?而且我們不知道,它們隱藏了它 They don't say "I'm mutating something". 它們並不說它們改寫了什麼 And sharing these pointers right – they share data and they often share data between 還有分享──它們使用指標對吧,他們分享資料而且經常 each other you know between themselves they share data 在彼此之間分享。你知道的,在它們之間分享資料 And mixing, sharing and mutation has a name 然後分享、改變並混合 有個名字 It's called a data race, ok? 它被稱為資料競奪,好嗎? So what the objects in object-oriented programming are abstracting over is the data races 所以物件導向中的物件對資料競奪進行抽象 and you are not supposed to abstract over data races 但你不該這麼做 because then when you start combining these objects you get data races for free, right. 因為一旦你開始組合它們,你就會有資料競奪 and it turns out that for some reason we don't like data races, ok? 而且因為某些原因我們不喜歡資料競奪 and so once you realize that you think 一旦你頓悟後你開始想 "okay, I know how to avoid data races, right, I'm going I'm going to use locks 「好,現在我知道怎麼避免資料競奪,我要使用數據鎖」 "and I'm going to hide locks too because I want to abstract over it" 而且我將也要隱藏它們因為我想要抽象於它們 so like in java where every object has its own lock, right? 所以像在Java裡所有物件都有它們自己的數據鎖對吧? and unfortunately locks don't compose either, right. 不幸的是數據鎖也無法被組合 That's the problem with locks. I'm not going to talk about this too much 這是數據鎖的問題。我不再談更多 because that is like a different course about concurrent programming but I'm 因為這可以開另一堂並行編程的課程 just mentioning it that this kind of raising the levels of abstraction 我只蜻蜓點水:你必須特別注意這類抽象 you have to be careful what you're abstracting over 對什麼進行了抽象 what are the things that you are subtracting, throwing away and not exposing, right? 你減去、扔掉了什麼並不暴露它們? So the next level of abstraction that came after that, well actually it came before 所以下一階段的抽象是,噢它其實更早出現 that but people realised that, "Hey, maybe we have to dig it out 但人們體會到「嘿,我們可能要重新找到它 "and start using this functional programming" when you abstract things into functions 並開始使用所謂「函數式編程」把東西抽象成函數 and especially in Haskell when, you know, 而且函數,特別是在Haskell裡面,你知道的 in a functional language you don't have mutations so you don't have this problem 在函數式語言裡你不需要可變變數所以你不會有 of hiding data races, and then you also have ways of composing 隱藏資料競奪的問題,然後你也有把 data structures into bigger data structures and that's also because everything is 資料結構組合成更大的資料結構的方法,也因為所有東西都是 immutable so you can safely compose and decompose things. 不可變的所以你可以安全組合和分解資料 Now every time I learned a new language I wanted to find where the boundaries of this language are 每次我學習一個新語言,我都想尋找語言的界線 like, "what are the hardest things to do in this language?", right? 例如,「這個語言最難做到什麼」? So for instance in C++, right, what are the highest levels of abstraction that you can get? 例如在 C++ 中,你能達到的最高抽象是什麼? Template metaprogramming, right? So I was really fascinated by template metaprogramming 模板元編程,是吧?我著迷於模板元編程 and I started reading these books about template metaprogramming 我開始閱讀了這些關於模板元編程的書 and was like "Wow, I would have never come up with these ideas, they are so complex", right? 那就像是「哇,我永遠想不出這些點子,他們太複雜了」,是吧? So it turns out that these are very simple ideas 事實上它們是很單純的點子 it's just that the language is so awkward in expressing them 只是被用一個雞掰的語言表達 So I learned a little bit of Haskell and I said "Ok this huge template that was so complicated, 所以我學了些Haskell然後說「好,這些巨大的模板太複雜了 that's one line of code in Haskell", right? So there are languages in which 那只不過是一行Haskell。」對吧?所以存在某些 there was like a jump in the level of abstraction and made it much easier to 在抽象層級上大躍進的語言,它們讓 program at a higher level of abstraction, right. 你在更高抽象上寫程式簡單得多 And in every language you know there is this group of people who are writing 在每個語言中你都知道有一群人撰寫著 libraries right and they always stretch the language they always go to the highest 程式庫,他們總是延展著可能性 possible abstraction level and they and they hack, right? They hack at it 他們一路向上至最高的抽象層級。他們、他們是「駭客」。對吧?他們駭程式 as much as possible. So I thought "Okay I don't like hacking, I just wanted to 直至極限。所以我想,「好我不想駭程式,我只是想 "use a language that allows me to express myself at a high level of 使用一個讓我能在更高層次抽象上表達自己的語言 "abstraction and that lets me express new ideas that are much more interesting" 而且它讓我表達有趣得多的新點子」 you know, like, with template metaprogramming right you express this 你知道的,像模板元編程能讓你表達 idea that you might have lots of data structures that only differ by the type 一堆只差在它們包裝型別的資料結構這樣的點子 that they hide. Like you can a vector of integers and vector of 像你能有一個整數的列表和 booleans right? And there's just so much code to share, so if you abstract over the 一個布林值的列表對吧?而且它們之間有好多的共通代碼,所以如果你抽象於 data type that you are storing there, if you forget about it, 你儲存的資料型別,如果你忘了它 hide it, abstract over it, you can write code, abstract code, and in 隱藏它、對它進行抽象,你能撰寫代碼、抽象代碼 C++ you do this with templates right. And you get something new, you 在C++裡你用模板進行這樣的事情,對吧。豻且你得到了一些新的東西,你獲得了 program at a higher level – a higher abstraction level because you 在更高層次──更高抽象層次──寫程式的能力因為 disregard some of the details, so that was great. 你忽略了某些細節,而那真的很棒 Now it turns out that once I learned Haskell 再後來,我發現在學了Haskell後 (I'm still learning Haskell to some extent) (某種程度上我還在學習Haskell) I found out there are things in Haskell that are at these boundaries of abstraction 我發現Haskell中有某些東西在抽象的極限上 that, like, there are people who are working on this frontier of Haskell, right? 像有些人在Haskell的最前線努力對吧? There are certain very abstract things that are unfortunately a little bit awkward to express in Haskell 有某些非常抽象的東西很可惜的有點難在Haskell中被表達 and then there is this barrier to abstraction even in Haskell right? 所以甚至在Haskell裡都有抽象程度上的障礙,對吧? I mean if you've seen some libraries that were written by Edward Kmett 我的意思是如果你看過某些Edward Kmett編寫的程式庫 you realise that they are extremely hard to understand what was the thought process, right? 你會發現它們之中的思路非常難被理解吧? And the secret is very simple – Category Theory. 而這之中的秘密很單純:範疇論 Edward Kmett knows Category Theory, and he just takes this stuff from Edward Kmett 懂範疇論,他只是把東西從 Category Theory, he reads these mathematical papers and he just 範疇論搬來,他讀那些數學論文 translates them into Haskell and when you translate stuff from Category 並把它翻譯成Haskell。如果你從範疇論 theory to Haskell you lose a lot of abstraction, you make it more concrete 翻譯到Haskell,你喪失了很多抽象,你讓它變得更具體 Haskell has one category built-in and you are Haskell有一個內建的範疇且你是在 restricting yourself to this particular category. 把自己限制在這個範疇 You can create other categories in Haskell and model them, but that becomes 你能在Haskell中創建其它範疇並對它們建模 awkward and that becomes sort of like template metaprogramming you know within 但那變得不方便且有點像在Haskell中進行你所知道的模板元編程── Haskell – not exactly the same mechanism but the the level of 並非經由完全一樣的機制,但 difficulty in expressing these ideas in Haskell is as big as template 在Haskell中表達這些點子的難度和 metaprogramming in C++. 在C++進行模板元編程一樣高 So Category Theory is this higher-level language above Haskell, above 所以範疇論是個Haskell之上的更高階語言 functional programming, above ML, 在函數式編程之上,在ML Haskell, Scala, and so on Haskell, Scala...等等之上 C++, assembly, it's a higher-level language, okay? It's not a practical 像 C++ ,組合語言。它是更高階層的語言好嗎?他並不是一個 language that we can write code in, but it's a language. 我們可以在之中寫實際程式的語言,但它是個語言 So just like people who write these horrible metaprogramming template libraries in C++ 所以就像在C++中撰寫恐怖模板元編程程式庫的人們一樣 they can only do this because they learned a little bit of Haskell. 只因為他們學了些Haskell而能做到這些 and they know what some constructs in Haskell are and how to do 而且他們知道 Haskell 中的某些構造是什麼而且要怎麼做── things – how to implement algorithms on immutable data structures right 怎麼在不可變資料結構正確實作演算法 they know how to do this because they learned it from Haskell 他們知道怎麼做因為他們從Haskell學了這些 and they just translated into this horrible template programming language. 然後他們僅把它翻譯成這糟糕的模板程式語言 Fine right? And it works and people are using it, the same way that if you're 是吧?它行得通且人們正在使用它們。同樣的道理 a functional programmer you can take these ideas from category theory, 如果你是個函數式工程師,你可以從範疇論擷取這些點子 these very very abstract ideas and translate it into this kind of awkward language called 把這些非常非常抽象的點子翻譯成這樣不便的程式語言 Haskell, right? Now from looking from from categorical perspective Haskell 名為Haskell,對吧?現在從範疇論角度看 becomes this ugly language just like looking from Haskell C++ becomes this Haskell變成了這個非常醜陋的語言一如從Haskell看C++變成這個 ugly language, right? But at least you know it's an executable language, its a 醜陋的語言,是吧?但至少你知道它是個可被執行的語言 language in which we can write programs. And of course these ideas when they 在它之中我們能寫程式。而且當然這些點子在 percolate from category theory down to Haskell they can also percolate then 從範疇論滲透到 Haskell 後,它們也能滲透 down to C++ and even to assembly, PHP or whatever, JavaScript if you want to 到 C++ 或可能甚至組合語言、PHP 或不管什麼語言,如果你想要的話,JavaScript because these are very general ideas 因為他們是非常一般性的點子 So we want to get to this highest possible level of abstraction to help us express ideas that later can be 所以我們想得到這個最高的可能層次上的抽象以幫助我們表達 translated into programs. So that for me is the main practical motivation 之後可被翻譯成程式的點子。這對我來說是最大的應用面的動機 ok? But then I started also thinking about the theoretical motivation or more 好嗎?但接下來我開始思考理論上的動機或 philosophical motivation because once you start learning Category Theory you 更哲學上的動機,因為一但你開始學習範疇論,你 say "Okay, Category Theory unifies lots of things". 會發現,「範疇論統一了很多東西」 I mean if you abstract enough, if you chop up all the unnecessary stuff and you are 我的意思是如果你夠抽象,如果你去蕪存菁 you know, top of Mount Everest and you're looking down and suddenly 你知道的,你在聖母峰頂鳥瞰,突然 all these things start looking similar, like from that high-level all these 所有這些東西看起來變得相似了,就像從如此高層次上 little programming languages look like little ants and they behave same way 這些小小的程式語言看起來像螞蟻以同樣的方式行動 and its like "Ok, they're all the same". At that level of abstraction all this 這就像「是啊,它們都是一樣的。」在那樣的抽象層次上這一切 programming stuff, it's all the same, it looks the same. 編程東西,它們都是一樣的,看起來一樣 But that's not all – suddenly at this high, high level other things look the same 不僅這樣──在這樣高的高角度,突然其它東西看起來也一樣 and then mathematicians discovered this, that they've been developing separate 然後數學家發現了它,他們曾開發 areas of mathematics, right? So first of all they developed geometry, 數學的其他領域,是吧?所以最初他們發展了幾何學 algebra, number theory, topology, what have you, set theory 代數、數論、拓墣學、集合論等等 these are all completely different separate theories, they look nothing like each other, right 這些分別都是完全不同的理論,它們怎麼看都不像,對吧 and category theory found out similarities between all these things. So it turns out that at a certain level of 而範疇論發現了其中的共通性。結果是在一定階層的 abstraction, the structure of all these theories is the same. So you can describe, 抽象上,這些理論的結構都是一樣的。所以你能描述 you know, you tweak with the structure of a category and you suddenly get topology, 誠如,你微調範疇的結構,忽然生出了拓墣 you tweak this structure of category, you suddenly get set theory, 你微調這樣範疇的結構,忽然生出了集合論 you tweak something else and you get algebra. 你微調其他部分便生出了代數 So there is this unification, this grand unification, of, essentially, all 所以有著這樣的一統,這樣子 areas of mathematics in category theory. But then there is also programming, right? 所有領域的數學大一統於範疇論。但也有所謂編程領域,對吧? Programming that deals with these computers with this memory and processor or registers, ok? 程式處理電腦的記憶體,處理器和暫存器 And this stuff can be described also, and then there's a programming language, 而這也能被描述,當有個程式語言 this stuff can be described mathematically, yes, lambda calculus 這些東西能被以數學的方式描述,如lambda演算 Like all these languages that essentially have roots in lambda calculus 似乎所有語言都建基於lambda演算上 they try to get away from bits and bytes and gotos and jumps, right 他們想要擺脫位元、位元組、goto、跳轉 and loops, and they want to abstract this stuff into stuff that's more 還有迴圈,他們想要把它抽象成更像是 like lambda calculus, right and there are these data structures and these data lambda演算的東西,然後我們有這些和 structures you know, we used to look at them like "here's a bunch of bytes, 那些我們知道的資料結構,我們曾把它們視為「這裡有一些位元組 "here's a bunch of bytes, here's a pointer from this bunch of bytes to this bunch 這裡有另一些位元組,這邊有個從這一些位元組指向另一些位元組的指標 "of bytes" and so on, right? 」......等等等,對吧? The very very low level, like plumbers working with tubes, right? 這像非常非常低階的工作,像處理管線的水管工,對吧? And then computer scientists say "Well actually these things, they form types" 接著計算機科學家說「事實上這些東西組合成型別」 So there's type theory, there's type theory that can describe all these 所以有了型別論,有了能描述所有這些 categories of data structures, but there's also type theory in 資料結構範疇的型別論,對吧?但數學中也有型別論 mathematics, right, people invented types in math not to solve problems that 嗯,人們在數學裡發明型別,不是為了解決 computer scientists have, they try to solve problems like paradoxes, like there is Russell's paradox 計算機科學家有的問題,他們想解決悖論之類的問題,例如有所謂羅素悖論 that says that you cannot construct a set of all sets, things like this, 它告訴我們如果你想,嗯,你沒辦法建構所有集合的集合,諸如此類的 or maybe you know the Barber's Paradox: can the barber shave himself or not – 或你可能知道理髮師悖論:理髮師能幫自己理髮嗎? every barber shaves only people who don't shave themselves so can he shave himself or not? 當所有理髮師只幫不幫自己理髮的人理髮,他能幫自己理髮嗎? This kind of paradox – its a funny paradox but they're serious 這類的悖論──這是個好笑的悖論但也是具有深刻意義的 So this Barber's Paradox actually can be translated into Russell's Paradox 所以理髮師悖論事實上能被翻譯成羅素悖論 which is like "the set of all sets" or "sets that don't contain themselves don't form a set" and so on stuff like this right and 諸如「所有集合的集合」或「不包含它們自己的集合不形成一個集合」之類的 and Russell came up with this theory of types that to say that sets 然後羅素想出了這樣型別的理論 form layers upon layers, that you cannot just have all sets and put them 闡述集合形成層層結構,你不能把所有的集合 one big set, right and then the type theory evolved from this and there's 放進一個大集合,然後型別論從此演化成 this very abstract Martin-Löf Type Theory that's very formal, right so that's 那樣非常抽象且形式化的Martin-Löf集合論 that's just a branch of mathematics that was created to deal with paradoxes and 這只是為了解決悖論而被創造的一支數學然後 then there is logic, and logic was created you know long, long time ago by the 你知道有邏輯,邏輯在很久以前被 ancient Greeks right, they used logic so there are laws of logic and people have 古希臘人創造,他們使用邏輯。所以有基本的邏輯律而人們已 been studying logics for thousands of years, right. And at some point people 研究了邏輯數千年。在某刻人們 suddenly realized that all these distinct areas of mathematics are exactly the same 忽然體悟這所有不同領域的數學都正好是一樣的 this is called the Curry-Howard-Lambek isomorphism which says that 這被稱為 Curry-Howard-Lambek 同構,它告訴我們 whatever you do in logic can be directly translated into whatever you do in 你在邏輯中做的任何事情能被直接翻譯成你在 type theory. They are exactly – it's not like they are similar – they are 型別論中做的事情。他們正好──不只是相似──是 exactly the same there is a one to one correspondence, right? And the Lambek 正好相同的,有個一對一對應,對吧?而Lambek part of this isomorphism says that in category theory, there's certain 部分的同構闡明在範疇論中,特定 types of categories – like the cartesian closed categories – that actually totally 類型的範疇──例如笛卡兒閉範疇──它們事實上完全 describe the same thing. It's just like there are these three different theories, one 描述了一樣的東西。這就像是有三個不同理論,一為 is about computing, one is about categories another is about types and 關於計算,一為關於範疇,另一則關於型別而 they are all the same so like all essentially all our human activity is 它們都是一樣的,所以基本上所有人類活動都 described by one theory 被一個理論說明 ok so this is like really mind-blowing and of course mathematicians you know 所以這真的讓人為之驚艷,而且你知道的,數學家當然 when they discover things like this they turn philosophical or 在發現這類東西後變得哲學了 I wouldn't say religious but at least philosophical right like "Oh my god we are discovering 我不會說是像宗教那樣的,但至少是哲學的,像「天啊我們正在發現 stuff!" it's like you're not really creating mathematics right you're 東西!」就像是你不是在創造數學,而是在 discovering some deep deep truth about the universe 發現某些宇宙深處的真理 okay like what do you think it is mathematics something that we invent or 像你認為數學是什麼?是我們發明的 is it like built into the universe, because for physicists, this is "no", right, physicists 或是內建在宇宙中的,因為對物理學家來說,是「不」,物理學家 do experiments so – "We study stuff, we throw these atoms at each other, bang bang 進行實驗所以「我們研究東西,我們撞擊這些原子,砰砰 and we observe something so we are discovering stuff that's around us" 然後我們觀察到了什麼,我們正發覺我們的周遭環境」 Whereas mathematicians, no, they just sit down at the desk with a pencil 數學家則不一樣,他們只拿著一支鉛筆坐在書桌前 or walk around in a park and think. 或在公園裡走動並思考 What are they discovering? And now they are saying "Well we have independently discovered 他們在發現什麼?而現在他們說「好我們分別發現了 that this branch of mathematics this guy discovered and this other guy in 這個人發現的這支數學而且另一個 ancient Greece he discovered logic and this guy in Sweden who discovered type 古希臘人發現了邏輯,這個瑞典人發現了 theory you know, and they all discovered the same thing – there is some really really 型別論,然後他們都發現了一樣的東西:我們真的、真的 deep truth that we are discovering" so it's like there is some Platonic Ideal right 發現了些真理,這就像是存在著某些柏拉圖式的理想 I don't know if you read [Neal] Stephenson's novels about, 我不知道你們是否讀過史蒂芬森的小說 there's this novel in which there are multiple universes and each of them is 在有一部小說中,有多重宇宙 其中每個宇宙都比其它某些 more platonic than the other, and people move from one platonic universe to another but the 更柏拉圖式而人們從一個柏拉圖式宇宙移動到另一個,但 mathematicians think that this is really what they are doing, right? 這就是數學家覺得他們正在做的對吧? And I was thinking about it and I thought "No this is really not – there 我曾思考過但我覺得,「不是這樣 has to be at a simpler explanation" 應該要有個更簡單的解釋。」 And that goes back to the way we do mathematics or the way we discover the universe, right. 而這能回溯到我們做數學的方法,或者我們探索宇宙的方法 So what do we do? I mean we are human, right? We are these evolved 我們怎麼做的?我的意思是我們是人類,我們是進化過的 monkeys, right now we have evolved our brains so some parts of our brain 猴子,現在我們的腦進化過了。我們的腦的某些部分 evolved you know since, I dunno, billion years of evolution, like, the image 自從,不知道,數十億年前就開始演化,例如 processing stuff for instance has been evolving for a very very long time right 圖像處理的部份就演化了很長很長一段時間 so this is a really very good system to analyse visual 所以我們真的有處理視覺輸入的非常非常棒的系統 input. That's been involving for a very long time starting with bacteria 很久以前自從細菌上的系統開始演化 that would just see whether it's bright or dark and change their metabolism 它只能辨別明暗而改變其代謝 and then they would start seeing this side is brighter than that side and so on and eventually, you know, the 接下來它們開始能看到某側比某側亮,最終,你知道的 most important thing "Where is the predator?" "Where's the food?" distinguishing 最重要的問題是「掠食者在哪裡?」「食物在哪裡?」分辨 between a predator and food right this is like the most important 掠食者和食物對吧?這是 thing that makes you survive right so we got very good at this and we have 讓你生存最重要的問題。我們已經在這上頭變得很強 inherited the stuff from lower animals and and there's been a lot of 我們繼承自較低等的動物且有了許多 evolutionary pressure to evolve these right and now we are trying to imitate 演化壓力使我們進化對吧?現在我們試著用 the stuff with computers and we're finding it really hard 電腦模仿這些而我們發現它非常非常困難 it's a really hard problem image recognition right now, recognizing 現在圖像識別是非常難的問題,識別 who's your foe and who's your friend and recognizing where the food is right 是敵是友且識別食物在哪 and whether it's spoiled or not – very important things computers don't do very well 和它有沒有被打翻──電腦做的不是很好但非常重要的事情 ok so this is a very hard problem but we've been working on it for a billion 所以這是個非常難的問題但我們已經處理了它數十億 years so our brains know how to do this stuff. But then there are there are 年所以我們的腦知道如何做對。但還有些 things that evolved in the last hundred thousand years or so, I don't 在這幾十萬年間左右進化的東西,我不 know, how long is human evolution? 100 million years? 知道,人類進化史有多長?一億年? No it's not. Whatever it is it's a tiny number, it's just like the 不、不到。不管怎樣,那是一段很短的時間,是 last millisecond of evolution that we suddenly had these brains that 演化史的最後一微秒,我們突然有了 that can actually think abstractly – we can count, we can communicate, we 能真正進行抽象思考的腦──我們能數數,我們能交流,我們 can organise stuff, and we can do math and we can do science and this is 能組織,且我們能進行數學和科學,這 like a really fresh ability, and and we've been doing science for 真的真的是新獲得的能力,我們 the last few thousand years onwards so that's that's nothing on the evolutionary scale 在這最後幾千年研究科學,這和演化史比只是九牛一毛 and we're doing it with these brains, what do these brains come 而我們正使用我們的腦,我們的腦是怎麼 from, they evolved to do things like hunt mammoths, or run away from 來的?它們為了如獵捕猛瑪象的活動演化,或為了 sabre-toothed tigers, they did not evolve to do programming, that's a 從劍齒虎眼光下逃走,它們不是為了寫程式演化 very recent thing! They didn't evolve even to do mathematics or logic 這是很晚期的事情!它們不為了做數學或邏輯演化 no, they evolved to do stuff like hunting, like finding food and 不,它們為了如打獵、覓食 especially organizing ourselves into groups, right? Social activities 特別是組織團體而進化,對吧?社交活動 and communicating, so language right? So we have these language skills 和溝通,所以語言對吧?所以我們有了這些語言能力 it's very fresh thing, right? 這是很新的事情對吧? So compared to the complexity of our visual cortex this new newly 所以和我們視覺皮質的複雜性相比,這樣新獲得的 acquired ability to actually think abstractly and using a language that 真正抽象思考的能力和使用語言 that's like really very very fresh thing and it's very primitive, ok. It hasn't had 是非常非常新的事情且它很原始,是吧。它沒有 time to evolve. It's very simple and we are using this tool that we evolved to 時間進化。它非常單純且我們正在使用這樣為了 throwing javelins or arrows, shooting arrows and communicating with with other 射標槍或箭和溝通演化出來的工具 guys like "Oh here, the mammoth is over there!" 例如「阿這裡,猛瑪象在那裡!」 "No don't run there!" or "Right behind you a sabre-toothed tiger!" or "A bear!" so we 「不要到那裡!」或「劍齒虎就在你後面!」或「一隻熊!」所以我們 are now using this tool to do mathematics 正在用這樣的工具進行數學 so there are certain things that we found useful in doing 因此我們發現了某些有助於抽象思考的特定概念 abstract thinking, in doing mathematics and the major thing is we 在進行數學時和更重要的在我們 come head-to-head with a very complex problem like how to provide food for our 一起處理一個非常複雜的問題如如何為 tribe right and we solve it, how do we solve it, we divide it into smaller 部落提供食物。我們怎麼解決?我們把它分割成更小 problems that we can solve and then we combine the solutions 而能被解決的問題然後我們組合這些方案 ok so this is the only way we know how to deal with complex situations by 好所以這是我們所知道解決複雜問題的唯一方法 decomposing these things into simpler things and then solving 把它們分解成簡單點的問題然後解決 them, the simplest problems, and combining the solutions into bigger solutions and this is everywhere. 它們,解決最簡單的問題,然後把解決方案組成更大的解決方案,我們到處這麼做 You find is that this permeates everything we do that we don't even notice it. 我們發現這滲透所有我們做的事情,我們甚至沒注意到它 But this is how we work and because this is how we work this is how we do science as well. 但這是我們運作的方式,因此這也是我們進行科學的方式 So every branch of science, every branch of mathematics is 所以每個科學的分支、每個數學的分支是 "We can only see these things that can be chopped into pieces and then put 「我們只能看到這些能被分而治之後組合的事情」 together" so no wonder they look the same! 難怪它們看起來都一樣! Because we can only see these problems that have this structure. If they 因為我們只能看到有這樣構造的問題 don't have this structure, we just don't see them, we just say "We cannot solve this problem" 如果它們沒有這樣構造,我們只會忽視,我們只會說「我們無法解決問題」 "Let's do something else, maybe we can get a grant for solving that other 「來做其它事情,或許我們能拿到解決其它問題的門票 "problem because that seems choppable into smaller pieces 因為它看起來是可被分割成小部分的 "this one doesn't, let's forget about it let's not even talk about it", right? 這一個不行,忘了它且甚至不要談論它。」對吧? So one good thing, 有件好事 ok, so maybe the whole universe is like this maybe everything in this universe 好的,或許這整個宇宙都像這樣子,或許這整個宇宙的所有事情 can be chopped into little pieces and then put together, maybe that's like the 都可以被分而治之,或許這是 property of this universe and our brains are just reflecting this. 這個宇宙的性質而且我們的腦只反映了它 And personally I don't think so. Maybe I'm wrong, hopefully I'm wrong, but I'm a 但我個人不這麼認為。或許我是錯的,希望如此,但我也是一個 physicist also, I started as a physicist, so I saw what was happening in physics 物理學家,我從物理學家開始做起的,所以我看到了物理學中發生的事情 and in physics we also wanted to chop things into little pieces, right. And 在物理學中我們也想把東西分割成小片段。且 we were very successful at this you know – we found out that matter is 我們這方面非常成功:我們發現物質 built from atoms, right? So atoms are these things that we can 由原子組成,對吧?所以原子是這些 separate and then study, find their properties and then say that the 我們可以分開來並研究,發現他們的性質的東西,就假如是 property of a rock or a piece of metal comes by combining the properties 一顆石頭或一塊金屬的性質好了,他們是 of these atoms so we can decompose a piece of rock into atoms, study 這些原子組合成的性質所以我們能把石頭分割成原子,研究 them and then recompose it, and then we have the understanding of them. 他們然後重組它,我們就有了對它的理解 But then we didn't stop at that because we wanted to see what's inside the 但我們不停在這裡因為我們想看看 atom right? So there are these elementary particles 原子內有什麼對吧?所以有了這些基本粒子 electrons, protons and so on. So at some level if we 電子、光子之類的,所以到了某個階段如果我們 want to decompose things into simpler things, these simpler things have 想要把東西分割成更簡單的東西,這些更簡單的東西必須 to have similar properties. For instance 有相似的性質。例如 what's the simplest thing that we can imagine for an elementary particle? It 什麼是我們能想到基本粒子最單純的性質?它 has to be a point, right? It should be a point. I mean, if it's a ball right then maybe we can 必須是個點,對吧?他應該是一個點,我的意思是,如果它是個球則我們或許能 cut it, chop it into smaller pieces and and then do the decomposition, 切割它,把它分割成部分而進行分解 recomposition and so on. So at some level, some lowest possible level we 再重組之類的。所以到了某個階層,某個所有之中最低的階層,我們 cannot chop it anymore and we should find a point, right? So an elementary 沒辦法再繼續分割它,我們應該發現一個點對吧?所以一個基本 particle should be a point. That would be the end of this level of decomposition, right? 粒子應該是一個點。那會是分割得出的階層的最底端,對吧? And we tried doing this, we have like the standard model of particles in 我們曾試著這麼做,我們有著粒子的標準模型 which we assume that the particles are points, which is sort of a cheat because it turns 在其中我們假設粒子是點,這有點作弊因為這麼 out that we cannot really deal theoretically with point particles because 一來我們不能真的處理點狀粒子因為 they get to infinities like two point particles when they get very very very 他們生成了無限例如如果兩個點粒子非常非常非常 close together, right. 接近 The interaction goes to infinity and everything blows up so we came up with 它們的交互作用成為無限且一切崩潰所以我們想出了 this renormalization theory which is like a big hack you know to get rid of 重整化理論,它像是為了擺脫無限的大補丁 infinities and so on and and of course physicists were not very happy with that 等等而且當然物理學家對此不太高興 So they thought 所以他們想 "ok so maybe at this lowest level things are not as choppable as we thought, maybe 「好的或許在這樣最低的階層東西不像我們想像中的可分割,或許 nature really does not follow this kind of divide and then combine" so 自然真的不遵循這種分割後重組的方法 they came up with this idea that maybe elementary particles are strings. If you've heard of 所以他們想出了這樣點子,或許基本粒子是弦。你聽說過 of string theory right? Like, what a crazy theory this is! That this most elementary 弦論對吧?就像,這是多瘋狂的理論!最基本的 thing is not a point but it actually is a string and you cannot chop it 東西不是點而事實上是弦而且你無法分割它 Its like the elementary thing is not divisible, but it's not a point. 最基本的東西不是可分割的,但它不是一個點 And quantum theory – and this is another non-choppable piece 還有量子理論,這是另一個 of knowledge that we found out – says that if you have a bigger system, maybe you 我們發現無法被分割的知識,它說如果你有個較大的系統,或許你 can separate it into elementary particles and say "Hey I have a system of 能把它分割成基本粒子後說「嘿我們有個有 10 particles, I know properties of these 10 particles and I call this system 10個粒子的系統,我知道這10個粒子的性質且稱呼這個系統叫做 something bigger like an object", right, and I can find out the structure of this 某個更大的東西例如一個物件」好的,然後我能 object by looking at these particles and it turns out in quantum mechanics that the states that 由聚焦在這些粒子上發現這個物件的結構,但後來在量子力學上他的相 it comes to – they don't add up! 變成:它們無法被這樣取總和! A state that has two particles is not a sum or a product or a convolution of 一個有兩個粒子的相並不是兩者分別的相的和或積或疊積 states of a single particle it's a new state which follows a different you know 它是一個遵循不同微分方程的一個不同的相 differential equation, and so on, so we try to separate particles 等等,所以我們試著分別粒子 And suddenly we cut the particles apart and it turns out that 我們切割粒子後突然間 something weird happens in between when you are cutting, right, you are actually 你切割的東西發生了些奇怪的事情,是的,你在分割東西時事實上正 changing the system by separating things, ok? So maybe, maybe at the very bottom – or 改變整個系統,好嗎?所以或許、或許在最底層 maybe there is no bottom – maybe at the very bottom things are not separable, ──或者可能沒有最底層──或許在最底層東西不是可分的 maybe things are not composable. Maybe this composability that we love so much 或許東西不是可組合的。或許我們最喜歡的可組合性 is not a property of nature, that's what I'm saying, maybe it's not a property of nature. 並不是大自然的性質,這是我正在說的,或許這不是大自然的性質 Maybe this is just the property of our brains, maybe our brains are such that we have 或許這只是我們腦袋的性質,或許我們的腦袋就是 to see structure everywhere and if we can't find the structure we just give up 必須在任何地方看到結構,如果沒辦法看到結構則放棄 So in this way category theory is not really about mathematics or physics 這麼一來範疇論不是真正和數學和物理相關 category theory is about our minds, how our brains work so it's more of 範疇論和我們的心智,和我們的腦怎麼運作相關所以 epistemology than ontology. Epistemology is how we can reason about stuff, how we can 這比較是知識論而非本體論。知識論是我們怎麼理解東西,怎麼 learn about stuff, ontology is about what things are, right. Maybe we cannot learn what 學習東西,本體論和東西是什麼。或許我們沒辦法學習 things are. But we can learn about how we can study, and that's what category theory tells us. 東西是什麼,但我們能學習如何研究而且這是範疇論告訴我們的 Let's take a break 先休息一下
B1 中級 中文 抽象 編程 物件 程式 語言 數學 類別理論 1.1:動機與哲學 (Category Theory 1.1: Motivation and Philosophy) 367 12 張嘉軒 發佈於 2021 年 01 月 14 日 更多分享 分享 收藏 回報 影片單字