What does it look like KleisliFunctor?

Here is how we can define KleisliFunctor :

 class (Monad m, Functor f) => KleisliFunctor mf where kmap :: (a -> mb) -> fa -> fb kmap f = kjoin . fmap f kjoin :: f (ma) -> fa kjoin = kmap id 

This class type

 class (Functor f, Monad m) => Absorb fm where (>>~) :: fa -> (a -> mb) -> mb a >>~ f = ajoin $ fmap fa ajoin :: f (ma) -> ma ajoin a = a >>~ id 

somewhere fits into category theory? What are the laws? Are they

 a >>~ g . f === fmap fa >>~ g a >>~ (f >=> g) === a >>~ f >>= g 

?

+6
source share
1 answer

This is a speculative answer. Use caution.

First, let's look at the KleisliFunctor , focusing on binding to the arrow:

 class (Monad m, Functor f) => KleisliFunctor mf where kmap :: (a -> mb) -> fa -> fb 

To do this, in fact, a functor from the Claysley category from m to Hask , kmap must follow the corresponding functor laws:

 -- Mapping the identity gives identity (in the other category). kmap return = id -- Mapping a composed arrow gives a composed arrow (in the other category). kmap (g <=< f) = kmap g . kmap f 

The fact that two Functor involved makes things a little unusual, but not unreasonable - for example, laws are preserved for mapMaybe , which is the first concrete example referenced by KleisliFunctor .

As for Absorb , I can easily redo the linking method:

 class (Functor f, Monad m) => Absorb fm where (~<<) :: (a -> mb) -> fa -> mb 

If we are looking for something similar to KleisliFunctor , the question arises which of the categories will have functions like fa -> mb like arrows. This, of course, cannot be Hask , since its identifier (of type fa -> ma ) cannot be id . We would have to find out not only the personality, but also the composition. For something that doesn't quite look like Monad ...

 idAbsorb :: fa -> ma compAbsorb :: (fb -> mc) -> (fa -> mb) -> (fa -> mc) 

... the only plausible thing I can think of right now is to have a monad morphism like idAbsorb and use the second monad morphism in the opposite direction (i.e. m to f ), so compAbsorb can be implemented by applying the first function, and then go back to f and finally apply the second function. We will need to do this in order to understand if my assumptions are suitable, if this approach works, and if it leads to something useful for your purposes.

+4
source

Source: https://habr.com/ru/post/1012516/


All Articles