Skip to main content

Finally tagless observable recursion for an abstract grammar model


We define a finally tagless, shallow embedding of a typed grammar language. In order to avoid the limitations of traditional parser combinator libraries (no bottom-up parsing, no full grammar analysis or transformation), we require object-language recursion to be observable in the meta-language. Since existing proposals for recursive constructs are not fully satisfactory, we propose new finally tagless primitive recursive constructs to solve the problem. To do this in a well-typed way, we require considerable infrastructure, for which we reuse techniques from the multirec generic programming library. Our infrastructure allows a precise model of the complex interaction between a grammar, a parsing algorithm and a set of semantic actions. On the flip side, our approach requires the grammar author to provide a type- and value-level encoding of the grammar's domain and we can provide only a limited form of constructs like many. We demonstrate five meta-language grammar algorithms exploiting our model, including a grammar pretty-printer, a reachability analysis, a translation of quantified recursive constructs to the standard one and an implementation of the left-corner grammar transform. The work we present forms the basis of the grammar-combinators parsing library1, which is the first to work with a precise, shallow model of abstract context-free grammars in a classical (not dependently typed) functional language and which supports a wide range of grammar manipulation primitives. From a more general point of view, our work shows a solution to the well-studied problem of observable sharing in shallowly embedded domain-specific languages and specifically in finally tagless domain-specific languages.

