skip to main content
research-article

Explicitly heterogeneous metaprogramming with MetaHaskell

Published:09 September 2012Publication History
Skip Abstract Section

Abstract

Languages with support for metaprogramming, like MetaOCaml, offer a principled approach to code generation by guaranteeing that well-typed metaprograms produce well-typed programs. However, many problem domains where metaprogramming can fruitfully be applied require generating code in languages like C, CUDA, or assembly. Rather than resorting to add-hoc code generation techniques, these applications should be directly supported by explicitly heterogeneous metaprogramming languages.

We present MetaHaskell, an extension of Haskell 98 that provides modular syntactic and type system support for type safe metaprogramming with multiple object languages. Adding a new object language to MetaHaskell requires only minor modifications to the host language to support type-level quantification over object language types and propagation of type equality constraints. We demonstrate the flexibility of our approach through three object languages: a core ML language, a linear variant of the core ML language, and a subset of C. All three languages support metaprogramming with open terms and guarantee that well-typed MetaHaskell programs will only produce closed object terms that are well-typed. The essence of MetaHaskell is captured in a type system for a simplified metalanguage. MetaHaskell, as well as all three object languages, are fully implemented in the mhc bytecode compiler.

References

  1. Robert Atkey, Sam Lindley, and Jeremy Yallop. Unembedding domain-specific languages. In Proceedings of the 2nd ACM SIGPLAN Symposium on Haskell (Haskell '09), pages 37--48, Edinburgh, Scotland, 2009. ACM. Google ScholarGoogle ScholarDigital LibraryDigital Library
  2. Franz Baader and Tobias Nipkow. Term Rewriting and All That. Cambridge University Press, 1998. Google ScholarGoogle ScholarDigital LibraryDigital Library
  3. Gershom Bazerman. jmacro, jul 2011.Google ScholarGoogle Scholar
  4. Cristiano Calcagno, Eugenio Moggi, and Tim Sheard. Closed types for a safe imperative MetaML. Journal of Functional Programming, 13 (03): 545--571, 2003. Google ScholarGoogle ScholarDigital LibraryDigital Library
  5. Cristiano Calcagno, Eugenio Moggi, and Walid Taha. ML-Like inference for classifiers. In In European Symposium on Programming (ESOP '04), volume 2986 of Lecture Notes in Computer Science, pages 79--93, 2004.Google ScholarGoogle ScholarCross RefCross Ref
  6. Jacques Carette. Gaussian elimination: A case study in efficient genericity with MetaOCaml. Science of Computer Programming, 62 (1): 3--24, sep 2006. Google ScholarGoogle ScholarDigital LibraryDigital Library
  7. P. P. Chang and W.-W. Hwu. Inline function expansion for compiling c programs. In Proceedings of the ACM SIGPLAN 1989 Conference on Programming language design and implementation, PLDI '89, page 246--257, New York, NY, USA, 1989. ACM. ISBN 0-89791-306-X. Google ScholarGoogle ScholarDigital LibraryDigital Library
  8. Chiyan Chen and Hongwei Xi. Meta-Programming through typeful code representation. Journal of Functional Programming, 15 (06): 797--835, 2005. Google ScholarGoogle ScholarDigital LibraryDigital Library
  9. R. Davies. A temporal-logic approach to binding-time analysis. In Proceedings of the 11th Annual IEEE Symposium on Logic in Computer Science, page 184. IEEE Computer Society, 1996. Google ScholarGoogle ScholarDigital LibraryDigital Library
  10. Rowan Davies and Frank Pfenning. A modal analysis of staged computation. In Proceedings of the 23rd ACM SIGPLAN-SIGACT symposium on Principles of programming languages, pages 258--270, St. Petersburg Beach, Florida, United States, 1996. ACM. Google ScholarGoogle ScholarDigital LibraryDigital Library
  11. Rowan Davies and Frank Pfenning. A modal analysis of staged computation. Journal of the ACM (JACM), 48: 555--604, may 2001. ACM ID: 382785. Google ScholarGoogle ScholarDigital LibraryDigital Library
  12. Edsko de Vries. Making Uniqueness Typing Less Unique. PhD thesis, Trinity College, Dublin, Ireland, 2008.Google ScholarGoogle Scholar
  13. Jason Eckhardt, Roumen Kaiabachev, Emir Pašalić, Kedar Swadi, and Walid Taha. Implicitly heterogeneous multi-stage programming. New Gen. Comput., 25 (3): 305--336, 2007. Google ScholarGoogle ScholarDigital LibraryDigital Library
  14. Matteo Frigo and Steven G. Johnson. The design and implementation of FFTW3. Proceedings of the IEEE, 93 (2): 216--231, 2005. Special issue on "Program Generation, Optimization, and Platform Adaptation".Google ScholarGoogle ScholarCross RefCross Ref
  15. Hideyuki Tanaka. peggy, feb 2012.Google ScholarGoogle Scholar
  16. Trevor Jim. What are principal typings and what are they good for? In Proceedings of the 23rd ACM SIGPLAN-SIGACT symposium on Principles of programming languages, pages 42--53, St. Petersburg Beach, Florida, United States, 1996. ACM. Google ScholarGoogle ScholarDigital LibraryDigital Library
  17. Yukiyoshi Kameyama, Oleg Kiselyov, and Chung-chieh Shan. Closing the stage: from staged code to typed closures. In Proceedings of the 2008 ACM SIGPLAN symposium on Partial evaluation and semantics-based program manipulation, pages 147--157, San Francisco, California, USA, 2008. ACM. Google ScholarGoogle ScholarDigital LibraryDigital Library
  18. Yukiyoshi Kameyama, Oleg Kiselyov, and Chung-Chieh Shan. Shifting the stage: staging with delimited control. Journal of Functional Programming, 21 (06): 617--662, 2011. Google ScholarGoogle ScholarDigital LibraryDigital Library
  19. Andrew J. Kennedy. Type inference and equational theories. Technical Report LIX/RR/96/09, LIX, Ecole Polytechnique, 91128 Palaiseau Cedex, France, sep 1996.Google ScholarGoogle Scholar
  20. Ik-Soon Kim, Kwangkeun Yi, and Cristiano Calcagno. A polymorphic modal type system for lisp-like multi-staged languages. In Conference record of the 33rd ACM SIGPLAN-SIGACT symposium on Principles of programming languages, pages 257--268, Charleston, South Carolina, USA, 2006. ACM. Google ScholarGoogle ScholarDigital LibraryDigital Library
  21. Oleg Kiselyov and Walid Taha. Relating FFTW and Split-Radix. In Embedded Software and Systems, pages 488--493. 2005. Google ScholarGoogle ScholarDigital LibraryDigital Library
  22. Geoffrey Mainland. Why it's nice to be quoted: Quasiquoting for haskell. In Haskell '07: Proceedings of the ACM SIGPLAN Workshop on Haskell, page 73--82, New York, NY, USA, 2007. ACM. Google ScholarGoogle ScholarDigital LibraryDigital Library
  23. Geoffrey Mainland. language-c-quote, 2010.Google ScholarGoogle Scholar
  24. Geoffrey Mainland and Greg Morrisett. Nikola: embedding compiled GPU functions in haskell. In Proceedings of the third ACM Haskell symposium on Haskell, pages 67--78, Baltimore, Maryland, USA, 2010. ACM. Google ScholarGoogle ScholarDigital LibraryDigital Library
  25. Geoffrey Mainland, Greg Morrisett, and Matt Welsh. Flask: Staged functional programming for sensor networks. In Proceeding of the 13th ACM SIGPLAN International Conference on Functional Programming (ICFP '08), page 335--346, New York, NY, USA, 2008. ACM. Google ScholarGoogle ScholarDigital LibraryDigital Library
  26. Urusula Martin and Tobias Nipkow. Boolean unification - The story so far. Journal of Symbolic Computation, 7 (3-4): 275--293, apr 1989. Google ScholarGoogle ScholarDigital LibraryDigital Library
  27. Aleksandar Nanevski. Meta-programming with names and necessity. In Proceedings of the seventh ACM SIGPLAN International Conference on Functional Programming, pages 206--217, Pittsburgh, PA, USA, 2002. ACM. Google ScholarGoogle ScholarDigital LibraryDigital Library
  28. Aleksandar Nanevski, Frank Pfenning, and Brigitte Pientka. Contextual modal type theory. ACM Transactions on Computational Logic (TOCL), 9: 23:1--23:49, jun 2008. ACM ID: 1352591. Google ScholarGoogle ScholarDigital LibraryDigital Library
  29. Markus Püschel, José M. F. Moura, Bryan Singer, Jianxin Xiong, Jeremy Johnson, David Padua, Manuela Veloso, and Robert W. Johnson. Spiral: A generator for Platform-Adapted libraries of signal processing alogorithms. International Journal of High Performance Computing Applications, 18 (1): 21--45, feb 2004. Google ScholarGoogle ScholarDigital LibraryDigital Library
  30. Didier Rémy. Type inference for records in natural extension of ML. Research Report 1431, Institut National de Recherche en Informatique et Automatisme, 1991.Google ScholarGoogle Scholar
  31. Didier Rémy. Syntactic theories and the algebra of record terms. Research Report 1869, Institut National de Recherche en Informatique et Automatisme, Rocquencourt, BP 105, 78 153 Le Chesnay Cedex, France, 1993.Google ScholarGoogle Scholar
  32. Morten Rhiger. First-class open and closed code fragments. In Proceedings of the Sixth Symposium on Trends in Functional Programming, 2005.Google ScholarGoogle Scholar
  33. Zhong Shao and Andrew W. Appel. Smartest recompilation. In Proceedings of the 20th ACM SIGPLAN-SIGACT symposium on Principles of programming languages, pages 439--450, Charleston, South Carolina, United States, 1993. ACM. Google ScholarGoogle ScholarDigital LibraryDigital Library
  34. Kedar Swadi, Walid Taha, Oleg Kiselyov, and Emir Pasalic. A monadic approach for avoiding code duplication when staging memoized functions. In Proceedings of the 2006 ACM SIGPLAN symposium on Partial evaluation and semantics-based program manipulation, PEPM '06, page 160--169, New York, NY, USA, 2006. ACM. ACM ID: 1111570. Google ScholarGoogle ScholarDigital LibraryDigital Library
  35. Walid Taha. A gentle introduction to multi-stage programming. In Christian Lengauer, Don S. Batory, Charles Consel, and Martin Odersky, editors, Domain-Specific Program Generation, volume 3016 of Lecture Notes in Computer Science, page 30--50. Springer, 2003.Google ScholarGoogle Scholar
  36. Walid Taha and Michael Florentin Nielsen. Environment classifiers. In Proceedings of the 30th ACM SIGPLAN-SIGACT symposium on Principles of programming languages, POPL '03, page 26--37, New York, NY, USA, 2003. ACM. Google ScholarGoogle ScholarDigital LibraryDigital Library
  37. Walid Taha and Tim Sheard. Multi-stage programming with explicit annotations. In Proceedings of the 1997 ACM SIGPLAN symposium on Partial Evaluation and Semantics-Based Program Manipulation (PEPM '97), pages 203--217, Amsterdam, The Netherlands, 1997. ACM. Google ScholarGoogle ScholarDigital LibraryDigital Library
  38. Philip Wadler. Is there a use for linear logic? In Proceedings of the 1991 ACM SIGPLAN symposium on Partial evaluation and semantics-based program manipulation, PEPM '91, page 255--273, New York, NY, USA, 1991. ACM. ISBN 0-89791-433-3. Google ScholarGoogle ScholarDigital LibraryDigital Library
  39. Stephanie Weirich, Dimitrios Vytiniotis, Simon L. Peyton Jones, and Steve Zdancewic. Generative type abstraction and type-level computation. In Proceedings of the 38th ACM SIGPLAN-SIGACT symposium on Principles of programming languages (POPL '11), Austin, TX, 2011. Google ScholarGoogle ScholarDigital LibraryDigital Library
  40. R. Clint Whaley and Antoine Petitet. Minimizing development and maintenance costs in supporting persistently optimized BLAS. Softw. Pract. Exper., 35 (2): 101--121, 2005. Google ScholarGoogle ScholarDigital LibraryDigital Library
  41. Hongwei Xi, Chiyan Chen, and Gang Chen. Guarded recursive datatype constructors. In Proceedings of the 30th ACM SIGPLAN-SIGACT symposium on Principles of programming languages - POPL '03, pages 224--235, New Orleans, Louisiana, USA, 2003. Google ScholarGoogle ScholarDigital LibraryDigital Library

Index Terms

  1. Explicitly heterogeneous metaprogramming with MetaHaskell

    Recommendations

    Comments

    Login options

    Check if you have access through your login credentials or your institution to get full access on this article.

    Sign in

    Full Access

    • Published in

      cover image ACM SIGPLAN Notices
      ACM SIGPLAN Notices  Volume 47, Issue 9
      ICFP '12
      September 2012
      368 pages
      ISSN:0362-1340
      EISSN:1558-1160
      DOI:10.1145/2398856
      Issue’s Table of Contents
      • cover image ACM Conferences
        ICFP '12: Proceedings of the 17th ACM SIGPLAN international conference on Functional programming
        September 2012
        392 pages
        ISBN:9781450310543
        DOI:10.1145/2364527

      Copyright © 2012 ACM

      Publisher

      Association for Computing Machinery

      New York, NY, United States

      Publication History

      • Published: 9 September 2012

      Check for updates

      Qualifiers

      • research-article

    PDF Format

    View or Download as a PDF file.

    PDF

    eReader

    View online with eReader.

    eReader
    About Cookies On This Site

    We use cookies to ensure that we give you the best experience on our website.

    Learn more

    Got it!