[Haskell-cafe] Making MVar and Chan Instances of Typeable

Koji Nakahara yu- at div.club.ne.jp
Fri Nov 5 10:44:41 EST 2004


On Fri, 5 Nov 2004 14:43:55 +0100
Benjamin Franksen <benjamin.franksen at bessy.de> wrote:
<snip>
> the instances by hand. My first attempt was:
> 
> instance Typeable a => Typeable (MVar a) where
>   typeOf x =
>     mkAppTy (mkTyCon "Control.Concurrent.MVar.MVar") [typeOf (undefined::a)]
> 
> but unfortunately this doesn't work. Ghc complains about 
> 
> Ambiguous type variable `a1' in the top-level constraint:
>       `Typeable a1' arising from use of `typeOf' at Helpers.hs:8
> 
> The reason is apparently that inside the definition of typeOf the type 
> variable 'a' is not unified with the 'a' from the instance header. I could 
> write 


You can write:

instance Typeable a => Typeable (MVar a) where
typeOf (x :: MVar a) =
	mkAppTy (mkTyCon "Control.Concurrent.MVar.MVar") [typeOf (undefined::a)]


Hope it helps,
Koji Nakahara


More information about the Haskell-Cafe mailing list