|Anonymous | Login | Signup for a new account||2014-09-02 01:31 CEST|
|Main | My View | View Issues | Change Log | Roadmap|
|View Issue Details|
|ID||Project||Category||View Status||Date Submitted||Last Update|
|0005470||OCaml||OCaml general||public||2012-01-09 14:47||2013-08-31 12:44|
|Target Version||Fixed in Version|
|Summary||0005470: simple overloading of numerical operators|
|Description||It is possible to extend OCaml to accept local overloading of numerical operators by intercepting constants in the parser. The attached patch does the following:|
For each numerical constant, the parser wrap the constant with a call to a special function. For example, 3 becomes (int_of_int 3), 3. becomes (float_of_float 3.), etc. where int_of_int, float_of_float, etc. are defined in Pervasives as just the identity.
This can later be used to overload operators in a local context, by translating constants in a general "num" type. For example, as an example, on try.ocamlpro.com, a "N" module has been defined to use this extension, so you can type:
N.( 1 + 3. / 2)
N.t = Float 2.500000
Of course, there are still problems with the representation of the N.t type (for example, the polymorphic comparisons won't work on it between floats and ints, so that for example N.(1 > 1.) is currently true :-( ).
Note that I tried to do the same for strings, but it cannot work, because the function added by the parser forces the type of string constants to string, which is incompatible with the fact that the typer may expect a "format" instead.
|Tags||No tags attached.|
|Attached Files||simple-overload.patch [^] (5,176 bytes) 2012-01-09 14:47 [Show Content]|
The new C standard has a related feature - user-defined literals
http://www2.research.att.com/~bs/C++0xFAQ.html#UD-literals [^] - something similar may be doable in OCaml.
|Interesting, that's indeed very close, and better. I tried playing with my patch with Big_ints, and there is no way to enter a big_int in OCaml easily, because ints have to representable at lexing time. With literal operators, ints would be parsed at runtime time (well, at compile time for normal ints), so it would be possible to use it for Big_ints. I have to think about how this can be done while being sure not to lose efficiency on standard literals...|
|I will provide more feedback later when this extension will be used in a real library.|
|2012-01-09 14:47||lefessan||New Issue|
|2012-01-09 14:47||lefessan||File Added: simple-overload.patch|
|2012-01-11 22:05||thelema||Note Added: 0006647|
|2012-01-12 00:50||lefessan||Note Added: 0006651|
|2012-01-17 23:01||lefessan||Note Added: 0006708|
|2012-01-17 23:01||lefessan||Status||new => resolved|
|2012-01-17 23:01||lefessan||Resolution||open => suspended|
|2012-01-17 23:01||lefessan||Assigned To||=> lefessan|
|2013-08-31 12:44||xleroy||Status||resolved => closed|
|Copyright © 2000 - 2011 MantisBT Group|