Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generating diagram of Haskell data structures

Tags:

graph

haskell

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

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

Thanks in advance.

like image 530
Schiavini Avatar asked May 29 '15 14:05

Schiavini


1 Answers

One option is to use the diagrams library, which has a variety of backends. The diagrams-contrib package includes auxiliary functions for rendering trees. So perhaps you could convert your parse tree into a rose tree from Data.Tree and render it in this way.

The following example uses the SVG backend:

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, given a function that creates a diagram for a tree node, and a function that creates a line between two given points, returns a function that creates a diagram out of a tree that has been annotated with node positions.

The position annotations are added using the symmLayout' function.

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

(~~) creates a line between two points.

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

enter image description here

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

like image 163
danidiaz Avatar answered Oct 04 '22 20:10

danidiaz