X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=Data%2FTree.hs;h=a7aab07c14265ab53d0572f6d45f014f9e6d0e0f;hb=53004f977617674ddec5d7315f0be92e0358ae7d;hp=d04fe3d92ca259471553cb2bc80e9b81f51bb8e3;hpb=88f0491c26432f4decf7cad2b3ad29dba1d5f5a6;p=haskell-directory.git diff --git a/Data/Tree.hs b/Data/Tree.hs index d04fe3d..a7aab07 100644 --- a/Data/Tree.hs +++ b/Data/Tree.hs @@ -20,9 +20,7 @@ module Data.Tree( flatten, levels, -- * Building trees unfoldTree, unfoldForest, -#ifndef __NHC__ unfoldTreeM, unfoldForestM, -#endif unfoldTreeM_BF, unfoldForestM_BF, ) where @@ -30,9 +28,18 @@ module Data.Tree( import Prelude #endif +import Control.Applicative (Applicative(..), (<$>)) import Control.Monad -import Data.Maybe -import Data.Queue +import Data.Monoid (Monoid(..)) +import Data.Sequence (Seq, empty, singleton, (<|), (|>), fromList, + ViewL(..), ViewR(..), viewl, viewr) +import Data.Foldable (Foldable(foldMap), toList) +import Data.Traversable (Traversable(traverse)) +import Data.Typeable + +#ifdef __GLASGOW_HASKELL__ +import Data.Generics.Basics (Data) +#endif -- | Multi-way trees, also known as /rose trees/. data Tree a = Node { @@ -40,19 +47,30 @@ data Tree a = Node { subForest :: Forest a -- ^ zero or more child trees } #ifndef __HADDOCK__ +# ifdef __GLASGOW_HASKELL__ + deriving (Eq, Read, Show, Data) +# else deriving (Eq, Read, Show) +# endif #else /* __HADDOCK__ (which can't figure these out by itself) */ instance Eq a => Eq (Tree a) instance Read a => Read (Tree a) instance Show a => Show (Tree a) +instance Data a => Data (Tree a) #endif type Forest a = [Tree a] +#include "Typeable.h" +INSTANCE_TYPEABLE1(Tree,treeTc,"Tree") + instance Functor Tree where - fmap = mapTree + fmap f (Node x ts) = Node (f x) (map (fmap f) ts) -mapTree :: (a -> b) -> (Tree a -> Tree b) -mapTree f (Node x ts) = Node (f x) (map (mapTree f) ts) +instance Traversable Tree where + traverse f (Node x ts) = Node <$> f x <*> traverse (traverse f) ts + +instance Foldable Tree where + foldMap f (Node x ts) = f x `mappend` foldMap (foldMap f) ts -- | Neat 2-dimensional drawing of a tree. drawTree :: Tree String -> String @@ -75,7 +93,7 @@ draw (Node x ts0) = x : drawSubTrees ts0 -- | The elements of a tree in pre-order. flatten :: Tree a -> [a] flatten t = squish t [] - where squish (Node x ts) xs = x:foldr squish xs ts + where squish (Node x ts) xs = x:Prelude.foldr squish xs ts -- | Lists of nodes at each level of the tree. levels :: Tree a -> [[a]] @@ -91,7 +109,6 @@ unfoldTree f b = let (a, bs) = f b in Node a (unfoldForest f bs) unfoldForest :: (b -> (a, [b])) -> [b] -> Forest a unfoldForest f = map (unfoldTree f) -#ifndef __NHC__ -- | Monadic tree builder, in depth-first order unfoldTreeM :: Monad m => (b -> m (a, [b])) -> b -> m (Tree a) unfoldTreeM f b = do @@ -100,40 +117,40 @@ unfoldTreeM f b = do return (Node a ts) -- | Monadic forest builder, in depth-first order +#ifndef __NHC__ unfoldForestM :: Monad m => (b -> m (a, [b])) -> [b] -> m (Forest a) -unfoldForestM f = mapM (unfoldTreeM f) #endif +unfoldForestM f = Prelude.mapM (unfoldTreeM f) -- | Monadic tree builder, in breadth-first order, -- using an algorithm adapted from --- /Breadth­First Numbering: Lessons from a Small Exercise in Algorithm Design/, +-- /Breadth-First Numbering: Lessons from a Small Exercise in Algorithm Design/, -- by Chris Okasaki, /ICFP'00/. unfoldTreeM_BF :: Monad m => (b -> m (a, [b])) -> b -> m (Tree a) -unfoldTreeM_BF f b = liftM (fst . fromJust . deQueue) $ - unfoldForestQ f (listToQueue [b]) +unfoldTreeM_BF f b = liftM getElement $ unfoldForestQ f (singleton b) + where getElement xs = case viewl xs of + x :< _ -> x + EmptyL -> error "unfoldTreeM_BF" -- | Monadic forest builder, in breadth-first order, -- using an algorithm adapted from --- /Breadth­First Numbering: Lessons from a Small Exercise in Algorithm Design/, +-- /Breadth-First Numbering: Lessons from a Small Exercise in Algorithm Design/, -- by Chris Okasaki, /ICFP'00/. unfoldForestM_BF :: Monad m => (b -> m (a, [b])) -> [b] -> m (Forest a) -unfoldForestM_BF f = liftM (reverseOnto []) . unfoldForestQ f . listToQueue - where reverseOnto :: [a] -> Queue a -> [a] - reverseOnto as q = case deQueue q of - Nothing -> as - Just (a, q') -> reverseOnto (a:as) q' - --- takes a queue of seeds --- produces a queue of trees of the same length, but in the reverse order -unfoldForestQ :: Monad m => (b -> m (a, [b])) -> Queue b -> m (Queue (Tree a)) -unfoldForestQ f aQ = case deQueue aQ of - Nothing -> return emptyQueue - Just (a, aQ) -> do +unfoldForestM_BF f = liftM toList . unfoldForestQ f . fromList + +-- takes a sequence (queue) of seeds +-- produces a sequence (reversed queue) of trees of the same length +unfoldForestQ :: Monad m => (b -> m (a, [b])) -> Seq b -> m (Seq (Tree a)) +unfoldForestQ f aQ = case viewl aQ of + EmptyL -> return empty + a :< aQ -> do (b, as) <- f a - tQ <- unfoldForestQ f (foldl addToQueue aQ as) - let (ts, tQ') = splitOnto [] as tQ - return (addToQueue tQ' (Node b ts)) - where splitOnto :: [a] -> [b] -> Queue a -> ([a], Queue a) - splitOnto as [] q = (as, q) - splitOnto as (_:bs) q = case fromJust (deQueue q) of - (a, q') -> splitOnto (a:as) bs q' + tQ <- unfoldForestQ f (Prelude.foldl (|>) aQ as) + let (tQ', ts) = splitOnto [] as tQ + return (Node b ts <| tQ') + where splitOnto :: [a'] -> [b'] -> Seq a' -> (Seq a', [a']) + splitOnto as [] q = (q, as) + splitOnto as (_:bs) q = case viewr q of + q' :> a -> splitOnto (a:as) bs q' + EmptyR -> error "unfoldForestQ"