Gabriel Lebec
July 11, 2019
220

Lambda Calc Talk (ConsenSys Condensed Version)

Truncated version of my LC talk, delivered for a remote talk. For the longer original version, see https://github.com/glebec/lambda-talk. For the follow-up code examples repo including the Z-combinator, see https://github.com/glebec/lambda-talk-practical.

July 11, 2019

Transcript

1. as.js A FL O C K of FU N C

T I O N S COMBINATORS, LAMBDA CALCULUS, & CHURCH ENCODINGS in JAVASCRIPT ConsenSys Crash Course  Edition™

5. λ JS I(x) === ? I x = ? I

:= a.a I = a => a
6. λ JS I(x) === x I x = x I

:= a.a I = a => a
7. λ JS I(I) === ? I I = ? I

:= a.a I = a => a
8. λ JS I(I) === I I I = I I

:= a.a I = a => a

16. -CALCULUS SYNTAX expression ::= variable identifier | expression expression application

| variable . expression abstraction | ( expression ) grouping

19. f a f(a) f a b f(a)(b) (f a) b

(f(a))(b) f (a b) f(a(b)) APPLICATIONS
20. a.b a => b a.b x a => b(x) a.(b

x) a => (b(x)) (a.b) x (a => b)(x) a.b.a a => b => a a.(b.a) a => (b => a) ABSTRACTIONS

41. ((a.a)b.c.b)(x)e.f = (b.c.b) (x)e.f = (c.x) e.f = x β-REDUCTION

β-NORMAL FORM
42. ((a.a)b.c.b)(x)e.f = (b.c.b) (x)e.f = (c.x) e.f = x β-REDUCTION*

β-NORMAL FORM *not covered: evaluation order, variable collision avoidance

:= f.ff

M := f.ff
47. λ JS M(I) === I(I) && I(I) === ? M

I = I I = ? M := f.ff
48. λ JS M(I) === I(I) && I(I) === I M

I = I I = I M := f.ff

:= f.ff

M := f.ff
51. λ JS M(M) === M(M) === ? M M =

M M = ? M := f.ff
52. λ JS M(M) === M(M) === M M = M

M = M M = … // stack overflow M := f.ff M(M) === M(M) === M(M) === M(M) === M M(M) === M(M) === M(M) === M(M) === M M(M) === M(M) === M(M) === M(M) === M M(M) === M(M) === M(M) === M(M) === M M(M) === M(M) === M(M) === M(M) === M
53. λ JS M M = M M = M M

= Ω // stack overflow M := f.ff M(M) === M(M) === M(M) === M(M) === M M(M) === M(M) === M(M) === M(M) === M M(M) === M(M) === M(M) === M(M) === M M(M) === M(M) === M(M) === M(M) === M M(M) === M(M) === M(M) === M(M) === M
54. a.b.c.b a => b => c => b abc.b a

=> b => c => b (a, b, c) => b = ABSTRACTIONS, again

again

again
75. ((a.a)bc.b)(x)e.f = (bc.b) (x)e.f = (c.x) e.f = x β-REDUCTION,

again β-NORMAL FORM

77. λ JS K = a => b => a K

:= ab.a = a.b.a
78. λ JS K(M)(I) === ? K M I = ?

K := ab.a K = a => b => a
79. λ JS K(M)(I) === M K M I = M

K := ab.a K = a => b => a
80. λ JS K(M)(I) === M K(I)(M) === ? K M

I = M K I M = ? K := ab.a K = a => b => a
81. λ JS K(M)(I) === M K(I)(M) === I K M

I = M K I M = I K := ab.a K = a => b => a

83. λ JS K(I)(x) === ? K I x = ?

K := ab.a K = a => b => a
84. λ JS K(I)(x) === I K I x = I

K := ab.a K = a => b => a
85. λ JS K(I)(x)(y) === I(y) K I x y =

I y K := ab.a K = a => b => a
86. λ JS K(I)(x)(y) === I(y) && I(y) === ? K

