[Haskell-cafe] using Typeable with STRefs

Michael Vanier mvanier42 at gmail.com
Mon Mar 16 20:48:45 EDT 2009


I'm having a problem using Typeable with STRefs.  Basically, I want to 
store STRefs (among other things) in a universal type.  STRef is an 
instance of Typeable2, which means that STRef s a is Typeable if s and a 
are both Typeable.  The problem is that the state type s is opaque and I 
can see no way to make it Typeable (other than making it RealWorld, and 
I don't want to use IO for this).  If this is the case, then AFAICT 
there is no point in having STRefs be instances of Typeable2.  Am I 
missing something?

Here's the code I'd like to write:

import Data.Typeable
import Data.STRef
import Control.Monad.ST

data Value = forall a . Typeable a => V a
  deriving Typeable

getValue :: Typeable a => Value -> Maybe a
getValue (V v) = cast v

-- I need the Typeable s constraint for the code to compile, but I'd 
rather leave it out.
test :: Typeable s => ST s Integer
test = do ref <- newSTRef (10 :: Integer)
          let refVal = V ref
          case getValue refVal of
            Nothing -> error "BAD"
            Just r -> readSTRef r
-- This doesn't compile, because s is not Typeable.       
test2 :: Integer
test2 = runST test

Thanks in advance,


More information about the Haskell-Cafe mailing list