Haskell类型的魔法世界的Oz过滤nicta课程的仿函数和应用程序

时间:2015-08-31 12:04:55

标签: haskell

取自NICTA course

-- | Filter a list with a predicate that produces an effect.
--
-- >>> filtering (Id . even) (4 :. 5 :. 6 :. Nil)
-- Id [4,6]
--
-- >>> filtering (\a -> if a > 13 then Empty else Full (a <= 7)) (4 :. 5 :. 6 :. Nil)
-- Full [4,5,6]
--
-- >>> filtering (\a -> if a > 13 then Empty else Full (a <= 7)) (4 :. 5 :. 6 :. 7 :. 8 :. 9 :. Nil)
-- Full [4,5,6,7]
--
-- >>> filtering (\a -> if a > 13 then Empty else Full (a <= 7)) (4 :. 5 :. 6 :. 13 :. 14 :. Nil)
-- Empty
--
-- >>> filtering (>) (4 :. 5 :. 6 :. 7 :. 8 :. 9 :. 10 :. 11 :. 12 :. Nil) 8
-- [9,10,11,12]
--
-- >>> filtering (const $ True :. True :.  Nil) (1 :. 2 :. 3 :. Nil)
-- [[1,2,3],[1,2,3],[1,2,3],[1,2,3],[1,2,3],[1,2,3],[1,2,3],[1,2,3]]
--
filtering :: Applicative f => (a -> f Bool) -> List a -> f (List a)

我不明白这个功能的签名。

“它需要一个函数(a -> f Bool)和一个List a并返回f (List a)

第一个例子:

-- >>> filtering (Id . even) (4 :. 5 :. 6 :. Nil)
-- :type (. even)
-- (Bool -> c) -> a -> c

给定: data Id a = Id a

这是怎么发生的:

-- :type (Id . even)
-- a -> Id Bool

我理解这一点:

-- >>> filtering (\a -> if a > 13 then Empty else Full (a <= 7)) (4 :. 5 :. Nil)

这两个怎么样:

-- >>> filtering (>) (4 :. 5 :. Nil) 8
-- >>> filtering (const $ True :. True :. Nil) (1 :. 2 :. Nil)

编辑:

-- :type Id
-- a -> Id a

-- :type filtering
-- (a -> f Bool) -> List a -> f (List a)

-- :type filtering Id
-- List Bool -> Id (List Bool)

-- Functor f is Id
-- (a -> f Bool) is replaced by (Bool -> Id Bool)

类似地:

-- :type (<)
-- a -> a -> Bool

-- :type filtering
-- (a -> f Bool) -> List a -> f (List a)

-- :type filtering (<)
-- List a -> a -> List a

-- Functor f is (-> a)
-- (a -> f Bool) is replaced by (a -> a -> Bool)

我想是这样的。

其他问题:

-- :type Id
-- a -> Id a
-- :type (. even)
-- (Bool -> c) -> a -> c
-- :type (Id . even)
-- a -> Id Bool

我不明白最后的转变。

通过aweinstock给出答案:

(Id :: a -> Id a)被置于(Bool -> c)的位置              ((. even) :: (Bool -> c) -> a -> c),所以“a”与“Bool”统一,              因此“c”与“Id Bool”结合使用

1 个答案:

答案 0 :(得分:3)

首先,考虑一下你手边的Applicative实例:

instance Applicative Id where

instance Applicative List where

instance Applicative Optional where

instance Applicative ((->) t) where

现在,filtering具有以下类型

filtering :: Applicative f => (a -> f Bool) -> List a -> f (List a)

我们专注于filtering (>)(>)的类型为Ord a => a -> a -> Bool。这会立即修复Applicative的实例:它是((->) t)

filtering (>) :: Ord a => List a -> ((-> a) List a)
-- or, written in the usual `a ->` style:
filtering (>) :: Ord a => List a -> (a -> List a)

因此,filtering (>)接受List a并返回一个期望单个值并最终返回列表的函数:

filtering                       :: Applicative f => (a -> f Bool) -> List a -> f (List a)
filtering (>)                   :: Ord a =>                          List a -> (a  -> List a)
filtering (>) (4 :. 5 :. Nil)   ::                                             Int -> List Int
filtering (>) (4 :. 5 :. Nil) 8 ::                                                    List Int

顺便说一句,如果您使用GHCi并且filtering类型但没有(有效)实现,则可以轻松检查类型:

ghci> let filtering :: Applicative f => (a -> f Bool) -> [a] -> f [a]; filtering = undefined
ghci> :t filtering (>)
filtering (>) :: Ord a => [a] -> a -> [a]