Is it possible to roll your own syntax sugar (like do-notation, or arrow-notation) in Haskell?
Asked Answered
S

2

19

Well, the question is self-explicative. Suppose I want to implement some special syntax just for fun. Is it possible? What tools should I use?

Sadowski answered 27/3, 2011 at 14:55 Comment(0)
I
17

There is no such meta-syntax in the Haskell standard, but there is in GHC. You can make almost any notation you want using the GHC "quasi-quotation" facilities (which are different from GHC's "Template Haskell" facility).

The GHC user guide on this is quite short, and mainly points to haskell wiki page on Quasiquotation and the implementor's home page, both of these point to the original publication: "Why It’s Nice to be Quoted: Quasiquoting for Haskell".

People have used quasi-quotation for embedding XML syntax, regular expressions, special string and text forms, and in "JMacro is a library for the programmatic generation of Javascript code."

Inner answered 27/3, 2011 at 16:2 Comment(0)
E
8

I guess, the best way would be to write a custom preprocessor. Several syntactic extensions started out from this approach. For instance, see:

  • DrIFt: Automatized generation of type class instances
  • arrowp: Preprocessor for arrow notation
  • c2hs: Automated generation of marshalling informations and type-signatures for FFI-bindings

and many more. The other way is to patch GHC, but this approach is rather difficult.

Evelynneven answered 27/3, 2011 at 15:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.