18
Comp 120, Spring 2005 page 1 2/3 Lecture Marc Pollefeys 2/3/2005 00:43 L07 – State Machines 1 Comp 120 – Spring 2005 2/3/05 State Machines 1) State Machine Design 2) How can we improve on FSMs? 3) Turing Machines 4) Computability Oh genie, will you now tell me what it means to compute? Doctor, I think you’ve built a monster! L07 – State Machines 2 Comp 120 – Spring 2005 2/3/05 Example FSM: Roboant SENSORS: antennae L and R, each 1 if in contact with something. ACTUATORS: Forward Step F, ten-degree turns TL and TR (left, right). 8 legs? Oh, he’s a binary bug GOAL: Make our ant smart enough to get out of a maze like: STRATEGY: "Right antenna to the wall"

State Machines - University of North Carolina at Chapel · PDF fileComp 120 – Spring 2005 2/3/05 L07 – State Machines 1 State Machines 1) State Machine Design 2) How can we improve

Embed Size (px)

Citation preview

Page 1: State Machines - University of North Carolina at Chapel · PDF fileComp 120 – Spring 2005 2/3/05 L07 – State Machines 1 State Machines 1) State Machine Design 2) How can we improve

Comp 120, Spring 2005 page 12/3 Lecture

Marc Pollefeys 2/3/2005 00:43

L07 – State Machines 1Comp 120 – Spring 2005 2/3/05

State Machines

1) State Machine Design2) How can we improve on FSMs?3) Turing Machines4) Computability

Oh genie, will you now tell me what it means

to compute?

Doctor, I think you’ve built a

monster!

L07 – State Machines 2Comp 120 – Spring 2005 2/3/05

Example FSM: Roboant

SENSORS: antennae L and R, each 1 if in contact with something.

ACTUATORS: Forward Step F, ten-degree turns TL and TR (left, right).

8 legs?Oh, he’s a binary bug

GOAL: Make our ant smart enough to get out of a maze like:

STRATEGY: "Right antenna to the wall"

Page 2: State Machines - University of North Carolina at Chapel · PDF fileComp 120 – Spring 2005 2/3/05 L07 – State Machines 1 State Machines 1) State Machine Design 2) How can we improve

Comp 120, Spring 2005 page 22/3 Lecture

Marc Pollefeys 2/3/2005 00:43

L07 – State Machines 3Comp 120 – Spring 2005 2/3/05

Lost in Space

?

LOSTF

L+R

_ _L R

“lost” is theinitial state

Action: Go forward until we hit something.

L07 – State Machines 4Comp 120 – Spring 2005 2/3/05

Bonk!

LOSTF

L+R

_ _L R

RCCW

L+R

TL

_ _L R

Action: Turn left (CCW) until we don’t touch anymore

Page 3: State Machines - University of North Carolina at Chapel · PDF fileComp 120 – Spring 2005 2/3/05 L07 – State Machines 1 State Machines 1) State Machine Design 2) How can we improve

Comp 120, Spring 2005 page 32/3 Lecture

Marc Pollefeys 2/3/2005 00:43

L07 – State Machines 5Comp 120 – Spring 2005 2/3/05

A Little to the Right…

LOSTF

RCCWL+R

L+R

Wall1TR,F

R

_R

TL

_ _L R

_ _L R

Action: Step and turn right a little, look for wall

L07 – State Machines 6Comp 120 – Spring 2005 2/3/05

Then a Little to the Left

LOSTF

Wall1TR,F

RCCW

R

L+R

L+R

_R

TL

_ _L R

_ _L R _ _

L R

Wall2 TL,FL

_ L R

Action: Step and turn left a little, till not touching (again)

Page 4: State Machines - University of North Carolina at Chapel · PDF fileComp 120 – Spring 2005 2/3/05 L07 – State Machines 1 State Machines 1) State Machine Design 2) How can we improve

Comp 120, Spring 2005 page 42/3 Lecture

Marc Pollefeys 2/3/2005 00:43

L07 – State Machines 7Comp 120 – Spring 2005 2/3/05

Dealing with Corners

LOSTF

Wall2

TL,F

Wall1TR,F

RCCW

R

L+R

L+R

_R

