From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from weis@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id MAA31508 for caml-redistribution; Mon, 18 Jan 1999 12:09:15 +0100 (MET) 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 MAA14936 for ; Sun, 17 Jan 1999 12:13:29 +0100 (MET) Received: from pauillac.inria.fr (pauillac.inria.fr [128.93.11.35]) by concorde.inria.fr (8.8.7/8.8.7) with ESMTP id MAA26405; Sun, 17 Jan 1999 12:13:26 +0100 (MET) Received: (from xleroy@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id MAA07874; Sun, 17 Jan 1999 12:13:25 +0100 (MET) Message-ID: <19990117121325.15238@pauillac.inria.fr> Date: Sun, 17 Jan 1999 12:13:25 +0100 From: Xavier Leroy To: Jerry Jackson , "'caml-list@inria.fr'" Subject: Re: null values and sentinels References: <8A24EC12044FD21195E200600895E0B34BFBC2@goat.channelpoint.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Mailer: Mutt 0.89.1 In-Reply-To: <8A24EC12044FD21195E200600895E0B34BFBC2@goat.channelpoint.com>; from Jerry Jackson on Tue, Jan 12, 1999 at 08:50:19AM -0700 Sender: weis > I'd like to create a type like: > # type foo = {xxx: int; mutable yyy: foo};; > so that I can add nodes by mutating the "yyy" slots. Unfortunately, > it seems that I must create a sentinel value of type "foo" to stand > for a null pointer. This works but I'd like to be able to use pattern > matching on the value of yyy so as to get compile-time warnings if I > forget to check for null. > > So, I tried this: > # type foo2 = {xxx: int; mutable yyy: bar} and bar = Empty | Pointer of > foo2;; > This works (i.e. I can match on yyy in the way that I want), but > it _seems_ like this will create an extra indirection object in the > yyy slot. Whether it does or not seems to be a compiler question to > me... "Pointer of foo2" could be collapsed into foo2 by the > compiler. Is it? How should I approach this situation? The representation of Caml datatypes is uniform and always involve an indirection. So, you'll indeed get an extra indirection with the "foo2" approach. You're correct that the compiler could eliminate the indirection in your example above. The old Caml V3.1 compiler did a lot of optimizations along these lines, however they don't mix well with the module system. More recently, Jacques Garrigue and I have been looking at similar schemes for optimizing the "'a option" type. While it can be done, the implementation cost is significant, and it messes up the compiler and the runtime system a little too much to my taste. My advice would be to go with the clearer solution "foo2", and then benchmark carefully your program to see if the extra indirection really hurts performance. (Most often, it does not.) If shaving an indirection really matters, you'll have to switch to "foo" and carefully rewrite your pattern-matchings. - Xavier Leroy