Skip to content

Commit

Permalink
fix: FunInd: avoid over-eta-expanding in preprocessing step (#5619)
Browse files Browse the repository at this point in the history
fixes #5602
  • Loading branch information
nomeata authored Oct 7, 2024
1 parent f1ff9ce commit 3e75d8f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
18 changes: 15 additions & 3 deletions src/Lean/Meta/Tactic/FunInd.lean
Original file line number Diff line number Diff line change
Expand Up @@ -662,8 +662,16 @@ def deriveUnaryInduction (name : Name) : MetaM Name := do
let varNames ← forallTelescope info.type fun xs _ => xs.mapM (·.fvarId!.getUserName)

-- Uses of WellFounded.fix can be partially applied. Here we eta-expand the body
-- to avoid dealing with this
let e ← lambdaTelescope info.value fun params body => do mkLambdaFVars params (← etaExpand body)
-- to make sure that `target` indeed the last parameter
let e := info.value
let e ← lambdaTelescope e fun params body => do
if body.isAppOfArity ``WellFounded.fix 5 then
forallBoundedTelescope (← inferType body) (some 1) fun xs _ => do
unless xs.size = 1 do
throwError "functional induction: Failed to eta-expand{indentExpr e}"
mkLambdaFVars (params ++ xs) (mkAppN body xs)
else
pure e
let e' ← lambdaTelescope e fun params funBody => MatcherApp.withUserNames params varNames do
match_expr funBody with
| [email protected] α _motive rel wf body target =>
Expand Down Expand Up @@ -710,7 +718,11 @@ def deriveUnaryInduction (name : Name) : MetaM Name := do
-- So for now lets just keep them around.
let e' ← mkLambdaFVars (binderInfoForMVars := .default) fixedParams e'
instantiateMVars e'
| _ => throwError "Function {name} not defined via WellFounded.fix:{indentExpr funBody}"
| _ =>
if funBody.isAppOf ``WellFounded.fix then
throwError "Function {name} defined via WellFounded.fix with unexpected arity {funBody.getAppNumArgs}:{indentExpr funBody}"
else
throwError "Function {name} not defined via WellFounded.fix:{indentExpr funBody}"

unless (← isTypeCorrect e') do
logError m!"failed to derive a type-correct induction principle:{indentExpr e'}"
Expand Down
22 changes: 22 additions & 0 deletions tests/lean/run/issue5602.lean
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
abbrev Word := List Nat
abbrev Alphabet := Nat

inductive Regex where
| none: Regex

axiom inter: Regex → (Word → Prop)
axiom concatenate (a b: Word → Prop) : (Word → Prop)
axiom eps: Word → Prop

def conc (a: Array Regex) (i: Nat): Word → Prop :=
if h: i < a.size then
concatenate (inter a[i]) (conc a (i + 1))
else
eps

/--
info: conc.induct (a : Array Regex) (motive : Nat → Prop) (case1 : ∀ (x : Nat), x < a.size → motive (x + 1) → motive x)
(case2 : ∀ (x : Nat), ¬x < a.size → motive x) (i : Nat) : motive i
-/
#guard_msgs in
#check conc.induct

0 comments on commit 3e75d8f

Please sign in to comment.