I x y = I y = ? K := ab.a K = a => b => a
87. λ JS K I x y = I y =

y K := ab.a K = a => b => a K(I)(x)(y) === I(y) && I(y) === y
88. λ JS K I x y = I y =

y K := ab.a K = a => b => a K(I)(x)(y) === I(y) && I(y) === y
89. λ JS K I x y = I y =

y K := ab.a K = a => b => a K(I)(x)(y) === I(y) && I(y) === y

91. λ JS KI = a => b => b KI

= K(I) KI := ab.b = K I
92. λ JS KI(M)(K) === ? KI M K = ?

KI := ab.b KI = a => b => b
93. λ JS KI(M)(K) === K KI M K = K

KI := ab.b KI = a => b => b
94. λ JS KI(M)(K) === K KI(K)(M) === ? KI M

K = K KI K M = ? KI := ab.b KI = a => b => b
95. λ JS KI(M)(K) === K KI(K)(M) === M KI M

K = K KI K M = M KI := ab.b KI = a => b => b

97. SCHÖNFINKEL CURRY SMULLYAN Identitätsfunktion Konstante Funktion verSchmelzungsfunktion verTauschungsfunktion Zusammensetzungsf. I

K  S  C  B Idiot  Kestrel  Starling  Cardinal  Bluebird Ibis?

99. PEANO FREGE RUSSELL SCHÖNFINKEL VON NEUMANN CURRY CHURCH GÖDEL TURING

KLEENE ROSSER TH E FO R M A L I Z AT I O N O F MAT H E M AT I C A L LO G I C
100. PEANO FREGE RUSSELL SCHÖNFINKEL VON NEUMANN CURRY CHURCH GÖDEL TURING

KLEENE ROSSER CO M B I N AT O RY LO G I C CO M B I N AT O R S · CU R RY I N G 1920
101. PEANO FREGE RUSSELL SCHÖNFINKEL VON NEUMANN CURRY CHURCH GÖDEL TURING

KLEENE ROSSER CO M B I N AT O RY LO G I C (AG A I N ) CO M B I N AT O R S · M A N Y C O N T R I B U T I O N S 1926
102. PEANO FREGE RUSSELL SCHÖNFINKEL VON NEUMANN CURRY CHURCH GÖDEL TURING

KLEENE ROSSER D I S C OV E R S SC H Ö N F I N K E L “This paper anticipates much of what I have done.” 1927
103. PEANO FREGE RUSSELL SCHÖNFINKEL VON NEUMANN CURRY CHURCH GÖDEL TURING

KLEENE ROSSER IN C O M P L E T E N E S S TH E O R E M S 1931 GE N E R A L RE C U R S I O N TH E O RY
104. RÓZSA PÉTER (POLITZER) RE C U R S I V

E FU N C T I O N TH E O RY 1932 RE K U R S I V E FU N K T I O N E N ConsenSys Crash Course  Edition™
105. PEANO FREGE RUSSELL SCHÖNFINKEL VON NEUMANN CURRY CHURCH GÖDEL TURING

KLEENE ROSSER -CA L C U L U S AN EF F E C T I V E MO D E L O F CO M P U TAT I O N 1932
106. PEANO FREGE RUSSELL SCHÖNFINKEL VON NEUMANN CURRY CHURCH GÖDEL TURING

KLEENE ROSSER I N C O N S I S T E N C Y O F S P E C I A L I Z E D 1931–1936 C O N S I S T E N C Y O F P U R E
107. PEANO FREGE RUSSELL SCHÖNFINKEL VON NEUMANN CURRY CHURCH GÖDEL TURING

KLEENE ROSSER SO LV E S T H E DE C I S I O N PRO B L E M V I A T H E -CA L C U L U S 1936
108. PEANO FREGE RUSSELL SCHÖNFINKEL VON NEUMANN CURRY CHURCH GÖDEL TURING

