[Haskell-cafe] some questions about Template Haskell
jwlato at gmail.com
Wed Jul 3 04:36:25 CEST 2013
lift and [| |] give similar results for that very stripped-down example,
but it would be incorrect to extrapolate their behaviors from that case.
They're executed at different times, by different mechanisms, and have
vastly different behavior. It's also best to think of [| |] as having the
type String -> ExpQ, even though the input isn't syntactically quoted.
Suppose you have a slightly different example:
> > let x = 1 :: Int
> > runQ $ lift $ show x
> ListE [LitE (CharL '1')]
> > runQ [| show x |]
> AppE (VarE GHC.Show.show) (VarE x_1627398832)
With lift, the expression is evaluated, then the result '1' is lifted into
an AST. But TH quotes do something entirely different: they lift *the
expression* into an AST. In order to do so, the quoting mechanism needs to
parse its input string, then determine what each identifier is referring to.
When you're defining a function:
> > let p :: (Show a, Lift a) => a -> ExpQ; p n = [| show n |]
The quote has two terms: show and n. 'n' is a lambda-bound value, and show
is free. Free variables are looked up in the environment. That's why we
see 'VarE GHC.Show.show' in the AST above; the fully-qualified Name is
generated and the AST references that name. (numeric and string literals
are represented directly)
This is the key difference between this function definition and running the
splice above: in the function 'n' is lambda-bound, whereas in the above
splice 'x' is a free variable.
Lambda bindings can't be referenced by name because that name may not be in
scope when the generated splice is run. Instead, lambda-bound values must
be lifted directly into the AST, which is exactly what 'lift' does. If we
apply the function to a value, we can see the generated AST:
> > runQ (p 2)
AppE (VarE GHC.Show.show) (LitE (IntegerL 2))
The generated AST has the number 2, and applies the function GHC.Show.show
If we want to show something that doesn't have a Lift instance, we can't do
it directly. However, we can do this:
> > let q :: Show a => a -> ExpQ; q n = [| $(lift $ show n) |]
> > runQ (q 2)
> ListE [LitE (CharL '2')]
Note the differences. We no longer require that 'n' has a Lift instance.
However, the actual value of 'n' never appears in the AST! Instead, we
first show 'n', then lift in the resulting string. The order of operations
is changed too. In the first case, the literal 2 is lifted into the AST
via lift, and the generated splice will apply show to that number whenever
the splice is run. In the second case, (show 2) is evaluated first, then
the result is lifted into the AST (again via lift), causing that string to
be referenced within the splice.
On Wed, Jul 3, 2013 at 5:44 AM, TP <paratribulations at free.fr> wrote:
> John Lato wrote:
> >> Now, I have found another behavior difficult to understand for me:
> >> > runQ $ lift "u"
> >> ListE [LitE (CharL 'u')
> >> > runQ $ [| "u" |]
> >> LitE (StringL "u")
> >> So we have similar behaviors for lift and [||]. We can check it in a
> >> splice:
> >> > $( [| "u" |] )
> >> "u"
> >> > $( lift "u" )
> >> "u"
> >> But if I replace a working version:
> >> pr n = [| putStrLn ( $(lift( nameBase n ++ " = " )) ++ show $(varE n) )
> >> |] ----- case (i) -----
> >> by
> >> pr n = [| putStrLn ( $([| (nameBase n) ++ " = " |]) ++ show $(varE n) )
> >> |] ----- case (ii) -----
> >> I again get the error
> > In the working version, 'n' appears inside a splice, whereas in the other
> > n
> > is in a quote. AFAIK any value can be used in a splice (provided it
> > the staging restrictions), whereas only Lift-able values can be used in a
> > quote.
> If I take this as a granted axiom, then I can admit the behavior above
> (error in case (ii), whereas it is working in case (i)) because n is a
> (Name), and so is not instance of Lift. Thus we are compelled to use lift
> instead of [||] (although the behavior is about the same for both in simple
> examples, as shown in my example above for "u").
> I do not understand the exact reason for that, but I can do without; and
> maybe it is better, because I am very probably not enough experienced to
> understand the details (and the reason is perhaps not trivial when I read
> Oleg who writes that what gives an error above in Haskell works in
> What is strange is that:
> * in the version using "lift", the definition of lift asks for the output
> (nameBase n) to be an instance of Lift, what is the case because it is a
> string (cf my previous post in this thread).
> * whereas in the second version, we ask for n, not (nameBase n), to be an
> instance of Lift.
> Anyway, if we admit your axiom as granted, then we can also admit that the
> following version does not work (version of my initial post):
> >> >> pr :: Name -> ExpQ
> >> >> pr n = [| putStrLn $ (nameBase n) ++ " = " ++ show $(varE n) |]
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Haskell-Cafe