Either is standard Haskell datatype defined by
data Either a b = Left a | Right b
Let's define Either monad as follows:
instance Monad (Either a) where return = Right x >>= f = case x of Left _ -> x Right r -> f r
This means we fix the type of
Left part and consider
Either a as monadic constructor of kind
* -> *. Binding operator has the following sense: the function transforms data if it's
Right and leaves it unchanged if it's
Left (it's similar to
Right corresponding to
Left corresponding to
Now the puzzle itself. 1) Why the above code won't typecheck and 2) how to make it valid with minimal changes?
The answer on the first question was obvious for me, and I thank
#haskellers for giving me solution to the second.