This function generates simple .dot files for visualizing the transition functions of automata using Graphviz. The main goal is to debug large sets of automatically generated transitions (for example, inflections of Latin verbs).
prepGraph :: ( ... ) => NFA c b a -> [String]
prepGraph nfa = "digraph finite_state_machine {"
: wrapSp "rankdir = LR"
: wrapSp ("node [shape = circle]" ++ (mapSp (states nfa \\ terminal nfa)))
: wrapSp ("node [shape = doublecircle]" ++ (mapSp $ terminal nfa))
: formatGraph nfa ++ ["}"]
formatGraph :: ( ... ) => NFA c b a -> [String]
formatGraph = map formatDelta . deltaTuples
where formatDelta (a, a', bc) = wrapSp (mkArrow a a' ++ " " ++ mkLabel bc)
mkArrow x y = show x ++ " -> " ++ show y
mkLabel (y, z) = case z of
(Just t) -> "[ label = \"(" ++ show y ++ ", " ++ show t ++ ")\" ]"
Nothing -> "[ label = \"(" ++ show y ++ ", " ++ "Null" ++ ")\" ]"
where wrap, wrapSpand mapSpare formatting functions, like and deltaTuples.
The problem is that it formatGraphkeeps double quotes around the lines, which causes errors in Graphviz. For example, when I print unlines $ prepGraphto a file, I get things like:
0 -> 1 [ label = "('a', "N. SF")" ];
instead
0 -> 1 [ label = "('a', N. SF)" ];
( , "Null", , ). , , "N. SF" , , . , Haskell: show String, ?