9

在编写一些Arbitrary实例时,我使用以下非常机械的模式实现了几个函数:

type A = Arbitrary -- to cut down on the size of the annotations below
shrink1 :: (A a          ) => (a           -> r) -> (a           -> [r])
shrink2 :: (A a, A b     ) => (a -> b      -> r) -> (a -> b      -> [r])
shrink3 :: (A a, A b, A c) => (a -> b -> c -> r) -> (a -> b -> c -> [r])

shrink1 f a     = [f a'     | a' <- shrink a]
shrink2 f a b   = [f a' b   | a' <- shrink a] ++ [f a b'   | b' <- shrink b]
shrink3 f a b c = [f a' b c | a' <- shrink a] ++ [f a b' c | b' <- shrink b] ++ [f a b c' | c' <- shrink c]

我手动将这些函数写到 ,shrink7这似乎足以满足我的需要。但我不禁想知道:这可以合理地自动化吗?解决方案的奖励积分:

  • 允许shrink0 f = []
  • 生成所有的收缩器
  • 有很多类型类黑客,我喜欢
  • 跳过可怕的扩展,如不连贯/不可判定/重叠的实例
  • 让我也吃蛋糕:不需要我在传递它时 uncurry或在应用它时fcurry 应用程序,和shrinkX fabc
4

2 回答 2

9

这编译,我希望它有效:

{-# LANGUAGE TypeFamilies #-}
import Test.QuickCheck

class Shrink t where
  type Inp t :: *
  shrinkn :: Inp t -> t
  (++*) :: [Inp t] -> t -> t

instance Shrink [r] where
  type Inp [r] = r
  shrinkn _ = []
  (++*) = (++) 

instance (Arbitrary a, Shrink s) => Shrink (a -> s) where
  type Inp (a -> s) = a -> Inp s
  shrinkn f a = [ f a' | a' <- shrink a ] ++* shrinkn (f a)
  l ++* f = \b -> map ($ b) l ++* f b

(++*)仅用于实现收缩。

抱歉,相对缺乏类型类黑客。为类型递归提供了一个很好的[r]停止条件,因此不需要hackery。

于 2012-08-18T00:35:35.103 回答
2

我怀疑在这种情况下你可以避免可怕的扩展,但除此之外:

{-# LANGUAGE MultiParamTypeClasses, FlexibleInstances, TypeFamilies,
 UndecidableInstances, IncoherentInstances #-}

import Test.QuickCheck

class Shrinkable a r where
    shrinkn :: a -> r

instance (Shrinkable [a -> b] r) => Shrinkable (a -> b) r where
    shrinkn f = shrinkn [f]

instance (Arbitrary a, Shrinkable [b] r1, r ~ (a -> r1)) => Shrinkable [a -> b] r where
    shrinkn fs@(f:_) a =
        let fs' = [f a | f <- fs]
        in shrinkn $ fs' ++ [f a' | a' <- shrink a]

instance (r ~ [a]) => Shrinkable [a] r where
    shrinkn (_:vs) = vs

instance (r ~ [a]) => Shrinkable a r where
    shrinkn e = []

以下是一些用于测试示例函数的 Quickcheck 属性:

prop0 a = shrinkn a == []

prop1 a = shrink1 not a == shrinkn not a 

prop2 a b = shrink2 (++) a b == shrinkn (++) a b 

f3 a b c = if a then b + c else b * c 
prop3 a b c = shrink3 f3 a b c == shrinkn f3 a b c 
于 2012-08-18T07:08:08.130 回答