Slide 1

Slide 1 text

Abstrakte Maschinen

Slide 2

Slide 2 text

@igorwhiletrue

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

Computers are grown-up tamagotchis

Slide 6

Slide 6 text

Programming is hard

Slide 7

Slide 7 text

Why?

Slide 8

Slide 8 text

• 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?

Slide 9

Slide 9 text

• Abstract machine is a model of computation • Or a really simple interpreter • Cellular automata are abstract machines

Slide 10

Slide 10 text

Conway’s Game of Life

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

• if alive • 2 or 3 neighbours to survive • if dead • exactly 3 neighbours to spawn • else • cell is dead

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

No content

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

No content

Slide 21

Slide 21 text

No content

Slide 22

Slide 22 text

No content

Slide 23

Slide 23 text

No content

Slide 24

Slide 24 text

No content

Slide 25

Slide 25 text

No content

Slide 26

Slide 26 text

No content

Slide 27

Slide 27 text

No content

Slide 28

Slide 28 text

No content

Slide 29

Slide 29 text

No content

Slide 30

Slide 30 text

No content

Slide 31

Slide 31 text

• Still lifes • Oscillators • Spaceships • Guns, puffers, breeders

Slide 32

Slide 32 text

• Cellular automaton • Metaphor for life • Complexity, emergence & stuff

Slide 33

Slide 33 text

• Other cellular automata • Codd’s automaton (8 states) • Langton’s loops (8 states) • Wireworld (4 states)

Slide 34

Slide 34 text

Deterministic finite automaton

Slide 35

Slide 35 text

No content

Slide 36

Slide 36 text

• Endlicher automat • Regular expressions • Directed state transition graph

Slide 37

Slide 37 text

No content

Slide 38

Slide 38 text

(refs|fixes|closes) #\d*

Slide 39

Slide 39 text

(refs|fixes|closes) #\d*

Slide 40

Slide 40 text

(refs|fixes|closes) #\d*

Slide 41

Slide 41 text

fixes #1234

Slide 42

Slide 42 text

ixes #1234

Slide 43

Slide 43 text

xes #1234

Slide 44

Slide 44 text

es #1234

Slide 45

Slide 45 text

s #1234

Slide 46

Slide 46 text

#1234

Slide 47

Slide 47 text

#1234

Slide 48

Slide 48 text

1234

Slide 49

Slide 49 text

234

Slide 50

Slide 50 text

34

Slide 51

Slide 51 text

4

Slide 52

Slide 52 text

No content

Slide 53

Slide 53 text

No content

Slide 54

Slide 54 text

• M = (Q, Σ, δ, q0, F) • Rule δ = (qi, a → qi1) • O(1) space, O(n) time • Can accept regular languages

Slide 55

Slide 55 text

• Regular expressions • Lexical analysis • Network protocols • Game states • Business rules • Workflows

Slide 56

Slide 56 text

$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);

Slide 57

Slide 57 text

Nondeterministic finite automaton

Slide 58

Slide 58 text

baz

Slide 59

Slide 59 text

baz

Slide 60

Slide 60 text

az

Slide 61

Slide 61 text

z

Slide 62

Slide 62 text

No content

Slide 63

Slide 63 text

No content

Slide 64

Slide 64 text

No content

Slide 65

Slide 65 text

• Does not add computational power • Can be compiled to a DFA • Previous DFA example already showed this • Parallel timelines on fixed time scale

Slide 66

Slide 66 text

No content

Slide 67

Slide 67 text

No content

Slide 68

Slide 68 text

Pushdown automaton

Slide 69

Slide 69 text

No content

Slide 70

Slide 70 text

• Kellerautomat • Introduces a stack • Can accept nested structures • e.g. balanced parens, JSON, PHP

Slide 71

Slide 71 text

S ::= ε | (S) | SS

Slide 72

Slide 72 text

No content

Slide 73

Slide 73 text

No content

Slide 74

Slide 74 text

e ( ( ( ( ) ) ) ( ) )

Slide 75

Slide 75 text

x e ( ( ( ) ) ) ( ) )

Slide 76

Slide 76 text

x x e ( ( ) ) ) ( ) )

Slide 77

Slide 77 text

x x x e ( ) ) ) ( ) )

Slide 78

Slide 78 text

x x x x e ) ) ) ( ) )

Slide 79

Slide 79 text

x x x e ) ) ( ) )

Slide 80

Slide 80 text

x x e ) ( ) )

Slide 81

Slide 81 text

x e ( ) )

Slide 82

Slide 82 text

x x e ) )

Slide 83

Slide 83 text

x e )

Slide 84

Slide 84 text

e

Slide 85

Slide 85 text

e

Slide 86

Slide 86 text

• 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

Slide 87

Slide 87 text

• Validation • Parsers • Stack machines

Slide 88

Slide 88 text

$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);

Slide 89

Slide 89 text

Turing Machine

Slide 90

Slide 90 text

No content

Slide 91

Slide 91 text

No content

Slide 92

Slide 92 text

No content

Slide 93

Slide 93 text

No content

Slide 94

Slide 94 text

No content

Slide 95

Slide 95 text

No content

Slide 96

Slide 96 text

0 0 1 1

Slide 97

Slide 97 text

0 0 1 1

Slide 98

Slide 98 text

0 0 1 0

Slide 99

Slide 99 text

0 0 0 0

Slide 100

Slide 100 text

0 1 0 0

Slide 101

Slide 101 text

0 1 0 0

Slide 102

Slide 102 text

0 1 0 0

Slide 103

Slide 103 text

0 1 0 0

Slide 104

