如何使用“oneof”在快速检查(Haskell)中
我正在尝试编写一个更改数独的道具,然后检查它是否仍然有效。
但是,我不确定如何正确使用“oneof”函数。您能给我一些提示吗?
prop_candidates :: Sudoku -> Bool
prop_candidates su = isSudoku newSu && isOkay newSu
where
newSu = update su aBlank aCandidate
aCandidate = oneof [return x | x <- candidates su aBlank]
aBlank = oneof [return x | x <- (blanks su)]
这里有更多信息...
type Pos = (Int, Int)
update :: Sudoku -> Pos -> Maybe Int -> Sudoku
blanks :: Sudoku -> [Pos]
candidates :: Sudoku -> Pos -> [Int]
[return x | x <- (blanks example)] :: (Monad m) => [m Pos]
我已经在这个道具上苦苦挣扎了 3 个小时,所以欢迎任何想法!
I am trying to write a prop that changes a Sudoku and then checks if it's still valid.
However, I am not sure how to use the "oneof"-function properly. Can you give me some hints, please?
prop_candidates :: Sudoku -> Bool
prop_candidates su = isSudoku newSu && isOkay newSu
where
newSu = update su aBlank aCandidate
aCandidate = oneof [return x | x <- candidates su aBlank]
aBlank = oneof [return x | x <- (blanks su)]
Here are some more info...
type Pos = (Int, Int)
update :: Sudoku -> Pos -> Maybe Int -> Sudoku
blanks :: Sudoku -> [Pos]
candidates :: Sudoku -> Pos -> [Int]
[return x | x <- (blanks example)] :: (Monad m) => [m Pos]
I have struggeled with this prop for 3 hours now, so any ideas are welcome!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我的意思是你的类型混淆了。也就是说,
aBlank
不是Pos
,而是Gen Pos
,因此update su aBlank aCandidate
没有任何意义!事实上,您想要的是一种在给定初始数独的情况下生成新数独的方法;换句话说,一个函数现在我们可以编写它:
甚至更简单:
属性看起来像
因为有实例
Sudoku ->; Gen Bool
也是可测试
(假设实例任意数独
)。What I was driving at is that you have a type mix-up. Namely,
aBlank
is not aPos
, but aGen Pos
, soupdate su aBlank aCandidate
makes no sense! In fact, what you want is a way to generate a new sudoku given an initial sudoku; in other words a functionNow we can write it:
or even simpler:
And the property looks like
Since there are instances
Sudoku -> Gen Bool
isTestable
as well (assuminginstance Arbitrary Sudoku
).在我的博客上,我编写了一个简单的双骰子模拟器,其中包含 QuickCheck 测试使用
oneof
生成有趣的卷。假设我们有一个超级简单的单行数独:
超级简单的数独不应该有重复的值:
您可以使用
trace
生成一个超级简单的数独,以显示随机生成的棋盘:
On my blog, I wrote a simple craps simulator with QuickCheck tests that use
oneof
to generate interesting rolls.Say we have a super-simple Sudoku of a single row:
No super-simple Sudoku should have repeated values:
You might generate a super-simple Sudoku with
The
trace
is there to show the randomly generated boards:似乎
aBlank :: Gen Pos
与用作candidates :: Sudoku -> 的参数的方式不匹配位置 -> [整数]
。我一直在此处查找转换方法
Gen a
到a
这将允许您将其与候选人一起使用。我能看到的最好的是generate
函数。告诉我我是否遗漏了什么...
it seems that
aBlank :: Gen Pos
which does not match the way it is used as an argument ofcandidates :: Sudoku -> Pos -> [Int]
.I've been looking through here to find a way to convert
Gen a
toa
which would allow you to use it with candidates. The best i could see is thegenerate
function.Tell me if I'm missing something...