lean4-htt/tests/elab/sym_discrtree_eta.lean
Leonardo de Moura 7120d9aef5
fix: eta-reduce expressions in sym discrimination tree lookup (#12920)
This PR adds eta reduction to the sym discrimination tree lookup
functions (`getMatch`, `getMatchWithExtra`, `getMatchLoop`). Without
this, expressions like `StateM Nat` that unfold to eta-expanded forms
`(fun α => StateT Nat Id α)` fail to match discrimination tree entries
for the eta-reduced form `(StateT Nat Id)`.

Also optimizes `etaReduce` with an early exit for non-lambda expressions
and removes a redundant `n == 0` check.
Includes a test verifying that `P (StateM Nat)` matches a disc tree
entry for `P (StateT Nat Id)`.

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-14 16:57:10 +00:00

20 lines
663 B
Text
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import Lean.Meta.Sym
open Lean Meta Sym
opaque P : (Type → Type) → Prop
axiom stateT_P : P (StateT Nat Id)
/--
info: disc tree lookup match count: 1
-/
#guard_msgs in
#eval show MetaM Unit from do
-- `StateM Nat` unfolds to `fun α => StateT Nat Id α` (an eta redex)
let e ← Sym.unfoldReducible (mkApp (mkConst ``P) (mkApp (mkConst ``StateM [0]) (mkConst ``Nat)))
-- Verify the eta redex is present
assert! e.appArg!.isLambda
let pat ← mkPatternFromDecl ``stateT_P
let dt := Sym.insertPattern (Lean.Meta.DiscrTree.empty (α := Unit)) pat ()
let nMatches := (Sym.getMatch dt e).size
logInfo m!"disc tree lookup match count: {nMatches}"