Skip to content

Commit 649109a

Browse files
committed
chore: merge master
2 parents 1233422 + c41cb64 commit 649109a

24 files changed

+239
-155
lines changed

.github/workflows/pr-title.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@ jobs:
1515
script: |
1616
const msg = context.payload.pull_request? context.payload.pull_request.title : context.payload.merge_group.head_commit.message;
1717
console.log(`Message: ${msg}`)
18-
if (!/^(feat|fix|doc|style|refactor|test|chore|perf): .*[^.]($|\n\n)/.test(msg)) {
18+
if (!/^(feat|fix|doc|style|refactor|test|chore|perf): (?![A-Z][a-z]).*[^.]($|\n\n)/.test(msg)) {
1919
core.setFailed('PR title does not follow the Commit Convention (https://leanprover.github.io/lean4/doc/dev/commit_convention.html).');
2020
}

src/Init/Grind/Config.lean

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ structure Config where
1818
/-- If `lax` is `true`, `grind` will silently ignore any parameters referring to non-existent theorems
1919
or for which no patterns can be generated. -/
2020
lax : Bool := false
21-
/-- If `premises` is `true`, `grind` will invoke the currently configured premise selecor on the current goal,
22-
and add attempt to use the resulting suggestions as premises to the `grind` tactic. -/
23-
premises : Bool := false
21+
/-- If `suggestions` is `true`, `grind` will invoke the currently configured library suggestion engine on the current goal,
22+
and add attempt to use the resulting suggestions as additional parameters to the `grind` tactic. -/
23+
suggestions : Bool := false
2424
/-- Maximum number of case-splits in a proof search branch. It does not include splits performed during normalization. -/
2525
splits : Nat := 9
2626
/-- Maximum number of E-matching (aka heuristic theorem instantiation) rounds before each case split. -/

src/Init/Notation.lean

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -863,12 +863,12 @@ syntax (name := infoTreesCmd)
863863
"#info_trees" " in" ppLine command : command
864864

865865
/--
866-
Specify a premise selection engine.
867-
Note that Lean does not ship a default premise selection engine,
866+
Specify a library suggestion engine.
867+
Note that Lean does not ship a default library suggestion engine,
868868
so this is only useful in conjunction with a downstream package which provides one.
869869
-/
870-
syntax (name := setPremiseSelectorCmd)
871-
"set_premise_selector" term : command
870+
syntax (name := setLibrarySuggestionsCmd)
871+
"set_library_suggestions" term : command
872872

873873
namespace Parser
874874

src/Init/Tactics.lean

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1759,11 +1759,12 @@ as well as tactics such as `next`, `case`, and `rename_i`.
17591759
syntax (name := exposeNames) "expose_names" : tactic
17601760

17611761
/--
1762-
`#suggest_premises` will suggest premises for the current goal, using the currently registered premise selector.
1762+
`#suggestions` will suggest relevant theorems from the library for the current goal,
1763+
using the currently registered library suggestion engine.
17631764
17641765
The suggestions are printed in the order of their confidence, from highest to lowest.
17651766
-/
1766-
syntax (name := suggestPremises) "suggest_premises" : tactic
1767+
syntax (name := suggestions) "suggestions" : tactic
17671768

17681769
/--
17691770
Close fixed-width `BitVec` and `Bool` goals by obtaining a proof from an external SAT solver and

src/Lean.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public import Lean.LabelAttribute
4040
public import Lean.AddDecl
4141
public import Lean.Replay
4242
public import Lean.PrivateName
43-
public import Lean.PremiseSelection
43+
public import Lean.LibrarySuggestions
4444
public import Lean.Namespace
4545
public import Lean.EnvExtension
4646
public import Lean.ErrorExplanation

src/Lean/Elab/Tactic/Grind/Main.lean

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public import Lean.Meta.Tactic.Grind.Main
99
public import Lean.Meta.Tactic.TryThis
1010
public import Lean.Elab.Command
1111
public import Lean.Elab.Tactic.Config
12-
public import Lean.PremiseSelection.Basic
12+
public import Lean.LibrarySuggestions.Basic
1313
import Lean.Meta.Tactic.Grind.SimpUtil
1414
import Lean.Meta.Tactic.Grind.EMatchTheoremParam
1515
import Lean.Elab.Tactic.Grind.Basic
@@ -77,35 +77,35 @@ private def parseModifier (s : String) : CoreM Grind.AttrKind := do
7777
| .ok stx => Grind.getAttrKindCore stx
7878
| _ => throwError "unexpected modifier {s}"
7979

80-
open PremiseSelection in
81-
def elabGrindPremises
82-
(params : Grind.Params) (premises : Array Suggestion := #[]) : MetaM Grind.Params := do
80+
open LibrarySuggestions in
81+
def elabGrindSuggestions
82+
(params : Grind.Params) (suggestions : Array Suggestion := #[]) : MetaM Grind.Params := do
8383
let mut params := params
84-
for p in premises do
84+
for p in suggestions do
8585
let attr ← match p.flag with
8686
| some flag => parseModifier flag
8787
| none => pure <| .ematch (.default false)
8888
match attr with
8989
| .ematch kind =>
9090
try
9191
params ← addEMatchTheorem params (mkIdent p.name) p.name kind false (warn := false)
92-
catch _ => pure () -- Don't worry if premise suggestion gave bad suggestions.
92+
catch _ => pure () -- Don't worry if library suggestions gave bad theorems.
9393
| _ =>
9494
-- We could actually support arbitrary grind modifiers,
9595
-- and call `processParam` rather than `addEMatchTheorem`,
9696
-- but this would require a larger refactor.
97-
-- Let's only do this if there is a prospect of a premise selector supporting this.
97+
-- Let's only do this if there is a prospect of a library suggestion engine supporting this.
9898
throwError "unexpected modifier {p.flag}"
9999
return params
100100

101-
open PremiseSelection in
102-
def elabGrindParamsAndPremises
101+
open LibrarySuggestions in
102+
def elabGrindParamsAndSuggestions
103103
(params : Grind.Params)
104104
(ps : TSyntaxArray ``Parser.Tactic.grindParam)
105-
(premises : Array Suggestion := #[])
105+
(suggestions : Array Suggestion := #[])
106106
(only : Bool) (lax : Bool := false) : MetaM Grind.Params := do
107107
let params ← elabGrindParams params ps (lax := lax) (only := only)
108-
elabGrindPremises params premises
108+
elabGrindSuggestions params suggestions
109109

110110
def mkGrindParams
111111
(config : Grind.Config) (only : Bool) (ps : TSyntaxArray ``Parser.Tactic.grindParam) (mvarId : MVarId) :
@@ -119,12 +119,11 @@ def mkGrindParams
119119
-/
120120
let casesTypes ← Grind.getCasesTypes
121121
let params := { params with ematch, casesTypes, inj }
122-
let premises ← if config.premises then
123-
let suggestions ← PremiseSelection.select mvarId
124-
pure suggestions
122+
let suggestions ← if config.suggestions then
123+
LibrarySuggestions.select mvarId
125124
else
126125
pure #[]
127-
let params ← elabGrindParamsAndPremises params ps premises (only := only) (lax := config.lax)
126+
let params ← elabGrindParamsAndSuggestions params ps suggestions (only := only) (lax := config.lax)
128127
trace[grind.debug.inj] "{params.inj.getOrigins.map (·.pp)}"
129128
return params
130129

@@ -201,6 +200,21 @@ def setGrindParams (stx : TSyntax `tactic) (params : Array Syntax) : TSyntax `ta
201200
def getGrindParams (stx : TSyntax `tactic) : Array Syntax :=
202201
stx.raw[grindParamsPos][1].getSepArgs
203202

203+
/-- Filter out `+suggestions` from the config syntax -/
204+
def filterSuggestionsFromConfig (config : TSyntax ``Lean.Parser.Tactic.optConfig) : TSyntax ``Lean.Parser.Tactic.optConfig :=
205+
let configItems := config.raw.getArgs
206+
let filteredItems := configItems.filter fun item =>
207+
-- Keep all items except +suggestions
208+
-- Structure: null node -> configItem -> posConfigItem -> ["+", ident]
209+
match item[0]? with
210+
| some configItem => match configItem[0]? with
211+
| some posConfigItem => match posConfigItem[1]? with
212+
| some ident => !(posConfigItem.getKind == ``Lean.Parser.Tactic.posConfigItem && ident.getId == `suggestions)
213+
| none => true
214+
| none => true
215+
| none => true
216+
⟨config.raw.setArgs filteredItems⟩
217+
204218
def mkGrindOnly
205219
(config : TSyntax ``Lean.Parser.Tactic.optConfig)
206220
(trace : Grind.Trace)
@@ -217,7 +231,8 @@ def mkGrindOnly
217231
else
218232
let param ← Grind.globalDeclToGrindParamSyntax declName kind minIndexable
219233
params := params.push param
220-
let result ← `(tactic| grind $config:optConfig only)
234+
let filteredConfig := filterSuggestionsFromConfig config
235+
let result ← `(tactic| grind $filteredConfig:optConfig only)
221236
return setGrindParams result params
222237

223238
private def elabGrindConfig' (config : TSyntax ``Lean.Parser.Tactic.optConfig) (interactive : Bool) : TacticM Grind.Config := do

src/Lean/Elab/Tactic/Try.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -673,7 +673,7 @@ private def mkTryEvalSuggestStx (info : Try.Info) : MetaM (TSyntax `tactic) := d
673673

674674
-- TODO: vanilla `induction`.
675675
-- TODO: make it extensible.
676-
-- TODO: premise selection.
676+
-- TODO: library suggestions.
677677

678678
@[builtin_tactic Lean.Parser.Tactic.tryTrace] def evalTryTrace : Tactic := fun stx => do
679679
match stx with
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Authors: Kim Morrison
66
module
77

88
prelude
9-
import Lean.PremiseSelection.Basic
10-
import Lean.PremiseSelection.SymbolFrequency
11-
import Lean.PremiseSelection.MePo
12-
import Lean.PremiseSelection.SineQuaNon
9+
import Lean.LibrarySuggestions.Basic
10+
import Lean.LibrarySuggestions.SymbolFrequency
11+
import Lean.LibrarySuggestions.MePo
12+
import Lean.LibrarySuggestions.SineQuaNon
Lines changed: 46 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,20 @@ public import Lean.Meta.CompletionName
1212
public import Init.Data.Random
1313

1414
/-!
15-
# An API for premise selection algorithms.
15+
# An API for library suggestion algorithms.
1616
17-
This module provides a basic API for premise selection algorithms,
18-
which are used to suggest identifiers that should be introduced in a proof.
17+
This module provides a basic API for library suggestion algorithms,
18+
which are used to suggest relevant theorems from the library for the current goal.
19+
In the literature this is usually known as "premise selection",
20+
but we mostly avoid that term as most of our users will not be familiar with the term.
1921
2022
The core interface is the `Selector` type, which is a function from a metavariable
2123
and a configuration to a list of suggestions.
24+
The `Selector` is registered as an environment extension,
25+
and the trivial (no suggestions) implementation is `Lean.LibrarySuggestions.empty`.
2226
23-
The `Selector` is registered as an environment extension, and the trivial (no suggestions) implementation
24-
is `Lean.PremiseSelection.empty`.
25-
26-
Lean does not provide a default premise selector, so this module is intended to be used in conjunction
27-
with a downstream package which registers a premise selector.
27+
Lean does not provide a default library suggestion engine, so this module is intended to be used in conjunction
28+
with a downstream package which registers a library suggestion engine.
2829
-/
2930

3031
namespace Lean.Expr.FoldRelevantConstantsImpl
@@ -113,7 +114,7 @@ public def Lean.MVarId.getRelevantConstants (g : MVarId) : MetaM NameSet := with
113114

114115
@[expose] public section
115116

116-
namespace Lean.PremiseSelection
117+
namespace Lean.LibrarySuggestions
117118

118119
/--
119120
A `Suggestion` is essentially just an identifier and a confidence score that the identifier is relevant.
@@ -285,52 +286,68 @@ def random (gen : StdGen := ⟨37, 59⟩) : Selector := fun _ cfg => do
285286
suggestions := suggestions.push { name := name, score := 1.0 / consts.size.toFloat }
286287
return suggestions
287288

288-
builtin_initialize premiseSelectorExt : EnvExtension (Option Selector) ←
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+
305+
builtin_initialize librarySuggestionsExt : EnvExtension (Option Selector) ←
289306
registerEnvExtension (pure none)
290307

291-
/-- Generate premise suggestions for the given metavariable, using the currently registered premise selector. -/
308+
/-- Generate library suggestions for the given metavariable, using the currently registered library suggestions engine. -/
292309
def select (m : MVarId) (c : Config := {}) : MetaM (Array Suggestion) := do
293-
let some selector := premiseSelectorExt.getState (← getEnv) |
294-
throwError "No premise selector registered. \
295-
(Note that Lean does not provide a default premise selector, \
310+
let some selector := librarySuggestionsExt.getState (← getEnv) |
311+
throwError "No library suggestions engine registered. \
312+
(Note that Lean does not provide a default library suggestions engine, \
296313
these must be provided by a downstream library, \
297-
and configured using `set_premise_selector`.)"
314+
and configured using `set_library_suggestions`.)"
298315
selector m c
299316

300317
/-!
301318
Currently the registration mechanism is just global state.
302-
This means that if multiple modules register premise selectors,
319+
This means that if multiple modules register library suggestions engines,
303320
the behaviour will be dependent on the order of loading modules.
304321
305322
We should replace this with a mechanism so that
306-
premise selectors are configured via options in the `lakefile`, and
323+
library suggestions engines are configured via options in the `lakefile`, and
307324
commands are only used to override in a single declaration or file.
308325
-/
309326

310-
/-- Set the current premise selector.-/
311-
def registerPremiseSelector (selector : Selector) : CoreM Unit := do
312-
modifyEnv fun env => premiseSelectorExt.setState env (some selector)
327+
/-- Set the current library suggestions engine.-/
328+
def registerLibrarySuggestions (selector : Selector) : CoreM Unit := do
329+
modifyEnv fun env => librarySuggestionsExt.setState env (some selector)
313330

314331
open Lean Elab Command in
315-
@[builtin_command_elab setPremiseSelectorCmd, inherit_doc setPremiseSelectorCmd]
316-
def elabSetPremiseSelector : CommandElab
317-
| `(command| set_premise_selector $selector) => do
318-
if `Lean.PremiseSelection.Basic ∉ (← getEnv).header.moduleNames then
319-
logWarning "Add `import Lean.PremiseSelection.Basic` before using the `set_premise_selector` command."
332+
@[builtin_command_elab setLibrarySuggestionsCmd, inherit_doc setLibrarySuggestionsCmd]
333+
def elabSetLibrarySuggestions : CommandElab
334+
| `(command| set_library_suggestions $selector) => do
335+
if `Lean.LibrarySuggestions.Basic ∉ (← getEnv).header.moduleNames then
336+
logWarning "Add `import Lean.LibrarySuggestions.Basic` before using the `set_library_suggestions` command."
320337
let selector ← liftTermElabM do
321338
try
322339
let selectorTerm ← Term.elabTermEnsuringType selector (some (Expr.const ``Selector []))
323340
unsafe Meta.evalExpr Selector (Expr.const ``Selector []) selectorTerm
324341
catch _ =>
325342
throwError "Failed to elaborate {selector} as a `MVarId → Config → MetaM (Array Suggestion)`."
326-
liftCoreM (registerPremiseSelector selector)
343+
liftCoreM (registerLibrarySuggestions selector)
327344
| _ => throwUnsupportedSyntax
328345

329346
open Lean.Elab.Tactic in
330-
@[builtin_tactic Lean.Parser.Tactic.suggestPremises] def evalSuggestPremises : Tactic := fun _ =>
347+
@[builtin_tactic Lean.Parser.Tactic.suggestions] def evalSuggestions : Tactic := fun _ =>
331348
liftMetaTactic1 fun mvarId => do
332349
let suggestions ← select mvarId
333-
logInfo m!"Premise suggestions: {suggestions.map (·.name)}"
350+
logInfo m!"Library suggestions: {suggestions.map (·.name)}"
334351
return mvarId
335352

336-
end Lean.PremiseSelection
353+
end Lean.LibrarySuggestions
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ Authors: Kim Morrison
66
module
77

88
prelude
9-
public import Lean.PremiseSelection.Basic
10-
import Lean.PremiseSelection.SymbolFrequency
9+
public import Lean.LibrarySuggestions.Basic
10+
import Lean.LibrarySuggestions.SymbolFrequency
1111
import Lean.Meta.Basic
1212

1313
/-!
@@ -21,7 +21,7 @@ It needs to be tuned and evaluated for Lean.
2121

2222
open Lean
2323

24-
namespace Lean.PremiseSelection.MePo
24+
namespace Lean.LibrarySuggestions.MePo
2525

2626
builtin_initialize registerTraceClass `mepo
2727

0 commit comments

Comments
 (0)