LTL

_ _L R

_ _L R _ _

L R

_ L R

TR,F

Corner

R _R

Action: Step and turn right until we hit perpendicular wall

L07 – State Machines 8Comp 120 – Spring 2005 2/3/05

Equivalent State Reduction

Observation: Si ≡ Sj if1. States have identical outputs; AND2. Every input →equivalent states.

Reduction Strategy:Find pairs of equivalent states, MERGE them.

LOSTF

Wall2

TL,F

Wall1TR,F

RCCW

R

L+R

L+R

_R

LTL

_ _L R

_ _L R _ _

L R

_ L R

TR,F

Corner

R _R

Page 5: State Machines - University of North Carolina at Chapel · PDF fileComp 120 – Spring 2005 2/3/05 L07 – State Machines 1 State Machines 1) State Machine Design 2) How can we improve

Comp 120, Spring 2005 page 52/3 Lecture

Marc Pollefeys 2/3/2005 00:43

L07 – State Machines 9Comp 120 – Spring 2005 2/3/05

An Evolutionary Step

Behaves exactly as previous (5-state) FSM, but requires half the ROM in its implementation!

Merge equivalent states Wall1 and Corner into a single new, combined state.

LOSTF

Wall2

TL,F

Wall1TR,F

RCCW

R

L+R

L+R

_R

LTL

_ _L R

_ _L R _ _

L R

_ L R

L07 – State Machines 10Comp 120 – Spring 2005 2/3/05

Building the Transition Table

LOSTF

_ _L R

S L R | S’ TR TL F-------+-----------00 0 0 | 00 0 0 1

| | | | || | |||

L+R 00 1 - 01 0 0 100 - 1 01 0 0 1

RCCW

L+R

01 1 - | 01 0 1 001 0 1 | 01 0 1 0

TL

Recall that a state Transition diagram specifies the function of a state machine, not its implementation. Next, we have to convert our specification to gates and registers. To do so, we rewrite our state transition diagram as a truth table. Each arc of the state transition diagram contributes one or more rows to our truth table.

Page 6: State Machines - University of North Carolina at Chapel · PDF fileComp 120 – Spring 2005 2/3/05 L07 – State Machines 1 State Machines 1) State Machine Design 2) How can we improve

Comp 120, Spring 2005 page 62/3 Lecture

Marc Pollefeys 2/3/2005 00:43

L07 – State Machines 11Comp 120 – Spring 2005 2/3/05

Implementation Details

S L R | S’ TR TL F-------+-----------00 0 0 | 00 0 0 100 1 - | 01 0 0 100 0 1 | 01 0 0 101 1 - | 01 0 1 001 0 1 | 01 0 1 001 0 0 | 10 0 1 010 – 0 | 10 1 0 110 – 1 | 11 1 0 111 1 - | 01 0 1 111 0 0 | 10 0 1 111 0 1 | 11 0 1 1

LOST

RCCW

WALL1

WALL2

S1’ S1S000 01 11 10

00 0 1 1 1LR 01 0 0 1 1

11 0 0 0 110 0 0 0 1

S0’ S1S000 01 11 10

00 0 0 0 0LR 01 1 1 1 1

11 1 1 1 110 1 1 1 0

01011 SRLSLSSS ++=′

Complete Transition table 010 LSSLRS ++=′

Eventually, we’ll complete the entire truth table. At this point we can directly implement the state machine using a ROM and STATE REGISTERS. Alternately, we can build a minimal SOP realization using Karnaugh maps.

L07 – State Machines 12Comp 120 – Spring 2005 2/3/05

Ant SchematicAre you telling me that essence amounts to no more than 15 gates!

Page 7: State Machines - University of North Carolina at Chapel · PDF fileComp 120 – Spring 2005 2/3/05 L07 – State Machines 1 State Machines 1) State Machine Design 2) How can we improve

Comp 120, Spring 2005 page 72/3 Lecture

Marc Pollefeys 2/3/2005 00:43

L07 – State Machines 13Comp 120 – Spring 2005 2/3/05

Super Roboant

Maze selectionFSM state

table

Status display

Plan viewof maze

Simulation controls

