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

Abstract Machines (laracon)

Abstract Machines (laracon)

From cellular automata to turing machines.

Igor Wiedler

May 16, 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. 234

  8. 34

  9. 4

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

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

    Game states • Business rules • Workflows
  12. $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);
  13. baz

  14. baz

  15. az

  16. z

  17. • Does not add computational power • Can be compiled

    to a DFA • Previous DFA example already showed this • Parallel timelines on fixed time scale
  18. • Kellerautomat • Introduces a stack • Can accept nested

    structures • e.g. balanced parens, JSON, PHP
  19. e

  20. e

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

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

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

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

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

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

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

    do not contain themselves • Does R contain itself?
  33. • 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?
  34. • 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
  35. • 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
  36. • Proof by contradiction • Decision machine cannot exist •

    Rice’s theorem generalises this • We are screwed
  37. Grammar Language Automaton Type-0 Recursively enumerable Turing machine Type-1 Context-sensitive

    Linear-bounded non- deterministic Turing machine Type-2 Context-free Non-deterministic pushdown automaton Type-3 Regular Finite state automaton Power
  38. Accidentally Turing Complete • Magic: The Gathering • Minecraft •

    Border Gateway Protocol (BGP) • Microsoft Excel • Electronic circuits
  39. Hypercomputation • Turing’s Oracle Machine • Zeno Machine • Turing

    Machine orbiting around a black hole • Quantum Computer • … and beyond
  40. • Is our universe really turing complete? • Are the

    possible paths finite and pre- determined? • Or do even stronger forces exist? ! • Example: Truly random numbers.
  41. 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])); }