Type-safe generation of modules in applicative and generative styles
The MetaML approach for multi-stage programming provides the static guarantee of type safety and scope safety for generated code regardless of the actual values of static parameters. While the traditional MetaML approach is restricted to term generation, Watanabe et al. presents a preliminary study on a two-stage language for generating ML-style modules, which provides high-level abstractions for large-scale programming. Unfortunately, their language has the problems of limited expressiveness and incomplete proofs of properties, and also an efficiency problem (the code-explosion problem). Recently, Sato et al. solved the latter issue, while the former problems are left as they are. This paper introduces refined two-stage programming languages for generating ML-style modules, which solve the above issues. Our languages accommodate module generation for two different module styles: first-class modules with generative functors and second-class modules with applicative functors, which are supported, but cannot be generated by MetaOCaml. Since generative functors and applicative functors have their own merits and demerits, our languages can be a better basis for the study of module generation. We also present a type-preserving translation from our languages to plain MetaOCaml, and an implementation based on the translations. Finally, we show the result of our experiments against a microbencmark, which shows that our translation does not have the code-explosion problem, and there are cases where module-generation is effective for program specialization.