[Template-haskell] reifyDecl problem

Simon Peyton-Jones simonpj@microsoft.com
Tue, 22 Jul 2003 08:46:29 +0100


| > But Section 8.1 of "Template Meta-programming for Haskell" says:
| >
| > "In a similar way, reifyDecl f, gives a data structure
| > that represens the value declaration for f;..."
| >
| > and later "... useful to restrict the use of reifyDecl to type
| > constructors, classes, or variables (e.g. functions) declared at the
| > top level..."
| >
| >  From that I made a conclusion that it is possible to reify function
| > declarations (the most interesting ability of TH to me comparing
with
| > C++, for instance). And I still hope :) that it is possible and the
| > error message above is just due to mere bug.
|=20
| This you definitely can't do and oh, how I wish you could.  The
closest
| you can get is to define a function as reified code and then splice
it.
| Example:
|=20
| d_fun =3D [d| fun =3D <function body> |]

You can certainly consider it a missing feature, if not exactly a bug
(though the error message is horrible). =20

I think you'll always be restricted to functions defined in the same
module.  For example

	import Prelude
	foo =3D $(do_it (reifyDecl map))

won't work, because 'map' isn't defined in this module.  To make this
work, I'd need to store the original source code for every function in
each module's interface file, just in case someone later imported and
reified it.  But it seems a bit unpleasant for module boundaries to be
significant in this way.  I'm not sure what to do about this.

I'd be interested to know whether the same-module restriction would
defeat the purposes for the "oh, how I wish I could" applications.

simon
Simon