ListTree-0.2.0: Trees and monadic trees expressed as monadic lists where the underlying monad is a list



Functions for iterating trees. A List whose underlying monad is also a List is a tree.

It's nodes are accessible, in contrast to the list monad, which can also be seen as a tree, except only its leafs are accessible and only in dfs order.

 import Control.Monad.Generator
 import Control.Monad.Trans
 import Data.List.Class (genericTake, takeWhile, toList, lastL)

 bits = generate (t "")
 t prev = do
   yield prev
   x <- lift "01"
   t (prev ++ [x])

 > take 3 (bfsLayers bits)

 > take 10 (bfs bits)

 > dfs (genericTake 4 bits)

 > toList $ genericTake 3 bits

Examples of pruning with prune and takeWhile:

 > dfs . takeWhile (not . isSuffixOf "11") $ genericTake 4 bits

 > lastL . takeWhile (not . isSuffixOf "11") $ genericTake 4 bits

 > lastL . prune (not . isSuffixOf "11") $ genericTake 4 bits



class (List t, List (ItemM t)) => Tree t Source

A 'type-class synonym' for Trees.


(List t, List (ItemM t)) => Tree t 

Search algorithms

dfs :: (List l, MonadPlus (ItemM l)) => l a -> ItemM l aSource

Iterate a tree in DFS pre-order. (Depth First Search)

bfs :: Tree t => t a -> ItemM t aSource

Iterate a tree in BFS order. (Breadth First Search)

bfsLayers :: Tree t => t a -> ItemM t (ItemM t a)Source

Transform a tree into lists of the items in its different layers

bestFirstSearchOn :: (Ord b, Tree t) => (a -> b) -> t a -> ItemM t aSource

Best First Search given a scoring function.

bestFirstSearchSortedChildrenOn :: (Ord b, Tree t) => (a -> b) -> t a -> ItemM t aSource

Best-First-Search given that a node's children are in sorted order (best first) and given a scoring function. Especially useful for trees where nodes have an infinite amount of children, where bestFirstSearchOn will get stuck.

Example: Find smallest Pythagorian Triplets

 import Control.Monad
 import Control.Monad.Generator
 import Control.Monad.Trans.Class
 import Data.List.Tree
 import Data.Maybe

 pythagorianTriplets =
   catMaybes .
   fmap fst .
   bestFirstSearchSortedChildrenOn snd .
   generate $ do
     x <- lift [1..]
     yield (Nothing, x)
     y <- lift [1..]
     yield (Nothing, x + y)
     z <- lift [1..]
     yield (Nothing, x + y + z)
     lift . guard $ x^2 + y^2 == z^2
     yield (Just (x, y, z), 0)

 > print $ take 10 pythagorianTriplets

sortChildrenOn :: (Ord b, Tree t) => (a -> b) -> t a -> ListT (ItemM t) aSource

Pruning methods

prune :: MonadPlus m => (a -> Bool) -> ListT m a -> ListT m aSource

Prune a tree or list given a predicate. Unlike takeWhile which stops a branch where the condition doesn't hold, prune cuts the whole branch (the underlying MonadPlus's mzero).

pruneM :: MonadPlus m => (a -> m Bool) -> ListT m a -> ListT m aSource

branchAndBound :: (Ord b, Tree t) => (a -> (Maybe b, Maybe b)) -> t a -> ListT (ListT (StateT (Maybe b) (ItemM (ItemM t)))) aSource

Generalized Branch and Bound. A method for pruning.

The result of this function would usually be given to another search algorithm, such as dfs, in order to find the node with lowest value.

This augments the regular search by pruning the tree. Given a function to calculate a lower and upper bound for a subtree, we keep the lowest upper bound (hence the State monad) encountered so far, and we prune any subtree whose lower bound is over the known upper bound.