Why WrappedMonad and WrappedArrow?

Why are there types of WrappedMonad and WrappedArrow ? Is it because Monad were not Applicative ? Given that there is a WrappedArrow , should an instance

 Arrow a => Applicative (Arrow ab) 

just embedded in Haskell itself, just as Applicative now the Monad superclass?

+8
source share
2 answers

To a large extent for WrappedMonad . I assume that it is becoming (and may have already been) substantially obsolete. But WrappedArrow more complicated because the Arrow and Applicative types have different types, * -> * -> * vs. * -> * . And because of how the resolution of the GHC instance works, adding the instance (I assume the extra Arrow was a typo)

 instance Arrow a => Applicative (ab) 

would mean that no type constructor with two or more arguments could give Applicative without providing Arrow - which seems pretty sharp.

The opposite of adding the Applicative (ab) => superclass to Arrow a will seem more enjoyable - except that you cannot have superclasses with an add-on type of type b . Such superclasses would be useful for other things, and they have been offered many times, so I assume that they are difficult to implement.

+9
source

I feel the DerivingVia extension gives new life to this kind of shell of a new type. Suppose, for some reason, we want to write Monad and MonadPlus for a type, and then add them for Applicative and Alternative :

 newtype StT sma = StT { runStT :: s -> m (a, s) } deriving Functor instance Monad m => Monad (StT sm) where return a = StT $ \s -> return (a, s) m >>= f = StT $ \s -> m 'runStT' s >>= \(a, t) -> fa 'runStT' t instance MonadPlus m => MonadPlus (StT sm) where mzero = StT $ \_ -> mzero m 'mplus' n = StT $ \s -> (m 'runStT' s) 'mplus' (n 'runStT' s) 

Usually we would have to write sample examples:

 instance Monad m => Applicative (StT sm) where pure = return (<*>) = ap instance MonadPlus m => Alternative (StT sm) where empty = mzero (<|>) = mplus 

With DerivingVia , WrappedMonad you can use his spell in a way that, in my eyes, is much better.

 newtype StT sma = StT { runStT :: s -> m (a, s) } deriving Functor deriving (Applicative, Alternative) via WrappedMonad (StT sm) 

Please note that pre-existing derivative offers are not affected. Another nice point is that the necessary restrictions of the superclass are deduced.

0
source

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


All Articles