[Haskell-cafe] Call for comments: neither package

Ross Paterson ross at soi.city.ac.uk
Tue Jun 29 16:16:24 EDT 2010


On Tue, Jun 29, 2010 at 02:56:18PM -0500, Jeremy Shaw wrote:
> On Jun 29, 2010, at 6:02 AM, Stephen Tetley wrote:
> >The "Applicative Programming with Effects Paper" has the "monodial
> >accumulating" applicative instance on a sum type Conor McBride and
> >Ross Paterson call Except:
> >
> >data Except err a = OK a | Failed err
> 
> The applicatives-extra package defines a type:
> 
> type ErrorMsg = String
> data Failing a = Failure [ErrorMsg] | Success a
> 
> Which is a less general version of that type.

There's also a generalization, which I was going to put in transformers,
but couldn't decide on the best name:

data Sum p a = Return a | Others (p a)

instance Functor p => Functor (Sum p) where
    fmap f (Return x) = Return (f x)
    fmap f (Others m) = Others (fmap f m)

instance Applicative p => Applicative (Sum p) where
    pure = Return
    Return x <*> Return y = Return (x y)
    Return x <*> Others my = Others (x <$> my)
    Others mx <*> Return y = Others (mx <*> pure y)
    Others mx <*> Others my = Others (mx <*> my)

instance Alternative p => Alternative (Sum p) where
    empty = Others empty
    Return x <|> _ = Return x
    Others _ <|> Return y = Return y
    Others mx <|> Others my = Others (mx <|> my)


More information about the Haskell-Cafe mailing list