[Haskell-cafe] Why doesn't TH expose its parser?

Christopher Done chrisdone at gmail.com
Fri Feb 13 21:32:27 UTC 2015


What's the reason that there isn't a parseExp :: String -> Q Exp,
parseDecl, etc.?

It's a pretty common use case to write some syntactic extension that
merely extends regular Haskell. We end up with the haskell-src-meta
package which doesn't support full GHC Haskell and doesn't know how to
parse infix applications. It doesn't give a strong story for
quasi-quotes to be useful for extensions to Haskell's syntax itself,
as opposed to just mini-DSLs.

Here's a case in point: idiom brackets. You'd like to implement them like this:

[i|foo bar mu|] → foo <$> bar <*> mu

You want it to be full Haskell inside, so foo could easilly be a case
or a let, etc., so a trivial way to do that would be to just parse the
string "foo bar mu" with parseExp and then do the above simple
transformation. Job done.

In practice, we can't do this. Our best option is HSE, but it's not
ideal. Another way to do this is:

$(idiom [|foo bar mu|])

And because [|…|] produces a Q Exp, you can just go ahead and do the
rewrite on the resulting Exp. Yay, we now have full GHC Haskell inside
the idiom bracket, but we've lost the syntactic convenience. Given
that [|…|] already does the String → Q Exp step, why not allow us to
re-use this functionality in quasi quotes?

Ciao!


More information about the Haskell-Cafe mailing list