Download PDF Download PDF

Journal of Functional Programming

Publication date: 2012-01-01
Volume: 22 Pages: 757 - 796
Publisher: Cambridge University Press

Author:

Devriese, D
Piessens, F

Keywords:

Science & Technology, Technology, Computer Science, Software Engineering, Computer Science, Observable Recursion, Finally Tagless, Haskell, Parsing, 0102 Applied Mathematics, 0103 Numerical and Computational Mathematics, 0803 Computer Software, Software Engineering, 4612 Software engineering, 4613 Theory of computation

Abstract:

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 library 1, which is the first to work with a precise, shallow model of 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. ©2012 Copyright Cambridge University Press.