caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Xavier Leroy <Xavier.Leroy@inria.fr>
To: John R Harrison <johnh@ichips.intel.com>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] User-defined equality on types?
Date: Mon, 23 Apr 2001 10:54:08 +0200	[thread overview]
Message-ID: <20010423105408.A2049@pauillac.inria.fr> (raw)
In-Reply-To: <200104181942.MAA05265@dhpc0010.pdx.intel.com>; from johnh@ichips.intel.com on Wed, Apr 18, 2001 at 12:42:16PM -0700

> I'd like to suggest allowing the user to define a chosen interpretation
> of the equality symbol, and perhaps the polymorphic orderings too, on
> each new (maybe just abstract) data type. This seems natural in the 
> context of abstract data types with non-canonical representation, giving
> a kind of quotient type. Has this ever been considered? 

Yes.  This was one of the first motivations for Haskell type classes,
I believe.

> Are there good reasons against it?

It's not easy to implement.  One can do it the Haskell way, by passing
around compiler-generated functions as extra arguments, but the
language extensions needed to declare ad-hoc polymorphic operations
and define implementations for these operations at particular types 
are fairly complex.  I'd rather not add Haskell's type classes to
OCaml :-)

Another route is to attach the special equality operation to values of
the types of interest.  OCaml allows this for abstract types that are
implemented entirely in C, via the "custom block" mechanism, but not
currently for datatypes implemented in Caml, like the "num" type.

The example of "num" is particularly tricky, since it is not even an
abstract type, just a regular sum type with 3 constructors.  Hence,
even if custom blocks were extended to contain pointers back into the
Caml heap, it would still be hard to use them for representing values
of the "num" type.

One workaround would be to implement the "num" type entirely in C,
which is what the ML-GMP library does, I think.  Another would be to
keep "num" values normalized at all times, so that structural equality
coincides with numeric equality.  Both may entail performance
penalties in some cases, though.

> At present, I find that it's often quite inconvenient to use complicated
> structures involving abstract types. In particular, I use the type of
> arbitrary-precision numbers a lot, and frequently end up having to
> define variants of standard polymorphic operations like set union for
> several different types involving ":num".

Agreed.  In OCaml, sets are presented as a functor, so you can easily
pass a non-standard ordering function and you don't have to duplicate
the code implementing sets.  But you still have to define the ordering
functions, which can be a bit of a pain for complex data structures
containing nums.

Cheers,

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


  parent reply	other threads:[~2001-04-23  8:57 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-04-18 19:42 John R Harrison
2001-04-19 17:44 ` Francisco Valverde Albacete
2001-04-19 23:25   ` John R Harrison
2001-04-19 19:57 ` Alain Frisch
2001-04-23  8:54 ` Xavier Leroy [this message]
2001-04-23 16:42   ` Brian Rogoff
2001-04-24  8:33     ` Andreas Rossberg
2001-04-19 21:00 Don Syme

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=20010423105408.A2049@pauillac.inria.fr \
    --to=xavier.leroy@inria.fr \
    --cc=caml-list@inria.fr \
    --cc=johnh@ichips.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).