#2316: Add Applicative instances for all MTL Monads
----------------------------------+-----------------------------------------
    Reporter:  sjanssen           |        Owner:                  
        Type:  proposal           |       Status:  new             
    Priority:  normal             |    Milestone:  Not GHC         
   Component:  libraries (other)  |      Version:  6.8.2           
    Severity:  normal             |   Resolution:                  
    Keywords:                     |   Difficulty:  Unknown         
    Testcase:                     |           Os:  Unknown/Multiple
Architecture:  Unknown/Multiple   |  
----------------------------------+-----------------------------------------
Comment (by basvandijk):

 Without checking for similar proposals I also made a patch that adds
 Applicative and Alternative instances for all types in mtl. Next time I
 will look and save me the work.

 == Differences ==

 There are differences however, between the original and my patch.

  * I don't write `pure` and `<*>` in terms of `return` and `ap`. Instead I
 define them directly. Maybe this is a bit more efficient.

  * I try not to require Monad as a superclass for the Applicative
 instances for all the transformer types. This also means that some Functor
 instances now have an Applicative instead of a Monad constraint. I don't
 know if this is really important but I found it clearer and more flexible.


 == Summary of my changes ==

 Here's a summary of my changes:

  * Cont
    * Added     `instance Applicative (Cont r)`
    * Replaced: `instance (Monad m) => Functor (ContT r m)` with: `instance
 Functor (ContT r m)`
    * Added     `instance Applicative m => Applicative (ContT r m)`

  * Error
    * Added:    `instance Applicative (Either e)`
    * Added:    `instance (Error e) => Alternative (Either e)`
    * Replaced: `instance (Monad m) => Functor (ErrorT e m)` with:
 `instance Applicative m => Functor (ErrorT e m)`
    * Added:    `instance Applicative m => Applicative (ErrorT e m)`
    * Added:    `instance (Alternative m, Error e) => Alternative (ErrorT e
 m)`

  * Identity
    * Added:    `instance Applicative Identity`

  * List
    * Replaced: `instance (Monad m) => Functor (ListT m` with: `instance
 Applicative m => Functor (ListT m)`
    * Added:    `instance Applicative m => Applicative (ListT m)`
    * Added:    `instance Alternative m => Alternative (ListT m)`

  * RWS (both lazy and strict) (Unfortunately I couldn't get rid of the
 Monad constraints)
    * Added:    `instance Monoid w => Applicative (RWS r w s)`
    * Addedd:   `instance (Monad m, Monoid w) => Applicative (RWST r w s
 m)`
    * Added:    `instance (MonadPlus m, Monoid w) => Alternative (RWST r w
 s m)`

  * Reader
    * Added:    `instance Applicative (Reader r)`
    * Replaced: `instance (Monad m) => Functor (ReaderT r m)` with:
 `instance (Applicative m) => Functor (ReaderT r m)`
    * Added:    `instance Applicative m => Applicative (ReaderT r m)`
    * Added:    `instance Alternative m => Alternative (ReaderT r m)`

  * State (both lazy and strict) (Unfortunately I couldn't get rid of the
 Monad constraints)
    * Added:    `instance Applicative (State s)`
    * Added:    `instance Monad m => Applicative (StateT s m)`
    * Added:    `instance MonadPlus m => Alternative (StateT s m)`

  * Writer (both lazy and strict)
    * Added:    `instance Monoid w => Applicative (Writer w)`
    * Replaced: `instance (Monad m) => Functor (WriterT w m)` with:
 `instance (Applicative m, Monoid w) => Functor (WriterT w m)`
    * Added:    `instance (Applicative m, Monoid w) => Applicative (WriterT
 w m)`
    * Added:    `instance (Alternative m, Monoid w) => Alternative (WriterT
 w m)`

 == Questions ==

 I also had some questions:

  * Does it make sense adding Applicative counterparts to the Monad*
 classes? For example:
    * `class Applicative f => ApplicativeError  e g | f -> e where ...`
    * `class Applicative f => ApplicativeState  s f | f -> s where ...`
    * `class Applicative f => ApplicativeReader r f | f -> r where ...`
    * `class (Applicative f, Monoid w) => ApplicativeWriter w f | f -> w
 where ...`
    * `class (Monoid w, ApplicativeReader r f, ApplicativeWriter w f,
 ApplicatveState s f) => ApplicativeRWS r w s f | f -> r, f -> w, f -> s
 where ...`
    * `class (Applicative m) => ApplicativeCont m where ...`
    * `class ApplicativeTrans t where lift :: Applicative f => f a -> t f
 a`

  * Can we get rid of the Monad and !MonadPlus constraints in the
 Applicative and Alternative instances for StateT and RWST?

-- 
Ticket URL: <http://hackage.haskell.org/trac/ghc/ticket/2316#comment:5>
GHC <http://www.haskell.org/ghc/>
The Glasgow Haskell Compiler
_______________________________________________
Glasgow-haskell-bugs mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/glasgow-haskell-bugs

Reply via email to