Automata Visualiser is a Haskell eDSL for creating images of various types of
automata. There is built in support for (D/N)FAs, PDAs, and Turing Machines,
with easy extensibililty available to add your own automata types via the
TransitionLabel type class.
Automata can be output to both an image format (SVG) and a
LaTeX format (TikZ). Alternate output formats can be implemented by writing a
function f :: AutomatonConfig -> AutomatonLayout s t -> AutomatonRender.
Automata Visualiser is available to use in an online playground at https://automata.codingcactus.codes
The package isn't available on Hackage currently, but you can install it using
the GitHub link in the cabal.project file:
packages: .
source-repository-package
type: git
location: https://github.com/Coding-Cactus/automata-visualiser
allow-newer: latex-svg-image:*And can then be included as a dependency in your .cabal file as automata-visualiser.
Automata are defined with the AutomatonBuilder monad. You need to also specify
the types used for the state and transition labels. State labels must implement
the Label type class, and transition labels must implement the
TransitionLabel type class. These have been implemented for most common types.
For example, for an automaton with String state labels and Int transition labels, use
AutomatonBuilder String Int
To create new states, simply use the state function, passing the desired label as the
only parameter. To declare states as the initial state or as final states, use
the initial and final functions respectively.
To create transitions between states, an arrow syntax is used of the form
state >--[transition label]--> state. Multiple labels can be placed within the
square brackets separated by commas to attach multiple labels to a single
transition arrow in the produced diagram.
automaton :: AutomatonBuilder String Int
automaton = do
a <- state "a"
b <- state "b"
initial a
final b
a >--[0]--> b
b >--[0,1]--> b- PDA transition labels can be created with the
(~~)operator. For example:0 ~~ ('a', 'b')represents the transition of seeing a0and popping'a'off the stack and pushing'b'onto the stack. The type of this transition label isStackTransition Int Char. - Turing Machine labels can be created with the
TmTconstructor. For exampleTmT 0 1 Rrepresents the transition of seeing a0on the tape, writing a1and then moving the head to the right. The type of this transition label isTuringMachineTransition Int.
Examples of these can be seen on https://automata.codingcactus.codes
Automata Visualiser attempts to automatically position the states in an aesthetically pleasing way. However, if you wish to alter the layout manually, you can do so with the following functions:
state1 `above` state2positionsstate1directly abovestate2.- Using the same syntax, the functions
leftOf,rightOf, andbelowcan also be used - For complete control over state positioning, the
positionfunction can be used. This function takes: two states, an angle (in degrees), and a distance at which the states should be positioned. For exampleposition state1 state2 45 2will positionstate2at and angle of 45 degress fromstate1, at a distance of 2 units.
The tr and tr' functions can be used as shorter altnertives to the arrow
transition syntax. Recreating the above example using these functions looks can
be done as follows:
automaton :: AutomatonBuilder String Int
automaton = do
a <- state "a"
b <- state "b"
initial a
final b
tr' a b 0
tr a b [0,1]There are a few configuration options available to tweak the appearance of the
diagrams. These can be set with the AutomatonConfig type. A default
configuration is available as defaultConfig.
config :: AutomatonConfig
config = setConfig {
acceptanceStyle = ..., -- DoubleCircle or Arrow
svgStateRadius = ..., -- Alter the state radius in SVG outputs by a multiplicative constant
svgLoopRadius = ..., -- Alter the self loop radius in SVG output by a multiplicative constant
svgLoopSepAngle = ..., -- Alter the endpoint separation angle of self loop in SVG output by a multiplicative constant
tikzLoopWidth = ... -- Alter the width of self loops in TikZ output by a multiplicative constant
}
To finally generate the visual representation of your automata, the render
function can be used.
main :: IO ()
main = do
render config fileName tikz automaton -- output to TikZ format
render config fileName svg automaton -- output to SVG formatAutomata Visualiser supports LaTeX maths mode syntax in both state and transition labels. For SVG output, your local LaTeX installation is used if available, and for TikZ output: LaTeX syntax is trivially available.
If you don't have LaTeX installed locally, the online playground can be used.