Commits

camlspotter committed 7546525

update

Comments (0)

Files changed (1)

       external (+) : 'a -> 'a -> 'a = "OVERLOADDED"
     end
 
-Here we declare ``Loaded.(+)`` to be a polymorphic function whose implementation is by C function named ``OVERLODED``. But we do not give any C code. The name ``OVERLOADED`` is just a marking for our overloading. Very luckily, we can have such a fake polymorphic value in OCaml as far as such a value is never used.
+Here we declare ``Loaded.(+)`` to be a polymorphic function whose implementation is by C function named ``OVERLODED``. But we do not give any C code. The name ``OVERLOADED`` is just a mark for our overloading. Very luckily, we can have such a fake polymorphic value in OCaml as far as such a value is never actually used.
 
-In this ``Loaded`` module we stack sub-modules which provide overloaded instances for this ``(+)``:: 
+In this ``Loaded`` module, we stack sub-modules which provide overloaded instances for this ``(+)``:: 
 
     module Loaded = struct
       external (+) : 'a -> 'a -> 'a = "OVERLOADDED"
     open Loaded
     let _ = 
       assert (1 + 2 = 3);
-      assert (1.2 + 3.4 = 4.6)
+      assert (1.2 + 3.4 = 4.6) (* See it is not +. but + !!! *)
 
 Hey, I used ``Loaded.(+)``, which is actually a C primitive without C code! Is it ok? It is NOT, without our compiler mod. The mod must replace the use of ``Loaded.(+)`` by ``Loaded.Int.(+)`` or ``Loaded.Float.(+)`` appropriately depending on its type from the context: the first ``+`` is ``int -> int -> int`` and the second is ``float -> float -> float``::
 
 
 Here is (some part of) the code of the mod. It is a function of ``Typedtree.structure -> Typedtree.structure``, but we are only interested in the uses of identifiers whose definitions are by primitives ``OVERLOADED``. So the boilerplate code to dig into the AST data types I used a generic map class ``Ttmap`` created by a CamlP4 hack. For each identifier whose definition is ``OVERLOADED`` is converted by the function ``resolve_overloading`` function.
 
-The actual overload resolution is quite simple, if you know the internals of OCaml type-checker. But if you don't, it is just too painful to read. So skipped (see ``mod.ml`` if you are really interested). The big picture is: traverse the module which defines the primitive to find the values with the same name, then filter out those which do not match the context type. If there is none matched, error. If there are more than one matches, error. If there is only one candidate, replace the primitive use by the candidate variable.
+The actual overload resolution is quite simple, if you know the internals of OCaml type-checker. But if you don't, it is just too painful to read. So it is skipped :^) (see ``mod.ml`` if you are really interested). The big picture is: traverse the module which defines the primitive to find the values with the same name, then filter out those which do not match the context type. If there is none left, error. If there are more than one matches, error. If there is only one candidate, replace the primitive use by the candidate variable.
 
 Anyway, building and playing this mod is very easy::
 
     $ cd overlaod
     $ make
 
-It creates a bytecode compiler ``poorman``. Well, compared to the full overloading by type classes, this is very simple, a poorman's solution. We have a test code at ``test/test.ml`` so you can try compiling it by ``poorman``::
+It creates a bytecode compiler ``poorman``. Well, compared to the full overloading by type classes, this is very simple, a poorman's overloading solution. We have a test code at ``test/test.ml`` so you can try compiling it by ``poorman``::
 
     $ ./poorman -o test/test test/test.ml
     $ ./test/test  # Well, it just tests some assertions
 ===============================
 
 This kind of compiler modifications are of course possible even in the previous versions of OCaml compilers, but their distributions had to be as patches against the original compilers, and the users need to recompile the whole compiler sets, which took about 10 minutes. But now, with compiler-libs, it is less than one minute. Compiler-libs are not just for strange compiler mods, but also good for compiler related tool development. It is really encouraging for us, OCaml mutators, since we can deliver our compiler prototypes very easily to end users!
-