<?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/6a3ad155513fe4dcb441ceaecc207515"
  from="Xavier Leroy &lt;xavier.leroy@i...&gt;"
  author="Xavier Leroy"
  date="2003-07-23T13:01:40"
  subject="Re: [Caml-list] Functor implementation"
  prev="2003/07/29d3d4246f320c31566fbe65671742e3"
  next="2003/07/97b33d8e4a056a0adde1f92c7edafae0"
  prev-in-thread="2003/07/29d3d4246f320c31566fbe65671742e3"
  next-in-thread="2003/07/43df2b95e7a15bf9dc8313265ae49889"
  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>
&gt; Hi all, I was curious as to how ocaml implements functors and
&gt; finding no help with google, I thought I could ask on the
&gt; list.

Basically, a structure is compiled like a record, and a functor like a
function.

&gt; After giving it a little thought I was thinking they might be
&gt; implemented as follows:
&gt; [...]
&gt; How far off is Ocaml's way and is the performance comparable?

It's pretty close, and the performance is comparable.
Here is pseudocode that is closest to the actual compiled code:

 type 'a orderedtype = {
     compare: 'a -&gt; 'a -&gt; comparison
 }

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

 let make compare =

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

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

  (* other struct members similar *)

  {
     empty = Empty;
     insert = insert;
     member = member
  }

  let mymodule =
    make { compare = fun x y -&gt; ... }


Now, let me warn you about the following code:

&gt; let compare x y =
    let cmp = x - y in if cmp &gt; 0 then GT else if cmp &lt; 0 then LT else EQ

which doesn't work if x and y are sufficiently far apart
(e.g. x = max_int and y = min_int).

- Xavier Leroy

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

