我想为自定义列表实现Applicative的实例。
import Test.QuickCheck
import Test.QuickCheck.Checkers
import Test.QuickCheck.Classes
data List a =
Nil
| Cons a (List a)
deriving (Eq, Show)
instance Eq a => EqProp (List a) where (=-=) = eq
instance Functor List where
fmap _ Nil = Nil
fmap f (Cons a Nil) = (Cons (f a) Nil)
fmap f (Cons a as) = (Cons (f a) (fmap f as))
main = do
let trigger = undefined :: List (Int, String, Int)
quickBatch $ applicative trigger
instance Arbitrary a => Arbitrary (List a) where
arbitrary = sized go
where go 0 = pure Nil
go n = do
xs <- go (n - 1)
x <- arbitrary
return (Cons x xs)
instance Applicative List where
pure x = (Cons x Nil)
Nil <*> _ = Nil
_ <*> Nil = Nil
(Cons f fs) <*> (Cons a as) = (Cons (f a) (fs <*> as))
这会产生以下错误:
λ> main
applicative:
identity: *** Failed! Falsifiable (after 3 tests):
Cons 0 (Cons (-1) Nil)
composition: *** Failed! Falsifiable (after 3 tests):
Cons <function> (Cons <function> Nil)
Cons <function> (Cons <function> Nil)
Cons 1 (Cons (-2) Nil)
homomorphism: +++ OK, passed 500 tests.
interchange: +++ OK, passed 500 tests.
functor: *** Failed! Falsifiable (after 3 tests):
<function>
Cons (-2) (Cons (-1) Nil)
首先是id法失败:
λ> Cons id Nil <*> Cons 0 (Cons (-1) Nil)
Cons 0 Nil
我该如何解决这个问题? pure需要a
而不是List a
所以我看不到如何在List上匹配并保留嵌套列表结构。
构成法也失败了,这并不奇怪:
λ> (Cons "b" Nil) <*> (Cons "c" Nil)
<interactive>:295:7:
Couldn't match expected type ‘[Char] -> b’
with actual type ‘[Char]’
Relevant bindings include
it :: List b (bound at <interactive>:295:1)
In the first argument of ‘Cons’, namely ‘"b"’
In the first argument of ‘(<*>)’, namely ‘(Cons "b" Nil)’
In the expression: (Cons "b" Nil) <*> (Cons "c" Nil)
编辑:因为我在实施拉链列表的应用方面得到了很好的答案,所以我将问题改为拉链列表。
答案 0 :(得分:7)
对于您的ZipList
式方法,我们希望保留以下左侧身份:
pure id <*> someList = someList
为此,pure
无法返回单个元素列表,因为这将立即停止:
(Cons id Nil) <*> Cons 1 (Cons 2 Nil)
= Cons (id 1) (Nil <*> Cons 2 Nil)
= Cons 1 Nil
这不是左侧身份的预期结果。如果pure
不能只返回单个元素列表,那么它应返回多少个?答案是:无限:
repeatList :: a -> List a
repeatList x = let c = Cons x c in c
为什么我称之为ZipList
方法?因为它与Control.Applicative.ZipList
中的行为相同,可以通过zipWith
来激励:
zipWithList :: (a -> b -> c) -> List a -> List b -> List c
zipWithList f (Cons x xs) (Cons y ys) = Cons (f x y) (zipWithList f xs ys)
zipWithList _ _ _ = Nil
现在你的实例是
instance Applicative List where
pure = repeatList
(<*>) = zipWithList ($)
但是,checkers
无法检查此实例是由于您的EqProb
实例,因为pure f <*> pure x == pure (f x)
(同态)导致检查无限列表。不过,您可以提供另一种选择。例如,您可以采用任意数量的元素并进行比较:
prop_sameList :: Eq a => (Int, Int) -> List a -> List a -> Property
prop_sameList bounds xs ys = forAll (choose bounds) $ \n ->
takeList n xs `eq` takeList n ys
takeList :: Int -> List a -> List a
takeList _ Nil = Nil
takeList n (Cons x xs)
| n <= 0 = Nil
| otherwise = Cons x (takeList (n - 1) xs)
然后,如果您想要比较至少1000
和最多10000
元素,您可以使用:
instance Eq a => EqProb (List a) where
(=-=) = prop_sameList (1000, 10000)
毕竟,我们只是想找到一个列表,其中我们的财产不持有。
答案 1 :(得分:3)
扩展我对Zeta更值得回答的评论,您需要进行第二次更改才能运行此测试:
-- | Test lists for equality (fallibly) by comparing finite prefixes
-- of them. I've arbitrarily chosen a depth of 1,000. There may be
-- better ideas than that.
instance Eq a => EqProp (List a) where
xs =-= ys = takeList 1000 xs `eq` takeList 1000 ys
-- | Take a prefix of up to @n@ elements from a 'List'.
takeList :: Int -> List a -> List a
takeList _ Nil = Nil
takeList n (Cons a as)
| n > 0 = Cons a (takeList (n-1) as)
| otherwise = Nil
通过Zeta的更改和此更改,您的测试套件将通过:
applicative:
identity: +++ OK, passed 500 tests.
composition: +++ OK, passed 500 tests.
homomorphism: +++ OK, passed 500 tests.
interchange: +++ OK, passed 500 tests.
functor: +++ OK, passed 500 tests.
获得此处的关键见解是,从根本上说,QuickCheck是一种用于查找属性的反例的工具。 QuickCheck通常无法证明属性适用于所有可能的输入,因为域可能是无限的。这就是为什么在checkers
中有EqProp
class的原因(&#34;可以通过随机抽样测试相等的值的类型,#34;) - 所以我们可以实现搜索不允许简单相等比较的类型和测试的反例的技术。