[Haskell-cafe] Parsec: Where's +++?

Nicolas Frisby nicolas.frisby at gmail.com
Fri Dec 1 22:53:55 EST 2006


A agreed upon technique for dealing with typeclass hierarchies has
been slow to arrive. For instance, all monads are functors, but
providing a monad instance for your type doesn't automatically make it
a functor as well.

All monads are also applicative functors, and Control.Applicative does
have a newtype to recognize them as such:

Prelude> :m + Control.Applicative
Prelude Control.Applicative> :i WrapMonad
newtype WrappedMonad m a = WrapMonad {unwrapMonad :: m a}
        -- Defined in Control.Applicative
Prelude Control.Applicative> :i Applicative
class (Functor f) => Applicative f where
  pure :: a -> f a
  (<*>) :: f (a -> b) -> f a -> f b
        -- Defined in Control.Applicative
instance (Monad m) => Applicative (WrappedMonad m)
  -- Defined in Control.Applicative
...

Just "wrap" up your monad in WrapMonad, treat it like an applicative
functor, and then unwrap it with unwrapMonad.

HTH,
Nick

On 12/1/06, Greg Fitzgerald <garious at gmail.com> wrote:
> > Text.ParserCombinators.ReadP.(+++) :: ReadP a -> ReadP a -> ReadP a
>
> Wow, fast and complete, Thanks Don!    :)
>
> Would it make sense to derive instances of Applicable and Alternative
> for ReadP?  Something like this maybe:
>
> instance Applicative ReadP where
>         pure = return
>         (<*>) = ap
>
> instance Alternative ReadP where
>         empty = pfail
>         (<|>) = (<++)
>
> Thanks,
> Greg
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>


More information about the Haskell-Cafe mailing list