函数变量不在 Haskell 范围内
您好,我有以下代码
import Data.Maybe
import Test.QuickCheck
import System.Random
rndExpr :: Gen Expr -> IO Expr
rndExpr gen = do
rnd <- newStdGen
return (generate 5 rnd gen)
,但我得到“不在“生成”范围内”,为什么会这样?
问候 Darren
编辑我正在导入 Test.QuickCheck 但它仍然抱怨“生成”不在范围内。
编辑 2
您将如何编写此函数以便它可以与 Quickcheck 版本 2 一起使用?我简单地尝试将“unGen”放在生成的位置但没有成功,我还安装了quickcheck v 2(cabal install QuickCheck-2.1.0.3)
我需要一个具有以下属性的函数stdGen->Gen Expr->Expr '
unGen 似乎给了我这个功能,但正如我所说,我的编译器找不到那个函数。还有其他函数可以用来解决这个问题吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看来您正在使用 Test.QuickCheck 中的生成器,并且生成是 QuickCheck 版本 1 中的函数。在 QuickCheck 的第 2 版中,情况有些不同,因此没有这样的功能。但是,您至少需要导入 Test.QuickCheck,并且可以从
unGen
获得类似的功能,如下所示:请注意,
unGen
在 Test.QuickCheck.Gen 中,因此您有也导入它。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:Please note that
unGen
is in Test.QuickCheck.Gen so you have to import that too.generate
不是System.Random
中的函数。也许您正在寻找下一个
?编辑:
让我澄清一下:我不知道为什么您使用 QuickCheck/Arbitrary 来执行 Random/MonadRandom 似乎更合适的任务。我假设您考虑了自己的选择并继续前进。
您必须选择发电机吗?你不能使用
sample' :: Gen a -> IO a?
这应该适用于 QC2。
OTOH,如果您确实想使用自己的
StdGen
(或者想避免 IO),请尝试:这将使用名为
g
的StdGen
并一个计数(此处为0
)来生成您的值。因为 unGen 不会步进生成器,并且计数器步进不会提供良好的随机性属性(看来,您可以自己尝试看看),您最终可能希望用生成 StdGen 的东西来包装它代码>s(恶心)。如果您不知道您正在使用哪个版本的软件包,请运行:
在我的设置中(如上所示),我有 1 和 2,但 2 是隐藏的(
()
表示隐藏),所以当我使用 GHCi 并导入Test.QuickCheck
这是我得到的版本 1。generate
isn't a function inSystem.Random
. Perhaps you are looking fornext
?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
?This should work for QC2.
OTOH, if you really want to use your own
StdGen
(or want to avoid IO) then try:This will use the
StdGen
namedg
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 generatesStdGen
s (yuck).If you don't know what version package you are using then run:
In my setup (seen above) I have both 1 and 2, but 2 is hidden (the
()
means hidden) so when I use GHCi and importTest.QuickCheck
it's version 1 that I get.