簡體   English   中英

如何在 Haskell 中使用 Property 輸出創建 quickCheck 屬性?

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

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM