Skip to content

Instantly share code, notes, and snippets.

@Solonarv
Created December 24, 2018 16:21
Show Gist options
  • Save Solonarv/0a84ee87299072f9d7bb8d7d9567d012 to your computer and use it in GitHub Desktop.
Save Solonarv/0a84ee87299072f9d7bb8d7d9567d012 to your computer and use it in GitHub Desktop.
Pointwise Semigroup lifting via MonadZip
newtype Pointwise f a = Pointwise { getPointwise :: f a }
deriving newtype (Functor, Applicative, Alternative, Monad, MonadPlus, MonadZip)
deriving stock (Eq, Ord, Show, Read)
instance (MonadZip f, Semigroup a) => Semigroup (Pointwise f a) where
Pointwise xs <> Pointwise ys = Pointwise (mzipWith (<>) xs ys)
-- Now Maybe (Pointwise (Vector (Sum a))) is the monoid you're looking for
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment