Pro Yearly is on sale from $80 to $50! »

Abstract Machines (bephpug)

A4b95be2145cc46f891707b6db9dd82d?s=47 Igor Wiedler
February 04, 2014

Abstract Machines (bephpug)

A4b95be2145cc46f891707b6db9dd82d?s=128

Igor Wiedler

February 04, 2014
Tweet

Transcript

  1. Abstrakte Maschinen

  2. @igorwhiletrue

  3. None
  4. Programming is hard

  5. Why?

  6. • 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?
  7. • Abstract machine is a model of computation • Cellular

    automata are abstract machines
  8. Conway’s Game of Life

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

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

    2 2 2 3 2 1
  13. 1 1 2 1 3 5 2 2 1 2

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

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

    2 3 2 1
  16. None
  17. None
  18. None
  19. None
  20. None
  21. None
  22. None
  23. None
  24. • Still lifes • Oscillators • Spaceships • Guns, puffers,

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

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

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

  28. • Endlicher automat • Regular expressions • Directed state transition

    graph
  29. None
  30. (refs|fixes|closes) #\d*

  31. (refs|fixes|closes) #\d*

  32. (refs|fixes|closes) #\d*

  33. fixes #1234

  34. ixes #1234

  35. xes #1234

  36. es #1234

  37. s #1234

  38. #1234

  39. #1234

  40. 1234

  41. 234

  42. 34

  43. 4

  44. None
  45. None
  46. • M = (Q, Σ, δ, q0, F) • Rule

    δ = (qi, a → qi1) • Can accept regular languages
  47. • Regular expressions • Network protocols • Game states •

    Business rules • Workflows • Queues
  48. $rules = [ 0 => ['c' => 1, 'f' =>

    7, 'r' => 9], 1 => ['l' => 2], 2 => ['o' => 3], ... ]; ! $tokens = ['f', 'i', 'x', 'e', 's', ' ', '#', '1', '2', '3', '4', 'EOF']; ! foreach ($tokens as $token) { if (!isset($rules[$state][$token])) { throw new NoTransitionException(); } ! $state = $rules[$state][$token]; } ! $accepted = in_array($state, $accept_states);
  49. Nondeterministic finite automaton

  50. baz

  51. baz

  52. az

  53. z

  54. None
  55. None
  56. None
  57. • Does not add computational power • Can be compiled

    to a DFA • Previous DFA example already showed this • Basic quantum physics
  58. None
  59. Pushdown automaton

  60. • Kellerautomat • Introduces a stack • Can determine balanced

    parens
  61. None
  62. None
  63. e ( ( ( ( ) ) ) ( )

    )
  64. x e ( ( ( ) ) ) ( )

    )
  65. x x e ( ( ) ) ) ( )

    )
  66. x x x e ( ) ) ) ( )

    )
  67. x x x x e ) ) ) ( )

    )
  68. x x x e ) ) ( ) )

  69. x x e ) ( ) )

  70. x e ( ) )

  71. x x e ) )

  72. x e )

  73. e

  74. e

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

    • Rule δ = (qi, a, sj → qi1, sj1) • Can accept context-free languages
  76. • Validation • Parsers • Stack machines

  77. Turing Machine

  78. None
  79. None
  80. None
  81. None
  82. None
  83. 0 0 1 1

  84. 0 0 1 1

  85. 0 0 1 0

  86. 0 0 0 0

  87. 0 1 0 0

  88. 0 1 0 0

  89. 0 1 0 0

  90. 0 1 0 0

  91. 0 1 0 0

  92. 0 1 0 0

  93. 0 1 0 0

  94. 0 1 0 1

  95. 0 1 0 1

  96. 0 1 0 1

  97. 0 1 0 1

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

    • Rule δ = (qi, aj → qi1, aj1, dk) • Can accept recursively enumerable languages • Or loop forever
  99. 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, $new_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, '_'); } } ! $state = $new_state; }
  100. This machine can run any algorithm

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

  102. Universality

  103. add increment one-third

  104. add increment one-third add increment one-third

  105. add increment one-third

  106. add increment one-third U

  107. U M

  108. None
  109. • Stored-program computer (John von Neumann) • Programs as data

    • FPGA • PHPPHP
  110. Turing completeness

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

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

    of Life • Brainfuck • PHP
  113. • 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
  114. • Is our universe really turing complete? • Or are

    the possible paths finite and pre- determined? • Do even stronger forces exist?
  115. Self-reference

  116. <?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;
  117. Recursion

  118. None
  119. call_user_func( function ($x) { return $x($x); }, function ($x) {

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

  121. None
  122. Russell’s paradox

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

    do not contain themselves • Does R contain itself? • If yes, then R’s definition is incorrect • If no, R is not in the set, so it must contain itself
  124. • Liar paradox: “This sentence is false.” • Type theory

    • Hierarchy of types avoids self-reference
  125. Entscheidungsproblem

  126. • 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
  127. Halts?

  128. Halts? Negate

  129. Halts? Negate Copy

  130. Halts? Negate Copy { X

  131. ( ) X X

  132. Halts? Negate Copy X

  133. Halts? Negate Copy X X

  134. Halts? Negate Copy true X X

  135. Halts? Negate Copy X ∞ true X

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

  137. Halts? Negate Copy false X X

  138. Halts? Negate Copy false halting now X X

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

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

    We are screwed
  141. Ways to cope

  142. None
  143. None
  144. Use finite state machines in parts of your programs

  145. Build restricted subsets of computing such as type systems that

    cannot loop forever
  146. Conclusion

  147. Programming is hard

  148. None
  149. Questions? • github.com/igorw • conway-php • turing-php • lambda-php •

    @igorwhiletrue