caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Tom <tom.primozic@gmail.com>
To: caml-list <caml-list@inria.fr>
Subject: Feature proposal: improved compare
Date: Mon, 2 Oct 2006 22:36:45 +0200	[thread overview]
Message-ID: <c1490a380610021336j380f9075u5544da1698295cf@mail.gmail.com> (raw)

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

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

[-- Attachment #2: Type: text/html, Size: 2137 bytes --]

             reply	other threads:[~2006-10-02 20:36 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-10-02 20:36 Tom [this message]
2006-10-02 21:12 ` [Caml-list] " Jon Harrop
2006-10-03  8:05   ` Tom
2006-10-05 14:08     ` Tom

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=c1490a380610021336j380f9075u5544da1698295cf@mail.gmail.com \
    --to=tom.primozic@gmail.com \
    --cc=caml-list@inria.fr \
    /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).