The Design and Implementation of BER MetaOCaml

System Description
  • Oleg Kiselyov
Part of the Lecture Notes in Computer Science book series (LNCS, volume 8475)


MetaOCaml is a superset of OCaml extending it with the data type for program code and operations for constructing and executing such typed code values. It has been used for compiling domain-specific languages and automating tedious and error-prone specializations of high-performance computational kernels. By statically ensuring that the generated code compiles and letting us quickly run it, MetaOCaml makes writing generators less daunting and more productive.

The current BER MetaOCaml is a complete re-implementation of the original MetaOCaml by Taha, Calcagno and collaborators. Besides the new organization, new algorithms, new code, BER MetaOCaml adds a scope extrusion check superseding environment classifiers. Attempting to build code values with unbound or mistakenly bound variables (liable to occur due to mutation or other effects) is now caught early, raising an exception with good diagnostics. The guarantee that the generated code always compiles becomes unconditional, no matter what effects were used in generating the code.

We describe BER MetaOCaml stressing the design decisions that made the new code modular and maintainable. We explain the implementation of the scope extrusion check.


Free Variable Open Code Type Checker Code Type Innermost Loop 
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.


Unable to display preview. Download preview PDF.

Unable to display preview. Download preview PDF.


  1. 1.
    Swadi, K., Taha, W., Kiselyov, O., Pašalić, E.: A monadic approach for avoiding code duplication when staging memoized functions. In: PEPM, pp. 160–169 (2006)Google Scholar
  2. 2.
    Carette, J., Kiselyov, O.: Multi-stage programming with functors and monads: Eliminating abstraction overhead from generic code. Science of Computer Programming 76, 349–375 (2011)CrossRefzbMATHMathSciNetGoogle Scholar
  3. 3.
    Kiselyov, O., Taha, W.: Relating FFTW and split-radix. In: Wu, Z., Chen, C., Guo, M., Bu, J. (eds.) ICESS 2004. LNCS, vol. 3605, pp. 488–493. Springer, Heidelberg (2005)CrossRefGoogle Scholar
  4. 4.
    Lengauer, C., Taha, W. (eds.) MetaOCaml Workshop 2004, Special Issue on the 1st MetaOCaml Workshop (2004); 62(1) of Science of Computer Programming (2006)Google Scholar
  5. 5.
    Calcagno, C., Taha, W., Huang, L., Leroy, X.: Implementing multi-stage languages using ASTs, gensym, and reflection. In: Pfenning, F., Macko, M. (eds.) GPCE 2003. LNCS, vol. 2830, pp. 57–76. Springer, Heidelberg (2003)CrossRefGoogle Scholar
  6. 6.
    Kiselyov, O.: BER MetaOCaml N101 (2013),
  7. 7.
    Calcagno, C., Moggi, E., Taha, W.: ML-like inference for classifiers. In: Schmidt, D. (ed.) ESOP 2004. LNCS, vol. 2986, pp. 79–93. Springer, Heidelberg (2004)CrossRefGoogle Scholar
  8. 8.
    Kiselyov, O., Swadi, K.N., Taha, W.: A methodology for generating verified combinatorial circuits. In: EMSOFT, pp. 249–258 (2004)Google Scholar
  9. 9.
    Kiselyov, O.: Modular, convenient, assured domain-specific optimizations: Can generative programming deliver? Poster at APLAS (2012),
  10. 10.
    Kameyama, Y., Kiselyov, O., Shan, C.-c.: Shifting the stage: Staging with delimited control. Journal of Functional Programming 21, 617–662 (2011)Google Scholar
  11. 11.
    Taha, W., Nielsen, M.F.: Environment classifiers. In: POPL, pp. 26–37 (2003)Google Scholar
  12. 12.
    Bawden, A.: Quasiquotation in Lisp. In: PEPM. Number NS-99-1 in Note, pp. 4–12. BRICS (1999)Google Scholar
  13. 13.
    Herman, D.: A Theory of Typed Hygienic Macros. PhD thesis, Northeastern University, Boston, MA (2010)Google Scholar
  14. 14.
    Sheard, T., Peyton Jones, S.L.: Template meta-programming for Haskell. In: Chakravarty, M.M.T. (ed.) Haskell Workshop, pp. 1–16 (2002)Google Scholar
  15. 15.
    Thiemann, P., Dussart, D.: Partial evaluation for higher-order languages with state (1999),
  16. 16.
    Rompf, T., Amin, N., Moors, A., Haller, P., Odersky, M.: Scala-Virtualized: linguistic reuse for deep embeddings. Higher-Order and Symbolic Computation (2013)Google Scholar
  17. 17.
    Rompf, T., Odersky, M.: Lightweight modular staging: a pragmatic approach to runtime code generation and compiled DSLs. Commun. ACM 55, 121–130 (2012)CrossRefGoogle Scholar

Copyright information

© Springer International Publishing Switzerland 2014

Authors and Affiliations

  • Oleg Kiselyov
    • 1
  1. 1.University of TsukubaJapan

Personalised recommendations