堆栈溢出在大型列表中的幺半折叠

时间:2013-02-23 22:36:40

标签: haskell stack-overflow monoids

首先是一些imports

import Control.Applicative
import Data.Traversable as T
import Data.Foldable as F
import Data.Monoid

假设我有一个带有一对值的仿函数,

data Fret a = Fret a a deriving (Show)

instance Functor Fret where fmap f (Fret a b) = Fret (f a) (f b)

instance Applicative Fret where
    pure a = Fret a a
    Fret aa ab <*> Fret ba bb = Fret (aa ba) (ab bb)

instance Monoid a => Monoid (Fret a) where
    mempty = Fret mempty mempty
    a `mappend` b = mappend <$> a <*> b

我有一大堆这些,

frets = replicate 10000000 (Fret 1 2)

我想要计算一个,例如,平均值

data Average a = Average !Int !a deriving (Read, Show)

instance Num a => Monoid (Average a) where
    mempty = Average 0 0
    Average n a `mappend` Average m b = Average (n+m) (a+b)

runAverage :: Fractional a => Average a -> a
runAverage (Average n a) = a / fromIntegral n

average = Average 1

以下是一些可能的实现,

average1 = runAverage <$> foldMap (fmap average) frets

average2 = pure (runAverage . mconcat) <*> T.sequenceA (map (pure (Average 1) <*>) frets)

不幸的是,所有这些都会导致堆栈溢出。

Foldable.foldMap中认为问题可能是过度懒惰,我尝试实施更严格的变体,

foldMap' :: (F.Foldable f, Monoid m) => (a -> m) -> f a -> m
foldMap' f = F.foldl' (\m a->mappend m $! f a) mempty

average3 = runAverage <$> foldMap' (fmap average) frets

不幸的是,这也太溢了。

如何在不损害方法清洁结构的情况下实现这一目标?

更新

如果我将Fret字段设为严格,则事情似乎按预期工作。检查这是否适用于较大的应用程序。

1 个答案:

答案 0 :(得分:7)

看起来foldMap太懒了,你的Fret数据类型肯定是,导致经典的foldl (+)类型空间泄漏,你积累了大量的thunk,试图减少你的平均名单。它类似于space leaks in list average with tuples

显然,你唯一循环中的累加器太懒了 - 你使用堆栈的唯一地方是foldMap

enter image description here

使用相同的解决方案 - Frets的{​​{1}}和foldl'实施的严格对类型就足够了,它将在恒定的空间中运行:

foldMap

 foldMap' f = F.foldl' (\m -> mappend m . f) mempty

enter image description here