Implementing memoization for partial evaluation

  • Peter Thiemann
Program Transformation
Part of the Lecture Notes in Computer Science book series (LNCS, volume 1140)


Memoization is a key ingredient in every partial evaluator. It enables folding by caching previously specialized functions. It is essential to make polyvariant specialization terminate. Its implementation is reasonably straightforward in a standard specializer that represents functions by closures. With the advent of handwritten program-generator generators (PGGs), implementing memoization gets harder, because PGGs use efficient standard representations of data at specialization time.

We present several implementations of memoization for PGGs that are able to deal with all features of current partial evaluators, specifically partially static data and functions. The first implementation is based on message passing. It is simple, portable, and efficient, but only suitable for untyped higher-order languages such as Scheme. The second implementation is geared towards typed language such as SML. Whereas the first two implementations are completely portable, our third implementation exploits introspective features that may not be available in all implementations.

Finally, we demonstrate that PGGs can solve the termination problem for partial evaluation. Our new incremental memoization algorithm performs incremental specialization and guarantees that specialization terminates whenever standard evaluation does.


partial evaluation automatic program transformation incremental specialization termination of partial evaluation reflection 


Unable to display preview. Download preview PDF.

Unable to display preview. Download preview PDF.


  1. 1.
    Peter Holst Andersen and Carsten Kehler Holst. Termination analysis for offline partial evaluation of a higher order functional language. In Radhia Cousot, editor, Proc. International Static Analysis Symposium, SAS'96, Aachen, Germany, September 1996. Springer-Verlag. LNCS.Google Scholar
  2. 2.
    Lars Birkedal and Morten Welinder. Partial evaluation of Standard ML. Rapport 93/22, DIKU, University of Copenhagen, 1993.Google Scholar
  3. 3.
    Lars Birkedal and Morten Welinder. Hand-writing program generator generators. In Manuel V. Hermenegildo and Jaan Penjam, editors, Programming Languages, Implementations, Logics, and Programs (PLILP '94), volume 844 of Lecture Notes in Computer Science, pages 198–214, Madrid, Spain, September 1994. Springer-Verlag.Google Scholar
  4. 4.
    Dines Bjørner, Andrei P. Ershov, and Neil D. Jones, editors. Partial Evaluation and Mixed Computation, Amsterdam, 1988. North-Holland.Google Scholar
  5. 5.
    Anders Bondorf. Automatic autoprojection of higher order recursive equations. Science of Programming, 17:3–34, 1991.Google Scholar
  6. 6.
    Anders Bondorf. Similix 5.0 Manual. DIKU, University of Copenhagen, May 1993.Google Scholar
  7. 7.
    Anders Bondorf and Olivier Danvy. Automatic autoprojection of recursive equations with global variables and abstract data types. Science of Programming, 16(2):151–195, 1991.Google Scholar
  8. 8.
    Anders Bondorf and Dirk Dussart. Improving CPS-based partial evaluation: Writing cogen by hand. In Peter Sestoft and Harald Søndergaard, editors, Proc. ACM SIGPLAN Workshop on Partial Evaluation and Semantics-Based Program Manipulation PEPM '94, pages 1–10, Orlando, Fla., June 1994. ACM.Google Scholar
  9. 9.
    Charles Consel. A tour of Schism. In David Schmidt, editor, Proc. ACM SIG-PLAN Symposium on Partial Evaluation and Semantics-Based Program Manipulation PEPM '93, pages 134–154, Copenhagen, Denmark, June 1993. ACM Press.Google Scholar
  10. 10.
    Charles Consel and Olivier Danvy. Tutorial notes on partial evaluation. In Proc. 20th Annual ACM Symposium on Principles of Programming Languages, pages 493–501, Charleston, South Carolina, January 1993. ACM Press.Google Scholar
  11. 11.
    Yoshihiko Futamura. Partial evaluation of computation process — an approach to a compiler-compiler. Systems, Computers, Controls, 2(5):45–50, 1971.Google Scholar
  12. 12.
    Robert Glück and Jesper Jørgensen. Efficient multi-level generating extensions for program specialization. In Doaitse Swierstra and Manuel Hermenegildo, editors, Programming Languages, Implementations, Logics, and Programs (PLILP '95), volume 982 of Lecture Notes in Computer Science, Utrecht, The Netherlands, September 1995. Springer-Verlag.Google Scholar
  13. 13.
    Arne John Glenstrup and Neil D. Jones. Bta algorithms to ensure termination of offline partial evaluation. In PSI-96: Andrei Ershov Second International Memorial Conference, Perspectives of System Informatics, Novosibirsk, Russia, June 1996.Google Scholar
  14. 14.
    Fritz Henglein. Efficient type inference for higher-order binding-time analysis. In Hughes [16]. pages 448–472. LNCS 523.Google Scholar
  15. 15.
    Carsten Kehler Holst. Finiteness analysis. In Hughes [16]. pages 473–495. LNCS 523.Google Scholar
  16. 16.
    John Hughes, editor. Functional Programming Languages and Computer Architecture, Cambridge, MA, 1991. Springer-Verlag. LNCS 523.Google Scholar
  17. 17.
    Institute of Electrical and Electronic Engineers, Inc. IEEE standard for the Scheme programming language. IEEE Std 1178–1990, New York, NY, 1991.Google Scholar
  18. 18.
    Neil D. Jones, Carsten K. Gomard, and Peter Sestoft. Partial Evaluation and Automatic Program Generation. Prentice Hall, 1993.Google Scholar
  19. 19.
    Neil D. Jones, Peter Sestoft, and Harald Søndergaard. An experiment in partial evaluation: The generation of a compiler generator. In J.-P. Jouannaud, editor, Rewriting Techniques and Applications, pages 124–140, Dijon, France, 1985. Springer-Verlag. LNCS 202.Google Scholar
  20. 20.
    Richard A. Kelsey and Jonathan Rees. A tractable Scheme implementation. Lisp and Symbolic Computation, 7(4):315–335, 1994.Google Scholar
  21. 21.
    John Launchbury and Carsten Kehler Holst. Handwriting cogen to avoid problems with static typing. In Draft Proceedings, Fourth Annual Glasgow Workshop on Functional Programming, pages 210–218, Skye, Scotland, 1991. Glasgow University.Google Scholar
  22. 22.
    Julia Lawall and Olivier Danvy. Continuation-based partial evaluation. In LFP 1994 [24], pages 227–238.Google Scholar
  23. 23.
    Julia Lawall and Olivier Danvy. Continuation-based partial evaluation. Extended version of [22] from, January 1995.Google Scholar
  24. 24.
    Proc. 1994 ACM Conference on Lisp and Functional Programming, Orlando, Florida, USA, June 1994. ACM Press.Google Scholar
  25. 25.
    Robin Milner, Mads Tofte, and Robert Harper. The Definition of Standard ML. MIT Press, 1990.Google Scholar
  26. 26.
    Yasuhiko Minamide, Greg Morrisett, and Robert Harper. Typed closure conversion. In Proc. 23rd Annual ACM Symposium on Principles of Programming Languages, pages 271–283, St. Petersburg, Fla., January 1996. ACM Press.Google Scholar
  27. 27.
    Torben Æ. Mogensen. Partially static structures in a self-applicable partial evaluator. In Bjørner et al.Google Scholar
  28. 28.
    Torben Æ. Mogensen. Separating binding times in language specifications. In Proc. Functional Programming Languages and Computer Architecture 1989, pages 14–25, London, GB, 1989.Google Scholar
  29. 29.
    Jonathan Rees. The Scheme of things: The June 1992 meeting. Lisp Pointers, V(4), October 1992.Google Scholar
  30. 30.
    John C. Reynolds. Definitional interpreters for higher-order programming. In ACM Annual Conference, pages 717–740, July 1972.Google Scholar
  31. 31.
    Peter Sestoft. The structure of a self-applicable partial evaluator. In Harald Ganzinger and Neil D. Jones, editors, Programs as Data Objects, pages 236–256, Copenhagen, Denmark, October 1985. Springer-Verlag. LNCS 217.Google Scholar
  32. 32.
    Peter Sestoft. Automatic call unfolding in a partial evaluator. In Bjørner et al. [4], pages 485–506.Google Scholar
  33. 33.
    Olin Shivers. Control-Flow Analysis of Higher-Order Languages. PhD thesis, School of Computer Science, Carnegie Mellon University, Pittsburgh, PA 15213, May 1991. Also technical report CMU-CS-91-145.Google Scholar
  34. 34.
    Guy L. Steele Jr. Building interpreters by composing monads. In Proc. 21st Annual A CM Symposium on Principles of Programming Languages, pages 472–492, Portland, OG, January 1994. ACM Press.Google Scholar
  35. 35.
    Dan Stefanescu and Yuli Zhou. An equational framework for the flow analysis of higher-order functional programs. In LFP 1994 [24], pages 318–327.Google Scholar
  36. 36.
    Peter Thiemann. Cogen in six lines. In R. Kent Dybvig, editor, Proc. International Conference on Functional Programming 1996, pages 180–189, Philadelphia, PA, May 1996. ACM Press, New York.Google Scholar
  37. 37.
    Valentin F. Turchin. The use of metasystem transition in theorem proving and program optimization. In International Conference on Automata, Languages, and Programming. Seventh ICALP, volume 85 of Lecture Notes in Computer Science, pages 645–657, Noordwijkerhout, The Netherlands, 1980. Springer-Verlag.Google Scholar

Copyright information

© Springer-Verlag Berlin Heidelberg 1996

Authors and Affiliations

  • Peter Thiemann
    • 1
  1. 1.Wilhelm-Schickard-InstitutUniversität TübingenTübingenGermany

Personalised recommendations