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

Simon Peyton-Jones simonpj at microsoft.com
Mon Nov 8 07:34:15 EST 2004


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

As I think you now know, the above declaration is fine if you use
-fglasgow-exts.  Haskell 98 does not support lexically scoped type
variables, but GHC -fglasgow-exts does.  You can read about it in the
GHC user manual.  

Type variables are bound by instance declarations, as above, but you can
also bind them in patterns.  So as others have said an alternative is

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

But that's not legal in Haskell 98 either, and since 'a' is already in
scope with -fglasgow-exts, the extra binding for 'b' seems less nice.

The best Haskell 98 solution is the one Remi gives:

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

Simon



More information about the Haskell-Cafe mailing list