on master help sign in
Implement Semigroup and Monoid for morphisms

These are just "reader-style" instances. The endomorphism version already exists for normal functions. There is currently no instance for Kleisli endomorphisms.

commit93fe664f3650500646f5ea5cafcf754c605dbf67 authorhowtonotwin <rasheeqhere@gmail.com> dateMon 30 Apr 2018, 13:25 (8 years ago) parent7c04687
1 file changed+13
libs/base/Data/Morphisms.idr+13
libs/base/Data/Morphisms.idr+13
@@ -28,6 +28,12 @@ Applicative (Morphism r) where
2828Monad (Morphism r) where
2929 (Mor h) >>= f = Mor $ \r => applyMor (f $ h r) r
3030
31+Semigroup a => Semigroup (Morphism r a) where
32+ f <+> g = [| f <+> g |]
33+
34+Monoid a => Monoid (Morphism r a) where
35+ neutral = Mor $ const neutral
36+
3137Semigroup (Endomorphism a) where
3238 (Endo f) <+> (Endo g) = Endo $ g . f
3339
@@ -44,6 +50,13 @@ Applicative f => Applicative (Kleislimorphism f a) where
4450Monad f => Monad (Kleislimorphism f a) where
4551 (Kleisli f) >>= g = Kleisli $ \r => applyKleisli (g !(f r)) r
4652
53+-- Applicative is a bit too strong, but there is no suitable superclass
54+(Semigroup a, Applicative f) => Semigroup (Kleislimorphism f r a) where
55+ f <+> g = [| f <+> g |]
56+
57+(Monoid a, Applicative f) => Monoid (Kleislimorphism f r a) where
58+ neutral = Kleisli $ const $ pure neutral
59+
4760Cast (Endomorphism a) (Morphism a a) where
4861 cast (Endo f) = Mor f
4962