{-# LANGUAGE CPP #-}
{-# LANGUAGE DeriveDataTypeable #-}
module Data.Tree.NTree.TypeDefs
where
import Control.DeepSeq (NFData (..))
import Control.FlatSeq (WNFData (..), rlnf)
import Data.Binary
import Data.Tree.Class (Tree (..))
import Data.Typeable (Typeable)
#if MIN_VERSION_base(4,13,0)
#else
import Data.Monoid ((<>))
#endif
#if MIN_VERSION_base(4,8,2)
#else
import Control.Applicative ((<$>))
#endif
#if MIN_VERSION_base(4,8,0)
#else
import Control.Applicative (Applicative (..))
import Data.Foldable (Foldable (..))
import Data.Monoid (Monoid (..))
import Data.Traversable (Traversable (..), sequenceA)
#endif
data NTree a = NTree a (NTrees a)
deriving
(NTree a -> NTree a -> Bool
forall a. Eq a => NTree a -> NTree a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: NTree a -> NTree a -> Bool
$c/= :: forall a. Eq a => NTree a -> NTree a -> Bool
== :: NTree a -> NTree a -> Bool
$c== :: forall a. Eq a => NTree a -> NTree a -> Bool
Eq, NTree a -> NTree a -> Bool
NTree a -> NTree a -> Ordering
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
forall {a}. Ord a => Eq (NTree a)
forall a. Ord a => NTree a -> NTree a -> Bool
forall a. Ord a => NTree a -> NTree a -> Ordering
forall a. Ord a => NTree a -> NTree a -> NTree a
min :: NTree a -> NTree a -> NTree a
$cmin :: forall a. Ord a => NTree a -> NTree a -> NTree a
max :: NTree a -> NTree a -> NTree a
$cmax :: forall a. Ord a => NTree a -> NTree a -> NTree a
>= :: NTree a -> NTree a -> Bool
$c>= :: forall a. Ord a => NTree a -> NTree a -> Bool
> :: NTree a -> NTree a -> Bool
$c> :: forall a. Ord a => NTree a -> NTree a -> Bool
<= :: NTree a -> NTree a -> Bool
$c<= :: forall a. Ord a => NTree a -> NTree a -> Bool
< :: NTree a -> NTree a -> Bool
$c< :: forall a. Ord a => NTree a -> NTree a -> Bool
compare :: NTree a -> NTree a -> Ordering
$ccompare :: forall a. Ord a => NTree a -> NTree a -> Ordering
Ord, Int -> NTree a -> ShowS
forall a. Show a => Int -> NTree a -> ShowS
forall a. Show a => [NTree a] -> ShowS
forall a. Show a => NTree a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [NTree a] -> ShowS
$cshowList :: forall a. Show a => [NTree a] -> ShowS
show :: NTree a -> String
$cshow :: forall a. Show a => NTree a -> String
showsPrec :: Int -> NTree a -> ShowS
$cshowsPrec :: forall a. Show a => Int -> NTree a -> ShowS
Show, ReadPrec [NTree a]
ReadPrec (NTree a)
ReadS [NTree a]
forall a. Read a => ReadPrec [NTree a]
forall a. Read a => ReadPrec (NTree a)
forall a. Read a => Int -> ReadS (NTree a)
forall a. Read a => ReadS [NTree a]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [NTree a]
$creadListPrec :: forall a. Read a => ReadPrec [NTree a]
readPrec :: ReadPrec (NTree a)
$creadPrec :: forall a. Read a => ReadPrec (NTree a)
readList :: ReadS [NTree a]
$creadList :: forall a. Read a => ReadS [NTree a]
readsPrec :: Int -> ReadS (NTree a)
$creadsPrec :: forall a. Read a => Int -> ReadS (NTree a)
Read, Typeable)
type NTrees a = [NTree a]
instance (NFData a) => NFData (NTree a) where
rnf :: NTree a -> ()
rnf (NTree a
n NTrees a
cl) = forall a. NFData a => a -> ()
rnf a
n seq :: forall a b. a -> b -> b
`seq` forall a. NFData a => a -> ()
rnf NTrees a
cl
{-# INLINE rnf #-}
instance (WNFData a) => WNFData (NTree a) where
rwnf :: NTree a -> ()
rwnf (NTree a
n NTrees a
cl) = forall a. WNFData a => a -> ()
rwnf a
n seq :: forall a b. a -> b -> b
`seq` forall a. WNFData a => a -> ()
rwnf NTrees a
cl
{-# INLINE rwnf #-}
rwnf2 :: NTree a -> ()
rwnf2 (NTree a
n NTrees a
cl) = forall a. WNFData a => a -> ()
rwnf a
n seq :: forall a b. a -> b -> b
`seq` forall a. (a -> ()) -> [a] -> ()
rlnf forall a. WNFData a => a -> ()
rwnf NTrees a
cl
{-# INLINE rwnf2 #-}
instance (Binary a) => Binary (NTree a) where
put :: NTree a -> Put
put (NTree a
n [NTree a]
cs) = forall t. Binary t => t -> Put
put a
n forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall t. Binary t => t -> Put
put [NTree a]
cs
get :: Get (NTree a)
get = do
a
n <- forall t. Binary t => Get t
get
[NTree a]
cs <- forall t. Binary t => Get t
get
forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. a -> NTrees a -> NTree a
NTree a
n [NTree a]
cs)
instance Functor NTree where
fmap :: forall a b. (a -> b) -> NTree a -> NTree b
fmap a -> b
f (NTree a
n NTrees a
cl) = forall a. a -> NTrees a -> NTree a
NTree (a -> b
f a
n) (forall a b. (a -> b) -> [a] -> [b]
map (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f) NTrees a
cl)
{-# INLINE fmap #-}
instance Foldable NTree where
foldMap :: forall m a. Monoid m => (a -> m) -> NTree a -> m
foldMap a -> m
f (NTree a
n NTrees a
cl) = a -> m
f a
n forall a. Semigroup a => a -> a -> a
<> forall a. Monoid a => [a] -> a
mconcat (forall a b. (a -> b) -> [a] -> [b]
map (forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap a -> m
f) NTrees a
cl)
{-# INLINE foldMap #-}
instance Traversable NTree where
traverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> NTree a -> f (NTree b)
traverse a -> f b
f (NTree a
n NTrees a
cl) = forall a. a -> NTrees a -> NTree a
NTree forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> f b
f a
n forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall (t :: * -> *) (f :: * -> *) a.
(Traversable t, Applicative f) =>
t (f a) -> f (t a)
sequenceA (forall a b. (a -> b) -> [a] -> [b]
map (forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse a -> f b
f) NTrees a
cl)
{-# INLINE traverse #-}
instance Tree NTree where
mkTree :: forall a. a -> NTrees a -> NTree a
mkTree a
n [NTree a]
cl = forall a. a -> NTrees a -> NTree a
NTree a
n [NTree a]
cl
{-# INLINE mkTree #-}
getNode :: forall a. NTree a -> a
getNode ~(NTree a
n NTrees a
_ ) = a
n
{-# INLINE getNode #-}
getChildren :: forall a. NTree a -> [NTree a]
getChildren ~(NTree a
_ NTrees a
cl) = NTrees a
cl
{-# INLINE getChildren #-}
changeNode :: forall a. (a -> a) -> NTree a -> NTree a
changeNode a -> a
cf ~(NTree a
n NTrees a
cl) = forall a. a -> NTrees a -> NTree a
NTree (a -> a
cf a
n) NTrees a
cl
{-# INLINE changeNode #-}
changeChildren :: forall a. ([NTree a] -> [NTree a]) -> NTree a -> NTree a
changeChildren [NTree a] -> [NTree a]
cf ~(NTree a
n [NTree a]
cl) = forall a. a -> NTrees a -> NTree a
NTree a
n ([NTree a] -> [NTree a]
cf [NTree a]
cl)
{-# INLINE changeChildren #-}
foldTree :: forall a b. (a -> [b] -> b) -> NTree a -> b
foldTree a -> [b] -> b
f ~(NTree a
n NTrees a
cs) = a -> [b] -> b
f a
n (forall a b. (a -> b) -> [a] -> [b]
map (forall (t :: * -> *) a b. Tree t => (a -> [b] -> b) -> t a -> b
foldTree a -> [b] -> b
f) NTrees a
cs)
{-# INLINE foldTree #-}