Type inference in ST monad?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Type inference in ST monad?

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




Reply | Threaded
Open this post in threaded view
|

Type inference in ST monad?

David McBride
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 <aurimas.anskaitis at vgtu.lt> wrote:

> 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 at haskell.org
> http://www.haskell.org/**mailman/listinfo/beginners<http://www.haskell.org/mailman/listinfo/beginners>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20130818/be2f64e1/attachment.htm>

Reply | Threaded
Open this post in threaded view
|

Type inference in ST monad?

Aurimas
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?



Reply | Threaded
Open this post in threaded view
|

Type inference in ST monad?

Brandon Allbery
On Mon, Aug 19, 2013 at 1:36 AM, Aurimas <aurimas.anskaitis at 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.

--
brandon s allbery kf8nh                               sine nomine associates
allbery.b at gmail.com                                  ballbery at sinenomine.net
unix, openafs, kerberos, infrastructure, xmonad        http://sinenomine.net
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20130819/dbf6a21d/attachment.htm>

Reply | Threaded
Open this post in threaded view
|

Type inference in ST monad?

Aurimas
On 08/19/2013 04:59 PM, Brandon Allbery wrote:

> On Mon, Aug 19, 2013 at 1:36 AM, Aurimas <aurimas.anskaitis at vgtu.lt
> <mailto:aurimas.anskaitis at 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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20130819/c4d784c1/attachment.htm>