On Tue, Jun 24, 2008 at 11:16 AM, Richard Jones <rich@annexia.org> wrote:
> On Tue, Jun 24, 2008 at 03:30:23PM +0100, Jeremy Yallop wrote:
> On a related note I had a crazy brainwave that we could use camlp4 to
> use 'ordinary' operators in an overloaded context. Something like:
>
> INT64 (2L * n / 3L)
>
> The INT64(expr) macro would inspect the AST of expr and change + ->
> Int64.add etc. Of course one can do this using the pa_openin macro
> and a module which overrides (+) etc, but maybe this is better? I was
> going to try modifying parts of virt-df to use this syntax to see if
> it would be beneficial.
I toyed around with a simple extension to the pa_float example on the
camlp4 wiki. The modified version is here:
http://0ok.org/code/ocaml/pa_float.ml
With this, Float.(1 + 2) becomes (1.0 +. 2.0). However, if you used
Foo.(a + b), the result would be (Foo.add a b). In the Int64 case,
Int64.(1L + 2L) becomes (Int64.add 1L 2L). Same for (-) -> .sub, ( *
) -> .mul, and (/) -> .div.
This is the result of some chatting on IRC and toying around with the
pa_float example, so I don't know how write or wrong this
implementation is.
Another approach I tried after further IRC discussion (thanks to
bluestorm and David Teller!) is a twist on pa_openin:
http://0ok.org/code/ocaml/pa_context.ml
In this case, Foo.(1 + 2) would use the operator Foo.(+) rather than a
pre-defined transform of (+) -> Foo.add. This might work well if
Num-like ( +/ ) operators were added to modules, since it would allow
something along the lines of:
Int64.(1L +/ some_int64_value +/ of_int 2 -/ of_float some_float_value)
to work properly. (+/) may not be very pretty, but it doesn't
conflict with (+) or (+.). David Teller may be doing something like
this with the Batteries project.
I think Zheng Li released an extension similar to pa_context, but I am
not sure how similar or different it is in function.
Sadly camlp4 3.10+ does not play well with the toplevel, so these are
difficult to work with interactively. I do use pa_context in several
places though, and it seems to work well.
Hez
--
Hezekiah M. Carty
Graduate Research Assistant
University of Maryland
Department of Atmospheric and Oceanic Science