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

Cannot match on negative integer literals #2415

Open
mgritter opened this issue Dec 28, 2021 · 2 comments
Open

Cannot match on negative integer literals #2415

mgritter opened this issue Dec 28, 2021 · 2 comments

Comments

@mgritter
Copy link
Contributor

There doesn't seem to be a way to match on a negative integer. Here's a small reproduction:

module ExampleMatch

let sign = (z:int{z = 0 \/ z = (-1) \/ z = 1})

let example_1 (s:sign) : string =
 match s with
 | (-1) -> "negative"
 | 1 -> "positive"
 | 0 -> "zero"

There is a syntax error in the -1 case, with or without parentheses.

I initially ran into this case while trying to use an optional sign so Some (-1) has the same problem.

@mgritter
Copy link
Contributor Author

mgritter commented Dec 28, 2021

Workaround:

let example_3 (s:sign) : string =
 match s with
 | v when v = (-1) -> "negative"
 | 1 -> "positive"
 | 0 -> "zero"

I'm not sure in what contexts the "match ... when" syntax is supported. I've gotten the error "When clauses are not yet supported in --verify mode; they will be some day" when I attempted to use it previously, so it's not clear what is OK and what isn't.

For example, we can't match on a option sign in this way, that triggers the "not yet supported" error.

@aseemr
Copy link
Collaborator

aseemr commented Dec 29, 2021

It is a parser issue, negative int literals are not supported as patterns in the parser.

As a workaround, you could also use if then else:

let example_1 (s:sign) : string =
  if s = -1 then "n"
  else if s = 1 then "p"
  else begin
    assert (s == 0); //not necessary, just for illustration
    "z"
  end

let example_2 (s:option sign) : string =
  match s with
  | None -> "none"
  | Some s -> example_1 s

when clauses are not yet supported for all practical purposes. (The typechecker has an internal "lax" mode of operation that does not verify code, that's where when may be used.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants