Proposal: In haskell-src, add Functor, Applicative, Monad, and Monoid instances for ParseResult

Yitzchak Gale gale at sefer.org
Wed Jun 10 15:32:39 EDT 2009


In the haskell-src package, add the following instances to ParseResult
that make it more convenient to traverse and modify a syntax tree.

http://hackage.haskell.org/trac/ghc/ticket/3290

Two weeks discussion period.

> instance Functor ParseResult where
>   fmap f (ParseOk x)           = ParseOk $ f x
>   fmap f (ParseFailed loc msg) = ParseFailed loc msg
>
> instance Applicative ParseResult where
>   pure = ParseOk
>   ParseOk f           <*> x = f <$> x
>   ParseFailed loc msg <*> _ = ParseFailed loc msg
>
> instance Monad ParseResult where
>   return = ParseOk
>   ParseOk x           >>= f = f x
>   ParseFailed loc msg >>= _ = ParseFailed loc msg
>
> instance Monoid m => Monoid (ParseResult m) where
>   mempty = ParseOk mempty
>   ParseOk x `mappend` ParseOk y = ParseOk $ x `mappend` y
>   ParseOk x `mappend` err       = err
>   err       `mappend` _         = err -- left-biased


More information about the Libraries mailing list