[Haskell-cafe] [Probably a dumb question] Quasiquoting

Robert Greayer robgreayer at yahoo.com
Mon Mar 30 18:36:01 EDT 2009

With some more context:

foo = ($expr "1 + 2")


bar = [$expr| 1 + 2]

In the first example (assuming TH is enabled), $expr is a splice, which happens at compile time. 'expr' is some value of type Q Exp (the AST for a Haskell expression, in the quotation monad).  The application of $expr to the value "1 + 2" happens at runtime (assuming $expr splices a value of type String -> a, otherwise its a compile time error).

In the second example, expr is a value of type QuasiQuoter, which contains an element quoteExpr, a function of type String -> Q Exp, which is applied at compile time to the contents of the quasiquotation (" 1 + 2"), and the result spliced in.  The value of 'foo' and 'bar' could work out to be exactly the same, depending on the implementation of expr in each instance.  But the 'work' of expr in the first instance happens when the value of foo is demanded, whereas in the second case, it happens at compile time.

Of course, you could also have:

foo = $(expr "1 + 2")

In this case expr is a function of type String -> Q Exp, which is applied to its argument "1 + 2" at compile time.  It is very similar to the QQ example.  One advantage to qq is that you can do:

foo s = [$expr|
    int main(int argc, char** argv) {
        printf("hello $s!\n");


assuming your expression parser supports anti-quotation.  Also you can in theory put qq's in patterns:

foo y [$expr|printf($_)|] = [$expr|printf($y)|]

although take this example with a grain of salt (I've not played with this aspect of quasiquotation).  Note also that antiquotation syntax is completely up to the QuasiQuoter (the $s, $_, could just as easily be @{s} or ***_***, or something else, depending on the implementation of expr).

----- Original Message ----
From: Andrew Coppin <andrewcoppin at btinternet.com>
To: haskell-cafe at haskell.org
Sent: Monday, March 30, 2009 5:26:28 PM
Subject: [Haskell-cafe] [Probably a dumb question] Quasiquoting

Can somebody explain to me how

[$expr| 1 + 2 |]

is different from

($expr "1 + 2")

Other than a superficially different type signature, I'm not seeing what the fundamental difference is...

Haskell-Cafe mailing list
Haskell-Cafe at haskell.org


More information about the Haskell-Cafe mailing list