KLEENE ROSSER SO LV E S T H E DE C I S I O N PRO B L E M 1936 V I A T H E TU R I N G MAC H I N E
109. PEANO FREGE RUSSELL SCHÖNFINKEL VON NEUMANN CURRY CHURCH GÖDEL TURING

KLEENE ROSSER ES TA B L I S H E S T H E CH U RC H -TU R I N G TH E S I S 1936 -CA L C U L U S 㱻 TU R I N G MAC H I N E
110. PEANO FREGE RUSSELL SCHÖNFINKEL VON NEUMANN CURRY CHURCH GÖDEL TURING

KLEENE ROSSER O B TA I N S PH D U N D E R CH U RC H 1936–1938 PU B L I S H E S 1S T FI X E D -PO I N T CO M B I N AT O R
111. COMBINATORS functions with no free variables b.b combinator b.a not

a combinator ab.a combinator a.ab not a combinator abc.c(e.b) combinator
112. COMBINATORS Sym. Bird -Calculus Use Haskell I Idiot a.a identity

id M Mockingbird f.ﬀ self-application (cannot deﬁne) K Kestrel ab.a ﬁrst, const const KI Kite ab.b = KI second const id C Cardinal fab.fba reverse arguments flip B Bluebird fga.f(ga) 1°-1° composition (.) B1 Blackbird fgab.f(gab) = BBB 1°-2° composition (.) . (.) Th Thrush af.fa = CI hold an argument flip id V Vireo abf.fab = BCT hold a pair of args flip . flip id

114. λ JS C = f => a => b =>

f(b)(a) C := fab.fba
115. λ JS C(K)(I)(M) === ? C K I M =

? C := fab.fba C = f => a => b => f(b)(a)
116. λ JS C(K)(I)(M) === M C K I M =

M C := fab.fba C = f => a => b => f(b)(a)
117. λ JS C(K)(I)(M) === M C K I M =

M C := fab.fba C = f => a => b => f(b)(a)
118. λ JS KI(I)(M) === M KI I M = M

C := fab.fba C = f => a => b => f(b)(a)
119. COMBINATORS Sym. Bird -Calculus Use Haskell I Idiot a.a identity

id M Mockingbird f.ﬀ self-application (cannot deﬁne) K Kestrel ab.a ﬁrst, const const KI Kite ab.b = KI = CK second const id C Cardinal fab.fba reverse arguments flip B Bluebird fga.f(ga) 1°-1° composition (.) B1 Blackbird fgab.f(gab) = BBB 1°-2° composition (.) . (.) Th Thrush af.fa = CI hold an argument flip id V Vireo abf.fab = BCT hold a pair of args flip . flip id

122. -CALCULUS abstract symbol rewriting functional computation TURING MACHINE hypothetical device

state-based computation (f.ff)a.a purely functional programming languages higher-level machine-centric languages assembly languages machine code higher-level abstract stateful languages real computers

124. EVERYTHING CAN BE FUNCTIONS ConsenSys Crash Course  Edition™: "but note,

not everything IS or  SHOULD be functions"

// true

// false

result := ?
132. λ JS const result = bool ? exp1 : exp2

result := bool ? exp1 : exp2
133. λ JS const result = bool ? exp1 : exp2

result := bool ? exp1 : exp2
134. λ JS const result = bool ? exp1 : exp2

result := bool exp1 exp2
135. λ JS const result = bool (exp1) (exp2) result :=

func exp1 exp2
136. λ JS result := func exp1 exp2 const result =

bool (exp1) (exp2) // true
137. λ JS result := func exp1 exp2 const result =

bool (exp1) (exp2) // false
138. λ JS const result = bool (exp1) (exp2) result :=

func exp1 exp2 TRUE FALSE
139. λ JS const result = bool (exp1) (exp2) result :=

func exp1 exp2 K KI
140. λ JS const T = K const F = KI

TRUE := K FALSE := KI = C K CHURCH ENCODINGS: BOOLEANS

146. λ JS C(K) (chirp)(tweet) === tweet C(KI)(chirp)(tweet) === chirp C