Featuring the new Mark-II ant: It can add crumbs (M), erase crumbs (E), and sense (S) crumbs along its path.

L07 – State Machines 14Comp 120 – Spring 2005 2/3/05

Finite State Machines:An Important Abstraction

• FORMAL MODEL:Substantial literature, theoretical results.

• PRACTICAL IMPACT: Major engineering tool…

• METAPHYSICAL IMPACT:Are we all just complicated FSMs? Perhaps with imperfect state registers and “fuzzy” logic…

... You'll see FSMs for the rest of your life!

Page 8: State Machines - University of North Carolina at Chapel · PDF fileComp 120 – Spring 2005 2/3/05 L07 – State Machines 1 State Machines 1) State Machine Design 2) How can we improve

Comp 120, Spring 2005 page 82/3 Lecture

Marc Pollefeys 2/3/2005 00:43

L07 – State Machines 15Comp 120 – Spring 2005 2/3/05

2-Flavors of Processing Elements

Combinational Logic:Table look-up, ROM

Finite State Machines:ROM with Feedback

Thus far, we know of nothing more powerful than an FSM

Addr Datai o

Addr Data

i o

s

Fundamentally,everythingthat we’ve

learned so farcan be donewith a ROMand registers

L07 – State Machines 16Comp 120 – Spring 2005 2/3/05

FSMs as Programmable Machines

ROM-based FSM sketch:

Given i, s, and o,we need a ROM organized as:

2i+s words x (o+s) bits

So how many possiblei-input,o-output,FSMs withs-state bitsexist?

i

s

0...010...00 0...00 10110 011

o

2i+s

sN+1 osNiinputs outputs

2 (o+s)2i+s

(some may beequivalent)

An FSM’s behavior is completely determined by its ROM contents.

Page 9: State Machines - University of North Carolina at Chapel · PDF fileComp 120 – Spring 2005 2/3/05 L07 – State Machines 1 State Machines 1) State Machine Design 2) How can we improve

Comp 120, Spring 2005 page 92/3 Lecture

Marc Pollefeys 2/3/2005 00:43

L07 – State Machines 17Comp 120 – Spring 2005 2/3/05

FSM EnumerationGOAL: List all possible FSMs in some canonical order.

• INFINITE list, but• Every FSM has an entry in

and an associated index.

0...01

0...00 0...00 10110 011

sN+1osNiinputs outputs

i s o FSM# Truth Table 1 1 1 1 00000000 1 1 1 2 00000001 … … 1 1 1 256 11111111 2 2 2 257 000000…0000002 2 2 258 000000…000001 … … 3 3 3 000000…000000 … 4 4 4 000000…000000

28

FSMs

264

Every possible FSM can be associated with a unique number. This requires a few wasteful simplifications. First, given an i-input, s-state-bit, and o-output FSM, we’ll replace it with its equivalent n-input, n-state-bit and n-output FSM, where n is the greatest of i, s, and o. We can always ignore the extra input-bits, and set the extra output bits to 0. This allows us to discuss the ith FSM

These FSMs have 2 inputs and 2 outputs, or 8-bits in their ROM.

L07 – State Machines 18Comp 120 – Spring 2005 2/3/05

Some Perennial Favorites...FSM837 modulo 3 counterFSM1077 4-bit counterFSM89143 Cheap digital watchFSM22698469884 Intel Pentium CPU – rev 1FSM784362783 Intel Pentium CPU – rev 2FSM784363783 Intel Pentium II CPU

Page 10: State Machines - University of North Carolina at Chapel · PDF fileComp 120 – Spring 2005 2/3/05 L07 – State Machines 1 State Machines 1) State Machine Design 2) How can we improve

Comp 120, Spring 2005 page 102/3 Lecture

Marc Pollefeys 2/3/2005 00:43

L07 – State Machines 19Comp 120 – Spring 2005 2/3/05

Are FSMs the UltimateComputation Device?

Nope!There exist many simple problems that cannot be computed by FSMs. For instance:

Checking for balanced parenthesis(()(()())) - Okay (()())) - No good!

PROBLEM: Requires ARBITRARILY many states, depending on input. Must "COUNT" unmatched LEFT parens. An FSM can only keep track of a finite number of objects.

Do we know of a machine that can solve this problem?

