State Machines Example FSM: Roboant

Comp 120, Spring 2005
2/3 Lecture
page 1
State Machines
1)
2)
3)
4)
State Machine Design
How can we improve on FSMs?
Turing Machines
Computability
Oh genie, will you now
tell me what it means
to compute?
Doctor, I think
you’ve built a
monster!
2/3/05
Comp 120 – Spring 2005
L07 – State Machines 1
Example FSM: Roboant
8 legs?
Oh, he’s a
binary bug
SENSORS: antennae L and R, each 1 if in contact with
something.
ACTUATORS: Forward Step F, ten-degree turns TL and
TR (left, right).
GOAL: Make our ant smart enough to get out of a maze like:
STRATEGY: "Right antenna to the wall"
Comp 120 – Spring 2005
Marc Pollefeys 2/3/2005 00:43
2/3/05
L07 – State Machines 2
Comp 120, Spring 2005
2/3 Lecture
page 2
Lost in Space
?
Action: Go forward until we hit something.
LOST
L+R
F
__
LR
“lost” is the
initial state
2/3/05
Comp 120 – Spring 2005
L07 – State Machines 3
Bonk!
Action: Turn left (CCW) until we don’t touch anymore
L+R
LOST
L+R
RCCW
F
__
LR
Comp 120 – Spring 2005
Marc Pollefeys 2/3/2005 00:43
TL
_ _
LR
2/3/05
L07 – State Machines 4
Comp 120, Spring 2005
2/3 Lecture
page 3
A Little to the Right…
Action: Step and turn right a little, look for wall
L+R
LOST
L+R
RCCW
F
TL
_ _
LR
__
LR
R
Wall1
TR,F
_
R
2/3/05
Comp 120 – Spring 2005
L07 – State Machines 5
Then a Little to the Left
Action: Step and turn left a little, till not touching (again)
L+R
LOST
L+R
RCCW
F
__
LR
_ _
LR
R
TR,F
Marc Pollefeys 2/3/2005 00:43
Wall2
L
TL
Wall1
Comp 120 – Spring 2005
_
LR
2/3/05
TL,F
__
LR
_
R
L07 – State Machines 6
Comp 120, Spring 2005
2/3 Lecture
page 4
Dealing with Corners
Action: Step and turn right until we hit perpendicular wall
L+R
LOST
L+R
Wall2
RCCW
F
L
TL
_ _
LR
__
LR
R
Wall1
TR,F
_
LR
TL,F
__
LR
R
Corner
_
R
_
R
TR,F
2/3/05
Comp 120 – Spring 2005
L07 – State Machines 7
Equivalent State Reduction
Observation: Si ≡ Sj if
1. States have identical outputs; AND
2. Every input →equivalent states.
Reduction Strategy:
Find pairs of equivalent states, MERGE them.
L+R
LOST
L+R
RCCW
F
__
LR
_ _
LR
R
TR,F
Marc Pollefeys 2/3/2005 00:43
L
TL
Wall1
Comp 120 – Spring 2005
Wall2
2/3/05
_
R
_
LR
TL,F
__
LR
R
Corner
_
R
TR,F
L07 – State Machines 8
2/3 Lecture
Comp 120, Spring 2005
page 5
An Evolutionary Step
Merge equivalent states Wall1 and Corner into a single new,
combined state.
L+R
LOST
Wall2
RCCW
L+R
F
_ _
LR
__
LR
TL,F
L
TL
R
_
LR
__
LR
Wall1
TR,F
_
R
Behaves exactly as previous (5-state) FSM, but requires half the ROM in its
implementation!
2/3/05
Comp 120 – Spring 2005
L07 – State Machines 9
Building the Transition Table
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.
L+R
LOST
L+R
F
RCCW
TL
__
LR
Comp 120 – Spring 2005
Marc Pollefeys 2/3/2005 00:43
2/3/05
S L R | S’ TR TL F
-------+----------00 0 0 | 00 0 0 1
00 1 - | 01 0 0 1
00 - 1 | 01 0 0 1
01 1 - | 01 0 1 0
01 0 1 | 01 0 1 0
|
|
|
|
|
|
L07 – State Machines 10
Comp 120, Spring 2005
2/3 Lecture
page 6
Implementation Details
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.
00
LR 01
11
10
S L R | S’ TR TL F
-------+----------00 0 0 | 00 0 0 1
00 1 - | 01 0 0 1
00 0 1 | 01 0 0 1
01 1 - | 01 0 1 0
01 0 1 | 01 0 1 0
01 0 0 | 10 0 1 0
10 – 0 | 10 1 0 1
10 – 1 | 11 1 0 1
11 1 - | 01 0 1 1
11 0 0 | 10 0 1 1
11 0 1 | 11 0 1 1
LOST
RCCW
WALL1
WALL2
S1’
S1′ = S1 S 0 + LS1 + L RS 0
S0’
00
LR 01
11
10
S1S0
00 01 11 10
0 0 0 0
1 1 1 1
1 1 1 1
1 1 1 0
S 0′ = R + L S1 + LS 0
Complete Transition table
2/3/05
Comp 120 – Spring 2005
S1S0
00 01 11 10
0 1 1 1
0 0 1 1
0 0 0 1
0 0 0 1
L07 – State Machines 11
Ant Schematic
Are you telling me that
essence amounts to no
more than 15 gates!
Comp 120 – Spring 2005
Marc Pollefeys 2/3/2005 00:43
2/3/05
L07 – State Machines 12
Comp 120, Spring 2005
2/3 Lecture
page 7
Super Roboant
Maze
selection
FSM state
table
Plan view
of maze
Simulation
controls
Status
display
Featuring the new Mark-II ant: It can add crumbs (M),
erase crumbs (E), and sense (S) crumbs along its path.
2/3/05
Comp 120 – Spring 2005
L07 – State Machines 13
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!
Comp 120 – Spring 2005
Marc Pollefeys 2/3/2005 00:43
2/3/05
L07 – State Machines 14
2/3 Lecture
Comp 120, Spring 2005
page 8
2-Flavors of Processing Elements
Combinational Logic:
Table look-up, ROM
i
Addr
Data
o
Fundamentally,
everything
that we’ve
learned so far
can be done
with a ROM
and registers
i
Finite State Machines:
ROM with Feedback
s
o
Addr
Data
Thus far, we know of nothing more
powerful than an FSM
2/3/05
Comp 120 – Spring 2005
L07 – State Machines 15
FSMs as Programmable Machines
An FSM’s behavior is completely
determined by its ROM contents.
ROM-based FSM sketch:
Given i, s, and o,we need a ROM
organized as:
inputs
i
2i+s words x (o+s) bits
So how many possible
i-input,
o-output,
FSMs with
s-state bits
exist?
2
0...00
0...01
i
sN+1
o
0...00
10110
011
s
o
(some may be
equivalent)
Marc Pollefeys 2/3/2005 00:43
sN
2i+s
(o+s)2i+s
Comp 120 – Spring 2005
outputs
2/3/05
L07 – State Machines 16
2/3 Lecture
Comp 120, Spring 2005
page 9
FSM Enumeration
GOAL: List all possible FSMs in some
canonical order.
i s o FSM#
• INFINITE list, but
• Every FSM has an entry in
and an associated index.
inputs
i
0...00
0...01
sN
0...00
outputs
o
10110
sN+1
These FSMs have 2
inputs and 2
outputs, or 8-bits in
their ROM.
Truth Table
1 1 1
1
00000000
1 1 1
2
00000001
…
…
1 1 1
256
11111111
2 2 2
257
000000…000000
2 2 2
258
000000…000001
…
…
3 3 3
28
FSMs
264
000000…000000
…
011
4 4 4
000000…000000
Every possible FSM can be associated with a unique number. This
requires a few wasteful simplifications. First, given an i-input, sstate-bit, and o-output FSM, we’ll replace it with its equivalent ninput, 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
2/3/05
Comp 120 – Spring 2005
L07 – State Machines 17
Some Perennial Favorites...
FSM837
FSM1077
FSM89143
FSM22698469884
FSM784362783
FSM784363783
Comp 120 – Spring 2005
Marc Pollefeys 2/3/2005 00:43
modulo 3 counter
4-bit counter
Cheap digital watch
Intel Pentium CPU – rev 1
Intel Pentium CPU – rev 2
Intel Pentium II CPU
2/3/05
L07 – State Machines 18
2/3 Lecture
Comp 120, Spring 2005
page 10
Are FSMs the Ultimate
Computation 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?
2/3/05
Comp 120 – Spring 2005
L07 – State Machines 19
Yes, Super Roboant can!
Next
State Input Crumb? Crumb Move State
S1
(
-
Y
R
Comment
S1
Mark open paren
S1
)
-
Y
L
S2
Mark close paren
S1
S2
S2
sp
-
N
Y
N
N
N
L
L
R
S4
S2
S3
Reached end
Scan back to last open
Eat crumb
S3
S3
S4
S4
S4
S5
S5
-
N
Y
N
Y
-
N
N
N
N
Y
N
N
R
R
L
L
L
L
L
S3
S1
S4
S5
Halt
S5
Halt
Goto close paren
Eat crumb
Move Left
Unmatched/Eat
Matched
Unmatched/Eat
Unmatched
( or )
( or )
sp
( or )
sp
(
(
(
(
)
(
)
)
)
)
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?
Comp 120 – Spring 2005
Marc Pollefeys 2/3/2005 00:43
2/3/05
L07 – State Machines 20
2/3 Lecture
Comp 120, Spring 2005
page 11
Yes, Roboant can!
Next
State Input Crumb? Crumb Move State
S1
(
-
Y
R
Comment
S1
Mark open paren
S1
)
-
Y
L
S2
Mark close paren
S1
S2
S2
sp
-
N
Y
N
N
N
L
L
R
S4
S2
S3
Reached end
Scan back to last open
Eat crumb
S3
S3
S4
S4
S4
S5
S5
-
N
Y
N
Y
-
N
N
N
N
Y
N
N
R
R
L
L
L
L
L
S3
S1
S4
S5
Halt
S5
Halt
Goto close paren
Eat crumb
Move Left
Unmatched/Eat
Matched
Unmatched/Eat
Unmatched
( or )
( or )
sp
( or )
sp
(
(
(
(
)
(
)
)
)
)
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?
2/3/05
Comp 120 – Spring 2005
L07 – State Machines 21
Yes, Roboant can!
Next
State Input Crumb? Crumb Move State
S1
(
-
Y
R
Comment
S1
Mark open paren
S1
)
-
Y
L
S2
Mark close paren
S1
S2
S2
sp
-
N
Y
N
N
N
L
L
R
S4
S2
S3
Reached end
Scan back to last open
Eat crumb
S3
S3
S4
S4
S4
S5
S5
-
N
Y
N
Y
-
N
N
N
N
Y
N
N
R
R
L
L
L
L
L
S3
S1
S4
S5
Halt
S5
Halt
Goto close paren
Eat crumb
Move Left
Unmatched/Eat
Matched
Unmatched/Eat
Unmatched
( or )
( or )
sp
( or )
sp
(
(
(
(
)
(
)
)
)
)
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?
Comp 120 – Spring 2005
Marc Pollefeys 2/3/2005 00:43
2/3/05
L07 – State Machines 22
2/3 Lecture
Comp 120, Spring 2005
page 12
Yes, Roboant can!
Next
State Input Crumb? Crumb Move State
S1
(
-
Y
R
Comment
S1
Mark open paren
S1
)
-
Y
L
S2
Mark close paren
S1
S2
S2
sp
-
N
Y
N
N
N
L
L
R
S4
S2
S3
Reached end
Scan back to last open
Eat crumb
S3
S3
S4
S4
S4
S5
S5
-
N
Y
N
Y
-
N
N
N
N
Y
N
N
R
R
L
L
L
L
L
S3
S1
S4
S5
Halt
S5
Halt
Goto close paren
Eat crumb
Move Left
Unmatched/Eat
Matched
Unmatched/Eat
Unmatched
( or )
( or )
sp
( or )
sp
(
(
(
(
)
(
)
)
)
)
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?
2/3/05
Comp 120 – Spring 2005
L07 – State Machines 23
Unbounded-Space Computation
0000000111100000
0,(1,R)
S1
1,(1,L)
S2
DURING 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.
0,(1,L)
1,Halt
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...
Alan Turing
Comp 120 – Spring 2005
Marc Pollefeys 2/3/2005 00:43
This simple addition solves "FINITE" problem
of FSMs. (Like crumbs)
2/3/05
L07 – State Machines 24
2/3 Lecture
Comp 120, Spring 2005
page 13
A Turing Machine Example
A Turing machine, like an FSM,
can be specified with a truth table.
The following Turing Machine
implements a unary (base 1)
incrementer.
Turing Machine Specification
• Doubly-infinite tape
• Discrete symbol positions
• Finite alphabet – say {0, 1}
• Control FSM
INPUTS:
Current symbol
OUTPUTS:
write 0/1
move Left/Right
• Initial Starting State {S0}
• Halt State {Halt}
Current Tape Write
Next
State Input Tape Move State
S0
S0
S1
S1
1
0
1
0
R
L
L
R
S0
S1
S1
Halt
000011111
00
2/3/05
Comp 120 – Spring 2005
1
1
1
0
L07 – State Machines 25
Turing Machine Tapes as Integers
Canonical names for bounded tape configurations:
b8
b6
b4
b2
b0
b1
b3
b5
b7
0 0 1 0 0 1 1 0 0
Look, it’s just FSM i
operating on tape j
FSM
Comp 120 – Spring 2005
Marc Pollefeys 2/3/2005 00:43
i
2/3/05
L07 – State Machines 26
Comp 120, Spring 2005
2/3 Lecture
page 14
TMs as Integer Functions
Turing Machine Ti operating on Tape x,
where x = …b8b7b6b5b4b3b2b1b0
y = Ti [x]
x: input tape configuration
y: output tape when TM halts
I wonder if a TM can compute
EVERY integer function...
2/3/05
Comp 120 – Spring 2005
L07 – State Machines 27
Alternative Models of Computation
Turing Machines [Turing]
Recursive Functions [Kleene]
F(0,x) ≡ x
F(1+y,x) ≡ 1+F(x,y)
(define (fact n)
(... (fact (- n 1)) ...)
Hardware
head
0 0 1 0 0 1 1 0 0
FSM i
Theory
head
Kleene
Production Systems [Post, Markov]
Turing
Lambda calculus [Church, Curry, Rosser...]
Math
head
λx.λy.xxy
Language
head
α→β
IF pulse=0 THEN
patient=dead
(lambda(x)(lambda(y)(x (x y))))
Post
Church
Comp 120 – Spring 2005
Marc Pollefeys 2/3/2005 00:43
2/3/05
L07 – State Machines 28
2/3 Lecture
Comp 120, Spring 2005
page 15
The 1st Computer Industry Shakeout
Here’s a TM that
computes SQUARE ROOT!
0
0
1
0
0
1
1
0
0
FSM
2/3/05
Comp 120 – Spring 2005
L07 – State Machines 29
And the Battles Raged
Here’s a Lambda Expression
that does the same thing...
(λ(x) .....)
... and here’s one that computes
the nth root for ANY n!
(λ(x n) .....)
Comp 120 – Spring 2005
Marc Pollefeys 2/3/2005 00:43
2/3/05
L07 – State Machines 30
Comp 120, Spring 2005
2/3 Lecture
page 16
Fundamental Result:
Computable Functions
Each model is capable of computing exactly the same set of integer
functions!
Proof Technique: Constructions that
translate between
models
BIG IDEA:
Computability,
independent of
computation scheme
chosen
Does,this
mean that
we know of
no computer
that is more
“powerful”
than a
Turing
machine?
Church's Thesis:
Every discrete function computable
by ANY realizable machine is
computable by some Turing machine.
2/3/05
Comp 120 – Spring 2005
L07 – State Machines 31
Computable Functions
f(x) computable <=> for some k, all x:
f(x) = TK[x]
fK(x)
Representation tricks: to compute fk(x,y)
<x,y> ≡ integer whose even bits come from x, and whose odd bits come from y;
whence
fK(x, y)
TK[<x, y>]
f12345(x,y) = x * y
f23456(x) = 1 iff x is prime, else 0
Comp 120 – Spring 2005
Marc Pollefeys 2/3/2005 00:43
2/3/05
L07 – State Machines 32
2/3 Lecture
Comp 120, Spring 2005
page 17
Enumeration of Computable functions
Conceptual 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
fi(0)
fi(1) fi(2)
f0
f1
37
62
23
*
•••
*
•••
•••
•••
•••
•••
•••
•••
fk
•••
•••
•••
•••
•••
•••
•••
•••
Is every Integer
Function that I can
precisely specify
computable?
••• fi(j) •••
•••
fk(j)
•••
The Halting Problem: Given j, k: Does TMk Halt with input j?
2/3/05
Comp 120 – Spring 2005
L07 – State Machines 33
The Halting Problem
The Halting Function: TH[k, j] = 1 iff TMk[j] halts, else 0
Can a Turing machine compute this function?
Suppose, for a moment, TH exists:
TH
1 iff Tx[y] HALTS
0 otherwise
Replace the
Halt state
of TH with
this.
1,(1,L)
-,(0,R)
N2
N1
0,(0,L)
Then we can build a TNasty:
LOOP
HALT
1
?
0
TH
x
If TH is
computable
then so is
TNasty
y
k
NH
TNasty[k]
Comp 120 – Spring 2005
Marc Pollefeys 2/3/2005 00:43
LOOPS if Tk[k] halts
HALTS if Tk[k] loops
2/3/05
L07 – State Machines 34
Comp 120, Spring 2005
2/3 Lecture
page 18
What does TNasty[Nasty] do?
Answer:
TNasty[Nasty] loops if TNasty[Nasty] halts
TNasty[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.
2/3/05
Comp 120 – Spring 2005
L07 – State Machines 35
Reality: Limits of Turing Machines
A 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)
Comp 120 – Spring 2005
Marc Pollefeys 2/3/2005 00:43
2/3/05
L07 – State Machines 36