From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.1.3 (2006-06-01) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.4 required=5.0 tests=AWL autolearn=disabled version=3.1.3 X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from mail1-relais-roc.national.inria.fr (mail1-relais-roc.national.inria.fr [192.134.164.82]) by yquem.inria.fr (Postfix) with ESMTP id 025B5BBAF for ; Wed, 3 Sep 2008 01:09:39 +0200 (CEST) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AgIBAKhkvUjRVYTzlGdsb2JhbACSEj4BAQEBCQUIBw8FmkSHFQECgyU X-IronPort-AV: E=Sophos;i="4.32,320,1217800800"; d="scan'208";a="16739479" Received: from an-out-0708.google.com ([209.85.132.243]) by mail1-smtp-roc.national.inria.fr with ESMTP; 03 Sep 2008 01:09:39 +0200 Received: by an-out-0708.google.com with SMTP id b2so377357ana.102 for ; Tue, 02 Sep 2008 16:09:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:from:reply-to:to:subject:date :user-agent:content-type:content-transfer-encoding :content-disposition:message-id; bh=ez7bncBjDLOLVGJs2C2OT5FUrWqoNRf8VYOE6Cu3IHk=; b=Isk8E8IYwh29fVltCzceuhqAGtUBfVOA8ynig6oM2GV09vx+/Hca4m44GQlIXputP8 pQBZTG5M4ErbTZwUUKVuy53wGvY86Eo1Njx/1mOvEanuPw4v/TO8mZVUP5nqEujM+qAl d4koFEuSe/hfyrA7qygc1wkq2WI6jdnGiScRs= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=from:reply-to:to:subject:date:user-agent:content-type :content-transfer-encoding:content-disposition:message-id; b=dvw0nR5ZA4+v+K8kAfGEvHEYXM5ekMbMYdqx7usHshBt5nET/10y8U3Z7TEjdHHmAa +W0TBdOp7NDpBVXuns5X4iiX4e31eGm5RDty7DVzXLQLGWr8/Y6/kK40o18t87pHbdZE lyimU0fj/idTPcFgw2P9wtkdoM4UfOvSevo1w= Received: by 10.100.171.10 with SMTP id t10mr8214441ane.123.1220396978318; Tue, 02 Sep 2008 16:09:38 -0700 (PDT) Received: from lawn-143-215-204-204.lawn.gatech.edu ( [143.215.204.204]) by mx.google.com with ESMTPS id d19sm14379839and.17.2008.09.02.16.09.37 (version=TLSv1/SSLv3 cipher=RC4-MD5); Tue, 02 Sep 2008 16:09:37 -0700 (PDT) From: Peng Zang Reply-To: peng.zang@gmail.com To: caml-list@yquem.inria.fr Subject: on polymorphic compare and objects Date: Tue, 2 Sep 2008 19:09:32 -0400 User-Agent: KMail/1.9.7 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Content-Disposition: inline Message-Id: <200809021909.35930.peng.zang@gmail.com> X-Spam: no; 0.00; hash:01 ocaml:01 ocaml:01 bool:01 ocaml's:01 ocaml's:01 structurally:01 bool:01 peng:98 peng:98 blog:98 buys:98 equality:01 equality:01 polymorphic:01 -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 I recently came across a post on Janest lamenting the perils of polymorphic compare. http://ocaml.janestcapital.com/?q=comment/reply/33 They make a great point, and I thought of a solution that has worked well for me in the past few months that I would like to share, in part, to solicit feedback from the community. Here is my approach. It is, in part, excerpted from a conversation with Brain Hurt and also posted on the Janest blog. As warning, my approach does use some magic and OCaml guts but no more than what is made available for OCaml/C interface. It is also mostly hidden once done. - ---- The (compare) function has the signature ('a -> 'a -> bool). That means it is a function that imposes a total ordering for *all* types -- even those that have not yet been defined. Clearly, it cannot be the desired ordering for all (and future) types. If OCaml had a "content" equality function (ie. two things are equal if they can be used interchangably in pure functions) it would have a similar problem. It is my opinion that this approach is really just for convenience and performance reasons. The right solution would use something like type classes or go with Java's object based solution (eg. Comparable interface). Here's my solution to this. I modify the (compare) and other similar functions to check for objects and use its appropriate method if available. In otherwords, if you call compare on a (int,int) tuple, it has OCaml's standard behavior. But if you call compare on a [int,int] tuple class, it will use the class' compare method. This hybrids between OCaml's polymorphic solution and the Java, method-dispatch solution. One might also think of it as a hook. If the user defines some type that has unique comparison behavior he/she can do so by wrapping the type in a class and specifying the unique behavior via the compare method. This means the rest of the code never needs to know and use special compare functions. It can always use compare, knowing that it will automatically do the right thing. In this way (compare) and (content-equals) can actually be well defined for all (including future) types. Let's take as an example, the content-equals function which I'll just call equal for now. (equal) has a very specific semantic meaning: it returns true if its two arguments are exchangable in a pure context (ie. no side-effects). (equal) is well defined for all simple types: int, float, string, etc.. For objects, we require that all objects implement an equal method that satisfies the semantic contract. Thus (equal) the function is also well defined on objects. For all other types, whatever they may be, either the default structural comparison behavior of (equal) satisfies the semantic contract or it does not. If it does, then (equal) is well defined. If it does not, we require it to be wrapped in an object so that an appropriate behavior can be specified. As a concrete example consider: type xycoord = (int, int) As it happens, structural equality is what we want. Since (equal) already does this we are done. Now consider: type rational_number = (int, int) Here, structural equality is not what we want. (1/2) is the same rational number as (2/4) but they are not structurally equal. Thus in order to satisfy the semantic contract of (equal) we must instead do: class type rational_number = object method equal : rational_number -> bool ... end Meanwhile, a function like List.assoc, but defined using (equal), works equally well on both types. This approach lets you keep the OCaml approach of a polymorphic compare/equal function, but does not limit you to a single algorithm for performing the comparison. It buys you the power of objects (much like that of type classes) but without the requirement that everything be an object. I have found it to be a sweet spot and use it in my code regularly. - ---- As always, I welcome any comments, suggestions, questions or flames =] Peng -----BEGIN PGP SIGNATURE----- Version: GnuPG v2.0.7 (GNU/Linux) iD8DBQFIvcevfIRcEFL/JewRAqQaAJ485qiwtMKy5W9dJbIvNzklH/DZ3wCfQlR6 UrtMhcho4xm4mII2Depv+vs= =HZ9F -----END PGP SIGNATURE-----