Why is there no Foldable instance for Either?

Dan Burton danburton.email at gmail.com
Thu May 30 22:45:53 CEST 2013


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 <shachaf at gmail.com> wrote:

> On Thu, May 30, 2013 at 12:53 PM, Gabriel Gonzalez <gabriel439 at 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-.patch
> >
> -- 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 at haskell.org
> http://www.haskell.org/mailman/listinfo/libraries
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/libraries/attachments/20130530/d9f64a2e/attachment.htm>


More information about the Libraries mailing list