blob: bf8c126a4c8f09d3186d4c38dbae5343ccb115e7 [file]
/-
Copyright (c) 2025 Lean FRO, LLC. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kim Morrison
-/
module
prelude
public import Lean.Meta.Eval
public import Lean.Meta.CompletionName
public import Init.Data.Random
public import Lean.Elab.Tactic.Grind.Annotated
import Init.Omega
/-!
# An API for library suggestion algorithms.
This module provides a basic API for library suggestion algorithms,
which are used to suggest relevant theorems from the library for the current goal.
In the literature this is usually known as "premise selection",
but we mostly avoid that term as most of our users will not be familiar with the term.
The core interface is the `Selector` type, which is a function from a metavariable
and a configuration to a list of suggestions.
The `Selector` is registered as an environment extension,
and the trivial (no suggestions) implementation is `Lean.LibrarySuggestions.empty`.
Lean does not provide a default library suggestion engine, so this module is intended to be used in conjunction
with a downstream package which registers a library suggestion engine.
-/
namespace Lean.Expr.FoldRelevantConstantsImpl
open Lean Meta
/-- How the traversal treats an argument of an application, decided by the head's parameter. -/
inductive ArgKind where
/-- An instance-implicit argument. It is not visited. -/
| instImplicit
/-- The parameter type is a proposition, so the argument is a proof. It is not visited. -/
| proof
/-- The parameter type is never a proposition, so the argument is not a proof. It is visited. -/
| value
/-- `isProof` decides whether the argument is visited. -/
| unknown
deriving Inhabited
/--
The `ArgKind` of the first `n` parameters of `fn`, from the binders of its type, which is unfolded
at default transparency. Unlike `getFunInfo`, this does not share results between threads, so
parallel callers do not contend on a shared cache.
-/
def argKinds (fn : Expr) (n : Nat) : MetaM (Array ArgKind) := do
let fnType ← inferType fn
withAtLeastTransparency .default do
forallBoundedTelescope fnType n fun xs _ =>
xs.mapM fun x => do
let decl ← x.fvarId!.getDecl
if decl.binderInfo.isInstImplicit then
return .instImplicit
-- The sort of the parameter type is the sort of the argument's type. It decides whether the
-- argument is a proof unless it depends on universe variables.
try
match ← whnfD (← inferType decl.type) with
| .sort u =>
return if u.isAlwaysZero then .proof else if u.isNeverZero then .value else .unknown
| _ => return .unknown
catch _ =>
return .unknown
unsafe structure State where
visited : PtrSet Expr := mkPtrSet
visitedConsts : NameHashSet := {}
/-- `argKinds` of constant heads, by head and number of arguments. -/
kinds : Std.HashMap (Expr × Nat) (Array ArgKind) := {}
unsafe abbrev FoldM := StateT State MetaM
unsafe def headKinds (fn : Expr) (n : Nat) : FoldM (Array ArgKind) := do
unless fn.isConst do
return ← argKinds fn n
if let some kinds := (← get).kinds[(fn, n)]? then
return kinds
let kinds ← argKinds fn n
modify fun s => { s with kinds := s.kinds.insert (fn, n) kinds }
return kinds
/--
`mayBeProof` is `false` when the context shows that `e` is not a proof, so `visit` skips `isProof`.
This holds for a type (a binder domain or the body of a `forallE`), for the head or the body of an
application, lambda, or `let` that is not a proof, and for an argument whose parameter type is
never a proposition.
-/
unsafe def fold {α : Type} (f : Name → α → MetaM α) (e : Expr) (acc : α) : FoldM α :=
let rec visit (e : Expr) (acc : α) (mayBeProof := true) : FoldM α := do
if (← get).visited.contains e then
return acc
modify fun s => { s with visited := s.visited.insert e }
if mayBeProof then
if ← isProof e then
-- Don't visit proofs.
return acc
match e with
| .forallE n d b bi =>
let r ← visit d acc (mayBeProof := false)
withLocalDecl n bi d fun x =>
visit (b.instantiate1 x) r (mayBeProof := false)
| .lam n d b bi =>
let r ← visit d acc (mayBeProof := false)
withLocalDecl n bi d fun x =>
visit (b.instantiate1 x) r (mayBeProof := false)
| .mdata _ b => visit b acc mayBeProof
| .letE n t v b nondep =>
let r₁ ← visit t acc (mayBeProof := false)
let r₂ ← visit v r₁
withLetDecl n t v (nondep := nondep) fun x =>
visit (b.instantiate1 x) r₂ (mayBeProof := false)
| .app .. =>
-- Visit the whole application spine at once, so that the parameter kinds of the head are
-- computed once per application.
let fn := e.getAppFn
let args := e.getAppArgs
let kinds ← headKinds fn args.size
let mut acc ← visit fn acc (mayBeProof := false)
for h : i in [0:args.size] do
let kind ← if h' : i < kinds.size then
pure kinds[i]
else
-- The head's type does not unfold to enough binders without the actual arguments.
pure ((← argKinds (mkAppRange fn 0 i args) 1)[0]?.getD .unknown)
match kind with
| .instImplicit | .proof => pure ()
| .value => acc ← visit args[i] acc (mayBeProof := false)
| .unknown => acc ← visit args[i] acc
return acc
| .proj _ _ b => visit b acc
| .const c _ =>
if (← get).visitedConsts.contains c then
return acc
else
modify fun s => { s with visitedConsts := s.visitedConsts.insert c }
if ← isInstanceReducible c then
return acc
else
f c acc
| _ => return acc
visit e acc
@[inline] unsafe def foldUnsafe {α : Type} (e : Expr) (init : α) (f : Name → α → MetaM α) : MetaM α :=
(fold f e init).run' {}
unsafe def relevantConstantsOfEachUnsafe (es : Array Expr)
(cancelTk? : Option IO.CancelToken := none) : MetaM (Array (Array Name)) := do
let mut kinds := {}
let mut out := Array.mkEmpty es.size
for e in es do
-- Parallel callers share `cancelTk?`, and concurrent reads of one `IO.Ref` contend.
if out.size % 64 == 0 then
if let some tk := cancelTk? then
if ← tk.isSet then
throwInterruptException
let (consts, s) ← try
(fold (fun n ns => return ns.push n) e #[]).run { kinds }
catch _ =>
-- For example, a statement that mentions a private auxiliary proof of a module whose
-- private part is not imported.
pure (#[], { kinds })
kinds := s.kinds
out := out.push consts
return out
end FoldRelevantConstantsImpl
/-- Apply `f` to every constant occurring in `e` once, skipping instance arguments and proofs. -/
@[implemented_by FoldRelevantConstantsImpl.foldUnsafe]
public opaque foldRelevantConstants {α : Type} (e : Expr) (init : α) (f : Name → α → MetaM α) : MetaM α := pure init
/-- Collect the constants occurring in `e` (once each), skipping instance arguments and proofs. -/
public def relevantConstants (e : Expr) : MetaM (Array Name) := foldRelevantConstants e #[] (fun n ns => return ns.push n)
/-- Collect the constants occurring in `e` (once each), skipping instance arguments and proofs. -/
public def relevantConstantsAsSet (e : Expr) : MetaM NameSet := foldRelevantConstants e ∅ (fun n ns => return ns.insert n)
/--
`relevantConstants` of each expression in `es`. It computes the parameter kinds of each head
constant once for all of `es`, so it is faster than `relevantConstants` on each expression. An
expression whose traversal fails gets `#[]`; interrupts and resource limits still propagate. When
`cancelTk?` is set, this throws an interrupt exception within the next 64 expressions.
-/
@[implemented_by FoldRelevantConstantsImpl.relevantConstantsOfEachUnsafe]
public opaque relevantConstantsOfEach (es : Array Expr)
(cancelTk? : Option IO.CancelToken := none) : MetaM (Array (Array Name)) :=
pure #[]
end Lean.Expr
open Lean Meta MVarId in
public def Lean.MVarId.getConstants (g : MVarId) : MetaM NameSet := withContext g do
-- `instantiateMVars` is needed so that constants are not lost behind assigned
-- metavariables, e.g. the `?motive` left in a goal reached via `induction`.
-- Note: this does not recover constants behind non-ground delayed-assigned
-- metavariables. Without evidence this matters for premise selection,
-- for now we avoid the extra complexity of walking the metavariable graph.
let mut c := (← instantiateMVars (← g.getType)).getUsedConstantsAsSet
for t in (← getLocalHyps) do
c := c ∪ (← instantiateMVars (← inferType t)).getUsedConstantsAsSet
return c
open Lean Meta MVarId in
public def Lean.MVarId.getRelevantConstants (g : MVarId) : MetaM NameSet := withContext g do
-- `instantiateMVars` is needed so that constants are not lost behind assigned
-- metavariables, e.g. the `?motive` left in a goal reached via `induction`.
-- Note: this does not recover constants behind non-ground delayed-assigned
-- metavariables. Without evidence this matters for premise selection,
-- for now we avoid the extra complexity of walking the metavariable graph.
let mut c ← (← instantiateMVars (← g.getType)).relevantConstantsAsSet
for t in (← getLocalHyps) do
c := c ∪ (← (← instantiateMVars (← inferType t)).relevantConstantsAsSet)
return c
@[expose] public section
namespace Lean.LibrarySuggestions
/--
A `Suggestion` is essentially just an identifier and a confidence score that the identifier is relevant.
If the premise selection request included information about the intended use (e.g. in the simplifier, in `grind`, etc.)
the score may be adjusted for that application.
-/
structure Suggestion where
name : Name
/--
The score of the suggestion, as a probability that this suggestion should be used.
-/
score : Float
/--
Optional flag associated with the suggestion, e.g. "←" or "=",
if the premise selection algorithm is aware of the tactic consuming the results,
and wants to suggest modifiers for this suggestion.
E.g. this supports calling `simp` in the reverse direction,
or telling `grind` or `aesop` to use the theorem in a particular way.
-/
flag : Option String := none
structure Config where
/--
The maximum number of suggestions to return.
-/
maxSuggestions : Nat := 100
/--
The tactic that is calling the premise selection, e.g. `simp`, `grind`, or `aesop`.
This may be used to adjust the score of the suggestions
-/
caller : Option String := none
/--
A filter on suggestions; only suggestions returning `true` should be returned.
(It can be better to filter on the premise selection side, to ensure that enough suggestions are returned.)
-/
filter : Name → MetaM Bool := fun _ => pure true
/--
An optional arbitrary "hint" to the premise selection algorithm.
There is no guarantee that the algorithm will make any use of the hint.
Potential use cases include a natural language comment provided by the user
(e.g. allowing use of the premise selector as a search engine)
or including context from the current proof and/or file.
We may later split these use cases into separate fields if necessary.
-/
hint : Option String := none
abbrev Selector : Type := MVarId → Config → MetaM (Array Suggestion)
/--
Construct a `Selector` (which acts on an `MVarId`)
from a function which takes the pretty printed goal.
-/
def ppSelector (selector : String → Config → MetaM (Array Suggestion)) (g : MVarId) (c : Config) :
MetaM (Array Suggestion) := do
selector (toString (← Meta.ppGoal g)) c
namespace Selector
/--
Respect the `Config.filter` option by implementing it as a post-filter.
If a premise selection implementation does not natively handle the filter,
it should be wrapped with this function.
-/
def postFilter (selector : Selector) : Selector := fun g c => do
let suggestions ← selector g { c with filter := fun _ => pure true }
suggestions.filterM (fun s => c.filter s.name)
/--
Wrapper for `Selector` that ensures
the `maxSuggestions` field in `Config` is respected, post-hoc.
-/
def maxSuggestions (selector : Selector) : Selector := fun g c => do
let suggestions ← selector g c
return suggestions.take c.maxSuggestions
/-- Combine two premise selectors, returning the best suggestions. -/
def combine (selector₁ selector₂ : Selector) : Selector := fun g c => do
let suggestions₁ ← selector₁ g c
let suggestions₂ ← selector₂ g c
let mut dedupMap : Std.HashMap (Name × Option String) Suggestion := {}
for s in suggestions₁ ++ suggestions₂ do
let key := (s.name, s.flag)
dedupMap := dedupMap.alter key fun
| none => some s
| some s' => if s.score > s'.score then some s else some s'
let deduped := dedupMap.valuesArray
let sorted := deduped.qsort (fun s₁ s₂ => s₁.score > s₂.score)
return sorted.take c.maxSuggestions
/--
Filter out theorems from grind-annotated modules when the caller is "grind".
Modules marked with `grind_annotated` contain manually reviewed/annotated theorems,
so they should be excluded from automatic premise selection for grind.
Other callers (like "simp") still receive suggestions from these modules.
-/
def filterGrindAnnotated (selector : Selector) : Selector := fun g c => do
let suggestions ← selector g c
-- Only filter when caller is "grind"
if c.caller == some "grind" then
let env ← getEnv
suggestions.filterM fun s => do
-- Check if the suggestion's module is grind-annotated
match env.getModuleIdxFor? s.name with
| none => return true -- Keep suggestions with no module info
| some modIdx => return !Lean.Elab.Tactic.Grind.isGrindAnnotatedModule env modIdx
else
return suggestions
/--
Combine two premise selectors by interspersing their results (ignoring scores).
The parameter `ratio` (defaulting to 0.5) controls the fraction of suggestions drawn from
`selector₁` while results are available from both. It is clamped to `[0, 1]`, and ties are
resolved in favour of `selector₁`.
Both selectors are asked for the full `maxSuggestions`, rather than a `ratio`-split share, so that
if one selector returns fewer than its share the other can compensate. This doubles the retrieval
work compared to splitting the budget.
-/
def intersperse (selector₁ selector₂ : Selector) (ratio : Float := 0.5) : Selector := fun g c => do
let ratio := min 1.0 (max 0.0 ratio)
let suggestions₁ ← selector₁ g c
let suggestions₂ ← selector₂ g c
let mut result := #[]
let mut i₁ := 0
let mut i₂ := 0
let mut count₁ := 0.0
let mut count₂ := 0.0
-- Intersperse while both arrays have elements
while h : i₁ < suggestions₁.size ∧ i₂ < suggestions₂.size ∧ result.size < c.maxSuggestions do
-- Take from whichever selector keeps the running fraction of `selector₁`
-- contributions closest to `ratio`. Comparing the two candidate next states
-- (rather than the achieved fraction) stays accurate at the endpoints, so
-- `ratio = 1` draws entirely from `selector₁` and `ratio = 0` entirely from
-- `selector₂` while both have results.
let total := count₁ + count₂
let errIfTake₁ := Float.abs ((count₁ + 1) / (total + 1) - ratio)
let errIfTake₂ := Float.abs (count₁ / (total + 1) - ratio)
if errIfTake₁ <= errIfTake₂ then
result := result.push suggestions₁[i₁]
i₁ := i₁ + 1
count₁ := count₁ + 1
else
result := result.push suggestions₂[i₂]
i₂ := i₂ + 1
count₂ := count₂ + 1
-- Append remaining elements from selector₁
while h : i₁ < suggestions₁.size ∧ result.size < c.maxSuggestions do
result := result.push suggestions₁[i₁]
i₁ := i₁ + 1
-- Append remaining elements from selector₂
while h : i₂ < suggestions₂.size ∧ result.size < c.maxSuggestions do
result := result.push suggestions₂[i₂]
i₂ := i₂ + 1
return result
end Selector
section DenyList
/--
Premises from a module whose name has one of the following components are not retrieved.
Use `run_cmd modifyEnv fun env => moduleDenyListExt.addEntry env module` to add a module to the deny list.
-/
builtin_initialize moduleDenyListExt : SimplePersistentEnvExtension String (List String) ←
registerSimplePersistentEnvExtension {
addEntryFn := (·.cons)
addImportedFn := mkStateFromImportedEntries (·.cons) ["Lake", "Lean", "Internal", "Tactic"]
}
/--
A premise whose name has one of the following components is not retrieved.
Use `run_cmd modifyEnv fun env => nameDenyListExt.addEntry env name` to add a name to the deny list.
-/
builtin_initialize nameDenyListExt : SimplePersistentEnvExtension String (List String) ←
registerSimplePersistentEnvExtension {
addEntryFn := (·.cons)
addImportedFn := mkStateFromImportedEntries (·.cons) ["Lake", "Lean", "Internal", "Tactic"]
}
/--
A premise whose `type.getForallBody.getAppFn` is a constant that has one of these prefixes is not retrieved.
Use `run_cmd modifyEnv fun env => typePrefixDenyListExt.addEntry env typePrefix` to add a type prefix to the deny list.
-/
builtin_initialize typePrefixDenyListExt : SimplePersistentEnvExtension Name (List Name) ←
registerSimplePersistentEnvExtension {
addEntryFn := (·.cons)
addImportedFn := mkStateFromImportedEntries (·.cons) [`Lake, `Lean]
}
def isDeniedModule (env : Environment) (moduleName : Name) : Bool :=
(moduleDenyListExt.getState env).any fun p => moduleName.anyS (· == p)
def isDeniedPremise (env : Environment) (name : Name) (allowPrivate : Bool := false) : Bool := Id.run do
if name == ``sorryAx then return true
-- Allow private names through if allowPrivate is set (e.g., for currentFile selector)
if name.isInternalDetail && !(allowPrivate && isPrivateName name) then return true
if isInstanceReducibleCore env name then return true
if Lean.Linter.isDeprecated env name then return true
if (nameDenyListExt.getState env).any (fun p => name.anyS (· == p)) then return true
if let some moduleIdx := env.getModuleIdxFor? name then
let moduleName := env.header.modules[moduleIdx.toNat]!.module
if isDeniedModule env moduleName then
return true
let some ci := env.find? name | return true
if let .const fnName _ := ci.type.getForallBody.getAppFn then
if (typePrefixDenyListExt.getState env).any (fun p => p.isPrefixOf fnName) then return true
return false
end DenyList
/--
The trivial premise selector, which returns no suggestions.
-/
def empty : Selector := fun _ _ => pure #[]
/-- A random premise selection algorithm, provided solely for testing purposes. -/
def random (gen : StdGen := ⟨37, 59⟩) : Selector := fun _ cfg => do
IO.stdGenRef.set gen
let env ← getEnv
let max := cfg.maxSuggestions
let consts := env.constants.map₁.foldl (fun cs n _ => cs.push n) #[]
let mut suggestions := #[]
while suggestions.size < max do
let i ← IO.rand 0 consts.size
let name := consts[i]!
unless isDeniedPremise env name do
suggestions := suggestions.push { name := name, score := 1.0 / consts.size.toFloat }
return suggestions
/-- A library suggestion engine that returns locally defined theorems (those in the current file). -/
def currentFile : Selector := fun _ cfg => do
let env ← getEnv
let max := cfg.maxSuggestions
-- `map₂` contains the locally defined constants
let mut suggestions := #[]
for (name, _) in env.constants.map₂ do
if suggestions.size >= max then
break
-- Allow private names since they're accessible from the current module
if isDeniedPremise env name (allowPrivate := true) then
continue
if wasOriginallyTheorem env name then
suggestions := suggestions.push { name := name, score := 1.0 }
return suggestions
builtin_initialize librarySuggestionsExt : SimplePersistentEnvExtension Name (Option Name) ←
registerSimplePersistentEnvExtension {
addEntryFn := fun _ name => some name -- Last entry wins
addImportedFn := fun entries =>
-- Take the last selector name from all imported modules
entries.foldl (init := none) fun acc moduleEntries =>
moduleEntries.foldl (init := acc) fun _ name => some name
}
/-- Attribute for registering library suggestions selectors. -/
builtin_initialize librarySuggestionsAttr : TagAttribute ←
registerTagAttribute `library_suggestions "library suggestions selector" fun declName => do
let decl ← getConstInfo declName
unless decl.type == mkConst ``Selector do
throwError "declaration '{declName}' must have type `Selector`"
modifyEnv fun env => librarySuggestionsExt.addEntry env declName
/--
Get the currently registered library suggestions selector by looking up the stored declaration name.
Returns `none` if no selector is registered or if evaluation fails.
-/
unsafe def getSelectorImpl : MetaM (Option Selector) := do
let some declName := librarySuggestionsExt.getState (← getEnv) | return none
try
evalConstCheck Selector ``Selector declName
catch _ =>
return none
@[implemented_by getSelectorImpl]
opaque getSelector : MetaM (Option Selector)
/-- Generate library suggestions for the given metavariable, using the currently registered library suggestions engine. -/
def select (m : MVarId) (c : Config := {}) : MetaM (Array Suggestion) := do
let some selector ← getSelector |
throwError "No library suggestions engine registered. \
(Add `import Lean.LibrarySuggestions.Default` to use Lean's built-in engine, \
or use `set_library_suggestions` to configure a custom one.)"
selector m c
/-!
Currently the registration mechanism is just global state.
This means that if multiple modules register library suggestions engines,
the behaviour will be dependent on the order of loading modules.
We should replace this with a mechanism so that
library suggestions engines are configured via options in the `lakefile`, and
commands are only used to override in a single declaration or file.
-/
open Lean Elab Command in
@[builtin_command_elab setLibrarySuggestionsCmd, inherit_doc setLibrarySuggestionsCmd]
def elabSetLibrarySuggestions : CommandElab
| `(command| set_library_suggestions $selector) => do
if `Lean.LibrarySuggestions.Basic ∉ (← getEnv).header.moduleNames then
logWarning "Add `import Lean.LibrarySuggestions.Basic` before using the `set_library_suggestions` command."
-- Generate a fresh name for the selector definition
let name ← liftMacroM <| Macro.addMacroScope `_librarySuggestions
-- Elaborate the definition with the library_suggestions attribute
elabCommand (← `(@[library_suggestions] public meta def $(mkIdent name) : Selector := $selector))
| _ => throwUnsupportedSyntax
open Lean.Elab.Tactic in
@[builtin_tactic Lean.Parser.Tactic.suggestions] def evalSuggestions : Tactic := fun _ =>
liftMetaTactic1 fun mvarId => do
let suggestions ← select mvarId
let mut msg : MessageData := "Library suggestions:"
-- Check if all scores are 1.0
let allScoresOne := suggestions.all (·.score == 1.0)
for s in suggestions do
msg := msg ++ Format.line ++ " " ++ MessageData.ofConstName s.name
if !allScoresOne then
msg := msg ++ m!" (score: {s.score})"
if let some flag := s.flag then
msg := msg ++ m!" [{flag}]"
logInfo msg
return mvarId
end Lean.LibrarySuggestions