0

would like to print a random number between 0 and 10, but generate seems undefined can not be compiled, the following code edited from example i am using Haskell Platform 2011.2.0.1

Updated:

import System.IO
import System.Random
import Test.QuickCheck.Function
import Test.QuickCheck.Gen
import Test.QuickCheck

main :: IO() 
main = putStrLn (show result)
  where result = unGen (choose (0, 10)) (mkStdGen 1) 1

The resulting error:

test6.hs:13:25:
    Ambiguous type variable `a0' in the constraints:
      (Random a0) arising from a use of `choose' at test6.hs:13:25-30
      (Show a0) arising from a use of `show' at test6.hs:12:18-21
      (Num a0) arising from the literal `10' at test6.hs:13:36-37
    Probable fix: add a type signature that fixes these type variable(s)
    In the first argument of `unGen', namely `(choose (0, 10))'
    In the expression: unGen (choose (0, 10)) (mkStdGen 1) 1
    In an equation for `result':
        result = unGen (choose (0, 10)) (mkStdGen 1) 1
Daniel Wagner
  • 145,880
  • 9
  • 220
  • 380
M-Askman
  • 400
  • 4
  • 17

1 Answers1

2

Ok, first problem is that let makes a local binding, and you are using it in global scope, if you want the binding to be local to the main action, I would use where to do the binding. Looking at the QuickCheck docs it appears that the generate function no longer exists. unGen has the same type signature so I believe that has replaced it.

import System.Random
import Test.QuickCheck
import Test.QuickCheck.Gen

main :: IO ()
main = putStrLn (show result)
  where result = unGen (choose (0::Int, 10::Int)) (mkStdGen 1) 1
Andrew Marsh
  • 2,032
  • 15
  • 14
  • change mkStdGen to MkGen, can not compile – M-Askman Nov 18 '11 at 07:47
  • @種瓜得瓜種豆得豆 You really need a `StdGen` object. The usage of other random generators is not supported. – fuz Nov 18 '11 at 08:01
  • mkStdGen can use now, but above can not be compiled – M-Askman Nov 18 '11 at 08:14
  • What is the compile error, don't have ghc on this computer, so if you give me the compile error I can work out what is wrong – Andrew Marsh Nov 18 '11 at 08:19
  • Try this, it probably just does not know what type of number to generate. – Andrew Marsh Nov 18 '11 at 08:40
  • 1
    Precisely. GHC infers `result :: (Random a, Show a, Num a) => a`. So far, that's fine (unless you run into the monomorphism restriction). But the desired output is `show result`, and the type of that becomes `(Random a, Show a, Num a) => String`, which is an ambiguous type (there's a type variable in the constraints not appearing on the right of `=>`). The [defaulting rules](http://www.haskell.org/onlinereport/haskell2010/haskellch4.html#x10-790004.3.4) specify under which circumstances such an ambiguity is resolved. Here, `Random` is not defined in one of the standard libs, so cont... – Daniel Fischer Nov 18 '11 at 09:50
  • 1
    ...cont no defaulting is done. In Haskell98, `Random` was one of the standard libraries, so with an old enough compiler (perhaps importing `Random` instead of `System.Random`), I expect that the type would be defaulted to `Integer` (I haven't an old enough ghc to check, 6.12 doesn't default). – Daniel Fischer Nov 18 '11 at 09:56