Haskell Either v. Error

Teach you Haskell presents Error :

 instance (Error e) => Monad (Either e) where return x = Right x Right x >>= f = fx Left err >>= f = Left err fail msg = Left (strMsg msg) 

Hackage Introduces Either :

 data Either ab = Left a | Right b 

If I understand correctly, then Error is Monad with Either as type a . It also looks like fail to handle exceptions.

But I also see that there is also Control.Monad.Either - http://hackage.haskell.org/package/category-extras-0.53.4/docs/Control-Monad-Either.html .

Why is Control.Monad.Error selected above Control.Monad.Either and vice versa?

+5
source share
1 answer

No, Error not a monad. Either e is a monad, and Error e is a prerequisite for a Monad instance for Either e . Basically, Error e means that e is the type with which you can convert error messages to strMsg , which was used for the Either e fail method.

However, people found that this requirement has an instance of Error e in order to be able to use Either e as a monad, annoying, so after LYAH was written, it was actually deleted. Now the instance is just

 instance Monad (Either e) where return = Right Left l >>= _ = Left l Right r >>= k = kr 

and fail instead uses the default value from the Monad class definition:

 fail s = error s 

However, the Error e requirement is still required (at least for the new Haskell platform) for monads and transformers defined in Control.Monad.Error , while Control.Monad.Either does not meet this requirement.

+9
source

Source: https://habr.com/ru/post/1201024/


All Articles