caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Gabriel Scherer <gabriel.scherer@gmail.com>
To: "Soegtrop, Michael" <michael.soegtrop@intel.com>
Cc: Alain Frisch <alain.frisch@lexifi.com>,
	"caml-list@inria.fr" <caml-list@inria.fr>
Subject: Re: [Caml-list] Specify the default hash function for a type
Date: Fri, 13 May 2016 08:01:14 -0400	[thread overview]
Message-ID: <CAPFanBEpjPrnWXC3=Lxd=3y7jzSZcsZpx8GWerYFr4Qg5PY9qg@mail.gmail.com> (raw)
In-Reply-To: <0F7D3B1B3C4B894D824F5B822E3E5A172CEF175C@IRSMSX102.ger.corp.intel.com>

From the C side it is possible to create "custom" values that come
with user-specified comparison and hashing functions. However, for now
the runtime does not allow creating them from OCaml -- this is indeed
a desirable feature, but it's not trivial to implement. I don't really
whether it's actually "really hard", or just that it requires some
effort that nobody has invested yet -- the latter would be my guess.

If you don't mind going through a C stubs to do this, you could thus
create the custom value in C land, and export that in OCaml land.
See the manual:
  Interfacing C with OCaml > custom blocks
  http://caml.inria.fr/pub/docs/manual-ocaml-4.03/intfc.html#sec442

On Fri, May 13, 2016 at 5:26 AM, Soegtrop, Michael
<michael.soegtrop@intel.com> wrote:
> Dear Alain,
>
> this are temptations on Friday morning ... I guess as a beginner I should resist, not be lazy, and write the hash function by hand. But I put it into appendix D of my OCaml notes ;-)
>
> Best regards,
>
> Michael
>
>> -----Original Message-----
>> From: Alain Frisch [mailto:alain.frisch@lexifi.com]
>> Sent: Friday, May 13, 2016 11:06 AM
>> To: Soegtrop, Michael; caml-list@inria.fr
>> Subject: Re: [Caml-list] Specify the default hash function for a type
>>
>> Dear Michael,
>>
>> Don't tell anybody (ahem), but the following might work depending on your
>> exact use case...
>>
>> If you define a type such as:
>>
>>   type t = {
>>     foo: ....;
>>     id: int;
>>   }
>>
>> and if you set the tag of values for this type to Obj.record_tag (= 248) with
>> Obj.set_tag, then polymorphic hash *and* comparison functions will use the
>> "id" field and ignore the "foo" field (and any other extra fields).  It is
>> important to keep "id" as the second field to mimic the layout of real object
>> values (and exception slots).
>>
>> In particular, this lets you easily "forget" sub-parts of a value (by setting "id"
>> to a fixed constant), or implement "references compared with physical
>> equality" (use a counter to assign unique "id" to each such reference).
>>
>>
>> Best regards,
>>
>> Alain
> Intel Deutschland GmbH
> Registered Address: Am Campeon 10-12, 85579 Neubiberg, Germany
> Tel: +49 89 99 8853-0, www.intel.de
> Managing Directors: Christin Eisenschmid, Christian Lamprechter
> Chairperson of the Supervisory Board: Nicole Lau
> Registered Office: Munich
> Commercial Register: Amtsgericht Muenchen HRB 186928
>
>
> --
> Caml-list mailing list.  Subscription management and archives:
> https://sympa.inria.fr/sympa/arc/caml-list
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs

  reply	other threads:[~2016-05-13 12:01 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-05-13  7:52 Soegtrop, Michael
2016-05-13  8:13 ` Ben Millwood
2016-05-13  8:40   ` Soegtrop, Michael
2016-05-13  9:06 ` Alain Frisch
2016-05-13  9:26   ` Soegtrop, Michael
2016-05-13 12:01     ` Gabriel Scherer [this message]
2016-05-13 12:23       ` Alain Frisch
2016-05-13 12:32       ` Soegtrop, Michael
2016-05-13 13:50   ` Pierre Chambart
2016-05-13 13:56     ` Alain Frisch
2016-05-13 16:17       ` Soegtrop, Michael
2016-05-13 18:57         ` Thomas Braibant
2016-05-13 22:45           ` Soegtrop, Michael
2016-05-14  8:41             ` Thomas Braibant
2016-05-14  9:06               ` Soegtrop, Michael
2016-05-17 13:03                 ` Soegtrop, Michael

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='CAPFanBEpjPrnWXC3=Lxd=3y7jzSZcsZpx8GWerYFr4Qg5PY9qg@mail.gmail.com' \
    --to=gabriel.scherer@gmail.com \
    --cc=alain.frisch@lexifi.com \
    --cc=caml-list@inria.fr \
    --cc=michael.soegtrop@intel.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).