haskellari / these

An either-or-both data type, with corresponding hybrid error/writer monad transformer.
117 stars 49 forks source link

Newtype to wrap any Semialign into a Semigroup #178

Open AlexandreTunstall opened 2 years ago

AlexandreTunstall commented 2 years ago

The Ap newtype wrapper provides a way of using an Applicative instance as a Monoid instance. I think a similar thing can be done for Semialign and Align.

newtype Al f a = Al { getAl :: f a }

instance (Semialign f, Semigroup a) => Semigroup (Al f a) where
    Al a <> Al b = Al (salign a b)

instance (Align f, Semigroup a) => Monoid (Al f a) where
    mempty = Al nil

The Monoid laws should follow from salign being associative and nil being an identity of salign.

I haven't looked at other type classes, but perhaps this admits instances for them too.