
Hi, I'm trying to acquire some confidence with the GHC-API and I'm having some problems, related to error handling, I seem not be able to solve. Basically there are 3 functions to (interactively) compile/run Haskell expressions: compileExpr, dyCompileExpr and runStmt. The first 2 will return the value of the compiled expression, as a Maybe HValue the first, and a Maybe Dynamic the second, while the third can bind names and report them, or report exceptions. But my problem is related to the first two. I run expressions (string_expression) wrapped around some let x = string_expression in take 2048 (show x) to avoid infinite list or loops. According to the documentation one should be able to control how error messages are dealt with, by manipulation the GHC session's state. One component of the session state, indeed, is the log_action which, by default, prints errors and warnings to the standard error (compiler/main/DynFlags.hs). Now, no matter what I do, I seem not to be able to change the log_action to collect error messages and warnings. In any case everything seem to be printed to standard error. This way I can only collect exceptions raised by the compiler. For instance: "Prelude.last: empty list" for string_expression = "last []" But if my expression raises a type error, this is printed to stderr. Like in the case of string_expression = "last" which would produce a "No instance for (Show ([a] -> a))" What am I doing wrong? Thanks for your kind attentions. Andrea ps: there are quite a lot of other questions I'd like to ask about some programming techniques I've seen applied in GHC. For instance, sometime a list of actions is given as a string, to be compiled to a HValue which is written to a IORef. Running those actions is just a matter of reading the IORef (you can see this in compiler/ghci/InteractiveUIhs, when it comes to buffer management. For instance: flushInterpBuffers). I ask to myself: Why? Just to amaze the reader? ;-)

On Sun, Sep 02, 2007 at 04:09:11PM +0200, Andrea Rossato wrote:
Hi,
I'm trying to acquire some confidence with the GHC-API and I'm having some problems, related to error handling, I seem not be able to solve.
It's always like this: you finally give up, ask for help in the haskell-cafe, go for a walk with your dog and then the glimpse. Now you know what you were doing wrong. In my case I did some confusion when reading the documentation: I thought that newSession was using the dynamic flag set with defaultErrorHandler. That is to say, I thought I should control the log_action with defaultErrorHandler, instead you must: ses <- newSession Interactive (Just ghcPath) df <- getSessionDynFlags ses setSessionDynFlags ses df {log_action = myLog } -- set here your stuff!! ... The wiki was reporting this correctly, but I was misguided when reading this blog entry: http://austin.youareinferior.net/?q=node/29 which, nonetheless, is very helpful (the post is not incorrect by itself. It was me who misunderstood it). Sorry for the noise. BTW, play with the GHC-API is a *great* fun. Thanks for your kind attention. Andrea
participants (1)
-
Andrea Rossato