2

Hi i have the following code

import Data.Maybe
import Test.QuickCheck
import System.Random


rndExpr :: Gen Expr ->  IO Expr
rndExpr gen = do
    rnd <-  newStdGen
    return (generate 5 rnd gen)

But i get "not in scope "generate", why is this so?

Regards Darren

Edit i am importing Test.QuickCheck but it still complaints about the "generate" is not in scope.

Edit 2

How would you write this function so that it would work with quickcheck version 2? I simple tried to put "unGen" where generate was with no succsess, i also installed quickcheck v 2 (cabal install QuickCheck-2.1.0.3)

I need a function with following properties stdGen->Gen Expr->Expr' and unGen seem to give me that functionality, but as I said, my compiler cant find that function. Are there any other functions that I could use for this problem?

2
  • Your edit still provides incomplete information. What version of quickcheck are you using? QuickCheck2 doesn't export generate afaik. Your code snippet works find with QC1 provided I fix the unicode arrows (please don't do that) and add type Expr = Int. Commented Dec 6, 2010 at 22:15
  • Dont know how you check what version im using, but im assume that im using version 1, and therefore i want to use the "generate" function in quickcheck. But if Im change my question, are there any other way of writing this function? Commented Dec 6, 2010 at 23:56

2 Answers 2

1

It seems like you are using generators from Test.QuickCheck, and generate is a function from version 1 of quickCheck. In version 2 of quickCheck things are a bit different so there is no such function. However, you atleast need to import Test.QuickCheck, and similar functionality can be gotten from unGen like this:

rundExpr gen = fmap (flip (unGen gen) 5) newStdGen

Please note that unGen is in Test.QuickCheck.Gen so you have to import that too.

Sign up to request clarification or add additional context in comments.

Comments

1

generate isn't a function in System.Random. Perhaps you are looking for next?

EDIT: Let me be clear: I don't know why you are using QuickCheck/Arbitrary for a task that Random/MonadRandom would seem to be more fitting. I'll assume you considered your options and move on.

Must you select your generator? Can't you use sample' :: Gen a -> IO a?

getVal :: IO a
getVal = sample' arbitrary

This should work for QC2.

OTOH, if you really want to use your own StdGen (or want to avoid IO) then try:

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

func :: StdGen -> Int
func g = unGen arbitrary g 0

This will use the StdGen named g and a count (0 here,) to generate your value. Because unGen doesn't step the generator, and the counter stepping doesn't give good randomness properties (it seems, you can try and see for yourself) you might end up wanting to wrap this with something that generates StdGens (yuck).

If you don't know what version package you are using then run:

$ ghc-pkg list | grep QuickCheck
(QuickCheck-2.1.1.1)
QuickCheck-1.2.0.1

In my setup (seen above) I have both 1 and 2, but 2 is hidden (the () means hidden) so when I use GHCi and import Test.QuickCheck it's version 1 that I get.

1 Comment

You usually click the check mark next to the acceptable answer. Some people here are spiteful and won't answer your questions if they see you have asked too many without accepting.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.