Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: "David Allsopp" <dra-news@metastack.com>
To: "'bluestorm'" <bluestorm.dylc@gmail.com>
Cc: "'W Dan Meyer'" <wojciech.meyer@googlemail.com>,
	<caml-list@yquem.inria.fr>
Subject: RE: [Caml-list] Converting variants with only constant constructors 	to integers
Date: Tue, 8 Jun 2010 10:14:02 +0100	[thread overview]
Message-ID: <015201cb06ea$f06adc00$d1409400$@romulus.metastack.com> (raw)
In-Reply-To: <AANLkTilku93HnpBpA6a3elkHX6u6lodPtPq7NFVKsV89@mail.gmail.com>

Gabriel Scherer wrote:
> Incidentally, the general function for getting a unique integer for a
> variant type with non-constant constructors isn't that bad either:
>
> let int_of_gen x =
> let x = Obj.repr x
> in
>   if Obj.is_block x
>   then -1 * Obj.tag x - 1
>   else (Obj.magic x : int)
>
> I consider that version much better. You could also write it in a more
restrictive way :
>
> let to_int x =
>   let repr = Obj.repr x in
>   if Obj.is_int repr
>   then (Obj.obj repr : int)
>   else invalid_arg "to_int";;

(that doesn't quite do the same thing but I don't think that was your point)

My "problem" is that for each specific case where it's only constant
constructors, I can't help but feel guilty that code will execute for the
conversion of a value to the same value (%identity is a no-op most of the
time). It's not that everything has to be pointlessly micro-optimised, but
you know what I mean?

> The good point about those is that they check that the memory
representation is compatible 
> before casting (while the "%identity" does not). They're safer. Of course,
they still break 
> parametricity if used polymorphically, so as you said type should be
constrained.

> I still would prefer the more direct pattern-matching definition : it may
require code 
> generation not to be tedious, but the generated code is an honourable
OCaml citizen.

Of course, if you have the following:

type t = A | B | C
let int_of_t = function
  A -> 0
| B -> 1
| C -> 2

Then in fact I believe that the compiler already converts that to a
hashtable lookup instead of a sequence of jumps so it would be fairly
reasonable (and easy) to have the compiler spot that the match is exactly a
conversion to the tag value and so remove the match completely (and given
that the compiler already has two different strategies for match clauses,
it's not even that inconsistent). camlp4 of course solves the generation
problem (but there's no need to tell you that!!)


David


  reply	other threads:[~2010-06-08  9:14 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-06-07 18:07 Török Edwin
2010-06-07 18:25 ` [Caml-list] " W Dan Meyer
2010-06-07 18:45   ` bluestorm
     [not found] ` <87sk4y7lc7.fsf@gmail.com>
     [not found]   ` <4C0D3B0F.4060502@gmail.com>
2010-06-07 18:32     ` Török Edwin
2010-06-07 18:50       ` W Dan Meyer
2010-06-07 18:48 ` David Allsopp
2010-06-07 19:46   ` Török Edwin
2010-06-07 19:56     ` bluestorm
2010-06-07 22:51       ` W Dan Meyer
2010-06-08  7:42         ` David Allsopp
2010-06-08  7:59           ` bluestorm
2010-06-08  9:14             ` David Allsopp [this message]
2010-06-08  9:36               ` Luc Maranget
2010-06-08  9:45                 ` David Allsopp
2010-06-08  9:51                   ` Luc Maranget
2010-06-08 10:21                     ` David Allsopp
2010-06-08 10:21                   ` Dario Teixeira
2010-06-08 11:28       ` Kaustuv Chaudhuri
2010-06-08 11:40         ` bluestorm
2010-06-08 14:37           ` Jacques Garrigue
2010-06-08 18:22             ` Kaustuv Chaudhuri
2010-06-09  1:34               ` Jacques Garrigue
2010-08-23 14:36               ` Damien Doligez

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='015201cb06ea$f06adc00$d1409400$@romulus.metastack.com' \
    --to=dra-news@metastack.com \
    --cc=bluestorm.dylc@gmail.com \
    --cc=caml-list@yquem.inria.fr \
    --cc=wojciech.meyer@googlemail.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