IN WORK 6 March 2019
Browser Advisory: The HTML version of this textbook requires a browser that supports the display of MathML. A good choice as of March 2019 is a recent version of Firefox from Mozilla.
TODO:
Either
the appropriate return in the various cases?getTransitionsFrom
return an Either
for an invalid state?In this project, you are asked to design and implement a Scala program to represent Mealy Machines and to simulate their execution.
This kind of machine is a useful abstraction for simple controllers that listen for input events and respond by generating output events. For example in an automobile application, the input might be an event such as “fuel level low” and the output might be command to “display low-fuel warning message”.
In the theory of computation, a Mealy Machine is a finite-state automaton whose output values are determined both by its current state and the current input. It is a deterministic finite state transducer such that, for each state and input, at most one transition is possible.
Appendix A of the Linz textbook [Linz 2017] defines a Mealy Machine mathematically by a tuple
where
is a finite set of internal states
is the input alphabet (a finite set of values)
is the output alphabet (a finite set of values)
is the transition function
is the output function
is the initial state of (an element of )
In an alternative formulation, the transition and output functions can be combined into a single function:
We often find it useful to picture a finite state machine as a transition graph where the states are mapped to vertices and the transition function represented by directed edges between vertices labelled with the input and output symbols.
TODO: Decide whether these functions are appropriate for the intended Scala structure. In particular should these be methods on a class rather than just functions. Should the constructor be a class constructor? etc.
Specify, design, and implement a general representation for a Mealy Machine as a set of Scala definitions implementing an abstract data type. It should hide the representation of the machine and should have, at least, the following public operations.
newMachine(s)
creates a new machine with initial (and current) state s
and no transitions.
Note: This assumes that the state, input, and output sets are exactly those added with the mutator operations below. An alternative would be to change this function to take the allowed state, input, and output sets.
addState(m,s)
adds a new state s
to machine m
and returns an Either
wrapping the modified machine or an error message.
addTransition(m,s1,in,out,s2)
adds a new transition to machine m
and returns an Either
wrapping the modified machine or an error message. From state s1
with input in
the modified machine outputs out
and transitions to state s2
.
addResets(m)
adds all reset transitions to machine m
and returns the modified machine. From state s1
on input in
the modified machine outputs out
and transitions to state s2
. This operation makes the transition function a total function by adding any missing transitions from a state back to the initial state.
setCurrent(m,s)
sets the current state of machine m
to s
and returns an Either
wrapping the modified machine or an error message.
getCurrent(m)
returns the current state of machine m
.
getStates(m)
returns a list of the elements of the state set of machine m
.
getInputs(m)
returns a list of the input set of machine m
.
getOutputs(m)
returns a list of the output set of machine m
.
getTransitions(m)
returns a list of the transition set of machine m
. Tuple (s1,in,out,s2)
occurs in the returned list if and only if, from state s1
with input in
, the machine outputs out
and moves to state s2
.
getTransitionsFrom(m,s)
returns an Either
wrapping a list of the set of transitions enabled from state s
of machine m
or an error message.
Given the above implementation for a Mealy Machine, design and implement a separate Haskell module that simulates the execution of a Mealy Machine. It should have, at least, the following new public operations.
move(m,in)
moves machine m
from the current state given input in
and returns an Either
wrapping a tuple (mm,out)
or an error message. The tuple gives the modified machine mm
and the output out
.
simulate(m,ins(
simulates execution of machine m
from its current state through a sequence of moves for the inputs in list ins
and returns an Either
wrapping a tuple (mm,outs)
or an error message. The tuple gives the modified machine mm
after the sequence of moves and the output list outs
.
Note: It is possible to use a Labelled Digraph ADT module in the implementation of the Mealy Machine.
Implement a Scala abstract data type that uses a different representation for the Mealy Machine. Make sure the simulator module still works correctly.
I created the original version of this project in Spring 2016 as a programming exercise in the first Scala-based offering of CSci 555, Functional Programming. I adapted and revised this project for possible use in the Haskell-based CSci 556 course in Spring 2017 but did not assign it. In 2018, I revised the project description and merged it into new Chapter 23, Data Abstraction Revisited, in the textbook Exploring Languages with Interpreters and Functional Programming.
In Spring 2019, I separated the Mealy Machine Simulator Project back into a separate document and modified it for use in a Scala-based course.
I maintain this chapter as text in Pandoc’s dialect of Markdown using embedded LaTeX markup for the mathematical formulas and then translate the document to HTML, PDF, and other forms as needed.
Mealy Machine, simulator, finite-state automaton (machine), deterministic finite state transducer, state, transition, transition graph.