From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id RAA23915; Mon, 16 Jul 2001 17:11:55 +0200 (MET DST) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id RAA23781 for ; Mon, 16 Jul 2001 17:11:54 +0200 (MET DST) Received: from pauillac.inria.fr (pauillac.inria.fr [128.93.11.35]) by concorde.inria.fr (8.11.1/8.10.0) with ESMTP id f6GFBpT03938; Mon, 16 Jul 2001 17:11:51 +0200 (MET DST) Received: (from weis@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id RAA23787; Mon, 16 Jul 2001 17:11:51 +0200 (MET DST) From: Pierre Weis Message-Id: <200107161511.RAA23787@pauillac.inria.fr> Subject: Re: [Caml-list] exceptions and the polymorphic equality In-Reply-To: <20010715220500D.sumii@yl.is.s.u-tokyo.ac.jp> from "eijiro_sumii@anet.ne.jp" at "Jul 15, 101 10:05:00 pm" To: eijiro_sumii@anet.ne.jp Date: Mon, 16 Jul 2001 17:11:51 +0200 (MET DST) Cc: caml-list@inria.fr X-Mailer: ELM [version 2.4ME+ PL28 (25)] MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk > 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 unreliable. 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 string). > # 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, Pierre.Weis@inria.fr, http://pauillac.inria.fr/~weis/ ------------------- Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/ To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr