
This works: Prelude System.Random> do { randomRIO (1,6) >>= (\x -> putStrLn $ "Value = " ++ show x) } Value = 5 So does this: Prelude System.Random> do { x <- randomRIO (1,6); putStrLn $ "Value = " ++ show x } Value = 2 But not this: 1 import Control.Monad 2 import System.Random 3 4 foo :: IO () 5 foo = do 6 x <- randomRIO (1,6) 7 putStrLn $ "Value = " ++ show x foo.hs:6:18: Ambiguous type variable `t' in the constraints: `Num t' arising from the literal `1' at foo.hs:6:18 `Random t' arising from a use of `randomRIO' at foo.hs:6:7-21 Probable fix: add a type signature that fixes these type variable(s) Failed, modules loaded: none. Prelude System.Random> Or this: 1 import Control.Monad 2 import System.Random 3 4 foo :: IO () 5 foo = randomRIO (1,6) >>= (\x -> putStrLn $ "Value = " ++ show x) foo.hs:5:17: Ambiguous type variable `t' in the constraints: `Num t' arising from the literal `1' at foo.hs:5:17 `Random t' arising from a use of `randomRIO' at foo.hs:5:6-20 Probable fix: add a type signature that fixes these type variable(s) Failed, modules loaded: none. How to fix? Michael

You have to fix the type of 1 and 6, e.g. by writing
x <- randomRIO (1, 6) :: IO Int
or
x <- randomRIO (1, 6 :: Int)
GHCi defaults integral numbers to Int, that's why it works there.
On 24 July 2010 21:36, michael rice
This works:
Prelude System.Random> do { randomRIO (1,6) >>= (\x -> putStrLn $ "Value = " ++ show x) } Value = 5
So does this:
Prelude System.Random> do { x <- randomRIO (1,6); putStrLn $ "Value = " ++ show x } Value = 2
But not this:
1 import Control.Monad 2 import System.Random 3 4 foo :: IO () 5 foo = do 6 x <- randomRIO (1,6) 7 putStrLn $ "Value = " ++ show x
foo.hs:6:18: Ambiguous type variable `t' in the constraints: `Num t' arising from the literal `1' at foo.hs:6:18 `Random t' arising from a use of `randomRIO' at foo.hs:6:7-21 Probable fix: add a type signature that fixes these type variable(s) Failed, modules loaded: none. Prelude System.Random>
Or this:
1 import Control.Monad 2 import System.Random 3 4 foo :: IO () 5 foo = randomRIO (1,6) >>= (\x -> putStrLn $ "Value = " ++ show x)
foo.hs:5:17: Ambiguous type variable `t' in the constraints: `Num t' arising from the literal `1' at foo.hs:5:17 `Random t' arising from a use of `randomRIO' at foo.hs:5:6-20 Probable fix: add a type signature that fixes these type variable(s) Failed, modules loaded: none.
How to fix?
Michael
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

Thanks, Tobias. I figured it was something like that but lack the syntax expertise on where to put it.
MIchael
--- On Sat, 7/24/10, Tobias Brandt

On 25 July 2010 05:50, Tobias Brandt
You have to fix the type of 1 and 6, e.g. by writing x <- randomRIO (1, 6) :: IO Int or x <- randomRIO (1, 6 :: Int) GHCi defaults integral numbers to Int, that's why it works there.
The default numeric type is Integer, not Int. -- Ivan Lazar Miljenovic Ivan.Miljenovic@gmail.com IvanMiljenovic.wordpress.com

Ivan Miljenovic wrote:
On 25 July 2010 05:50, Tobias Brandt
wrote: You have to fix the type of 1 and 6, e.g. by writing x <- randomRIO (1, 6) :: IO Int or x <- randomRIO (1, 6 :: Int) GHCi defaults integral numbers to Int, that's why it works there.
The default numeric type is Integer, not Int.
Isn't there a "default" declaration where you can specify type defaulting? At least, "default" appears to be a Haskell language keyword - much to my irritating the other day... ;-)

On Sunday 25 July 2010 14:12:03, Andrew Coppin wrote:
Ivan Miljenovic wrote:
On 25 July 2010 05:50, Tobias Brandt
wrote: You have to fix the type of 1 and 6, e.g. by writing x <- randomRIO (1, 6) :: IO Int or x <- randomRIO (1, 6 :: Int) GHCi defaults integral numbers to Int, that's why it works there.
The default numeric type is Integer, not Int.
Isn't there a "default" declaration where you can specify type defaulting?
Yes, you can have one default declaration per module. The default default is (Integer, Double), meaning Integer is chosen if it's possible, otherwise Double is tried (for Fractional, Floating, ... constraints), if that doesn't satisfy all constraints either, error out with Ambiguous type variable ...

Daniel Fischer wrote:
On Sunday 25 July 2010 14:12:03, Andrew Coppin wrote:
Isn't there a "default" declaration where you can specify type defaulting?
Yes, you can have one default declaration per module.
Not a feature I ever use. I just vaguely remembered reading about it somewhere... (No idea if it has any effect on GHCi though.)

On Sunday 25 July 2010 14:47:41, Andrew Coppin wrote:
Daniel Fischer wrote:
On Sunday 25 July 2010 14:12:03, Andrew Coppin wrote:
Isn't there a "default" declaration where you can specify type defaulting?
Yes, you can have one default declaration per module.
Not a feature I ever use. I just vaguely remembered reading about it somewhere... (No idea if it has any effect on GHCi though.)
ghci ignores default declarations (but has extended defaulting rules), so at the ghci prompt, you always get Integer for a generic Num type.

On Saturday 24 July 2010 21:36:14, michael rice wrote:
This works:
Prelude System.Random> do { randomRIO (1,6) >>= (\x -> putStrLn $ "Value = " ++ show x) } Value = 5
So does this:
Prelude System.Random> do { x <- randomRIO (1,6); putStrLn $ "Value = " ++ show x } Value = 2
But not this:
1 import Control.Monad 2 import System.Random 3 4 foo :: IO () 5 foo = do 6 x <- randomRIO (1,6) 7 putStrLn $ "Value = " ++ show x
foo.hs:6:18: Ambiguous type variable `t' in the constraints: `Num t' arising from the literal `1' at foo.hs:6:18 `Random t' arising from a use of `randomRIO' at foo.hs:6:7-21 Probable fix: add a type signature that fixes these type variable(s) Failed, modules loaded: none. Prelude System.Random>
The x has type (Num a, Random a) => a so GHC doesn't know which type to choose. GHCi uses extended defaulting rules and picks Integer. Fix, tell GHC which type to choose: putStrLn $ "Value = " ++ show (x :: Integer) or x <- randomRIO (1,6) :: IO Integer
Or this:
1 import Control.Monad 2 import System.Random 3 4 foo :: IO () 5 foo = randomRIO (1,6) >>= (\x -> putStrLn $ "Value = " ++ show x)
foo.hs:5:17: Ambiguous type variable `t' in the constraints: `Num t' arising from the literal `1' at foo.hs:5:17 `Random t' arising from a use of `randomRIO' at foo.hs:5:6-20 Probable fix: add a type signature that fixes these type variable(s) Failed, modules loaded: none.
How to fix?
Michael
participants (5)
-
Andrew Coppin
-
Daniel Fischer
-
Ivan Miljenovic
-
michael rice
-
Tobias Brandt