Skip to content

Commit 1ce05b2

Browse files
authored
feat: library suggestion engine for local theorems (#11030)
This PR adds a library suggestion engine for local theorems. To be useful, I still need to write more combinators to re-rank and combine suggestions from multiple engines. This is stacked on top of #11029.
1 parent 0db89d6 commit 1ce05b2

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

src/Lean/LibrarySuggestions/Basic.lean

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,22 @@ def random (gen : StdGen := ⟨37, 59⟩) : Selector := fun _ cfg => do
286286
suggestions := suggestions.push { name := name, score := 1.0 / consts.size.toFloat }
287287
return suggestions
288288

289+
/-- A library suggestion engine that returns locally defined theorems (those in the current file). -/
290+
def currentFile : Selector := fun _ cfg => do
291+
let env ← getEnv
292+
let max := cfg.maxSuggestions
293+
-- Use map₂ from the staged map, which contains locally defined constants
294+
let mut suggestions := #[]
295+
for (name, ci) in env.constants.map₂.toList do
296+
if suggestions.size >= max then
297+
break
298+
if isDeniedPremise env name then
299+
continue
300+
match ci with
301+
| .thmInfo _ => suggestions := suggestions.push { name := name, score := 1.0 }
302+
| _ => continue
303+
return suggestions
304+
289305
builtin_initialize librarySuggestionsExt : EnvExtension (Option Selector) ←
290306
registerEnvExtension (pure none)
291307

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import Lean.LibrarySuggestions.Basic
2+
3+
-- Define some initial local constants
4+
def myLocalDef : Nat := 42
5+
6+
theorem myLocalTheorem : myLocalDef = 42 := rfl
7+
8+
-- Add a theorem in the Lean namespace (should be filtered by isDeniedPremise)
9+
namespace Lean
10+
theorem shouldBeFiltered : True := trivial
11+
end Lean
12+
13+
-- Test the currentFile selector (should only show theorems, not definitions)
14+
set_library_suggestions Lean.LibrarySuggestions.currentFile
15+
16+
-- First test: should show only myLocalTheorem
17+
-- (not myLocalDef since it's a def, not Lean.shouldBeFiltered since it's in Lean namespace)
18+
/--
19+
info: Library suggestions: [myLocalTheorem]
20+
-/
21+
#guard_msgs in
22+
example : True := by
23+
suggestions
24+
trivial
25+
26+
-- Add more local constants (mix of theorems and definitions)
27+
theorem anotherTheorem : True := trivial
28+
29+
def myFunction (x : Nat) : Nat := x + 1
30+
31+
-- Second test: should show only the two theorems (not myFunction)
32+
/--
33+
info: Library suggestions: [anotherTheorem, myLocalTheorem]
34+
-/
35+
#guard_msgs in
36+
example : False → True := by
37+
suggestions
38+
intro h
39+
trivial

0 commit comments

Comments
 (0)