Layout-sensitive language extensibility with SugarHaskell

Programmers need convenient syntax to write elegant and concise programs. Consequently, the Haskell standard provides syntactic sugar for some scenarios (e.g., do notation for monadic code), authors of Haskell compilers provide syntactic sugar for more scenarios (e.g., arrow notation in GHC), and some Haskell programmers implement preprocessors for their individual needs (e.g., idiom brackets in SHE). But manually written preprocessors cannot scale: They are expensive, error-prone, and not composable. Most researchers and programmers therefore refrain from using the syntactic notations they need in actual Haskell programs, but only use them in documentation or papers. We present a syntactically extensible version of Haskell, SugarHaskell, that empowers ordinary programmers to implement and use custom syntactic sugar. Building on our previous work on syntactic extensibility for Java, SugarHaskell integrates syntactic extensions as sugar libraries into Haskell's module system. Syntax extensions in SugarHaskell can declare arbitrary context-free and layout-sensitive syntax. SugarHaskell modules are compiled into Haskell modules and further processed by a Haskell compiler. We provide an Eclipse-based IDE for SugarHaskell that is extensible, too, and automatically provides syntax coloring for all syntax extensions imported into a module. We have validated SugarHaskell with several case studies, including arrow notation (as implemented in GHC) and EBNF as a concise syntax for the declaration of algebraic data types with associated concrete syntax. EBNF declarations also show how to extend the extension mechanism itself: They introduce syntactic sugar for using the declared concrete syntax in other SugarHaskell modules.

[1]  Paul J. Layzell The History of Macro Processors in Programming Language Extensibility , 1985, Comput. J..

[2]  Eelco Visser,et al.  Growing a language environment with editor libraries , 2011, GPCE '11.

[3]  John Hughes,et al.  Generalising monads to arrows , 2000, Sci. Comput. Program..

[4]  Donald E. Knuth,et al.  Semantics of context-free languages , 1968, Mathematical systems theory.

[5]  Michael R. Clarkson,et al.  Polyglot: An Extensible Compiler Framework for Java , 2003, CC.

[6]  Torbjörn Ekman,et al.  The jastadd extensible java compiler , 2007, OOPSLA.

[7]  Daan Leijen,et al.  Parsec: direct style monadic parser combinators for the real world , 2001 .

[8]  Jurriaan Hage,et al.  Scripting the type inference process , 2003, ACM SIGPLAN Notices.

[9]  M. Douglas McIlroy Macro instruction extensions of compiler languages , 1960, CACM.

[10]  Eelco Visser,et al.  Generalized type-based disambiguation of meta programs with concrete object syntax , 2005, GPCE'05.

[11]  Eric Van Wyk,et al.  Silver: an Extensible Attribute Grammar System , 2008, Electron. Notes Theor. Comput. Sci..

[12]  S. Doaitse Swierstra,et al.  The architecture of the Utrecht Haskell compiler , 2009, Haskell.

[13]  Jonathan Rees,et al.  Macros that work , 1991, POPL '91.

[14]  Simon L. Peyton Jones,et al.  Template meta-programming for Haskell , 2002, Haskell '02.

[15]  Felix Rieger A language-independent framework for syntactic extensibility , 2012 .

[16]  Steffen Priebe,et al.  Preprocessing eden with template haskell , 2005, GPCE'05.

[17]  Eelco Visser,et al.  Meta-programming with Concrete Object Syntax , 2002, GPCE.

[18]  Paul Klint,et al.  The syntax definition formalism SDF—reference manual— , 1989, SIGP.

[19]  Ross Paterson,et al.  A new notation for arrows , 2001, ICFP '01.

[20]  Sebastian Erdweg,et al.  Language composition untangled , 2012, LDTA.

[21]  Sebastian Erdweg,et al.  Layout-Sensitive Generalized Parsing , 2012, SLE.

[22]  Eelco Visser,et al.  Stratego: A Language for Program Transformation Based on Rewriting Strategies , 2001, RTA.

[23]  Simon Marlow,et al.  Haskell 2010 Language Report , 2010 .

[24]  R. Kent Dybvig,et al.  Revised6 Report on the Algorithmic Language Scheme , 2009 .

[25]  Konstantin Solomatov,et al.  Language Modularization and Composition with Projectional Language Workbenches illustrated with MPS , 2010 .

[26]  Geoffrey Mainland Why it's nice to be quoted: quasiquoting for haskell , 2007, Haskell '07.

[27]  Conor McBride,et al.  Applicative programming with effects , 2008, J. Funct. Program..

[28]  Eelco Visser,et al.  The spoofax language workbench: rules for declarative specification of languages and IDEs , 2010, OOPSLA.

[29]  Sam Tobin-Hochstadt,et al.  Languages as libraries , 2011, PLDI '11.

[30]  Sebastian Erdweg,et al.  SugarJ: library-based syntactic language extensibility , 2011, OOPSLA '11.

[31]  Eelco Visser,et al.  Code generation by model transformation: a case study in transformation modularity , 2008, Software & Systems Modeling.

[32]  Gilad Bracha Pluggable Type Systems , 2004 .