Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Xavier Leroy <Xavier.Leroy@inria.fr>
To: Georges Brun-Cottan <gbruncot@emc.com>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] equality over functional value
Date: Mon, 23 Apr 2001 09:56:27 +0200	[thread overview]
Message-ID: <20010423095627.A517@pauillac.inria.fr> (raw)
In-Reply-To: <200104202012.QAA09326@lub0127.lss.emc.com>; from gbruncot@emc.com on Fri, Apr 20, 2001 at 04:12:14PM -0400

> Hi, 
> 
> A friend of mine is just starting with ocaml. He was puzzled by the
> following result:
> 
> # let a i = i;;
> val a : 'a -> 'a = <fun>
> # let b i = i;;
> val b : 'a -> 'a = <fun>
> # a=b;;
> Uncaught exception: Invalid_argument "equal: functional value".
> # a=a;;
> - : bool = true
> # 
> 
> That is 'a=a' does not return the expected exception.  Actually it
> first hit this "curiosity" when creating a polymorphic 'sort' function
> on lists - and by applying it to a [sort;sort..] list. It worked.
> 
> Is this a bug? 

It's a questionable behavior.  As Alain Frisch said, polymorphic
structural equality is implemented by first testing physical (pointer)
equality.  Besides speed, this has the advantage that x == y implies x = y.

However, this causes a = a where a is a function to return true
instead of raising an exception as you expect.

(Another weird consequence of this implementation of physical equality
is that
             (nan, nan) = (nan, nan)
returns true, which is incorrect according to the IEEE specs: the NaN
float is not equal to itself!)

The special case (first test pointer equality) in structural equality
could be eliminated, although I don't know how big a performance
impact this would have.

More generally, equality between functions can be interpreted in
several ways:

1- Extensional, pessimistic: f = g iff for all x, f x = g x,
and since this is undecidable, equality always raises an exception
when passed function values.

2- Extensional, approximated: same definition as above, but we return
"true" in some cases where the two functions are guaranteed to be
extensionally equal, e.g. f and g point to the same closure, or even f
and g have the same code pointer and contain equal values in their
environments.  Otherwise, we raise an exception.

3- By representation: two functions are equal iff their closures are
structurally equal, i.e. they have the same code pointer and contain
equal values in their environment.

Interpretation 3- is useless I think, because it depends very much on
the compiler's closure representation strategy.  In other terms, while
a "true" result guarantees that the two functions are extensionally
equal, a "false" result does not mean anything.

The current interpretation 2- is semantically more sound: if it
returns "true", then the two functions are extensionally equal;
and it never returns "false", so it never claims that two functions
are extensionally different!  Still, it is rather unintuitive.
Also, the current implementation exposes too much the order of the tests,
i.e. "(0, f) = (1, g)" returns "false", but
"(f, 0) = (g, 1)" raises an exception.

Interpretation 1- is easiest to explain, although it entails a bit of
a performance penalty as I said above.

Food for thoughts...

- Xavier Leroy
-------------------
To unsubscribe, mail caml-list-request@inria.fr.  Archives: http://caml.inria.fr


  parent reply	other threads:[~2001-04-23  7:56 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-04-20 20:12 Georges Brun-Cottan
2001-04-20 21:23 ` Alain Frisch
2001-04-21 11:32   ` Marcin 'Qrczak' Kowalczyk
2001-04-21 15:24 ` David Monniaux
2001-04-23  7:01 ` Jean-Christophe Filliatre
2001-04-23  7:56 ` Xavier Leroy [this message]
2001-04-23 14:04   ` Alain Frisch
2001-04-24  7:13   ` Fabrice Le Fessant

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=20010423095627.A517@pauillac.inria.fr \
    --to=xavier.leroy@inria.fr \
    --cc=caml-list@inria.fr \
    --cc=gbruncot@emc.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