Skip to main content

Multi-stage Programming with Functors and Monads: Eliminating Abstraction Overhead from Generic Code

  • Conference paper

Part of the book series: Lecture Notes in Computer Science ((LNPSE,volume 3676))

Abstract

With Gaussian Elimination as a representative family of numerical and symbolic algorithms, we use multi-stage programming, monads and Ocaml’s advanced module system to demonstrate the complete elimination of the abstraction overhead while avoiding any inspection of the generated code. We parameterize our Gaussian Elimination code to a great extent (over domain, matrix representations, determinant tracking, pivoting policies, result types, etc) at no run-time cost. Because the resulting code is generated just right and not changed afterwards, we enjoy MetaOCaml’s guaranty that the generated code is well-typed. We further demonstrate that various abstraction parameters (aspects) can be made orthogonal and compositional, even in the presence of name-generation for temporaries and other bindings and “interleaving” of aspects. We also show how to encode some domain-specific knowledge so that “clearly wrong” compositions can be statically rejected by the compiler when processing the generator rather than the generated code.

This is a preview of subscription content, log in via an institution.

Buying options

Chapter
USD   29.95
Price excludes VAT (USA)
  • Available as PDF
  • Read on any device
  • Instant download
  • Own it forever
eBook
USD   39.99
Price excludes VAT (USA)
  • Available as PDF
  • Read on any device
  • Instant download
  • Own it forever
Softcover Book
USD   54.99
Price excludes VAT (USA)
  • Compact, lightweight edition
  • Dispatched in 3 to 5 business days
  • Free shipping worldwide - see info

Tax calculation will be finalised at checkout

Purchases are for personal use only

Learn about institutional subscriptions

Preview

Unable to display preview. Download preview PDF.

Unable to display preview. Download preview PDF.

