Mantis Bug Tracker

View Issue Details Jump to Notes ] Issue History ] Print ]
IDProjectCategoryView StatusDate SubmittedLast Update
0005591OCamlOCaml standard librarypublic2012-04-15 07:592012-04-16 10:50
Reporterrbonichon 
Assigned Togasche 
PrioritynormalSeverityminorReproducibilityalways
StatusresolvedResolutionno change required 
PlatformOSOS Version
Product Version3.12.1 
Target VersionFixed in Version 
Summary0005591: Mismatch between Pervasives.format_of_string's type signature and its documented behavior
DescriptionThe type signature of this function is documented as

format_of_string : ('a, 'b, 'c, 'd, 'e, 'f) format6 ->
                      ('a, 'b, 'c, 'd, 'e, 'f) format6

though its behavior is documented -- as we expect -- as
"format_of_string s returns a format string read from the string literal s."

Further note
============

There are three functions (seemingly) related to the conversion of string/format in pervasives.ml:
* format_of_string (exported but with the mismatched documentation/signature above)
* format_to_string (exported)
* string_to_format (not exported)

string_to_format is used in the module (not format_of_string) for conversion and seems to be redundant with format_of_string.


Additional InformationThe fix might be as simple as replacing:

external format_of_string :
 ('a, 'b, 'c,'d, 'e, 'f) format6 ->
 ('a, 'b, 'c, 'd, 'e, 'f) format6 = "%identity"

by

external format_of_string :
 string ->
 ('a, 'b, 'c, 'd, 'e, 'f) format6 = "%identity"

Or exporting the function "string_to_format" from pervasives.ml in its stand.
TagsNo tags attached.
Attached Files

- Relationships

-  Notes
(0007355)
gasche (developer)
2012-04-15 08:16
edited on: 2012-04-15 08:25

The whole printf/scanf magic is based on on the fact that the typer can analyse string literal as format string and extract rich format typing information out of them. Of course this can only work with *constant* strings (hence the restriction to string literals): format_of_string (read_line ()) would of course not be typeable.

This explains the type of format_of_string: it only has the effect of a type annotation to force the type-checker to use its "format hack" on the string literal, even if it wasn't clear from the context that it will indeed be used as a format. And it's much more convenient than explicitly coercing to (blah, blah, blah...) format6.

Consider the two toplevel sessions:

# let s = "%s";;
val s : string = "%s"
# Printf.printf s;;
Error: This expression has type string but an expression was expected of type
         ('a, out_channel, unit) format =
           ('a, out_channel, unit, unit, unit, unit) format6

# let s = format_of_string "%s";;
val s : (string -> '_a, '_b, '_c, '_d, '_d, '_a) format6 = <abstr>
# Printf.printf s;;
- : string -> unit = <fun>

(string_to_format : string -> ... format) is a type-unsafe coercion that is only used inside the standard library in provably safe places (namely the implementation of "%,"). Exposing it to the users would give them yet another way to shoot them in the foot (including segfaults) for dubious benefit -- if you want to do subtle type-level format handling, you should use an equally-expressive type-safe printing combinator library anyway.

To conclude, I am under the impression that your bug report is not a bug (even an documentation bug), but the result of a mismatch between your expectations and how formats are handled by the compiler. As the misunderstanding could equally likely be on my side, I mark this bug as "feedback": can you confirm that your suggested fix is not correct for the reasons above?

(0007356)
rbonichon (reporter)
2012-04-15 10:09

You're right, your impression is correct and I alsostand corrected. This is not a bug. Do close it!
(0007357)
glondu (reporter)
2012-04-16 10:50

For the record, there is Scanf.format_from_string that converts a string to a format6 in a type-safe way.

- Issue History
Date Modified Username Field Change
2012-04-15 07:59 rbonichon New Issue
2012-04-15 08:16 gasche Note Added: 0007355
2012-04-15 08:16 gasche Status new => feedback
2012-04-15 08:17 gasche Assigned To => gasche
2012-04-15 08:17 gasche Status feedback => assigned
2012-04-15 08:17 gasche Status assigned => feedback
2012-04-15 08:25 gasche Note Edited: 0007355 View Revisions
2012-04-15 10:09 rbonichon Note Added: 0007356
2012-04-15 10:09 rbonichon Status feedback => assigned
2012-04-15 10:37 gasche Status assigned => resolved
2012-04-15 10:37 gasche Resolution open => no change required
2012-04-16 10:50 glondu Note Added: 0007357


Copyright © 2000 - 2011 MantisBT Group
Powered by Mantis Bugtracker