make Control.Monad.Instances compilable by nhc98
[haskell-directory.git] / Data / Tree.hs
index 5a30470..f2a55f5 100644 (file)
@@ -28,12 +28,18 @@ module Data.Tree(
 import Prelude
 #endif
 
+import Control.Applicative (Applicative(..), (<$>))
 import Control.Monad
-import Data.Sequence (Seq, empty, singleton, (<|), (|>), fromList, toList,
+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
 
-#include "Typeable.h"
+#ifdef __GLASGOW_HASKELL__
+import Data.Generics.Basics (Data)
+#endif
 
 -- | Multi-way trees, also known as /rose trees/.
 data Tree a   = Node {
@@ -41,21 +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)
+
+instance Traversable Tree where
+  traverse f (Node x ts) = Node <$> f x <*> traverse (traverse f) ts
 
-mapTree              :: (a -> b) -> (Tree a -> Tree b)
-mapTree f (Node x ts) = Node (f x) (map (mapTree 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