1 -----------------------------------------------------------------------------
4 -- Copyright : (c) The University of Glasgow 2002
5 -- License : BSD-style (see the file libraries/base/LICENSE)
7 -- Maintainer : libraries@haskell.org
8 -- Stability : experimental
9 -- Portability : portable
11 -- Multi-way trees (/aka/ rose trees) and forests.
13 -----------------------------------------------------------------------------
17 -- * Two-dimensional drawing
22 unfoldTree, unfoldForest,
23 unfoldTreeM, unfoldForestM,
24 unfoldTreeM_BF, unfoldForestM_BF,
31 import Control.Applicative (Applicative(..), (<$>))
33 import Data.Monoid (Monoid(..))
34 import Data.Sequence (Seq, empty, singleton, (<|), (|>), fromList,
35 ViewL(..), ViewR(..), viewl, viewr)
36 import Data.Foldable (Foldable(foldMap), toList)
37 import Data.Traversable (Traversable(traverse))
40 #ifdef __GLASGOW_HASKELL__
41 import Data.Generics.Basics (Data)
44 -- | Multi-way trees, also known as /rose trees/.
46 rootLabel :: a, -- ^ label value
47 subForest :: Forest a -- ^ zero or more child trees
50 # ifdef __GLASGOW_HASKELL__
51 deriving (Eq, Read, Show, Data)
53 deriving (Eq, Read, Show)
55 #else /* __HADDOCK__ (which can't figure these out by itself) */
56 instance Eq a => Eq (Tree a)
57 instance Read a => Read (Tree a)
58 instance Show a => Show (Tree a)
59 instance Data a => Data (Tree a)
61 type Forest a = [Tree a]
64 INSTANCE_TYPEABLE1(Tree,treeTc,"Tree")
66 instance Functor Tree where
67 fmap f (Node x ts) = Node (f x) (map (fmap f) ts)
69 instance Traversable Tree where
70 traverse f (Node x ts) = Node <$> f x <*> traverse (traverse f) ts
72 instance Foldable Tree where
73 foldMap f (Node x ts) = f x `mappend` foldMap (foldMap f) ts
75 -- | Neat 2-dimensional drawing of a tree.
76 drawTree :: Tree String -> String
77 drawTree = unlines . draw
79 -- | Neat 2-dimensional drawing of a forest.
80 drawForest :: Forest String -> String
81 drawForest = unlines . map drawTree
83 draw :: Tree String -> [String]
84 draw (Node x ts0) = x : drawSubTrees ts0
85 where drawSubTrees [] = []
87 "|" : shift "`- " " " (draw t)
89 "|" : shift "+- " "| " (draw t) ++ drawSubTrees ts
91 shift first other = zipWith (++) (first : repeat other)
93 -- | The elements of a tree in pre-order.
94 flatten :: Tree a -> [a]
95 flatten t = squish t []
96 where squish (Node x ts) xs = x:Prelude.foldr squish xs ts
98 -- | Lists of nodes at each level of the tree.
99 levels :: Tree a -> [[a]]
100 levels t = map (map rootLabel) $
101 takeWhile (not . null) $
102 iterate (concatMap subForest) [t]
104 -- | Build a tree from a seed value
105 unfoldTree :: (b -> (a, [b])) -> b -> Tree a
106 unfoldTree f b = let (a, bs) = f b in Node a (unfoldForest f bs)
108 -- | Build a forest from a list of seed values
109 unfoldForest :: (b -> (a, [b])) -> [b] -> Forest a
110 unfoldForest f = map (unfoldTree f)
112 -- | Monadic tree builder, in depth-first order
113 unfoldTreeM :: Monad m => (b -> m (a, [b])) -> b -> m (Tree a)
116 ts <- unfoldForestM f bs
119 -- | Monadic forest builder, in depth-first order
121 unfoldForestM :: Monad m => (b -> m (a, [b])) -> [b] -> m (Forest a)
123 unfoldForestM f = Prelude.mapM (unfoldTreeM f)
125 -- | Monadic tree builder, in breadth-first order,
126 -- using an algorithm adapted from
127 -- /Breadth-First Numbering: Lessons from a Small Exercise in Algorithm Design/,
128 -- by Chris Okasaki, /ICFP'00/.
129 unfoldTreeM_BF :: Monad m => (b -> m (a, [b])) -> b -> m (Tree a)
130 unfoldTreeM_BF f b = liftM getElement $ unfoldForestQ f (singleton b)
131 where getElement xs = case viewl xs of
133 EmptyL -> error "unfoldTreeM_BF"
135 -- | Monadic forest builder, in breadth-first order,
136 -- using an algorithm adapted from
137 -- /Breadth-First Numbering: Lessons from a Small Exercise in Algorithm Design/,
138 -- by Chris Okasaki, /ICFP'00/.
139 unfoldForestM_BF :: Monad m => (b -> m (a, [b])) -> [b] -> m (Forest a)
140 unfoldForestM_BF f = liftM toList . unfoldForestQ f . fromList
142 -- takes a sequence (queue) of seeds
143 -- produces a sequence (reversed queue) of trees of the same length
144 unfoldForestQ :: Monad m => (b -> m (a, [b])) -> Seq b -> m (Seq (Tree a))
145 unfoldForestQ f aQ = case viewl aQ of
146 EmptyL -> return empty
149 tQ <- unfoldForestQ f (Prelude.foldl (|>) aQ as)
150 let (tQ', ts) = splitOnto [] as tQ
151 return (Node b ts <| tQ')
152 where splitOnto :: [a'] -> [b'] -> Seq a' -> (Seq a', [a'])
153 splitOnto as [] q = (q, as)
154 splitOnto as (_:bs) q = case viewr q of
155 q' :> a -> splitOnto (a:as) bs q'
156 EmptyR -> error "unfoldForestQ"