Mantis Bug Tracker

View Issue Details Jump to Notes ] Issue History ] Print ]
IDProjectCategoryView StatusDate SubmittedLast Update
0005470OCamlOCaml generalpublic2012-01-09 14:472013-08-31 12:44
Reporterlefessan 
Assigned Tolefessan 
PrioritynormalSeverityfeatureReproducibilityalways
StatusclosedResolutionsuspended 
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 try.ocamlpro.com, 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
(0006647)
thelema (reporter)
2012-01-11 22:05

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.
(0006651)
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...
(0006708)
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


Copyright © 2000 - 2011 MantisBT Group
Powered by Mantis Bugtracker