Commits

Emil Axelsson committed ee4458d

Added class Render which gives methods for converting terms to Data.Tree and drawing terms as ASCII art

Comments (0)

Files changed (2)

                         Data.Comp.TermRewriting,
                         Data.Comp.Arbitrary,
                         Data.Comp.Show,
+                        Data.Comp.Render,
                         Data.Comp.Variables,
                         Data.Comp.Decompose,
                         Data.Comp.Unification,
                         Data.Comp.MultiParam.Derive.Injections,
                         Data.Comp.MultiParam.Derive.Projections
 
-  Build-Depends:	base == 4.*, template-haskell, containers, mtl, QuickCheck >= 2, derive, deepseq, th-expand-syns, transformers
+  Build-Depends:	base == 4.*, template-haskell, containers, mtl, QuickCheck >= 2, derive, deepseq, th-expand-syns, transformers, tree-view
   hs-source-dirs:	src
   ghc-options:          -W
 
 
 source-repository head
   type:     hg
-  location: https://bitbucket.org/paba/compdata
+  location: https://bitbucket.org/paba/compdata

src/Data/Comp/Render.hs

+module Data.Comp.Render where
+
+import Data.Foldable (toList)
+import Data.Tree (Tree (..))
+import Data.Tree.View
+import Data.Comp
+import Data.Comp.Derive
+
+-- | The 'stringTree' algebra of a functor. The default instance creates a tree
+-- with the same structure as the term.
+class (Functor f, Foldable f, ShowConstr f) => Render f where
+    stringTreeAlg :: Alg f (Tree String)
+    stringTreeAlg f = Node (showConstr f) $ toList f
+
+-- | Convert a term to a 'Tree'
+stringTree :: Render f => Term f -> Tree String
+stringTree = cata stringTreeAlg
+
+-- | Show a term using ASCII art
+showTerm :: Render f => Term f -> String
+showTerm = showTree . stringTree
+
+-- | Print a term using ASCII art
+drawTerm :: Render f => Term f -> IO ()
+drawTerm = putStrLn . showTerm