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

Abstract Machines (dpc14)

Abstract Machines (dpc14)

Igor Wiedler

June 27, 2014
Tweet

More Decks by Igor Wiedler

Other Decks in Programming

Transcript

  1. Abstrakte Maschinen

  2. @igorwhiletrue

  3. None
  4. Computers are grown-up tamagotchis

  5. Programming is hard

  6. Why?

  7. • Link between our universe and computational universe • Cellular

    automata are self-replicating abstract machines • Humans are self-replicating biological machines (down to the cellular level) • Or is the entire universe a single machine?
  8. • Abstract machine is a model of computation • Or

    a really simple interpreter • Cellular automata are abstract machines
  9. Conway’s Game of Life

  10. None
  11. • if alive • 2 or 3 neighbours to survive

    • if dead • exactly 3 neighbours to spawn • else • cell is dead
  12. None
  13. 1 1 2 1 3 5 2 2 1 2

    2 2 2 3 2 1
  14. 1 1 2 1 3 5 2 2 1 2

    2 2 2 3 2 1
  15. 1 2 1 3 5 2 2 2 2 2

    2 3 2 1
  16. 1 2 1 3 5 2 2 2 2 2

    2 3 2 1
  17. None
  18. None
  19. None
  20. None
  21. None
  22. None
  23. None
  24. None
  25. None
  26. None
  27. None
  28. None
  29. None
  30. • Still lifes • Oscillators • Spaceships • Guns, puffers,

    breeders
  31. • Cellular automaton • Metaphor for life • Complexity, emergence

    & stuff
  32. • Other cellular automata • Codd’s automaton (8 states) •

    Langton’s loops (8 states) • Wireworld (4 states)
  33. Deterministic finite automaton

  34. None
  35. • Endlicher automat • Regular expressions • Directed state transition

    graph
  36. /foo/

  37. /foo|bar/

  38. /\d*/

  39. /\d+/

  40. /tro(lo)+/

  41. “trololo”

  42. “rololo”

  43. “ololo”

  44. “lolo”

  45. “olo”

  46. “lo”

  47. “o”

  48. “”

  49. Anas Ramadan, from The Noun Project

  50. “trolu”

  51. “rolu”

  52. “olu”

  53. “lu”

  54. “u”

  55. Anas Ramadan, from The Noun Project

  56. • M = (Q, Σ, δ, q0, F) • Rule

    δ = (qi, a → qi1) • O(1) space, O(n) time • Can accept regular languages
  57. • Regular expressions • Lexical analysis • Network protocols •

    Game states • Business rules • Workflows
  58. $rules = [ 0 => ['t' => 1], 1 =>

    ['r' => 2], 2 => ['o' => 3], ... ]; ! $tokens = ['t', 'r', 'o', 'l', ‘o', ‘l', 'o', 'EOF']; ! foreach ($tokens as $token) { if (!isset($rules[$state][$token])) { throw new NoTransitionException(); } ! $state = $rules[$state][$token]; } ! $accepted = in_array($state, $accept_states);
  59. Pushdown automaton

  60. None
  61. • Kellerautomat • Introduces a stack • Can accept nested

    structures
  62. None
  63. { "look!": [ "it's a JSON", {"yes!": "nesting!"} ] }

  64. None
  65. (define Y (lambda (le) ((lambda (f) (f f)) (lambda (f)

    (le (lambda (x) ((f f) x)))))))
  66. sexpr = atom | (sexpr*) atom = letter alphanum* |

    number+ alphanum = letter | number letter = a | b | ... | z number = 1 | 2 | ... | 9
  67. None
  68. S = ε | (S) | SS

  69. None
  70. None
  71. None
  72. e “( ( ( ( ) ) ) ( )

    )”
  73. x e “( ( ( ) ) ) ( )

    )”
  74. x x e “( ( ) ) ) ( )

    )”
  75. x x x e “( ) ) ) ( )

    )”
  76. x x x x e “) ) ) ( )

    )”
  77. x x x e “) ) ( ) )”

  78. x x e “) ( ) )”

  79. x e “( ) )”

  80. x x e “) )”

  81. x e “)”

  82. e “”

  83. e Anas Ramadan, from The Noun Project

  84. • M = (Q, Σ, Γ, δ, q0, Zo, F)

    • Rule δ = (qi, a, sj → qi1, sj1) • O(n) space, O(n) time • Can accept context-free languages
  85. • Parsers • Data formats • Network protocols • Interpreters

    • Query languages • Stack machines
  86. $rules = [ 0 => ['(' => ['e' => [0,

    ['e', 'x']]]], ... ]; ! $stack = new SplStack(); $stack->push($init_stack); ! foreach ($tokens as $token) { $top = $stack->pop(); ! if (!isset($rules[$state][$token][$top])) { throw new NoTransitionException(); } ! list($state, $push_tokens) = $rules[$state][$token][$top]; ! foreach ($push_tokens as $push_token) { $stack->push($push_token); } } ! $accepted = in_array($state, $accept_states);
  87. $rules = [ 0 => ['(' => ['e' => [0,

    ['e', 'x']]]], ... ]; ! $stack = new SplStack(); $stack->push($init_stack); ! foreach ($tokens as $token) { $top = $stack->pop(); ! if (!isset($rules[$state][$token][$top])) { throw new NoTransitionException(); } ! list($state, $push_tokens) = $rules[$state][$token][$top]; ! foreach ($push_tokens as $push_token) { $stack->push($push_token); } } ! $accepted = in_array($state, $accept_states);
  88. Turing Machine

  89. None
  90. None
  91. None
  92. None
  93. None
  94. None
  95. 0 0 1 1

  96. 0 0 1 1

  97. 0 0 1 0

  98. 0 0 0 0

  99. 0 1 0 0

  100. 0 1 0 0

  101. 0 1 0 0

  102. 0 1 0 0

  103. 0 1 0 0

  104. 0 1 0 0

  105. 0 1 0 0

  106. 0 1 0 1

  107. 0 1 0 1

  108. 0 1 0 1

  109. 0 1 0 1

  110. 0 1 0 1 Anas Ramadan, from The Noun Project

  111. • M = (Q, Σ, Γ, δ, q0, b, F)

    • Rule δ = (qi, aj → qi1, aj1, dk) • Can accept recursively enumerable languages • Can loop forever
  112. This machine can run any algorithm

  113. This machine can run any algorithm etsy.com/shop/sharpwriter

  114. while (!in_array($state, $accept_states)) { $read_val = isset($tape[$position]) ? $tape[$position] :

    '_'; ! if (!isset($rules[$state][$read_val])) { throw new NoTransitionException(); } ! list($write_val, $move_dir, $state) = $rules[$state][$read_val]; ! $tape[$position] = $write_val; ! if ('l' === $move_dir) { $position--; if ($position < 0) { $position++; array_unshift($tape, '_'); } } else if ('r' === $move_dir) { $position++; if ($position >= count($tape)) { array_push($tape, '_'); } } }
  115. while (!in_array($state, $accept_states)) { $read_val = isset($tape[$position]) ? $tape[$position] :

    '_'; ! if (!isset($rules[$state][$read_val])) { throw new NoTransitionException(); } ! list($write_val, $move_dir, $state) = $rules[$state][$read_val]; ! $tape[$position] = $write_val; ! if ('l' === $move_dir) { $position--; if ($position < 0) { $position++; array_unshift($tape, '_'); } } else if ('r' === $move_dir) { $position++; if ($position >= count($tape)) { array_push($tape, '_'); } } } oh no! unbounded loop!
  116. $rules = [ 1 => ['0' => ['1', 'r', 2],

    '_' => ['1', 'r', 2], '1' => ['0', 'l', 1]], 2 => ['0' => ['0', 'r', 2], '1' => ['1', 'r', 2], '_' => ['_', 'l', 3]], ];
  117. None
  118. None
  119. None
  120. Universality

  121. • For every problem there is a special purpose brain

    that solves it as fast as possible.
 
 — Konrad Zuse, 1937
  122. add increment one-third

  123. add increment one-third add increment one-third

  124. add increment one-third

  125. add increment one-third U

  126. U M

  127. None
  128. • Stored-program computer (John von Neumann) • Programs as data

    • PHPPHP
  129. Turing completeness

  130. • System capable of emulating a turing machine • Unbounded

    storage • Conditional branching • Recursion
  131. • Universal Turing Machine • λ-calculus (Alonzo Church) • Game

    of Life • Brainfuck • PHP
  132. • If PHP can only do as much as a

    turing machine, why bother? • Beware of the Turing tar-pit in which everything is possible but nothing of interest is easy. • Epigrams on Programming by Alan Perlis
  133. Self-reference

  134. <?php $data = <<<'DATA' $program = <<<PROGRAM <?php \$data =

    <<<'DATA'\n$data\nDATA; $data ! PROGRAM; echo $program; DATA; $program = <<<PROGRAM <?php \$data = <<<'DATA'\n$data\nDATA; $data ! PROGRAM; echo $program;
  135. Recursion

  136. call_user_func( function ($x) { return $x($x); }, function ($x) {

    return $x($x); } );
  137. while (true);

  138. None
  139. None
  140. Russell’s paradox

  141. • Let R be the set of all sets that

    do not contain themselves • Does R contain itself?
  142. • Barber paradox • A town with just one barber

    • Everyone either shaves themselves or goes to the barber • Barber shaves all who do not shave themselves • Who shaves the barber?
  143. • Liar paradox: “This sentence is false.” • Type theory

    • Hierarchy of types avoids self-reference • And then came Gödel in 1931 and smashed the foundation of mathematical reasoning
  144. Entscheidungsproblem

  145. • David Hilbert asks for an algorithm that decides if

    a statement in first-order logic is universally valid • Halting problem can be reduced to Entscheidungsproblem • Machine that determines if another machine will halt
  146. Halts?

  147. Halts? Negate

  148. Halts? Negate Copy

  149. Halts? Negate Copy { X

  150. ( ) X X

  151. Halts? Negate Copy X

  152. Halts? Negate Copy X X

  153. Halts? Negate Copy true X X

  154. Halts? Negate Copy X ∞ true X

  155. Halts? Negate Copy X ∞ true X X X }

  156. Halts? Negate Copy false X X

  157. Halts? Negate Copy false halting now X X

  158. Halts? Negate Copy false halting now X X X X

    }
  159. • Proof by contradiction • Decision machine cannot exist •

    Rice’s theorem generalises this • We are screwed
  160. Ways to cope

  161. None
  162. None
  163. Use finite state machines in parts of your programs

  164. Introduce high-level concepts such as bounded loops

  165. Build restricted subsets of computing such as type systems

  166. Conclusion

  167. Programming is hard

  168. Church-Turing Thesis

  169. The brain is a machine

  170. The universe is a machine

  171. None
  172. Questions? • joind.in/10868 • github.com/igorw • conway-php • turing-php •

    lambda-php • @igorwhiletrue
  173. function evaluate($exp, array $env = []) { if (is_string($exp)) {

    return $env[$exp]; } ! if ('λ' === first($exp)) { list($_, $arg, $body) = $exp; return ['λ', $arg, $body, $env]; } ! $f = evaluate(first($exp), $env); $arg = evaluate(second($exp), $env); return apply($f, $arg); } ! function apply($f, $x) { list($_, $arg, $body, $env) = $f; return evaluate($body, array_merge($env, [$arg => $x])); }