[Haskell-cafe] A Strict GCL Interpreter in Haskell
Tillmann Rendel
rendel at cs.au.dk
Thu Jul 2 17:44:24 EDT 2009
Hi Hector,
Hector Guilarte wrote:
> 1) Since Haskell is Lazy, and my GCL program is being interpreted in Haskell
> then my GCL is Lazy too (I know is not as simple as that but believe me,
> somehow it is behaving lazy). The problem is that it can't be lazy (said to
> me by my teacher on monday).
> evalExpr:: Expr -> Tabla -> Int
> evalExpr expr tabla =
> let salida = (snd (evalAritmetico expr tabla))
> in salida `seq` if (isLeft salida) then error (getLeft salida)
> else getRight salida
I think the problem with your code is that you call error, instead of
reporting the error back to the caller. evalExpr can go wrong (consider
5 / 0), but this fact is not represented in the type:
evalExpr:: Expr -> Tabla -> Int
The type says that for all expressions and all symbol tables, you can
produce an int result. But that is not true! So try using this more
adequate type:
evalExpr :: Expr -> Tabla -> Either String Int
Now, for all expressions and symbol tables, evalExpr either can compute
an integer result, or it informs you that something went wrong. If you
have these kinds of types on all your functions, your main program
becomes something like:
main = do
code <- readAndParseFile "somefile"
case evalProgram code of
Left error -> putStrLn ("Error: " ++ error)
Right () -> putStrLn "Worked fine!"
So if you need fine-grained control over error-handling, add explicit
error handling, and do not use the error function.
Good luck!
Tillmann
More information about the Haskell-Cafe
mailing list