Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haskell: automatically de-sugar syntax

Is there any function in Haskell that, given Haskell syntax input, return an equivalent expression with all syntactical sugar expanded?

For example, in Clojure, I can pass 'a to the reader, and it will return (quote a) which helps me learn that ' is just the sugared form of the quote function.

like image 915
bukzor Avatar asked Jan 30 '14 17:01

bukzor


1 Answers

The Haskell Report is quite short and covers all the transformations of interest. In particular, you will probably like the sections on list comprehensions, do syntax, and implicit layout (informal, formal).

You can also ask GHC to dump its idea of what your code should look like with its numerous -ddump options; in particular I stare at -ddump-simpl from time to time. However, there's a huge caveat here: the Report does not demand that the transformations it specifies be implemented as an actual desugaring-to-kernel-Haskell phase, only that the implementation must behave the same way as the given desugaring to kernel Haskell. So GHC takes that leeway and desugars directly to its own internal language; and -ddump-simpl will show you terms in that internal language, not in Haskell. On the other hand, it is more complete than the Report in the sense that it is completely aware of the desugaring done by any language extension GHC knows (whereas the Report obviously does not cover any language extensions).

like image 85
Daniel Wagner Avatar answered Sep 20 '22 13:09

Daniel Wagner