fix: keep lambdas in the Sym.simp grind normalizer This PR fixes `grind` failures with `backward.grind.normalizer := false` when a function is equated to a lambda such as `fun x y => x + y`. The `Sym.simp` normalizer no longer eta-reduces such lambdas into partial applications. It still eta-reduces a lambda whose result is a constant or a variable, such as `fun x => Nat.succ x`.
diff --git a/src/Lean/Meta/Tactic/Grind/NormSymProcs.lean b/src/Lean/Meta/Tactic/Grind/NormSymProcs.lean index 9f5caef..942dd17 100644 --- a/src/Lean/Meta/Tactic/Grind/NormSymProcs.lean +++ b/src/Lean/Meta/Tactic/Grind/NormSymProcs.lean
@@ -48,8 +48,12 @@ /-- Eta-reduces the lambda arguments of an application before they are visited. Rewriting a body first would hide the redex: `fun x => Nat.succ x` would become `fun x => x + 1` and no longer be -the `Nat.succ` found elsewhere in the goal. `grind` eta-reduces the terms it internalizes anyway -(`Sym.Canon`). The body of a binder notation (`∃ y, q y`, `{x // p x}`) keeps its binder. +the `Nat.succ` found elsewhere in the goal. The body of a binder notation (`∃ y, q y`, +`{x // p x}`) keeps its binder. + +Only a lambda that eta-reduces to a constant or a free variable is reduced. `grind` propagates +`f a = b[x := a]` for `f = fun x => b` only when the class of `f` contains a lambda (`Grind.Beta`), +so `fun x y => x + y` must not become the partial application `HAdd.hAdd`. -/ def etaReduce : Simproc := fun e => do unless e.isApp do return .rfl @@ -59,11 +63,14 @@ let e' ← go e return .step e' (← Sym.mkEqRefl e') where + reduceArg (a : Expr) : Expr := + let a' := Sym.etaReduce a + if a'.isConst || a'.isFVar then a' else a hasEtaReducibleArg : Expr → Bool - | .app f a => Sym.isEtaReducible a || hasEtaReducibleArg f + | .app f a => !isSameExpr a (reduceArg a) || hasEtaReducibleArg f | _ => false go : Expr → SimpM Expr - | .app f a => do mkAppS (← go f) (Sym.etaReduce a) + | .app f a => do mkAppS (← go f) (reduceArg a) | f => pure f /--
diff --git a/tests/elab/grind_sym_eta_lambda.lean b/tests/elab/grind_sym_eta_lambda.lean new file mode 100644 index 0000000..0df1e4c --- /dev/null +++ b/tests/elab/grind_sym_eta_lambda.lean
@@ -0,0 +1,24 @@ +/-! +The `Sym.simp`-based `grind` normalizer must not eta-reduce `fun x y => x + y` to the partial +application `HAdd.hAdd`: `grind` instantiates `f a = b[x := a]` from `f = fun x => b` only when +the equivalence class of `f` contains a lambda. +-/ +set_option backward.grind.normalizer false +set_option warn.sorry false + +example (f : Nat → Nat → Nat) : f 2 3 ≠ 5 → f = (fun x y : Nat => x + y) → False := by + grind + +def g (x : Nat) : Nat → Nat → Nat := + match x with + | 0 => fun _ _ => 0 + | _+1 => fun a b => a + b + +example : g (a+1) b c = b + c := by + grind [g] + +example (p : (Nat → Nat → Nat) → Prop) : p (fun x y => x + y) := by + grind_norm sym; guard_target =ₛ p (fun x y => x + y); sorry + +example (p : (Nat → Nat) → Prop) (h : Nat → Nat → Nat) (a : Nat) : p (fun x => h a x) := by + grind_norm sym; guard_target =ₛ p (fun x => h a x); sorry