Ticket #5627 (closed feature request: fixed)

Opened 7 months ago

Last modified 4 months ago

Proposal: Add Applicative instances for the remaining monads in base

Reported by: basvandijk Owned by:
Priority: normal Milestone: 7.6.1
Component: libraries/base Version: 7.2.1
Keywords: Cc:
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: None/Unknown Difficulty: Unknown
Test Case: Blocked By:
Blocking: Related Tickets:

Description

The only public monads in base that don't have Applicative instances yet are: ReadP, ReadPrec and ArrowMonad. Because of the rule of least surprise I would like to propose adding Applicative instances for these monads.

Proposal

Concretely I would like to propose adding the following to Control.Applicative:

instance Applicative ReadP where
   pure = return
   (<*>) = ap

instance Alternative ReadP where
   empty = mzero
   (<|>) = mplus

instance Applicative ReadPrec where
   pure = return
   (<*>) = ap

instance Alternative ReadPrec where
   empty = mzero
   (<|>) = mplus

instance Arrow a => Applicative (ArrowMonad a) where
   pure x = ArrowMonad (arr (const x))
   ArrowMonad f <*> ArrowMonad x = ArrowMonad (f &&& x >>> arr (uncurry id))

instance ArrowPlus a => Alternative (ArrowMonad a) where
   empty = ArrowMonad zeroArrow
   ArrowMonad x <|> ArrowMonad y = ArrowMonad (x <+> y)

And adding the following to Control.Arrow:

instance Arrow a => Functor (ArrowMonad a) where
   fmap f (ArrowMonad m) = ArrowMonad $ m >>> arr f

instance (ArrowApply a, ArrowPlus a) => MonadPlus (ArrowMonad a) where
   mzero = ArrowMonad zeroArrow
   ArrowMonad x `mplus` ArrowMonad y = ArrowMonad (x <+> y)

Discussion deadline

I don't think this is a controversial proposal and so I don't expect objections. Can we bend the rules a bit and shorten the discussion period so this can be integrated into the base library that comes with the upcoming ghc-7.4?

Attachments

Change History

Changed 6 months ago by igloo

  • status changed from new to patch
  • milestone set to 7.6.1

Changed 4 months ago by igloo

  • status changed from patch to closed
  • resolution set to fixed

Thanks for the patch. Applied as c01010387a0c1d4d71de55f2957800337c00264d.

Note: See TracTickets for help on using tickets.