deriving Typeable

Simon Peyton-Jones simonpj at microsoft.com
Wed Jul 21 07:46:35 EDT 2004


| newtype Y e = Y { unY :: (e (Y e)) }
|     deriving(Data,Typeable,Show,Read,Eq)
| 
| gives
| E.hs:64:
|     Can't make a derived instance of `Typeable (Y e)'
|     (`Y' is parameterised over arguments of kind other than `*')
|     When deriving instances for type `Y'
| 
| Is there any way around this limitation other than manually expanding
Y
| everywhere I want to use it (which I really don't want to do)? Is the
| limitation inherent to the way Typeable works, or is it just that no
one
| has implemented it yet?

You'll want to read "Scrap more boilerplate" in my home page, for an
account of the Typeable classes.  In this case you can make Y Typeable
easily enough, like this (compile with -fglasgow-exts to make the scoped
type variable work):

newtype Y e = Y { unY :: (e (Y e)) } 

instance Typeable1 e => Typeable (Y e) where
   typeOf _ = mkTyConApp yTc [typeOf1 (undefined :: e ())]

yTc :: TyCon
yTc = mkTyCon "Main.Y"


This could be automated, but there's always an arbitrary limit on the
complexity of the kinds of the arguments -- until we get kind
polymorphism, that is!

Making Y an instance of Data is harder.  I'm not sure how to do that.
You'd need something like
	instance Data1 e => Data (Y e)
and we don't have a Data1 class yet.  

Simon


More information about the Glasgow-haskell-users mailing list