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

parsing bug on function types in variant type definitions #11445

Closed
gasche opened this issue Jul 19, 2022 · 9 comments
Closed

parsing bug on function types in variant type definitions #11445

gasche opened this issue Jul 19, 2022 · 9 comments
Labels
syntax-error-messages Error messages for syntax errors

Comments

@gasche
Copy link
Member

gasche commented Jul 19, 2022

The following is rejected as a parse error, but it should be accepted:

type t = Foo of int -> int
@xavierleroy
Copy link
Contributor

Accepting this (without parentheses around the function type) makes the following example ambiguous:

type t = Foo of int -> int * int

Is this Foo of (int -> int) * int (two arguments) or Foo of (int -> (int * int)) (one argument) ?

@damiendoligez
Copy link
Member

So we need to change the grammar in the documentation because the current version has this ambiguity.

@gasche
Copy link
Member Author

gasche commented Jul 19, 2022

My expectation would be:

  1. parse the right-hand-side of of as a type expression
  2. split the toplevel tuple type (if any) into several arguments

This gives a justification for the interpretation (int -> int * int) in your example.

This being said, in the case of int -> int * int it makes sense to fail instead of pick a choice, forcing users to be explicit. The failure is rather confusing on examples that do not contain any tuples. But of course there is no easy way to write simple parsing rules that accept arrow types without tuple types below (except by writing yet another specialized syntactic category for this). So maybe one sensible long-term solution would be to keep failing with an error here, but hopefully with an error message that explains the solution. (I hear that @let-def work on parsing error messages for OCaml has made progress recently, so one can hope.)

let-def added a commit to let-def/lrgrep that referenced this issue Jul 20, 2022
@let-def
Copy link
Contributor

let-def commented Jul 20, 2022

I added your example as a test case.

However accepting this input might be nice. There are two difficulties I can think of:

  • when parsing the RHS as a type expression, we still need to distinguish Foo of a * b from Foo of (a * b) (not splitting a tuple that was nested on purpose). Even this simple solution needs subtle changes to the grammar
  • for generalized constructors, the -> has another meaning. So the grammar of arguments after of and : will differ again in subtle ways.

@craff
Copy link
Contributor

craff commented Jul 20, 2022

Why not declare the "of" syntax deprecated to keep only the more general syntax of GADT in the future ? OK it is a bit annoying to have to write the result type but a notation like

type a_long_name_to_make_sure_we_know_what_it_means = Foo : (int -> int * int) -> _

could solve that problem for type which have no parameters but have a long name.
I think any simplification of OCaml is welcome ?

@gasche
Copy link
Member Author

gasche commented Jul 20, 2022

No, we don't deprecate simpler, perfectly-fine syntax used all over the place (programs, docs, textbook, wikipedia, whatever) just to work around a parsing ambiguity. The cost of those deprecations is huge and in this case the benfit is razor thin.

@craff
Copy link
Contributor

craff commented Jul 21, 2022 via email

@github-actions
Copy link

This issue has been open one year with no activity. Consequently, it is being marked with the "stale" label. What this means is that the issue will be automatically closed in 30 days unless more comments are added or the "stale" label is removed. Comments that provide new information on the issue are especially welcome: is it still reproducible? did it appear in other contexts? how critical is it? etc.

@github-actions github-actions bot added the Stale label Jul 24, 2023
@gasche gasche added syntax-error-messages Error messages for syntax errors and removed Stale labels Jul 24, 2023
@gasche
Copy link
Member Author

gasche commented Jul 24, 2023

I don't want to receive further notifications on this issue before we integrate @let-def's work on the parser. In the meantime I propose to close, after tagging with syntax-error-messages (which is morphing into "stuff that would benefit from Fred's work").

@gasche gasche closed this as not planned Won't fix, can't repro, duplicate, stale Jul 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
syntax-error-messages Error messages for syntax errors
Projects
None yet
Development

No branches or pull requests

5 participants