What is the easiest way to update an immutable AST?

假装没事ソ 提交于 2019-12-04 07:17:10
Don Stewart

Modifying poly-typed nodes in data structures generically is a classic case of datatype generics (parameterization of code via type constructor).

Several approaches exist for such operations, such as the "Scrap Your Boilerplate" approach, which define datatype generic traversal functions.

In Haskell, the node update function is parameterized in two dimensions: by datatype and by code -- so you can apply different update functions to different types, anywhere in a structure:

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

Doing so in Haskell relies heavily on type classes. In Scala, there are several ported examples

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!