【发布时间】:2016-05-19 14:54:13
【问题描述】:
我想为我的自定义列表实现一个 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)
首先是身份证法失效:
λ> 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)
编辑:由于我在实施适用于 ziplists 时得到了很好的答案,因此我将问题更改为关于 ziplists 的问题。
【问题讨论】:
-
仅供参考,您的
fmap定义有一个不需要的额外案例。你能弄清楚是哪一个吗? -
Applicatives 的组合法则是
u <*> (v <*> w) = pure (.) <*> u <*> v <*> w。
标签: list haskell applicative