lean4-htt/src/Lean
Kim Morrison eb675f708b
feat: user extensibility in try? (#11149)
This PR adds a user-extension mechanism for the `try?` tactic. You can
either use the `@[try_suggestion]` attribute on a declaration with
signature ``MVarId -> Try.Info -> MetaM (Array (TSyntax `tactic))`` to
produce suggestions, or the `register_try?_tactic <stx>` command with a
fixed piece of syntax. User-extensions are only tried *after* the
built-in try strategies have been tried and failed.

I wanted to ensure that if the user provides a tactic that produces a
"Try this:" suggestion, we both emit the original tactic and the
suggested replacement (this is what we already do with `grind` and
`simp`). I have this working, but it is quite hacky: we grab the message
log and parse it. I fear this will break when the "Try this:" format is
inevitably changed in the future.


<!-- CURSOR_SUMMARY -->
---

> [!NOTE]
> Adds user-defined suggestion generators for `try?` via
`@[try_suggestion]` and `register_try?_tactic`, executed after built-ins
with priority and double-suggestion handling.
> 
> - **Parser/Command**:
> - Add command syntax `register_try?_tactic (priority := n)?
<tacticSeq>` in `Lean.Parser.Command`.
> - **Suggestion registry**:
> - Introduce `@[try_suggestion (prio)]` attribute with a scoped env
extension to register generators (`MVarId → Try.Info → MetaM (Array
(TSyntax `tactic))`).
>   - Priority ordering (higher first); supports local/global scope.
> - **Tactic engine (`try?`)**:
> - New unsafe pipeline to collect and run user generators after
built-in tactics; expands nested "Try this" outputs from user tactics.
> - `mkTryEvalSuggestStx` now takes `(goal, info)`; integrates user
tactics as fallback via `attempt_all`.
> - Suppress intermediate "Try this" messages during `evalAndSuggest` by
restoring the message log.
> - **Imports**:
>   - Add `meta import Lean.Elab.Command` for command elaboration.
> - **Tests**:
> - `try_register_builtin.lean`: command availability and warning
without import.
> - `try_user_suggestions.lean`: basic, priority, built-in fallback,
double-suggestion, and command registration cases.
> - Update `versoDocMissing.lean.expected.out` to include
`register_try?_tactic` in expected commands.
> 
> <sup>Written by [Cursor
Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit
302dc9454450eb29ad4ea9b01d87ac60365299ad. This will update automatically
on new commits. Configure
[here](https://cursor.com/dashboard?tab=bugbot).</sup>
<!-- /CURSOR_SUMMARY -->
2025-11-12 23:49:54 +00:00
..
Compiler refactor: do not use String.Iterator (#11127) 2025-11-11 11:46:58 +00:00
Data refactor: do not use String.Iterator (#11127) 2025-11-11 11:46:58 +00:00
DocString refactor: do not use String.Iterator (#11127) 2025-11-11 11:46:58 +00:00
Elab feat: user extensibility in try? (#11149) 2025-11-12 23:49:54 +00:00
ErrorExplanations feat: overhaul meta system (#10362) 2025-09-17 21:04:29 +00:00
Language chore: rename String.endPos -> String.rawEndPos (#10853) 2025-10-21 11:25:30 +00:00
LibrarySuggestions fix: don't suggest deprecated theorems (#11146) 2025-11-12 00:58:47 +00:00
Linter chore: rename String.Range to Lean.Syntax.Range (#10852) 2025-10-21 07:32:25 +00:00
Meta feat: new do elaborator, part 1: doElem_elab attribute (#11150) 2025-11-12 14:25:28 +00:00
Parser feat: new do elaborator, part 1: doElem_elab attribute (#11150) 2025-11-12 14:25:28 +00:00
ParserCompiler chore: remove redundant imports in core (#10750) 2025-10-16 20:27:46 +00:00
PrettyPrinter chore: rename String.endPos -> String.rawEndPos (#10853) 2025-10-21 11:25:30 +00:00
Server fix: pre-filter completion items mod ascii casing (#11140) 2025-11-11 14:11:05 +00:00
Util feat: instantiate tactic parameter optimizer (#10916) 2025-10-23 01:22:33 +00:00
Widget chore: rename String.Range to Lean.Syntax.Range (#10852) 2025-10-21 07:32:25 +00:00
AddDecl.lean test: avoid testing colliding private inductives (#11041) 2025-11-01 11:47:52 +00:00
Attributes.lean chore: more module system fixes and refinements for finishing batteries port (#10819) 2025-10-21 08:19:50 +00:00
AuxRecursor.lean feat: sparse casesOn constructions (#11072) 2025-11-05 15:49:11 +00:00
BuiltinDocAttr.lean chore: remove redundant imports in core (#10750) 2025-10-16 20:27:46 +00:00
Class.lean refactor: update and consolidate attribute-related error messages (#9495) 2025-07-26 02:03:18 +00:00
Compiler.lean chore: remove public section from end of files (#10684) 2025-10-06 13:30:48 +00:00
CoreM.lean perf: reduce amount of symbols in DLLs (#10864) 2025-10-21 09:00:56 +00:00
Data.lean chore: remove public section from end of files (#10684) 2025-10-06 13:30:48 +00:00
Declaration.lean chore: >6 month old deprecations (#10969) 2025-10-26 22:48:41 +00:00
DeclarationRange.lean chore: remove redundant imports in core (#10750) 2025-10-16 20:27:46 +00:00
DefEqAttrib.lean perf: clarify and granularize access to async env ext state (#9587) 2025-08-02 17:01:08 +00:00
DocString.lean refactor: module-ize Lean (#9330) 2025-07-25 12:02:51 +00:00
Elab.lean chore: remove public section from end of files (#10684) 2025-10-06 13:30:48 +00:00
EnvExtension.lean feat: shake import minimizer aware of the module system and arbitrary elaboration dependencies (#10575) 2025-09-28 16:00:00 +00:00
Environment.lean chore: move some material out of Init.Data.String.Basic (#10893) 2025-10-22 16:31:08 +00:00
ErrorExplanation.lean refactor: do not use String.Iterator (#11127) 2025-11-11 11:46:58 +00:00
ErrorExplanations.lean chore: remove public section from end of files (#10684) 2025-10-06 13:30:48 +00:00
Exception.lean feat: hint about inaccessible private declaration on dot notation failure (#10803) 2025-10-17 09:31:56 +00:00
Expr.lean chore: new module system adjustments for the Mathlib port (#11093) 2025-11-05 22:17:53 +00:00
ExtraModUses.lean feat: shake import minimizer aware of the module system and arbitrary elaboration dependencies (#10575) 2025-09-28 16:00:00 +00:00
HeadIndex.lean refactor: module-ize Lean (#9330) 2025-07-25 12:02:51 +00:00
Hygiene.lean chore: remove redundant imports in core (#10750) 2025-10-16 20:27:46 +00:00
ImportingFlag.lean chore: miscellaneous documentation typos (#10009) 2025-08-20 21:39:03 +00:00
InternalExceptionId.lean chore: reorganize Init imports around strings (#10289) 2025-09-07 17:09:14 +00:00
KeyedDeclsAttribute.lean fix: some ExtraModUses (#10620) 2025-10-03 15:50:40 +00:00
LabelAttribute.lean chore: remove redundant imports in core (#10750) 2025-10-16 20:27:46 +00:00
Level.lean chore: remove redundant imports in core (#10750) 2025-10-16 20:27:46 +00:00
LibrarySuggestions.lean feat: make set_library_suggestions persistent 2025-11-03 23:26:40 +11:00
Linter.lean chore: remove public section from end of files (#10684) 2025-10-06 13:30:48 +00:00
LoadDynlib.lean chore: move some material out of Init.Data.String.Basic (#10893) 2025-10-22 16:31:08 +00:00
LocalContext.lean feat: new do elaborator, part 1: doElem_elab attribute (#11150) 2025-11-12 14:25:28 +00:00
Log.lean chore: remove redundant imports in core (#10750) 2025-10-16 20:27:46 +00:00
Message.lean refactor: rename String.split to String.splitToList (#10822) 2025-10-18 12:12:54 +00:00
Meta.lean feat: @[method_specs] to generate specification theorems from class instances (#10302) 2025-09-15 11:17:06 +00:00
MetavarContext.lean fix: remove superfluous Monad instances from some spec lemmas (#10564) (#10618) 2025-09-29 15:02:43 +00:00
Modifiers.lean chore: more module system fixes and refinements for finishing batteries port (#10819) 2025-10-21 08:19:50 +00:00
MonadEnv.lean chore: remove redundant imports in core (#10750) 2025-10-16 20:27:46 +00:00
Namespace.lean refactor: module-ize Lean (#9330) 2025-07-25 12:02:51 +00:00
Parser.lean feat: hexnum parser (#10716) 2025-10-08 21:12:03 +00:00
ParserCompiler.lean chore: even more module system fixes and refinements from Mathlib porting (#10726) 2025-10-15 14:59:09 +00:00
PrettyPrinter.lean perf: reduce amount of symbols in DLLs (#10864) 2025-10-21 09:00:56 +00:00
PrivateName.lean refactor: module-ize Lean (#9330) 2025-07-25 12:02:51 +00:00
ProjFns.lean refactor: module-ize Lean (#9330) 2025-07-25 12:02:51 +00:00
ReducibilityAttrs.lean chore: remove redundant imports in core (#10750) 2025-10-16 20:27:46 +00:00
Replay.lean chore: remove redundant imports in core (#10750) 2025-10-16 20:27:46 +00:00
ReservedNameAction.lean refactor: module-ize Lean (#9330) 2025-07-25 12:02:51 +00:00
ResolveName.lean chore: more module system fixes and refinements for finishing batteries port (#10819) 2025-10-21 08:19:50 +00:00
Runtime.lean refactor: module-ize Lean (#9330) 2025-07-25 12:02:51 +00:00
ScopedEnvExtension.lean chore: remove redundant imports in core (#10750) 2025-10-16 20:27:46 +00:00
Server.lean feat: revamp server logging (#10787) 2025-10-28 16:26:59 +00:00
Setup.lean chore: remove redundant imports in core (#10750) 2025-10-16 20:27:46 +00:00
Shell.lean chore: rename String.endPos -> String.rawEndPos (#10853) 2025-10-21 11:25:30 +00:00
Structure.lean chore: remove redundant imports in core (#10750) 2025-10-16 20:27:46 +00:00
SubExpr.lean chore: remove redundant imports in core (#10750) 2025-10-16 20:27:46 +00:00
Syntax.lean chore: rename String.Range to Lean.Syntax.Range (#10852) 2025-10-21 07:32:25 +00:00
ToExpr.lean feat: support for Rat scientific literals (#10961) 2025-10-26 02:05:26 +00:00
ToLevel.lean refactor: module-ize Lean (#9330) 2025-07-25 12:02:51 +00:00
Util.lean feat: instantiate tactic parameter optimizer (#10916) 2025-10-23 01:22:33 +00:00
Widget.lean chore: remove public section from end of files (#10684) 2025-10-06 13:30:48 +00:00