Portability | ghc |
---|---|

Stability | beta |

Maintainer | Neil Sculthorpe <neil@ittc.ku.edu> |

Safe Haskell | Safe-Infered |

This module provides combinators that traverse a tree.

Note that all traversals take place on the node, its children, or its descendents. There is no mechanism for "ascending" the tree.

- class (Injection a (Generic a), Generic a ~ Generic (Generic a)) => Term a where
- type Generic a :: *
- numChildren :: a -> Int

- class (MonadPlus m, Term a) => Walker c m a where
- childR :: Walker c m a => Int -> Rewrite c m (Generic a) -> Rewrite c m a
- alltdR :: (Walker c m a, a ~ Generic a) => Rewrite c m (Generic a) -> Rewrite c m (Generic a)
- anytdR :: (Walker c m a, a ~ Generic a) => Rewrite c m (Generic a) -> Rewrite c m (Generic a)
- allbuR :: (Walker c m a, a ~ Generic a) => Rewrite c m (Generic a) -> Rewrite c m (Generic a)
- anybuR :: (Walker c m a, a ~ Generic a) => Rewrite c m (Generic a) -> Rewrite c m (Generic a)
- allduR :: (Walker c m a, a ~ Generic a) => Rewrite c m (Generic a) -> Rewrite c m (Generic a)
- anyduR :: (Walker c m a, a ~ Generic a) => Rewrite c m (Generic a) -> Rewrite c m (Generic a)
- tdpruneR :: (Walker c m a, a ~ Generic a) => Rewrite c m (Generic a) -> Rewrite c m (Generic a)
- innermostR :: (Walker c m a, Generic a ~ a) => Rewrite c m (Generic a) -> Rewrite c m (Generic a)
- childT :: Walker c m a => Int -> Translate c m (Generic a) b -> Translate c m a b
- foldtdT :: (Walker c m a, Monoid b, a ~ Generic a) => Translate c m (Generic a) b -> Translate c m (Generic a) b
- foldbuT :: (Walker c m a, Monoid b, a ~ Generic a) => Translate c m (Generic a) b -> Translate c m (Generic a) b
- tdpruneT :: (Walker c m a, Monoid b, a ~ Generic a) => Translate c m (Generic a) b -> Translate c m (Generic a) b
- crushtdT :: (Walker c m a, Monoid b, a ~ Generic a) => Translate c m (Generic a) b -> Translate c m (Generic a) b
- crushbuT :: (Walker c m a, Monoid b, a ~ Generic a) => Translate c m (Generic a) b -> Translate c m (Generic a) b
- type Path = [Int]
- pathL :: (Walker c m a, a ~ Generic a) => Path -> Lens c m (Generic a) (Generic a)
- exhaustPathL :: (Walker c m a, a ~ Generic a) => Path -> Lens c m (Generic a) (Generic a)
- repeatPathL :: (Walker c m a, a ~ Generic a) => Path -> Lens c m (Generic a) (Generic a)

# Traversal Classes

class (Injection a (Generic a), Generic a ~ Generic (Generic a)) => Term a whereSource

A `Term`

is any node in the tree that you wish to be able to traverse.

`Generic`

is a sum of all the interesting sub-types, transitively, of `a`

.
We use `Generic a ~ a`

to signify that something is its own Generic.
Simple expression types might be their own sole `Generic`

, more complex examples
will have a new datatype for the `Generic`

, which will also be an instance of class `Term`

.

numChildren :: a -> IntSource

Count the number of interesting children.

class (MonadPlus m, Term a) => Walker c m a whereSource

`Walker`

captures the ability to walk over a `Term`

applying `Rewrite`

s,
using a specific context `c`

and a `MonadPlus`

`m`

.

Minimal complete definition: `childL`

.

Default instances are provided for `allT`

, `allR`

and `anyR`

, but they may be overridden for efficiency.
For small numbers of interesting children this will not be an issue, but for a large number, say
for a list of children, it may be.

childL :: Int -> Lens c m a (Generic a)Source

Construct a `Lens`

pointing at the n-th interesting child of this node.

allT :: Monoid b => Translate c m (Generic a) b -> Translate c m a bSource

Apply a `Generic`

`Translate`

to all interesting children of this node, succeeding if they all succeed.
The results are combined in a `Monoid`

.

# Rewrite Traversals

childR :: Walker c m a => Int -> Rewrite c m (Generic a) -> Rewrite c m aSource

Apply a `Rewrite`

to a specific child.

alltdR :: (Walker c m a, a ~ Generic a) => Rewrite c m (Generic a) -> Rewrite c m (Generic a)Source

Apply a `Rewrite`

in a top-down manner, succeeding if they all succeed.

anytdR :: (Walker c m a, a ~ Generic a) => Rewrite c m (Generic a) -> Rewrite c m (Generic a)Source

Apply a `Rewrite`

in a top-down manner, succeeding if any succeed.

allbuR :: (Walker c m a, a ~ Generic a) => Rewrite c m (Generic a) -> Rewrite c m (Generic a)Source

Apply a `Rewrite`

in a bottom-up manner, succeeding if they all succeed.

anybuR :: (Walker c m a, a ~ Generic a) => Rewrite c m (Generic a) -> Rewrite c m (Generic a)Source

Apply a `Rewrite`

in a bottom-up manner, succeeding if any succeed.

allduR :: (Walker c m a, a ~ Generic a) => Rewrite c m (Generic a) -> Rewrite c m (Generic a)Source

Apply a `Rewrite`

twice, in a top-down and bottom-up way, using one single tree traversal,
succeeding if they all succeed.

anyduR :: (Walker c m a, a ~ Generic a) => Rewrite c m (Generic a) -> Rewrite c m (Generic a)Source

Apply a `Rewrite`

twice, in a top-down and bottom-up way, using one single tree traversal,
succeeding if any succeed.

tdpruneR :: (Walker c m a, a ~ Generic a) => Rewrite c m (Generic a) -> Rewrite c m (Generic a)Source

Attempt to apply a `Rewrite`

in a top-down manner, prunning at successful rewrites.

innermostR :: (Walker c m a, Generic a ~ a) => Rewrite c m (Generic a) -> Rewrite c m (Generic a)Source

A fixed-point traveral, starting with the innermost term.

# Translate Traversals

childT :: Walker c m a => Int -> Translate c m (Generic a) b -> Translate c m a bSource

Apply a `Translate`

to a specific child.

foldtdT :: (Walker c m a, Monoid b, a ~ Generic a) => Translate c m (Generic a) b -> Translate c m (Generic a) bSource

Fold a tree in a top-down manner, using a single `Translate`

for each node.

foldbuT :: (Walker c m a, Monoid b, a ~ Generic a) => Translate c m (Generic a) b -> Translate c m (Generic a) bSource

Fold a tree in a bottom-up manner, using a single `Translate`

for each node.

tdpruneT :: (Walker c m a, Monoid b, a ~ Generic a) => Translate c m (Generic a) b -> Translate c m (Generic a) bSource

Attempt to apply a `Translate`

in a top-down manner, prunning at successes.

crushtdT :: (Walker c m a, Monoid b, a ~ Generic a) => Translate c m (Generic a) b -> Translate c m (Generic a) bSource

An always successful top-down fold, replacing failures with `mempty`

.

crushbuT :: (Walker c m a, Monoid b, a ~ Generic a) => Translate c m (Generic a) b -> Translate c m (Generic a) bSource

An always successful bottom-up fold, replacing failures with `mempty`

.