module Data.Tree.Class
( module Data.Tree.Class )
where
class Tree t where
mkTree :: a -> [t a] -> t a
mkLeaf :: a -> t a
mkLeaf a
n = forall (t :: * -> *) a. Tree t => a -> [t a] -> t a
mkTree a
n []
{-# INLINE mkLeaf #-}
isLeaf :: t a -> Bool
isLeaf = forall (t :: * -> *) a. Foldable t => t a -> Bool
null forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) a. Tree t => t a -> [t a]
getChildren
{-# INLINE isLeaf #-}
isInner :: t a -> Bool
isInner = Bool -> Bool
not forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) a. Tree t => t a -> Bool
isLeaf
{-# INLINE isInner #-}
getNode :: t a -> a
getChildren :: t a -> [t a]
changeNode :: (a -> a) -> t a -> t a
changeChildren :: ([t a] -> [t a]) -> t a -> t a
setNode :: a -> t a -> t a
setNode a
n = forall (t :: * -> *) a. Tree t => (a -> a) -> t a -> t a
changeNode (forall a b. a -> b -> a
const a
n)
{-# INLINE setNode #-}
setChildren :: [t a] -> t a -> t a
setChildren [t a]
cl = forall (t :: * -> *) a. Tree t => ([t a] -> [t a]) -> t a -> t a
changeChildren (forall a b. a -> b -> a
const [t a]
cl)
{-# INLINE setChildren #-}
foldTree :: (a -> [b] -> b) -> t a -> b
nodesTree :: t a -> [a]
nodesTree = forall (t :: * -> *) a b. Tree t => (a -> [b] -> b) -> t a -> b
foldTree (\ a
n [[a]]
rs -> a
n forall a. a -> [a] -> [a]
: forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat [[a]]
rs)
{-# INLINE nodesTree #-}
depthTree :: t a -> Int
depthTree = forall (t :: * -> *) a b. Tree t => (a -> [b] -> b) -> t a -> b
foldTree (\ a
_ [Int]
rs -> Int
1 forall a. Num a => a -> a -> a
+ forall (t :: * -> *) a. (Foldable t, Ord a) => t a -> a
maximum (Int
0 forall a. a -> [a] -> [a]
: [Int]
rs))
cardTree :: t a -> Int
cardTree = forall (t :: * -> *) a b. Tree t => (a -> [b] -> b) -> t a -> b
foldTree (\ a
_ [Int]
rs -> Int
1 forall a. Num a => a -> a -> a
+ forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum [Int]
rs)
formatTree :: (a -> String) -> t a -> String
formatTree a -> String
nf t a
n = forall (t :: * -> *) a.
Tree t =>
(a -> String)
-> (String -> String)
-> (String -> String)
-> t a
-> String
-> String
formatNTree' a -> String
nf (String -> String -> String
showString String
"---") (String -> String -> String
showString String
" ") t a
n String
""
formatNTree' :: Tree t => (a -> String) -> (String -> String) -> (String -> String) -> t a -> String -> String
formatNTree' :: forall (t :: * -> *) a.
Tree t =>
(a -> String)
-> (String -> String)
-> (String -> String)
-> t a
-> String
-> String
formatNTree' a -> String
node2String String -> String
pf1 String -> String
pf2 t a
tree
= String -> String
formatNode
forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall {t :: * -> *}.
Tree t =>
(String -> String) -> [t a] -> String -> String
formatChildren String -> String
pf2 [t a]
l
where
n :: a
n = forall (t :: * -> *) a. Tree t => t a -> a
getNode t a
tree
l :: [t a]
l = forall (t :: * -> *) a. Tree t => t a -> [t a]
getChildren t a
tree
formatNode :: String -> String
formatNode = String -> String
pf1 forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr forall b c a. (b -> c) -> (a -> b) -> a -> c
(.) forall a. a -> a
id (forall a b. (a -> b) -> [a] -> [b]
map Char -> String -> String
trNL (a -> String
node2String a
n)) forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> String
showNL
trNL :: Char -> String -> String
trNL Char
'\n' = String -> String
showNL forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> String
pf2
trNL Char
c = Char -> String -> String
showChar Char
c
showNL :: String -> String
showNL = Char -> String -> String
showChar Char
'\n'
formatChildren :: (String -> String) -> [t a] -> String -> String
formatChildren String -> String
_ []
= forall a. a -> a
id
formatChildren String -> String
pf (t a
t:[t a]
ts)
| forall (t :: * -> *) a. Foldable t => t a -> Bool
null [t a]
ts
= String -> String
pfl'
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (String -> String) -> t a -> String -> String
formatTr String -> String
pf2' t a
t
| Bool
otherwise
= String -> String
pfl'
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (String -> String) -> t a -> String -> String
formatTr String -> String
pf1' t a
t
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (String -> String) -> [t a] -> String -> String
formatChildren String -> String
pf [t a]
ts
where
pf0' :: String -> String
pf0' = String -> String
pf forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> String -> String
showString String
indent1
pf1' :: String -> String
pf1' = String -> String
pf forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> String -> String
showString String
indent2
pf2' :: String -> String
pf2' = String -> String
pf forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> String -> String
showString String
indent3
pfl' :: String -> String
pfl' = String -> String
pf forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> String -> String
showString String
indent4
formatTr :: (String -> String) -> t a -> String -> String
formatTr = forall (t :: * -> *) a.
Tree t =>
(a -> String)
-> (String -> String)
-> (String -> String)
-> t a
-> String
-> String
formatNTree' a -> String
node2String String -> String
pf0'
indent1 :: String
indent1 = String
"+---"
indent2 :: String
indent2 = String
"| "
indent3 :: String
indent3 = String
" "
indent4 :: String
indent4 = String
"|\n"