100
1
There is a Bluebird in my
Talk that Wants to Get out
Programming with birds
THEWIZARDLUCAS
Slide 2
Slide 2 text
100
2
PROGRAMMING
IS A GREAT
EXPERIENCE
Slide 3
Slide 3 text
100
3
I want to make it awful.
And I'm not the first one.
Programming
with nothing
By Tom Stuart
Slide 4
Slide 4 text
100
4
I want to make it awful.
But I want to take it one step further.
z
Programming
with nothing
By Tom Stuart
Slide 5
Slide 5 text
100
5
Ruin
JavaScript
Part 1
Slide 6
Slide 6 text
100
6
Ruin
JavaScript
Ruin
JavaScript
with birds
Part 1 Part 2
Slide 7
Slide 7 text
100
7
Ruin
JavaScript
Ruin
JavaScript
with birds
Ruin
JavaScript
and The
Birds
Part 1 Part 2 Part 3
Slide 8
Slide 8 text
100
8
Ruin
JavaScript
Ruin
JavaScript
with birds
Ruin
JavaScript
and The
Birds
Part 1 Part 2 Part 3
Apologise
Part 4
Slide 9
Slide 9 text
100
9
Ruining JavaScript
Part 1
Slide 10
Slide 10 text
100
10
We are spoiled
We program with way too much:
• Objects
• Classes/Inheritance
• Booleans
• Numbers
• Builtin functions
• Builtin data structures
Slide 11
Slide 11 text
100
11
This is what we're
going to rewrite
T H E W R E C K
Slide 12
Slide 12 text
100
12
We are only allowed to use four
kinds of tokens
THE RULES
1 2 3 4
Identifier
ID λID. E E E (E)
Abstraction Application Grouping
Slide 13
Slide 13 text
100
13
We are only allowed to use four
kinds of tokens
THE RULES
1 2 3 4
Identifier
ID λID. E E E (E)
Abstraction Application Grouping
Yes, all functions only take
one argument
I will use assignments to
make this more convenient In JavaScript this is E(E)
Slide 14
Slide 14 text
100
14
Lambda
Calculus
This is
λ
Slide 15
Slide 15 text
100
15
Our first step
T H E W R E C K
Slide 16
Slide 16 text
100
16
Replacing
Booleans
P a r t 1
Slide 17
Slide 17 text
100
17
Why do we need booleans anyway?
Replacing Numbers
if { this } else { that }
const val = boolean ? "Yes": "No"
Slide 18
Slide 18 text
100
18
Why do we need booleans anyway?
Replacing Numbers
if { this } else { that }
const val = boolean ? "Yes": "No"
const TRUE = a => b => a
Always selects the first value.
Slide 19
Slide 19 text
100
19
Why do we need booleans anyway?
Replacing Numbers
if { this } else { that }
const val = boolean ? "Yes": "No"
const FALSE = a => b => b
Always selects the second value.
Slide 20
Slide 20 text
100
20
Booleans
Replacing Numbers
const FALSE = a => b => b
const TRUE = a => b => a
Slide 21
Slide 21 text
100
21
Booleans
Replacing Numbers
const SECOND = a => b => b
const FIRST = a => b => a
Slide 22
Slide 22 text
100
22
Booleans
Replacing Numbers
Slide 23
Slide 23 text
100
23
Booleans
Replacing Numbers
What about the if keyword?
Slide 24
Slide 24 text
100
24
Booleans
Replacing Numbers
What about the if keyword?
if { this } else { that }
Slide 25
Slide 25 text
100
25
Booleans
Replacing Numbers
What about the if keyword?
BOOLEAN { this } else { that }
Slide 26
Slide 26 text
100
26
Booleans
Replacing Numbers
What about the if keyword?
BOOLEAN { this } { that }
Slide 27
Slide 27 text
100
27
Booleans
Replacing Numbers
What about the if keyword?
BOOLEAN { this } { that }
The boolean itself is our this.
Slide 28
Slide 28 text
100
28
Booleans
Replacing Numbers
Let's have some sugar
Slide 29
Slide 29 text
100
29
What can we replace?
Replacing arithmetics
Slide 30
Slide 30 text
100
30
Replacing
Numbers
P a r t 1
Slide 31
Slide 31 text
100
31
What is a number, anyway?
Replacing Numbers
2
Slide 32
Slide 32 text
100
32
What is a number, anyway?
Replacing Numbers
2
Slide 33
Slide 33 text
100
33
What is a number, anyway?
Replacing Numbers
2
Slide 34
Slide 34 text
100
34
What is a number, anyway?
Replacing Numbers
2
Representation Meaning Meaning
Slide 35
Slide 35 text
100
35
How can we represent quantities with functions?
Replacing Numbers
Slide 36
Slide 36 text
100
36
How can we represent quantities with functions?
Replacing Numbers
Function applications!
Slide 37
Slide 37 text
100
37
How can we represent quantities with functions?
Replacing Numbers
Function applications!
Slide 38
Slide 38 text
100
38
How can we represent quantities with functions?
Replacing Numbers
Function applications!
Slide 39
Slide 39 text
100
39
How can we represent quantities with functions?
Replacing Numbers
Function applications!
Slide 40
Slide 40 text
100
40
From meaning to representation
Replacing Numbers
Slide 41
Slide 41 text
100
41
From meaning to representation
Replacing Numbers
Slide 42
Slide 42 text
100
42
From meaning to representation
Replacing Numbers
Slide 43
Slide 43 text
100
43
From meaning to representation
Replacing Numbers
Slide 44
Slide 44 text
100
44
From meaning to representation
Replacing Numbers
Slide 45
Slide 45 text
100
45
What can we replace?
Replacing arithmetics
Slide 46
Slide 46 text
100
46
Replacing
Arithmetics
P a r t 1
Slide 47
Slide 47 text
100
47
The successor function
Replacing arithmetics
const ONE = f => x => f(x)
const TWO = f => x => f(f(x))
Slide 48
Slide 48 text
100
48
The successor function
Replacing arithmetics
const ONE = f => x => f(x)
const SUCCESSOR = n => f => x => f(n(f)(x))
const TWO = f => x => f(f(x))
Slide 49
Slide 49 text
100
49
The successor function
Replacing arithmetics
const ONE = f => x => f(x)
SUCCESSOR(ONE)
const TWO = f => x => f(f(x))
const SUCCESSOR = n => f => x => f(n(f)(x))
Slide 50
Slide 50 text
100
50
The successor function
Replacing arithmetics
const ONE = f => x => f(x)
SUCCESSOR(ONE) f => x => f((ONE(f))(x))
const TWO = f => x => f(f(x))
const SUCCESSOR = n => f => x => f(n(f)(x))
Slide 51
Slide 51 text
100
51
The successor function
Replacing arithmetics
const ONE = f => x => f(x)
SUCCESSOR(ONE) f => x => f((ONE(f))(x))
f => x => f((x => f(x))(x))
const TWO = f => x => f(f(x))
const SUCCESSOR = n => f => x => f(n(f)(x))
Slide 52
Slide 52 text
100
52
The successor function
Replacing arithmetics
const ONE = f => x => f(x)
f => x => f((ONE(f))(x))
f => x => f((x => f(x))(x))
f => x => f(f(x))
const TWO = f => x => f(f(x))
const SUCCESSOR = n => f => x => f(n(f)(x))
SUCCESSOR(ONE)
Slide 53
Slide 53 text
100
53
The addition function
Replacing arithmetics
const TWO = f => x => f(f(x))
const ONE = f => x => f(x)
Slide 54
Slide 54 text
100
54
The addition function
Replacing arithmetics
const TWICE = f => x => f(f(x))
const ONE = f => x => f(x)
Slide 55
Slide 55 text
100
55
The addition function
Replacing arithmetics
twice(SUCCESSOR(ONE))
ADDITION(TWO)(ONE)
Slide 56
Slide 56 text
100
56
The multiplication function
Replacing arithmetics
Slide 57
Slide 57 text
100
57
The multiplication function
Replacing arithmetics
multiplication(THREE)(TWO)
Slide 58
Slide 58 text
100
58
The multiplication function
Replacing arithmetics
THRICE(TWO)
multiplication(THREE)(TWO)
Slide 59
Slide 59 text
100
59
What can we replace?
Replacing arithmetics
Slide 60
Slide 60 text
100
60
What can we replace?
Replacing arithmetics
Slide 61
Slide 61 text
100
61
What is next?
Replacing arithmetics
Subtraction
Slide 62
Slide 62 text
100
62
The predecessor function
Replacing arithmetics
Predecessor
TWO ONE
Slide 63
Slide 63 text
100
63
The predecessor function
Replacing arithmetics
TWO ONE
?
Slide 64
Slide 64 text
100
64
Pairs
Replacing arithmetics
Slide 65
Slide 65 text
100
65
Pairs
Replacing arithmetics
First we store two values.
Slide 66
Slide 66 text
100
66
Pairs
Replacing arithmetics
Then we tell which one we want.
100
83
Replacing arithmetics
The Predecessor Function
N x INCREMENT_PAIR(0, 0)
First of
Slide 84
Slide 84 text
100
84
Replacing arithmetics
The Subtraction Function
Predecessor of N K times
Slide 85
Slide 85 text
100
85
What can we replace?
Replacing arithmetics
Slide 86
Slide 86 text
100
86
What can we replace?
Replacing arithmetics
Slide 87
Slide 87 text
100
87
Replacing
Boolean
Operators
P a r t 1
Slide 88
Slide 88 text
100
88
AND { true } { true } is true
AND
Replacing Boolean Operators
Any other combinations are false
Slide 89
Slide 89 text
100
89
AND { true } { true } is true
AND
Replacing Boolean Operators
Any other combinations are false
Slide 90
Slide 90 text
100
90
AND
Replacing Boolean Operators
If a is true b must be true
If a is false it returns itself
Slide 91
Slide 91 text
100
91
It is true if any values are { true }
OR
Replacing Boolean Operators
Slide 92
Slide 92 text
100
92
It is true if any values are { true }
OR
Replacing Boolean Operators
Slide 93
Slide 93 text
100
93
It is true if any values are { true }
OR
Replacing Boolean Operators
If a is true it returns itself, otherwise tries b
Slide 94
Slide 94 text
100
94
NOT
Replacing Boolean Operators
Inverts any { true } and { false }
Slide 95
Slide 95 text
100
95
NOT
Replacing Boolean Operators
Inverts any { true } and { false }
Slide 96
Slide 96 text
100
96
NOT
Replacing Boolean Operators
Inverts any { true } and { false }
Slide 97
Slide 97 text
100
97
NOT
Replacing Boolean Operators
Inverts any { true } and { false }
Takes a boolean f and applies it to the arguments in reverse order
Slide 98
Slide 98 text
100
98
NOT
Replacing Boolean Operators
Inverts the selector
Takes a boolean f and applies it to the arguments in reverse order
Slide 99
Slide 99 text
100
99
NOT
Replacing Boolean Operators
Inverts the selector
Takes a boolean f and applies it to the arguments in reverse order
Slide 100
Slide 100 text
100
100
IS ZERO
Replacing Boolean Operators
Checks if a is zero
If n is zero, it will return the last TRUE
Slide 101
Slide 101 text
100
101
EQUALS
Replacing Boolean Operators
Checks if a equals b
Checks if both n <= k and k <= n
Slide 102
Slide 102 text
100
102
GREATER THAN
Replacing Boolean Operators
The opposite of lesser or equal
Slide 103
Slide 103 text
100
103
What can we replace?
Replacing Boolean Operators
Slide 104
Slide 104 text
100
104
What can we replace?
Replacing Boolean Operators
Slide 105
Slide 105 text
100
105
Replacing everything with functions
Ruining Javascript
Slide 106
Slide 106 text
100
106
Ruining Javascript
Replacing everything with functions
Slide 107
Slide 107 text
100
107
Ruining Javascript
Replacing everything with functions
Slide 108
Slide 108 text
100
108
Ruining Javascript
Replacing everything with functions
Slide 109
Slide 109 text
100
109
Ruining Javascript
Replacing everything with functions
Slide 110
Slide 110 text
100
110
Ruining Javascript
Replacing everything with functions
Slide 111
Slide 111 text
100
111
Ruining Javascript
We could replace the
names of these functions
by their definitions and
remove all variable names
Replacing everything with functions
Slide 112
Slide 112 text
100
112
Unleash Hell
Ruining Javascript
But I will save you from
this pain for now
Slide 113
Slide 113 text
100
113
Ruining JavaScript With Birds
Part 2
Slide 114
Slide 114 text
100
114
Combinators
Our Birds
Functions that don't
have free variables.
const combinator = a => b => a
const withContext = a => b(a)
a is bound and b is free
both a and b are bound
Slide 115
Slide 115 text
100
115
To Mock a
Mockingbird
C o m b i n a t o r s a n d B i r d s
Slide 116
Slide 116 text
100
116
Combinators
The Idiot Bird
Slide 117
Slide 117 text
100
117
Combinators
The Kestrel
Slide 118
Slide 118 text
100
118
Combinators
The Kestrel
Slide 119
Slide 119 text
100
119
Combinators
The Cardinal
Slide 120
Slide 120 text
100
120
Combinators
The Vireo
Slide 121
Slide 121 text
100
121
Combinators
The Bluebird
Slide 122
Slide 122 text
100
122
Combinators
The Thrush
Slide 123
Slide 123 text
100
123
Combinators
The Starling
Slide 124
Slide 124 text
100
124
Combinators
Replacing Functions (Successor)
const SUCCESSOR = n => f => x => f(n(f)(x))
Slide 125
Slide 125 text
100
125
Combinators
const SUCCESSOR = n => f => B(f)(n(f))
Replacing Functions (Successor)
Slide 126
Slide 126 text
100
126
Combinators
const SUCCESSOR = n => f => B(f)(n(f))
Replacing Functions (Successor)
Slide 127
Slide 127 text
100
127
Combinators
const SUCCESSOR = n => f => B(f)(n(f))
const SUCCESSOR = S(B)
Replacing Functions (Successor)
Slide 128
Slide 128 text
100
128
Combinators
Replacing Functions (Addition)
const ADDITION = n => k => n(SUCESSOR)(k)
Slide 129
Slide 129 text
100
129
Combinators
Replacing Functions (Addition)
const ADDITION = n => k => n(S(B))(k)
100
133
Ruining Javascript
B Th
C
K
KI
V
S
Nice birds m8
Slide 134
Slide 134 text
100
134
Ruining Javascript
Haskell's Data.Aviary.Birds
This module is intended
for illustration (the type
signatures!) rather than
utility.
Slide 135
Slide 135 text
100
135
Ruining Javascript
Nice birds m8
Slide 136
Slide 136 text
100
136
Ruining JavaScript and the Birds
Part 3
Slide 137
Slide 137 text
100
137
WHAT IF I TOLD YOU
YOU ONLY NEED
TWO COMBINATORS?
Yes, that's right. Two.
Slide 138
Slide 138 text
SK
SK Calculus
S K
Slide 139
Slide 139 text
100
139
SK Calculus
Replacing Numbers
People also call it SKI Calculus
Slide 140
Slide 140 text
100
140
SK Calculus
Replacing Numbers
People also call it SKI Calculus
Because it's more convenient to have I
Slide 141
Slide 141 text
100
141
SK Calculus
Replacing Numbers
People also call it SKI Calculus
const I = S(K)(K)
Because it's more convenient to have I
Slide 142
Slide 142 text
100
142
SK Calculus
Replacing Numbers
const KI = K(S(K)(K))
Slide 143
Slide 143 text
100
143
SK Calculus
Replacing Numbers
const KI = K(I)
Slide 144
Slide 144 text
100
144
SK Calculus
Replacing Numbers
const KI = K(I)
const B = S(K(S))(K)
Slide 145
Slide 145 text
100
145
SK Calculus
Replacing Numbers
const KI = K(I)
const B = S(K(S))(K)
const V = PAIRING
Slide 146
Slide 146 text
100
146
SK Calculus
Replacing Numbers
const V =
((S(K((S((S(K((S(KS)
)K)))S))(KK))))
((S(K(S((SK)K))))K))
Slide 147
Slide 147 text
100
147
SK Calculus
Replacing Numbers
const V =
((S(K((S((S(K((S(KS)
)K)))S))(KK))))
((S(K(S((SK)K))))K))
Easy.
Slide 148
Slide 148 text
100
148
What does this mean?
Slide 149
Slide 149 text
100
149
What does this mean?
If we can replace all code by functions
Slide 150
Slide 150 text
100
150
What does this mean?
If we can replace all code by functions
Replace all functions by combinators
Slide 151
Slide 151 text
100
151
What does this mean?
If we can replace all code by functions
Replace all functions by combinators
And replace all combinators by S and K
Slide 152
Slide 152 text
100
152
What does this mean?
If we can replace all code by functions
Replace all functions by combinators
And replace all combinators by S and K
Then we can replace all code by S and K
Slide 153
Slide 153 text
100
153
What does this mean?
https://crypto.stanford.edu/~blynn/lambda/sk.html
http://xn--wxak1a.com/blog/Combinators.html
Slide 154
Slide 154 text
100
154
Apologising
Part 4
Slide 155
Slide 155 text
100
155
Apologising
Part 4
Functional Programming
Combinatory Logic
Computability Theory History of Math
Compiler Theory
Language Design
Recursion
Wanting to frame
Gödel pictures to
hang in your room
Slide 156
Slide 156 text
100
156
Thank you!
Shhh, no tears. Only lambdas now.
@THEWIZARDLUCAS (TWITTER)
@LUCASFCOSTA (GITHUB)
LUCASFCOSTA.COM
Slide 157
Slide 157 text
100
157
References
http://codon.com/programming-with-nothing
http://www.angelfire.com/tx4/cus/combinator/birds.html
https://bit.ly/2xpcPKn A Flock of Functions - Gabriel Lebec
List of Notorious Combinators
The blog post for the talk I mentioned in the beginning
https://speakerdeck.com/tomstuart/programming-with-nothing Slides for the talk "Programming with Nothing"
https://amzn.to/2BVVsa1 To Mock a Mockingbird - Raymond Smullyan
http://computationbook.com Understanding Computation - Tom Stuart
https://www.youtube.com/watch?v=_kYGDJSm0gE - ASU Lectures - Lambda Calculus by Adam Doupé
Slide 158
Slide 158 text
100
158
Thank you!
Shhh, no tears. Only lambdas now.
@THEWIZARDLUCAS (TWITTER)
@LUCASFCOSTA (GITHUB)
LUCASFCOSTA.COM