I have the following datatype and semigroup instance:
data Or a b =
Fst a
| Snd b deriving (Eq, Show)
instance Semigroup (Or a b) where
(<>) (Fst a) (Fst b) = Fst b
(<>) (Snd a) (Fst b) = Snd a
(<>) (Fst a) (Snd b) = Snd b
(<>) (Snd a) (Snd b) = Snd a
I want to make a monoid instance for the type above, but I do not see how to do it. If I use the following definition
instance (Monoid a, Monoid b) => Monoid (Or a b) where
mempty = (Fst mempty)
mappend = (<>)
it will work on all pairs of input to <>
, except the one where I mappend
(Fst a) <> mempty
which will evaluate to mempty
.
How do I fix this so that the mempty
is valid? It seems like it cannot be done without some new syntax or concepts since it hinges on whether the mempty is to the left or right...