Proposal: Add isLeft/isRight to Data.Either

Henning Thielemann lemming at henning-thielemann.de
Sat Dec 1 01:14:25 CET 2012


On Fri, 30 Nov 2012, Simon Hengel wrote:

> Hi,
> I propose to add isLeft/isRight to Data.Either, with the obvious
> definitions:
>
>    isLeft :: Either a b -> Bool
>    isLeft (Left  _) = True
>    isLeft (Right _) = False
>
>    isRight :: Either a b -> Bool
>    isRight (Left  _) = False
>    isRight (Right _) = True
>
> There has been a discussion on that before [1].  While I agree that
> fromLeft/fromRight are moot, I don't see issues with isLeft/isRight.
>
> Personally I care mostly about isLeft, but for orthogonality I propose
> to add both isLeft and isRight.
>
> Here is a (possibly incomplete) list of packages that come with their
> own definition of isLeft:
>
>    snap-core, multifocal, PriorityChansConverger, tamarin-prover-utils,
>    Agda, PCLT, cmdtheline, scyther-proof, xmlhtml, hspec-expectations,
>    Glob, language-glsl, Craft3e, hledger-lib, narc, nemesis,
>    type-settheory, PCLT-DB, RJson, bio, errors, rss2irc, heist


It would be a good opportunity to check how these packages use isLeft and 
isRight. E.g. if they use them in connection with fromLeft and fromRight 
then this would be an argument for me to exclude isLeft and isRight as 
well.


cmdtheline uses these functions in test/Main.hs for checking whether 
command options could be parsed or not. Maybe the tests could be stricter 
if they do (Left expectedValue ==) or (Right expectedValue ==) instead.

snap-core uses these functions once in test/suite/Snap/Core/Tests.hs for 
testing whether Left or Right is returned as expected.

multifocal uses isLeft once in src/Language/XML/Xml2Type.hs:
   map (\(Left x) -> x) . filter isLeft

Functions like

   maybeLeft :: Either a b -> Maybe a
   maybeRight :: Either a b -> Maybe b

  in connection with mapMaybe would be more helpful in this case, or just 
the existing 'Data.Either.lefts'.

PriorityChansConverger defines isLeft, isRight, fromLeft, fromRight but 
does not use them anywhere.

tamarin-prover-utils defines isLeft, isRight but does not use them 
anywhere.

Agda uses isLeft and isRight in some QuickCheck properties. And then there 
is this application:

    do
       -- ps0 :: [NamedArg ParseLHS]
       ps0 <- mapM classPat ps
       let (ps1, rest) = span (isLeft . namedArg) ps0
       (p2, ps3) <- uncons rest -- when (null rest): no field pattern or def pattern found
       guard $ all (isLeft . namedArg) ps3
       let (f, lhs)      = fromR p2
           (ps', _:ps'') = splitAt (length ps1) ps
       return $ Right (f, LHSProj x ps' lhs ps'')

Looks at least interesting ... :-)

I get tired ...

So far it seems that isLeft and isRight are frequently used in testing.



More information about the Libraries mailing list