Why is there no Foldable instance for Either?

I'm surprised that `Either` does not have a `Foldable` instance of the form: import Data.Foldable import Data.Monoid instance Foldable (Either e) where foldMap f (Left _) = mempty foldMap f (Right r) = f r foldr _ z (Left _) = z foldr f z (Right r) = f r z In other words, the above instance would behave like the `Maybe` `Foldable` instance, treating `Left` in the same way as `Nothing`. This came up in the context of an issue raised on the `errors` package: https://github.com/Gabriel439/Haskell-Errors-Library/issues/16 I could provide an orphan instance for `Either` in the `errors` package, but I wanted to ask if it was possible to incorporate the instance directly into `Data.Foldable`.

On Thu, 30 May 2013, Gabriel Gonzalez wrote:
This came up in the context of an issue raised on the `errors` package:
https://github.com/Gabriel439/Haskell-Errors-Library/issues/16
I could provide an orphan instance for `Either` in the `errors` package, but I wanted to ask if it was possible to incorporate the instance directly into `Data.Foldable`.
For my taste it is abuse to use Either for handling exceptions, thus it would be more abuse to support that with a Foldable instance.

On Thu, May 30, 2013 at 12:53 PM, Gabriel Gonzalez
I'm surprised that `Either` does not have a `Foldable` instance of the form:
import Data.Foldable import Data.Monoid
instance Foldable (Either e) where foldMap f (Left _) = mempty foldMap f (Right r) = f r
foldr _ z (Left _) = z foldr f z (Right r) = f r z
In other words, the above instance would behave like the `Maybe` `Foldable` instance, treating `Left` in the same way as `Nothing`.
This came up in the context of an issue raised on the `errors` package:
https://github.com/Gabriel439/Haskell-Errors-Library/issues/16
I could provide an orphan instance for `Either` in the `errors` package, but I wanted to ask if it was possible to incorporate the instance directly into `Data.Foldable`.
This instance should exist. There's been a couple of discussion on libraries@ before. One of them is at http://www.haskell.org/pipermail/libraries/2012-July/018246.html. I wrote a patch for base at one point -- http://shachaf.net/0001-Add-Foldable-and-Traversable-instances-for-Either-e-... -- but didn't go through the rest of the process at the time. Right now `lens` has an orphan instance for this, by the way. Shachaf

We have already right-biased Eithers in Haskell for Functor, Applicative,
and Monad. We should be consistent and right-bias it for Foldable,
Traversable, and similar typeclasses. Left-biasing can still be
accomplished with a newtype wrapper, and perhaps we should consider
including this in the standard libraries as well.
Until the change happens, I'd suggest recommending that "errors" users
requiring this functionality lean on the "lens" instances.
-- Dan Burton
On Thu, May 30, 2013 at 1:12 PM, Shachaf Ben-Kiki
I'm surprised that `Either` does not have a `Foldable` instance of the
On Thu, May 30, 2013 at 12:53 PM, Gabriel Gonzalez
wrote: form: import Data.Foldable import Data.Monoid
instance Foldable (Either e) where foldMap f (Left _) = mempty foldMap f (Right r) = f r
foldr _ z (Left _) = z foldr f z (Right r) = f r z
In other words, the above instance would behave like the `Maybe`
`Foldable`
instance, treating `Left` in the same way as `Nothing`.
This came up in the context of an issue raised on the `errors` package:
https://github.com/Gabriel439/Haskell-Errors-Library/issues/16
I could provide an orphan instance for `Either` in the `errors` package, but I wanted to ask if it was possible to incorporate the instance directly into `Data.Foldable`.
This instance should exist. There's been a couple of discussion on libraries@ before. One of them is at http://www.haskell.org/pipermail/libraries/2012-July/018246.html.
I wrote a patch for base at one point -- < http://shachaf.net/0001-Add-Foldable-and-Traversable-instances-for-Either-e-...
-- but didn't go through the rest of the process at the time.
Right now `lens` has an orphan instance for this, by the way.
Shachaf
_______________________________________________ Libraries mailing list Libraries@haskell.org http://www.haskell.org/mailman/listinfo/libraries

