[MLton-devel] Strange behaviour with exceptions

Joe Hurd joe.hurd@comlab.ox.ac.uk
Sat, 18 Oct 2003 17:26:14 +0100 (BST)


HOL makes use of a "can" function, defined as

fun can f x = (f x; true) handle Interrupt => raise Interrupt | _  => false;

But I can't make it work in MLton, as the following program
demonstrates:

carp:~$ mlton
MLton 20030716 (built Wed Jul 16 10:23:10 2003 on redhat71)
carp:~$ cat t.sml
fun can f x = (f x; true) handle Interrupt => raise Interrupt | _  => false;
fun f 0 = 1 | f _ = raise Fail "";
val _ = print (if can f 1 then "yes\n" else "no\n");
carp:~$ mlton -basis 1997 t.sml
carp:~$ t
unhandled exception:

It's fine without any reference to Interrupt:

carp:~$ cat t.sml
fun can f x = (f x; true) handle _  => false;
fun f 0 = 1 | f _ = raise Fail "";
val _ = print (if can f 1 then "yes\n" else "no\n");
carp:~$ mlton -basis 1997 t.sml
carp:~$ t
no

or even:

carp:~$ cat t.sml
exception Fred;
fun can f x = (f x; true) handle Fred => raise Fred | _  => false;
fun f 0 = 1 | f _ = raise Fail "";
val _ = print (if can f 1 then "yes\n" else "no\n");
carp:~$ mlton -basis 1997 t.sml
carp:~$ t
no

So I thought that MLton must think Interrupt is not an exception, and
so be treating it as a catch-all pattern. But if this is the case,
then why doesn't it complain about raising Interrupt?

There are plenty of workarounds. This seems like the most elegant:

carp:~$ cat t.sml
local open SML90; in
fun can f x = (f x; true) handle Interrupt => raise Interrupt | _  => false;
end;
fun f 0 = 1 | f _ = raise Fail "";
val _ = print (if can f 1 then "yes\n" else "no\n");
carp:~$ mlton -basis 1997 t.sml
carp:~$ t
no

But I'd prefer not to have to change the HOL source any more than
necessary. And I can't see why the original program doesn't work.

Joe




-------------------------------------------------------
This SF.net email sponsored by: Enterprise Linux Forum Conference & Expo
The Event For Linux Datacenter Solutions & Strategies in The Enterprise 
Linux in the Boardroom; in the Front Office; & in the Server Room 
http://www.enterpriselinuxforum.com
_______________________________________________
MLton-devel mailing list
MLton-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/mlton-devel