Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
[Caml-list] exceptions and the polymorphic equality
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Pierre Weis <Pierre.Weis@i...>
Subject: Re: [Caml-list] exceptions and the polymorphic equality
> Hello,
> I have a question about the polymorphic equality and exceptions: it
> seems to me that the behaviour of "=" is counter-intuitive with
> respect to pattern matching of exceptions, for example as follows.

Exception definitions are generative in Caml: it means that the
definition of two exceptions with the same names generate two
different exceptions that are not confused by the language. It is
similar to type definitions, and constructors for sum type.

On the other hand, the structural equality (=) when applied to
exceptions and constructors is not completely specified, hence


>         Objective Caml version 3.01
> # exception Foo;;
> exception Foo
> # let e = Foo;;
> val e : exn = Foo
> # exception Foo;;
> exception Foo

This (new) exception Foo is different from the preceding (old) one:
the language will not confuse them.

> # let e' = Foo;;
> val e' : exn = Foo
> # e = e';;
> - : bool = true

Here, you learn that (old) Foo and (new) Foo are structurally
equivalent, i.e. represented by equivalent Caml values (e.g. the same

> # match e with Foo -> true | _ -> false;;
> - : bool = false

Yes, (old) Foo is not confused with (new) Foo.

> # try raise e with Foo -> ();;
> Uncaught exception: Foo.

Once more the two exceptions are not confused.

> I know that the two Foo's above should be distinct, but then shouldn't
> e = e' also return false?  Is this issue well known?

You're right, having e <> e' should be desirable. However, in this
case you should not test structural equality, since it is very likely
the case that e and e' are represented by the same kind of value. You
should test identity (==) instead:

# e == e';;
- : bool = false

That's what the compiler generates when pattern matching exception
values (more precisely it uses == for the exception constructor and
regular pattern matching for the rest of the pattern).

Hope this helps,

Pierre Weis

INRIA, Projet Cristal,,

Bug reports:  FAQ:
To unsubscribe, mail  Archives: