Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the difference in usage between Either and Except in Haskell?

I have a class that can be created from several arguments in Haskell which requires some complex validation of those arguments. Currently I have something like

makeAThingExcept :: String -> String -> ... String -> Except ThingError AThing
makeAThingExcept s1 s2 ... = do
    unless (s1CheckPasses s1) (throwError (BadS1 s1))
    ...

data ThingError = BadS1 String ...

instance Show ThingError where
        show (BadS1 s) = "Bad S1: " ++ s

makeAThing :: String -> String -> ... String -> AThing
makeAThing s1 s2 ... = case runExcept (makeAThingExcept s1 s2 ...) of
        Right thing  -> thing
        Left err -> error (show err)

Setting aside whether there is a better way to do this by using more specific types than String as arguments to makeAThingExcept, is there a reason to prefer Except over Either in a case like this? What are the differences here between the capabilities and idiom of Except vs Either?

like image 339
orome Avatar asked Nov 07 '18 14:11

orome


2 Answers

The difference is in the instance of Alternative. The "base" package does not export any for Either, as I understand because the authors didn't want to introduce a bias towards any of the values, which on itself is because Either is supposed to be a general sum-type, and representing the possibility of errors is only a special case. However the "transformers" package does provide an orphan instance, which binds it to the Error class:

Error e => Alternative (Either e)

However the community has never accepted either the typeclass or the orphan instance, which is why it is now deprecated. IOW you can look at it as if Either still has no instance of Alternative.

The Except type does have a non-orphan instance, which doesn't even bind the user to any made-up classes, but Monoid instead:

(Monad m, Monoid e) => Alternative (ExceptT e m)

It is also worth noting, that for cases when dealing with validation and accumulating all errors, the Validation abstraction is more appropriate. There is a tradeoff though: it cannot be a monad, but it is a selective (between applicative and monad) and an alternative.

like image 165
Nikita Volkov Avatar answered Oct 17 '22 23:10

Nikita Volkov


As noted in the comments, it's easy to convert between Except & Either. The runtime representation is the same, even.

I would always choose to use Either. It's ubiquitous in libraries. I very rarely see Except.

Except is a special case of ExceptT, which you will see in libraries. If you find yourself writing a lot of functions with Reader SomeType (Either e a) or IO (Either e a) or Monad m => m (Either e a), then you might want to consider ExceptT. It's fine not to worry about it until then - Either is easier to use until it isn't.

like image 2
bergey Avatar answered Oct 18 '22 01:10

bergey