References

  1. Bondorf, A.: Improving binding times without explicit CPS-conversion. In: 1992 ACM Conference on Lisp and Functional Programming, San Francisco, California, pp. 1–10 (1992)

    Google Scholar 

  2. Calcagno, C., Taha, W., Huang, L., Leroy, X.: Implementing multi-stage languages using asts, gensym, and reflection. In: Czarnecki, K., Pfenning, F., Smaragdakis, Y. (eds.) GPCE 2003. LNCS, vol. 2830, pp. 57–76. Springer, Heidelberg (2003)

    Chapter  Google Scholar 

  3. Carette, J.: Gaussian Elimination: a case study in efficient genericity with MetaOCaml (2005) (submitted)

    Google Scholar 

  4. Chen, Z., Dongarra, J., Luszczek, P., Rothe, K.: Lapack for clusters project: An example of self adapting numerical software. In: Hawaii International Conference on System Sciences HICSS-37 (2004)

    Google Scholar 

  5. Source code, http://www.cas.mcmaster.ca/~carette/metamonads/

  6. Cohen, A., Donadio, S., Garzara’n, M.J., Herrmann, C., Padua, D. In Search for a program generator to implement generic transformations for high-performance computing. In: MetaOCaml Workshop (October 2004)

    Google Scholar 

  7. Czarnecki, K., Eisenecker, U.W.: Generative programming: methods, tools, and applications. ACM Press/Addison-Wesley Publishing Co. (2000)

    Google Scholar 

  8. Czarnecki, K., O’Donnell, J.T., Striegnitz, J., Taha, W.: DSL implementation in MetaOCaml, Template Haskell, and C++. In: Lengauer, C., Batory, D.S., Consel, C., Odersky, M. (eds.) Domain-Specific Program Generation. LNCS, vol. 3016, pp. 51–72. Springer, Heidelberg (2004)

    Chapter  Google Scholar 

  9. de Rauglaudre, D.: Camlp4 reference manual (January 2002), http://caml.inria.fr/camlp4/manual/

  10. Dijkstra, E.W.: On the role of scientific thought. Published as [11]

    Google Scholar 

  11. Dijkstra, E.W.: On the role of scientific thought. In: Selected Writings on Computing: A Personal Perspective, pp. 60–66. Springer, Heidelberg (1982)

    Google Scholar 

  12. Eckhardt, J.L., Kaiabachev, R., Swadi, K.N., Taha, W., Kiselyov, O.: Practical aspects of multi-stage programming. Rice University Techical Report TR05-451 (2004), http://www.cs.rice.edu/~taha/publications/preprints/2004-02-16.pdf (February 2004)

  13. Filinski, A.: Representing monads. In: POPL, pp. 446–457 (1994)

    Google Scholar 

  14. Glück, R., Jørgensen, J.: An automatic program generator for multi-level specialization. Lisp and Symbolic Computation 10(2), 113–158 (1997)

    Article  Google Scholar 

  15. Glück, R., Nakashige, R., Zöchling, R.: Binding-time analysis applied to mathematical algorithms. In: System Modelling and Optimization (1995)

    Google Scholar 

  16. Jenks, R.D., Sutor, R.S.: AXIOM: The Scientific Computation System. Springer, Heidelberg (1992)

    MATH  Google Scholar 

  17. Reynders III, J.V.W., Cummings, J.C.: The POOMA framework. Comput. Phys. 12(5), 453–459 (1998)

    Article  Google Scholar 

  18. Kennedy, K., Broom, B., Cooper, K., Dongarra, J., Fowler, R., Gannon, D., Johnsson, L., Mellor-Crummey, J., Torczon, L.: Telescoping languages: A strategy for automatic generation of scientific problem-solving systems from annotated libraries. Journal of Parallel and Distributed Computing 61(12), 1803–1826 (2001)

    Article  MATH  Google Scholar 

  19. Kiczales, G., Lamping, J., Menhdhekar, A., Maeda, C., Lopes, C., Loingtier, J.-M., Irwin, J.: Aspect-oriented programming. In: Aksit, M., Matsuoka, S. (eds.) ECOOP 1997. LNCS, vol. 1241, pp. 220–242. Springer, Heidelberg (1997)

    Chapter  Google Scholar 

  20. Kiselyov, O., Swadi, K.N., Taha, W.: A methodology for generating verified combinatorial circuits. In: EMSOFT 2004: Proceedings of the fourth ACM international conference on Embedded software, pp. 249–258. ACM Press, New York (2004)

    Chapter  Google Scholar 

  21. Kohlbecker, E.E., Friedman, D.P., Felleisen, M., Duba, B.F.: Hygienic macro expansion. In: LISP and Functional Programming, pp. 151–161 (1986)

    Google Scholar 

  22. Liang, S., Hudak, P., Jones, M.: Monad transformers and modular interpreters. In: POPL 1995: Conference Record of the Annual ACM Symposium on Principles of Programming Languages, pp. 333–343. ACM Press, New York (1995)

    Google Scholar 

  23. MetaOCaml, http://www.metaocaml.org

  24. Moggi, E.: Notions of computation and monads. Information and Computation 93(1), 55–92 (1991)

    Article  MATH  MathSciNet  Google Scholar 

  25. Musser, D.R., Stepanov, A.A.: Algorithm-oriented generic libraries. Software - Practice and Experience 24(7), 623–642 (1994)

    Article  Google Scholar 

  26. Parnas, D.L.: On the criteria to be used in decomposing systems into modules. Commun. ACM 15(12), 1053–1058 (1972)

    Article  Google Scholar 

  27. Jones, S.P., et al.: The Revised Haskell 1998 Report. Cambridge Univ. Press, Cambridge (2003), Also on: http://haskell.org/

    Google Scholar 

  28. Püschel, M., Moura, J.M.F., Johnson, J., Padua, D., Veloso, M., Singer, B.W., Xiong, J., Franchetti, F., Gačić, A., Voronenko, Y., Chen, K., Johnson, R.W., Rizzolo, N.: SPIRAL: Code generation for DSP transforms. In: Proceedings of the IEEE, special issue on Program Generation, Optimization, and Adaptation, vol. 93(2) (2005)

    Google Scholar 

  29. Siek, J., Lee, L.-Q., Lumsdaine, A.: The Boost Graph Library: User Guide and Reference Manual. Addison-Wesley, Reading (2002)

    Google Scholar 

  30. Taha, W.: Multi-Stage Programming: its theory and applications. PhD thesis, Oregon Graduate Institute of Science and Technology (1999)

    Google Scholar 

  31. Taha, W.: A sound reduction semantics for untyped CBN multi-stage computation. Or, the theory of MetaML is non-trival. In: PEPM, pp. 34–43 (2000)

    Google Scholar 

  32. 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)

    Chapter  Google Scholar 

  33. Veldhuizen, T.L.: Arrays in Blitz++. In: Caromel, D., Oldehoeft, R.R., Tholburn, M. (eds.) ISCOPE 1998. LNCS, vol. 1505, pp. 223–230. Springer, Heidelberg (1998)

    Chapter  Google Scholar 

  34. Veldhuizen, T.L.: Active Libraries and Universal Languages. PhD thesis, Indiana University Computer Science (May 2004)

    Google Scholar 

  35. Watt, S.M.: Aldor. In: Grabmeier, J., Kaltofen, E., Weispfennig, V. (eds.) Computer Algebra Handbook: Foundations, Applications, Systems. Springer, Heidelberg (2003)

    Google Scholar 

  36. Clint Whaley, R., Petitet, A., Dongarra, J.J.: Automated empirical optimization of software and the ATLAS project. Parallel Computing 27(1-2), 3–35 (2001)

    Article  MATH  Google Scholar 

Download references

Author information

Authors and Affiliations

Authors

Editor information

Editors and Affiliations

Rights and permissions

Reprints and permissions

Copyright information

© 2005 Springer-Verlag Berlin Heidelberg

About this paper

Cite this paper

Carette, J., Kiselyov, O. (2005). Multi-stage Programming with Functors and Monads: Eliminating Abstraction Overhead from Generic Code. In: Glück, R., Lowry, M. (eds) Generative Programming and Component Engineering. GPCE 2005. Lecture Notes in Computer Science, vol 3676. Springer, Berlin, Heidelberg. https://doi.org/10.1007/11561347_18

Download citation

  • DOI: https://doi.org/10.1007/11561347_18

  • Publisher Name: Springer, Berlin, Heidelberg

  • Print ISBN: 978-3-540-29138-1

  • Online ISBN: 978-3-540-31977-1

  • eBook Packages: Computer ScienceComputer Science (R0)

Publish with us

Policies and ethics