caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Lauri Alanko <la@iki.fi>
To: caml-list@inria.fr
Subject: [Caml-list] Identity crisis
Date: Mon, 10 Jan 2011 14:13:25 +0200	[thread overview]
Message-ID: <20110110121325.GH323@melkinpaasi.cs.helsinki.fi> (raw)

As is well known, ocaml as no true equality comparison: (=) conflates
observably distinct values (mutable records that happen to have the
same contents at the time of comparison), whereas (==) introduces
distinctions that would otherwise been unobservable (between immutable
records that have the same contents but happen to have different
addresses). Also, (=) fails with an exception if it encounters
something it doesn't like (abstract values).

Moreover, since addresses are not stable (objects can be moved during
GC), there is no way efficient way to use record identities as keys in
trees or hash tables.

What all this means is that if the programmer wants to have an
efficiently implemented set of stateful mutable records, she has to
make sure that each record contains a unique id, and she has to
provide a custom hash or compare operation that uses this id, and she
also has to write wrappers if these stateful things are ever elements
of some other structures that need comparison or hashing. This is
superbly tedious.

Strangely enough, there is a class of stateful values that Does The
Right Thing straight out of the box: objects. You can use the standard
equality, hashing and comparison operations with objects, and they all
use a stable identity that isn't affected by GC.

Hence, if you want references in your data structures to be compared
by identity (and I can't see a reason why you would ever want
otherwise), you could use this as an alternative implementation of refs:

class ['a] ref (x : 'a) = object
    val mutable contents = x
    method get = contents
    method set x = contents <- x
end

let ref = new ref
let (!) r = r#get
let (:=) r = r#set

However, it seems really strange that one has to use objects (which
are usually considered a wholly optional component of the language) in
order to get correct semantics for stateful identities.

I don't really have any good suggestions. It would be nice if records
with mutable fields were compared like objects (and had stable ids
internally). But with e.g. strings there is no solution in sight since
the very same type is used both for string values and mutable string
buffers, so every comparison would be wrong in some context. Perhaps
we must simply accept that ocaml is irredeemably broken in this
regard.

And perhaps I will begin to use objects more from now on. I don't like
their complexity, but at least objects know who they are.


Lauri

             reply	other threads:[~2011-01-10 12:14 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-01-10 12:13 Lauri Alanko [this message]
2011-01-10 12:33 ` Alain Frisch

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=20110110121325.GH323@melkinpaasi.cs.helsinki.fi \
    --to=la@iki.fi \
    --cc=caml-list@inria.fr \
    /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).