Duncan
Interesting. I'm ccing the TH mailing list. What do TH-afficionados
think?
Nested brackets
~~~~~~~~~~
| > evalProg' = [| evalProg |] --same evalProg as before
|
| > compileProg :: ExpQ -> ExpQ
| > compileProg = $( cogen [| evalProg' |] )
I'm very keen that TH should obey the beta rule. That is, given
x = e
you can always replace x by e. So in this case you ought to be able to
write
compileProg = $(cogen [| [| evalProg |] |])
I'm not saying anything about efficiency yet -- but it ought to be
correct.
===> Conclusion 1: TH should allow nested brackets (and presumably
nested $'s).
Quoting
~~~~~
Consider
v1 = [| x * 7 + x |]
We get a wodge of syntax tree for v1. But consider this:
w = x*7 + x
v2 = [| w |]
We get a tiny syntax tree for v2. How has this happened? Because
every client of v2 (who might say $v) can also "see" w.
We can't do this so easily if x is local:
v3 x = [| x*7 + x |]
Now if we say $(v3 5) we'll get a syntax tree. We can still try lifting
the body out:
v3 x = let w = x*7 + x in [| w |]
Actually this will work if w is "liftable". When we call $(v3 5), we'll
build the code [| 40 |]. Hmm.
===> Observation 2. Something I didn't realise: this forces w.
So substituting for w might change strictness behaviour. Sigh.
However, if w's type is not liftable (e.g. it's Exp, or Tree), then the
program will be rejected as ill typed.
====> Observation 3. I wonder if it's a coincidence that you got away
with the lift-to-top-level trick. In a more general setting you might
not be able to, and then you'd get very heavy encoding.
Double encoding
~~~~~~~~~~
You ask for a function
enc :: Exp t -> Exp (Exp t)
Well that's easy, isn't it?
enc x = return x
I'm not sure if that's what you want. Indeed, you say " So, the thing
that concerns me is the double encoding. I can't intuitively see the
need for it..." Maybe you can give the intuition for why you can't see
the need for it?
I don't think I understand all the issues nearly well enough to comment
further. Maybe you can chew on it with Ian, who is pretty au fait with
TH.
Simon