Abstrakte Maschinen
@igorwhiletrue
Programming is hard
Why?
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?
Abstract machine is a model of computation Cellular automata are abstract machines
Conway s Game of Life
if alive 2 or 3 neighbours to survive if dead exactly 3 neighbours to spawn else cell is dead
1 1 2 1 3 5 2 2 1 2 2 2 2 3 2 1
1 1 2 1 3 5 2 2 1 2 2 2 2 3 2 1
1 2 1 3 5 2 2 2 2 2 2 3 2 1
1 2 1 3 5 2 2 2 2 2 2 3 2 1
Still lifes Oscillators Spaceships Guns, puffers, breeders
Cellular automaton Metaphor for life Complexity, emergence & stuff
Other cellular automata Codd s automaton (8 states) Langton s loops (8 states) Wireworld (4 states)
Deterministic finite automaton
Endlicher automat Regular expressions Directed state transition graph
(refs fixes closes) #\d*
(refs fixes closes) #\d*
(refs fixes closes) #\d*
fixes #1234
ixes #1234
xes #1234
es #1234
s #1234
#1234
#1234
1234
234
34
4
M = (Q, Σ, δ, q0, F) Rule δ = (q i, a qi1) Can accept regular languages
Regular expressions Network protocols Game states Business rules Workflows Queues
$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);
Nondeterministic finite automaton
baz
baz
az
z
Does not add computational power Can be compiled to a DFA Previous DFA example already showed this Basic quantum physics
Pushdown automaton
Kellerautomat Introduces a stack Can determine balanced parens
e ( ( ( ( ) ) ) ( ) )
( ( ( ) ) ) ( ) ) x e
( ( ) ) ) ( ) ) x x e
( ) ) ) ( ) ) x x x e
x ) ) ) ( ) ) x x x e
) ) ( ) ) x x x e
) ( ) ) x x e
( ) ) x e
) ) x x e
) x e
e
e
M = (Q, Σ, Γ, δ, q0, Zo, F) Rule δ = (q i, a, sj qi1, sj1) Can accept context-free languages
Validation Parsers Stack machines
Turing Machine
0 0 1 1
0 0 1 1
0 0 1 0
0 0 0 0
0 1 0 0
0 1 0 0
0 1 0 0
0 1 0 0
0 1 0 0
0 1 0 0
0 1 0 0
0 1 0 1
0 1 0 1
0 1 0 1
0 1 0 1
M = (Q, Σ, Γ, δ, q0, b, F) Rule δ = (q i, aj qi1, aj1, dk) Can accept recursively enumerable languages Or loop forever
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; }
This machine can run any algorithm
This machine can run any algorithm etsy.com/shop/sharpwriter
Universality
increment add one-third
increment add one-third increment add one-third
increment add one-third
U increment add one-third
U M
Stored-program computer (John von Neumann) Programs as data FPGA PHPPHP
Turing completeness
System capable of emulating a turing machine Unbounded storage Conditional branching Recursion
Universal Turing Machine λ-calculus (Alonzo Church) Game of Life Brainfuck PHP
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
Is our universe really turing complete? Or are the possible paths finite and predetermined? Do even stronger forces exist?
Self-reference
<?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;
Recursion
call_user_func( function ($x) { return $x($x); }, function ($x) { return $x($x); } );
while (true);
Russell s paradox
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
Liar paradox: This sentence is false. Type theory Hierarchy of types avoids self-reference
Entscheidungsproblem
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
Halts?
Halts? Negate
Copy Halts? Negate
X { Copy Halts? Negate
X ( ) X
X Copy Halts? Negate
Copy X X Halts? Negate
Copy X X Halts? true Negate
Copy X X Halts? true Negate
Copy X X Halts? }X true X Negate
Copy X X Halts? false Negate
Copy X X Halts? false Negate halting now
Copy X X Halts? }X false X Negate halting now
Proof by contradiction Decision machine cannot exist We are screwed
Ways to cope
Use finite state machines in parts of your programs
Build restricted subsets of computing such as type systems that cannot loop forever
Conclusion
Programming is hard
Questions? github.com/igorw conway-php turing-php lambda-php @igorwhiletrue