[Template-haskell] Extending the reification information of a class with its known instances

Alfonso Acosta alfonso.acosta at gmail.com
Sun Nov 4 17:17:07 EST 2007


Hi,

I don't know whether this can be considered a generally desired
feature, that's why I didn't create the ticket directly.

I have to code a TH processing function, which, due to the untyped
nature of the TH's AST, as it seems, would only be able to guarantee
generating valid code if instance information of a class could be
obtained.

In my concrete case I have to, among other things, keep the AST of a
function (for later processing by my embedded compiler) and it's value
in Dynamic form (for later simulation of my embedded language).

-- phantom parameter to guarantee type correctness
newtype MyFun a = MyFun MyFunPrim

data MyFunPrim = MyFun Name [Clause] Dynamic

My TH constructor function has the type

mkMyFun :: Q [Dec] -> ExpQ

and a (naive) concrete example of use could be

myFun1 :: MyFun (Bool -> Bool)
myFun1 = $(mkMyFun [d| and :: Bool -> Bool
                                               and a b = a && b |])

The problem comes when generating the Dynamic value. There is no way
to guarantee that the function.

The only solutions I can think of to solve my problem are:

a) The hard way: providing a catching mechanism badly generated code.
b) The cleaner way: Providing a mechanism to check whether the
function passed to mkMyFun is Typeable.

something like

isClassInstance :: Name -- ^ Class name
                            -> Type   -- ^ Type to check
                            -> Bool

(and maybe c)  Hope for a new TH with a typed AST  which could let me
expess type constraints in declarations and expressions.)

Right now, I can only expect the user to be nice and only supply
Typeable functions.

Cheers,

Fons


More information about the template-haskell mailing list