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

Abstract Machines (dpc14)

Sponsored · Your Podcast. Everywhere. Effortlessly. Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.

Abstract Machines (dpc14)

Avatar for Igor Wiedler

Igor Wiedler

June 27, 2014
Tweet

More Decks by Igor Wiedler

Other Decks in Programming

Transcript

  1. • 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?
  2. • Abstract machine is a model of computation • Or

    a really simple interpreter • Cellular automata are abstract machines
  3. • if alive • 2 or 3 neighbours to survive

    • if dead • exactly 3 neighbours to spawn • else • cell is dead
  4. 1 1 2 1 3 5 2 2 1 2

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

    2 2 2 3 2 1
  6. • Other cellular automata • Codd’s automaton (8 states) •

    Langton’s loops (8 states) • Wireworld (4 states)
  7. • M = (Q, Σ, δ, q0, F) • Rule

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

    Game states • Business rules • Workflows
  9. $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);
  10. sexpr = atom | (sexpr*) atom = letter alphanum* |

    number+ alphanum = letter | number letter = a | b | ... | z number = 1 | 2 | ... | 9
  11. • M = (Q, Σ, Γ, δ, q0, Zo, F)

    • Rule δ = (qi, a, sj → qi1, sj1) • O(n) space, O(n) time • Can accept context-free languages
  12. $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);
  13. $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);
  14. • M = (Q, Σ, Γ, δ, q0, b, F)

    • Rule δ = (qi, aj → qi1, aj1, dk) • Can accept recursively enumerable languages • Can loop forever
  15. 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, '_'); } } }
  16. 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!
  17. $rules = [ 1 => ['0' => ['1', 'r', 2],

    '_' => ['1', 'r', 2], '1' => ['0', 'l', 1]], 2 => ['0' => ['0', 'r', 2], '1' => ['1', 'r', 2], '_' => ['_', 'l', 3]], ];
  18. • For every problem there is a special purpose brain

    that solves it as fast as possible.
 
 — Konrad Zuse, 1937
  19. U M

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

    storage • Conditional branching • Recursion
  21. • 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
  22. <?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;
  23. • Let R be the set of all sets that

    do not contain themselves • Does R contain itself?
  24. • 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?
  25. • 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
  26. • 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
  27. • Proof by contradiction • Decision machine cannot exist •

    Rice’s theorem generalises this • We are screwed
  28. 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])); }