如何在quickCheck中使用'oneof'(Haskell) [英] How to use 'oneof' in quickCheck (Haskell)

查看:138
本文介绍了如何在quickCheck中使用'oneof'(Haskell)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



然而,我不确定如何使用oneof -功能正常。你可以给我一些提示吗?

  prop_candidates :: Sudoku  - > Bool 
prop_candidates su = isSudoku newSu&& isOkay newSu
其中
newSu =更新su aBlank aCandidate
aCandidate = oneof [return x | x< - 候选suaBlank]
aBlank = oneof [return x | x< - (blanks su)]

以下是更多信息...

  type Pos =(Int,Int)
update :: Sudoku - > Pos - >也许Int - > Sudoku
blanks :: Sudoku - > [Pos]
个候选人:: Sudoku - > Pos - > [Int]
[return x | x< - (空白例子)] ::(Monad m)=> [m Pos]

现在我已经用这个道具拼了3个小时了,所以任何想法都是值得欢迎的!

解决方案

我在驾驶的是你有一种混淆。也就是说, aBlank 不是 Pos ,但是 Gen Pos ,所以更新su aBlank aCandidate 没有意义!事实上,你想要的是一种在初始数独时产生新的数独的方法;换句话说,一个函数

  similarSudoku :: Sudoku  - > Gen Sudoku 

现在我们可以编写它:

  similarSudoku su = do aBlank<  -  elements(blanks su)
- 比oneOf简单[return x | x < - blanks su]
aCandidate< - 元素(候选项su aBlank)
返回(更新su aBlank aCandidate)


$ b

或者甚至更简单:

pre $类似的方法su = liftM2(更新su)(元素)(元素(候选人su aBlank))

该属性看起来像

  prop_similar :: Sudoku  - > Gen Bool 
prop_similar su = do newSu < - similarSudoku su
return(isSudoku newSu&& isOkay newSu)

因为有实例

 可测试Bool 
可测试的prop => Testable(Gen prop)
(Arbitrary a,Show a,Testable prop)=> Testable(a - > prop)

Sudoku - > Gen Bool 也是 Testable (假设实例任意数独)。


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!

解决方案

What I was driving at is that you have a type mix-up. Namely, aBlank is not a Pos, but a Gen Pos, so update 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 function

similarSudoku :: Sudoku -> Gen Sudoku

Now we can write it:

similarSudoku su = do aBlank <- elements (blanks su) 
                      -- simpler than oneOf [return x | x <- blanks su]
                      aCandidate <- elements (candidates su aBlank)
                      return (update su aBlank aCandidate)

or even simpler:

similarSudoku su = liftM2 (update su) (elements (blanks su)) (elements (candidates su aBlank))

And the property looks like

prop_similar :: Sudoku -> Gen Bool
prop_similar su = do newSu <- similarSudoku su
                     return (isSudoku newSu && isOkay newSu)

Since there are instances

Testable Bool
Testable prop => Testable (Gen prop)
(Arbitrary a, Show a, Testable prop) => Testable (a -> prop)

Sudoku -> Gen Bool is Testable as well (assuming instance Arbitrary Sudoku).

这篇关于如何在quickCheck中使用'oneof'(Haskell)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