From: John R Harrison <johnh@ichips.intel.com>
To: "Francisco Valverde Albacete" <fva@tsc.uc3m.es>
Cc: John Harrison <johnh@ichips.intel.com>, caml-list@inria.fr
Subject: Re: [Caml-list] User-defined equality on types?
Date: Thu, 19 Apr 2001 16:25:40 -0700 [thread overview]
Message-ID: <200104192325.QAA09318@dhpc0010.pdx.intel.com> (raw)
In-Reply-To: Your message of "Thu, 19 Apr 2001 19:44:37 PDT." <3ADF2405.4976B91E@tsc.uc3m.es>
Hi Francisco,
| Have you considered using functors defining your datatype
| structures? In that way you can customise the use of equality
| throught the whole module with something like:
| [...]
Thanks for the suggestion; I hadn't considered using functors. But I'm
not quite sure I understand your idea completely. I see how I can
localize the instances of the custom equality operation, but it seems
I still need to make explicit reference to a non-standard equality
*outside* the module, and it doesn't happen "transparently", still
less in composite data structures. This is the critical thing that I
want. For example, suppose I have a type of integers modulo 2 with a
non-canonical representation and a user-defined equality relation:
module Mod2 =
struct
type z2 = Integer of int
let (=) (Integer m) (Integer n) = (m - n) mod 2 = 0
let mk n = Integer n
let dest(Integer n) = n mod 2
end;;
I want all equality on type "z2" to use this defined equality
relation, including on the appropriate fields of composite data
structures where some but (maybe not all) components are of type
"z2". But this doesn't happen by default:
# let x = Mod2.mk 3 and y = Mod2.mk 5;;
val x : Mod2.z2 = Mod2.Integer 3
val y : Mod2.z2 = Mod2.Integer 5
# x = y;;
- : bool = false
If I explicitly refer to Mod2.(=), then all is well:
# Mod2.(=) x y;;
- : bool = true
But this doesn't happen automatically on composite data structures:
# (x,1) = (y,1);;
- : bool = false
and if I open the module, the regular equality is hidden:
# open Mod2;;
# x = y;;
- : bool = true
# (x,1) = (x,2);;
This expression has type Mod2.z2 * int but is here used with type Mod2.z2
Can I use your idea to set things up to do what I want?
Btw, Alain Frisch's message reminded me that one would also like to be
able to customize the generic hashing function to ensure it's substitutive
w.r.t. the defined equality relation.
Of course, the "z2" example is articifial. But there are lots of real
situations where one might want types to be represented
non-canonically. For example, I think the standard library's sets,
represented by quasi-balanced trees, are non-canonical.
John.
-------------------
To unsubscribe, mail caml-list-request@inria.fr. Archives: http://caml.inria.fr
next prev parent reply other threads:[~2001-04-23 11: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 [this message]
2001-04-19 19:57 ` Alain Frisch
2001-04-23 8:54 ` Xavier Leroy
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=200104192325.QAA09318@dhpc0010.pdx.intel.com \
--to=johnh@ichips.intel.com \
--cc=caml-list@inria.fr \
--cc=fva@tsc.uc3m.es \
/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