Reputation: 5073
instance Monad (Either e) where
return = Right
Right m >>= k = k m
Left e >>= _ = Left e
I found a such piece of code. It is not understandable for me because, after all, Either
constructor type gets two arguments ( Either a b
) and here
took one. I don't understandy why it is possible.
Upvotes: 0
Views: 71
Reputation: 71899
Type constructors, like normal functions, are curried in Haskell. That means you can give a constructor that takes two arguments, such as Either
, one argument, and the result is a type constructor that needs only one argument to construct a full Either
.
The Monad
typeclass is not for simple types, but for type constructors taking one argument. For example, you have instance Monad Maybe
, but Maybe
is not a type, it's a type constructor taking one argument. So you can't make Either
an instance of Monad
, since it takes two arguments, not one, and you can't make Either a b
an instance, since it takes no arguments. You can make Either a
an instance of monad.
In Haskell, we say that a simple type has kind *
, a type constructor taking one argument like Maybe
has kind * -> *
, and Either
has kind * -> * -> *
. Monad
wants something of kind * -> *
, and by supplying one type to Either
, that's what you get.
Upvotes: 8