Skip to content

Conversation

@kim-em
Copy link
Collaborator

@kim-em kim-em commented Dec 3, 2025

This PR re-enables star-indexed lemmas as a fallback for exact? and apply?.

Star-indexed lemmas (those with overly-general discrimination tree keys like [*])
were previously dropped entirely for performance reasons. This caused useful lemmas
like Empty.elim, And.left, not_not.mp, Sum.elim, and Function.mtr to be
unfindable by library search.

The implementation adds a two-pass search strategy:

  1. First, search using concrete discrimination keys (the current behavior)
  2. If no results are found, fall back to trying star-indexed lemmas

The star-indexed lemmas are extracted during tree initialization and cached in an
environment extension, avoiding repeated computation.

Users can disable the fallback with -star:

example {α : Sort u} (h : Empty) : α := by apply? -star  -- error: no lemmas found
example {α : Sort u} (h : Empty) : α := by apply?        -- finds Empty.elim

🤖 Prepared with Claude Code

@kim-em kim-em changed the title feat: exact? falls back to trying un-indexable theorems feat: exact? uses star-indexed lemmas as fallback Dec 3, 2025
@github-actions github-actions bot added the toolchain-available A toolchain is available for this PR, at leanprover/lean4-pr-releases:pr-release-NNNN label Dec 3, 2025
@leanprover-community-bot
Copy link
Collaborator

leanprover-community-bot commented Dec 3, 2025

Mathlib CI status (docs):

  • ❗ Batteries/Mathlib CI will not be attempted unless your PR branches off the nightly-with-mathlib branch. Try git rebase 54fbe931ab77351ccba28aa7c03f22c717ba0681 --onto 5bd331e85d9d110a29fb3367dbb21854010ffcbd. You can force Mathlib CI using the force-mathlib-ci label. (2025-12-03 13:51:58)
  • ✅ Mathlib branch lean-pr-testing-11494 has successfully built against this PR. (2025-12-04 23:01:58) View Log

@leanprover-bot
Copy link
Collaborator

leanprover-bot commented Dec 3, 2025

Reference manual CI status:

  • ❗ Reference manual CI will not be attempted unless your PR branches off the nightly-with-manual branch. Try git rebase 54fbe931ab77351ccba28aa7c03f22c717ba0681 --onto d3dda9f6d4428a906c096067ecb75e432afc4615. You can force reference manual CI using the force-manual-ci label. (2025-12-03 13:52:00)
  • ❗ Reference manual CI can not be attempted yet, as the nightly-testing-2025-12-03 tag does not exist there yet. We will retry when you push more commits. If you rebase your branch onto nightly-with-manual, reference manual CI should run now. You can force reference manual CI using the force-manual-ci label. (2025-12-04 06:42:32)

@kim-em
Copy link
Collaborator Author

kim-em commented Dec 4, 2025

I would like to rebase this onto nightly-with-mathlib and test in Mathlib, but I need Mathlib to catch up first.

kim-em and others added 5 commits December 4, 2025 05:32
This PR adds support for star-indexed lemmas (like `Empty.elim`, `And.left`, `not_not.mp`)
to `exact?` and `apply?` as a fallback search when no concrete-keyed lemmas are found.

Star-indexed lemmas are those whose discrimination tree keys are `[*]` or patterns like
`[Eq,*,*,*]` - these match too broadly to include in the primary search, but can be
helpful when the goal type is headed by a free variable (e.g., `⊢ h` where `h : P`).

The fallback is only triggered when:
1. No results are found from the primary (concrete-keyed) search
2. The goal type is headed by a free variable
3. The `-star` flag is not set

Use `exact? -star` or `apply? -star` to disable the fallback.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
This PR makes two main improvements to library search:

1. **Replace duplicate tree with array for star-indexed lemmas**
   - Instead of maintaining `fullExt` (a complete duplicate discrimination tree),
     use `starLemmasExt` (a simple array for lemmas with `[*]` or `[Eq,*,*,*]` keys)
   - Star-indexed lemmas are captured during tree initialization via `extractKeys`
   - Two-pass search: first excludes star lemmas, fallback includes them
   - Controlled by `-star`/`+star` flags

2. **Support eliminator-style theorems like `iteInduction`**
   - Add `getFirstArgEntry` to create secondary index entries for fvar-headed theorems
   - For `motive (ite c t e)`, creates entry keyed by `.const ite 4`
   - Modify `getMatchCore` to check first argument's const key for fvar-headed goals
   - This enables finding eliminators even with `-star`

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
@kim-em kim-em force-pushed the librarySearch-star2 branch from 48be6a0 to 36b63ce Compare December 4, 2025 05:32
@kim-em kim-em added awaiting-mathlib We should not merge this until we have a successful Mathlib build and removed toolchain-available A toolchain is available for this PR, at leanprover/lean4-pr-releases:pr-release-NNNN labels Dec 4, 2025
@github-actions github-actions bot added the toolchain-available A toolchain is available for this PR, at leanprover/lean4-pr-releases:pr-release-NNNN label Dec 4, 2025
leanprover-community-mathlib4-bot added a commit to leanprover-community/batteries that referenced this pull request Dec 4, 2025
leanprover-community-mathlib4-bot added a commit to leanprover-community/mathlib4-nightly-testing that referenced this pull request Dec 4, 2025
@leanprover-community-bot leanprover-community-bot removed the awaiting-mathlib We should not merge this until we have a successful Mathlib build label Dec 4, 2025
@leanprover-community-bot
Copy link
Collaborator

@leanprover-community-bot leanprover-community-bot added the builds-mathlib CI has verified that Mathlib builds against this PR label Dec 4, 2025
@kim-em kim-em marked this pull request as ready for review December 4, 2025 22:50
@kim-em kim-em requested a review from leodemoura as a code owner December 4, 2025 22:50
@kim-em
Copy link
Collaborator Author

kim-em commented Dec 4, 2025

Performance in Mathlib seems acceptable.

@kim-em kim-em enabled auto-merge December 4, 2025 22:50
@kim-em kim-em added the changelog-tactics User facing tactics label Dec 4, 2025
@kim-em kim-em added this pull request to the merge queue Dec 4, 2025
Merged via the queue into master with commit 0ba40b7 Dec 4, 2025
36 of 39 checks passed
algebraic-dev pushed a commit that referenced this pull request Dec 8, 2025
This PR re-enables star-indexed lemmas as a fallback for `exact?` and
`apply?`.

Star-indexed lemmas (those with overly-general discrimination tree keys
like `[*]`)
were previously dropped entirely for performance reasons. This caused
useful lemmas
like `Empty.elim`, `And.left`, `not_not.mp`, `Sum.elim`, and
`Function.mtr` to be
unfindable by library search.

The implementation adds a two-pass search strategy:
1. First, search using concrete discrimination keys (the current
behavior)
2. If no results are found, fall back to trying star-indexed lemmas

The star-indexed lemmas are extracted during tree initialization and
cached in an
environment extension, avoiding repeated computation.

Users can disable the fallback with `-star`:
```lean
example {α : Sort u} (h : Empty) : α := by apply? -star  -- error: no lemmas found
example {α : Sort u} (h : Empty) : α := by apply?        -- finds Empty.elim
```

🤖 Prepared with Claude Code

---------

Co-authored-by: Claude <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

builds-mathlib CI has verified that Mathlib builds against this PR changelog-tactics User facing tactics toolchain-available A toolchain is available for this PR, at leanprover/lean4-pr-releases:pr-release-NNNN

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants