Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

simple overloading of numerical operators #5470

Closed
vicuna opened this issue Jan 9, 2012 · 3 comments
Closed

simple overloading of numerical operators #5470

vicuna opened this issue Jan 9, 2012 · 3 comments

Comments

@vicuna
Copy link

vicuna commented Jan 9, 2012

Original bug ID: 5470
Reporter: @lefessan
Assigned to: @lefessan
Status: closed (set by @xavierleroy on 2013-08-31T10:44:23Z)
Resolution: suspended
Priority: normal
Severity: feature
Category: ~DO NOT USE (was: OCaml general)
Monitored by: tgazagna @protz @hcarty

Bug 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)

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.

File attachments

@vicuna
Copy link
Author

vicuna commented Jan 11, 2012

Comment author: thelema

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.

@vicuna
Copy link
Author

vicuna commented Jan 11, 2012

Comment author: @lefessan

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...

@vicuna
Copy link
Author

vicuna commented Jan 17, 2012

Comment author: @lefessan

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants