From: rugger@cc.umanitoba.ca (Kip Rugger)
To: caml-list@pauillac.inria.fr
Cc: rugger@cc.umanitoba.ca
Subject: Re: definition recursive de valeur
Date: Tue, 5 Mar 96 15:34 CST [thread overview]
Message-ID: <m0tu4NT-0007ILC@rugger.nodomain.ca> (raw)
>As you may know, Caml offers two polymorphic predicates to compare
>values, that is == (physical identity) and = (re'cursive descent into
>values to find differences). None of these is the mathematical
>equality, except for some basic data types such that integers. In the
>library of the Caml V3.1 system, we have a third ``equal''
>predicate that ``Compare graphs''. Unfortunately this ``equal''
>predicate is hard to implement efficiently, is highly not portable,
>and moreover was found not very useful in practice: when people need
>such a sophisticated predicate, they need a complex semantic equality,
>upto some equivalence relationship, this predicate by no means
>correspond to ``equal'', so that they must write the predicate by themselves.
>Pierre Weis
The ``equal'' predicate does suggest an implementation using mark bits
in the data structure, making it not portable. The only portable
algorithm that I know is as follows (in pseudo C code):
if ( {terminal case} ) ...
if ( *p == *q ) return true;
save( p, *p );
t = *p, *p = *q;
return compare( t, *q );
so given the initial conditions:
p --> x = 1 :: y
q --> y = 1 :: x
this is reduced after one iteration to:
q --> x = 1 :: x
p --> y = 1 :: x
from which equality follows. Unfortunately, both data structures are
potentially destroyed in the process, making it necessary to save and
restore the modifications on a stack.
I agree that ``equal'' is of limited use, even if it could handle
cyclical structures. It is usually better to build unique representations
of the underlying structures so that ``=='' can be used instead.
What is useful is a ``shallow equal'' which applies ``=='' to the top
level of a data structure. This allows easy comparison of records,
tuples, or arrays of scalar objects, as well as supplying the terminal
condition for recursive data structures.
Kip Rugger
next reply other threads:[~1996-03-06 11:29 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
1996-03-05 21:34 Kip Rugger [this message]
-- strict thread matches above, loose matches on Subject: below --
1996-02-22 14:16 Michel Levy
1996-02-23 12:19 ` Pierre Weis
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=m0tu4NT-0007ILC@rugger.nodomain.ca \
--to=rugger@cc.umanitoba.ca \
--cc=caml-list@pauillac.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