Slide 104 text

0 1 0 0

Slide 105

Slide 105 text

0 1 0 0

Slide 106

Slide 106 text

0 1 0 0

Slide 107

Slide 107 text

0 1 0 1

Slide 108

Slide 108 text

0 1 0 1

Slide 109

Slide 109 text

0 1 0 1

Slide 110

Slide 110 text

0 1 0 1

Slide 111

Slide 111 text

• M = (Q, Σ, Γ, δ, q0, b, F) • Rule δ = (qi, aj → qi1, aj1, dk) • Can accept recursively enumerable languages • Can emulate a PDA • Or loop forever

Slide 112

Slide 112 text

This machine can run any algorithm

Slide 113

Slide 113 text

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

Slide 114

Slide 114 text

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, '_'); } } }

Slide 115

Slide 115 text

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!

Slide 116

Slide 116 text

$rules = [ 1 => ['0' => ['1', 'r', 2], '_' => ['1', 'r', 2], '1' => ['0', 'l', 1]], 2 => ['0' => ['0', 'r', 2], '1' => ['1', 'r', 2], '_' => ['_', 'l', 3]], ];

Slide 117

Slide 117 text

No content

Slide 118

Slide 118 text

No content

Slide 119

Slide 119 text

No content

Slide 120

Slide 120 text

Universality

Slide 121

Slide 121 text

• For every problem there is a special purpose brain that solves it as fast as possible.
 
 — Konrad Zuse, 1937

Slide 122

Slide 122 text

add increment one-third

Slide 123

Slide 123 text

add increment one-third add increment one-third

Slide 124

Slide 124 text

add increment one-third

Slide 125

Slide 125 text

add increment one-third U

Slide 126

Slide 126 text

U M

Slide 127

Slide 127 text

No content

Slide 128

Slide 128 text

• Stored-program computer (John von Neumann) • Programs as data • PHPPHP

Slide 129

Slide 129 text

Turing completeness

Slide 130

Slide 130 text

• System capable of emulating a turing machine • Unbounded storage • Conditional branching • Recursion

Slide 131

Slide 131 text

• Universal Turing Machine • λ-calculus (Alonzo Church) • Game of Life • Brainfuck • PHP

Slide 132

Slide 132 text

• 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

Slide 133

Slide 133 text

Self-reference

Slide 134

Slide 134 text

Slide 135

Slide 135 text

Recursion

Slide 136

Slide 136 text

call_user_func( function ($x) { return $x($x); }, function ($x) { return $x($x); } );

Slide 137

Slide 137 text

while (true);

Slide 138

Slide 138 text

No content

Slide 139

Slide 139 text

No content

Slide 140

Slide 140 text

Russell’s paradox

Slide 141

Slide 141 text

• Let R be the set of all sets that do not contain themselves • Does R contain itself?

Slide 142

Slide 142 text

• 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?

Slide 143

Slide 143 text

• 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

Slide 144

Slide 144 text

Entscheidungsproblem

Slide 145

Slide 145 text

• 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

Slide 146

Slide 146 text

Halts?

Slide 147

Slide 147 text

Halts? Negate

Slide 148

Slide 148 text

Halts? Negate Copy

Slide 149

Slide 149 text

Halts? Negate Copy { X

Slide 150

Slide 150 text

( ) X X

Slide 151

Slide 151 text

Halts? Negate Copy X

Slide 152

Slide 152 text

Halts? Negate Copy X X

Slide 153

Slide 153 text

Halts? Negate Copy true X X

Slide 154

Slide 154 text

Halts? Negate Copy X ∞ true X

Slide 155

Slide 155 text

Halts? Negate Copy X ∞ true X X X }

Slide 156

Slide 156 text

Halts? Negate Copy false X X

Slide 157

Slide 157 text

Halts? Negate Copy false halting now X X

Slide 158

Slide 158 text

Halts? Negate Copy false halting now X X X X }

Slide 159

Slide 159 text

• Proof by contradiction • Decision machine cannot exist • Rice’s theorem generalises this • We are screwed

Slide 160

Slide 160 text

Ways to cope

Slide 161

Slide 161 text

No content

Slide 162

Slide 162 text

No content

Slide 163

Slide 163 text

Use finite state machines in parts of your programs

Slide 164

Slide 164 text

Introduce high-level concepts such as bounded loops

Slide 165

Slide 165 text

Build restricted subsets of computing such as type systems

Slide 166

Slide 166 text

Conclusion

Slide 167

Slide 167 text

Programming is hard

Slide 168

Slide 168 text

Big picture

Slide 169

Slide 169 text

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

Slide 170

Slide 170 text

Accidentally Turing Complete • Magic: The Gathering • Minecraft • Border Gateway Protocol (BGP) • Microsoft Excel • Electronic circuits

Slide 171

Slide 171 text

Church-Turing Thesis

Slide 172

Slide 172 text

The brain is a machine

Slide 173

Slide 173 text

Beyond Turing Machines

Slide 174

Slide 174 text

Hypercomputation • Turing’s Oracle Machine • Zeno Machine • Turing Machine orbiting around a black hole • Quantum Computer • … and beyond

Slide 175

Slide 175 text

• Is our universe really turing complete? • Are the possible paths finite and pre- determined? • Or do even stronger forces exist? ! • Example: Truly random numbers.

Slide 176

Slide 176 text

The universe is a machine

Slide 177

Slide 177 text

No content

Slide 178

Slide 178 text

Questions? • joind.in/11341 • github.com/igorw • conway-php • turing-php • lambda-php • @igorwhiletrue

Slide 179

Slide 179 text

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])); }