【问题标题】:How do I create an applicative instance for ziplist?如何为 ziplist 创建应用实例?
【发布时间】: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 的问题。

【问题讨论】:

标签: list haskell applicative


【解决方案1】:

对于您的ZipList-like 方法,我们希望保持以下左身份:

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 ($)

然而,由于EqProb 实例,checkers 也无法检查此实例,因为pure f &lt;*&gt; 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)

毕竟,我们只是想找到一个我们的属性持有的列表。

【讨论】:

  • 其实checkers应该可以测试这个实例。 QuickCheck 是一种用于查找属性的反例的工具,而不是用于证明普遍命题的工具。它不能证明两个无限列表是相等的,但它可以在一定深度上测试相等的前缀,如果找到反例就报告。这就是为什么checkers 使用自己的EqProp class(“可以通过随机抽样来测试相等性的值的类型”)而不是标准的Eq 类。
  • 在中间 zWL 定义行的第二个 f 上出现此错误:chap18/List.hs:34:47: Couldn't match expected type ‘b -&gt; c’ with actual type ‘b’ … ‘b’ is a rigid type variable bound by the type signature for zipWithList :: (a -&gt; b) -&gt; List a -&gt; List b -&gt; List c at /Users/ebs/code/haskell/book/chap18/List.hs:33:16
  • @TheUnfunCat 老实说,我会稍微更改您的问题,以便它实际上要求ZipList,然后打开一个新问题。您永远无法确定答案的作者在 StackOverflow* 上是否仍然活跃。话虽如此:没有。这是不一样的。 gApplicative [] 实例中应用的频率是多少,f 在您的实例中应用的频率是多少? *哦,哇,我累了,你刚提出来。哎呀。
  • @TheUnfunCat 不,不是。你正在做的是gs &lt;*&gt; xs = [ g x | g &lt;- gs | x &lt;- xs ] == [ g x | (g,x) &lt;- zip gs xs ]。常规列表推导定义 嵌套 循环:[ g x | g &lt;- gs, x &lt;- xs ] == {for g in gs: {for x in xs: emit (g x)}} == concat (map (\g-&gt; map g xs) gs)(或其他)。
  • ... 即对于您的数据类型,它是 (Cons f fs) &lt;*&gt; as = append (fmap f as) (fs &lt;*&gt; as) where append Nil ys = ys; append (Cons x xs) ys = Cons x (append xs ys)
【解决方案2】:

扩展我对 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 的原因(“可以通过随机抽样来测试相等的值的类型”)——这样我们就可以实施技术来搜索反例的类型和测试不允许简单的相等比较。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-08
    • 2015-12-24
    • 1970-01-01
    相关资源
    最近更新 更多