简体   繁体   English

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

[英]How do you create quickCheck properties with a Property output in Haskell?

How do you create a property that checks that all solutions provided are valid solutions, I need it to output as a Property, but I'm unsure how to do that, I only understand how to do Bool outputs for quickCheck properties.你如何创建一个属性来检查提供的所有解决方案是否都是有效的解决方案,我需要它作为一个属性输出,但我不确定该怎么做,我只知道如何为 quickCheck 属性做 Bool 输出。 See below for my attempt, and the general idea of how I want it to function:请参阅下面的尝试,以及我希望它如何运行的总体思路:

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

Any help is much appreciated, I guess what I'm asking is how can you convert the - quite clearly - Bool output from prop_SolveSound to a Property output?非常感谢任何帮助,我想我要问的是如何将 - 非常清楚 - Bool输出从prop_SolveSound转换为Property输出?

At the very simplest, you can use property method to convert eg Bool to Property .最简单的,您可以使用property方法将Bool等转换为Property I suggest to look at the instances of Testable class, and try to understand what each of them does, and how it can be used.我建议查看Testable类的实例,并尝试了解它们各自的作用以及如何使用它们。

Or you can be more sophisticated and use some other functions returning Property , eg === .或者您可以更复杂并使用一些其他返回Property的函数,例如=== That might be tricky in your example.在您的示例中,这可能很棘手。

One quite useful function, is counterexample .一个非常有用的函数是counterexample It allows you to print additional output, when property doesn't hold.当属性不成立时,它允许您打印额外的输出。 For example, it's used to implement === :例如,它用于实现===

(===) :: (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 = " /= "

As this is an assignment, I'm not giving you any more hints.由于这是一项任务,我不会再给你任何提示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM