Multi-stage programming with functors and monads: Eliminating abstraction overhead from generic code

Jacques Carette, Oleg Kiselyov

Research output: Contribution to journalArticlepeer-review

19 Citations (Scopus)


We use multi-stage programming, monads and Ocaml's advanced module system to demonstrate how to eliminate all abstraction overhead from generic programs, while avoiding any inspection of the resulting code. We demonstrate this clearly with Gaussian Elimination as a representative family of symbolic and numeric algorithms. We parameterize our code to a great extentover domain, input and permutation matrix representations, determinant and rank tracking, pivoting policies, result types, no run-time cost. Because the resulting code is generated just right and not changed afterward, MetaOCaml guarantees 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 "interleaving" of aspects. We also show how to encode some domain-specific knowledge so that "clearly wrong" compositions can be rejected at or before generation time, rather than during the compilation or running of the generated code.

Original languageEnglish
Pages (from-to)349-375
Number of pages27
JournalScience of Computer Programming
Issue number5
Publication statusPublished - 2011 May 1
Externally publishedYes


  • Functor
  • Generative
  • Genericity
  • Linear algebra
  • MetaOCaml
  • Staging
  • Symbolic

ASJC Scopus subject areas

  • Software


Dive into the research topics of 'Multi-stage programming with functors and monads: Eliminating abstraction overhead from generic code'. Together they form a unique fingerprint.

Cite this