Slide 1

Slide 1 text

Abstrakte Maschinen

Slide 2

Slide 2 text

https://twitter.com/old_sound/status/432365241352331264

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

#nomadphp irc.freenode.net

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

@igorwhiletrue

Slide 7

Slide 7 text

“PHP things”

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

Computers are grown-up tamagotchis

Slide 10

Slide 10 text

Programming is hard

Slide 11

Slide 11 text

Why?

Slide 12

Slide 12 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 13

Slide 13 text

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

Slide 14

Slide 14 text

Conway’s Game of Life

Slide 15

Slide 15 text

No content

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

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

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

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

Slide 31

Slide 31 text

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

Slide 32

Slide 32 text

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

Slide 33

Slide 33 text

Deterministic finite automaton

Slide 34

Slide 34 text

• Endlicher automat • Regular expressions • Directed state transition graph

Slide 35

Slide 35 text

No content

Slide 36

Slide 36 text

(refs|fixes|closes) #\d*

Slide 37

Slide 37 text

(refs|fixes|closes) #\d*

Slide 38

Slide 38 text

(refs|fixes|closes) #\d*

Slide 39

Slide 39 text

fixes #1234

Slide 40

Slide 40 text

ixes #1234

Slide 41

Slide 41 text

xes #1234

Slide 42

Slide 42 text

es #1234

Slide 43

Slide 43 text

s #1234

Slide 44

Slide 44 text

#1234

Slide 45

Slide 45 text

#1234

Slide 46

Slide 46 text

1234

Slide 47

Slide 47 text

234

Slide 48

Slide 48 text

34

Slide 49

Slide 49 text

4

Slide 50

Slide 50 text

No content

Slide 51

Slide 51 text

No content

Slide 52

Slide 52 text

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

Slide 53

Slide 53 text

• Regular expressions • Network protocols • Game states • Business rules • Workflows • Queues

Slide 54

Slide 54 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 55

Slide 55 text

Nondeterministic finite automaton

Slide 56

Slide 56 text

baz

Slide 57

Slide 57 text

baz

Slide 58

Slide 58 text

az

Slide 59

Slide 59 text

z

Slide 60

Slide 60 text

No content

Slide 61

Slide 61 text

No content

Slide 62

Slide 62 text

No content

Slide 63

Slide 63 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 64

Slide 64 text

No content

Slide 65

Slide 65 text

Pushdown automaton

Slide 66

Slide 66 text

• Kellerautomat • Introduces a stack • Can determine balanced parens

Slide 67

Slide 67 text

No content

Slide 68

Slide 68 text

No content

Slide 69

Slide 69 text

e ( ( ( ( ) ) ) ( ) )

Slide 70

Slide 70 text

x e ( ( ( ) ) ) ( ) )

Slide 71

Slide 71 text

x x e ( ( ) ) ) ( ) )

Slide 72

Slide 72 text

x x x e ( ) ) ) ( ) )

Slide 73

Slide 73 text

x x x x e ) ) ) ( ) )

Slide 74

Slide 74 text

x x x e ) ) ( ) )

Slide 75

Slide 75 text

x x e ) ( ) )

Slide 76

Slide 76 text

x e ( ) )

Slide 77

Slide 77 text

x x e ) )

Slide 78

Slide 78 text

x e )

Slide 79

Slide 79 text

e

Slide 80

Slide 80 text

e

Slide 81

Slide 81 text

• M = (Q, Σ, Γ, δ, q0, Zo, F) • Rule δ = (qi, a, sj → qi1, sj1) • Can accept context-free languages

Slide 82

Slide 82 text

• Validation • Parsers • Stack machines

Slide 83

Slide 83 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 84

Slide 84 text

Turing Machine

Slide 85

Slide 85 text

No content

Slide 86

Slide 86 text

No content

Slide 87

Slide 87 text

No content

Slide 88

Slide 88 text

No content

Slide 89

Slide 89 text

No content

Slide 90

Slide 90 text

0 0 1 1

Slide 91

Slide 91 text

0 0 1 1

Slide 92

Slide 92 text

0 0 1 0

Slide 93

Slide 93 text

0 0 0 0

Slide 94

Slide 94 text

0 1 0 0

Slide 95

Slide 95 text

0 1 0 0

Slide 96

Slide 96 text

0 1 0 0

Slide 97

Slide 97 text

0 1 0 0

Slide 98

Slide 98 text

0 1 0 0

Slide 99

Slide 99 text

0 1 0 0

Slide 100

Slide 100 text

0 1 0 0

Slide 101

Slide 101 text

0 1 0 1

Slide 102

Slide 102 text

0 1 0 1

Slide 103

Slide 103 text

0 1 0 1

Slide 104

Slide 104 text

0 1 0 1

Slide 105

Slide 105 text

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

Slide 106

Slide 106 text

This machine can run any algorithm

Slide 107

Slide 107 text

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

Slide 108

Slide 108 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, $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; }

Slide 109

Slide 109 text

No content

Slide 110

Slide 110 text

No content

Slide 111

Slide 111 text

No content

Slide 112

Slide 112 text

Universality

Slide 113

Slide 113 text

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

Slide 114

Slide 114 text

add increment one-third

Slide 115

Slide 115 text

add increment one-third add increment one-third

Slide 116

Slide 116 text

add increment one-third

Slide 117

Slide 117 text

add increment one-third U

Slide 118

Slide 118 text

U M

Slide 119

Slide 119 text

No content

Slide 120

Slide 120 text

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

Slide 121

Slide 121 text

Turing completeness

Slide 122

Slide 122 text

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

Slide 123

Slide 123 text

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

Slide 124

Slide 124 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 125

Slide 125 text

Self-reference

Slide 126

Slide 126 text

Slide 127

Slide 127 text

Recursion

Slide 128

Slide 128 text

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

Slide 129

Slide 129 text

while (true);

Slide 130

Slide 130 text

No content

Slide 131

Slide 131 text

Russell’s paradox

Slide 132

Slide 132 text

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

Slide 133

Slide 133 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 134

Slide 134 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 135

Slide 135 text

Entscheidungsproblem

Slide 136

Slide 136 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 137

Slide 137 text

Halts?

Slide 138

Slide 138 text

Halts? Negate

Slide 139

Slide 139 text

Halts? Negate Copy

Slide 140

Slide 140 text

Halts? Negate Copy { X

Slide 141

Slide 141 text

( ) X X

Slide 142

Slide 142 text

Halts? Negate Copy X

Slide 143

Slide 143 text

Halts? Negate Copy X X

Slide 144

Slide 144 text

Halts? Negate Copy true X X

Slide 145

Slide 145 text

Halts? Negate Copy X ∞ true X

Slide 146

Slide 146 text

Halts? Negate Copy X ∞ true X X X }

Slide 147

Slide 147 text

Halts? Negate Copy false X X

Slide 148

Slide 148 text

Halts? Negate Copy false halting now X X

Slide 149

Slide 149 text

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

Slide 150

Slide 150 text

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

Slide 151

Slide 151 text

Ways to cope

Slide 152

Slide 152 text

No content

Slide 153

Slide 153 text

No content

Slide 154

Slide 154 text

Use finite state machines in parts of your programs

Slide 155

Slide 155 text

Introduce high-level concepts such as bounded loops

Slide 156

Slide 156 text

Build restricted subsets of computing such as type systems

Slide 157

Slide 157 text

Conclusion I

Slide 158

Slide 158 text

Programming is hard

Slide 159

Slide 159 text

Big picture

Slide 160

Slide 160 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 161

Slide 161 text

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

Slide 162

Slide 162 text

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

Slide 163

Slide 163 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 164

Slide 164 text

Conclusion II

Slide 165

Slide 165 text

Machines are human-like

Slide 166

Slide 166 text

Humans are machine-like

Slide 167

Slide 167 text

The universe is a machine

Slide 168

Slide 168 text

No content

Slide 169

Slide 169 text

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

Slide 170

Slide 170 text

λ-calculus

Slide 171

Slide 171 text

The first functional programming language

Slide 172

Slide 172 text

Abstraction λ.x … function ($x) { ... } Application f x $f($x) Variables x return $x;

Slide 173

Slide 173 text

Identity λx.x function ($x) { return $x; }

Slide 174

Slide 174 text

Currying λx.λy.x function ($x) { return function ($y) use ($x) { return $x; } }

Slide 175

Slide 175 text

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

Slide 176

Slide 176 text

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

Slide 177

Slide 177 text

• 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

Slide 178

Slide 178 text

No content

Slide 179

Slide 179 text

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

Slide 180

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

Slide 181

Slide 181 text

That’s it, folks!