Skip to main content Accessibility help

Boxes go bananas: Encoding higher-order abstract syntax with parametric polymorphism*


Higher-order abstract syntax is a simple technique for implementing languages with functional programming. Object variables and binders are implemented by variables and binders in the host language. By using this technique, one can avoid implementing common and tricky routines dealing with variables, such as capture-avoiding substitution. However, despite the advantages this technique provides, it is not commonly used because it is difficult to write sound elimination forms (such as folds or catamorphisms) for higher-order abstract syntax. To fold over such a data type, one must either simultaneously define an inverse operation (which may not exist) or show that all functions embedded in the data type are parametric. In this paper, we show how first-class polymorphism can be used to guarantee the parametricity of functions embedded in higher-order abstract syntax. With this restriction, we implement a library of iteration operators over data structures containing functionals. From this implementation, we derive “fusion laws” that functional programmers may use to reason about the iteration operator. Finally, we show how this use of parametric polymorphism corresponds to the Schürmann, Despeyroux and Pfenning method of enforcing parametricity through modal types. We do so by using this library to give a sound and complete encoding of their calculus into System . This encoding can serve as a starting point for reasoning about higher-order structures in polymorphic languages.

Hide All
Acar, U., Blelloch, G. & Harper, R. (2002) Selective memoization. In 30th ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages. New Orleans, LA: ACM Press, pp. 1425.
Ambler, S., Crole, R. L. & Momigliano, A. (2002) Combining higher order abstract syntax with tactical theorem proving and (co)induction. In 15th International Conference on Theorem Proving in Higher Order Logics. Lecture Notes in Computer Science, vol. 2410. Hampton, VA: Springer.
Bekić, H. (1984) Definable operation in general algebras, and the theory of automata and flowcharts. Programming Languages and Their Definition. Springer-Verlag. LNCS vol. 177.
Böhm, C. & Berarducci, A. (1985) Automatic synthesis of typed Λ-programs on term algebras. Theor. Comput. Sci. 39, 135154.
Church, A. (1940) A formulation of the simple theory of types. J. Symbolic Logic, 5, 5668.
Clarke, D., Hinze, R., Jeuring, J., Löh, A. & de Wit, J. (2001) The Generic Haskell User's Guide. Tech. rept. UU-CS-2001-26. Utrecht University.
Danvy, O. & Filinski, A. (1992) Representing control: A study of the CPS transformation. Math. Struct. Comput. Sci. 2 (4), 361391.
Davies, R. & Pfenning, F. (2001) A modal analysis of staged computation. J. ACM, 48 (3), 555604.
Despeyroux, J. (2000) A higher-order specification of the π–calculus. In IFIP International Theoretical Computer Science. Sendai, Japan: Springer.
Despeyroux, J. & Leleu, P. (2001) Recursion over objects of functional type. Math. Struct. Comput. Sci. 11, 555572.
Despeyroux, J., Felty, A. P. & Hirschowitz, A. (1995) Higher-order abstract syntax in Coq. In Second International Conference on Typed Lambda Calculi and Applications. London, UK: Springer-Verlag.
Fegaras, L. & Sheard, T. (1996) Revisiting catamorphisms over data types with embedded functions (or, programs from outer space). In 23rd ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages. St. Petersburg Beach, FL: ACM Press.
Gabbay, M. J. & Cheney, J. (2004) A sequent calculus for nominal logic. In 19th IEEE Symposium on Logic in Computer Science.
Girard, J.-Y. (1971) Une extension de l'interprétation de Gödel à l'analyse, et son application à l'élimination de coupures dans l'analyse et la théorie des types. In Fenstad, J. E. (ed), Second Scandinavian Logic Symposium. North-Holland Publishing Co.
Hinze, R. (2002) Polytypic values possess polykinded types. Sci. Computer Programming, 43 (2–3), 129159. MPC Special Issue.
Honsell, F. & Miculan, M. (1995, June) A natural deduction approach to dynamic logic. In TYPES 1995, Berardi, C. (ed). Published in LNCS 1158, 1996.
Honsell, F., Miculan, M. & Scagnetto, I. (2001) An axiomatic approach to metareasoning on nominal algebras in HOAS. Lecture Notes Comput. Sci. 2076.
Johann, P. (2002) A generalization of short-cut fution and its correctness proof. Higher-order Symbolic Comput. 15, 273300.
Jones, M. P. (1995) A system of constructor classes: overloading and implicit higher-order polymorphism. J. Funct. Program. 5 (1), 135.
Jones, M. P. (2000) Type classes with functional dependencies. Ninth European Symposium on Programming. LNCS, no. 1782. Berlin, Germany: Springer-Verlag.
Kripke, S. A. (1959) A completeness theorem in modal logic. J. Symb. Logic. 24, 115.
Leszczyłowski, J. (1971) A theory on resolving equations in the space of languages. Bull. Polish Acad. Sci. 19 (Oct.), 967970.
Meijer, E. & Hutton, G. (1995) Bananas in space: Extending fold and unfold to exponential types. In Conference on Functional Programming Languages and Computer Architecture. La Jolla, CA: ACM Press.
Meijer, E., Fokkinga, M. M. & Paterson, R. (1991) Functional programming with bananas, lenses, envelopes and barbed wire. In Conference on Functional Programming Languages and Computer Architecture. Cambridge, MA: Springer-Verlag.
Miller, D. (1990, May) An extension to ML to handle bound variables in data structures: Preliminary report. Proceedings of the Logical Frameworks BRA Workshop.
Miller, D. & Tiu, A. (2005) A proof theory for generic judgments. ACM Trans. Computat. Logic. 6 (4), 749783.
Nanevski, A. (2002) Meta-programming with names and necessity. In Seventh ACM SIGPLAN International Conference on Functional Programming. ACM Press, pp. 206217.
Peyton Jones, S. (ed). (2003) Haskell 98 Language and Libraries: The Revised Report. Cambridge University Press.
Peyton Jones, S., Vytiniotis, D., Weirich, S. & Shields, M. (2005) Practical type inference for arbitrary-rank types. J. Funct. Program. 17 (1), 182.
Pfenning, F. & Davies, R. (2001) A judgmental reconstruction of modal logic. Math. Struct. Comput. Sci. 11 (4), 511540.
Pfenning, F. & Elliott, C. (1988). Higher-order abstract syntax. In ACM SIGPLAN Conference on Programming Language Design and Implementation. Atlanta, GA: ACM Press.
Pfenning, F. & Schürmann, C. (1999) System description: Twelf—a meta-logical framework for deductive systems. In 16th International Conference on Automated Deduction, Ganzinger, H. (ed). Trento, Italy: Springer-Verlag.
Pitts, A. M. & Gabbay, M. J. (2000) A metalanguage for programming with bound names modulo renaming. In Mathematics of Program Construction. Port de Lima, Portugal: Springer-Verlag.
Poswolsky, A. & Carsten Schrmann, C. (2007, Jan.). Delphin: A Functional Programming Language with Higher-Other Encodings and Dependent Types. Tech. Rept. YALEU/DCS/TR-1375. Yale University.
Reynolds, J. C. (1983) Types, abstraction and parametric polymorphism. Information Processing /83. North-Holland. Proceedings of the IFIP 9th World Computer Congress.
Schürmann, C., Despeyroux, J. & Pfenning, F. (2001) Primitive recursion for higher-order abstract syntax. Theor. Comput. Sci. 266 (1–2), 158.
Schürmann, C., Poswolsky, A. & Sarnat, J. (2004, Nov.) The ∇-Calculus: Functional Programming With Higher-Order Encodings. Tech. Rept. YALEU/DCS/TR-1272. Yale University.
Sumii, E. & Kobayashi, N. (2001) A hybrid approach to online and offline partial evaluation. Higher-order Symbol. Comput. 14 (2/3), 101142.
Trifonov, V., Saha, B. & Shao, Z. (2000) Fully reflexive intensional type analysis. Fifth ACM SIGPLAN International Conference on Functional Programming. Montreal, Quebec, Canada: ACM Press. Extended version is YALEU/DCS/TR-1194.
Wadler, P. (1989) Theorems for free! In Conference on Functional Programming Languages and Computer Architecture. London, UK: ACM Press.
Washburn, G. (2001) Modal Typing for Specifying Run-time Code Generation. Available at accessed 31 July 2007.
Washburn, G. & Weirich, S. (2003) Boxes go bananas: Encoding higher-order abstract syntax with parametric polymorphism. In Eighth ACM SIGPLAN International Conference on Functional Programming. Uppsala, Sweden: ACM Press, for ACM SIGPLAN.
Weirich, S. (2006) Type-safe run-time polytypic programming. J. Funct. Program. 16 (10), 681710.
Xi, H., Chen, C. & Chen, G. (2003) Guarded recursive data type constructors. In 30th ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages. New Orleans, LA: ACM Press.
Recommend this journal

Email your librarian or administrator to recommend adding this journal to your organisation's collection.

Journal of Functional Programming
  • ISSN: 0956-7968
  • EISSN: 1469-7653
  • URL: /core/journals/journal-of-functional-programming
Please enter your name
Please enter a valid email address
Who would you like to send this to? *


Full text views

Total number of HTML views: 0
Total number of PDF views: 0 *
Loading metrics...

Abstract views

Total abstract views: 0 *
Loading metrics...

* Views captured on Cambridge Core between <date>. This data will be updated every 24 hours.

Usage data cannot currently be displayed

Boxes go bananas: Encoding higher-order abstract syntax with parametric polymorphism*

Submit a response


No Discussions have been published for this article.


Reply to: Submit a response

Your details

Conflicting interests

Do you have any conflicting interests? *