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

using Option.attach can crash lean #6957

Open
3 tasks done
Rob23oba opened this issue Feb 5, 2025 · 1 comment · May be fixed by #6993
Open
3 tasks done

using Option.attach can crash lean #6957

Rob23oba opened this issue Feb 5, 2025 · 1 comment · May be fixed by #6993
Labels
bug Something isn't working

Comments

@Rob23oba
Copy link
Contributor

Rob23oba commented Feb 5, 2025

Prerequisites

Please put an X between the brackets as you perform the following steps:

Description

The unsafe implementation of Option.attach sometimes causes lean to instantly crash. I however suspect that this is a wider problem that would also affect List.attach and others. This is most likely a bug in the compiler where it doesn't expect such a type mismatch (even if α and { x : α // p x } have compatible representations).

Steps to Reproduce

example (x : Nat) : Option Nat := do
  let ⟨a, ha⟩ ← Option.attach (guard (x < 10))
  return 0

Impact

Add 👍 to issues you consider important. If others are impacted by this issue, please ask them to add 👍 to it.

@Rob23oba Rob23oba added the bug Something isn't working label Feb 5, 2025
@Rob23oba Rob23oba changed the title using Option.attach crashes lean using Option.attach can crash lean Feb 5, 2025
@Rob23oba
Copy link
Contributor Author

Rob23oba commented Feb 5, 2025

After a bit of debugging, the cause of the issue seems to be that lean::csimp_fn::reduce_cases_cnstr (in library/compiler/csimp.cpp) gets called with major = PUnit.unit and I_val = Subtype while it expects major to be a constructor application of I_val. A solution to this problem might be to check whether major actually is a constructor of Subtype and not something else and otherwise don't do the optimization step.
From this insight, a simpler example is:

example : Nat :=
  Subtype.casesOn (unsafeCast () : { x : Unit // x = x }) (fun a b => 0)

@Rob23oba Rob23oba linked a pull request Feb 7, 2025 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant