Mantis Bug Tracker

View Issue Details Jump to Notes ] Issue History ] Print ]
IDProjectCategoryView StatusDate SubmittedLast Update
0005470OCaml~DO NOT USE (was: OCaml general)public2012-01-09 14:472013-08-31 12:44
Assigned Tolefessan 
PlatformOSOS Version
Product Version 
Target VersionFixed in Version 
Summary0005470: simple overloading of numerical operators
DescriptionIt 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, a "N" module has been defined to use this extension, so you can type:

N.( 1 + 3. / 2)

and get

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.
TagsNo tags attached.
Attached Filespatch file icon simple-overload.patch [^] (5,176 bytes) 2012-01-09 14:47 [Show Content]

- Relationships

-  Notes
thelema (reporter)
2012-01-11 22:05

The new C standard has a related feature - user-defined literals [^] - something similar may be doable in OCaml.
lefessan (developer)
2012-01-12 00:50

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...
lefessan (developer)
2012-01-17 23:01

I will provide more feedback later when this extension will be used in a real library.

- Issue History
Date Modified Username Field Change
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
2017-02-23 16:36 doligez Category OCaml general => -OCaml general
2017-03-03 17:55 doligez Category -OCaml general => -(deprecated) general
2017-03-03 18:01 doligez Category -(deprecated) general => ~deprecated (was: OCaml general)
2017-03-06 17:04 doligez Category ~deprecated (was: OCaml general) => ~DO NOT USE (was: OCaml general)

Copyright © 2000 - 2011 MantisBT Group
Powered by Mantis Bugtracker