$30 off During Our Annual Pro Sale. View Details »

Programming with Birds - There is a Bluebird in My Talk That Wants to Get Out (LambdAle 2018)

Programming with Birds - There is a Bluebird in My Talk That Wants to Get Out (LambdAle 2018)

These are the slides for my talk at LambdAle 2018 in London, UK.

The whole idea here is to show people how Lambda Calculus works, how to encode data using functions and demonstrate its Turing-Completeness. I then proceed to explain combinators and replace the functions with combinators and then replace the combinators themselves with only S and K, the smallest combinator base possible for completeness.

This talk is inspired in the excellent "Programming With Nothing" by Tom Stuart, the main difference is that in this one I use JavaScript and I also added combinators and SK calculus. My take is a bit different, but Tom's talk is definitely worth watching (one of the best I've ever seen).

Lucas Fernandes da Costa

September 01, 2018
Tweet

More Decks by Lucas Fernandes da Costa

Other Decks in Programming

Transcript

  1. 100
    1
    There is a Bluebird in my
    Talk that Wants to Get out
    Programming with birds
    THEWIZARDLUCAS

    View Slide

  2. 100
    2
    PROGRAMMING
    IS A GREAT
    EXPERIENCE

    View Slide

  3. 100
    3
    I want to make it awful.
    And I'm not the first one.
    Programming
    with nothing
    By Tom Stuart

    View Slide

  4. 100
    4
    I want to make it awful.
    But I want to take it one step further.
    z
    Programming
    with nothing
    By Tom Stuart

    View Slide

  5. 100
    5
    Ruin
    JavaScript
    Part 1

    View Slide

  6. 100
    6
    Ruin
    JavaScript
    Ruin
    JavaScript
    with birds
    Part 1 Part 2

    View Slide

  7. 100
    7
    Ruin
    JavaScript
    Ruin
    JavaScript
    with birds
    Ruin
    JavaScript
    and The
    Birds
    Part 1 Part 2 Part 3

    View Slide

  8. 100
    8
    Ruin
    JavaScript
    Ruin
    JavaScript
    with birds
    Ruin
    JavaScript
    and The
    Birds
    Part 1 Part 2 Part 3
    Apologise
    Part 4

    View Slide

  9. 100
    9
    Ruining JavaScript
    Part 1

    View Slide

  10. 100
    10
    We are spoiled
    We program with way too much:
    • Objects
    • Classes/Inheritance
    • Booleans
    • Numbers
    • Builtin functions
    • Builtin data structures

    View Slide

  11. 100
    11
    This is what we're
    going to rewrite
    T H E W R E C K

    View Slide

  12. 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

    View Slide

  13. 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)

    View Slide

  14. 100
    14
    Lambda
    Calculus
    This is
    λ

    View Slide

  15. 100
    15
    Our first step
    T H E W R E C K

    View Slide

  16. 100
    16
    Replacing
    Booleans
    P a r t 1

    View Slide

  17. 100
    17
    Why do we need booleans anyway?
    Replacing Numbers
    if { this } else { that }
    const val = boolean ? "Yes": "No"

    View Slide

  18. 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.

    View Slide

  19. 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.

    View Slide

  20. 100
    20
    Booleans
    Replacing Numbers
    const FALSE = a => b => b
    const TRUE = a => b => a

    View Slide

  21. 100
    21
    Booleans
    Replacing Numbers
    const SECOND = a => b => b
    const FIRST = a => b => a

    View Slide

  22. 100
    22
    Booleans
    Replacing Numbers

    View Slide

  23. 100
    23
    Booleans
    Replacing Numbers
    What about the if keyword?

    View Slide

  24. 100
    24
    Booleans
    Replacing Numbers
    What about the if keyword?
    if { this } else { that }

    View Slide

  25. 100
    25
    Booleans
    Replacing Numbers
    What about the if keyword?
    BOOLEAN { this } else { that }

    View Slide

  26. 100
    26
    Booleans
    Replacing Numbers
    What about the if keyword?
    BOOLEAN { this } { that }

    View Slide

  27. 100
    27
    Booleans
    Replacing Numbers
    What about the if keyword?
    BOOLEAN { this } { that }
    The boolean itself is our this.

    View Slide

  28. 100
    28
    Booleans
    Replacing Numbers
    Let's have some sugar

    View Slide

  29. 100
    29
    What can we replace?
    Replacing arithmetics

    View Slide

  30. 100
    30
    Replacing
    Numbers
    P a r t 1

    View Slide

  31. 100
    31
    What is a number, anyway?
    Replacing Numbers
    2

    View Slide

  32. 100
    32
    What is a number, anyway?
    Replacing Numbers
    2

    View Slide

  33. 100
    33
    What is a number, anyway?
    Replacing Numbers
    2

    View Slide

  34. 100
    34
    What is a number, anyway?
    Replacing Numbers
    2
    Representation Meaning Meaning

    View Slide

  35. 100
    35
    How can we represent quantities with functions?
    Replacing Numbers

    View Slide

  36. 100
    36
    How can we represent quantities with functions?
    Replacing Numbers
    Function applications!

    View Slide

  37. 100
    37
    How can we represent quantities with functions?
    Replacing Numbers
    Function applications!

    View Slide

  38. 100
    38
    How can we represent quantities with functions?
    Replacing Numbers
    Function applications!

    View Slide

  39. 100
    39
    How can we represent quantities with functions?
    Replacing Numbers
    Function applications!

    View Slide

  40. 100
    40
    From meaning to representation
    Replacing Numbers

    View Slide

  41. 100
    41
    From meaning to representation
    Replacing Numbers

    View Slide

  42. 100
    42
    From meaning to representation
    Replacing Numbers

    View Slide

  43. 100
    43
    From meaning to representation
    Replacing Numbers

    View Slide

  44. 100
    44
    From meaning to representation
    Replacing Numbers

    View Slide

  45. 100
    45
    What can we replace?
    Replacing arithmetics

    View Slide

  46. 100
    46
    Replacing
    Arithmetics
    P a r t 1

    View Slide

  47. 100
    47
    The successor function
    Replacing arithmetics
    const ONE = f => x => f(x)
    const TWO = f => x => f(f(x))

    View Slide

  48. 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))

    View Slide

  49. 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))

    View Slide

  50. 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))

    View Slide

  51. 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))

    View Slide

  52. 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)

    View Slide

  53. 100
    53
    The addition function
    Replacing arithmetics
    const TWO = f => x => f(f(x))
    const ONE = f => x => f(x)

    View Slide

  54. 100
    54
    The addition function
    Replacing arithmetics
    const TWICE = f => x => f(f(x))
    const ONE = f => x => f(x)

    View Slide

  55. 100
    55
    The addition function
    Replacing arithmetics
    twice(SUCCESSOR(ONE))
    ADDITION(TWO)(ONE)

    View Slide

  56. 100
    56
    The multiplication function
    Replacing arithmetics

    View Slide

  57. 100
    57
    The multiplication function
    Replacing arithmetics
    multiplication(THREE)(TWO)

    View Slide

  58. 100
    58
    The multiplication function
    Replacing arithmetics
    THRICE(TWO)
    multiplication(THREE)(TWO)

    View Slide

  59. 100
    59
    What can we replace?
    Replacing arithmetics

    View Slide

  60. 100
    60
    What can we replace?
    Replacing arithmetics

    View Slide

  61. 100
    61
    What is next?
    Replacing arithmetics
    Subtraction

    View Slide

  62. 100
    62
    The predecessor function
    Replacing arithmetics
    Predecessor
    TWO ONE

    View Slide

  63. 100
    63
    The predecessor function
    Replacing arithmetics
    TWO ONE
    ?

    View Slide

  64. 100
    64
    Pairs
    Replacing arithmetics

    View Slide

  65. 100
    65
    Pairs
    Replacing arithmetics
    First we store two values.

    View Slide

  66. 100
    66
    Pairs
    Replacing arithmetics
    Then we tell which one we want.

    View Slide

  67. 100
    67
    Pairs
    Replacing arithmetics

    View Slide

  68. 100
    68
    Pairs
    Replacing arithmetics

    View Slide

  69. 100
    69
    Pairs
    Replacing arithmetics

    View Slide

  70. 100
    70
    Pairs
    Replacing arithmetics

    View Slide

  71. 100
    71
    Pairs
    Replacing arithmetics

    View Slide

  72. 100
    72
    Replacing arithmetics
    Incrementing Pairs
    { 0, 1 }
    { 0, 0 }

    View Slide

  73. 100
    73
    Replacing arithmetics
    { 1, 2 }
    Incrementing Pairs
    { 0, 1 }
    { 0, 0 }

    View Slide

  74. 100
    74
    Replacing arithmetics
    { 1, 2 } { 2, 3 }
    Incrementing Pairs
    { 0, 1 }
    { 0, 0 }

    View Slide

  75. 100
    75
    Replacing arithmetics
    { 1, 2 } { 2, 3 }
    { 4, 5 }
    Incrementing Pairs
    { 0, 1 }
    { 0, 0 }

    View Slide

  76. 100
    76
    Replacing arithmetics
    { 1, 2 } { 2, 3 }
    { 4, 5 } { 5, 6 }
    Incrementing Pairs
    { 0, 1 }
    { 0, 0 }

    View Slide

  77. 100
    77
    Replacing arithmetics
    { 1, 2 } { 2, 3 }
    { 4, 5 } { 5, 6 }
    { 0, 0 }
    Thrice
    Incrementing Pairs
    { 0, 1 }
    { 0, 0 }

    View Slide

  78. 100
    78
    Replacing arithmetics
    { 1, 2 } { 2, 3 }
    { 4, 5 } { 5, 6 }
    { 0, 0 }
    Thrice { 0, 1 }
    Incrementing Pairs
    { 0, 1 }
    { 0, 0 }
    x1

    View Slide

  79. 100
    79
    Replacing arithmetics
    { 1, 2 } { 2, 3 }
    { 4, 5 } { 5, 6 }
    { 0, 0 }
    Thrice { 1, 2 }
    Incrementing Pairs
    { 0, 1 }
    { 0, 0 }
    x2

    View Slide

  80. 100
    80
    Replacing arithmetics
    { 1, 2 } { 2, 3 }
    { 4, 5 } { 5, 6 }
    { 0, 0 }
    Thrice { 2, 3 }
    Incrementing Pairs
    { 0, 1 }
    { 0, 0 }
    x3

    View Slide

  81. 100
    81
    Replacing arithmetics
    { 1, 2 } { 2, 3 }
    { 4, 5 } { 5, 6 }
    { 0, 0 }
    Thrice { 2, 3 }
    Incrementing Pairs
    { 0, 1 }
    { 0, 0 }
    x3

    View Slide

  82. 100
    82
    Replacing arithmetics
    { 1, 2 } { 2, 3 }
    { 4, 5 } { 5, 6 }
    { 0, 0 }
    Three { 2, 3 }
    Incrementing Pairs
    { 0, 1 }
    { 0, 0 }
    x3
    Predecessor

    View Slide

  83. 100
    83
    Replacing arithmetics
    The Predecessor Function
    N x INCREMENT_PAIR(0, 0)
    First of

    View Slide

  84. 100
    84
    Replacing arithmetics
    The Subtraction Function
    Predecessor of N K times

    View Slide

  85. 100
    85
    What can we replace?
    Replacing arithmetics

    View Slide

  86. 100
    86
    What can we replace?
    Replacing arithmetics

    View Slide

  87. 100
    87
    Replacing
    Boolean
    Operators
    P a r t 1

    View Slide

  88. 100
    88
    AND { true } { true } is true
    AND
    Replacing Boolean Operators
    Any other combinations are false

    View Slide

  89. 100
    89
    AND { true } { true } is true
    AND
    Replacing Boolean Operators
    Any other combinations are false

    View Slide

  90. 100
    90
    AND
    Replacing Boolean Operators
    If a is true b must be true
    If a is false it returns itself

    View Slide

  91. 100
    91
    It is true if any values are { true }
    OR
    Replacing Boolean Operators

    View Slide

  92. 100
    92
    It is true if any values are { true }
    OR
    Replacing Boolean Operators

    View Slide

  93. 100
    93
    It is true if any values are { true }
    OR
    Replacing Boolean Operators
    If a is true it returns itself, otherwise tries b

    View Slide

  94. 100
    94
    NOT
    Replacing Boolean Operators
    Inverts any { true } and { false }

    View Slide

  95. 100
    95
    NOT
    Replacing Boolean Operators
    Inverts any { true } and { false }

    View Slide

  96. 100
    96
    NOT
    Replacing Boolean Operators
    Inverts any { true } and { false }

    View Slide

  97. 100
    97
    NOT
    Replacing Boolean Operators
    Inverts any { true } and { false }
    Takes a boolean f and applies it to the arguments in reverse order

    View Slide

  98. 100
    98
    NOT
    Replacing Boolean Operators
    Inverts the selector
    Takes a boolean f and applies it to the arguments in reverse order

    View Slide

  99. 100
    99
    NOT
    Replacing Boolean Operators
    Inverts the selector
    Takes a boolean f and applies it to the arguments in reverse order

    View Slide

  100. 100
    100
    IS ZERO
    Replacing Boolean Operators
    Checks if a is zero
    If n is zero, it will return the last TRUE

    View Slide

  101. 100
    101
    EQUALS
    Replacing Boolean Operators
    Checks if a equals b
    Checks if both n <= k and k <= n

    View Slide

  102. 100
    102
    GREATER THAN
    Replacing Boolean Operators
    The opposite of lesser or equal

    View Slide

  103. 100
    103
    What can we replace?
    Replacing Boolean Operators

    View Slide

  104. 100
    104
    What can we replace?
    Replacing Boolean Operators

    View Slide

  105. 100
    105
    Replacing everything with functions
    Ruining Javascript

    View Slide

  106. 100
    106
    Ruining Javascript
    Replacing everything with functions

    View Slide

  107. 100
    107
    Ruining Javascript
    Replacing everything with functions

    View Slide

  108. 100
    108
    Ruining Javascript
    Replacing everything with functions

    View Slide

  109. 100
    109
    Ruining Javascript
    Replacing everything with functions

    View Slide

  110. 100
    110
    Ruining Javascript
    Replacing everything with functions

    View Slide

  111. 100
    111
    Ruining Javascript
    We could replace the
    names of these functions
    by their definitions and
    remove all variable names
    Replacing everything with functions

    View Slide

  112. 100
    112
    Unleash Hell
    Ruining Javascript
    But I will save you from
    this pain for now

    View Slide

  113. 100
    113
    Ruining JavaScript With Birds
    Part 2

    View Slide

  114. 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

    View Slide

  115. 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

    View Slide

  116. 100
    116
    Combinators
    The Idiot Bird

    View Slide

  117. 100
    117
    Combinators
    The Kestrel

    View Slide

  118. 100
    118
    Combinators
    The Kestrel

    View Slide

  119. 100
    119
    Combinators
    The Cardinal

    View Slide

  120. 100
    120
    Combinators
    The Vireo

    View Slide

  121. 100
    121
    Combinators
    The Bluebird

    View Slide

  122. 100
    122
    Combinators
    The Thrush

    View Slide

  123. 100
    123
    Combinators
    The Starling

    View Slide

  124. 100
    124
    Combinators
    Replacing Functions (Successor)
    const SUCCESSOR = n => f => x => f(n(f)(x))

    View Slide

  125. 100
    125
    Combinators
    const SUCCESSOR = n => f => B(f)(n(f))
    Replacing Functions (Successor)

    View Slide

  126. 100
    126
    Combinators
    const SUCCESSOR = n => f => B(f)(n(f))
    Replacing Functions (Successor)

    View Slide

  127. 100
    127
    Combinators
    const SUCCESSOR = n => f => B(f)(n(f))
    const SUCCESSOR = S(B)
    Replacing Functions (Successor)

    View Slide

  128. 100
    128
    Combinators
    Replacing Functions (Addition)
    const ADDITION = n => k => n(SUCESSOR)(k)

    View Slide

  129. 100
    129
    Combinators
    Replacing Functions (Addition)
    const ADDITION = n => k => n(S(B))(k)

    View Slide

  130. 100
    130
    Combinators
    Replacing Functions (Addition)
    const ADDITION = k => Th(S(B))(k)

    View Slide

  131. 100
    131
    Combinators
    Replacing Functions (Addition)
    const ADDITION = C(Th)(Th(S(B)))

    View Slide

  132. 100
    132
    Nice birds m8
    Ruining Javascript

    View Slide

  133. 100
    133
    Ruining Javascript
    B Th
    C
    K
    KI
    V
    S
    Nice birds m8

    View Slide

  134. 100
    134
    Ruining Javascript
    Haskell's Data.Aviary.Birds
    This module is intended
    for illustration (the type
    signatures!) rather than
    utility.

    View Slide

  135. 100
    135
    Ruining Javascript
    Nice birds m8

    View Slide

  136. 100
    136
    Ruining JavaScript and the Birds
    Part 3

    View Slide

  137. 100
    137
    WHAT IF I TOLD YOU
    YOU ONLY NEED
    TWO COMBINATORS?
    Yes, that's right. Two.

    View Slide

  138. SK
    SK Calculus
    S K

    View Slide

  139. 100
    139
    SK Calculus
    Replacing Numbers
    People also call it SKI Calculus

    View Slide

  140. 100
    140
    SK Calculus
    Replacing Numbers
    People also call it SKI Calculus
    Because it's more convenient to have I

    View Slide

  141. 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

    View Slide

  142. 100
    142
    SK Calculus
    Replacing Numbers
    const KI = K(S(K)(K))

    View Slide

  143. 100
    143
    SK Calculus
    Replacing Numbers
    const KI = K(I)

    View Slide

  144. 100
    144
    SK Calculus
    Replacing Numbers
    const KI = K(I)
    const B = S(K(S))(K)

    View Slide

  145. 100
    145
    SK Calculus
    Replacing Numbers
    const KI = K(I)
    const B = S(K(S))(K)
    const V = PAIRING

    View Slide

  146. 100
    146
    SK Calculus
    Replacing Numbers
    const V =
    ((S(K((S((S(K((S(KS)
    )K)))S))(KK))))
    ((S(K(S((SK)K))))K))

    View Slide

  147. 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.

    View Slide

  148. 100
    148
    What does this mean?

    View Slide

  149. 100
    149
    What does this mean?
    If we can replace all code by functions

    View Slide

  150. 100
    150
    What does this mean?
    If we can replace all code by functions
    Replace all functions by combinators

    View Slide

  151. 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

    View Slide

  152. 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

    View Slide

  153. 100
    153
    What does this mean?
    https://crypto.stanford.edu/~blynn/lambda/sk.html
    http://xn--wxak1a.com/blog/Combinators.html

    View Slide

  154. 100
    154
    Apologising
    Part 4

    View Slide

  155. 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

    View Slide

  156. 100
    156
    Thank you!
    Shhh, no tears. Only lambdas now.
    @THEWIZARDLUCAS (TWITTER)
    @LUCASFCOSTA (GITHUB)
    LUCASFCOSTA.COM

    View Slide

  157. 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é

    View Slide

  158. 100
    158
    Thank you!
    Shhh, no tears. Only lambdas now.
    @THEWIZARDLUCAS (TWITTER)
    @LUCASFCOSTA (GITHUB)
    LUCASFCOSTA.COM

    View Slide