ABSTRACT
Attribute Grammars (AGs), a general-purpose formalism for describing recursive computations over data types, avoid the trade-off which arises when building software incrementally: should it be easy to add new data types and data type alternatives or to add new operations on existing data types? However, AGs are usually implemented as a pre-processor, leaving e.g. type checking to later processing phases and making interactive development, proper error reporting and debugging difficult. Embedding AG into Haskell as a combinator library solves these problems.
Previous attempts at embedding AGs as a domain-specific language were based on extensible records and thus exploiting Haskell's type system to check the well formedness of the AG, but fell short in compactness and the possibility to abstract over oft occurring AG patterns. Other attempts used a very generic mapping for which the AG well-formedness could not be statically checked.
We present a typed embedding of AG in Haskell satisfying all these requirements. The key lies in using HList-like typed heterogeneous collections (extensible polymorphic records) and expressing AG well-formedness conditions as type-level predicates (i.e., type-class constraints). By further type-level programming we can also express common programming patterns, corresponding to the typical use cases of monads such as Reader, Writer and State. The paper presents a realistic example of type-class-based type-level programming in Haskell.
Supplemental Material
- Richard S. Bird. Using circular programs to eliminate multiple traversals of data. Acta Inf., 21:239--250, 1984.Google ScholarDigital Library
- John Boyland. Remote attribute grammars. Journal of the ACM (JACM, 52(4), Jul 2005. URL http://portal.acm.org/citation.cfm?id=1082036.1082042. Google ScholarDigital Library
- Manuel M. T. Chakravarty, Gabriele Keller, and Simon Peyton Jones. Associated type synonyms. In ICFP '05: Proceedings of the tenth ACM SIGPLAN international conference on Functional programming, pages 241--253, New York, NY, USA, 2005. ACM. Google ScholarDigital Library
- Oege de Moor, Kevin Backhouse, and S. Doaitse Swierstra. First-class attribute grammars. Informatica (Slovenia), 24(3), 2000a.Google Scholar
- Oege de Moor, L. Peyton Jones, Simon, and Van Wyk, Eric. Aspect-oriented compilers. In GCSE '99: Proceedings of the First International Symposium on Generative and Component-Based Software Engineering, pages 121--133, London, UK, 2000b. Springer-Verlag. ISBN 3-540-41172-0. Google ScholarDigital Library
- Atze Dijkstra and S. Doaitse Swierstra. Typing Haskell with an Attribute Grammar. In Advanced Functional Programming Summerschool, number 3622 in LNCS. Springer-Verlag, 2004. Google ScholarDigital Library
- Atze Dijkstra, Jeroen Fokker, and S. Doaitse Swierstra. The structure of the essential haskell compiler, or coping with compiler complexity. In Implementation of Functional Languages, 2007.Google Scholar
- Atze Dijkstra, Jeroen Fokker, and S. Doaitse Swierstra. The architecture of the Utrecht Haskell compiler. In Haskell Symposium, New York, NY, USA, September 2009. ACM. Google ScholarDigital Library
- Jeroen Fokker and S. Doaitse Swierstra. Abstract interpretation of functional programs using an attribute grammar system. In Adrian Johnstone and Jurgen Vinju, editors, Language Descriptions, Tools and Applications, 2008.Google Scholar
- Benedict R. Gaster and Mark P. Jones. A polymorphic type system for extensible records and variants. NOTTCS-TR 96-3, Nottingham, 1996.Google Scholar
- Thomas Hallgren. Fun with functional dependencies or (draft) types as values in static computations in haskell. In Proc. of the Joint CS/CE Winter Meeting, 2001.Google Scholar
- Ralf Hinze. Fun with phantom types. In Jeremy Gibbons and Oege de Moor, editors, The Fun of Programming, pages 245--262. Palgrave Macmillan, 2003.Google ScholarCross Ref
- Mark P. Jones. Typing Haskell in Haskell. In Haskell Workshop, 1999. URL http://www.cse.ogi.edu/ mpj/thih/thih-sep1-1999/.Google Scholar
- P. Jones, Mark. Type classes with functional dependencies. In ESOP '00: Proceedings of the 9th European Symposium on Programming Languages and Systems, pages 230--244, London, UK, 2000. Springer-Verlag. Google ScholarDigital Library
- Uwe Kastens. Ordered Attribute Grammars. Acta Informatica, 13: 229--256, 1980.Google ScholarDigital Library
- Oleg Kiselyov, Ralf Lammel, and Keean Schupke. Strongly typed heterogeneous collections. In Haskell '04: Proceedings of the ACM SIGPLAN workshop on Haskell, pages 96--107. ACM Press, 2004. Google ScholarDigital Library
- Conor McBride. Faking it simulating dependent types in haskell. J. Funct. Program., 12(5):375--392, 2002. Google ScholarDigital Library
- Ulf Norell. Dependently typed programming in Agda. In 6th International School on Advanced Functional Programming, 2008. Google ScholarDigital Library
- Nicolas Oury and Wouter Swierstra. The power of Pi. In ICFP '08: Proceedings of the Thirteenth ACM SIGPLAN International Conference on Functional Programming, 2008. Google ScholarDigital Library
- Simon Peyton Jones, Mark Jones, and Erik Meijer. Type classes: an exploration of the design space. In Haskell Workshop, June 1997.Google Scholar
- W. Reps, Thomas, Carla Marceau, and Tim Teitelbaum. Remote attribute updating for language-based editors. In POPL '86: Proceedings of the 13th ACM SIGACT-SIGPLAN symposium on Principles of programming languages, pages 1--13, New York, NY, USA, 1986. ACM. Google ScholarDigital Library
- J.C. Reynolds. User defined types and procedural data as complementary approaches to data abstraction. In S.A. Schuman, editor, New Directions in Algorithmic Languages. INRIA, 1975.Google Scholar
- Alexandra Silva and Joost Visser. Strong types for relational databases. In Haskell '06: Proceedings of the 2006 ACM SIGPLAN workshop on Haskell, pages 25--36, New York, NY, USA, 2006. ACM. ISBN 1-59593-489-8. Google ScholarDigital Library
- S. Doaitse Swierstra and Olaf Chitil. Linear, bounded, functional pretty-printing. Journal of Functional Programming, 19(01):1--16, 2009. Google ScholarDigital Library
- S. Doaitse Swierstra, Pablo R. Azero Alcocer, and Joao A. Saraiva. Designing and implementing combinator languages. In S. D. Swierstra, Pedro Henriques, and Jose Oliveira, editors, Advanced Functional Programming, Third International School, AFP'98, volume 1608 of LNCS, pages 150--206. Springer-Verlag, 1999.Google Scholar
- S.D. Swierstra. Linear, online, functional pretty printing (extended and corrected version). Technical Report UU-CS-2004-025a, Inst. of Information and Comp. Science, Utrecht Univ., 2004.Google Scholar
- Phil Wadler. The Expression Problem. E-mail available online., 1998.Google Scholar
Index Terms
- Attribute grammars fly first-class: how to do aspect oriented programming in Haskell
Recommendations
Attribute grammars fly first-class: how to do aspect oriented programming in Haskell
ICFP '09Attribute Grammars (AGs), a general-purpose formalism for describing recursive computations over data types, avoid the trade-off which arises when building software incrementally: should it be easy to add new data types and data type alternatives or to ...
Attribute grammars fly first-class... safer!: dealing with DSL errors in type-level programming
IFL '19: Proceedings of the 31st Symposium on Implementation and Application of Functional LanguagesAspectAG is a domain specific language embedded in Haskell to represent modular attribute grammars. In AspectAG attribute grammar fragments can be defined independently (even in separate modules) and then combined in a safe way. This flexibility is ...
First Class Syntax, Semantics, and Their Composition
IFL '13: Proceedings of the 25th symposium on Implementation and Application of Functional LanguagesIdeally complexity is managed by composing a system out of quite a few, more or less independent, and much smaller descriptions of various aspects of the overall artefact. When describing (extensible) programming languages, attribute grammars have ...
Comments