
My brain is too small to accommodate all this, and I agree with Pedro that we should keep the "splitting TypeRep" question separate from the "derive Typeable for everything" question.
This response is only about splitting TypeReps. I think Gabor's proposal below will lead to lots of ambiguity, because there is no way to fix the result type of 'split' except by giving it a type signature, which seems a bit clumsy.
Here are some related suggestions though. Consider:
· Currently TypeRep is not parameterised (ie not TypeRep a), for good reason.
· But consider Gabor's Dict (Typeable t). It is a data constructor containing a dictionary that contains the typeOf function, whose only payload is a TypeRep. So in effect, Dict (Typeable t) is a type-parameterised versoin of TypeRep.
OK suppose we pulled that out, so we have (hidden in the library)
class Typeable a where
typeOf :: Proxy a -> TypeRep
newtype PTypeRep a where
PTR :: Typeable a => PTypeRep a
ptr :: Typeable a => Proxy a => PTypeRep a
ptr _ = PTR
ptrToTR :: forall a. PTypeRep a -> TypeRep
ptrToTR PTR = typeOf (undefined :: Proxy a)
(Pay no attention to the choice of names.) Now PTypeRep is the type-parameterised version of TypeRep. I'm guessing this is a generally-useful thing to have, because it's a first-class Typeable dictionary.
· You can get it from Typeable via 'ptr'
· By pattern matching on it you can bring the Typable dictionary into scope if you want.
· You can drop down to TypeRep via ptrToTR.
Now to split. This has to be unsafe, just like Typeable.cast is.
right :: forall f a. PTypeRep (f a) -> PTypeRep a
left PTR = let instance Typeable f where
typeOf _ = case typeOf (undefined :: Proxy (f a)) of
TyConApp tc tys -> last tys
in PTR
We can get the TypeRep for f by decomposing the TypeRep for (f a). But then we need a dictionary for Typable a, and we don't have a way to build that in Haskell. But it's trivial in Core. And unsafe of course.
I'm not sure I have all this right, and I'm not at all sure that it's urgent. But I thought I'd jot it down in case it's useful.
Simon
| -----Original Message-----
| From: Gábor Lehel [mailto:illissius@gmail.com
]
| Sent: 14 October 2012 14:34
| To: Simon Peyton-Jones
| Cc: Dominique Devriese; José Pedro Magalhães; libraries@haskell.org
| Subject: Re: Changes to Typeable
|
| On Sun, Oct 14, 2012 at 2:34 PM, Gábor Lehel