Type-directed defunctionalization in the presence of typeclasses and monomorphization
We present the design and implementation of type-directed defunctionalization in Roc, a pure functional language with a continuation-based effect system and typeclasses. We have developed a compiler for Roc that performs monomorphization and specialization of higher-order functions over the function values they consume, via the lambda set specialization introduced by Brandon et al.
We describe our implementation effort to efficiently infer lambda sets, which can be modeled as anonymous sums. We also present our strategies to compile lambda sets with closure data to optimal runtime representations, which avoid boxing closure data except in the case of recursive closures.
We describe challenges we have encountered in our implementation of inference of recursive lambda sets, and the interning of those computations.
Finally, we present a single-pass algorithm we have developed to infer and specialize lambda sets in the presence of typeclasses compiled via type specialization. We believe this algorithm to be novel, and hope that it can inspire efforts to specialize higher-order functions over closure values in other languages.