2007-04-06

Generic Visitor Combinators

It's simple and practical - Visitor Combination and Traversal Control.

The main idea is to separate specific code that executes concrete actions from the code that effects visits check.








IdentyDo nothing
Sequence(v1,v2)Sequentially perform visitor v2 after v1.
FailRaise exception.
Choice(v1,v2)Try visitor v1. If v1 fails, try v2 (left-biased choice).
All(v)Apply visitor v sequentially to every immediate subtree.
One(v)Apply visitor v sequentially to the immediate subtrees until it succeeds.


So, it's rather flexible to implement different actions by combine it and inherit from it.


TopDown(v) = def Sequence(v,All(TopDown(v)))
BottomUp(v) = def Sequence(All(BottomUp(v)),v)
IfZeroAddOne = def TopDown(Try(Sequence(IsZero,AddOne)))

No comments:

Post a Comment