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

Abstract Machines (nomadphp)

Sponsored · Ship Features Fearlessly Turn features on and off without deploys. Used by thousands of Ruby developers.

Abstract Machines (nomadphp)

Avatar for Igor Wiedler

Igor Wiedler

April 17, 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) • Can accept regular languages
  11. $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);
  12. baz

  13. baz

  14. az

  15. z

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

    to a DFA • Previous DFA example already showed this • Parallel timelines on fixed time scale
  17. e

  18. e

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

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

    • Rule δ = (qi, aj → qi1, aj1, dk) • Can accept recursively enumerable languages • Or loop forever
  22. 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; }
  23. • For every problem there is a special purpose brain

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

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

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

    do not contain themselves • Does R contain itself?
  29. • 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?
  30. • 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
  31. • 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
  32. • Proof by contradiction • Decision machine cannot exist •

    Rice’s theorem generalises this • We are screwed
  33. 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
  34. Accidentally Turing Complete • Magic: The Gathering • Minecraft •

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

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

    possible paths finite and pre- determined? • Or do even stronger forces exist? ! • Example: Truly random numbers.
  37. Booleans λx.λy.x λx.λy.y function ($x) { return function ($y) use

    ($x) { return $x; } } function ($x) { return function ($y) use ($x) { return $y; } }
  38. Church numerals λf.λx.x λf.λx.f x λf.λx.f (f x) function ($f)

    { return function ($x) use ($f) { return $x; } } function ($f) { return function ($x) use ($f) { return $f($x); } } function ($f) { return function ($x) use ($f) { return $f($f($x)); } }
  39. • increment • λn.λf.λx.f (n f x) • addition •

    λm.λn.m SUCC n • if • λx.λy.x • λx.λy.y • bool already acts as an if statement
  40. Y combinator λf.(λx.f (λv.((x x) v))) (λx.f (λv.((x x) v)))

    function ($f) { return call_user_func( function ($x) use ($f) { return $f(function ($v) use ($x) { return $x($x)($v) }); }, function ($x) use ($f) { return $f(function ($v) use ($x) { return $x($x)($v) }); } ); }
  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])); }