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?
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.
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 StdGen
s (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.