A Theory of Hygienic Macros

  • David Herman
  • Mitchell Wand
Part of the Lecture Notes in Computer Science book series (LNCS, volume 4960)

Abstract

Hygienic macro systems, such as Scheme’s, automatically rename variables to prevent unintentional variable capture—in short, they “just work.” Yet hygiene has never been formally presented as a specification rather than an algorithm. According to folklore, the definition of hygienic macro expansion hinges on the preservation of alpha-equivalence. But the only known notion of alpha-equivalence for programs with macros depends on the results of macro expansion! We break this circularity by introducing explicit binding specifications into the syntax of macro definitions, permitting a definition of alpha-equivalence independent of expansion. We define a semantics for a first-order subset of Scheme-like macros and prove hygiene as a consequence of confluence.

References

  1. 1.
    Kiselyov, O.: How to write seemingly unhygienic and referentially opaque macros with syntax-rules. In: Scheme Workshop (2002)Google Scholar
  2. 2.
    Kohlbecker, E., Friedman, D.P., Felleisen, M., Duba, B.: Hygienic macro expansion. In: LISP and Functional Programming (1986)Google Scholar
  3. 3.
    Clinger, W., Rees, J.: Macros that work. In: POPL (1991)Google Scholar
  4. 4.
    Bawden, A., Rees, J.: Syntactic closures. In: LISP and Functional Programming, pp. 86–95 (1988)Google Scholar
  5. 5.
    Kent Dybvig, R., Bruggeman, C.: Syntactic abstraction in Scheme. Lisp and Symbolic Computation 5(4), 295–326 (1993)CrossRefGoogle Scholar
  6. 6.
    van Tonder, A.: SRFI 72: Hygienic macros. Online (September 2005)Google Scholar
  7. 7.
    Clinger, W., Rees, J.: Revised4 report on the algorithmic language Scheme. Technical report (1991)Google Scholar
  8. 8.
    Culpepper, R., Felleisen, M.: Taming macros. In: Karsai, G., Visser, E. (eds.) GPCE 2004. LNCS, vol. 3286, Springer, Heidelberg (2004)Google Scholar
  9. 9.
    Gabbay, M.J., Pitts, A.M.: A new approach to abstract syntax with variable binding. Formal Aspects of Computing 13(3–5), 341–363 (2001)Google Scholar
  10. 10.
    Gorn, S.: Explicit definitions and linguistic dominoes. In: Systems and Computer Science, Proceedings of the Conference held at Univ. of Western Ontario (1967)Google Scholar
  11. 11.
    Barendregt, H.P.: The Lambda Calculus: Its Syntax and Semantics. revised edn. North-Holland, Amsterdam (1984)Google Scholar
  12. 12.
    Wright, A.K., Felleisen, M.: A syntactic approach to type soundness. Information and Computation 115(1), 38–94 (1994)MATHCrossRefMathSciNetGoogle Scholar
  13. 13.
    Flatt, M.: PLT MzScheme: Language manual. Technical Report PLT-TR2007-1-v371, PLT Scheme Inc. (2007), http://www.plt-scheme.org/techreports/
  14. 14.
    Krishnamurthi, S.: Linguistic Reuse. PhD thesis, Rice University (May 2001)Google Scholar
  15. 15.
    de Rauglaudre, D.: Camlp4 reference manual. Online (September 2003)Google Scholar
  16. 16.
    Sheard, T., Peyton Jones, S.: Template metaprogramming for Haskell. In: Chakravarty, M.M.T. (ed.) Haskell Workshop, pp. 1–16 (2002)Google Scholar
  17. 17.
    Ganz, S.E., Sabry, A., Taha, W.: Macros as multi-stage computations: Type-safe, generative, binding macros in MacroML. In: ICFP 2001, pp. 74–85 (2001)Google Scholar
  18. 18.
    Taha, W., Johann, P.: Staged notational definitions. In: Pfenning, F., Smaragdakis, Y. (eds.) GPCE 2003. LNCS, vol. 2830, pp. 97–116. Springer, Heidelberg (2003)Google Scholar
  19. 19.
    Gasbichler, M.: Fully-parameterized, first-class modules with hygienic macros. PhD thesis, University of Tübingen (August 2006)Google Scholar
  20. 20.
    Griffin, T.: Notational definition—a formal account. In: LICS 1988, pp. 372–383 (1988)Google Scholar
  21. 21.
    Bove, A., Arbilla, L.: A confluent calculus of macro expansion and evaluation. In: LISP and Functional Programming, pp. 278–287. ACM Press, New York (1992)CrossRefGoogle Scholar
  22. 22.
    McKinna, J., Pollack, R.: Some lambda calculus and type theory formalized. Journal of Automated Reasoning 23, 373–409 (1999)MATHCrossRefMathSciNetGoogle Scholar
  23. 23.
    Gordon, A.D.: A mechanisation of name-carrying syntax up to alpha-conversion. In: Workshop on HOL Theorem Proving and its Applications, pp. 413–425 (1994)Google Scholar
  24. 24.
    McBride, C., McKinna, J.: Functional pearl: I am not a number—I am a free variable. In: Haskell Workshop, pp. 1–9 (2004)Google Scholar
  25. 25.
    Kohlbecker, E.E., Wand, M.: Macro-by-example: Deriving syntactic transformations from their specifications. In: Principles of Programming Languages (1987)Google Scholar

Copyright information

© Springer-Verlag Berlin Heidelberg 2008

Authors and Affiliations

  • David Herman
    • 1
  • Mitchell Wand
    • 1
  1. 1.College of Computer and Information ScienceNortheastern UniversityBoston 

Personalised recommendations