stateful walk through a tree?

Hi, I am looking for help in design of a stateful tree walker. I have a tree-like data structure (an AST for a small language) for which there is a general stateful walk procedure. That is, each node type is an instance of a walker class, and the walk function is monadic: class Walker a where walk :: a -> State Context b The context is used to store names in the scope, for example. Now, I'd like to use Walker as a general class for implementing several different transformations on the tree (like cross-referencing, code emission, tree visualisation). Those transformations will need expanded state. What is the proper design for that? In Common Lisp, for example, I would Context to contain an opaque slot (visitor), and would assign additional state information to it; a would also define a generic function that would dispatch on the visitor (and probably on walker if I end up having more than one walker). How would I do that in Haskell? I'd like to avoid using mutable variables for now (mostly for didactic puproses). David

David Tolpin wrote:
I am looking for help in design of a stateful tree walker.
I think that you can use Data.Traversable to great effect. Related to that are Control.Applicative and Data.Foldable. The papers that are mentioned in the Haddocks explain what these modules do and what they are useful for.
How would I do that in Haskell? I'd like to avoid using mutable variables for now (mostly for didactic puproses).
Well, Haskell doesn't have mutable variables as LISP or ML do. In the end, avoiding mutable variables is more useful for non-didactic purposes :) Regards, apfelmus

Hi,
I am looking for help in design of a stateful tree walker.
I think that you can use Data.Traversable to great effect.
thanks, the advice has helped. I can't use it directly since i didn't yet come up with a consistent solution to serialization of events, but I am going to compile the second pass into combined actions.
Well, Haskell doesn't have mutable variables as LISP or ML do.
I meant STM, sorry for being unclear. David
participants (2)
-
apfelmus@quantentunnel.de
-
David Tolpin