You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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...
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
The text was updated successfully, but these errors were encountered: