![](/img/trans.png)
[英]Using functors/applicatives on custom data types with multiple type classes?
[英]Haskell type magical world of Oz filtering nicta course functors and applicatives
取自NICTA課程 :
-- | 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”統一
首先,考慮一下您現有的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
並返回一個期望單個值的函數,最終返回一個list:
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]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.