开发者

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 f开发者_JAVA技巧un. Is it possible? What tools should I use?


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