函数变量不在 Haskell 范围内

发布于 2024-10-06 11:18:14 字数 575 浏览 0 评论 0原文

您好,我有以下代码

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

暮凉 2024-10-13 11:18:14

看来您正在使用 Test.QuickCheck 中的生成器,并且生成是 QuickCheck 版本 1 中的函数。在 QuickCheck 的第 2 版中,情况有些不同,因此没有这样的功能。但是,您至少需要导入 Test.QuickCheck,并且可以从 unGen 获得类似的功能,如下所示:

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

请注意,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:

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

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

被你宠の有点坏 2024-10-13 11:18:14

generate 不是 System.Random 中的函数。也许您正在寻找下一个

编辑:
让我澄清一下:我不知道为什么您使用 QuickCheck/Arbitrary 来执行 Random/MonadRandom 似乎更合适的任务。我假设您考虑了自己的选择并继续前进。

您必须选择发电机吗?你不能使用 sample' :: Gen a -> IO a?

getVal :: IO a
getVal = sample' arbitrary

这应该适用于 QC2。

OTOH,如果您确实想使用自己的 StdGen (或者想避免 IO),请尝试:

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

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

这将使用名为 gStdGen 并一个计数(此处为0)来生成您的值。因为 unGen 不会步进生成器,并且计数器步进不会提供良好的随机性属性(看来,您可以自己尝试看看),您最终可能希望用生成 StdGen 的东西来包装它代码>s(恶心)。

如果您不知道您正在使用哪个版本的软件包,请运行:

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

在我的设置中(如上所示),我有 1 和 2,但 2 是隐藏的(() 表示隐藏),所以当我使用 GHCi 并导入 Test.QuickCheck 这是我得到的版本 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文