[Haskell-cafe] instance Alternative (Either [a])

John Meacham john at repetae.net
Wed May 21 21:46:04 UTC 2014


On Mon, May 19, 2014 at 8:31 AM, Corentin Dupont
<corentin.dupont at gmail.com> wrote:
> However the instance in Control.Monad.Trans.Error.Error gets in the way...
> I cannot use this one, it works only with Strings.
> My use case is that the Either handle in the Right a final value, or else in
> the Left a list of things "left to do" to compute the value.
> How can I do?

You want to use a newtype to wrap your Either. It sounds like you would want
to do this independently of the String instance because you are not using
left as shortcutting failure anyway, rather you want to collect together
the failure actions.

newtype NotDoneYet b a = NotDoneYet (Either [b] a)

instance Applicative (NotDoneYet b) where
        Left xs <*> Left ys = Left (xs ++ ys)
        ...


--
John Meacham - http://notanumber.net/


More information about the Haskell-Cafe mailing list