Closed LeventErkok closed 4 years ago
This isn't a good case for uniplate. You really want the function:
children2 :: Expr -> [Either Expr Stmt]
Where you get the children, but stopping at the first Expr OR Stmt you find, so you can continue from there. You can do such a traversal with plain SYB with something along the lines of:
everything (++) (const [] `extQ` onStmt `extQ` onExpr)
where onStmt (x :: Stmt) = ...
onExpr (x :: Expr) = freeVars x
Uniplate makes the simplifying assumption you only want to work on one target type. Once you mix Stmt and Expr freely that's no longer true.
Thanks Neil. That's what I suspected. But this makes the assumption that I now that I only have Stmt
and OnExpr
and nohing else. This is where the syb-with-class comes into play, but it appears it isn't really supported that well anymore.
Thanks for the feedback though.
I'm curious if there's an easier/less-error-prone way to write traversals that go through a class method in Uniplate. Here's what I mean:
Sorry, this is a wall of text; but all it really is doing is to compute free-variables in a lambda-calculus like language; pared down to demonstrate the issue.
It's the final case in
freeVars
forExpr
that I'm concerned about. I'd really love to write it as inchoice = 1
, but that's wrong as it simply picks all the names. Option2
would be nice, but it doesn't work since it only "folds" over expressions and hence misses statements. (You can run the expressiontest
to see what I mean by changing thechoice
value.)The only way I found how to write this correctly is
choice = 3
, i.e., extract "all" interesting subcomponents and collect the frees recursively. But this is very error prone, as I need to know that there areStmt
's insideExprs
, and if I add another kind of data, the code would break without any indication.What's the best solution for this problem in Uniplate? I gather syb-with-class is really the way to go with this, but that doesn't really seem to be supported with
GHC.Generics
and I'm curious what the Uniplate solution for this sort of problem is.