[Haskell-cafe] MonadPlus and Alternative folds
david.feuer at gmail.com
Sat Sep 19 08:37:53 UTC 2015
Alt doesn't have a Foldable instance; these "folds" don't have quite
the right types for that. I don't understand how the reducers relate
to what I'm asking.
On Sat, Sep 19, 2015 at 3:22 AM, Danny Navarro <j at dannynavarro.net> wrote:
> Not sure about performance, but you could wrap the `Alternative` with
> an `Alt` and use the `Foldable` instance.
> There is also `Alternative` folding in the `reducers` package.
> : https://hackage.haskell.org/package/base-126.96.36.199/docs/Data-Monoid.html#t:Alt
> : https://hackage.haskell.org/package/reducers-3.12.1/docs/Data-Semigroup-Alternative.html
> On Sat, Sep 19, 2015 at 7:25 AM, David Feuer <david.feuer at gmail.com> wrote:
>> It kind of seems like there should be fold-like things to match some and
>> foldrAlt :: Alternative f => (a -> b -> b) -> b -> f a -> f b
>> foldrAlt c n m = foldr1Alt c n m <|> pure n
>> foldr1Alt :: Alternative f => (a -> b -> b) -> b -> f a -> f b
>> foldr1Alt c n m = c <$> m <*> foldrAlt c n m
>> foldlMP' :: MonadPlus f => (b -> a -> b) -> b -> f a -> f b
>> foldlMP' f b m = foldl1MP' f b m <|> pure b
>> foldl1MP' :: MonadPlus f => (b -> a -> b) -> b -> f a -> f b
>> foldl1MP' f b m = m >>= \r -> let !b' = f b r in foldlMP' f b' m
>> --These look a bit iffy
>> foldlAlt :: Alternative f => (b -> a -> b) -> b -> f a -> f b
>> foldlAlt f b m = ($ b) <$> foldrAlt (\x r acc -> r (f acc x)) id m
>> foldl1Alt :: Alternative f => (b -> a -> b) -> b -> f a -> f b
>> foldl1Alt f b m = ($ b) <$> foldr1Alt (\x r acc -> r (f acc x)) id m
>> Do these or similar exist somewhere? If not, should they exist somewhere?
>> I'm particularly interested in whether either of these will be fast, or will
>> admit a fast implementation, for attoparsec parsers.
>> David Feuer
>> Haskell-Cafe mailing list
>> Haskell-Cafe at haskell.org
> Danny Navarro | http://dannynavarro.net
More information about the Haskell-Cafe