In Haskell, are there aliases for (liftM. LiftM), (liftM. LiftM. LiftM), etc.?

In Haskell, are there any aliases for (liftM. LiftM), (liftM. LiftM. LiftM), etc.

So I do not need to be so detailed, for example:

(liftM . liftM) (+ 1) [Just 1, Just 2] = [Just 2, Just 3] (liftM2 . liftM2) (+) [Just 1] [Just 2] = [Just 3] 
+6
source share
1 answer

There is no such thing in the database, but it is well done in order to ask the most interesting question for me in Qaru for some time.

Functors and applicative functors are closed in composition (which, of course, is not necessary for monads, therefore the need for monad transformers), so liftA2.liftA2 works here, and liftM2 usually just liftA2 , especially now Applicative becomes the Monad superclass.

Retreat:

You can use the newtype composition in the Data.Functor.Compose package to compose the application, but you can also use the new applicators from the old ones. I highly recommend Gershom Bazerman's “Abstracts with Application” in Comonad Reader for people who want to understand how beautiful the combined the applicative structure is compared with the monad transformer stack - now I'm always looking to make things simply applicative, not monadic, where I can get the functionality I need. Often I can use Applicative to combine all the input into the value I want to output, then pass it right where i t using >>= .

Your functions and operators

Of course, there is nothing that prevents you from defining your own functions:

 liftliftA2 :: (Applicative f, Applicative g) => (a -> b -> c) -> f (ga) -> f (gb) -> f (gc) liftliftA2 = liftA2.liftA2 

but it is not much smaller than liftA2.liftA2 .

I like your idea of ​​making nested applicative operators, but will switch to increasing angle brackets, rather than repeating the internal operator, because <**> comes across (<**>) = flip (<*>) in Control.Applicative , and this is more logical.

 import Control.Applicative (<<$>>) :: (Functor f, Functor g) => (a -> b) -> f (ga) -> f (gb) (<<$>>) = fmap.fmap (<<*>>) :: (Functor m, Applicative m, Applicative n) => m (n (a -> b)) -> m (na) -> m (nb) mnf <<*>> mna = (<*>) <$> mnf <*> mna 

gives

 ghci> (+) <<$>> [Just 5] <<*>> [Just 7,Just 10] [Just 12,Just 15] 

and of course you can continue:

 (<<<$>>>) :: (Functor f, Functor g, Functor h) => (a -> b) -> f (g (ha)) -> f (g (hb)) (<<<$>>>) = fmap.fmap.fmap (<<<*>>>) :: (Functor l,Functor m, Applicative l, Applicative m, Applicative n) => l (m (n (a -> b))) -> l (m (na)) -> l (m (nb)) lmnf <<<*>>> lmna = (<*>) <<$>> lmnf <<*>> lmna 

What makes visible incredible

 ghci> subtract <<<$>>> Right [Just 5,Nothing,Just 10] <<<*>>> Right [Just 100,Just 20] Right [Just 95,Just 15,Nothing,Nothing,Just 90,Just 10] 

but again, as Gershom Bazerman's article shows, you might want to embed applications as deep as you might want to embed Monads.

+7
source

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


All Articles