Mantis Bug Tracker

View Issue Details Jump to Notes ] Issue History ] Print ]
IDProjectCategoryView StatusDate SubmittedLast Update
0006015OCamldocumentationpublic2013-05-16 15:212016-12-07 11:47
Assigned To 
PrioritynormalSeverityminorReproducibilityhave not tried
StatusclosedResolutionwon't fix 
PlatformOSOS Version
Product Version4.00.1 
Target VersionFixed in Version 
Summary0006015: Negative integer and float literals
DescriptionI never understood this behaviour:

# sin -3.14;;
Error: This expression has type float -> float
but an expression was expected of type int

# pred -1;;
Error: This expression has type int -> int
but an expression was expected of type int

If you follow the grammar production of the docs from expr [1] to integer or float literals the above programs should be interpreted correctly. So what is wrong the lexer [4,5] or the docs ?

Either one should be fixed (and I'd prefer the lexer to be fixed).

[1] [^]
[2] [^]
[3] [^]
[4] [^]
[5] [^]
TagsNo tags attached.
Attached Files

- Relationships

-  Notes
doligez (administrator)
2013-05-17 15:08

The grammar given in the doc is ambiguous and it doesn't decide between your interpretation and the parser's. But if we take x - y to mean x (-y) instead of x - y, we essentially lose the binary - operator.

As far as literals are concerned, it would be possible to implement what you want, but that would break every program that contains (x-1) as an expression without a space between the - and the 1.

So we're not going to change the lexer/parser. If you want to suggest improvements to the doc, they will be welcome, but you should start from the trunk version because it changed a lot since 4.00.
dbuenzli (reporter)
2013-05-17 15:32

Note I'm only talking about literals here. But you are right that it doesn't seem to be reasonable to change the current behaviour. Regarding fixing the doc isn't it just about removing the optional "[-]" from the literals descriptions ?
gasche (developer)
2013-05-17 18:29

Removing "-" from literals would be incorrect, as this is what explains why [-1] is a valid integer list for example. In the current grammar, the ambiguity between minus-as-unary-operator and minus-as-binary-operator is handled through a yacc precedence: the binary operator has precedence over the unary one. This means that in all situations that are *syntactically* ambiguous (it may be that only one choice is well-typed or more generally makes sense), the binary interpretation is chosen.
doligez (administrator)
2013-07-12 15:53

Note that the doc explicitly doesn't try to be fully formal. I'm closing this as "won't fix".
romildo (reporter)
2015-04-19 17:37

At parser.mly in the implementation, the token "MINUS" has a lower precedence than the token "prec_unary_minus", and the expressions related to the prefix and infix "-" operator are "expr MINUS expr" and "subtractive expr %prec prec_unary_minus" where "subtractive" may be "MINUS". Clearly the prefix operator is given higher precedence than the infix operator. Thus the note written by gasche seems to be wrong and the negative literal is not needed as was claimed by him.

This would be enough to reopen the ticket. The solution would be to fix the documentation to remove negative numeric literals.

- Issue History
Date Modified Username Field Change
2013-05-16 15:21 dbuenzli New Issue
2013-05-17 15:08 doligez Note Added: 0009291
2013-05-17 15:08 doligez Status new => feedback
2013-05-17 15:08 doligez Category OCaml general => OCaml documentation
2013-05-17 15:32 dbuenzli Note Added: 0009292
2013-05-17 15:32 dbuenzli Status feedback => new
2013-05-17 18:29 gasche Note Added: 0009299
2013-05-17 18:29 gasche Status new => acknowledged
2013-07-12 15:53 doligez Note Added: 0009765
2013-07-12 15:53 doligez Status acknowledged => resolved
2013-07-12 15:53 doligez Resolution open => won't fix
2015-04-19 17:37 romildo Note Added: 0013688
2016-12-07 11:47 xleroy Status resolved => closed
2017-02-23 16:35 doligez Category OCaml documentation => Documentation
2017-02-23 16:44 doligez Category Documentation => documentation

Copyright © 2000 - 2011 MantisBT Group
Powered by Mantis Bugtracker