Extending the reification information of a class with its known instances
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
On 11/4/07, Alfonso Acosta
The problem comes when generating the Dynamic value. There is no way to guarantee that the function.
I meant, "that the function is Typeable, sorry. I should get used to reread before posting.
Reification is precisely supposed to allow you to ask the compiler about the current typing environment. In principle that could reasonably include the instance environment. It gets trickier if 'Type' contains type variables, because then it make a difference what local constraints are in scope. For example are we in the RHS of a function with type Num a => ..., in which case we have a Num dictionary available. But what if the current function has no type signature -- then we don't have declared dictionaries in scope. So I'm not sure what the Right Thing is. Create a feature request to hold the discussion, by all means, though. Simon | -----Original Message----- | From: template-haskell-bounces@haskell.org [mailto:template-haskell-bounces@haskell.org] On Behalf Of Alfonso | Acosta | Sent: 04 November 2007 22:17 | To: template-haskell@haskell.org | Subject: [Template-haskell] Extending the reification information of a class with its known instances | | 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 is Typeable. | | 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 | _______________________________________________ | template-haskell mailing list | template-haskell@haskell.org | http://www.haskell.org/mailman/listinfo/template-haskell
participants (2)
-
Alfonso Acosta -
Simon Peyton-Jones