After parsing source code with megaparsec, I have an abstract syntax tree. In a second stage, I compute aggregates on each subtree, e.g. the list of variables it contains, by aggregating leaf values, and I want to store them somewhere, so I don't have to recompute them later. How should I do that ?
I have considered these options.
- Add an aggregates value to each node. I would need to modify each node data type.
- Wrap each node data type in a generic aggregate data type.
- Use generics node data type, with aggregates, but I'm afraid compilation error messages won't be as clear.
- Build a "shadow tree", with the same topology as the AST, but containing the aggregates.
What should I chose ?