<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE message PUBLIC
  "-//MLarc//DTD MLarc output files//EN"
  "../../mlarc.dtd"[
  <!ATTLIST message
    listname CDATA #REQUIRED
    title CDATA #REQUIRED
  >
]>

  <?xml-stylesheet href="../../mlarc.xsl" type="text/xsl"?>


<message 
  url="2003/07/29d3d4246f320c31566fbe65671742e3"
  from="Chris Clearwater &lt;chris@s...&gt;"
  author="Chris Clearwater"
  date="2003-07-23T11:56:50"
  subject="[Caml-list] Functor implementation"
  prev="2003/07/3001bb0ec83622213b1bd18747888dce"
  next="2003/07/6a3ad155513fe4dcb441ceaecc207515"
  next-in-thread="2003/07/6a3ad155513fe4dcb441ceaecc207515"
  prev-thread="2003/07/7347ef3d79f516ef8692babe85c8791a"
  next-thread="2003/07/97b33d8e4a056a0adde1f92c7edafae0"
  root="../../"
  period="month"
  listname="caml-list"
  title="Archives of the Caml mailing list">

<thread subject="[Caml-list] Functor implementation">
<msg 
  url="2003/07/29d3d4246f320c31566fbe65671742e3"
  from="Chris Clearwater &lt;chris@s...&gt;"
  author="Chris Clearwater"
  date="2003-07-23T11:56:50"
  subject="[Caml-list] Functor implementation">
<msg 
  url="2003/07/6a3ad155513fe4dcb441ceaecc207515"
  from="Xavier Leroy &lt;xavier.leroy@i...&gt;"
  author="Xavier Leroy"
  date="2003-07-23T13:01:40"
  subject="Re: [Caml-list] Functor implementation">
<msg 
  url="2003/07/43df2b95e7a15bf9dc8313265ae49889"
  from="Chris Clearwater &lt;chris@s...&gt;"
  author="Chris Clearwater"
  date="2003-07-23T22:41:24"
  subject="Re: [Caml-list] Functor implementation">
<msg 
  url="2003/07/566ce50e8a50d43803c753eee1449a3c"
  from="brogoff@s..."
  author="brogoff@s..."
  date="2003-07-24T15:16:16"
  subject="Re: [Caml-list] Functor implementation">
</msg>
</msg>
</msg>
</msg>
</thread>

<contents>
Hi all, I was curious as to how ocaml implements functors and finding no help
with google, I thought I could ask on the list. After giving it a little
thought I was thinking they might be implemented as follows:

type 'a tree = Empty | Node of ('a tree) * 'a * ('a tree)
type comparison = GT | EQ | LT

let node l v r = Node(l, v, r)
let single v = Node(Empty, v, Empty)

let rec insert c t a =
    match t with
        Empty -&gt; single a
      | Node(l, v, r) -&gt; match (c a v) with
            LT -&gt; node (insert c l a) v r
          | EQ -&gt; t
          | GT -&gt; node l v (insert c r a)

let rec member c t a =
    match t with
        Empty -&gt; false
      | Node(l, v, r) -&gt; match (c a v) with
            LT -&gt; member c l a
          | EQ -&gt; true
          | GT -&gt; member c r a

(* Functor implementation follows *)

type 'a setmodule = {
    empty: 'a tree;
    insert: 'a tree -&gt; 'a -&gt; 'a tree;
    member: 'a tree -&gt; 'a -&gt; bool;
}

let make c = {
    empty = Empty;
    insert = insert c;
    member = member c;
}

(* Use a functor *)
let compare x y = let cmp = x - y in if cmp &gt; 0 then GT else if cmp &lt; 0 then LT else EQ
let mymodule = make compare
let s = mymodule.insert mymodule.empty 5

EOF

How far off is Ocaml's way and is the performance comparable? And on a
sidenote, wouldn't it be beneficial for there to be a built in comparison
type and a function to convert from integers greater than less than or
equal to, to the comparrison type? I imagine this would clarify code
that uses comparisons greatly.

Thanks in advance for your answers!

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners

</contents>

</message>

