Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 483
Rafael S. Calsaverini Avatar asked Mar 27 '11 14:03

Rafael S. Calsaverini


People also ask

Does Haskell have syntactic sugar?

Syntactic sugar refers to any redundant type of syntax in a programming language that is redundant to the main syntax but which (hopefully) makes the code easier to understand or write.

What is do Haskell?

do { putStr "Hello" ; putStr " " ; putStr "world!" ; putStr "\n" } (using the optional braces and semicolons explicitly, for clarity). This sequence of instructions nearly matches that in any imperative language. In Haskell, we can chain any actions as long as all of them are in the same monad.


1 Answers

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."

like image 184
Chris Kuklewicz Avatar answered Oct 23 '22 10:10

Chris Kuklewicz