Refined Environment Classifiers

Type- and Scope-Safe Code Generation with Mutable Cells
  • Oleg Kiselyov
  • Yukiyoshi Kameyama
  • Yuto Sudo
Conference paper
Part of the Lecture Notes in Computer Science book series (LNCS, volume 10017)


Generating high-performance code and applying typical optimizations within the bodies of loops and functions involves moving or storing open code for later use, often in a different binding environment. There are ample opportunities for variables being left unbound or accidentally captured. It has been a tough challenge to statically ensure that by construction the generated code is nevertheless well-typed and well-scoped: all free variables in manipulated and stored code fragments shall eventually be bound, by their intended binders.

We present the calculus for code generation with mutable state that for the first time achieves type-safety and hygiene without ad hoc restrictions. The calculus strongly resembles region-based memory management, but with the orders of magnitude simpler proofs. It employs the rightly abstract representation for free variables, which, like hypothesis in natural deduction, are free from the bureaucracy of syntax imposed by the type environment or numbering conventions.

Although the calculus was designed for the sake of formalization and is deliberately bare-bone, it turns out easily implementable and not too bothersome for writing realistic program.


Free Variable Open Code Reference Cell Natural Deduction Type Environment 
These keywords were added by machine and not by the authors. This process is experimental and the keywords may be updated as the learning algorithm improves.



We thank anonymous reviewers for many helpful comments. This work was partially supported by JSPS KAKENHI Grant Numbers 15K12007, 16K12409, 15H02681.

Supplementary material


  1. 1.
    Calcagno, C., Moggi, E., Taha, W.: Closed types as a simple approach to safe imperative multi-stage programming. In: Montanari, U., Rolim, J.D.P., Welzl, E. (eds.) ICALP 2000. LNCS, vol. 1853, pp. 25–36. Springer, Heidelberg (2000). doi: 10.1007/3-540-45022-X_4 CrossRefGoogle Scholar
  2. 2.
    Calcagno, C., Taha, W., Huang, L., Leroy, X.: Implementing multi-stage languages using ASTs, gensym, and reflection. In: Pfenning, F., Smaragdakis, Y. (eds.) GPCE 2003. LNCS, vol. 2830, pp. 57–76. Springer, Heidelberg (2003). doi: 10.1007/978-3-540-39815-8_4 CrossRefGoogle Scholar
  3. 3.
    Chen, C., Xi, H.: Meta-programming through typeful code representation. J. Funct. Program. 15(6), 797–835 (2005)CrossRefzbMATHGoogle Scholar
  4. 4.
    Davies, R.: A temporal logic approach to binding-time analysis. In: LICS, pp. 184–195 (1996)Google Scholar
  5. 5.
    Fluet, M., Morrisett, J.G.: Monadic regions. J. Funct. Program. 16(4–5), 485–545 (2006)MathSciNetCrossRefzbMATHGoogle Scholar
  6. 6.
    Kameyama, Y., Kiselyov, O., Shan, C.: Combinators for impure yet hygienic code generation. Sci. Comput. Program. 112, 120–144 (2015)CrossRefGoogle Scholar
  7. 7.
    Kim, I.S., Yi, K., Calcagno, C.: A polymorphic modal type system for lisp-like multi-staged languages. In: POPL, pp. 257–268 (2006)Google Scholar
  8. 8.
    Kiselyov, O.: The design and implementation of BER MetaOCaml. In: Codish, M., Sumii, E. (eds.) FLOPS 2014. LNCS, vol. 8475, pp. 86–102. Springer, Heidelberg (2014). doi: 10.1007/978-3-319-07151-0_6 CrossRefGoogle Scholar
  9. 9.
    Le Botlan, D., Rémy, D.: ML\(^{\rm F}\): raising ML to the power of system F. In: ICFP, pp. 27–38 (2003)Google Scholar
  10. 10.
    Nanevski, A., Pfenning, F., Pientka, B.: Contextual modal type theory. Trans. Comput. Logic 9(3), 1–49 (2008)MathSciNetCrossRefGoogle Scholar
  11. 11.
    POPL 2003: Conference Record of the Annual ACM Symposium on Principles of Programming Languages (2003)Google Scholar
  12. 12.
    Pottier, F.: Static name control for FreshML. In: LICS, pp. 356–365. IEEE Computer Society (2007)Google Scholar
  13. 13.
    Pouillard, N., Pottier, F.: A fresh look at programming with names and binders. In: ICFP, pp. 217–228. ACM, New York (2010)Google Scholar
  14. 14.
    Rompf, T., Amin, N., Moors, A., Haller, P., Odersky, M.: Scala-virtualized: linguistic reuse for deep embeddings. High. Order Symbolic Comput. 25, 165–207 (2013)CrossRefGoogle Scholar
  15. 15.
    Taha, W., Nielsen, M.F.: Environment classifiers. In: POPL [11], pp. 26–37Google Scholar
  16. 16.
    Thiemann, P.: Combinators for program generation. J. Funct. Program. 9(5), 483–525 (1999)MathSciNetCrossRefzbMATHGoogle Scholar
  17. 17.
    Westbrook, E., Ricken, M., Inoue, J., Yao, Y., Abdelatif, T., Taha, W.: Mint: Java multi-stage programming using weak separability. In: PLDI 2010. ACM, New York (2010)Google Scholar
  18. 18.
    Xi, H., Chen, C., Chen, G.: Guarded recursive datatype constructors. In: POPL [11], pp. 224–235Google Scholar

Copyright information

© Springer International Publishing AG 2016

Authors and Affiliations

  1. 1.Tohoku UniversitySendaiJapan
  2. 2.University of TsukubaTsukubaJapan

Personalised recommendations