0 | module Data.Morphisms
2 | import Data.Contravariant
7 | record Morphism a b where
14 | (~>) : Type -> Type -> Type
18 | record Endomorphism a where
23 | record Kleislimorphism (f : Type -> Type) a b where
25 | applyKleisli : a -> f b
33 | Functor (Morphism r) where
34 | map f (Mor a) = Mor $
f . a
37 | Applicative (Morphism r) where
38 | pure a = Mor $
const a
39 | (Mor f) <*> (Mor a) = Mor $
\r => f r $
a r
42 | Monad (Morphism r) where
43 | (Mor h) >>= f = Mor $
\r => applyMor (f $
h r) r
46 | Semigroup a => Semigroup (Morphism r a) where
47 | f <+> g = Mor $
\r => (applyMor f) r <+> (applyMor g) r
50 | Monoid a => Monoid (Morphism r a) where
51 | neutral = Mor $
\_ => neutral
54 | Semigroup (Endomorphism a) where
55 | (Endo f) <+> (Endo g) = Endo $
g . f
58 | Monoid (Endomorphism a) where
62 | Functor f => Functor (Kleislimorphism f a) where
63 | map f (Kleisli g) = Kleisli (map f . g)
66 | Applicative f => Applicative (Kleislimorphism f a) where
67 | pure a = Kleisli $
const $
pure a
68 | (Kleisli f) <*> (Kleisli a) = Kleisli $
\r => f r <*> a r
71 | Monad f => Monad (Kleislimorphism f a) where
72 | (Kleisli f) >>= g = Kleisli $
\r => do
74 | applyKleisli (g k1) r
77 | Contravariant (Op b) where
78 | contramap f (MkOp g) = MkOp (g . f)
79 | v >$ (MkOp f) = MkOp $
\_ => f v
83 | (Semigroup a, Applicative f) => Semigroup (Kleislimorphism f r a) where
84 | f <+> g = Kleisli $
\r => (<+>) <$> (applyKleisli f) r <*> (applyKleisli g) r
87 | (Monoid a, Applicative f) => Monoid (Kleislimorphism f r a) where
88 | neutral = Kleisli $
\_ => pure neutral
91 | Cast (Endomorphism a) (Morphism a a) where
92 | cast (Endo f) = Mor f
95 | Cast (Morphism a a) (Endomorphism a) where
96 | cast (Mor f) = Endo f
99 | Cast (Morphism a (f b)) (Kleislimorphism f a b) where
100 | cast (Mor f) = Kleisli f
103 | Cast (Kleislimorphism f a b) (Morphism a (f b)) where
104 | cast (Kleisli f) = Mor f
107 | Cast (Endomorphism a) (Op a a) where
108 | cast (Endo f) = MkOp f
111 | Cast (Op a a) (Endomorphism a) where
112 | cast (MkOp f) = Endo f
115 | Cast (Op (f b) a) (Kleislimorphism f a b) where
116 | cast (MkOp f) = Kleisli f
119 | Cast (Kleislimorphism f a b) (Op (f b) a) where
120 | cast (Kleisli f) = MkOp f
123 | Cast (Morphism a b) (Op b a) where
124 | cast (Mor f) = MkOp f
127 | Cast (Op b a) (Morphism a b) where
128 | cast (MkOp f) = Mor f