Mantis Bug Tracker

View Issue Details Jump to Notes ] Issue History ] Print ]
IDProjectCategoryView StatusDate SubmittedLast Update
0005837OCamlCamlp4public2012-11-28 01:412014-05-25 20:20
Assigned To 
PlatformOSOS Version
Product Version4.00.1 
Target Version4.02.0+devFixed in Version 
Summary0005837: Strange camlp4 rewrite
Descriptioncamlp4 rewrites 'let r = ref run () -> ()' in a strange way:

$ camlp4o -printer Camlp4OCamlPrinter -str 'let r = ref fun () -> ()'
let r = ref

let _ = fun () -> ()
Steps To Reproduce$ echo 'let r = ref fun () -> ()' >
$ ocamlc -i
File "", line 1, characters 12-15:
Error: Syntax error
$ ocamlc -pp camlp4o -i
val r : 'a -> 'a ref
TagsNo tags attached.
Attached Files

- Relationships

-  Notes
hongboz (developer)
2012-11-28 02:30

this is syntax ambiguity here
let r = ref ;;
let _ = fun () -> () ;;
let r = ref (fun () -> ()) ;;

since ";;" is optional
elnatan (reporter)
2012-11-28 15:36

Your second alternative is *still* ambiguous. You need extra parentheses to remove the ambiguity:

let r = (ref (fun () -> ()));;

In any case, it seems clear that either camlp4 or ocamlc should change to match the other one.
gasche (developer)
2012-12-03 11:09
edited on: 2012-12-03 11:11

OCaml and Camlp4 both reject "ref fun () -> ()" as an expression, despite it being correct according to the hand-wavy context-free grammar of the manual. The problem is that Camlp4 is then clever enough to turn the ambiguity on its head and parse something different, while OCaml's different parsing technology plainly rejects this case.

It's hard to say that Camlp4 is "wrong" in this case, as its resulting parsing is valid as per the grammar rules, and it cannot be faulted for parsing more input than OCaml. However, the parse we really want, if any, would rather be "let r = (ref fun () -> ())", or a rejection of the ambiguity.

I think this is a case where Camlp4 could be changed to mirror OCaml's behavior. However, changing Camlp4's parser is subtle and can have unintented consequences. My own guess it that is a case of "too hard and too risky to fix", and I'm preparing to close the bug as "won't fix" in a medium-term future if nobody acts.
Hongbo or Jérémie, if you disagree with my assessment of the situation or know how to fix this properly, please feel free to take this one!

PS: while a "fun ..." is rejected on the right of an application, it is accepted as a second argument of an infix operator by both OCaml and Camlp4. That is very convenient for monadic-style syntax: "foo >>= fun x -> ..." doesn't need a terminal delimiter, and therefore allows the same style as "let .. in .. ".

- Issue History
Date Modified Username Field Change
2012-11-28 01:41 elnatan New Issue
2012-11-28 02:30 hongboz Note Added: 0008542
2012-11-28 15:36 elnatan Note Added: 0008543
2012-12-03 11:09 gasche Note Added: 0008555
2012-12-03 11:09 gasche Status new => acknowledged
2012-12-03 11:11 gasche Note Edited: 0008555 View Revisions
2013-07-11 20:52 doligez Target Version => 4.02.0+dev
2013-07-12 18:15 doligez Target Version 4.02.0+dev => 4.01.1+dev
2014-05-25 20:20 doligez Target Version 4.01.1+dev => 4.02.0+dev

Copyright © 2000 - 2011 MantisBT Group
Powered by Mantis Bugtracker