What is wrong with my Haskell definition of the binding operator in this example?

I follow the instructions for monad transformers here .

At this point in the tutorial, he asks me to try to implement a Monad instance for a data type EitherIOthat is defined as:

data EitherIO e a = EitherIO {
    runEitherIO :: IO (Either e a)
}

So I tried:

instance Functor (EitherIO e) where
  fmap f = EitherIO . fmap (fmap f) . runEitherIO

instance Monad (EitherIO e) where
  return  = EitherIO . return . Right
  x >>= f = join $ fmap f x

The tutorial was a bit different:

instance Monad (EitherIO e) where
  return  = pure -- the same as EitherIO . return . Right
  x >>= f = EitherIO $ runEitherIO x >>= either (return . Left) (runEitherIO . f)

Now all types are suitable, so I thought I was good, and congratulated myself on the final search for use join.

As it turned out, later in the lesson , I was asked to run runEitherIO getTokenthe following:

liftEither x = EitherIO (return x)
liftIO x = EitherIO (fmap Right x)

getToken = do
  liftIO (T.putStrLn "Enter email address:")
  input <- liftIO T.getLine
  liftEither (getDomain input)

>>=, GHCi . , ^C, GHCi , , . GHCi . >>= , .

.

:

  • ?

  • GHCi , , ? GHCi?

+4
2

, :

instance Monad (EitherIO e) where
  return  = EitherIO . return . Right
  x >>= f = join $ fmap f x

join :

join x = x >>= id

join >>= - , .

, , :

f, g :: Int -> Int
f x = g x
g x = f x

: >>=, join.

+6

join Control.Monad :

join              :: (Monad m) => m (m a) -> m a
join x            =  x >>= id

, join >>=. , , >>= (.. ), join, , , >>=.

, >>= Monad. , .

, ghc, , . Haskell .

+5

All Articles