On 05/30/2013 01:45 PM, Dan Burton wrote:
We have already right-biased Eithers in Haskell for Functor, Applicative, and Monad. We should be consistent and right-bias it for Foldable, Traversable, and similar typeclasses. Left-biasing can still be accomplished with a newtype wrapper, and perhaps we should consider including this in the standard libraries as well.
Keep in mind that `errors` can provide the `Left`-biased versions because it already has the appropriate newtype for doing so: `EitherR`.
Until the change happens, I'd suggest recommending that "errors" users requiring this functionality lean on the "lens" instances.
-- Dan Burton
On Thu, May 30, 2013 at 1:12 PM, Shachaf Ben-Kiki
mailto:shachaf@gmail.com> wrote: On Thu, May 30, 2013 at 12:53 PM, Gabriel Gonzalez
mailto:gabriel439@gmail.com> wrote: > I'm surprised that `Either` does not have a `Foldable` instance of the form: > > import Data.Foldable > import Data.Monoid > > instance Foldable (Either e) where > foldMap f (Left _) = mempty > foldMap f (Right r) = f r > > foldr _ z (Left _) = z > foldr f z (Right r) = f r z > > In other words, the above instance would behave like the `Maybe` `Foldable` > instance, treating `Left` in the same way as `Nothing`. > > This came up in the context of an issue raised on the `errors` package: > > https://github.com/Gabriel439/Haskell-Errors-Library/issues/16 > > I could provide an orphan instance for `Either` in the `errors` package, but > I wanted to ask if it was possible to incorporate the instance directly into > `Data.Foldable`. > This instance should exist. There's been a couple of discussion on libraries@ before. One of them is at http://www.haskell.org/pipermail/libraries/2012-July/018246.html.
I wrote a patch for base at one point -- http://shachaf.net/0001-Add-Foldable-and-Traversable-instances-for-Either-e-... -- but didn't go through the rest of the process at the time.
Right now `lens` has an orphan instance for this, by the way.
Shachaf
_______________________________________________ Libraries mailing list Libraries@haskell.org mailto:Libraries@haskell.org http://www.haskell.org/mailman/listinfo/libraries

+1, I wanted this recently. Erik On Thursday, May 30, 2013, Shachaf Ben-Kiki wrote:
I'm surprised that `Either` does not have a `Foldable` instance of the
On Thu, May 30, 2013 at 12:53 PM, Gabriel Gonzalez
javascript:;> wrote: form: import Data.Foldable import Data.Monoid
instance Foldable (Either e) where foldMap f (Left _) = mempty foldMap f (Right r) = f r
foldr _ z (Left _) = z foldr f z (Right r) = f r z
In other words, the above instance would behave like the `Maybe`
`Foldable`
instance, treating `Left` in the same way as `Nothing`.
This came up in the context of an issue raised on the `errors` package:
https://github.com/Gabriel439/Haskell-Errors-Library/issues/16
I could provide an orphan instance for `Either` in the `errors` package, but I wanted to ask if it was possible to incorporate the instance directly into `Data.Foldable`.
This instance should exist. There's been a couple of discussion on libraries@ before. One of them is at http://www.haskell.org/pipermail/libraries/2012-July/018246.html.
I wrote a patch for base at one point -- < http://shachaf.net/0001-Add-Foldable-and-Traversable-instances-for-Either-e-...
-- but didn't go through the rest of the process at the time.
Right now `lens` has an orphan instance for this, by the way.
Shachaf
_______________________________________________ Libraries mailing list Libraries@haskell.org javascript:; http://www.haskell.org/mailman/listinfo/libraries

