RE: [Template-haskell] RE: Question regarding template Haskell
I missed that message. How does rType differ from typeOf, defined in Data.Typeable? simon | -----Original Message----- | From: ozone@algorithm.com.au [mailto:ozone@algorithm.com.au] | Sent: 17 September 2003 11:04 | To: Simon Peyton-Jones | Cc: template-haskell@haskell.org; diyu60607@yahoo.com | Subject: Re: [Template-haskell] RE: Question regarding template Haskell | | On 17/09/2003, at 7:45 PM, Simon Peyton-Jones wrote: | | > | Hi, I have a question regarding template Haskell: from | > | the documentation, I have the impression that it is | > | impossible to reify the type of the variable passed to | > | the template function, is this true? Or, in other | > | words, can I write a function like: | > | | > | getTupleCount :: a -> ExpQ | > | | > | getTupleCount x= | > | do | > | t <- (reifyType x) | > | --work on t | > | ... | | If your 'x' variable is monomorphic, you can use the rType function | that Sean and I came up with, detailed here: | | | http://www.haskell.org/pipermail/template-haskell/2003-July/000128.html | | That currently handles only 2-tuples, but it shouldn't be too hard to | extend it to work on any-size tuples. Let me know if you're having | problems. | | | -- | % Andre Pang : trust.in.love.to.save
On 17/09/2003, at 9:07 PM, Simon Peyton-Jones wrote:
| If your 'x' variable is monomorphic, you can use the rType function | that Sean and I came up with, detailed here: | | http://www.haskell.org/pipermail/template-haskell/2003-July/ 000128.html | | That currently handles only 2-tuples, but it shouldn't be too hard to | extend it to work on any-size tuples. Let me know if you're having | problems.
I missed that message. How does rType differ from typeOf, defined in Data.Typeable?
typeOf :: a -> TypeRep rType :: a -> Typ (or :: a -> Type in the new THSyntax definitions). The two are conceptually the same, but they return you different data types: typeOf returns you the TypeRep which the Data.Dynamic functions use, and rType returns you a Typ (now Type) which THSyntax uses. Another way to think about it is that rType is Template Haskell's reifyType as a function. The point is that you can use rType to return you the definition of a Type given in THSyntax, which you can then use directly within THSyntax: *Mocha.ReifyType> Data.Dynamic.typeOf (undefined :: (Int, Char)) (Int,Char) *Mocha.ReifyType> rType (undefined :: (Int, Char)) AppT (AppT (TupleT 2) (ConT "GHC.Base:Int")) (ConT "GHC.Base:Char") *Mocha.ReifyType> Come to think of it, now that we have a concrete syntax, perhaps it'd be worth merging the definitions of THSyntax's Type and Data.Dynamic's TypeRep? They're conceptually the same thing, but with different data representations ... A slightly wilder idea (which is not a serious suggestion) would be to use Template Haskell to implement Dynamics. If there's a pre-defined "Dynamic" data type, all the various toDyn and fromDyn functions could be meta-programmed so that they emit code to convert to and from the Dynamic type. Effectively you could use Template Haskell as a generics/polytopic system. I have no idea whether this is possible, it's just a fleeting thought. BTW, you didn't miss that message Simon -- you even replied[1] to it :). 1. http://www.haskell.org/pipermail/template-haskell/2003-July/000129.html -- % Andre Pang : trust.in.love.to.save
participants (2)
-
ozone@algorithm.com.au -
Simon Peyton-Jones