Abstract
The Haskell definition and implementation of read is far from perfect. In the first place read is not able to handle the associativities defined for infix operators. Furthermore, it puts constraints on the way show is defined, and especially forces it to generate far more parentheses than expected. Lastly, it may give rise to exponential parsing times. All this is due to the compositionality requirement for read functions, which imposes a top-down parsing strategy.
We propose a different approach, based on typed abstract syntax, in which grammars describing the data types are composed dynamically. Using the transformation libraries described in a companion paper these syntax descriptions are combined and transformed into parsers at runtime, from which the required read function are constructed. In this way we obtain linear parsing times, achieve consistency with the defined associativities, and may use a version of show which generates far fewer parentheses, thus improving readability of printed values.
The described transformation algorithms can be incorporated in a Haskell compiler, thus moving most of the work involved to compile time.
Supplemental Material
Available for Download
- Arthur Baars and Doaitse Swierstra. Typed transformations of typed abstract syntax. http://www.cs.uu.nl/research/techreps/, Utrecht University, 2008.Google Scholar
- Arthur Baars, Andres Löh, and Doaitse Swierstra. Parsing permutation phrases. J. Funct. Program., 14 (6): 635--646, 2004. ISSN 0956-7968. Google Scholar
Digital Library
- Arthur I. Baars and S. Doaitse Swierstra. Type-safe, self inspecting code. In Haskell '04: Proceedings of the 2004 ACM SIGPLAN workshop on Haskell, pages 69--79, New York, NY, USA, 2004. ACM. ISBN 1-58113-850-4. http://doi.acm.org/10.1145/1017472.1017485. Google Scholar
Digital Library
- Eric Bouwers, Martin Bravenboer, and Eelco Visser. Grammar engineering support for precedence rule recovery and compatibility checking. Electron. Notes Theor. Comput. Sci., 203 (2): 85--101, 2008. ISSN 1571-0661. Google Scholar
Digital Library
- Martin Bravenboer. Exercises in Free Syntax. Syntax Definition, Parsing, and Assimilation of Language Conglomerates. PhD thesis, Utrecht University, Utrecht, The Netherlands, January 2008.Google Scholar
- M. Johnson. Finite-state approximation of constraint-based grammars using left-corner grammar transforms. In COLING-ACL '98, Montreal, Quebec, Canada, pages 619--623. Association for Computational Linguistics, 1998. Google Scholar
Digital Library
- Ralf Lämmel and Simon Peyton Jones. Scrap your boilerplate: a practical design pattern for generic programming. ACM SIGPLAN Notices, 38 (3): 26--37, March 2003. Proceedings of the ACM SIGPLAN Workshop on Types in Language Design and Implementation (TLDI 2003). Google Scholar
Digital Library
- Ralf Lämmel and Simon Peyton Jones. Scrap more boilerplate: reflection, zips, and generalised casts. In Proceedings of the ACM SIGPLAN International Conference on Functional Programming (ICFP 2004), pages 244--255. ACM Press, 2004. Google Scholar
Digital Library
- Conor McBride and Ross Paterson. Applicative programming with effects. Journal of Functional Programming, 18 (01): 1--13, 2007. 10.1017/S0956796807006326. Google Scholar
Digital Library
- Emir Pasalic and Nathan Linger. Meta-programming with typed object-language representations. In Generative Programming and Component Engineering (GPCE'04), volume LNCS 3286, pages 136--167, October 2004.Google Scholar
- JC Petruzza, Koen Claessen, and Simon Peyton Jones. Derived read instances for recursive datatypes with infix constructors are too inefficient. URL http://hackage.haskell.org/trac/ghc/ticket/1544. GHC Ticket 1544.Google Scholar
- Simon Peyton Jones. Haskell 98 Language and Libraries: the Revised Report. 2003.Google Scholar
- Simon Peyton Jones, Dimitrios Vytiniotis, Stephanie Weirich, and Geoffrey Washburn. Simple nification-based type inference for gadts. SIGPLAN Not., 41 (9): 50--61, 2006. ISSN 0362--1340. Google Scholar
Digital Library
- Tim Sheard and Simon Peyton Jones. Template meta-programming for haskell. In Proceedings of the ACM SIGPLAN workshop on Haskell, pages 1--16. ACM Press, 2002. ISBN 1-58113-605-6. Google Scholar
Digital Library
- S. D. Swierstra. The Utrecht Parsing Libraries. http://www.cs.uu.nl/wiki/bin/view/HUT/ParserCombinators, July 2008.Google Scholar
Index Terms
Haskell, do you read me?: constructing and composing efficient top-down parsers at runtime
Recommendations
Haskell, do you read me?: constructing and composing efficient top-down parsers at runtime
Haskell '08: Proceedings of the first ACM SIGPLAN symposium on HaskellThe Haskell definition and implementation of read is far from perfect. In the first place read is not able to handle the associativities defined for infix operators. Furthermore, it puts constraints on the way show is defined, and especially forces it ...
Typed Transformations of Typed Grammars: The Left Corner Transform
One of the questions which comes up when using embedded domain specific languages is to what extent we can analyze and transform embedded programs, as normally done in more conventional compilers. Special problems arise when the host language is ...
Typed transformations of typed abstract syntax
TLDI '09: Proceedings of the 4th international workshop on Types in language design and implementationAdvantages of embedded domain-specific languages (EDSLs) are that one does not have to implement a separate type system nor an abstraction mechanism, since these are directly borrowed from the host language. Straightforward implementations of embedded ...







Comments