Tagless final: How to create values at runtime?

Viewed 103

The common "initial" representation of ASTs involves building a tree with sum types. My understanding of the tagless final approach is that it involves instead representing ASTs as a function which takes in a set of callbacks (either implicitly via typeclass or explicitly as a record), calls the functions corresponding to the AST being represented, and returns the result.

All the examples I've seen hardcode this function. But often, you want to generate an AST at runtime, e.g. as the output of a parser. With initial representations, this is trivial, but it's not clear to me how this is handled in the tagless final approach. How is this done?

The only thing I can think of is to build up the function by combining partial functions corresponding to each layer of the tree, but this seems like it would be really inefficient and it would require higher ranked types. Is this really the answer?

0 Answers
Related