Home > other >  Haskell monad: Couldn't match expected type: (Either CError a, [String]) with actual type: Eith
Haskell monad: Couldn't match expected type: (Either CError a, [String]) with actual type: Eith

Time:09-22

data CError = EA Int | EB String
   deriving (Eq, Show)

type Env = [(VName, Value)]

newtype Test a = Test {runTest :: Env -> (Either CError a, [String]) }

I am learning haskell, and now trying to define a monad, but encounters this error and couldn't find the way to fix it.

My code below:

instance Monad Test where
  return a = Test $ \e -> (Right a, mempty)
  
  m >>= f = Test $ \env -> case runTest m env of
    (Right a, s) -> runTest (f a) env
    (Left error) -> Left error

Currently, this line "(Left error) -> Left error" displays an exception "Couldn't match expected type: (Either CError b, [String]) with actual type: Either a0 b1". Can someone help me with this? Thank you

CodePudding user response:

The type (Either CError a, [String]) is not inhabited by any value of the form Left err; you probably meant (Left err, s).

  • Related