Suppose I used language-javascript
library to build AST in Haskell. The AST has nodes of different types, and each node can have fields of those different types.
And each type can have numerous constructors. (All the types instantiate Data
, Eq
and Show
).
I would like to count each type's constructor occurrence in the tree. I could use toConstr
to get the constructor, and ideally I'd make a Tree -> [Constr]
function fisrt (then counting is easy).
There are different ways to do that. Obviously pattern matching is too verbose (imagine around 3 types with 9-28 constructors).
So I'd like to use a generic traversal, and I tried to find the solution in SYB library.
- There is an
everywhere
function, which doesn't suit my needs since I don't need aTree -> Tree
transformation. - There is
gmapQ
, which seems suitable in terms of its type, but as it turns out it's not recursive. - The most viable option so far is
everywhereM
. It still does the useless transformation, but I can use a Writer to collecttoConstr
results. Still, this way doesn't really feel right.
Is there an alternative that will not perform a useless (for this task) transformation and still deliver the list of constructors? (The order of their appearance in the tree doesn't matter for now)