
Hi all, This works fine, as expected
f :: (Num a, Random a) => Int -> [a] f = randomRs (0,1).mkStdGen
If I skip the type signature, though, I get the following error messages: Main.hs:14: Ambiguous type variable(s) `a' in the constraint `Random a' arising from use of `randomRs' at Main.hs:14 In the first argument of `(.)', namely `randomRs (0, 1)' In the definition of `f': (randomRs (0, 1)) . mkStdGen Main.hs:14: Ambiguous type variable(s) `a' in the constraint `Num a' arising from the literal `1' at Main.hs:14 In the first argument of `randomRs', namely `(0, 1)' In the first argument of `(.)', namely `randomRs (0, 1)' Why exctly can't ghci figure how the type of f? This just happens when loading the code from a file,
:t randomRs (0,1).mkStdGen in ghci works fine.
J.A.

Jorge Adriano wrote:
This works fine, as expected
f :: (Num a, Random a) => Int -> [a] f = randomRs (0,1).mkStdGen
If I skip the type signature, though, I get the following error messages: ...
You just tripped over the infamous monomorphic restriction of Haskell: A variable that is defined like a constant (no arguments before "=") is not allowed to be overloaded (have a class context), except when the type of the variable is explicitly given. Maybe it would be possible to generate a more informative type error message in such cases? Olaf -- OLAF CHITIL, Dept. of Computer Science, The University of York, York YO10 5DD, UK. URL: http://www.cs.york.ac.uk/~olaf/ Tel: +44 1904 434756; Fax: +44 1904 432767

This works fine, as expected
f :: (Num a, Random a) => Int -> [a] f = randomRs (0,1).mkStdGen
If I skip the type signature, though, I get the following error messages: ...
You just tripped over the infamous monomorphic restriction of Haskell: A variable that is defined like a constant (no arguments before "=") is not allowed to be overloaded (have a class context), except when the type of the variable is explicitly given. Oh, yes of course... I knew that :) Sorry to waste your time over this, thanks!
J.A.
participants (2)
-
Jorge Adriano
-
Olaf Chitil