The areas / V34. The tactics of Mathlib
Mathlib.Tactic.TacticAnalysis
64 declarations. Mathlib docs · Source
Tactic analysis framework
In this file we define a framework for analyzing sequences of tactics.
This can be used for linting (for instance: report when two rw calls can be merged into one),
but it can also be run in a more batch-like mode to report larger potential refactors
(for instance: report when a sequence of three or more tactics can be replaced with grind,
without taking more heartbeats than the original proof did).
Using the framework
The framework runs, but does nothing by default (set_option linter.tacticAnalysis false
to turn it off completely). Enable the analysis round roundName by enabling its corresponding
option: set_option linter.tacticAnalysis.roundName true.
To add a round of analysis called roundName, declare an option linter.tacticAnalysis.roundName,
make a definition of type Mathlib.TacticAnalysis.Config and give the Config declaration the
@[tacticAnalysis linter.tacticAnalysis.roundName] attribute. Don't forget to enable the option.
Passes report their findings by logging messages, e.g. with logWarningAt or logInfoAt.
The framework tags these messages with the pass's option name, the way Lean.Linter.logLint
does, so that lake lint and CI reports can group them by pass. Unlike core linters, passes also
report at info severity; such messages are tagged too, so a lint driver without a severity
filter (such as Lake's builtin one) counts them as lint failures.
Warning
The ComplexConfig interface doesn't feel quite intuitive and flexible yet and should be changed
in the future. Please do not rely on this interface being stable.
Work in progress: Config building blocks
In this section we define ComplexConfig which is supposed to make it easier to build standard
analysis rounds.
Work in progress note: This interface does not feel intuitive yet and might be redesigned. Please do not rely on it being stable!
def Mathlib.TacticAnalysis.TriggerCondition.ctorIdx E1 #
{ctx : Type u_1} → Mathlib.TacticAnalysis.TriggerCondition ctx → ℕ
abbrev Mathlib.TacticAnalysis.TriggerCondition.ctorElimType E1 #
{ctx : Type u_1} →
{motive : Mathlib.TacticAnalysis.TriggerCondition ctx → Sort u} → ℕ → Sort (max 1 u (imax (u_1 + 1) u))
theorem Mathlib.TacticAnalysis.Config.mk.inj E1 #
∀ {run run_1 : Array Mathlib.TacticAnalysis.TacticNode → Lean.Elab.Command.CommandElabM Unit},
{ run := run } = { run := run_1 } → run = run_1
theorem Mathlib.TacticAnalysis.TacticNode.mk.injEq E0 #
∀ (ctxI : Lean.Elab.ContextInfo) (tacI : Lean.Elab.TacticInfo) (ctxI_1 : Lean.Elab.ContextInfo)
(tacI_1 : Lean.Elab.TacticInfo),
({ ctxI := ctxI, tacI := tacI } = { ctxI := ctxI_1, tacI := tacI_1 }) = (ctxI = ctxI_1 ∧ tacI = tacI_1)
theorem Mathlib.TacticAnalysis.TriggerCondition.continue.inj E1 #
∀ {ctx : Type u_1} {context context_1 : ctx},
Mathlib.TacticAnalysis.TriggerCondition.continue context =
Mathlib.TacticAnalysis.TriggerCondition.continue context_1 →
context = context_1
theorem Mathlib.TacticAnalysis.Config.mk.injEq E0 #
∀ (run run_1 : Array Mathlib.TacticAnalysis.TacticNode → Lean.Elab.Command.CommandElabM Unit),
({ run := run } = { run := run_1 }) = (run = run_1)
theorem Mathlib.TacticAnalysis.ComplexConfig.mk.injEq E0 #
∀ (out ctx : Type) (trigger : Option ctx → Lean.Syntax → Mathlib.TacticAnalysis.TriggerCondition ctx)
(test : Lean.Elab.ContextInfo → Lean.Elab.TacticInfo → ctx → Lean.MVarId → Lean.Elab.Command.CommandElabM out)
(tell : Lean.Syntax → List Lean.MVarId → ℕ → out → ℕ → Lean.Elab.Command.CommandElabM (Option Lean.MessageData))
(out_1 ctx_1 : Type) (trigger_1 : Option ctx_1 → Lean.Syntax → Mathlib.TacticAnalysis.TriggerCondition ctx_1)
(test_1 : Lean.Elab.ContextInfo → Lean.Elab.TacticInfo → ctx_1 → Lean.MVarId → Lean.Elab.Command.CommandElabM out_1)
(tell_1 : Lean.Syntax → List Lean.MVarId → ℕ → out_1 → ℕ → Lean.Elab.Command.CommandElabM (Option Lean.MessageData)),
({ out := out, ctx := ctx, trigger := trigger, test := test, tell := tell } =
{ out := out_1, ctx := ctx_1, trigger := trigger_1, test := test_1, tell := tell_1 }) =
(out = out_1 ∧ ctx = ctx_1 ∧ trigger ≍ trigger_1 ∧ test ≍ test_1 ∧ tell ≍ tell_1)
theorem Mathlib.TacticAnalysis.ComplexConfig.mk.inj E1 #
∀ {out ctx : Type} {trigger : Option ctx → Lean.Syntax → Mathlib.TacticAnalysis.TriggerCondition ctx}
{test : Lean.Elab.ContextInfo → Lean.Elab.TacticInfo → ctx → Lean.MVarId → Lean.Elab.Command.CommandElabM out}
{tell : Lean.Syntax → List Lean.MVarId → ℕ → out → ℕ → Lean.Elab.Command.CommandElabM (Option Lean.MessageData)}
{out_1 ctx_1 : Type} {trigger_1 : Option ctx_1 → Lean.Syntax → Mathlib.TacticAnalysis.TriggerCondition ctx_1}
{test_1 : Lean.Elab.ContextInfo → Lean.Elab.TacticInfo → ctx_1 → Lean.MVarId → Lean.Elab.Command.CommandElabM out_1}
{tell_1 : Lean.Syntax → List Lean.MVarId → ℕ → out_1 → ℕ → Lean.Elab.Command.CommandElabM (Option Lean.MessageData)},
{ out := out, ctx := ctx, trigger := trigger, test := test, tell := tell } =
{ out := out_1, ctx := ctx_1, trigger := trigger_1, test := test_1, tell := tell_1 } →
out = out_1 ∧ ctx = ctx_1 ∧ trigger ≍ trigger_1 ∧ test ≍ test_1 ∧ tell ≍ tell_1
theorem Mathlib.TacticAnalysis.Entry.mk.inj E1 #
∀ {declName optionName declName_1 optionName_1 : Lean.Name},
{ declName := declName, optionName := optionName } = { declName := declName_1, optionName := optionName_1 } →
declName = declName_1 ∧ optionName = optionName_1
def Mathlib.TacticAnalysis.Config.ctorIdx E1 #
Mathlib.TacticAnalysis.Config → ℕ
theorem Mathlib.TacticAnalysis.Pass.mk.injEq E0 #
∀ (toConfig : Mathlib.TacticAnalysis.Config) (opt : Option (Lean.Option Bool))
(toConfig_1 : Mathlib.TacticAnalysis.Config) (opt_1 : Option (Lean.Option Bool)),
({ toConfig := toConfig, opt := opt } = { toConfig := toConfig_1, opt := opt_1 }) =
(toConfig = toConfig_1 ∧ opt = opt_1)
def Mathlib.TacticAnalysis.Entry.ctorIdx E1 #
Mathlib.TacticAnalysis.Entry → ℕ
theorem Mathlib.TacticAnalysis.TriggerCondition.continue.injEq E0 #
∀ {ctx : Type u_1} (context context_1 : ctx),
(Mathlib.TacticAnalysis.TriggerCondition.continue context =
Mathlib.TacticAnalysis.TriggerCondition.continue context_1) =
(context = context_1)
def Mathlib.TacticAnalysis.Pass.ctorIdx E1 #
Mathlib.TacticAnalysis.Pass → ℕ
theorem Mathlib.TacticAnalysis.TacticNode.mk.inj E1 #
∀ {ctxI : Lean.Elab.ContextInfo} {tacI : Lean.Elab.TacticInfo} {ctxI_1 : Lean.Elab.ContextInfo}
{tacI_1 : Lean.Elab.TacticInfo},
{ ctxI := ctxI, tacI := tacI } = { ctxI := ctxI_1, tacI := tacI_1 } → ctxI = ctxI_1 ∧ tacI = tacI_1
def Mathlib.TacticAnalysis.ComplexConfig.ctorIdx E1 #
Mathlib.TacticAnalysis.ComplexConfig → ℕ
def Mathlib.TacticAnalysis.TacticNode.ctorIdx E1 #
Mathlib.TacticAnalysis.TacticNode → ℕ
theorem Mathlib.TacticAnalysis.Entry.mk.injEq E0 #
∀ (declName optionName declName_1 optionName_1 : Lean.Name),
({ declName := declName, optionName := optionName } = { declName := declName_1, optionName := optionName_1 }) =
(declName = declName_1 ∧ optionName = optionName_1)
theorem Mathlib.TacticAnalysis.TriggerCondition.accept.inj E1 #
∀ {ctx : Type u_1} {context context_1 : ctx},
Mathlib.TacticAnalysis.TriggerCondition.accept context = Mathlib.TacticAnalysis.TriggerCondition.accept context_1 →
context = context_1
theorem Mathlib.TacticAnalysis.Pass.mk.inj E1 #
∀ {toConfig : Mathlib.TacticAnalysis.Config} {opt : Option (Lean.Option Bool)}
{toConfig_1 : Mathlib.TacticAnalysis.Config} {opt_1 : Option (Lean.Option Bool)},
{ toConfig := toConfig, opt := opt } = { toConfig := toConfig_1, opt := opt_1 } → toConfig = toConfig_1 ∧ opt = opt_1
theorem Mathlib.TacticAnalysis.TriggerCondition.accept.injEq E0 #
∀ {ctx : Type u_1} (context context_1 : ctx),
(Mathlib.TacticAnalysis.TriggerCondition.accept context = Mathlib.TacticAnalysis.TriggerCondition.accept context_1) =
(context = context_1)
opaque linter.tacticAnalysis E1 #
Lean.Option Bool
The tactic analysis framework hooks into the linter to run analysis rounds on sequences of tactics. This can be used for linting, or in a more batch-like mode to report potential refactors.
structure Mathlib.TacticAnalysis.TacticNode E1 #
Type
Information about a tactic in a sequence, parsed from infotrees and passed to a tactic analysis pass.
ctor Mathlib.TacticAnalysis.TacticNode.mk E0 #
Lean.Elab.ContextInfo → Lean.Elab.TacticInfo → Mathlib.TacticAnalysis.TacticNode
abbrev Mathlib.TacticAnalysis.TacticNode.ctxI E1 #
Mathlib.TacticAnalysis.TacticNode → Lean.Elab.ContextInfo
ContextInfo at the infotree node.
abbrev Mathlib.TacticAnalysis.TacticNode.tacI E1 #
Mathlib.TacticAnalysis.TacticNode → Lean.Elab.TacticInfo
TacticInfo at the infotree node.
abbrev Mathlib.TacticAnalysis.TacticNode.runTacticCode E1 #
Mathlib.TacticAnalysis.TacticNode → Lean.MVarId → Lean.Syntax → Lean.Elab.Command.CommandElabM (List Lean.MVarId)
Run tactic code, given by a piece of syntax, in the context of a tactic info node.
Convenience abbreviation for ContextInfo.runTacticCode.
abbrev Mathlib.TacticAnalysis.TacticNode.runTacticCodeCapturingInfoTree E1 #
Mathlib.TacticAnalysis.TacticNode →
Lean.MVarId →
Lean.Syntax → Lean.Elab.Command.CommandElabM (List Lean.MVarId × Lean.PersistentArray Lean.Elab.InfoTree)
Run tactic code, capturing InfoTrees for extracting "Try this:" suggestions.
Returns both the resulting goals and the InfoTrees produced during tactic execution.
Use collectTryThisSuggestions from Mathlib.Lean.Elab.InfoTree to extract suggestions.
structure Mathlib.TacticAnalysis.Config E1 #
Type
Stores the configuration for a tactic analysis pass.
This provides the low-level interface into the tactic analysis framework.
ctor Mathlib.TacticAnalysis.Config.mk E0 #
(Array Mathlib.TacticAnalysis.TacticNode → Lean.Elab.Command.CommandElabM Unit) → Mathlib.TacticAnalysis.Config
abbrev Mathlib.TacticAnalysis.Config.run E1 #
Mathlib.TacticAnalysis.Config → Array Mathlib.TacticAnalysis.TacticNode → Lean.Elab.Command.CommandElabM Unit
The function that runs this pass. Takes an array of infotree nodes corresponding
to a sequence of tactics from the source file. Should do all reporting itself,
for example by logWarningAt; the framework tags the messages with the pass's option name.
structure Mathlib.TacticAnalysis.Pass E1 #
Type
The internal representation of a tactic analysis pass,
extending Config with some declaration meta-information.
ctor Mathlib.TacticAnalysis.Pass.mk E0 #
Mathlib.TacticAnalysis.Config → Option (Lean.Option Bool) → Mathlib.TacticAnalysis.Pass
abbrev Mathlib.TacticAnalysis.Pass.toConfig E1 #
Mathlib.TacticAnalysis.Pass → Mathlib.TacticAnalysis.Config
abbrev Mathlib.TacticAnalysis.Pass.opt E1 #
Mathlib.TacticAnalysis.Pass → Option (Lean.Option Bool)
The option corresponding to this pass, used to enable it.
Example: linter.tacticAnalysis.grindReplacement.
structure Mathlib.TacticAnalysis.Entry E1 #
Type
Each tactic analysis round is represented by the declaration name for the Config.
ctor Mathlib.TacticAnalysis.Entry.mk E0 #
Lean.Name → Lean.Name → Mathlib.TacticAnalysis.Entry
abbrev Mathlib.TacticAnalysis.Entry.declName E1 #
Mathlib.TacticAnalysis.Entry → Lean.Name
The declaration, of type Config, that defines this pass.
abbrev Mathlib.TacticAnalysis.Entry.optionName E1 #
Mathlib.TacticAnalysis.Entry → Lean.Name
The option, of type Lean.Option Bool, that controls whether the pass is enabled.
def Mathlib.TacticAnalysis.Entry.import E1 #
Mathlib.TacticAnalysis.Entry → Lean.ImportM Mathlib.TacticAnalysis.Pass
Read a configuration from a declaration of the right type.
instance Mathlib.TacticAnalysis.instOrdEntry E0 #
Ord Mathlib.TacticAnalysis.Entry
opaque Mathlib.TacticAnalysis.tacticAnalysisExt E1 #
Lean.PersistentEnvExtension Mathlib.TacticAnalysis.Entry (Mathlib.TacticAnalysis.Entry × Mathlib.TacticAnalysis.Pass)
(List Mathlib.TacticAnalysis.Entry × Array Mathlib.TacticAnalysis.Pass)
Environment extensions for tacticAnalysis declarations
def Mathlib.TacticAnalysis.findTacticSeqs E1 #
Lean.Elab.InfoTree → Array (Array Mathlib.TacticAnalysis.TacticNode)
Parse an infotree to find all the sequences of tactics contained in the original syntax.
We consider a sequence here to be a maximal interval of tactics joined by ; or newlines.
This function returns an array of sequences. For example, a proof of the form:
by
tac1
· tac2; tac3
· tac4; tac5
would result in three sequences:
#[tac1, (· tac2; tac3), (· tac4; tac5)]#[tac2, tac3]#[tac4, tac5]
Similarly, a declaration with multiple by blocks results in each of the blocks getting its
own sequence.
def Mathlib.TacticAnalysis.tagLintMessage E1 #
Lean.Option Bool → Lean.Message → Lean.Message
Tag msg as a finding of the pass enabled by opt, copying the shape of
Lean.Linter.logLint (core has no MessageData-level helper for this, so keep the two in sync):
the kind becomes opt.name, Lean.Linter.linterMessageTag sits directly inside it, and a note
on how to disable the pass is appended.
Errors, and messages that are already linter messages, are returned unchanged.
def Mathlib.TacticAnalysis.withLintTagging E1 #
{α : Type} → Lean.Option Bool → Lean.Elab.Command.CommandElabM α → Lean.Elab.Command.CommandElabM α
Run x with an empty message log, then tag every message it logged with
tagLintMessage opt and append them to the messages logged before.
def Mathlib.TacticAnalysis.runPasses E1 #
Array Mathlib.TacticAnalysis.Pass → Lean.PersistentArray Lean.Elab.InfoTree → Lean.Elab.Command.CommandElabM Unit
Run the tactic analysis passes from configs on the tactic sequences in stx,
using trees to get the infotrees.
def Mathlib.TacticAnalysis.tacticAnalysis E1 #
Lean.Linter
A tactic analysis framework. It is aimed at allowing developers to specify refactoring patterns, which will be tested against a whole project, to report proposed changes.
It hooks into the linting system to move through the infotree, collecting tactic syntax and state to call the passes on.
inductive Mathlib.TacticAnalysis.TriggerCondition E1 #
Type u_1 → Type u_1
The condition is returned from the .trigger function to indicate which sublists of a
tactic sequence to test.
The context field can be used to accumulate data between different invocations of .trigger.
ctor Mathlib.TacticAnalysis.TriggerCondition.skip E0 #
{ctx : Type u_1} → Mathlib.TacticAnalysis.TriggerCondition ctx
skip means that the current tactic and the ones before it will be discarded.
ctor Mathlib.TacticAnalysis.TriggerCondition.continue E0 #
{ctx : Type u_1} → ctx → Mathlib.TacticAnalysis.TriggerCondition ctx
continue means to accumulate the current tactic, but not yet run the test on it.
ctor Mathlib.TacticAnalysis.TriggerCondition.accept E0 #
{ctx : Type u_1} → ctx → Mathlib.TacticAnalysis.TriggerCondition ctx
accept means to run the test on the sequence of .continues up to this .accept.
def Mathlib.TacticAnalysis.instBEqTriggerCondition.beq E1 #
{ctx : Type u_1} →
[BEq ctx] → Mathlib.TacticAnalysis.TriggerCondition ctx → Mathlib.TacticAnalysis.TriggerCondition ctx → Bool
instance Mathlib.TacticAnalysis.instBEqTriggerCondition E0 #
{ctx : Type u_1} → [BEq ctx] → BEq (Mathlib.TacticAnalysis.TriggerCondition ctx)
structure Mathlib.TacticAnalysis.ComplexConfig E1 #
Type 1
Specifies which analysis steps to take.
The overall design will have three user-supplied components:
- trigger on a piece of syntax (which could contain multiple tactic calls);
- test if a suggested change is indeed an improvement;
- tell the user where changes can be made.
ctor Mathlib.TacticAnalysis.ComplexConfig.mk E0 #
(out ctx : Type) →
(Option ctx → Lean.Syntax → Mathlib.TacticAnalysis.TriggerCondition ctx) →
(Lean.Elab.ContextInfo → Lean.Elab.TacticInfo → ctx → Lean.MVarId → Lean.Elab.Command.CommandElabM out) →
(Lean.Syntax → List Lean.MVarId → ℕ → out → ℕ → Lean.Elab.Command.CommandElabM (Option Lean.MessageData)) →
Mathlib.TacticAnalysis.ComplexConfig
abbrev Mathlib.TacticAnalysis.ComplexConfig.out E1 #
Mathlib.TacticAnalysis.ComplexConfig → Type
Type returned by the .test function.
abbrev Mathlib.TacticAnalysis.ComplexConfig.ctx E1 #
Mathlib.TacticAnalysis.ComplexConfig → Type
Type returned by the .trigger function.
abbrev Mathlib.TacticAnalysis.ComplexConfig.trigger E1 #
(self : Mathlib.TacticAnalysis.ComplexConfig) →
Option self.ctx → Lean.Syntax → Mathlib.TacticAnalysis.TriggerCondition self.ctx
Determines which (sequences of) tactics to analyze.
context is some ctx whenever the previous trigger returned continue ctx,
none at the start of a tactic sequence or after a skip/accept.
If the last returned value is continue at the end of the sequence, the framework inserts an
extra done to run the trigger on.
abbrev Mathlib.TacticAnalysis.ComplexConfig.test E1 #
(self : Mathlib.TacticAnalysis.ComplexConfig) →
Lean.Elab.ContextInfo → Lean.Elab.TacticInfo → self.ctx → Lean.MVarId → Lean.Elab.Command.CommandElabM self.out
Code to run in the context of the tactic, for example an alternative tactic.
abbrev Mathlib.TacticAnalysis.ComplexConfig.tell E1 #
(self : Mathlib.TacticAnalysis.ComplexConfig) →
Lean.Syntax → List Lean.MVarId → ℕ → self.out → ℕ → Lean.Elab.Command.CommandElabM (Option Lean.MessageData)
Decides what to report to the user.
def Mathlib.TacticAnalysis.testTacticSeq E1 #
(config : Mathlib.TacticAnalysis.ComplexConfig) →
Array (Lean.TSyntax `tactic) → Mathlib.TacticAnalysis.TacticNode → config.ctx → Lean.Elab.Command.CommandElabM Unit
Test the config against a sequence of tactics, using the context info and tactic info
from the start of the sequence.
def Mathlib.TacticAnalysis.runPass E1 #
Mathlib.TacticAnalysis.ComplexConfig → Array Mathlib.TacticAnalysis.TacticNode → Lean.Elab.Command.CommandElabM Unit
Run the config against a sequence of tactics, using the trigger to determine which
subsequences should be tested.
def Mathlib.TacticAnalysis.Config.ofComplex E1 #
Mathlib.TacticAnalysis.ComplexConfig → Mathlib.TacticAnalysis.Config
Constructor for a Config which breaks the pass up into multiple pieces.
opaque linter.tacticAnalysis.dummy E1 #
Lean.Option Bool
A dummy option for testing the tactic analysis framework