[Haskell-beginners] Print values stored in an "Either" in the main method

Mathew Phillips mathewrphillips at gmail.com
Fri Feb 21 00:19:34 UTC 2014


I have a module that looks like this

runEval :: (Show a) => Env -> Eval a -> Either String a
runEval = ...

data Value = IntVal Integer

main :: IO ()
main = do
    let env = Map.fromList [("x",IntVal 3)]
    result <- runEval env (eval (Plus (Var "x") (Lit 2)))
    case result of
        Left err -> putStrLn "Error: "++err
        Right (IntVal i) -> print i

when I call runEval from ghci I get back a result just fine

let env = Map.fromList [("x",IntVal 3)]
runEval env (eval (Plus (Var "x") (Lit 2)))

This outputs "Right (IntVal 5)" as I would expect. But when I try and
compile my main method I get the following error.

Couldn't match expected type `Value'
with actual type `Either t1 Value'
    In the pattern: Right (IntVal i)
    In a case alternative: Right (IntVal i) -> print i
    In a stmt of a 'do' block:
      case result of {
        Left err -> print err
        Right (IntVal i) -> print i }

I also tried using (putStrLn . either show show) result, but this gave me
the following error.

Couldn't match expected type `Either a0 b0'
with actual type `Value'
    In the first argument of `putStrLn . either show show', namely
      `result'
    In a stmt of a 'do' block: (putStrLn . either show show) result
    In the expression:
      do { let env = Map.fromList ...;
           result <- runEval3 env (eval3 (Plus (Var "x") (Lit 2)));
           (putStrLn . either show show) result }

Why is it when I try and pattern match on Either String Value it says
result is of type "Value", but when I try and use either it says result is
of type "Either String Value"?

Matt P.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20140220/30717d72/attachment.html>


More information about the Beginners mailing list