What is the easiest way to upgrade an immutable AST?

While working with macros, I reached this point (I try to avoid it), where I need to update those nodes in the AST that contain certain conditions. For example, let's say I would like to update each node:

Literal(Constant(1))

with value:

Literal(Constant(2))

These AST nodes can be located anywhere in the expression tree, so I cannot use a special template template. Obviously, the last thing I would like to do is the code for the complete template template, which can cover all compiler primitives. I searched in the API , but I got the impression that methods like collecting and moving family are not good enough to satisfy my needs, because they consider the tree as a linear thing, and I want the result to be an entire updated tree. So, is it possible to confidently update the tree of immutable expressions? Why is there no such “update” operation in the standard API?

+5
source share
2 answers

( ).

, "Scrap Your Boilerplate" , .

Haskell node : - :

-- | Apply a transformation everywhere in bottom-up manner
everywhere :: (forall a. Data a => a -> a)
           -> (forall a. Data a => a -> a)

Haskell . Scala

+2

All Articles