[英]How do you create quickCheck properties with a Property output in Haskell?
你如何创建一个属性来检查提供的所有解决方案是否都是有效的解决方案,我需要它作为一个属性输出,但我不确定该怎么做,我只知道如何为 quickCheck 属性做 Bool 输出。 请参阅下面的尝试,以及我希望它如何运行的总体思路:
solve :: Sudoku -> Maybe Sudoku
solve s = solve' (blanks s) s
solve' :: [Pos] -> Sudoku -> Maybe Sudoku
solve' blankl s
| not (isOkay s) = Nothing
| isFilled s = Just s
| otherwise = listToMaybe [fromJust sol | n <- [1..9],
let sol = solve' (tail blankl) (update s (head blankl) (Just n)),
sol /= Nothing]
isSolutionOf :: Sudoku -> Sudoku -> Bool
isSolutionOf s1 s2 =
isOkay s1
&& isFilled s1
&& and [ a == b || b == Nothing |
(a,b) <- zip (concat (rows s1)) (concat (rows s2)) ]
prop_SolveSound :: Sudoku -> Property
prop_SolveSound s
| solution == Nothing = True
| otherwise = isSolutionOf (fromJust solution) s where
solution = solve s
非常感谢任何帮助,我想我要问的是如何将 - 非常清楚 - Bool
输出从prop_SolveSound
转换为Property
输出?
最简单的,您可以使用property
方法将Bool
等转换为Property
。 我建议查看Testable
类的实例,并尝试了解它们各自的作用以及如何使用它们。
或者您可以更复杂并使用一些其他返回Property
的函数,例如===
。 在您的示例中,这可能很棘手。
一个非常有用的函数是counterexample
。 当属性不成立时,它允许您打印额外的输出。 例如,它用于实现===
:
(===) :: (Eq a, Show a) => a -> a -> Property
x === y =
counterexample (show x ++ interpret res ++ show y) res
where
res = x == y
interpret True = " == "
interpret False = " /= "
由于这是一项任务,我不会再给你任何提示。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.