Hostname: page-component-8448b6f56d-sxzjt Total loading time: 0 Render date: 2024-04-19T07:26:05.335Z Has data issue: false hasContentIssue false

Finally tagless observable recursion for an abstract grammar model

Published online by Cambridge University Press:  10 July 2012

DOMINIQUE DEVRIESE
Affiliation:
IBBT-Distrinet, KU Leuven, Belgium (e-mail: dominique.devriese@cs.kuleuven.be, frank.piessens@cs.kuleuven.be)
FRANK PIESSENS
Affiliation:
IBBT-Distrinet, KU Leuven, Belgium (e-mail: dominique.devriese@cs.kuleuven.be, frank.piessens@cs.kuleuven.be)
Rights & Permissions [Opens in a new window]

Abstract

Core share and HTML view are not available for this content. However, as you have access to this content, a full PDF is available via the ‘Save PDF’ action button.

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.

Type
Articles
Copyright
Copyright © Cambridge University Press 2012

References

Aho, Alfred V., Lam, Monica S., Sethi, R. & Ullman, Jeffrey D. (2006) Compilers: Principles, Techniques and Tools, 2nd ed.Boston, MA: Addison-Wesley.Google Scholar
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.Google Scholar
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.CrossRefGoogle Scholar
Blum, N. & Koch, R. (1999) Greibach normal form transformation revisited. Inf. Comput. 150 (1), 112118.CrossRefGoogle Scholar
Brink, K., Holdermans, S. & Löh, A. (2010) Dependently typed grammars. In Proceedings of the MPC, Québec City, Canada, June 21–23.Google Scholar
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.CrossRefGoogle Scholar
Claessen, K. & Sands, D. (1999) Observable sharing for functional circuit description. In Proceedings of the ASIAN'99, Phuket, Thailand.Google Scholar
Danielsson, N. A. & Norell, U. (2010) Total parser combinators. In Proceedings of the 15th ICFP, Baltimore, MD, USA, September 27–29.Google Scholar
Devriese, D. & Piessens, F. (2010) Explicitly Recursive Grammar Combinators – Implemention of Some Grammar Algorithms. Tech. Rep. CW594, KU Leuven, CS. Available at: http://www.cs.kuleuven.be/publicaties/rapporten/cw/CW594.abs.html.Google Scholar
Devriese, D. & Piessens, F. (2011) Explicitly recursive grammar combinators. In Proceedings of the 13th PADL'11, Austin, TX, USA, January 24–25.Google Scholar
Erkök, L. & Launchbury, J. (2002) A recursive do for haskell. In 2002 Haskell Workshop, Pittsburgh, PA, USA, October 3.Google Scholar
Ford, B. (2002) Packrat parsing: Simple, powerful, lazy, linear time – functional Pearl. In Proceedings of the ICFP, Pittsburgh, PA, USA, October 4–6.Google Scholar
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.Google Scholar
Gill, A. (2009) Type-safe observable sharing in Haskell. In Proceedings of the HASKELL, Edinburgh, Scotland, September 3, pp. 117128.CrossRefGoogle Scholar
Grune, D. & Jacobs, C. J. H. (2008) Parsing Techniques: A Practical Guide, 2nd ed.New York: Springer-Verlag (ISBN 038720248X).CrossRefGoogle Scholar
Hughes, J. (2000) Generalising monads to arrows. Sci. Comput. Program. 37 (1–3), 67111.CrossRefGoogle Scholar
Johnson, S. C. (1979) YACC. In Unix Programmer's Manual, vol. 2b. Madison, WI: Bell Laboratories.Google Scholar
Leijen, D. & Meijer, E. (2001) Parsec: Direct Style Monadic Parser Combinators for the Real World. Tech. Rep. UU-CS-2001-27, Universiteit Utrecht CS.Google Scholar
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.Google Scholar
McBride, C. & Paterson, R. (2008) Functional pearl: Applicative programming with effects. J. Funct. Program. 18 (1), 113.CrossRefGoogle Scholar
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.Google Scholar
Might, M., Darais, D. & Spiewak, D. (2011) Parsing with derivatives: A Functional Pearl. Proceedings of the ICFP, Tokyo, Japan, September 19–21.Google Scholar
Moore, R. C. (2000) Removing left recursion from context-free grammars. In Proceedings of the NAACL, Seattle, WA, USA, May 4.Google Scholar
Norell, U. (2007) Towards a Practical Programming Language Based on Dependent Type Theory. PhD thesis, Chalmers University of Technology, Sweden.Google Scholar
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.CrossRefGoogle Scholar
Parr, T. J. & Quong, R. W. (1995) ANTLR: A predicated-LL(k) parser generator. Softw. Pract. Exp. 25 (7), 789810.CrossRefGoogle Scholar
Paterson, R. (2001) A new notation for arrows. In Proceedings of the ICFP, Florence, Italy, September 3–5, 240 pp.Google Scholar
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.Google Scholar
Pierce, B. C. (2002) Types and Programming Languages. Cambridge, MA: MIT Press.Google Scholar
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.Google Scholar
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.CrossRefGoogle Scholar
Sabry, A. (1998) What is a purely functional language? J. Funct. Program. 8 (1), 122.CrossRefGoogle Scholar
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.Google Scholar
Sheard, T. (December 2005) Another look at hardware design languages. Available at: http://www.cs.pdx.edu/~sheard/.Google Scholar
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.Google Scholar
Swierstra, W. (July 2008) Data types à la carte. J. Funct. Program. 18, 423436.CrossRefGoogle Scholar
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.CrossRefGoogle Scholar
Swierstra, S. & Duponcheel, L. (1996) Deterministic, error-correcting combinator parsers. Proceedings of the AFP, Olympia, WA, USA.Google Scholar
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.Google Scholar
Submit a response

Discussions

No Discussions have been published for this article.