Finite State Automata design -...

27
Recap from Lesson Basic Design Recognizer FSA Operations on FSAs Proving a property of an FSA FSA based translators App Finite State Automata design Alessandro Barenghi Dipartimento di Elettronica e Informazione Politecnico di Milano barenghi - at - elet.polimi.it October 9, 2012

Transcript of Finite State Automata design -...

Page 1: Finite State Automata design - home.deib.polimi.ithome.deib.polimi.it/barenghi/lib/exe/fetch.php?media=teaching:fsa... · Finite State Automata (Automi a stati niti AF) are the ...

Recap from Lesson Basic Design Recognizer FSA Operations on FSAs Proving a property of an FSA FSA based translators Appendix

Finite State Automata design

Alessandro Barenghi

Dipartimento di Elettronica e InformazionePolitecnico di Milano

barenghi - at - elet.polimi.it

October 9, 2012

Page 2: Finite State Automata design - home.deib.polimi.ithome.deib.polimi.it/barenghi/lib/exe/fetch.php?media=teaching:fsa... · Finite State Automata (Automi a stati niti AF) are the ...

Recap from Lesson Basic Design Recognizer FSA Operations on FSAs Proving a property of an FSA FSA based translators Appendix

Finite State Automata

What are FSAs?

Finite State Automata (Automi a stati finiti AF) are thesimplest among abstract computing models

They are still able to describe a good deal of useful systems

They are characterized by a finite memory capacity

In a sense, every real world computing machine is a FSA (as itonly has a limited number of memory cells)

