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 FAA19684; Sat, 23 Oct 2004 05:31:56 +0200 (MET DST) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id FAA19526 for ; Sat, 23 Oct 2004 05:31:55 +0200 (MET DST) Received: from kurims.kurims.kyoto-u.ac.jp (kurims.kurims.kyoto-u.ac.jp [130.54.16.1]) by nez-perce.inria.fr (8.13.0/8.13.0) with ESMTP id i9N3VqRS011826 for ; Sat, 23 Oct 2004 05:31:54 +0200 Received: from localhost (suiren.kurims.kyoto-u.ac.jp [130.54.16.25]) by kurims.kurims.kyoto-u.ac.jp (8.9.3p2-20030924/3.7W) with ESMTP id MAA08649; Sat, 23 Oct 2004 12:31:47 +0900 (JST) Date: Sat, 23 Oct 2004 12:31:39 +0900 (JST) Message-Id: <20041023.123139.106410806.garrigue@kurims.kyoto-u.ac.jp> To: n8gray@gmail.com Cc: caml-list@inria.fr Subject: Re: [Caml-list] Single-case union types as strong typedefs From: Jacques Garrigue In-Reply-To: References: X-Mailer: Mew version 4.0.64 on Emacs 21.3 / Mule 5.0 (SAKAKI) Mime-Version: 1.0 Content-Type: Text/Plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Miltered: at nez-perce with ID 4179D0A8.001 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Loop: caml-list@inria.fr X-Spam: no; 0.00; caml-list:01 typedefs:01 jacques:01 degenerate:01 conceptually:01 heights:99 unlucky:01 boxing:01 1,3:01 1,3:01 dumped:01 haskell:01 jacques:01 int:01 int:01 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk From: Nathaniel Gray > Since we're talking about degenerate cases (unit vs. void) I might as > well ask a question that's been on my mind for a while. There are > many times when you have types that are structurally identical but > conceptually different. One example is lengths measured in different > units, another is values measured in identical units that shouldn't be > mixed unintentionally (e.g. widths and heights). It would be really > nice to be able to use a coding style like this (contrived) example > that you could imagine coming from a graphics library: > > (* Library code: *) > type relative_pos = Rpos of int * int > type absolute_pos = Apos of int * int [...] > This is all legal right now, but the problem is performance since the > tuples get boxed unnecessarily. But non-polymorphic single-case union > types can always be optimized away. Unlucky! With your example, there is no extra boxing involved. I.e. (1,3) and Rpos(1,3) and Apos(1,3) all share the same physical representation (a block with 2 fields). The problem you describe only occurs when your single constructor has only one argument. Indeed, this would be nice to have the overhead compiled away. I don't remember whether there was a concrete reason not to. At least one reason could be that if you add a new constructor later, you will not be able to read dumped values of the previous type anymore, but this doesn't seem a very strong reason. Note that Haskell has a special notation for this case, using "newtype" rather than "type" or "data", which marks explicit this as a special case. Jacques Garrigue ------------------- To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/ Beginner's list: http://groups.yahoo.com/group/ocaml_beginners