如何在Haskell中使用属性输出创建quickCheck属性?

问题描述 投票:1回答:1

您如何创建一个属性,以检查提供的所有解决方案是否都是有效的解决方案,我需要将其作为属性输出,但是我不确定该如何做,我只知道如何为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,这很清楚?

haskell types sudoku quickcheck
1个回答
1
投票

最简单的说,您可以使用property方法进行转换,例如propertyBool。我建议查看Property类的实例,并尝试了解它们各自的功能以及如何使用。

© www.soinside.com 2019 - 2024. All rights reserved.