L07 – State Machines 20Comp 120 – Spring 2005 2/3/05

Yes, Super Roboant can!

State Input Crumb? Crumb MoveNextState Comment

S1 ( - Y R S1 Mark open parenS1 ) - Y L S2 Mark close parenS1 sp - N L S4 Reached endS2 - N N L S2 Scan back to last openS2 - Y N R S3 Eat crumbS3 - N N R S3 Goto close parenS3 - Y N R S1 Eat crumbS4 ( or ) N N L S4 Move LeftS4 ( or ) Y N L S5 Unmatched/EatS4 sp - Y L Halt MatchedS5 ( or ) - N L S5 Unmatched/EatS5 sp - N L Halt Unmatched

( ( ( ( ) ( ) ) ) )

What is it that makes Roboant so powerful? RoboAnt is very FSM-like. Is there some extension to an FSM that allows it to “compute” more?

Page 11: State Machines - University of North Carolina at Chapel · PDF fileComp 120 – Spring 2005 2/3/05 L07 – State Machines 1 State Machines 1) State Machine Design 2) How can we improve

Comp 120, Spring 2005 page 112/3 Lecture

Marc Pollefeys 2/3/2005 00:43

L07 – State Machines 21Comp 120 – Spring 2005 2/3/05

Yes, Roboant can!

State Input Crumb? Crumb MoveNextState Comment

S1 ( - Y R S1 Mark open parenS1 ) - Y L S2 Mark close parenS1 sp - N L S4 Reached endS2 - N N L S2 Scan back to last openS2 - Y N R S3 Eat crumbS3 - N N R S3 Goto close parenS3 - Y N R S1 Eat crumbS4 ( or ) N N L S4 Move LeftS4 ( or ) Y N L S5 Unmatched/EatS4 sp - Y L Halt MatchedS5 ( or ) - N L S5 Unmatched/EatS5 sp - N L Halt Unmatched

( ( ( ( ) ( ) ) ) )

What is it that makes Roboant so powerful? RoboAnt is very FSM-like. Is there exist some extension to an FSM that allows it to “compute” more?

L07 – State Machines 22Comp 120 – Spring 2005 2/3/05

Yes, Roboant can!

State Input Crumb? Crumb MoveNextState Comment

S1 ( - Y R S1 Mark open parenS1 ) - Y L S2 Mark close parenS1 sp - N L S4 Reached endS2 - N N L S2 Scan back to last openS2 - Y N R S3 Eat crumbS3 - N N R S3 Goto close parenS3 - Y N R S1 Eat crumbS4 ( or ) N N L S4 Move LeftS4 ( or ) Y N L S5 Unmatched/EatS4 sp - Y L Halt MatchedS5 ( or ) - N L S5 Unmatched/EatS5 sp - N L Halt Unmatched

( ( ( ( ) ( ) ) ) )

What is it that makes Roboant so powerful? RoboAnt is very FSM-like. Is there exist some extension to an FSM that allows it to “compute” more?

Page 12: State Machines - University of North Carolina at Chapel · PDF fileComp 120 – Spring 2005 2/3/05 L07 – State Machines 1 State Machines 1) State Machine Design 2) How can we improve

Comp 120, Spring 2005 page 122/3 Lecture

Marc Pollefeys 2/3/2005 00:43

L07 – State Machines 23Comp 120 – Spring 2005 2/3/05

Yes, Roboant can!

State Input Crumb? Crumb MoveNextState Comment

S1 ( - Y R S1 Mark open parenS1 ) - Y L S2 Mark close parenS1 sp - N L S4 Reached endS2 - N N L S2 Scan back to last openS2 - Y N R S3 Eat crumbS3 - N N R S3 Goto close parenS3 - Y N R S1 Eat crumbS4 ( or ) N N L S4 Move LeftS4 ( or ) Y N L S5 Unmatched/EatS4 sp - Y L Halt MatchedS5 ( or ) - N L S5 Unmatched/EatS5 sp - N L Halt Unmatched

( ( ( ( ) ( ) ) ) )

What is it that makes Roboant so powerful? RoboAnt is very FSM-like. Is there exist some extension to an FSM that allows it to “compute” more?

