caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Jacques Garrigue <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
Date: Sat, 23 Oct 2004 12:31:39 +0900 (JST)	[thread overview]
Message-ID: <20041023.123139.106410806.garrigue@kurims.kyoto-u.ac.jp> (raw)
In-Reply-To: <aee06c9e041022184937006109@mail.gmail.com>

From: Nathaniel Gray <n8gray@gmail.com>

> 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


  reply	other threads:[~2004-10-23  3:31 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-10-23  1:49 Nathaniel Gray
2004-10-23  3:31 ` Jacques Garrigue [this message]
2004-10-23  3:39   ` David Brown
2004-10-23  5:00     ` skaller
2004-10-23 21:49       ` Nathaniel Gray
2004-10-23 21:24   ` Nathaniel Gray
2004-10-23 21:33     ` Nathaniel Gray
2004-10-24  3:00     ` John Prevost
2004-10-24  5:18     ` skaller
2004-10-24 22:52       ` Nathaniel Gray
2004-10-25 13:21 ` Damien Doligez
2004-10-25 14:25   ` Jacques Carette
2004-10-26 14:07     ` Damien Doligez
2004-10-26 15:05       ` Jacques Carette
2004-10-26 17:34         ` skaller
2004-10-26 20:02           ` [Caml-list] Combined compilation and linking Jon Harrop

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20041023.123139.106410806.garrigue@kurims.kyoto-u.ac.jp \
    --to=garrigue@kurims.kyoto-u.ac.jp \
    --cc=caml-list@inria.fr \
    --cc=n8gray@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).