caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* on objects, equality and playing nicely with the stdlib
@ 2008-05-29  4:50 Peng Zang
  2008-05-29  7:13 ` [Caml-list] " Jon Harrop
  0 siblings, 1 reply; 4+ messages in thread
From: Peng Zang @ 2008-05-29  4:50 UTC (permalink / raw)
  To: caml-list

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hello list,

Recently, in building some toy games, I've started using objects.  I've been 
pleased with them and often find the subtyping convenient.

However, I've run into some issues with the standard physical equality of 
objects that the (=) operator performs.  Namely, it makes several stdlib 
modules difficult to use.  Modules that provides a polymorphic (generic) 
interface such as Hashtbl, List, Stack, etc.. rely on the (=) operator.  This 
is fine as long as it happens to be what I need.  The minute I introduce my 
own equality however, things break down.

What is the best way to go about using the stdlib (or similar) when you have 
your own equality function?  This problem almost never shows up if you just 
stick with basic data types, but with objects you quickly run into it.

Thanks in advance,

Peng
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.7 (GNU/Linux)

iD8DBQFIPjYcfIRcEFL/JewRAlkvAKCYjJhIc5dw5YMiwUS02LR8RowsDQCgzzAI
gpif0G1fWn0vlCzzaoalWa4=
=+0ZK
-----END PGP SIGNATURE-----


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Caml-list] on objects, equality and playing nicely with the stdlib
  2008-05-29  4:50 on objects, equality and playing nicely with the stdlib Peng Zang
@ 2008-05-29  7:13 ` Jon Harrop
  2008-05-29  8:13   ` Christophe Raffalli
  0 siblings, 1 reply; 4+ messages in thread
From: Jon Harrop @ 2008-05-29  7:13 UTC (permalink / raw)
  To: peng.zang, caml-list

On Thursday 29 May 2008 05:50:33 Peng Zang wrote:
> Hello list,
>
> Recently, in building some toy games, I've started using objects.  I've
> been pleased with them and often find the subtyping convenient.
>
> However, I've run into some issues with the standard physical equality of
> objects that the (=) operator performs.  Namely, it makes several stdlib
> modules difficult to use.  Modules that provides a polymorphic (generic)
> interface such as Hashtbl, List, Stack, etc.. rely on the (=) operator. 
> This is fine as long as it happens to be what I need.  The minute I
> introduce my own equality however, things break down.
>
> What is the best way to go about using the stdlib (or similar) when you
> have your own equality function?  This problem almost never shows up if you
> just stick with basic data types, but with objects you quickly run into it.

Hi Peng,

You can use the Hashtbl.Make functor to create hash tables using your own 
equality (and hashing) function. Other than that, you're screwed: resort to 
cut and paste from the stdlib.

-- 
Dr Jon D Harrop, Flying Frog Consultancy Ltd.
http://www.ffconsultancy.com/products/?e


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Caml-list] on objects, equality and playing nicely with the stdlib
  2008-05-29  7:13 ` [Caml-list] " Jon Harrop
@ 2008-05-29  8:13   ` Christophe Raffalli
  2008-05-29 13:25     ` Peng Zang
  0 siblings, 1 reply; 4+ messages in thread
From: Christophe Raffalli @ 2008-05-29  8:13 UTC (permalink / raw)
  To: Jon Harrop; +Cc: peng.zang, caml-list

[-- Attachment #1: Type: text/plain, Size: 2464 bytes --]

Jon Harrop a écrit :
> On Thursday 29 May 2008 05:50:33 Peng Zang wrote:
>> Hello list,
>>
>> Recently, in building some toy games, I've started using objects.  I've
>> been pleased with them and often find the subtyping convenient.
>>
>> However, I've run into some issues with the standard physical equality of
>> objects that the (=) operator performs.  Namely, it makes several stdlib
>> modules difficult to use.  Modules that provides a polymorphic (generic)
>> interface such as Hashtbl, List, Stack, etc.. rely on the (=) operator. 
>> This is fine as long as it happens to be what I need.  The minute I
>> introduce my own equality however, things break down.
>>
>> What is the best way to go about using the stdlib (or similar) when you
>> have your own equality function?  This problem almost never shows up if you
>> just stick with basic data types, but with objects you quickly run into it.
> 
> Hi Peng,
> 
> You can use the Hashtbl.Make functor to create hash tables using your own 
> equality (and hashing) function. Other than that, you're screwed: resort to 
> cut and paste from the stdlib.
> 

Via Custom block (section 18.9.1 of the manual), you could create a functor
with the following shape (I did not check my syntax):

module type Quotient =
  type t
  type qt
  val class : t -> qt
  val get_representent : qt -> t
end

module type Equivalence =
  type t
  val compare : t -> t -> int
end

module Make_Quotient (R : Equivalence) : Quotient with type t = R.t

Such that (class x) builds a custom block with
the given compare as comparison function ...
You can add hash and serialization to the Equivalence type too ...

It is a pity we have to do that from C, this should be the default way
to bridge the gap between polymorphic comparison and specific comparison.
(and PML will provide a buildin class constructor in the near future).

Hope this helps,
Christophe


-- 
Christophe Raffalli
Universite de Savoie
Batiment Le Chablais, bureau 21
73376 Le Bourget-du-Lac Cedex

tel: (33) 4 79 75 81 03
fax: (33) 4 79 75 87 42
mail: Christophe.Raffalli@univ-savoie.fr
www: http://www.lama.univ-savoie.fr/~RAFFALLI
---------------------------------------------
IMPORTANT: this mail is signed using PGP/MIME
At least Enigmail/Mozilla, mutt or evolution
can check this signature. The public key is
stored on www.keyserver.net
---------------------------------------------


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 252 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Caml-list] on objects, equality and playing nicely with the stdlib
  2008-05-29  8:13   ` Christophe Raffalli
@ 2008-05-29 13:25     ` Peng Zang
  0 siblings, 0 replies; 4+ messages in thread
From: Peng Zang @ 2008-05-29 13:25 UTC (permalink / raw)
  To: Christophe Raffalli; +Cc: caml-list

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1


On Thursday 29 May 2008 04:13:20 am Christophe Raffalli wrote:
> Via Custom block (section 18.9.1 of the manual), you could create a functor
> with the following shape (I did not check my syntax):
>
> module type Quotient =
>   type t
>   type qt
>   val class : t -> qt
>   val get_representent : qt -> t
> end
>
> module type Equivalence =
>   type t
>   val compare : t -> t -> int
> end
>
> module Make_Quotient (R : Equivalence) : Quotient with type t = R.t
>
> Such that (class x) builds a custom block with
> the given compare as comparison function ...
> You can add hash and serialization to the Equivalence type too ...
>
> It is a pity we have to do that from C, this should be the default way
> to bridge the gap between polymorphic comparison and specific comparison.
> (and PML will provide a buildin class constructor in the near future).
>
> Hope this helps,
> Christophe

This is very interesting.  I've never read that part of the manual.  It's 
probably too much overhead for me, so I'll resort to cut + paste first and 
see how it goes.  Thanks,

Peng
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.7 (GNU/Linux)

iD8DBQFIPq66fIRcEFL/JewRAh2mAKCjCBE3OLYeAudddf5qCoF5jtKmLgCfTRWP
Q5xoPoXccaD512zj99mDs7Q=
=X2pn
-----END PGP SIGNATURE-----


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2008-05-29 13:25 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-05-29  4:50 on objects, equality and playing nicely with the stdlib Peng Zang
2008-05-29  7:13 ` [Caml-list] " Jon Harrop
2008-05-29  8:13   ` Christophe Raffalli
2008-05-29 13:25     ` Peng Zang

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).