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

fix: PANIC at Lean.MVarId.falseOrByContra #5157

Merged
merged 1 commit into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 13 additions & 10 deletions src/Lean/Elab/Tactic/FalseOrByContra.lean
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,16 @@ open Lean Meta Elab Tactic
-- but fall back to a classical instance. When it is `some true`, we always use the classical instance.
-- When it is `some false`, if there is no `Decidable` instance we don't introduce the double negation,
-- and fall back to `False.elim`.
partial def falseOrByContra (g : MVarId) (useClassical : Option Bool := none) : MetaM MVarId := do
partial def falseOrByContra (g : MVarId) (useClassical : Option Bool := none) : MetaM (Option MVarId) := do
let ty ← whnfR (← g.getType)
match ty with
| .const ``False _ => pure g
| .forallE _ _ _ _
| .const ``False _ => return g
| .forallE ..
| .app (.const ``Not _) _ =>
-- We set the transparency back to default; otherwise this breaks when run by a `simp` discharger.
falseOrByContra (← withTransparency default g.intro1P).2 useClassical
| _ =>
let gs ← if ← isProp ty then
let gs ← if (← isProp ty) then
match useClassical with
| some true => some <$> g.applyConst ``Classical.byContradiction
| some false =>
Expand All @@ -51,12 +51,15 @@ partial def falseOrByContra (g : MVarId) (useClassical : Option Bool := none) :
catch _ => some <$> g.applyConst ``Classical.byContradiction
else
pure none
if let some gs := gs then
let [g] := gs | panic! "expected one subgoal"
pure (← g.intro1).2
else
let [g] ← g.applyConst ``False.elim | panic! "expected one sugoal"
pure g
match gs with
| some [] => return none
| some [g] => return some (← g.intro1).2
| some _ => panic! "expected at most one sugoal"
Copy link
Contributor

@eric-wieser eric-wieser Aug 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
| some _ => panic! "expected at most one sugoal"
| some _ => panic! "expected at most one subgoal"

| none =>
match (← g.applyConst ``False.elim) with
| [] => return none
| [g] => return some g
| _ => panic! "expected at most one sugoal"

@[builtin_tactic Lean.Parser.Tactic.falseOrByContra]
def elabFalseOrByContra : Tactic
Expand Down
2 changes: 1 addition & 1 deletion src/Lean/Elab/Tactic/Omega/Frontend.lean
Original file line number Diff line number Diff line change
Expand Up @@ -682,7 +682,7 @@ open Lean Elab Tactic Parser.Tactic
/-- The `omega` tactic, for resolving integer and natural linear arithmetic problems. -/
def omegaTactic (cfg : OmegaConfig) : TacticM Unit := do
liftMetaFinishingTactic fun g => do
let g ← g.falseOrByContra
let some g ← g.falseOrByContra | return ()
g.withContext do
let hyps := (← getLocalHyps).toList
trace[omega] "analyzing {hyps.length} hypotheses:\n{← hyps.mapM inferType}"
Expand Down
3 changes: 3 additions & 0 deletions tests/lean/run/4985.lean
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
example : id (False → Nat) := by false_or_by_contra

example : id ((¬True → False) → True) := by false_or_by_contra
Loading