However modeling it as such is not a good idea (too manystates, roughly 22

43 ' 10300000000000)

Page 3: Finite State Automata design - home.deib.polimi.ithome.deib.polimi.it/barenghi/lib/exe/fetch.php?media=teaching:fsa... · Finite State Automata (Automi a stati niti AF) are the ...

Recap from Lesson Basic Design Recognizer FSA Operations on FSAs Proving a property of an FSA FSA based translators Appendix

Formalization

A recognizer FSA is formally defined as a quintuple(Q, I, δ, q0,F), where:

Q, is the set of states of the automataI is the alphabet of the input string which will be checkedδ : Q× I 7→ Q the transition functionq0 ∈ Q the (unique) initial state from where the automatonstartsF ⊆ Q the set of final accepting states of the automaton

Page 4: Finite State Automata design - home.deib.polimi.ithome.deib.polimi.it/barenghi/lib/exe/fetch.php?media=teaching:fsa... · Finite State Automata (Automi a stati niti AF) are the ...

Recap from Lesson Basic Design Recognizer FSA Operations on FSAs Proving a property of an FSA FSA based translators Appendix

Introduction

An oven knob

We will start looking at the design for a simple automatonrepresenting an oven knob

The oven may be On, Off or on Grill position.

The knob turns left and right: left raises the temperature,right lowers it.

Page 5: Finite State Automata design - home.deib.polimi.ithome.deib.polimi.it/barenghi/lib/exe/fetch.php?media=teaching:fsa... · Finite State Automata (Automi a stati niti AF) are the ...

Recap from Lesson Basic Design Recognizer FSA Operations on FSAs Proving a property of an FSA FSA based translators Appendix

A first attempt

Off On Grill

l

r

l

r

But this oven starts in an undefined state...

Page 6: Finite State Automata design - home.deib.polimi.ithome.deib.polimi.it/barenghi/lib/exe/fetch.php?media=teaching:fsa... · Finite State Automata (Automi a stati niti AF) are the ...

Recap from Lesson Basic Design Recognizer FSA Operations on FSAs Proving a property of an FSA FSA based translators Appendix

A first attempt

Offstart On Grill

l

r

l

r

This one’s better, but we’d like to accept only when weremembered to turn it off at the end of the cooking...

Page 7: Finite State Automata design - home.deib.polimi.ithome.deib.polimi.it/barenghi/lib/exe/fetch.php?media=teaching:fsa... · Finite State Automata (Automi a stati niti AF) are the ...

Recap from Lesson Basic Design Recognizer FSA Operations on FSAs Proving a property of an FSA FSA based translators Appendix

A first attempt

Offstart On Grill

l

r

l

r

Ok, but what if the knob is turned further left (resp. right) whenit’s already on the “Grill” (resp.“Off”) position?

Page 8: Finite State Automata design - home.deib.polimi.ithome.deib.polimi.it/barenghi/lib/exe/fetch.php?media=teaching:fsa... · Finite State Automata (Automi a stati niti AF) are the ...

Recap from Lesson Basic Design Recognizer FSA Operations on FSAs Proving a property of an FSA FSA based translators Appendix

A first attempt

Offstart On Grill

l

r

r

l

l

r

Great. What sequences of actions (language strings) does thisrecognize? L = (l∗(lr)∗(llrr(r)∗)∗)∗

Page 9: Finite State Automata design - home.deib.polimi.ithome.deib.polimi.it/barenghi/lib/exe/fetch.php?media=teaching:fsa... · Finite State Automata (Automi a stati niti AF) are the ...

Recap from Lesson Basic Design Recognizer FSA Operations on FSAs Proving a property of an FSA FSA based translators Appendix

A first recognizer

Let’s try the other way around: design a FSA recognizing aspecific language

Example: L = {(0|1)∗} \ {0(0)+}, that is the language whereevery zero is followed by at least a one (i.e. there cannot betwo consecutive zeros)

q0start q1

0

1

1

δ(q1, 0) is undefined here!

Page 10: Finite State Automata design - home.deib.polimi.ithome.deib.polimi.it/barenghi/lib/exe/fetch.php?media=teaching:fsa... · Finite State Automata (Automi a stati niti AF) are the ...

Recap from Lesson Basic Design Recognizer FSA Operations on FSAs Proving a property of an FSA FSA based translators Appendix

The error state

The previous FSA does not explicitly represent error states

As this may result in issues when performing operation amongautomata, we’ll add it

q0start q1 error

0

1

1

0

The δ(·, ·) function is now complete (safer to performcomplement).

Page 11: Finite State Automata design - home.deib.polimi.ithome.deib.polimi.it/barenghi/lib/exe/fetch.php?media=teaching:fsa... · Finite State Automata (Automi a stati niti AF) are the ...

Recap from Lesson Basic Design Recognizer FSA Operations on FSAs Proving a property of an FSA FSA based translators Appendix

Complement

How-To

Given a recogniser FSA for the language L, the onerecognising the complement of the language LC can beobtained flipping the termination condition

Basically: all the accepting states become non accepting andvice-versa

Take care: the error state is nothing more than a commonnon accepting state and must be included in the process.

A safe way to take it into account is to complete theautomaton before building the complementary one

Page 12: Finite State Automata design - home.deib.polimi.ithome.deib.polimi.it/barenghi/lib/exe/fetch.php?media=teaching:fsa... · Finite State Automata (Automi a stati niti AF) are the ...

Recap from Lesson Basic Design Recognizer FSA Operations on FSAs Proving a property of an FSA FSA based translators Appendix

Complement

Let’s take as an example the recogniser for L = 0(0|1)∗

The recognizer looks like this :

Partial δ(·, ·)

q0start

q10

0

1

Complete δ(·, ·)

q0start

q1

error

0

1

0 | 1

0 | 1

Page 13: Finite State Automata design - home.deib.polimi.ithome.deib.polimi.it/barenghi/lib/exe/fetch.php?media=teaching:fsa... · Finite State Automata (Automi a stati niti AF) are the ...

Recap from Lesson Basic Design Recognizer FSA Operations on FSAs Proving a property of an FSA FSA based translators Appendix

Complement

The recogniser for LC is thus :

Recogniser for (0|1)∗ \ L

q0start q1 error

0

1

0 | 1 0 | 1

Page 14: Finite State Automata design - home.deib.polimi.ithome.deib.polimi.it/barenghi/lib/exe/fetch.php?media=teaching:fsa... · Finite State Automata (Automi a stati niti AF) are the ...

Recap from Lesson Basic Design Recognizer FSA Operations on FSAs Proving a property of an FSA FSA based translators Appendix

Intersection

The recogniser automaton for the intersection of twolanguages L ∩M can be built starting from the onesrecognising them

The steps to be followed are :1 Build the state set as the Cartesian product of the state sets

(hint: label the states with the combination of the two originallabels)

2 Set the initial state to the one obtained via the Cartesianproduct of q0l and q0m

3 Start deriving the δ function from the initial state: simplycheck the original deltas and choose the destination stateobtained as the product of the destinations

4 Mark as final only the states obtained as the product of twofinal states

Page 15: Finite State Automata design - home.deib.polimi.ithome.deib.polimi.it/barenghi/lib/exe/fetch.php?media=teaching:fsa... · Finite State Automata (Automi a stati niti AF) are the ...

Recap from Lesson Basic Design Recognizer FSA Operations on FSAs Proving a property of an FSA FSA based translators Appendix

Example intersection

We want to obtain a recogniser for L ∩M

Recogniser for L

l0start

l10

0 | 1

Recogniser for M

m0start

m1

1

0 | 1

Page 16: Finite State Automata design - home.deib.polimi.ithome.deib.polimi.it/barenghi/lib/exe/fetch.php?media=teaching:fsa... · Finite State Automata (Automi a stati niti AF) are the ...

Recap from Lesson Basic Design Recognizer FSA Operations on FSAs Proving a property of an FSA FSA based translators Appendix

Intersection Automaton

Resulting automaton (including unreachable states)

l0,m0start l0,m1

l1,m0 l1,m1

0

10 | 1

Page 17: Finite State Automata design - home.deib.polimi.ithome.deib.polimi.it/barenghi/lib/exe/fetch.php?media=teaching:fsa... · Finite State Automata (Automi a stati niti AF) are the ...

Recap from Lesson Basic Design Recognizer FSA Operations on FSAs Proving a property of an FSA FSA based translators Appendix

Union

It is possible to use complement and intersection to computethe recogniser automaton for L ∪M

The key idea is to exploit De Morgan’s Law applied to setoperations: ¬(a ∪ b) = (¬a) ∩ (¬b)

It is thus possible to derivea ∪ b = ¬(¬(a ∪ b)) = ¬((¬a) ∩ (¬b))

Therefore , applying in sequence two complements anintersection and another complement we obtain the unionautomaton

Page 18: Finite State Automata design - home.deib.polimi.ithome.deib.polimi.it/barenghi/lib/exe/fetch.php?media=teaching:fsa... · Finite State Automata (Automi a stati niti AF) are the ...

Recap from Lesson Basic Design Recognizer FSA Operations on FSAs Proving a property of an FSA FSA based translators Appendix

Proving Properties on an FSA

The following automaton recognises L = (0|1)∗12k+1.

q0start

q1

q2

1

0

0

1

0

1

The most straightforward way to provide it is by induction onthe steps performed

Even if it is possible to intuitively check that this is right, weneed to provide a formal proof

Page 19: Finite State Automata design - home.deib.polimi.ithome.deib.polimi.it/barenghi/lib/exe/fetch.php?media=teaching:fsa... · Finite State Automata (Automi a stati niti AF) are the ...

Recap from Lesson Basic Design Recognizer FSA Operations on FSAs Proving a property of an FSA FSA based translators Appendix

Mathematical Induction

The idea is to prove by induction (on the steps performed bythe automaton):

That all the strings in the language lead the computation in anaccepting stateThat all the strings not in the language lead the computationto a non accepting state

To this end, we split the possible strings in three classes :1 Not ending in 1, i.e. L1 = (0|1)∗0 ∪ ε (/∈ L)2 Ending in 12k , i.e. L2 = (0|1)∗12k (/∈ L)3 Ending in 12k+1, i.e. L3 = (0|1)∗12k+1 (∈ L)

Page 20: Finite State Automata design - home.deib.polimi.ithome.deib.polimi.it/barenghi/lib/exe/fetch.php?media=teaching:fsa... · Finite State Automata (Automi a stati niti AF) are the ...

Recap from Lesson Basic Design Recognizer FSA Operations on FSAs Proving a property of an FSA FSA based translators Appendix

Mathematical Induction

Building hypotheses and checking base cases

Our purpose is thus to prove that:1 ∀s ∈ L1, δ∗(q0, s) = q02 ∀s ∈ L2, δ∗(q0, s) = q23 ∀s ∈ L3, δ∗(q0, s) = q1

These three propositions will work for us as inductionhypotheses

Since we are performing the induction on the steps of theautomaton, the base cases are represented by the shorteststrings from each class

Let’s start from checking the base cases:1 δ(q0, ε) = q0 and δ(q0, 0) = q0 ? Yes.2 δ(q0, 11) = q2 ? Yes.3 δ(q0, 1) = q1 ? Yes.

Page 21: Finite State Automata design - home.deib.polimi.ithome.deib.polimi.it/barenghi/lib/exe/fetch.php?media=teaching:fsa... · Finite State Automata (Automi a stati niti AF) are the ...

Recap from Lesson Basic Design Recognizer FSA Operations on FSAs Proving a property of an FSA FSA based translators Appendix

Mathematical Induction

Proofs

1 δ∗(q0, s) = δ(δ∗(q0, s′′), 0) = δ(qi , 0) where i ∈ 0, 1, 2. Since

δ goes back to q0 for every possible value of δ∗(q0, s′′) if the

input is 0, the first statement is correct.2 Let’s tackle the second hypothesis in two steps :

1 s is only constituted by ones: δ∗(q0, 12k) = δ(δ∗(q0, 1

2k−1), 1)which can be reduced, exploiting hypothesis 3 to δ(q1, 1) = q2.

2 s ends in 12k after a preamble:δ∗(q0, (0|1)∗012k) = δ(δ∗(q0, (0|1)∗0), 12k) which, accordingto hypothesis 1 δ(q0, 1

2k), which by the previous step = q2

Page 22: Finite State Automata design - home.deib.polimi.ithome.deib.polimi.it/barenghi/lib/exe/fetch.php?media=teaching:fsa... · Finite State Automata (Automi a stati niti AF) are the ...

Recap from Lesson Basic Design Recognizer FSA Operations on FSAs Proving a property of an FSA FSA based translators Appendix

Mathematical Induction

Proofs

Analogously for the third:1 s is only constituted by ones: δ∗(q0, 1

2k+1) = δ(δ∗(q0, 12k), 1)

which can be reduced, exploiting hypothesis 2 to δ(q2, 1) = q1.2 s ends in 12k+1 after a preamble:δ∗(q0, (0|1)∗012k+1) = δ(δ∗(q0, (0|1)∗0), 12k+1) which,according to hypothesis 1 δ(q0, 1

2k+1), which by the previousstep = q1

Page 23: Finite State Automata design - home.deib.polimi.ithome.deib.polimi.it/barenghi/lib/exe/fetch.php?media=teaching:fsa... · Finite State Automata (Automi a stati niti AF) are the ...

Recap from Lesson Basic Design Recognizer FSA Operations on FSAs Proving a property of an FSA FSA based translators Appendix

Translators

It is possible to enrich the computation model of a recogniserFSA with the ability to translate languages

The FSA is only able to translate languages where the sourceis recognisable

A translation move is characterised by reading at most onecharacter and outputting at most one

A typical example is the find-replace function of a commoneditor

Page 24: Finite State Automata design - home.deib.polimi.ithome.deib.polimi.it/barenghi/lib/exe/fetch.php?media=teaching:fsa... · Finite State Automata (Automi a stati niti AF) are the ...

Recap from Lesson Basic Design Recognizer FSA Operations on FSAs Proving a property of an FSA FSA based translators Appendix

Formalization

A transducer FSA is formally defined as a 7-tuple(Q, I, δ, q0,F,O, η), where:

Q, is the set of states of the automataI is the alphabet of the input string which will be checkedδ : Q× I 7→ Q the transition functionq0 ∈ Q the (unique) initial state from where the automatonstartsF ⊆ Q the set of final accepting states of the automatonO the output alphabet (may coincide with I)η : Q× I 7→ {O ∪ ε} the transduction function

Page 25: Finite State Automata design - home.deib.polimi.ithome.deib.polimi.it/barenghi/lib/exe/fetch.php?media=teaching:fsa... · Finite State Automata (Automi a stati niti AF) are the ...

Recap from Lesson Basic Design Recognizer FSA Operations on FSAs Proving a property of an FSA FSA based translators Appendix

Example

We want to build a transducer automaton accepting the inputlanguage L = (ab)+c(ba)∗

The target language is Lt = (dd)+ef ∗

The translation map τ is defined asτ((ab)nc(ba)m) = d2nef (m÷2)

Since only a finite memory is required to perform the ÷2operation, the language can be translated by a FSA.

The transducer automaton is represented graphically markingeach edge with the read/written symbol pair

Page 26: Finite State Automata design - home.deib.polimi.ithome.deib.polimi.it/barenghi/lib/exe/fetch.php?media=teaching:fsa... · Finite State Automata (Automi a stati niti AF) are the ...

Recap from Lesson Basic Design Recognizer FSA Operations on FSAs Proving a property of an FSA FSA based translators Appendix

Transducer FSA

L to Lt transducer

q0start q1 q2

q6 q5 q4q7

a/d b/d

a/dc/ε

b/εa/fb/ε

a/f

Page 27: Finite State Automata design - home.deib.polimi.ithome.deib.polimi.it/barenghi/lib/exe/fetch.php?media=teaching:fsa... · Finite State Automata (Automi a stati niti AF) are the ...

Recap from Lesson Basic Design Recognizer FSA Operations on FSAs Proving a property of an FSA FSA based translators Appendix

Regular expressions cheat sheet

A small RE cheat sheet

Symbols belonging to the input alphabet are usually lowercase

s.t : s concatenated with t

r | s : either r or s

Parentheses force a precedence when reading the regexpr

s∗: an arbitrary number of occurrences of s (Kleene’s Star)

s+: one or more occurrences of s (equivalent to s(s)∗)