Instance of `Semigroup (Either a b)` needs to be eventually changed.

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Instance of `Semigroup (Either a b)` needs to be eventually changed.

박신환

Now since `base` package version `4.11.0.0`, `Monoid` is a subclass of `Semigroup`. And that made `Monoid (Either a b)` instance impossible to exist.

Current (`4.11.0.1`) instance of `Semigroup (Either a b)` (excluding `stimes`:
{{{#!hs
-- | @since 4.9.0.0
instance Semigroup (Either a b) where
    Left _ <> b = b
    a      <> _ = a
}}}

No value of `Either a b` can be `mempty`.

This must be eventually changed to:
{{{#!hs
instance Semigroup a => Semigroup (Either a b) where
    Left a <> Left b = Left (a <> b)
    Left _ <> b      = b
    a      <> _      = a
}}}

The former behavior can be made with `Either (Last a) b`.

This makes `Monoid (Either a b)` possible:
{{{#!hs
instance Monoid a => Monoid (Either a b) where
    mempty = Left mempty
}}}

Also `Alternative (Either e)`:
{{{#!hs
instance Monoid e => Alternative (Either e) where
    empty = mempty
    (<|>) = (<>)
}}} 


_______________________________________________
Libraries mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries