[英]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.