5

我正在尝试编写一个更改数独的道具,然后检查它是否仍然有效。

但是,我不确定如何正确使用“oneof”功能。请给我一些提示好吗?

prop_candidates :: Sudoku -> Bool
prop_candidates su = isSudoku newSu && isOkay newSu
    where
        newSu       = update su aBlank aCandidate
        aCandidate  = oneof [return x | x <- candidates su aBlank]
        aBlank      = oneof [return x | x <- (blanks su)]

这里有更多信息...

type Pos = (Int, Int)
update :: Sudoku -> Pos -> Maybe Int -> Sudoku
blanks :: Sudoku -> [Pos]
candidates :: Sudoku -> Pos -> [Int]
[return x | x <- (blanks example)] :: (Monad m) => [m Pos]

我已经为这个道具奋斗了 3 个小时,所以欢迎任何想法!

4

3 回答 3

5

我的意思是你有一个类型混淆。即,aBlank不是 a Pos,而是 a Gen Pos,所以update su aBlank aCandidate没有意义!实际上,您想要的是一种在给定初始数独的情况下生成新数独的方法;换句话说,一个函数

similarSudoku :: Sudoku -> Gen Sudoku

现在我们可以写了:

similarSudoku su = do aBlank <- elements (blanks su) 
                      -- simpler than oneOf [return x | x <- blanks su]
                      aCandidate <- elements (candidates su aBlank)
                      return (update su aBlank aCandidate)

甚至更简单:

similarSudoku su = liftM2 (update su) (elements (blanks su)) (elements (candidates su aBlank))

财产看起来像

prop_similar :: Sudoku -> Gen Bool
prop_similar su = do newSu <- similarSudoku su
                     return (isSudoku newSu && isOkay newSu)

既然有实例

Testable Bool
Testable prop => Testable (Gen prop)
(Arbitrary a, Show a, Testable prop) => Testable (a -> prop)

Sudoku -> Gen Bool也是Testable(假设instance Arbitrary Sudoku)。

于 2009-12-02T08:15:06.407 回答
2

在我的博客上,我用 QuickCheck 测试编写了一个简单的掷骰子模拟器oneof,用于生成有趣的掷骰。

假设我们有一个超简单的单行数独:

module Main where
import Control.Monad
import Data.List
import Test.QuickCheck
import Debug.Trace

type Pos = Int
data Sudoku = Sudoku [Char] deriving (Show)

没有超级简单的数独应该有重复的值:

prop_noRepeats :: Sudoku -> Bool
prop_noRepeats s@(Sudoku xs) =
  trace (show s) $ all ((==1) . length) $
                   filter ((/='.') . head) $
                   group $ sort xs

您可能会生成一个超级简单的数独

instance Arbitrary Sudoku where
  arbitrary = sized board :: Gen Sudoku
    where board :: Int -> Gen Sudoku
          board 0 = Sudoku `liftM` shuffle values
          board n | n > 6 = resize 6 arbitrary
                  | otherwise =
                      do xs <- shuffle values
                         let removed = take n xs
                             dots = take n $ repeat '.'
                             remain = values \\ removed
                         ys <- shuffle $ dots ++ remain
                         return $ Sudoku ys

          values = ['1' .. '9']

          shuffle :: (Eq a) => [a] -> Gen [a]
          shuffle [] = return []
          shuffle xs = do x  <- oneof $ map return xs
                          ys <- shuffle $ delete x xs
                          return (x:ys)

trace那里显示随机生成的板:

*Main> quickCheck prop_noRepeats 
Sudoku "629387451"
Sudoku "91.235786"
Sudoku "1423.6.95"
Sudoku "613.4..87"
Sudoku "6..5..894"
Sudoku "7.2..49.."
Sudoku "24....1.."
[...]
+++ OK, passed 100 tests.
于 2009-12-02T00:54:23.007 回答
1

似乎aBlank :: Gen Pos这与它用作参数的方式不匹配candidates :: Sudoku -> Pos -> [Int]

我一直在寻找一种方法转换Gen aa允许您与候选人一起使用它的方法。我能看到的最好的是generate功能。

告诉我我是否遗漏了什么...

于 2009-12-02T01:09:45.903 回答