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=1.6 required=5.0 tests=AWL,DNS_FROM_RFC_ABUSE, HTML_MESSAGE,SPF_NEUTRAL autolearn=disabled version=3.1.3 X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from discorde.inria.fr (discorde.inria.fr [192.93.2.38]) by yquem.inria.fr (Postfix) with ESMTP id B91B4BC50 for ; Mon, 2 Oct 2006 22:36:46 +0200 (CEST) Received: from nf-out-0910.google.com (nf-out-0910.google.com [64.233.182.188]) by discorde.inria.fr (8.13.6/8.13.6) with ESMTP id k92Kakn2022193 for ; Mon, 2 Oct 2006 22:36:46 +0200 Received: by nf-out-0910.google.com with SMTP id y38so1672254nfb for ; Mon, 02 Oct 2006 13:36:46 -0700 (PDT) DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=beta; d=gmail.com; h=received:message-id:date:from:to:subject:mime-version:content-type; b=L7tqI+FkxeMhGdhq8Qk0hanNvNE2DE+DfSY8scVw5I9xXHlgwUToJ8zEfFjdwFt69Mo12HHMXvdIwhjXlkLhY3vMTr+8RNNytAIdyp8lmHxyS91CapyNH+x4xK0vz/+Yr7JvzFRLCtXRMk3pqGXc1UA6rpP5qhRWGgEtgLCc4uk= Received: by 10.49.43.2 with SMTP id v2mr6180711nfj; Mon, 02 Oct 2006 13:36:45 -0700 (PDT) Received: by 10.49.32.12 with HTTP; Mon, 2 Oct 2006 13:36:45 -0700 (PDT) Message-ID: Date: Mon, 2 Oct 2006 22:36:45 +0200 From: Tom To: caml-list Subject: Feature proposal: improved compare MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_Part_56330_7612966.1159821405905" X-j-chkmail-Score: MSGID : 4521785E.000 on discorde : j-chkmail score : X : 0/20 1 X-Miltered: at discorde with ID 4521785E.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; pointer:01 pointers:01 pointer:01 pointers:01 rec:01 rec:01 closures:01 closures:01 exception:01 exception:01 behaviour:01 behaviour:01 functions:01 functions:01 int:01 X-Attachments: cset="UTF-8" cset="UTF-8" ------=_Part_56330_7612966.1159821405905 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline I believe the compare function from standard library should be extended to allow total ordering of functional types (= closures). I suggest something like... let old_compare = compare;; let rec compare a b = let ar = Obj.repr a in let br = Obj.repr b in if Obj.tag ar = Obj.closure_tag && Obj.tag br = Obj.closure_tag then (* Field 0 of a closure is a pointer of the function code. Continue if pointers match *) let d = (-) (Obj.obj (Obj.field ar 0)) (Obj.obj (Obj.field br 0)) in if d <> 0 then d else (* now match every other field of the closures - these are the arguments to partially applied functions *) let rec f x = if x = Obj.size ar then 0 else let d = (-) (Obj.obj (Obj.field ar x)) (Obj.obj (Obj.field br x)) in if d <> 0 then d else f (x+1) in f 0 (* if the two values are not closures, call the old compare. This in fact is incorrect behaviour, as old compare will fail if it meets (different) closure values. All the match cases in old compare should be included in the new compare if the behaviour is to be correct. *) else old_compare a b;; This way, one could use for example association lists in order to store functional values and use List.assoc with them. The improvement over the old compare is this: # old_compare (f 0) (f 0);; Exception: Invalid_argument "equal: functional value". # compare (f 0) (f 0);; - : int = 0 - Tom ------=_Part_56330_7612966.1159821405905 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: 7bit Content-Disposition: inline I believe the compare function from standard library should be extended to allow total ordering of functional types (= closures). I suggest something like...

let old_compare = compare;;

let rec compare a b =
      let ar = Obj.repr a in
      let br = Obj.repr b in
      if Obj.tag ar = Obj.closure_tag && Obj.tag br = Obj.closure_tag then
              (* Field 0 of a closure is a pointer of the function code. Continue if pointers match *)
          let d = (-) (Obj.obj (Obj.field ar 0)) (Obj.obj (Obj.field br 0)) in
          if d <> 0 then d else
              (* now match every other field of the closures - these are the arguments to partially applied functions *)
          let rec f x = if x = Obj.size ar then 0 else
              let d = (-) (Obj.obj (Obj.field ar x)) (Obj.obj (Obj.field br x)) in
              if d <> 0 then d else f (x+1)
          in
          f 0
        (* if the two values are not closures, call the old compare. This in fact is incorrect behaviour, as old compare will fail if it meets (different) closure values. All the match cases in old compare should be included in the new compare if the behaviour is to be correct. *)
      else old_compare a b;;



This way, one could use for example association lists in order to store functional values and use List.assoc with them. The improvement over the old compare is this:

# old_compare (f 0) (f 0);;
Exception: Invalid_argument "equal: functional value".

# compare (f 0) (f 0);;
- : int = 0

- Tom
------=_Part_56330_7612966.1159821405905--