
It kind of seems like there should be fold-like things to match some and many. 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. Thanks, David Feuer

Not sure about performance, but you could wrap the `Alternative` with
an `Alt`[1] and use the `Foldable` instance.
There is also `Alternative` folding in the `reducers` package.
[1]: https://hackage.haskell.org/package/base-4.8.1.0/docs/Data-Monoid.html#t:Alt
[2]: https://hackage.haskell.org/package/reducers-3.12.1/docs/Data-Semigroup-Alte...
On Sat, Sep 19, 2015 at 7:25 AM, David Feuer
It kind of seems like there should be fold-like things to match some and many.
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.
Thanks,
David Feuer
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
-- Danny Navarro | http://dannynavarro.net

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
Not sure about performance, but you could wrap the `Alternative` with an `Alt`[1] and use the `Foldable` instance.
There is also `Alternative` folding in the `reducers` package.
[1]: https://hackage.haskell.org/package/base-4.8.1.0/docs/Data-Monoid.html#t:Alt [2]: https://hackage.haskell.org/package/reducers-3.12.1/docs/Data-Semigroup-Alte...
On Sat, Sep 19, 2015 at 7:25 AM, David Feuer
wrote: It kind of seems like there should be fold-like things to match some and many.
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.
Thanks,
David Feuer
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
-- Danny Navarro | http://dannynavarro.net

Sorry, you are right, I was thinking about folding `Alt` as a
`Monoid`, but what you need is different.
On Sat, Sep 19, 2015 at 10:37 AM, David Feuer
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
wrote: Not sure about performance, but you could wrap the `Alternative` with an `Alt`[1] and use the `Foldable` instance.
There is also `Alternative` folding in the `reducers` package.
[1]: https://hackage.haskell.org/package/base-4.8.1.0/docs/Data-Monoid.html#t:Alt [2]: https://hackage.haskell.org/package/reducers-3.12.1/docs/Data-Semigroup-Alte...
On Sat, Sep 19, 2015 at 7:25 AM, David Feuer
wrote: It kind of seems like there should be fold-like things to match some and many.
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.
Thanks,
David Feuer
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
-- Danny Navarro | http://dannynavarro.net
-- Danny Navarro | http://dannynavarro.net

On Sat, Sep 19, 2015 at 01:25:00AM -0400, David Feuer wrote:
It kind of seems like there should be fold-like things to match some and many.
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 [...]
An Alternative allows list-like behaviour when it comes to *construction*, but offers no operation analogous to pattern-matching and therefore doesn't allow you to look inside it or do any sort of *destruction*. I tried foldrAlt and it looped forever, as I expected. I also expect the others to loop forever. Tom

On Sat, Sep 19, 2015 at 06:13:18PM +0100, Tom Ellis wrote:
On Sat, Sep 19, 2015 at 01:25:00AM -0400, David Feuer wrote:
It kind of seems like there should be fold-like things to match some and many.
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 [...]
An Alternative allows list-like behaviour when it comes to *construction*, but offers no operation analogous to pattern-matching and therefore doesn't allow you to look inside it or do any sort of *destruction*.
I tried foldrAlt and it looped forever, as I expected. I also expect the others to loop forever.
I now understand this can work for some Alternatives. The operational reading is 1. Try to read an a 2. If it fails, return the b 3. If it succeeds, update the b 4. Go to 1 However, since it doesn't work even for an Alternative as simple as Maybe it's worth thinking about what class of Alternatives we really want it to apply to. Tom

In the case of both Maybe and [], an action that succeeds once will always succeed. Parsers, other state transformers, and IO don't work like that, however. I'm not sure where else these apply. On Sep 19, 2015 1:39 PM, "Tom Ellis" < tom-lists-haskell-cafe-2013@jaguarpaw.co.uk> wrote:
On Sat, Sep 19, 2015 at 06:13:18PM +0100, Tom Ellis wrote:
On Sat, Sep 19, 2015 at 01:25:00AM -0400, David Feuer wrote:
It kind of seems like there should be fold-like things to match some and many.
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 [...]
An Alternative allows list-like behaviour when it comes to *construction*, but offers no operation analogous to pattern-matching and therefore doesn't allow you to look inside it or do any sort of *destruction*.
I tried foldrAlt and it looped forever, as I expected. I also expect the others to loop forever.
I now understand this can work for some Alternatives. The operational reading is
1. Try to read an a 2. If it fails, return the b 3. If it succeeds, update the b 4. Go to 1
However, since it doesn't work even for an Alternative as simple as Maybe it's worth thinking about what class of Alternatives we really want it to apply to.
Tom _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
participants (3)
-
Danny Navarro
-
David Feuer
-
Tom Ellis