K = KI C (KI) = K
147. λ JS C(T) (chirp)(tweet) === tweet C(F) (chirp)(tweet) === chirp

C T = F C F = T
148. CHURCH ENCODINGS: BOOLEANS Sym. Name -Calculus Use T TRUE ab.a

= K encoding for true F FALSE ab.b = KI = CK encoding for false NOT p.pFT or C negation AND pq.pqF or pq.pqp conjunction OR pq.pTq or pq.ppq disjunction BEQ pq.p q (NOT q) equality

?.?

AND := pq.?
151. λ JS const and = p => q => p(?)(¿)

AND := pq.p?¿
152. λ JS const and = p => q => p(?)(¿)

AND := pq.p?¿ F F
153. λ JS const and = p => q => p(?)(F)

AND := pq.p?F
154. λ JS const and = p => q => p(?)(F)

AND := pq.p?F T T
155. λ JS const and = p => q => p(q)(F)

AND := pq.pqF
156. CHURCH ENCODINGS: BOOLEANS Sym. Name -Calculus Use T TRUE ab.a

= K encoding for true F FALSE ab.b = KI = CK encoding for false NOT p.pFT or C negation AND pq.pqF or pq.pqp conjunction OR pq.pTq or pq.ppq = M* disjunction BEQ pq.p q (NOT q) equality
157. (ONE OF) DE MORGAN'S LAWS ¬(P ∧ Q) = (¬P)

∨ (¬Q) BEQ (NOT (AND p q)) (OR (NOT p) (NOT q)) !(p && q) === (!p) || (!q)
158. BEQ (NOT (AND p q)) (OR (NOT p) (NOT q))

(xy.x y ((fab.fba) y))  ((fab.fba) ((xy.xyx) p q))  ((f.ff) ((fab.fba) p) ((fab.fba) q))
159. BEQ (NOT (AND p q)) (OR (NOT p) (NOT q))

(xy.x y ((fab.fba) y))  ((fab.fba) ((xy.xyx) p q))  ((f.ff) ((fab.fba) p) ((fab.fba) q))
160. BEQ (NOT (AND p q)) (OR (NOT p) (NOT q))

(xy.x y ((fab.fba) y))  ((fab.fba) ((xy.xyx) p q))  ((f.ff) ((fab.fba) p) ((fab.fba) q))
161. BEQ (NOT (AND p q)) (OR (NOT p) (NOT q))

(xy.x y ((fab.fba) y))  ((fab.fba) ((xy.xyx) p q))  ((f.ff) ((fab.fba) p) ((fab.fba) q))
162. BEQ (NOT (AND p q)) (OR (NOT p) (NOT q))

(xy.x y ((fab.fba) y))  ((fab.fba) ((xy.xyx) p q))  ((f.ff) ((fab.fba) p) ((fab.fba) q))
163. BEQ (NOT (AND p q)) (OR (NOT p) (NOT q))

(xy.x y ((fab.fba) y))  ((fab.fba) ((xy.xyx) p q))  ((f.ff) ((fab.fba) p) ((fab.fba) q))
164. BEQ (NOT (AND p q)) (OR (NOT p) (NOT q))

(xy.x y ((fab.fba) y))  ((fab.fba) ((xy.xyx) p q))  ((f.ff) ((fab.fba) p) ((fab.fba) q))
165. BEQ (NOT (AND p q)) (OR (NOT p) (NOT q))

(xy.x y ((fab.fba) y))  ((fab.fba) ((xy.xyx) p q))  ((f.ff) ((fab.fba) p) ((fab.fba) q))

20? 10? 5?

174. IOTA ι := f.(f abc.ac(bc))ab.a I := ιι K :=

ι(ι(ιι)) S := ι(ι(ι(ιι)))

178. pseudo-recursive functorial to invoke THE Z FIXED-POINT COMBINATOR Z =

f => M(x => f(v => x(x)(v))) “engine” which invents recursion ConsenSys Crash Course  Edition™

180. RECURSIVE fact =   n =>   (n > 0)

