Lambda-Calculus by John Lång, 20 February 2020 “Yön Maku, Kun Linnut Syöksyy Mustina Siipinä Tähtiä Päin; Vaan Mikä on Nimesi Nimi, Tähtesi Salainen Luku Ja Numero?”
Total Page:16
File Type:pdf, Size:1020Kb
Introduction to Lambda-Calculus By John Lång, 20 February 2020 “Yön maku, kun linnut syöksyy mustina siipinä tähtiä päin; vaan mikä on nimesi nimi, tähtesi salainen luku ja numero?” – Arcana by CMX, from their album Discopolis 2 / 252 An Appetizer: The MIU-System ● Douglas Hofstadter presented the following puzzle in his famous book “Gödel, Escher, Bach: an Eternal Golden Braid”: ● The MIU-system has the letters M, I, and U in its alphabet ● It has four production rules: 1) αI αIU; 2) Mα Mαα; 3) αIIIβ → αUβ; and 4) αUUβ → αβ; ● The question→ is: Is MU derivable from MI→? 3 / 252 ● This slide was inspired by the West Metro, that connects Espoo and Helsinki (most of the time). The stops are the concepts worth learning. Page number is written beneath the stops Subway Map Philosophy 5 Strings and λ-terms 30 Renaming Variables 54 Reducing λ-terms 75 de Bruijn Indexing 95 Equality 104 Encodings 125 Undecidability 149 Combinatory Logic 160 Types 176 Further Topics 217 Conclusion 247 4 / 252 Map of the Uni erse We are here Computer Mathematics Science 5 / 252 !hat Is a "unction? Formula Interpretation ● f(x) 2x ● f(x) yields 2x ● g(x,y=) 5 ● g is a constant function ● h : X =Y ● h maps every X to a Y ● x 3→ ● x is mapped to 3 ● (a↦b)(x) a(b(x)) ● composition of a and b ∘ = 6 / 252 First-Order s. &igher-$rder First-Order Higher-Order A×B f C A A×B (a1, b1) c 1 f×1 f (a1, b2) f B c2 (a , b ) 1 3 c ⋮ 3 CB CB×B C ⋮ ϵ f ϵ ∘ f×1 f {((a1,b1),c1), ((a1,b2),c2), ...} B = = 7 / 252 First-Order s. &igher-$rder First-Order Higher-Order A×B f C A A×B (a1, b1) c 1 f×1 f (a1, b2) f B c2 (a , b ) 1 3 c ⋮ 3 CB CB×B C ⋮ ϵ f ϵ ∘ f×1 f {((a1,b1),c1), ((a1,b2),c2), ...} B = = 8 / 252 Extensional s% Intensional Extensional Intensional x≤y y GCD(x,y) f()+ false true x = 0 ) (x,y) ← (y%x,x) return y 9 / 252 Extensional s% Intensional Extensional Intensional x≤y y GCD(x,y) f()+ false true x = 0 ) (x,y) ← (y%x,x) return y 10 / 252 My ,oint of -iew ● According to Wiktionary (viewed some time in 2017): – The word “function” comes from a Latin word that means performance or execution – The Latin word “calculus” means “a pebble or stone used for counting” ● Does set theoretical mathematics do justice to these concepts? – Do you see pebbles in the real line? ● I think of functions as a primitive rather than a derived notion 11 / 252 Some Terminology ● Functions are abstract dependencies between objects ● Algorithms are representations of functions in a formal system. Several algorithms per function; one function per algorithm ● Strings can represent functions as well as their arguments ● Computation is the art of algorithmic string manipulation ● A model of computation is a formal system that defines effectively computable functions in terms of algorithms 12 / 252 Some Models of Computation Industrial Legend: TT FC LC CA Cellular Automata CL = Combinatory Logic URM µRF PN FC = Flow Charts M FT = FRACTRAN x e i l LC Lambda-Calculus PA CL n p = i m µRF Mu Recursive Functions m a = o l PA = Peano Arithmetic C PN = Petri Nets TM PCS CA PCS = Post Canonical Systems TM = Turing Machines TT = Type Theory FT URM = Unlimited Register Machines = Academic 13 / 252 Some Models of Computation Industrial Legend: TT FC LC CA Cellular Automata CL = Combinatory Logic URM µRF PN FC = Flow Charts M FT = FRACTRAN x e i l LC Lambda-Calculus PA CL n p = i m µRF Mu Recursive Functions m a = o l PA = Peano Arithmetic C PN = Petri Nets TM PCS CA PCS = Post Canonical Systems TM = Turing Machines TT = Type Theory FT URM = Unlimited Register Machines = Academic 14 / 252 The Computational Uni erse ● Lambda-Calculus, Combinatory Logic, Mu Recursive Functions, Turing Machines, and others, represent the strongest class of models of computation. (Church-Turing Thesis) ● They’re powerful enough to feature undecidability, i.e. the incompleteness of computational universe. (Thanks, Gödel!) – There are strange loops between object and meta languages – On the bright side, we can extend every formal system infinitely! – We’ll see later how undecidability emerges in LC 15 / 252 Lambda-Calculus? ● Lambda-Calculus (LC) is the model (or language) of computation (i.e. programming) discussed in this presentation. – It is a system that expresses functions as strings of symbols ● A few common misconceptions need to be addressed: – It’s lambda (the Greek letter Λ, λ), not “lambada” (the dance) – “Calculus” referes to proof calculus, not the differential/integral one – “Barendregt convention” was actually initiated by Thomas Ottmann – “Curry transformation” was actually discovered by Gottlob Frege 16 / 252 The .ou'h Idea ● Lambda-Calculus is about anonymous functions, called lambda expressions (λ-exprs) ● There are conversion and reduction rules that allow us to reason about (in)equality of λ-exprs. Reducing λ-exprs is like – running a computer program; – performing a series of algebraic simplifications; or – transforming graphs ● We’ll see how this works in due time 17 / 252 Teaser: Buildin' "unctions ● f(x) 2x + c translates into f λx.+ ((λt.⋅ 2 t)x) c in LC – 2=x can be seen as a function≝ of t, t 2 ⋅ t, applied to x – In LC, we write 2 ⋅ t in prefix notation↦ as ⋅ 2 t – c is constant (or free variable); it stays fixed as x varies – We bind x as the formal parameter: λx.+ ((λt.⋅ 2 t)x) c ● (λt.⋅ 2 t)x means evaluating t (⋅ 2 t) with the argument x ● We can simplify: f λx.+ ((λt↦.⋅ 2 t)x) c λx.+ (⋅ 2 x) c = = 18 / 252 The /lack /ox Analogy .evisited ● Traditionally, functions are ● In LC, functions are the stars seen as black boxes of the show input1 # output1 # function1 # input2 # output2 # function2 # input3 19 / 252 The /lack /ox Analogy .evisited ● Traditionally, functions are ● In LC, functions are the stars seen as black boxes of the show input1 # output1 # function1 # input2 # output2 # function2 # input3 20 / 252 Two ,rogramming Paradi'ms ● Imperative paradigm operates ● Declarative paradigm defines algorithms like cooking algorithms as compositions of recipes, on step-by-step basis: computational primitives: 0: IN R1, =KBD select name, email 1: LOAD R2, =1 from students join staff 2: JZER R1, 6 where email like ’%.fi’ 3: MUL R2, R1 ● 4: SUB R1, =1 LC is declarative 5: JUMP 2 ● 6: OUT R2, =CRT LC doesn’t have implicit state 7: SVC R0, =HALT or side effects 21 / 252 Two ,rogramming Paradi'ms ● Imperative paradigm operates ● Declarative paradigm defines algorithms like cooking algorithms as compositions of recipes, on step-by-step basis: computational primitives: 0: IN R1, =KBD select name, email 1: LOAD R2, =1 from students join staff 2: JZER R1, 6 where email like ’%.fi’ 3: MUL R2, R1 ● 4: SUB R1, =1 LC is declarative 5: JUMP 2 ● 6: OUT R2, =CRT LC doesn’t have implicit state 7: SVC R0, =HALT or side effects 22 / 252 Side-(4ects Make ,rogram Analysis &ard ● A “function” in C: int c = 0; int f(int i) { return i + c++; } f(3); // Returns 3 f(3); // Returns 4 ● Thus, f(3) f(3)! ≠ 23 / 252 Lambda-Calculus Makes (4ects Explicit ● A “function” in C: ● Consider the following: int g(int c, int i) int c = 0; {return i + (c + 1);} int f(int i) int h(int i) {return g(0);} { return i + c++; ● The state of c is now explicit } f(3); // Returns 3 ● In pseudo-LC, we’d declare f(3); // Returns 4 int g(int c)(int i); int h(int i){return g(0)(i);} ● Thus, f(3) f(3)! ● ≠ Now, h(3) h(3) = 24 / 252 Traditional Notation is Imprecise ● Take “f(x) y” for instance – What does= ‘ ’ mean? – Definition or= assertion? – Did we apply f to x? – Perhaps we multiplied f by x? – Do we need the graph of f ? ● Is ‘ ’ in (f g) a function? ∘ ∘ 25 / 252 Lambda-Calculus is Unambi'uous ● Take “f(x) y” for instance ● In LC, f(x) y means – What does= ‘ ’ mean? “f applied to= x equals y” – Definition or= assertion? – It is an assertion (“==”), not a definition (“=”) – Did we apply f to x? – No other interpretations – Perhaps we multiplied f by x? – ‘ ’ always has a direct proof – Do we need the graph of f ? ● is =just another function: ● Is ‘ ’ in (f g) a function? ∘– f g (λvwx.v(wx))fg ∘ ∘ ∘ ≡ 26 / 252 Why Lambda-Calculus? ● Lambda-Calculus is capable of describing computable functions using a referentially transparent (i.e. compositional) language ● LC has an unambiguous syntax with very few special cases ● The computer of LC doesn’t need soul, intuition, or magic ● LC doesn’t burden us with boring and error-prone technical details, such as memory management or instruction sequencing ● LC gives the best of both mathematics and computer science 27 / 252 My -ery Small !orld Map encode express construct are Logic Sets Types Categories p e e e o q x z p pr i u u ov p s l e l e n a i s v t inspire g p a ri a e d e s i g n r n s e s o r e models builds a n Computability LC Functions l fi i z e e e d xp uses extends loi composes ed t ne Complexity Programs Algebra create helps 28 / 252 ● You’re probably very eager to get started already, so let’s get to business! Subway Map Philosophy 5 Strings and λ-terms 30 Renaming Variables 54 Reducing λ-terms 75 de Bruijn Indexing 95 Equality 104 Encodings 125 Undecidability 149 Combinatory Logic 160 Types 176 Further Topics 217 Conclusion 247 29 / 252 Before !e Start ● Our object language is the language of lambda expressions – Words in the object language consist of variable and other symbols ● Our meta language (e.g.