Haskell Control.Arrow:尝试编写filterA函数

时间:2015-07-04 01:33:27

标签: haskell arrows

我正在尝试编写一个filterA :: (ArrowChoice arr) => arr a Bool -> arr [a] [a]函数,该函数会从f :: arr a Bool返回False的列表中删除每个元素。这就是我到目前为止所拥有的

listcase [] = Left ()
listcase (x:xs) = Right (x, xs)

filterA f = arr listcase >>>
            arr (const []) ||| (first (f &&& arr id) >>>
            arr (\((b,x),xs) -> if b then
                x : (filterA f xs)
                else filterA f xs
            ))

现在,当使用(->) a箭头进行测试时,这会起作用,如下所示:

λ> filterA (== 8) [8,9]
[8]

然而,对于像Kleisli Arrows这样的

,它并不起作用
λ> runKleisli (Kleisli $ filterA (== 8)) (return [8,9] :: [IO Int])

<interactive>:160:47:
    Couldn't match expected type `IO Int' with actual type `[t0]'
    In the first argument of `return', namely `[8, 9]'
    In the second argument of `runKleisli', namely
      `(return [8, 9] :: [IO Int])'
    In the expression:
      runKleisli (Kleisli $ filterA (== 8)) (return [8, 9] :: [IO Int])

添加类型签名filterA :: (Arrow arr) => arr a Bool -> arr [a] [a]filterA :: (ArrowChoice arr) => arr a Bool -> arr [a] [a]时,会抛出此错误:

arrows.hs:11:22:
    Could not deduce (arr ~ (->))
    from the context (Arrow arr)
      bound by the type signature for
                 filterA :: Arrow arr => arr a Bool -> arr [a] [a]
      at arrows.hs:7:12-51
      `arr' is a rigid type variable bound by
            the type signature for
              filterA :: Arrow arr => arr a Bool -> arr [a] [a]
            at arrows.hs:7:12
    Expected type: [a] -> [a]
      Actual type: arr [a] [a]
    The function `filterA' is applied to two arguments,
    but its type `arr a Bool -> arr [a] [a]' has only one
    In the second argument of `(:)', namely `(filterA f xs)'
    In the expression: x : (filterA f xs)

我不明白为什么。我错过了什么吗?

修改: @ jaket的评论有效(我猜这有点愚蠢),但类型签名仍然不匹配。 我还将代码更新为更紧凑(尽管仍然得到相同的错误)

filterA f = arr listcase >>>
            arr (const []) ||| (arr toEither >>>
            (filterA f) ||| (second (filterA f) >>> arr uncurry (:)))
  where toEither (x, xs) = if f x then Right (x, xs) else Left xs

顺便说一句,GHC将类型推断为filterA :: (a -> Bool) -> [a] -> [a]

3 个答案:

答案 0 :(得分:1)

您的问题是您尝试在使用arr打包的函数定义中进行递归,并且调用filterA f就好像它是此行中的函数一样:< / p>

                x : (filterA f xs)

仅当箭头类型为(->)时才有效,这是其中一种类型错误告诉您的。

相反,您需要在箭头级别进行递归,如:

listcase :: [t] -> Either () (t, [t])
listcase [] = Left ()
listcase (x:xs) = Right (x, xs)

filterA :: (ArrowChoice arr) => arr a Bool -> arr [a] [a]
filterA f = listcase ^>>
            arr (const []) ||| ((f &&& arr id) *** filterA f >>^
                                (\((b, x), xs) -> if b then x:xs else xs))

(确实编译)

你的runKleisli示例有点困惑,你想说:

runKleisli (filterA $ Kleisli $ return . (== 8)) [8,9]

runKleisli (filterA $ arr (== 8)) [8,9] :: IO [Int]

直接观察类型。

答案 1 :(得分:1)

只是为了补充其他答案:使用Arrow syntax(另请参阅GHC手册,章节Arrow notation),您可以编写更具可读性的函数:

{-# LANGUAGE Arrows #-}

import Control.Arrow

filterA :: (ArrowChoice arr) => arr a Bool -> arr [a] [a]
filterA f = farr
  where
    farr = proc xs ->
            case xs of
                []       -> returnA -< []
                (x:xs')  -> do
                    b   <- f    -< x
                    ys' <- farr -< xs'
                    returnA -< if b then x : ys' else ys'

内部转换为箭头符号的结果可能不那么简洁,但希望编译器能为您优化。

答案 2 :(得分:0)

正如我的评论所述:

runKleisli (Kleisli $ filterA (== 8)) [8, 9]

接下来,您需要将f :: a -> b提升为箭头arr a b

(first (arr f &&& arr id)
        ^^^

在您的函数中:

filterA :: ArrowChoice arr => (a -> Bool) -> arr [a] [a]
filterA f = arr listcase >>>
            arr (const []) ||| (first (f &&& arr id) >>>
            arr (\((b,x),xs) -> if b then
                x : (filterA f xs)
                else filterA f xs
            ))