Haskell data structure generating diagram

I am looking for a tool that, given a file with several data structures in Haskell, is able to generate a diagram with the relationships between data structures.

Currently, I have a file with a parse tree (+ - 600 lines), and I would like to see the parse tree more visually. What are my options?

Thanks in advance.

+5
source share
2 answers

One option is to use the diagrams library, which has many backends. The diagrams-contrib package includes helper functions for rendering trees . Therefore, perhaps you can convert the parse tree to a pink tree from Data.Tree and make it that way.

The following example uses an SVG server:

 module Treeish where -- This example requires the containers, -- diagrams-core, diagrams-lib, diagrams-contrib and diagrams-svg packages import Data.Tree import Diagrams.Prelude import Diagrams.TwoD.Layout.Tree (renderTree,symmLayout',_slHSep,_slVSep) import Diagrams.Backend.SVG (SVG) import Diagrams.Backend.SVG.CmdLine (defaultMain) exampleTree :: Tree String exampleTree = Node "A" [Node "B" [], Node "C" []] renderNodeTree :: Tree String -> QDiagram SVG V2 Double Any renderNodeTree nodeTree = renderTree (\a -> letter a `atop` square 1.03 # fc white) (~~) (symmLayout' (with{ _slHSep = 3, _slVSep = 2}) nodeTree) where letter a = text a # font "monospace" # fontSize (local 0.47) main :: IO () main = defaultMain (renderNodeTree exampleTree) 

renderTree is a function that, when renderTree a function that creates a diagram for a node tree, and a function that creates a line between two given points, returns a function that creates a diagram from a tree that was annotated with node positions.

Position annotations are added using the symmLayout' function.

with is simply a synonym for default from Data.Default .

(~~) creates a line between two points.

When the program starts from the command line (with something like runhaskell Treeish -o foo.svg -w 300 ), it will generate an SVG file that can be viewed in a browser:

enter image description here

Here and here are two parts of a recent tutorial on diagrams .

+5
source

If I understand you correctly, you want to take an AST from some Haskell code and visualize it. How about the first analysis of it with https://hackage.haskell.org/package/haskell-src , and then generating the output using https://hackage.haskell.org/package/graphviz .

If this is not the AST you want to visualize, simply map your data structure to GraphViz output and ignore the Parser.Haskell part.

In any case, I would suggest that your own visualization code would be the most flexible, and given Haskellโ€™s expressiveness, perhaps not too time-consuming.

+4
source

All Articles