L07 – State Machines 24Comp 120 – Spring 2005 2/3/05

Unbounded-Space ComputationDURING 1920s & 1930s, much of the

“science” part of computer science was being developed (long before actual electronic computers existed). Many different

“Models of Computation”were proposed, and the classes of “functions” which could be computed by each were analyzed.

One of these models was the TURING MACHINE named after Alan Turing.

A Turing Machine is just an FSM which receives its inputs and writes outputs onto an infinite tape...

This simple addition solves "FINITE" problem of FSMs. (Like crumbs)Alan Turing

S1

1 1 1 00 0 0 1 0 0 0 00 0 0 0

S2

0,(1,R)

0,(1,L)

1,Halt

1,(1,L)

Page 13: State Machines - University of North Carolina at Chapel · PDF fileComp 120 – Spring 2005 2/3/05 L07 – State Machines 1 State Machines 1) State Machine Design 2) How can we improve

Comp 120, Spring 2005 page 132/3 Lecture

Marc Pollefeys 2/3/2005 00:43

L07 – State Machines 25Comp 120 – Spring 2005 2/3/05

A Turing Machine Example

Turing Machine Specification• Doubly-infinite tape• Discrete symbol positions• Finite alphabet – say {0, 1}• Control FSM

INPUTS:Current symbol

OUTPUTS:write 0/1move Left/Right

• Initial Starting State {S0}• Halt State {Halt}

Current State

Tape Input

Write Tape

Move

Next State

S0 1 1 R S0 S0 0 1 L S1 S1 1 1 L S1 S1 0 0 R Halt

A Turing machine, like an FSM,can be specified with a truth table. The following Turing Machine implements a unary (base 1) incrementer.

0 0 0 0 1 1 1 1 0 01

L07 – State Machines 26Comp 120 – Spring 2005 2/3/05

Turing Machine Tapes as Integers

Canonical names for bounded tape configurations:

FSM i

0 1 1 00 0 1 0 0

Look, it’s just FSM ioperating on tape j

b8 b6 b4 b2 b0 b1 b3 b5 b7

Page 14: State Machines - University of North Carolina at Chapel · PDF fileComp 120 – Spring 2005 2/3/05 L07 – State Machines 1 State Machines 1) State Machine Design 2) How can we improve

Comp 120, Spring 2005 page 142/3 Lecture

Marc Pollefeys 2/3/2005 00:43

L07 – State Machines 27Comp 120 – Spring 2005 2/3/05

TMs as Integer Functions

Turing Machine Ti operating on Tape x,where x = …b8b7b6b5b4b3b2b1b0

I wonder if a TM can computeEVERY integer function...

y = T [x]ix: input tape configuration y: output tape when TM halts

L07 – State Machines 28Comp 120 – Spring 2005 2/3/05

Alternative Models of Computation

Turing Machines [Turing]

FSMi

0 1 1 00 0 1 0 0

Turing

Lambda calculus [Church, Curry, Rosser...]

λx.λy.xxy

(lambda(x)(lambda(y)(x (x y))))

Church

Recursive Functions [Kleene]F(0,x) ≡ x

F(1+y,x) ≡ 1+F(x,y) (define (fact n)

(... (fact (- n 1)) ...)

Kleene

Production Systems [Post, Markov]

α → βIF pulse=0 THEN

patient=dead

Post

Hardwarehead

Mathhead

Theoryhead

Languagehead

Page 15: State Machines - University of North Carolina at Chapel · PDF fileComp 120 – Spring 2005 2/3/05 L07 – State Machines 1 State Machines 1) State Machine Design 2) How can we improve

Comp 120, Spring 2005 page 152/3 Lecture

Marc Pollefeys 2/3/2005 00:43

L07 – State Machines 29Comp 120 – Spring 2005 2/3/05

The 1st Computer Industry Shakeout

Here’s a TM thatcomputes SQUARE ROOT!

FSM

0 1 1 00 0 1 0 0

L07 – State Machines 30Comp 120 – Spring 2005 2/3/05

And the Battles Raged

Here’s a Lambda Expressionthat does the same thing...

... and here’s one that computesthe nth root for ANY n!

(λ(x) .....)

(λ(x n) .....)

