
That's it !
Indeed, you do not create a STRef yourself using a STRef data constructor,
you use the function:
newSTRef :: a -> GHC.ST.ST s (STRef s a)
Thanks.
2010/7/4 Dan Doel
On Sunday 04 July 2010 5:41:07 am Yves Parès wrote:
Okay, I understand better, now. But I could never have guessed it just from the GHC error message.
Another question on the same code:
import Control.Monad.Identity
newtype SomeMonad s a = SomeMonad { unSome :: Identity a } deriving (Monad)
newtype SomeType s = SomeType Int
runSomeMonad :: (forall s. SomeMonad s a) -> a runSomeMonad x = runIdentity . unSome $ x
foo :: SomeType s foo = runSomeMonad (return $ SomeType 42)
According to what I read about ST, it should not compile because of 'foo' (hence the protection), well it does. What have I forgotten in my code?
The s in your SomeType isn't linked in any way to the variable quantified in the SomeMonad. You're producing:
return $ SomeType 42 :: forall t. SomeMonad t (SomeType s)
and running it to get the SomeType s. You need something to tie them together, like:
mkSomeType :: Int -> SomeMonad s (SomeType s) mkSomeType i = return (SomeType i)
and then hide the SomeType constructor, perhaps.
-- Dan