Hide All
Aho Alfred V., Lam Monica S., Sethi R. & Ullman Jeffrey D. (2006) Compilers: Principles, Techniques and Tools, 2nd ed.Boston, MA: Addison-Wesley.
Baars A. I. and Swierstra S. D. (2004) Type-safe, self-inspecting code. In Proceedings of the 2004 HASKELL Workshop, Snowbird, UT, USA, September 22.
Baars A. I., Swierstra S. D. & Viera M. (2009) Typed transformations of typed abstract syntax. In Proceedings of the TLDI'09, Savannah, GA, USA, January 24, pp. 1526.
Blum N. & Koch R. (1999) Greibach normal form transformation revisited. Inf. Comput. 150 (1), 112118.
Brink K., Holdermans S. & Löh A. (2010) Dependently typed grammars. In Proceedings of the MPC, Québec City, Canada, June 21–23.
Carette J., Kiselyov O. & Shan C.-C. (2009) Finally tagless, partially evaluated: Tagless staged interpreters for simpler typed languages. J. Funct. Program. 19 (5), 509543.
Claessen K. & Sands D. (1999) Observable sharing for functional circuit description. In Proceedings of the ASIAN'99, Phuket, Thailand.
Danielsson N. A. & Norell U. (2010) Total parser combinators. In Proceedings of the 15th ICFP, Baltimore, MD, USA, September 27–29.
Devriese D. & Piessens F. (2010) Explicitly Recursive Grammar Combinators – Implemention of Some Grammar Algorithms. Tech. Rep. CW594, KU Leuven, CS. Available at:
Devriese D. & Piessens F. (2011) Explicitly recursive grammar combinators. In Proceedings of the 13th PADL'11, Austin, TX, USA, January 24–25.
Erkök L. & Launchbury J. (2002) A recursive do for haskell. In 2002 Haskell Workshop, Pittsburgh, PA, USA, October 3.
Ford B. (2002) Packrat parsing: Simple, powerful, lazy, linear time – functional Pearl. In Proceedings of the ICFP, Pittsburgh, PA, USA, October 4–6.
Frost R. A., Hafiz R. & Callaghan P. (2008) Parser combinators for ambiguous left-recursive grammars. In Proceedings of the 10th PADL, San Francisco, CA, USA.
Gill A. (2009) Type-safe observable sharing in Haskell. In Proceedings of the HASKELL, Edinburgh, Scotland, September 3, pp. 117128.
Grune D. & Jacobs C. J. H. (2008) Parsing Techniques: A Practical Guide, 2nd ed.New York: Springer-Verlag (ISBN 038720248X).
Hughes J. (2000) Generalising monads to arrows. Sci. Comput. Program. 37 (1–3), 67111.
Johnson S. C. (1979) YACC. In Unix Programmer's Manual, vol. 2b. Madison, WI: Bell Laboratories.
Leijen D. & Meijer E. (2001) Parsec: Direct Style Monadic Parser Combinators for the Real World. Tech. Rep. UU-CS-2001-27, Universiteit Utrecht CS.
Magalhães J. P., Holdermans S., Jeuring J. & Löh A. (2010) Optimizing generics is easy! In Proceedings of the PEPM, Madrid, Spain, January 18–19.
McBride C. & Paterson R. (2008) Functional pearl: Applicative programming with effects. J. Funct. Program. 18 (1), 113.
Meijer E., Fokkinga M. & Paterson R. (1991) Functional programming with bananas, lenses, envelopes and barbed wire. In Proceedings of the FPLCA, August 1991. (Lect. Notes in Comp. Sci., 523). New York: Springer-Verlag.
Might M., Darais D. & Spiewak D. (2011) Parsing with derivatives: A Functional Pearl. Proceedings of the ICFP, Tokyo, Japan, September 19–21.
Moore R. C. (2000) Removing left recursion from context-free grammars. In Proceedings of the NAACL, Seattle, WA, USA, May 4.
Norell U. (2007) Towards a Practical Programming Language Based on Dependent Type Theory. PhD thesis, Chalmers University of Technology, Sweden.
Park D. (1981) Concurrency and automata on infinite sequences. In Proceedings of the Theoretical Computer Science, 5th GI-Conference, Karlsruhe, Germany, March 23–25, pp. 167183.
Parr T. J. & Quong R. W. (1995) ANTLR: A predicated-LL(k) parser generator. Softw. Pract. Exp. 25 (7), 789810.
Paterson R. (2001) A new notation for arrows. In Proceedings of the ICFP, Florence, Italy, September 3–5, 240 pp.
Peyton Jones S., Vytiniotis D., Weirich S. & Washburn G. (2006) Simple unification-based type inference for GADTs. In Proceedings of the ICFP, Portland, OR, USA, September 18–20.
Pierce B. C. (2002) Types and Programming Languages. Cambridge, MA: MIT Press.
Rodriguez A., Holdermans S., Löh A. & Jeuring J. (2009) Generic programming with fixed points for mutually recursive datatypes. In Proceedings of the 14th ICFP, Edinburgh, Scotland, August 31– September 2.
Rosenkrantz D. J. & Lewis P. M. (1970) Deterministic left corner parsing. In Proceedings of the 11th Annual Symposium on Switching and Automata Theory, October 28–30.
Sabry A. (1998) What is a purely functional language? J. Funct. Program. 8 (1), 122.
Schrijvers T., Peyton Jones S., Chakravarty M. & Sulzmann M. (2008) Type checking with open type functions. In Proceedings of the 13th ICFP, British Columbia, Canada, September 22–24.
Sheard T. (December 2005) Another look at hardware design languages. Available at:
Sheard T. & Peyton Jones S. (2002) Template meta-programming for Haskell. In Proceedings of the 2002 ACM SIGPLAN workshop on Haskell, Pittsburgh, USA, 3rd October, pp. 116.
Swierstra W. (July 2008) Data types à la carte. J. Funct. Program. 18, 423436.
Swierstra D. (2009) Combinator Parsing: A Short Tutorial. Language Engineering and Rigorous Software Development, Piriapolis, Uruguay, Revised Tutorial Lectures, Lecture Notes in Computer Science, vol. 5520. New York: Springer-Verlag, pp. 252–300.
Swierstra S. & Duponcheel L. (1996) Deterministic, error-correcting combinator parsers. Proceedings of the AFP, Olympia, WA, USA.
Viera M., Swierstra S. D. & Lempsink E. (2008) Haskell, do you read me? Constructing and composing efficient top-down parsers at runtime. In Proceedings of the HASKELL 2008, British Columbia, Canada, September 25.
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: 8 *
Loading metrics...

Abstract views

Total abstract views: 72 *
Loading metrics...

* Views captured on Cambridge Core between September 2016 - 21st November 2017. This data will be updated every 24 hours.