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,
32 import Data.Sequence (Seq, empty, singleton, (<|), (|>), fromList, toList,
33 ViewL(..), ViewR(..), viewl, viewr)
38 -- | Multi-way trees, also known as /rose trees/.
40 rootLabel :: a, -- ^ label value
41 subForest :: Forest a -- ^ zero or more child trees
44 deriving (Eq, Read, Show)
45 #else /* __HADDOCK__ (which can't figure these out by itself) */
46 instance Eq a => Eq (Tree a)
47 instance Read a => Read (Tree a)
48 instance Show a => Show (Tree a)
50 type Forest a = [Tree a]
52 INSTANCE_TYPEABLE1(Tree,treeTc,"Tree")
54 instance Functor Tree where
57 mapTree :: (a -> b) -> (Tree a -> Tree b)
58 mapTree f (Node x ts) = Node (f x) (map (mapTree f) ts)
60 -- | Neat 2-dimensional drawing of a tree.
61 drawTree :: Tree String -> String
62 drawTree = unlines . draw
64 -- | Neat 2-dimensional drawing of a forest.
65 drawForest :: Forest String -> String
66 drawForest = unlines . map drawTree
68 draw :: Tree String -> [String]
69 draw (Node x ts0) = x : drawSubTrees ts0
70 where drawSubTrees [] = []
72 "|" : shift "`- " " " (draw t)
74 "|" : shift "+- " "| " (draw t) ++ drawSubTrees ts
76 shift first other = zipWith (++) (first : repeat other)
78 -- | The elements of a tree in pre-order.
79 flatten :: Tree a -> [a]
80 flatten t = squish t []
81 where squish (Node x ts) xs = x:foldr squish xs ts
83 -- | Lists of nodes at each level of the tree.
84 levels :: Tree a -> [[a]]
85 levels t = map (map rootLabel) $
86 takeWhile (not . null) $
87 iterate (concatMap subForest) [t]
89 -- | Build a tree from a seed value
90 unfoldTree :: (b -> (a, [b])) -> b -> Tree a
91 unfoldTree f b = let (a, bs) = f b in Node a (unfoldForest f bs)
93 -- | Build a forest from a list of seed values
94 unfoldForest :: (b -> (a, [b])) -> [b] -> Forest a
95 unfoldForest f = map (unfoldTree f)
97 -- | Monadic tree builder, in depth-first order
98 unfoldTreeM :: Monad m => (b -> m (a, [b])) -> b -> m (Tree a)
101 ts <- unfoldForestM f bs
104 -- | Monadic forest builder, in depth-first order
106 unfoldForestM :: Monad m => (b -> m (a, [b])) -> [b] -> m (Forest a)
108 unfoldForestM f = mapM (unfoldTreeM f)
110 -- | Monadic tree builder, in breadth-first order,
111 -- using an algorithm adapted from
112 -- /Breadth-First Numbering: Lessons from a Small Exercise in Algorithm Design/,
113 -- by Chris Okasaki, /ICFP'00/.
114 unfoldTreeM_BF :: Monad m => (b -> m (a, [b])) -> b -> m (Tree a)
115 unfoldTreeM_BF f b = liftM getElement $ unfoldForestQ f (singleton b)
116 where getElement xs = case viewl xs of
118 EmptyL -> error "unfoldTreeM_BF"
120 -- | Monadic forest builder, in breadth-first order,
121 -- using an algorithm adapted from
122 -- /Breadth-First Numbering: Lessons from a Small Exercise in Algorithm Design/,
123 -- by Chris Okasaki, /ICFP'00/.
124 unfoldForestM_BF :: Monad m => (b -> m (a, [b])) -> [b] -> m (Forest a)
125 unfoldForestM_BF f = liftM toList . unfoldForestQ f . fromList
127 -- takes a sequence (queue) of seeds
128 -- produces a sequence (reversed queue) of trees of the same length
129 unfoldForestQ :: Monad m => (b -> m (a, [b])) -> Seq b -> m (Seq (Tree a))
130 unfoldForestQ f aQ = case viewl aQ of
131 EmptyL -> return empty
134 tQ <- unfoldForestQ f (foldl (|>) aQ as)
135 let (tQ', ts) = splitOnto [] as tQ
136 return (Node b ts <| tQ')
137 where splitOnto :: [a'] -> [b'] -> Seq a' -> (Seq a', [a'])
138 splitOnto as [] q = (q, as)
139 splitOnto as (_:bs) q = case viewr q of
140 q' :> a -> splitOnto (a:as) bs q'
141 EmptyR -> error "unfoldForestQ"