Type inference in ST monad?

I have the following code which does not compile due to explicit type annotation (ST s Double). Error message says usual thing about "s" type variables. ---------------------------------------------------------------------------------------------------------- import Control.Monad.ST import System.Random.MWC (initialize, uniformR, Gen) import Control.Monad.Loops (whileM) import Data.Vector (singleton) import Data.Word(Word32) main :: IO () main = do print $ runSimulation 1 runSimulation :: Word32 -> [Int] runSimulation seed = runST $ do gen <- initialize (singleton seed) whileM (do r1 <- uniformR (-1.0, 1.0) gen :: ST s Double -- does not compile due to this if r1 > 0.0 then return True else return False) (do r2 <- uniformR (0, 10) gen if r2 > 5 then return r2 else return 0) --------------------------------------------------------------------------------------------------------- if I rewrite runSimulation like this (below), everything is OK. --------------------------------------------------------------------------------------------------------- runSimulation :: Word32 -> [Int] runSimulation seed = runST $ do gen <- initialize (singleton seed) whileM (do r1 <- tempFun gen if r1 > 0.0 then return True else return False) (do r2 <- uniformR (0, 10) gen if r2 > 5 then return r2 else return 0) where tempFun :: Gen s -> ST s Double -- this line automatically provides required type annotation tempFun g = uniformR (-1.0, 1.0) g --------------------------------------------------------------------------------------------------------- Ca somebody explain what's wrong with the first version? Best Regards, Aurimas

I'm not exactly sure why you get the error, but the easiest way to fix it
is just to type it this way:
runSimulation :: Word32 -> [Int]
runSimulation seed = runST $ do
gen <- initialize (singleton seed)
whileM (do r1 <- uniformR (-1.0, 1.0 :: Double) gen
if r1 > 0.0 then return True else return False)
(do r2 <- uniformR (0, 10 :: Int) gen
if r2 > 5 then return r2 else return 0)
It has something to do with the forall s in runST, although I'm not
completely sure what.
On Sun, Aug 18, 2013 at 9:18 PM, Aurimas
I have the following code which does not compile due to explicit type annotation (ST s Double). Error message says usual thing about "s" type variables.
------------------------------**------------------------------** ------------------------------**---------------- import Control.Monad.ST import System.Random.MWC (initialize, uniformR, Gen) import Control.Monad.Loops (whileM) import Data.Vector (singleton) import Data.Word(Word32)
main :: IO () main = do print $ runSimulation 1
runSimulation :: Word32 -> [Int] runSimulation seed = runST $ do gen <- initialize (singleton seed) whileM (do r1 <- uniformR (-1.0, 1.0) gen :: ST s Double -- does not compile due to this if r1 > 0.0 then return True else return False) (do r2 <- uniformR (0, 10) gen if r2 > 5 then return r2 else return 0) ------------------------------**------------------------------** ------------------------------**---------------
if I rewrite runSimulation like this (below), everything is OK.
------------------------------**------------------------------** ------------------------------**--------------- runSimulation :: Word32 -> [Int] runSimulation seed = runST $ do gen <- initialize (singleton seed) whileM (do r1 <- tempFun gen if r1 > 0.0 then return True else return False) (do r2 <- uniformR (0, 10) gen if r2 > 5 then return r2 else return 0) where tempFun :: Gen s -> ST s Double -- this line automatically provides required type annotation tempFun g = uniformR (-1.0, 1.0) g ------------------------------**------------------------------** ------------------------------**---------------
Ca somebody explain what's wrong with the first version?
Best Regards, Aurimas
______________________________**_________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/**mailman/listinfo/beginnershttp://www.haskell.org/mailman/listinfo/beginners

On 08/19/2013 05:04 AM, David McBride wrote:
I'm not exactly sure why you get the error, but the easiest way to fix it is just to type it this way:
runSimulation :: Word32 -> [Int] runSimulation seed = runST $ do gen <- initialize (singleton seed) whileM (do r1 <- uniformR (-1.0, 1.0 :: Double) gen if r1 > 0.0 then return True else return False) (do r2 <- uniformR (0, 10 :: Int) gen if r2 > 5 then return r2 else return 0)
It has something to do with the forall s in runST, although I'm not completely sure what.
Thanks, this is clearly the most easy way to fix the problem. But the question remains - why the line "do r1 <- uniformR (-1.0, 1.0 :: Double) gen" cannot be annotated with ST s Double?

On Mon, Aug 19, 2013 at 1:36 AM, Aurimas
Thanks, this is clearly the most easy way to fix the problem. But the question remains - why the line "do r1 <- uniformR (-1.0, 1.0 :: Double) gen" cannot be annotated with ST s Double?
Maybe you need ScopedTypeVariables? But I'd be suspicious that, given the forall in the definition of ST, that such a type annotation *always* creates a new `s` and some other way to "force" the type is needed. -- brandon s allbery kf8nh sine nomine associates allbery.b@gmail.com ballbery@sinenomine.net unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net

On 08/19/2013 04:59 PM, Brandon Allbery wrote:
On Mon, Aug 19, 2013 at 1:36 AM, Aurimas
mailto:aurimas.anskaitis@vgtu.lt> wrote: Thanks, this is clearly the most easy way to fix the problem. But the question remains - why the line "do r1 <- uniformR (-1.0, 1.0 :: Double) gen" cannot be annotated with ST s Double?
Maybe you need ScopedTypeVariables? But I'd be suspicious that, given the forall in the definition of ST, that such a type annotation *always* creates a new `s` and some other way to "force" the type is needed.
ScopedTypeVariables does not solve the problem. As you noticed, new s is created and runST is not happy about this. It is strange that this type can be "forced" replacing given line by a function returning ST s a.
participants (3)
-
Aurimas
-
Brandon Allbery
-
David McBride