Fmapping arrows over monads

I understand that Arrow is a Profunctor where you can convert its input and its output, but is it possible to display an arrow over Functor?

I understand that as-ask answers "no", since the signature is of the type of the function fmap (a -> b) -> fa -> fb and does not allow Arrow ab , but I hope that I ask clearly.

I am looking for a way, for example, to convert Maybe input with an arrow, where Nothing goes to Nothing and Just x goes to Just y , where y is the result of applying the arrow to x .

+5
source share
2 answers

Arrow combines two concepts. One of them, as you say, is a professor, but first of all it is only a certain class of categories (like the proof of a superclass).

This is very important for this question: yes, the signature fmap is equal to (a -> b) -> fa -> fb , but in fact it is not a complete generality of what the functor can do! In mathematics, a functor is a mapping between two categories C and D, which assigns to each arrow in C the arrow in D. Arrows in different categories, that is! The standard Functor class simply captures the simplest special case - endofunctors in the Hask category.

The full general version of the functor class is more like this (here is my version from the restricted categories ):

 class (Category r, Category t) => Functor frt | fr -> t, ft -> r where fmap :: rab -> t (fa) (fb) 

Or, in pseudo-syntax,

 class (Category (──>), Category (~>)) => Functor f (──>) (~>) where fmap :: (a ──> b) -> fa ~> fb 

It can also be justified if one of the categories is the right arrow, and not the usual category of functions. For example, you can define

 instance Functor Maybe (Kleisli [] (->)) (Kleisli [] (->)) where fmap (Kleisli f) = Kleisli mf where mf Nothing = [Nothing] mf (Just a) = Just <$> fa 

to be used as

 > runKleisli (fmap . Kleisli $ \i -> [0..i]) $ Nothing [Nothing] > runKleisli (fmap . Kleisli $ \i -> [0..i]) $ Just 4 [Just 0,Just 1,Just 2,Just 3,Just 4] 

Not sure if this would be useful for something non-trivial if you use standard arrow-finances. This is definitely useful in other categories that are not Hask -profunctors, e.g.

 instance (TensorSpace v) => Functor (Tensor sv) (LinearFunction s) (LinearFunction s) 

expressing that you can display a linear function by one coefficient of the tensor product (whereas it is generally impossible to display a nonlinear function over such a product - the result will depend on the choice of a basis on the vector space).

+7
source

I'm looking for a way, for example, to convert Maybe input with an arrow, where Nothing goes to Nothing and Just x goes to Just y , where y is the result of applying the arrow to x .

This can be implemented for a specific Functor (e.g. Maybe ), although ArrowChoice likely to be ArrowChoice :

 maybeAmap :: ArrowChoice p => pab -> p (Maybe a) (Maybe b) maybeAmap p = maybe (Left ()) Right ^>> returnA +++ p >>^ const Nothing ||| Just 

See mapM equivalent arrow? for a similar function written in proc-notation.

Speaking of mapM , profuntors have an interesting class called Traversing :

 -- Abbreviated class definition: class (Choice p, Strong p) => Traversing p where traverse' :: Traversable f => pab -> p (fa) (fb) wander :: (forall f. Applicative f => (a -> fb) -> s -> ft) -> pab -> pst 

The Traversing flag instance is the one used for Star profunctor , which provides alternative coding for the familiar traverse . Note that while the leftaroundabout answer demonstrates a non- Hask functor for categories that are optionally Hask -profunctors, with Traversing we have a construct for Profunctor that does not necessarily have a Category instance.

+4
source

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


All Articles