On Thu, May 30, 2013 at 01:12:00PM -0700, Shachaf Ben-Kiki wrote:
This instance should exist. There's been a couple of discussion on libraries@ before. One of them is at http://www.haskell.org/pipermail/libraries/2012-July/018246.html.
The previous one was in January 2011: http://thread.gmane.org/gmane.comp.lang.haskell.libraries/15196 I've taken the liberty of pushing the previously discussed Foldable and Traversable instances for Either a and (,) a.

On Thu, May 30, 2013 at 6:34 PM, Ross Paterson
On Thu, May 30, 2013 at 01:12:00PM -0700, Shachaf Ben-Kiki wrote:
This instance should exist. There's been a couple of discussion on libraries@ before. One of them is at http://www.haskell.org/pipermail/libraries/2012-July/018246.html.
The previous one was in January 2011:
http://thread.gmane.org/gmane.comp.lang.haskell.libraries/15196
I've taken the liberty of pushing the previously discussed Foldable and Traversable instances for Either a and (,) a.
Thank you! While you're at it would it be possible to add (trivial) Foldable and Traversable instances for (Const r), for completeness? I think that was also mentioned in one of the discussions. As in my patch, the instances are: instance Foldable (Const m) where foldMap _ _ = mempty instance Traversable (Const m) where traverse f (Const m) = pure (Const m) Shachaf

On Thu, May 30, 2013 at 6:41 PM, Shachaf Ben-Kiki
On Thu, May 30, 2013 at 6:34 PM, Ross Paterson
wrote: On Thu, May 30, 2013 at 01:12:00PM -0700, Shachaf Ben-Kiki wrote:
This instance should exist. There's been a couple of discussion on libraries@ before. One of them is at http://www.haskell.org/pipermail/libraries/2012-July/018246.html.
The previous one was in January 2011:
http://thread.gmane.org/gmane.comp.lang.haskell.libraries/15196
I've taken the liberty of pushing the previously discussed Foldable and Traversable instances for Either a and (,) a.
Thank you! While you're at it would it be possible to add (trivial) Foldable and Traversable instances for (Const r), for completeness? I think that was also mentioned in one of the discussions. As in my patch, the instances are:
instance Foldable (Const m) where foldMap _ _ = mempty
instance Traversable (Const m) where traverse f (Const m) = pure (Const m)
Shachaf
Just to make sure this isn't slipping through the cracks -- is there a particular reason not to add the instance for (Const r)? It was brought up in the last discussion and it has one obviously-correct definition. It's a useful instance. (Right now, as mentioned, lens defines orphans for all three types -- it would be nice to get rid of all of them in one go.) Thanks, Shachaf

No reason at all other than an annoying major version bump in transformers.
On Wed, Jun 12, 2013 at 5:10 AM, Shachaf Ben-Kiki
On Thu, May 30, 2013 at 6:41 PM, Shachaf Ben-Kiki
wrote: On Thu, May 30, 2013 at 6:34 PM, Ross Paterson
wrote: On Thu, May 30, 2013 at 01:12:00PM -0700, Shachaf Ben-Kiki wrote:
This instance should exist. There's been a couple of discussion on libraries@ before. One of them is at http://www.haskell.org/pipermail/libraries/2012-July/018246.html.
The previous one was in January 2011:
http://thread.gmane.org/gmane.comp.lang.haskell.libraries/15196
I've taken the liberty of pushing the previously discussed Foldable and Traversable instances for Either a and (,) a.
Thank you! While you're at it would it be possible to add (trivial) Foldable and Traversable instances for (Const r), for completeness? I think that was also mentioned in one of the discussions. As in my patch, the instances are:
instance Foldable (Const m) where foldMap _ _ = mempty
instance Traversable (Const m) where traverse f (Const m) = pure (Const m)
Shachaf
Just to make sure this isn't slipping through the cracks -- is there a particular reason not to add the instance for (Const r)? It was brought up in the last discussion and it has one obviously-correct definition. It's a useful instance.
(Right now, as mentioned, lens defines orphans for all three types -- it would be nice to get rid of all of them in one go.)
Thanks, Shachaf
_______________________________________________ Libraries mailing list Libraries@haskell.org http://www.haskell.org/mailman/listinfo/libraries

