Finite state systems TDDC05: Embedded Systems • Inputs: modelled with finite set of symbols • Reaction: based on finite sequence of earlier input symbols • Time: sequence of reactions • ⇒ Finite state machines (FSM) Simulation and Verification Lecture 2: Finite state automata as reactive systems Simin Nadjm-Tehrani Real-time Systems Laboratory Department of Computer and Information Science Embedded systems simulation and verification Linköping university 28 pages Spring term 2006 Embedded systems simulation and verification Linköping university Example: Cash dispenser code amount rejected return Strings • • • • approved card ok rejected rejected 2 of 28 Spring term 2006 Over alphabet Σ of symbols Finite sequence of symbols over Σ Empty string denoted ε Definition: – ε is a string – if w is a string, and a ∈ Σ then aw is a string rejected money Embedded systems simulation and verification Linköping university 3 of 28 Spring term 2006 Embedded systems simulation and verification Linköping university Finite deterministic automata • • • • • Finite alphabet Finite number of states Defined initial state A (number of) final state(s) Deterministic transitions: – for each state and each input there is exactly one next state Embedded systems simulation and verification Linköping university 5 of 28 Spring term 2006 4 of 28 Spring term 2006 Formal definition A finite deterministic automaton is a 5-tuple 〈Q, Σ, δ, q0, F〉 where • Q is a finite set of states, q0 ∈ Q • Σ is a finite (input) alphabet • δ is a function: Q × Σ → Q that gives next state for each input from each state • F ⊆ Q denotes final state Embedded systems simulation and verification Linköping university 6 of 28 Spring term 2006 The language of Automata Discretised landing gear • Extend δ to strings over the alphabet – δ(q, ε) = q – δ(q, aw) = δ(δ(q, a), w) open close retract extend • A string w is accepted by the automaton M iff δ(q0, w) ∈ F • M:s language L(M) = {w| δ(q0, w) ∈ F} Controller Embedded systems simulation and verification Linköping university 7 of 28 Spring term 2006 Landing gear Embedded systems simulation and verification Linköping university FSM model r,c o,r opened+retracted e o,e c e –A language Ls –A set of reachable states Rs closed+extended r opened+extended o,c,r,e c Embedded systems simulation and verification Linköping university 9 of 28 Spring term 2006 Verification • M satisfies the specification S if –L(M) ⊆ Ls –R(M) ⊆ Rs Only those states allowed by the specification S are reachable in M Embedded systems simulation and verification Linköping university Specification • Automaton M:s reachability set R(M) = {q| ∃ w. δ(q0, w) = q} • A specification S: closed+retracted o 8 of 28 Spring term 2006 11 of 28 Spring term 2006 • What we will (will not) the system to do Embedded systems simulation and verification Linköping university 10 of 28 Spring term 2006 Non-deterministic FSM A finite non-deterministic automaton is a 5-tuple 〈Q, Σ, δ, q0, F〉 where • Q is a finite set of states, q0 ∈ Q • Σ is a finite (input) alphabet • δ is a function: Q × Σ → 2Q that gives possible next state for each input and each state • F ⊆ Q describes the final states Embedded systems simulation and verification Linköping university 12 of 28 Spring term 2006 Exercise Example: Telephone • Describe the cash dispenser with a nondeterministic finite automaton! 2 connect 4 Embedded systems simulation and verification Linköping university 13 of 28 Spring term 2006 ring dial 0 hang-up = = = = = dial reciever on reciever off idle busy connected ui qi qi+1= δ (ui , qi) Memory Clock Embedded systems simulation and verification Linköping university 15 of 28 Spring term 2006 Gear: open A Moore machine is a tuple 〈Q, Σ, ∆, δ, λ, q0〉 where • Q is a finite set of states, q0 ∈ Q • Σ is a finite input alphabet • ∆ is a finite output alphabet • δ is a function: Q × Σ → Q that gives the next state for each input • λ is a function: Q → ∆ that gives the output in each state Embedded systems simulation and verification Linköping university open close closed close Embedded systems simulation and verification Linköping university retract 16 of 28 Spring term 2006 Mealy machine A Mealy machine is a tuple 〈Q, Σ, ∆, δ, q0〉 where • Q, Σ, ∆, q0 as in Moore-machine retract retracted opened 14 of 28 Spring term 2006 Moore machine Landing gear: Moore-machine Door: 3 Embedded systems simulation and verification Linköping university Dynamic systems with output yi = λ (ui , qi) 1 hang-up hang-up 0 1 2 3 4 hang-up extend extended extend 17 of 28 Spring term 2006 • δ is a relation: δ ⊆ Q × 2Σ × 2∆ × Q that for a given state and possible inputs gives possible output and next state • For deterministic machines the function λ is defined based on δ Embedded systems simulation and verification Linköping university 18 of 28 Spring term 2006 Exercise • Non-determinism: – Note: δ ⊆ Q × 2Σ × 2∆ × Q is a notation that describes a relation between elements in a domain. Same set of elements can be represented by a function δ: Q × 2Σ × 2∆ → 2Q • Relative expressiveness of DFA and NDFA • Expressiveness of Moore and Mealy machines • Draw the landing gear as a Mealy machine! Embedded systems simulation and verification Linköping university FAQs 19 of 28 Spring term 2006 Embedded systems simulation and verification Linköping university Composition • Assume that the subsystems are synchronous, i.e. When one takes one step the others take (at least) one step • Then one can define the synchronous composition • Special consideration if the subsystems communicate with each other 20 of 28 Spring term 2006 Parallel composition F1 q1 F1 F2 F2 q2 q2 q1 Embedded systems simulation and verification Linköping university 21 of 28 Spring term 2006 Embedded systems simulation and verification Linköping university 22 of 28 Spring term 2006 ||-operator for Mealy Let M1 = 〈Q1, Σ1, ∆1, δ1, q01〉 and • Q = Q1 × Q2 • q0 = 〈q01,q02〉 • Σ = Σ1 ∪ Σ2 - ( ∆1 ∪ ∆2) • ∆ = ∆1 ∪ ∆2 • δ ⊆ Q × 2Σ × 2∆ × Q such that 〈 〈q1,q2〉, i, o, 〈q1´,q2´〉 〉 ∈ δ iff 〈 q1, (i ∪ o) ∩ Σ1, o ∩ ∆1, q1´ 〉 ∈ δ1 and 〈 q2, (i ∪ o) ∩ Σ2, o ∩ ∆2, q2´ 〉 ∈ δ2 M2 = 〈Q2, Σ2, ∆2, δ2, q02〉 be two Mealymachines where ∆1 ∩ ∆2 = ∅ Then the synchronous composition M = M1 || M2 is a Mealy-machine 〈Q, Σ, ∆, δ, q0〉 such that: Embedded systems simulation and verification Linköping university 23 of 28 Spring term 2006 Embedded systems simulation and verification Linköping university 24 of 28 Spring term 2006 Exercise Serial composition • Construct the landing gear door/gear composition as a Mealy machine! Embedded systems simulation and verification Linköping university 25 of 28 Spring term 2006 F1 F2 q1 q2 Embedded systems simulation and verification Linköping university 26 of 28 Spring term 2006 Formal definition Let M1 = 〈Q1, Σ, ∆1, δ1, λ1, q01〉 and M2 = 〈Q2, ∆1, ∆2, δ2, λ2, q02〉 be Moore-machines. • The serial composition M of M1 and M2 = 〈Q, Σ, ∆, δ, λ, q0〉 is defined as: • Q = Q1 × Q2 • ∆ = ∆2 Embedded systems simulation and verification Linköping university • q0 = 〈q01,q02〉 • δ(〈q1,q2〉, i) = 〈 δ1(q1,i), δ2(q2, λ1(δ1(q1,i))) 〉 • λ(〈q1,q2〉) = λ2 (q2) Exercise: Define serial composition for Mealy and parallel composition for Moore machines! 27 of 28 Spring term 2006 Embedded systems simulation and verification Linköping university 28 of 28 Spring term 2006