haskell / haskell/transformers

Offer stricter versions of AccumT

Open
#91 5 comments 0 reactions 0 assignees View on GitHub
Dominant language
Haskell
Stars
5
Forks
10
Avg merge
2h 21m
Merged PRs (30d)
1

Description

[original issue 89 by @treeowl]


AccumT is extremely lazy, much like lazy StateT. I suspect that for most purposes, what users actually want is a version that's strict in the accumulator value. That would correspond to instances (at least approximately) like the following:

instance (Monoid w, Functor m, Monad m) => Applicative (AccumT w m) where

pure a = AccumT $ \ !_ -> return (a, mempty)
{-# INLINE pure #-}
mf <*> mv = AccumT $ \ !w -> do
(f, !w&#39;) <- runAccumT mf w
(v, !w&#39;&#39;) <- runAccumT mv (w `mappend` w&#39;)
return (f v, w&#39; `mappend` w&#39;&#39;)
{-# INLINE (<*>) #-}

instance (Monoid w, Functor m, Monad m) => Monad (AccumT w m) where
m >>= k = AccumT $ \ !w -> do
(a, !w&#39;) <- runAccumT m w
(b, !w&#39;&#39;) <- runAccumT (k a) (w `mappend` w&#39;)
return (b, w&#39; `mappend` w&#39;&#39;)
{-# INLINE (>>=) #-}

I think it's also plausible that some users will want a version that's strict in the result pairs, but not in the accumulators. Would you be open to offering the totally strict version, and perhaps the pair-strict version?

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.