如何在 Haskell 中创建带有 属性 输出的 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

非常感谢任何帮助,我想我想问的是如何将 prop_SolveSoundBool 输出非常清楚地转换为 Property 输出?

最简单的,你可以使用property method to convert e.g. Bool to Property. I suggest to look at the instances of Testable class,并尝试了解它们各自的作用,以及如何使用它们。

或者你可以更复杂一些,使用一些返回 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 = " /= "

由于这是一项作业,我不会再给你任何提示了。