Page 16: State Machines - University of North Carolina at Chapel · PDF fileComp 120 – Spring 2005 2/3/05 L07 – State Machines 1 State Machines 1) State Machine Design 2) How can we improve

Comp 120, Spring 2005 page 162/3 Lecture

Marc Pollefeys 2/3/2005 00:43

L07 – State Machines 31Comp 120 – Spring 2005 2/3/05

Fundamental Result:Computable Functions

Each model is capable of computing exactly the same set of integer functions!

Proof Technique: Constructions thattranslate betweenmodels

BIG IDEA: Computability,independent ofcomputation schemechosen

Church's Thesis:

Every discrete function computableby ANY realizable machine is

computable by some Turing machine.

Does,thismean thatwe know ofno computerthat is more“powerful”

than aTuring

machine?

L07 – State Machines 32Comp 120 – Spring 2005 2/3/05

Computable Functions

Representation tricks: to compute fk(x,y)<x,y> ≡ integer whose even bits come from x, and whose odd bits come from y;

whence

f12345(x,y) = x * yf23456(x) = 1 iff x is prime, else 0

f(x) computable <=> for some k, all x:f(x) = TK[x] fK(x)

fK(x, y) TK[<x, y>]

Page 17: State Machines - University of North Carolina at Chapel · PDF fileComp 120 – Spring 2005 2/3/05 L07 – State Machines 1 State Machines 1) State Machine Design 2) How can we improve

Comp 120, Spring 2005 page 172/3 Lecture

Marc Pollefeys 2/3/2005 00:43

L07 – State Machines 33Comp 120 – Spring 2005 2/3/05

Enumeration of Computable functionsConceptual table of TM behaviors...

VERTICAL AXIS: Enumeration of TMs.HORIZONTAL AXIS: Enumeration of input tapes.

(j, k) entry = result of TMk[j] -- integer, or * if never halts.

fi

f0f1

fk(j)fk

fi(0) fi(1) fi(2) fi(j)••• •••

•••

•••

37 23

*62*

•••••••••

•••••••••••••••

•••

•••

•••

•••

••• ••• ••• •••

Is every Integer Function that I can precisely specify computable?

The Halting Problem: Given j, k: Does TMk Halt with input j?

L07 – State Machines 34Comp 120 – Spring 2005 2/3/05

The Halting ProblemThe Halting Function: TH[k, j] = 1 iff TMk[j] halts, else 0

Can a Turing machine compute this function?

x

yTH

1 iff Tx[y] HALTS0 otherwise

Suppose, for a moment, TH exists:

Then we can build a TNasty:

TH?LOOP

HALT

1

0k

TNasty[k] LOOPS if Tk[k] haltsHALTS if Tk[k] loops

If TH iscomputablethen so is

TNasty

N1

NH

1,(1,L)

0,(0,L)N2

-,(0,R)

Replace theHalt stateof TH withthis.

Page 18: State Machines - University of North Carolina at Chapel · PDF fileComp 120 – Spring 2005 2/3/05 L07 – State Machines 1 State Machines 1) State Machine Design 2) How can we improve

Comp 120, Spring 2005 page 182/3 Lecture

Marc Pollefeys 2/3/2005 00:43

L07 – State Machines 35Comp 120 – Spring 2005 2/3/05

What does TNasty[Nasty] do?Answer:

TNasty[Nasty] loops if TNasty[Nasty] haltsTNasty[Nasty] halts if TNasty[Nasty] loops

That’s a contradiction. Thus, TH is uncomputable by a Turing Machine!

Net Result: There are some questions that Turing Machines simply cannot answer. Since, we know of no better model of computation than a Turing machine, this implies that there are some questions that defy computation.

L07 – State Machines 36Comp 120 – Spring 2005 2/3/05

Reality: Limits of Turing MachinesA Turing machine is formal abstraction that addresses

• Fundamental Limits of Computability –What it means to compute.The existence of incomputable functions.

• We know of no machine that is more powerful than a Turing machine in terms of the functions it can compute.

But they ignore

• Practical coding of programs

• Performance

• Implementability

• Programmability

... these latter issues are the primary focus of contemporary computer science (Remainder of Comp 120)