On Wed, Jun 12, 2013 at 11:01:15AM -0400, Edward Kmett wrote:
On Wed, Jun 12, 2013 at 5:10 AM, Shachaf Ben-Kiki
wrote: Just to make sure this isn't slipping through the cracks -- is there a particular reason not to add the instance for (Const r)? It was brought up in the last discussion and it has one obviously-correct definition. It's a useful instance. No reason at all other than an annoying major version bump in transformers.
Constant in transformers already has these instances, but Const in base doesn't. Otherwise they're the same (and both of them are my fault). One of them should go -- I'm in favour of keeping the full name.

The main concern I would have is that Const actually seems to be in use
everywhere, while Constant is more or less unused by dint of the fact that
Const comes into scope with Control.Applicative, so many more people are
aware of it.
-Edward
On Wed, Jun 12, 2013 at 1:26 PM, Ross Paterson
On Wed, Jun 12, 2013 at 5:10 AM, Shachaf Ben-Kiki
wrote: Just to make sure this isn't slipping through the cracks -- is there a particular reason not to add the instance for (Const r)? It was brought up in the last discussion and it has one obviously-correct definition. It's a useful instance. No reason at all other than an annoying major version bump in
On Wed, Jun 12, 2013 at 11:01:15AM -0400, Edward Kmett wrote: transformers.
Constant in transformers already has these instances, but Const in base doesn't. Otherwise they're the same (and both of them are my fault). One of them should go -- I'm in favour of keeping the full name.
_______________________________________________ Libraries mailing list Libraries@haskell.org http://www.haskell.org/mailman/listinfo/libraries

I actually kind of like Const as it matches 'const' - though I'm not sure
how similar they really are in theory.
On 12 Jun 2013 20:39, "Edward Kmett"
The main concern I would have is that Const actually seems to be in use everywhere, while Constant is more or less unused by dint of the fact that Const comes into scope with Control.Applicative, so many more people are aware of it.
-Edward
On Wed, Jun 12, 2013 at 1:26 PM, Ross Paterson
wrote: On Wed, Jun 12, 2013 at 5:10 AM, Shachaf Ben-Kiki
wrote: Just to make sure this isn't slipping through the cracks -- is On Wed, Jun 12, 2013 at 11:01:15AM -0400, Edward Kmett wrote: there a
particular reason not to add the instance for (Const r)? It was brought up in the last discussion and it has one obviously-correct definition. It's a useful instance.
No reason at all other than an annoying major version bump in
transformers.
Constant in transformers already has these instances, but Const in base doesn't. Otherwise they're the same (and both of them are my fault). One of them should go -- I'm in favour of keeping the full name.
_______________________________________________ Libraries mailing list Libraries@haskell.org http://www.haskell.org/mailman/listinfo/libraries
_______________________________________________ Libraries mailing list Libraries@haskell.org http://www.haskell.org/mailman/listinfo/libraries

(const originalProposal)? I'd like to use for_ with Either; one whenJust less.

Ah, I've actually found the commit (https://github.com/ghc/packages-base/commit/a9a9ce6).

On 13-05-30 03:53 PM, Gabriel Gonzalez wrote:
I'm surprised that `Either` does not have a `Foldable` instance of the form:
import Data.Foldable import Data.Monoid
instance Foldable (Either e) where foldMap f (Left _) = mempty foldMap f (Right r) = f r
foldr _ z (Left _) = z foldr f z (Right r) = f r z
+1 for adding it
participants (10)
-
Dan Burton
-
Edward Kmett
-
Erik Hesselink
-
Gabriel Gonzalez
-
Henning Thielemann
-
Mario Blažević
-
Niklas Hambüchen
-
Oliver Charles
-
Ross Paterson
-
Shachaf Ben-Kiki