Implementing Multi-stage Languages Using ASTs, Gensym, and Reflection

  • Cristiano Calcagno
  • Walid Taha
  • Liwen Huang
  • Xavier Leroy
Part of the Lecture Notes in Computer Science book series (LNCS, volume 2830)

Abstract

The paper addresses theoretical and practical aspects of implementing multi-stage languages using abstract syntax trees (ASTs), gensym, and reflection. We present an operational account of the correctness of this approach, and report on our experience with a bytecode compiler called MetaOCaml that is based on this strategy. Current performance measurements reveal interesting characteristics of the underlying OCaml compiler, and illustrate why this strategy can be particularly useful for implementing domain-specific languages in a typed, functional setting.

Preview

Unable to display preview. Download preview PDF.

Unable to display preview. Download preview PDF.

References

  1. 1.
    Bawden, A.: Quasiquotation in LISP. In: Danvy, O. (ed.) Proceedings of the Workshop on Partial Evaluation and Semantics-Based Program Manipulation, San Antonio, pp. 88–99. University of Aarhus, Dept. of Computer Science (1999) (invited talk)Google Scholar
  2. 2.
    Benaissa, Z.E.-A., Moggi, E., Taha, W., Sheard, T.: Logical modalities and multi-stage programming. In: Federated Logic Conference (FLoC) Satellite Workshop on Intuitionistic Modal Logics and Applications (IMLA) (1999)Google Scholar
  3. 3.
    Calcagno, C., Moggi, E.: Adequacy and correctness for two-level languages (1998) (unpublished manuscript)Google Scholar
  4. 4.
    Calcagno, C., Moggi, E.: Multi-stage imperative languages: A conservative extension result. In: [41], pp. 92–107 (2000)Google Scholar
  5. 5.
    Calcagno, C., Moggi, E., Sheard, T.: Closed types for a safe imperative MetaML. Journal of Functional Programming (2003) (to appear)Google Scholar
  6. 6.
    Calcagno, C., Moggi, E., Taha, W.: Closed types as a simple approach to safe imperative multi-stage programming. In: Welzl, E., Montanari, U., Rolim, J.D.P. (eds.) ICALP 2000. LNCS, vol. 1853, pp. 25–36. Springer, Heidelberg (2000)CrossRefGoogle Scholar
  7. 7.
    Clinger, W., Rees, J.: Macros that work. In: Proceedings of the ACM Symposium on Principles of Programming Languages (POPL), Orlando, pp. 155–162. ACM Press, New York (1991)Google Scholar
  8. 8.
    Consel, C., Noël, F.: A general approach for run-time specialization and its application to C. In. In: Proceedings of the ACM Symposium on Principles of Programming Languages (POPL), St. Petersburg Beach, pp. 145–156 (1996)Google Scholar
  9. 9.
    Consel, C., Pu, C., Walpole, J.: Incremental specialization: The key to high performance, modularity, and portability in operating systems. In: Proceedings of the Symposium on Partial Evaluation and Semantics-Based Program Manipulation, New York, pp. 44–46. ACM Press, New York (1993)Google Scholar
  10. 10.
    Danvy, O., Malmkjær, K.: Intensions and extensions in a reflective tower. In: Proceedings of the 1988 ACM Conference on LISP and Functional Programming, pp. 327–341. ACM Press, New York (1988)CrossRefGoogle Scholar
  11. 11.
    Davies, R.: A temporal-logic approach to binding-time analysis. In: The Symposium on Logic in Computer Science (LICS 1996), New Brunswick, pp. 184–195. IEEE Computer Society Press, Los Alamitos (1996)Google Scholar
  12. 12.
    Davies, R., Pfenning, F.: A modal analysis of staged computation. In: The Symposium on Principles of Programming Languages (POPL 1996), St. Petersburg Beach, pp. 258–270 (1996)Google Scholar
  13. 13.
    Dybvig, R.K., Hieb, R., Bruggeman, C.: Syntactic abstraction in Scheme. Lisp and Symbolic Computation 5(4), 295–326 (1992)CrossRefGoogle Scholar
  14. 14.
    Filinski, A.: A semantic account of type-directed partial evaluation. In: Nadathur, G. (ed.) PPDP 1999. LNCS, vol. 1702, pp. 378–395. Springer, Heidelberg (1999)CrossRefGoogle Scholar
  15. 15.
    Filinski, A.: Normalization by evaluation for the computational lambda-calculus. In: Abramsky, S. (ed.) TLCA 2001. LNCS, vol. 2044, pp. 151–165. Springer, Heidelberg (2001)CrossRefGoogle Scholar
  16. 16.
    Ganz, S., Sabry, A., Taha, W.: Macros as multi-stage computations: Typesafe, generative, binding macros in MacroML. In: The International Conference on Functional Programming (ICFP 2001), Florence, Italy. ACM, New York (2001)Google Scholar
  17. 17.
    Glück, R., Jørgensen, J.: Efficient multi-level generating extensions for program specialization. In: Swierstra, S.D., Hermenegildo, M. (eds.) PLILP 1995. LNCS, vol. 982, pp. 259–278. Springer, Heidelberg (1995)CrossRefGoogle Scholar
  18. 18.
    Glück, R., Jørgensen, J.: Fast binding-time analysis for multi-level specialization. In: Bjorner, D., Broy, M., Pottosin, I.V. (eds.) PSI 1996. LNCS, vol. 1181, pp. 261–272. Springer, Heidelberg (1996)Google Scholar
  19. 19.
    Glück, R., Nakashige, R., Zöchling, R.: Binding-time analysis applied to mathematical algorithms. In: Doležal, J., Fidler, J. (eds.) System Modelling and Optimization, pp. 137–146. Chapman & Hall, Boca Raton (1995)Google Scholar
  20. 20.
    Gomard, C.K., Jones, N.D.: A partial evaluator for untyped lambda calculus. Journal of Functional Programming 1(1), 21–69 (1991)MATHCrossRefMathSciNetGoogle Scholar
  21. 21.
    Hornof, L., Jim, T.: Certifying compilation and run-time code generation. Higher-Order and Symbolic Computation 12(4), 337–375 (1999)MATHCrossRefGoogle Scholar
  22. 22.
    Jones, N.D., Gomard, C.K., Sestoft, P.: Partial Evaluation and Automatic Program Generation. Prentice-Hall, Englewood Cliffs (1993)MATHGoogle Scholar
  23. 23.
    Jones, N.D., Sestoft, P., Sondergraard, H.: An experiment in partial evaluation: The generation of a compiler generator. In: Jouannaud, J.-P. (ed.) RTA 1985. LNCS, vol. 202, pp. 124–140. Springer, Heidelberg (1985)Google Scholar
  24. 24.
    Keppel, D., Eggers, S.J., Henry, R.R.: A case for runtime code generation. Tech. Rep. 91-11-04, University of Washington (1991)Google Scholar
  25. 25.
    Kolhlbecker, E.E.: Syntactic Extensions in the Programming Language Lisp. PhD thesis, Indiana University, Bloomington, Indiana (1986)Google Scholar
  26. 26.
    Leroy, X.: Objective Caml (2000), Available from http://caml.inria.fr/ocaml/
  27. 27.
    Malmkjær, K.: On some semantic issues in the reflective tower. In: Schmidt, D.A., Main, M.G., Melton, A.C., Mislove, M.W. (eds.) MFPS 1989. LNCS, vol. 442, pp. 229–246. Springer, Heidelberg (1990)CrossRefGoogle Scholar
  28. 28.
    Masuhara, H., Yonezawa, A.: Run-time bytecode specialization. In: Danvy, O., Filinski, A. (eds.) PADO 2001. LNCS, vol. 2053, p. 138. Springer, Heidelberg (2001)CrossRefGoogle Scholar
  29. 29.
    MetaOCaml: A compiled, type-safe multi-stage programming language (2003), Available online from http://www.cs.rice.edu/~taha/MetaOCaml/
  30. 30.
    Moggi, E.: A categorical account of two-level languages. In: Mathematics Foundations of Program Semantics, Elsevier Science, Amsterdam (1997)Google Scholar
  31. 31.
    Moggi, E.: Functor categories and two-level languages. In: Nivat, M. (ed.) FOSSACS 1998. LNCS, vol. 1378, p. 211. Springer, Heidelberg (1998)CrossRefGoogle Scholar
  32. 32.
    Moggi, E., Taha, W., Benaissa, Z.E.-A., Sheard, T.: An idealized MetaML: Simpler, and more expressive. In: Swierstra, S.D. (ed.) ESOP 1999. LNCS, vol. 1576, pp. 193–207. Springer, Heidelberg (1999)CrossRefGoogle Scholar
  33. 33.
    Multi-stage programming (2003), http://www.cs.rice.edu/~taha/teaching/02F/511
  34. 34.
    Noël, F., Hornof, L., Consel, C., Lawall, J.L.: Automatic, templatebased run-time specialization: Implementation and experimental study. In: Proceedings of the 1998 International Conference on Computer Languages, pp. 132–142. IEEE Computer Society Press, Los Alamitos (1998)Google Scholar
  35. 35.
    Oregon Graduate Institute Technical Reports. P.O. Box 91000, Portland, OR 97291-1000,USA, Last viewed (August 1999), Available online, from ftp://cse.ogi.edu/pub/tech-reports/README.html
  36. 36.
    Pu, C., Walpole, J.: A study of dynamic optimization techniques: Lessons and directions in kernel design. Tech. Rep. CSE-93-007, Oregon Graduate Institute (1993), Available from [35]Google Scholar
  37. 37.
    Smith, B.C.: Reflection and Semantics in a Procedural Language. PhD thesis, Massachusetts Institute of Technology (1982)Google Scholar
  38. 38.
    Smith, B.C.: Reflection and semantics in LISP. In: ACM Symposium on Principles of Programming Languages, pp. 23–35 (1984)Google Scholar
  39. 39.
    Smith, F., Grossman, D., Morrisett, G., Hornof, L., Jim, T.: Compiling for template-based run-time code generation. Journal of Functional Programming (2002) ( to appear)Google Scholar
  40. 40.
    Taha, W.: Multi-Stage Programming: Its Theory and Applications. PhD thesis, Oregon Graduate Institute of Science and Technology (1999); Available from [35]Google Scholar
  41. 41.
    Taha, W. (ed.): Semantics, Applications, and Implementation of Program Generation. LNCS, vol. 1924. Springer, Heidelberg (2000)MATHGoogle Scholar
  42. 42.
    Taha, W.: A sound reduction semantics for untyped CBN multi-stage computation. Or, the theory of MetaML is non-trivial. In: Proceedings of the Workshop on Partial Evaluation and Semantics-Based Program Maniplation (PEPM), Boston. ACM Press, New York (2000)Google Scholar
  43. 43.
    Taha, W., Benaissa, Z.-E.-A., Sheard, T.: Multi-stage programming: Axiomatization and type-safety. In: Larsen, K.G., Skyum, S., Winskel, G. (eds.) ICALP 1998. LNCS, vol. 1443, pp. 918–929. Springer, Heidelberg (1998)CrossRefGoogle Scholar
  44. 44.
    Taha, W., Makholm, H., Hughes, J.: Tag elimination and Jones-optimality. In: Danvy, O., Filinski, A. (eds.) PADO 2001. LNCS, vol. 2053, pp. 257–275. Springer, Heidelberg (2001)CrossRefGoogle Scholar
  45. 45.
    Taha, W., Nielsen, M.F.: Environment classifiers. In: The Symposium on Principles of Programming Languages (POPL 2003), New Orleans (2003)Google Scholar
  46. 46.
    Taha, W., Sheard, T.: Multi-stage programming with explicit annotations. In: Proceedings of the Symposium on Partial Evaluation and Semantic-Based Program Manipulation (PEPM), Amsterdam, pp. 203–217. ACM Press, New York (1997)CrossRefGoogle Scholar
  47. 47.
    Taha, W., Sheard, T.: MetaML and multi-stage programming with explicit annotations. Tech. Rep. CSE-99-007, Department of Computer Science, Oregon Graduate Institute (1999); Extended version of [46], Available from [35]Google Scholar
  48. 48.
    Taha, W., Sheard, T.: MetaML: Multi-stage programming with explicit annotations. Theoretical Computer Science 248, 1–2 (2000); Revision of [47]Google Scholar
  49. 49.
    Thibault, S., Consel, C., Lawall, J.L., Marlet, R., Muller, G.: Static and dynamic program compilation by interpreter specialization. Higher-Order and Symbolic Computation 13(3), 161–178 (2000)MATHCrossRefGoogle Scholar

Copyright information

© Springer-Verlag Berlin Heidelberg 2003

Authors and Affiliations

  • Cristiano Calcagno
    • 1
  • Walid Taha
    • 2
  • Liwen Huang
    • 3
  • Xavier Leroy
    • 4
  1. 1.Imperial College LondonUK
  2. 2.Rice UniversityHoustonUSA
  3. 3.Yale UniversityNew HavenUSA
  4. 4.INRIARoquencourtFrance

Personalised recommendations