caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Bugs with pattern-matching and exceptions
@ 2006-02-28 12:29 Louis Gesbert
  2006-02-28 15:51 ` [Caml-list] " Alain Frisch
                   ` (2 more replies)
  0 siblings, 3 replies; 8+ messages in thread
From: Louis Gesbert @ 2006-02-28 12:29 UTC (permalink / raw)
  To: caml-list

There seems to be some problems with exceptions related to pattern-matching
in Ocaml.

First, exceptions don't support marshalling. This problem is already known,
but yet undocumented. The problem doesn't arises when an exception is
marshalled or unmarshalled however, but only when you try to pattern-match
the un-marshalled value -- which, in my experience, hangs the program --.
Equality tests work fine. I managed to build a workaround for this problem
using ugly Obj code, but some feedback on wether this problem is going to
be solved in future versions of Caml would be appreciated.

More recently, I discovered a far worse bug which leads to some
inconsistency. Ocaml offers a syntax " exception E' = E " to create an
alias to an exception, which I have hardly ever seen used (with reason, it
seems). See the following code:

# exception E;;
exception E
# exception E' = E;;
exception E'
# exception E;;
exception E
# E;;
- : exn = E
# E';;
- : exn = E
(* At this stage, E and E' should be two distinct exceptions both called E.
Nothing wrong yet, that kind of weird situation can also be created with
types (redefining a type when objects of this type exist) *)
# E = E';;
- : bool = true
# match E with E' -> true | _ -> false;;
- : bool = false

The exceptions are equal, but don't match according to the toplevel... my
opinion is that they should be different in both cases: they were defined
separately with two different definitions of "exception E".
When E is parametrised with a type, this can lead to a failure of the type
system (objects of type t = T are shown as int = 0 !)

This is with Caml 3.08.3, but it doesn't seem to have been corrected in
earlier versions. Any ideas ? If the structure exception E = F is
considered useful, a stronger semantics should be attached to it, but is it
really ?

These two problems point to the handling of pattern-matching of exceptions,
maybe not the marshalling of exceptions itself. Could someone explain where
this weakness comes from (implementation of the extensible variable type ?)

Regards,
Louis Gesbert


^ permalink raw reply	[flat|nested] 8+ messages in thread
* Re: [Caml-list] Bugs with pattern-matching and exceptions
@ 2006-03-01 23:07 Louis Gesbert
  2006-03-02  6:47 ` Alain Frisch
  0 siblings, 1 reply; 8+ messages in thread
From: Louis Gesbert @ 2006-03-01 23:07 UTC (permalink / raw)
  To: caml-list

> Marshalling is known to be type unsafe, of course, so that's "not a
> problem" although it would be nice to have type-safe marshalling.

I don't agree here, there really *is* a problem with marshalling
exceptions, independently from type safety: it doesn't work. This problem
has already been mentionned on the list (and in the bug tracker), which is
why I didn't describe it in detail. However, it would be nice to have a
word from the caml team about it.

Let's suppose we have two processes running the exact same compiled program.
- Process A marshalls an exception (Ex "arg")
- Process A transmits the resulting string to process B
- Process B unmarshalls the string, and makes sure to type it as an
exception. Since we have the same compiled program, exception Ex is defined
in the same way in A and B.
- On process B, the exception *seems* right, and you can test equality.
However, if you try to pattern-match against it, the process just hangs.

As I understand it, the constructor Ex of the variant type exn is
represented by a pointer in (Ex "arg"), and this pointer is kept as-is by
the marshaller which doesn't understand the particular structure of
exceptions.

The (awfully ugly ;-)) workaround I used (I *really* needed to marshal
exceptions, you see...) is to extract the constructor (as a string) and the
parameters from the exception, using Obj, marshal the couple and deal with
it on the receiving hand.

> However, what happens with dynamically loaded bytecode that declares
> new exceptions? Perhaps there is some unsoundness there... :-)

Yes, that could be worth looking into, but I don't actually think it makes
the problem worse since the structure of exceptions is already dynamic.

Thanks for the pointers :-)
Louis


^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2006-03-03  9:03 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-02-28 12:29 Bugs with pattern-matching and exceptions Louis Gesbert
2006-02-28 15:51 ` [Caml-list] " Alain Frisch
2006-03-01  0:03 ` Jacques Garrigue
2006-03-01  7:00   ` Alain Frisch
     [not found] ` <200602281333.52448.jon@ffconsultancy.com>
2006-03-01 11:10   ` Louis Gesbert
2006-03-01 23:07 Louis Gesbert
2006-03-02  6:47 ` Alain Frisch
2006-03-03  9:02   ` Louis Gesbert

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).