Upgrade to Pro — share decks privately, control downloads, hide ads and more …

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

5a3b0043a07f45bba968c22b7e7bf296?s=128

Lucas Fernandes da Costa

September 01, 2018
Tweet

Transcript

  1. 100 1 There is a Bluebird in my Talk that

    Wants to Get out Programming with birds THEWIZARDLUCAS
  2. 100 2 PROGRAMMING IS A GREAT EXPERIENCE

  3. 100 3 I want to make it awful. And I'm

    not the first one. Programming with nothing By Tom Stuart
  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
  5. 100 5 Ruin JavaScript Part 1

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

    Part 2
  7. 100 7 Ruin JavaScript Ruin JavaScript with birds Ruin JavaScript

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

    and The Birds Part 1 Part 2 Part 3 Apologise Part 4
  9. 100 9 Ruining JavaScript Part 1

  10. 100 10 We are spoiled We program with way too

    much: • Objects • Classes/Inheritance • Booleans • Numbers • Builtin functions • Builtin data structures
  11. 100 11 This is what we're going to rewrite T

    H E W R E C K
  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
  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)
  14. 100 14 Lambda Calculus This is λ

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

    E C K
  16. 100 16 Replacing Booleans P a r t 1

  17. 100 17 Why do we need booleans anyway? Replacing Numbers

    if { this } else { that } const val = boolean ? "Yes": "No"
  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.
  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.
  20. 100 20 Booleans Replacing Numbers const FALSE = a =>

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

    b => b const FIRST = a => b => a
  22. 100 22 Booleans Replacing Numbers

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

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

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

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

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

    BOOLEAN { this } { that } The boolean itself is our this.
  28. 100 28 Booleans Replacing Numbers Let's have some sugar

  29. 100 29 What can we replace? Replacing arithmetics

  30. 100 30 Replacing Numbers P a r t 1

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

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

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

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

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

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

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

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

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

    Numbers Function applications!
  40. 100 40 From meaning to representation Replacing Numbers

  41. 100 41 From meaning to representation Replacing Numbers

  42. 100 42 From meaning to representation Replacing Numbers

  43. 100 43 From meaning to representation Replacing Numbers

  44. 100 44 From meaning to representation Replacing Numbers

  45. 100 45 What can we replace? Replacing arithmetics

  46. 100 46 Replacing Arithmetics P a r t 1

  47. 100 47 The successor function Replacing arithmetics const ONE =

    f => x => f(x) const TWO = f => x => f(f(x))
  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))
  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))
  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))
  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))
  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)
  53. 100 53 The addition function Replacing arithmetics const TWO =

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

    f => x => f(f(x)) const ONE = f => x => f(x)
  55. 100 55 The addition function Replacing arithmetics twice(SUCCESSOR(ONE)) ADDITION(TWO)(ONE)

  56. 100 56 The multiplication function Replacing arithmetics

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

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

  59. 100 59 What can we replace? Replacing arithmetics

  60. 100 60 What can we replace? Replacing arithmetics

  61. 100 61 What is next? Replacing arithmetics Subtraction

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

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

  64. 100 64 Pairs Replacing arithmetics

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

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

    we want.
  67. 100 67 Pairs Replacing arithmetics

  68. 100 68 Pairs Replacing arithmetics

  69. 100 69 Pairs Replacing arithmetics

  70. 100 70 Pairs Replacing arithmetics

  71. 100 71 Pairs Replacing arithmetics

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

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

    { 0, 1 } { 0, 0 }
  74. 100 74 Replacing arithmetics { 1, 2 } { 2,

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

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

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

    3 } { 4, 5 } { 5, 6 } { 0, 0 } Thrice Incrementing Pairs { 0, 1 } { 0, 0 }
  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
  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
  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
  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
  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
  83. 100 83 Replacing arithmetics The Predecessor Function N x INCREMENT_PAIR(0,

    0) First of
  84. 100 84 Replacing arithmetics The Subtraction Function Predecessor of N

    K times
  85. 100 85 What can we replace? Replacing arithmetics

  86. 100 86 What can we replace? Replacing arithmetics

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

  88. 100 88 AND { true } { true } is

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

    true AND Replacing Boolean Operators Any other combinations are false
  90. 100 90 AND Replacing Boolean Operators If a is true

    b must be true If a is false it returns itself
  91. 100 91 It is true if any values are {

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

    true } OR Replacing Boolean Operators
  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
  94. 100 94 NOT Replacing Boolean Operators Inverts any { true

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

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

    } and { false }
  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
  98. 100 98 NOT Replacing Boolean Operators Inverts the selector Takes

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

    a boolean f and applies it to the arguments in reverse order
  100. 100 100 IS ZERO Replacing Boolean Operators Checks if a

    is zero If n is zero, it will return the last TRUE
  101. 100 101 EQUALS Replacing Boolean Operators Checks if a equals

    b Checks if both n <= k and k <= n
  102. 100 102 GREATER THAN Replacing Boolean Operators The opposite of

    lesser or equal
  103. 100 103 What can we replace? Replacing Boolean Operators

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

  105. 100 105 Replacing everything with functions Ruining Javascript

  106. 100 106 Ruining Javascript Replacing everything with functions

  107. 100 107 Ruining Javascript Replacing everything with functions

  108. 100 108 Ruining Javascript Replacing everything with functions

  109. 100 109 Ruining Javascript Replacing everything with functions

  110. 100 110 Ruining Javascript Replacing everything with functions

  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
  112. 100 112 Unleash Hell Ruining Javascript But I will save

    you from this pain for now
  113. 100 113 Ruining JavaScript With Birds Part 2

  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
  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
  116. 100 116 Combinators The Idiot Bird

  117. 100 117 Combinators The Kestrel

  118. 100 118 Combinators The Kestrel

  119. 100 119 Combinators The Cardinal

  120. 100 120 Combinators The Vireo

  121. 100 121 Combinators The Bluebird

  122. 100 122 Combinators The Thrush

  123. 100 123 Combinators The Starling

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

    => f => x => f(n(f)(x))
  125. 100 125 Combinators const SUCCESSOR = n => f =>

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

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

    B(f)(n(f)) const SUCCESSOR = S(B) Replacing Functions (Successor)
  128. 100 128 Combinators Replacing Functions (Addition) const ADDITION = n

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

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

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

  132. 100 132 Nice birds m8 Ruining Javascript

  133. 100 133 Ruining Javascript B Th C K KI V

    S Nice birds m8
  134. 100 134 Ruining Javascript Haskell's Data.Aviary.Birds This module is intended

    for illustration (the type signatures!) rather than utility.
  135. 100 135 Ruining Javascript Nice birds m8

  136. 100 136 Ruining JavaScript and the Birds Part 3

  137. 100 137 WHAT IF I TOLD YOU YOU ONLY NEED

    TWO COMBINATORS? Yes, that's right. Two.
  138. SK SK Calculus S K

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

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

    SKI Calculus Because it's more convenient to have I
  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
  142. 100 142 SK Calculus Replacing Numbers const KI = K(S(K)(K))

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

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

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

    const B = S(K(S))(K) const V = PAIRING
  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))
  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.
  148. 100 148 What does this mean?

  149. 100 149 What does this mean? If we can replace

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

    all code by functions Replace all functions by combinators
  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
  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
  153. 100 153 What does this mean? https://crypto.stanford.edu/~blynn/lambda/sk.html http://xn--wxak1a.com/blog/Combinators.html

  154. 100 154 Apologising Part 4

  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
  156. 100 156 Thank you! Shhh, no tears. Only lambdas now.

    @THEWIZARDLUCAS (TWITTER) @LUCASFCOSTA (GITHUB) LUCASFCOSTA.COM
  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é
  158. 100 158 Thank you! Shhh, no tears. Only lambdas now.

    @THEWIZARDLUCAS (TWITTER) @LUCASFCOSTA (GITHUB) LUCASFCOSTA.COM