? n * (n - 1) : 1 fact
181. (n > 0) ? n * (n - 1)

: 1 “PSEUDO-RECURSIVE” pseudoFact =   n => f f => replaced recursive call with parameter
182. RECURSIVE VS.PSUEDO-RECURSIVE rec = a => ___ rec(x)    pseudoRec

= step => a => ___ step(x) rec = Z(pseudoRec) rec(input) -> result

187. COMBINATORS Sym. Bird -Calculus Use Haskell I Idiot a.a identity

id M Mockingbird f.ﬀ self-application (cannot deﬁne) K Kestrel ab.a true, ﬁrst, const const KI Kite ab.b = KI = CK false, second const id C Cardinal fab.fba reverse arguments flip B Bluebird fga.f(ga) 1°←1° composition (.) Th Thrush af.fa = CI hold an argument flip id V Vireo abf.fab = BCT hold a pair of args flip . flip id B1 Blackbird fgab.f(gab) = BBB 1°←2° composition (.) . (.)
188. CHURCH ENCODINGS: BOOLEANS Sym. Name -Calculus Use T TRUE ab.a

= K = C(KI) encoding for true F FALSE ab.b = KI = CK encoding for false NOT p.pFT or C negation AND pq.pqF or pq.pqp conjunction OR pq.pTq or pq.ppq = M* disjunction BEQ pq.p q (NOT q) equality
189. CHURCH ENCODINGS: NUMERALS Sym. Name -Calculus Use N0 ZERO fa.a

= F apply f no times to a N1 ONCE fa.f a = I* apply f once to a N2 TWICE fa.f (f a) apply 2-fold f to a N3 THRICE fa.f (f (f a)) apply 3-fold f to a N4 FOURFOLD fa.f (f (f (f a))) apply 4-fold f to a N5 FIVEFOLD fa.f (f (f (f (f a))))) apply 5-fold f to a
190. CHURCH ARITHMETIC Name -Calculus Use SUCC nf.B f (nf) =

nfa.f(nfa) successor of n ADD nk.n SUCC k = nkf.B (n f) (k f) addition of n and k MULT nkf.n(kf) = B multiplication of n and k POW nk.kn = Th raise n to the power of k PRED n.n (g.IS0 (g N1) I (B SUCC g)) (K N0) N0 predecessor of n PRED n.FST (n Φ (PAIR N0 N0)) predecessor of n (easier) SUB nk.k PRED n subtract k from n
191. CHURCH ARITHMETIC: BOOLEAN OPS Name -Calculus Use IS0 n.n (K

F) T test if n = 0 LEQ nk.IS0 (SUB n k) test if n <= k EQ nk.AND (LEQ n k) (LEQ k n) test if n = k GT nk.B1 NOT LEQ test if n > k
192. CHURCH PAIRS Sym. Name -Calculus Use PAIR abf.fab = V

pair two arguments FST p.pK extract ﬁrst of pair SND p.p(KI) extract second of pair Φ PHI p.PAIR (SND p) (SUCC (SND p) copy 2nd to 1st, inc 2nd SET1ST cp.PAIR c (SND p) set ﬁrst, immutably SET2ND cp.PAIR (FST p) c set second, immutably
193. ADDITIONAL RESOURCES Combinator Birds · Rathman · http://bit.ly/2iudab9 To Mock

a Mockingbird · Smullyan · http://amzn.to/2g9AlXl To Dissect a Mockingbird · Keenan · http://dkeenan.com/Lambda .:.  A Tutorial Introduction to the Lambda Calculus · Rojas · http://bit.ly/1agRC97 Lambda Calculus · Wikipedia · http://bit.ly/1TsPkGn The Lambda Calculus · Stanford · http://stanford.io/2vtg8hp .:.  History of Lambda-calculus and Combinatory Logic Cardone, Hindley · http://bit.ly/2wCxv4k .:.  An Introduction to Functional Programming  through Lambda Calculus · Michaelson · http://amzn.to/2vtts56