Portability | portable |
---|---|

Stability | experimental |

Maintainer | bgwines@cs.stanford.edu |

Safe Haskell | None |

A typeclass with default implementation for graphing trees with Haskell GraphViz. It is intended to be extremely straightforward to graph your data type; you only need to define three very simple functions (example implementations below).

# Documentation

class TreeGraphable g whereSource

A typeclass for algebraic data types that are able to be graphed.

For these descriptions, assume the following example data type:

data Tree a = Empty | Leaf a | Node a (Tree a) (Tree a)

Being an instance of `TreeGraphable`

is enough to make your data type an instance of `Zoldable`

; just define

instance Zoldable Tree where zoldMap :: (Monoid m) => (Tree a -> m) -> Tree a -> m zoldMap = G.zoldMap

Gets the value contained in a node. For example,

value (Empty) = error "Empty nodes don't contain values." value (Leaf x) = x value (Node x _ _) = x

get_children :: g a -> [g a]Source

Gets the children of the current node. For example,

get_children (Empty) = error "Empty nodes don't have children." get_children (Leaf _) = [] get_children (Node _ l r) = [l, r]

Returns whether a node is empty. Sometimes, when declaring algebraic data types, it is desirable to have an "Empty" value constructor. If your data type does not have an "Empty" value constructor, just always return `False`

.

is_empty Empty = True is_empty _ = False

zoldMap :: Monoid m => (g a -> m) -> g a -> mSource

A default implementation of `zoldMap`

. This enough to make you an instance of `Zoldable`

(in `Zora.Types`

). You shouldn't need to override this implementation.

graph :: (Show a, Ord a) => g a -> IO StringSource

Graphs the given `TreeGraphable`

data type. Creates and writes to a file named "graph.png", overwriting any existing files with that name. You won't need to override this implementation.