[MLton] MLton HOL

Matthew Fluet fluet@cs.cornell.edu
Thu, 1 Jul 2004 10:42:13 -0400 (EDT)


> I don't suppose it would be possible for ask MLton to print more than
> just "unhandled exception: x"? Even just the arguments of x would help
> a lot. HOL_ERR is defined as:
>
>      type error_record = {origin_structure : string,
>                           origin_function  : string,
>                           message          : string}
>
>      exception HOL_ERR of error_record

The exception message printer is defined in the Basis Library
implementation; so, the compiler isn't deciding how to print exceptions.
That's why Fail, IO, and SysErr get special messages.

However, it should be easy enough to extend the basis library with a
function:

MLton.Exn.setExnMessage :
  (exn -> string option) -> unit

Then you could use it like:

exception HOL_ERR of error_record
val () =
  MLton.Exn.setExnMessage
    (fn HOL_ERR {origin_structure, origin_function, message} =>
        SOME (concat ["HOL_ERR: {origin_structure = ", origin_structure,
                      ", origin_function = ", origin_function,
                      "message = ", message, "}"])
      | _ => NONE)

The MLton.Exn structure would simply keep a
(exn -> string option) list ref of exception message extensions;
MLton.Exn.setExnMessage would add a new exception message to the list.
MLton.Exn.exnMessage (which is called to print the unhandled exception),
would simply iterate through the list to see if there is an installed
exception printer; if not, it would do the default think of print the
exception name.

Note that this is also nice for the MLton.Exn structure, because it could
be defined much earlier in the Basis Library.  In particular, it no longer
needs to go after the introduction of the IO and SysErr exceptions,
because the special printers for those exceptions could be installed along
with their declarations.