[英]Why are difference lists not an instance of foldable?
該DLIST包包含DList
數據類型,它有大量的實例,但不Foldable
或者Traversable
。 在我看來,這是兩個最“類似列表”的類型。 是否存在DList
不是這些類的實例的性能原因?
此外,該包確實實現了foldr
和unfoldr
,但沒有其他折疊功能。
您應該考慮的另一種方法是使用教會編碼列表而不是DList
。 我們的想法是將列表表示為一個不透明的值,它知道如何在列表上執行foldr
。 這需要使用RankNTypes
擴展:
{-# LANGUAGE RankNTypes #-}
import Prelude
import Control.Applicative
import Data.Foldable (Foldable)
import qualified Data.Foldable as F
import Data.Traversable (Traversable)
import qualified Data.Traversable as T
-- | Laws:
--
-- > runList xs cons nil == xs
-- > runList (fromList xs) f z == foldr f z xs
-- > foldr f z (toList xs) == runList xs f z
newtype ChurchList a =
ChurchList { runList :: forall r. (a -> r -> r) -> r -> r }
-- | Make a 'ChurchList' out of a regular list.
fromList :: [a] -> ChurchList a
fromList xs = ChurchList $ \k z -> foldr k z xs
-- | Turn a 'ChurchList' into a regular list.
toList :: ChurchList a -> [a]
toList xs = runList xs (:) []
-- | We can construct an empty 'ChurchList' without using a @[]@.
nil :: ChurchList a
nil = ChurchList $ \_ z -> z
-- | The 'ChurchList' counterpart to '(:)'. Unlike 'DList', whose
-- implementation uses the regular list type, 'ChurchList' doesn't
-- rely on it at all.
cons :: a -> ChurchList a -> ChurchList a
cons x xs = ChurchList $ \k z -> k x (runList xs k z)
-- | Append two 'ChurchList's. This runs in O(1) time. Note that
-- there is no need to materialize the lists as @[a]@.
append :: ChurchList a -> ChurchList a -> ChurchList a
append xs ys = ChurchList $ \k z -> runList xs k (runList ys k z)
-- | Map over a 'ChurchList'. No need to materialize the list.
instance Functor ChurchList where
fmap f xs = ChurchList $ \k z -> runList xs (\x xs' -> k (f x) xs') z
-- | The 'Foldable' instance is trivial, given the 'ChurchList' law.
instance Foldable ChurchList where
foldr f z xs = runList xs f z
instance Traversable ChurchList where
traverse f xs = runList xs step (pure nil)
where step x rest = cons <$> f x <*> rest
這樣做的缺點是ChurchList
沒有有效的tail
操作 - 折疊ChurchList
很便宜,但重復尾巴很昂貴......
DList a
是NEWTYPE包裝周圍[a] -> [a]
其具有a
在一個逆變位置,因此它不能執行Foldable
或Traversable
,或甚至Functor
直接。 實現它們的唯一方法是轉換為常規列表(參見foldr
實現),這會破壞差異列表的性能優勢。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.