Safe Haskell | Safe-Inferred |
---|---|
Language | Haskell2010 |
Synopsis
- data Tree a = Node {}
- type Forest a = [Tree a]
- type NEForest a = NonEmpty (Tree a)
- sortTree :: Ord a => Tree a -> Tree a
- sortTreeOn :: Ord b => (a -> b) -> Tree a -> Tree a
- sortForest :: Ord a => Forest a -> Forest a
- sortForestOn :: Ord b => (a -> b) -> Forest a -> Forest a
- sortNEForest :: Ord a => NEForest a -> NEForest a
- sortNEForestOn :: Ord b => (a -> b) -> NEForest a -> NEForest a
- findNodeInTree :: (a -> Bool) -> Tree a -> Maybe (Tree a)
- isSubtreeOf :: Ord a => Tree a -> Tree a -> Bool
- isExactSubtreeOf :: Eq a => Tree a -> Tree a -> Bool
- isSubtreeOfUsing :: forall a. Eq a => (Tree a -> Tree a) -> Tree a -> Tree a -> Bool
- findNodeInForest :: Foldable t => (a -> Bool) -> t (Tree a) -> Maybe (Tree a)
- isSubtreeIn :: (Foldable t, Ord a) => Tree a -> t (Tree a) -> Bool
- isExactSubtreeIn :: (Eq a, Foldable t) => Tree a -> t (Tree a) -> Bool
- isSubtreeInUsing :: (Eq a, Foldable t) => (Tree a -> Tree a) -> Tree a -> t (Tree a) -> Bool
- enumerateTree :: (Enum a, Num a) => Tree b -> Tree (a, b)
- zipTree :: Tree a -> Tree b -> Tree (a, b)
- zipWithTree :: (a -> b -> c) -> Tree a -> Tree b -> Tree c
- pathsTree :: forall a. Tree a -> NonEmpty (Seq a)
- leavesTree :: Tree a -> NonEmpty a
- enumerateForest :: (Enum a, Num a) => Forest b -> Forest (a, b)
- enumerateNEForest :: (Enum a, Num a) => NEForest b -> NEForest (a, b)
- mapForest :: (a -> b) -> Forest a -> Forest b
- mapNEForest :: (a -> b) -> NEForest a -> NEForest b
- zipForest :: Forest a -> Forest b -> Forest (a, b)
- zipNEForest :: NEForest a -> NEForest b -> NEForest (a, b)
- zipWithForest :: (a -> b -> c) -> Forest a -> Forest b -> Forest c
- zipWithNEForest :: (a -> b -> c) -> NEForest a -> NEForest b -> NEForest c
- pathsForest :: Forest a -> Maybe (NonEmpty (Seq a))
- pathsNEForest :: NEForest a -> NonEmpty (Seq a)
- leavesForest :: Forest a -> Maybe (NonEmpty a)
- leavesNEForest :: NEForest a -> NonEmpty a
- flattenForest :: Forest a -> [a]
- flattenNEForest :: NEForest a -> NonEmpty a
- singletonTree :: a -> Tree a
- indicesTree :: (Enum a, Num a) => Tree a
- eitherTreeFromLabels :: (a -> a -> Bool) -> a -> [a] -> Either (FromPartitionedLabelsError a) (Tree a)
- unsafeTreeFromLabels :: (Show a, Typeable a) => (a -> a -> Bool) -> a -> [a] -> Tree a
- singletonForest :: a -> Forest a
- singletonNEForest :: a -> NEForest a
- indicesForest :: (Enum a, Num a) => Forest a
- indicesNEForest :: (Enum a, Num a) => NEForest a
- subtrees :: Tree a -> Forest a
- neSubtrees :: Tree a -> NEForest a
- eitherNEForestFromPartitionedLabels :: forall a. (a -> a -> Bool) -> NonEmpty a -> [a] -> Either (FromPartitionedLabelsError a) (NEForest a)
- unsafeNEForestFromPartitionedLabels :: (Show a, Typeable a) => (a -> a -> Bool) -> NonEmpty a -> [a] -> NEForest a
- eitherNEForestFromLabels :: forall a. (a -> Bool) -> (a -> a -> Bool) -> NonEmpty a -> Either (FromLabelsError a) (NEForest a)
- unsafeNEForestFromLabels :: (Show a, Typeable a) => (a -> Bool) -> (a -> a -> Bool) -> NonEmpty a -> NEForest a
- neForest :: Forest a -> Maybe (NEForest a)
- unsafeNEForest :: Forest a -> NEForest a
- data FromPartitionedLabelsError a = OrphansFoundError (NEForest a) (NonEmpty a)
- data FromLabelsError a
Introduction
This module captures functions and patterns often reached for when working
with Data.Tree from the containers
package.
Re-exports
Non-empty, possibly infinite, multi-way trees; also known as rose trees.
Instances
Monad Tree | |
Functor Tree | |
MonadFix Tree | Since: containers-0.5.11 |
Applicative Tree | |
Foldable Tree | |
Defined in Data.Tree fold :: Monoid m => Tree m -> m # foldMap :: Monoid m => (a -> m) -> Tree a -> m # foldMap' :: Monoid m => (a -> m) -> Tree a -> m # foldr :: (a -> b -> b) -> b -> Tree a -> b # foldr' :: (a -> b -> b) -> b -> Tree a -> b # foldl :: (b -> a -> b) -> b -> Tree a -> b # foldl' :: (b -> a -> b) -> b -> Tree a -> b # foldr1 :: (a -> a -> a) -> Tree a -> a # foldl1 :: (a -> a -> a) -> Tree a -> a # elem :: Eq a => a -> Tree a -> Bool # maximum :: Ord a => Tree a -> a # | |
Traversable Tree | |
Eq1 Tree | Since: containers-0.5.9 |
Ord1 Tree | Since: containers-0.5.9 |
Read1 Tree | Since: containers-0.5.9 |
Show1 Tree | Since: containers-0.5.9 |
MonadZip Tree | |
Eq a => Eq (Tree a) | |
Data a => Data (Tree a) | |
Defined in Data.Tree gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b) -> (forall g. g -> c g) -> Tree a -> c (Tree a) # gunfold :: (forall b r. Data b => c (b -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c (Tree a) # toConstr :: Tree a -> Constr # dataTypeOf :: Tree a -> DataType # dataCast1 :: Typeable t => (forall d. Data d => c (t d)) -> Maybe (c (Tree a)) # dataCast2 :: Typeable t => (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c (Tree a)) # gmapT :: (forall b. Data b => b -> b) -> Tree a -> Tree a # gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> Tree a -> r # gmapQr :: forall r r'. (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> Tree a -> r # gmapQ :: (forall d. Data d => d -> u) -> Tree a -> [u] # gmapQi :: Int -> (forall d. Data d => d -> u) -> Tree a -> u # gmapM :: Monad m => (forall d. Data d => d -> m d) -> Tree a -> m (Tree a) # gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> Tree a -> m (Tree a) # gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> Tree a -> m (Tree a) # | |
Read a => Read (Tree a) | |
Show a => Show (Tree a) | |
Generic (Tree a) | Since: containers-0.5.8 |
NFData a => NFData (Tree a) | |
Generic1 Tree | Since: containers-0.5.8 |
type Rep (Tree a) | |
Defined in Data.Tree type Rep (Tree a) = D1 ('MetaData "Tree" "Data.Tree" "containers-0.6.2.1" 'False) (C1 ('MetaCons "Node" 'PrefixI 'True) (S1 ('MetaSel ('Just "rootLabel") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 a) :*: S1 ('MetaSel ('Just "subForest") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 (Forest a)))) | |
type Rep1 Tree | |
Defined in Data.Tree type Rep1 Tree = D1 ('MetaData "Tree" "Data.Tree" "containers-0.6.2.1" 'False) (C1 ('MetaCons "Node" 'PrefixI 'True) (S1 ('MetaSel ('Just "rootLabel") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) Par1 :*: S1 ('MetaSel ('Just "subForest") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) ([] :.: Rec1 Tree))) |
Types
type NEForest a = NonEmpty (Tree a) Source #
A convenience type alias for a non-empty Forest
.
Since: 0.1.0.0
Sorting
Trees
sortTree :: Ord a => Tree a -> Tree a Source #
Sort from lowest to highest at each level in the Tree
.
Since: 0.1.0.0
sortTreeOn :: Ord b => (a -> b) -> Tree a -> Tree a Source #
Sort from lowest to highest at each level in the Tree
, using the results
of a key function applied to each label.
Since: 0.1.0.0
Forests
Searching
Trees
:: forall a. Eq a | |
=> (Tree a -> Tree a) | Transforms |
-> Tree a | |
-> Tree a | |
-> Bool |
Check if the first Tree
is a subtree of the second via equality of the
first Tree
with any node in the second Tree
.
This is a lower-level function. Users should prefer isSubtreeOf
and
isExactSubtreeOf
over this function. The function argument enables
pre-processing over the Tree
values involved, before equality-checking is
performed.
Since: 0.1.0.0
Forests
findNodeInForest :: Foldable t => (a -> Bool) -> t (Tree a) -> Maybe (Tree a) Source #
Find a particular Node
in a forest via the provided label predicate. This
function delegates to findNodeInTree
for each Tree
in the forest.
Since: 0.1.0.0
:: (Eq a, Foldable t) | |
=> (Tree a -> Tree a) | Transforms |
-> Tree a | |
-> t (Tree a) | |
-> Bool |
Check if the first Tree
is a subtree in the forest via equality of the
first Tree
with any node in any Tree
in the forest.
This is a lower-level function. Users should prefer isSubtreeIn
and
isExactSubtreeIn
over this function. The function argument enables
pre-processing over the Tree
values involved, before equality-checking is
performed.
Since: 0.1.0.0
Transformation
Trees
enumerateTree :: (Enum a, Num a) => Tree b -> Tree (a, b) Source #
Number each level of labels in the tree, starting from 0 at each level.
Since: 0.1.0.0
zipTree :: Tree a -> Tree b -> Tree (a, b) Source #
Given two input Tree
values, provide a Tree
of corresponding label
pairs. This function exists for the convenience of not needing to import
Control.Monad.Zip.
Since: 0.1.0.0
zipWithTree :: (a -> b -> c) -> Tree a -> Tree b -> Tree c Source #
Generalizes zipTree
by zipping label values via the provided function.
This function exists for the convenience of not needing to import
Control.Monad.Zip.
Since: 0.1.0.0
pathsTree :: forall a. Tree a -> NonEmpty (Seq a) Source #
Produce all the paths for the given Tree
.
λ> pathsTree $ Node 1 [Node 2 [Node 4 [], Node 5 []], Node 3 []] fromList [1] :| [fromList [1,2],fromList [1,2,4],fromList [1,2,5],fromList [1,3]]
Since: 0.1.0.0
Forests
enumerateForest :: (Enum a, Num a) => Forest b -> Forest (a, b) Source #
Number each level of labels in the Forest
, starting from 0 at each level.
Since: 0.1.0.0
enumerateNEForest :: (Enum a, Num a) => NEForest b -> NEForest (a, b) Source #
Number each level of labels in the NEForest
, starting from 0 at each
level.
Since: 0.1.0.0
mapNEForest :: (a -> b) -> NEForest a -> NEForest b Source #
zipWithForest :: (a -> b -> c) -> Forest a -> Forest b -> Forest c Source #
Generalizes zipForest
by zipping label values via the provided function.
Since: 0.1.0.0
zipWithNEForest :: (a -> b -> c) -> NEForest a -> NEForest b -> NEForest c Source #
Generalizes zipNEForest
by zipping label values via the provided
function.
Since: 0.1.0.0
pathsNEForest :: NEForest a -> NonEmpty (Seq a) Source #
Produce all the paths for the given NEForest
.
Since: 0.1.0.0
leavesNEForest :: NEForest a -> NonEmpty a Source #
Produce all the leaves for the given NEForest
.
Since: 0.1.0.0
flattenForest :: Forest a -> [a] Source #
flattenNEForest :: NEForest a -> NonEmpty a Source #
Construction
Trees
singletonTree :: a -> Tree a Source #
Creates a Tree
containing the provided label and no children.
Since: 0.1.0.0
indicesTree :: (Enum a, Num a) => Tree a Source #
Produce an infinite Tree
of indices, starting from 0 at each level.
Since: 0.1.0.0
:: (a -> a -> Bool) | Is the first label an immediate child of the second? |
-> a | Root label |
-> [a] | Flat list of child labels |
-> Either (FromPartitionedLabelsError a) (Tree a) |
Build a Tree
from a root label and a flat list of child labels.
Since: 0.2.0.0
:: (Show a, Typeable a) | |
=> (a -> a -> Bool) | Is the first label an immediate child of the second? |
-> a | Root label |
-> [a] | Flat list of labels |
-> Tree a |
Build a Tree
from a root label and a flat list of child labels.
Throws FromPartitionedLabelsError
if anything goes wrong when building
the Tree
.
Since: 0.2.0.0
Forests
singletonForest :: a -> Forest a Source #
singletonNEForest :: a -> NEForest a Source #
indicesForest :: (Enum a, Num a) => Forest a Source #
Produce an infinite Forest
of indices, starting from 0 at each level.
Since: 0.1.0.0
indicesNEForest :: (Enum a, Num a) => NEForest a Source #
Produce an infinite NEForest
of indices, starting from 0 at each level.
Since: 0.1.0.0
subtrees :: Tree a -> Forest a Source #
Produces all subtrees of the given Tree
.
The output is a Forest
out of convenience, but is guaranteed non-empty as
a Tree
itself is non-empty by construction. See neSubtrees
for a variant
that returns an NEForest
.
Since: 0.1.0.0
eitherNEForestFromPartitionedLabels Source #
:: forall a. (a -> a -> Bool) | Is the first label an immediate child of the second? |
-> NonEmpty a | Flat list of root labels |
-> [a] | Flat list of child labels |
-> Either (FromPartitionedLabelsError a) (NEForest a) |
Build an NEForest
from flat input lists of root and child labels.
Since: 0.2.0.0
unsafeNEForestFromPartitionedLabels Source #
:: (Show a, Typeable a) | |
=> (a -> a -> Bool) | Is the first label an immediate child of the second? |
-> NonEmpty a | Flat list of root labels |
-> [a] | Flat list of child labels |
-> NEForest a |
Build an NEForest
from flat input lists of root and child labels.
Throws FromPartitionedLabelsError
if anything goes wrong when building
the NEForest
.
Since: 0.2.0.0
eitherNEForestFromLabels Source #
:: forall a. (a -> Bool) | Is the label a root? |
-> (a -> a -> Bool) | Is the first label an immediate child of the second? |
-> NonEmpty a | Flat list of labels |
-> Either (FromLabelsError a) (NEForest a) |
Build an NEForest
from a flat input list of labels.
Since: 0.1.0.0
unsafeNEForestFromLabels Source #
:: (Show a, Typeable a) | |
=> (a -> Bool) | Is the label a root? |
-> (a -> a -> Bool) | Is the first label an immediate child of the second? |
-> NonEmpty a | Flat list of labels |
-> NEForest a |
Build an NEForest
from a flat input list of labels.
Throws FromLabelsError
if anything goes wrong when building the NEForest
.
Since: 0.1.0.0
unsafeNEForest :: Forest a -> NEForest a Source #
Errors
data FromPartitionedLabelsError a Source #
The error type when building a Tree
/NEForest
from labels already
partitioned into roots and children.
Since: 0.2.0.0
OrphansFoundError (NEForest a) (NonEmpty a) | Orphan labels were found. Provides the assembled Since: 0.2.0.0 |
Instances
data FromLabelsError a Source #
The error type when building an NEForest
from a flat list of labels.
Since: 0.2.0.0
NoRootsFoundError (NonEmpty a) | No root label(s) were found. Provides the flat list of input labels. Since: 0.2.0.0 |
FromPartitionedLabels (FromPartitionedLabelsError a) | Produced via internally building from partitioned labels. Since: 0.2.0.0 |