add Applicative methods (#372)

* add Applicative methods

- add Applicative `liftA2`, `(<*)`, and `(*>)`
- resolves https://github.com/polysemy-research/polysemy/issues/368

* remove redundant Monad return definition

- As directed in review of https://github.com/polysemy-research/polysemy/pull/372
This commit is contained in:
Daniel Goertzen 2020-07-31 00:15:44 -05:00 committed by GitHub
parent 235813da00
commit de19721ef5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -271,11 +271,18 @@ instance Applicative (Sem f) where
Sem f <*> Sem a = Sem $ \k -> f k <*> a k
{-# INLINE (<*>) #-}
liftA2 f ma mb = Sem $ \k -> liftA2 f (runSem ma k) (runSem mb k)
{-# INLINE liftA2 #-}
ma <* mb = Sem $ \k -> runSem ma k <* runSem mb k
{-# INLINE (<*) #-}
-- Use (>>=) because many monads are bad at optimizing (*>).
-- Ref https://github.com/polysemy-research/polysemy/issues/368
ma *> mb = Sem $ \k -> runSem ma k >>= \_ -> runSem mb k
{-# INLINE (*>) #-}
instance Monad (Sem f) where
return = pure
{-# INLINE return #-}
Sem ma >>= f = Sem $ \k -> do
z <- ma k
runSem (f z) k