|
|
|
|
Quelle TacticAnalysis.lean
Sprache: Lean
|
|
module
public import Lean.LibrarySuggestions
public import Mathlib.Algebra.BigOperators.Associated
public import Mathlib.Tactic.AdaptationNote
public import Mathlib.Tactic.TacticAnalysis.Declarations
@[expose] public section
section terminalReplacement
section omega
set_option linter.tacticAnalysis.omegaToLia true
/--
warning: `lia` can replace `omega`
Note: This linter can be disabled with `set_option linter.tacticAnalysis.omegaToLia false`
-/
#guard_msgs in
example : 1 + 1 = 2 := by
omega
end omega
@[tacticAnalysis linter.tacticAnalysis.dummy]
meta def foo : Mathlib.TacticAnalysis.Config :=
Mathlib.TacticAnalysis.terminalReplacement "simp" "simp only" ``Lean.Parser.Tactic.simp
(fun _ _ _ => `(tactic| simp only))
(reportSuccess := true) (reportFailure := true)
/--
warning: `simp only` failed where `simp` succeeded.
Original tactic:
simp
Counterexample:
import Mathlib.Algebra.GroupWithZero.Nat
-/
-- The counterexample name contains a number that varies between runs, so we can't fully test this.
#guard_msgs (substring := true) in
set_option linter.tacticAnalysis.dummy true in
example : List.sum ([1,2,3].map fun x ↦ x + 1) = 9 := by
simp
end terminalReplacement
section rwaSuggestion
-- Preserve an `at` location.
/--
info: Try this:
[apply] rwa [hab] at h
Note: This linter can be disabled with `set_option linter.tacticAnalysis.rwaSuggestion false`
-/
#guard_msgs in
example (P : ℕ → Prop) (a b : ℕ) (hab : a = b) (h : P a) : P b := by
rw [hab] at h
assumption
-- Preserve multiple rewrite rules.
/--
info: Try this:
[apply] rwa [h₁, ← h₂]
Note: This linter can be disabled with `set_option linter.tacticAnalysis.rwaSuggestion false`
-/
#guard_msgs in
example (a b c d : ℕ) (h₁ : a = b) (h₂ : c = b) (h₃ : c = d) : a = d := by
rw [h₁, ← h₂]
assumption
-- `rw` and `assumption` are not adjacent, so don't suggest `rwa`.
#guard_msgs in
example (a b c : ℕ) (h₁ : a = b) (h₂ : c = b) : a = c := by
rw [h₁]
symm
assumption
-- Tactics in nested `by` blocks should also be analyzed.
/--
info: Try this:
[apply] rwa [h₁]
Note: This linter can be disabled with `set_option linter.tacticAnalysis.rwaSuggestion false`
-/
#guard_msgs in
example (a b c : ℕ) (h₁ : a = b) (h₂ : b = c) : a = c := by
have h : a = c := by
rw [h₁]
assumption
exact h
-- `rwa` only supports a single hypothesis as location, so the linter must not fire here.
#guard_msgs in
example (P : ℕ → Prop) (a b : ℕ) (hab : a = b) (h : P a) : P a := by
rw [hab] at h ⊢
assumption
-- `rwa` doesn't support `(config := ...)`, so the linter must not fire here.
#guard_msgs in
example (a b c : ℕ) (h₁ : a = b) (h₂ : b = c) : a = c := by
rw (occs := .pos [1]) [h₁]
assumption
end rwaSuggestion
section tagging
open Lean Elab Command Mathlib.TacticAnalysis
-- Messages are tagged like `Lean.Linter.logLint` does: with the option name as kind and the
-- linter tag. Messages with some other kind (e.g. named errors) are tagged all the same;
-- messages that are already linter messages are left alone.
/--
info: plain
Note: This linter can be disabled with `set_option linter.tacticAnalysis.dummy false`
---
warning: named kind
Note: This linter can be disabled with `set_option linter.tacticAnalysis.dummy false`
---
warning: already a linter message
Note: This linter can be disabled with `set_option linter.tacticAnalysis.dummy false`
-/
#guard_msgs in
run_cmd do
withLintTagging linter.tacticAnalysis.dummy do
logInfo "plain"
logWarning (.tagged `other "named kind")
Linter.logLint linter.tacticAnalysis.dummy (← getRef) "already a linter message"
for msg in (← get).messages.toArray do
unless msg.data.kind == ``linter.tacticAnalysis.dummy do
throwError "wrong kind: {msg.data.kind}"
unless msg.data.isLinterMessage do
throwError "missing linter tag"
-- Errors are not findings: like `logLint`, the wrapper never tags them.
/-- error: an error -/
#guard_msgs in
run_cmd do
withLintTagging linter.tacticAnalysis.dummy do
logError "an error"
let #[err] := (← get).messages.toArray | throwError "expected one message"
unless err.data.kind.isAnonymous do
throwError "error was tagged: {err.data.kind}"
-- The exact nesting `logLint` uses: the kind outside, the linter tag directly inside.
run_cmd do
withLintTagging linter.tacticAnalysis.dummy do
logInfo "shape"
let #[msg] := (← get).messages.toArray | throwError "expected one message"
let .tagged ``linter.tacticAnalysis.dummy (.tagged tag _) := msg.data
| throwError "unexpected message shape"
unless tag == Linter.linterMessageTag do throwError "wrong inner tag: {tag}"
modify fun s => { s with messages := {} }
-- Messages logged outside the wrapper are kept, in order, and untouched,
-- also when the wrapped action throws.
/--
info: before
---
info: inside
Note: This linter can be disabled with `set_option linter.tacticAnalysis.dummy false`
---
info: after
---
warning: before throw
Note: This linter can be disabled with `set_option linter.tacticAnalysis.dummy false`
-/
#guard_msgs in
run_cmd do
logInfo "before"
withLintTagging linter.tacticAnalysis.dummy do
logInfo "inside"
logInfo "after"
try
withLintTagging linter.tacticAnalysis.dummy do
logWarning "before throw"
throwError "boom"
catch _ => pure ()
let kinds := (← get).messages.toArray.map (·.data.kind)
unless kinds == #[.anonymous, ``linter.tacticAnalysis.dummy, .anonymous,
``linter.tacticAnalysis.dummy] do
throwError "wrong kinds: {kinds}"
open Tactic in
/-- Closes the goal with `trivial`, logging a message along the way. -/
elab "loggingTrivial" : tactic => do
logInfo "from re-run"
evalTactic (← `(tactic| trivial))
/-- A tactic only `leaky` triggers on, so that it does not interfere with the other tests. -/
macro "probe" : tactic => `(tactic| trivial)
@[tacticAnalysis linter.tacticAnalysis.dummy]
meta def leaky := terminalReplacement "probe" "loggingTrivial" ``tacticProbe
(fun _ _ _ => `(tactic| loggingTrivial)) (reportSuccess := true)
-- What a re-run tactic logs is not a finding of the pass and is not attributed to it.
/--
warning: `loggingTrivial` can replace `probe`
Note: This linter can be disabled with `set_option linter.tacticAnalysis.dummy false`
-/
#guard_msgs in
set_option linter.tacticAnalysis.dummy true in
example : True := by
probe
end tagging
section rwMerge
set_option linter.tacticAnalysis.rwMerge true
def x := 1
def y := 1
def z := 1
theorem xy : x = y := rfl
theorem yz : y = z := rfl
example : x = y := by
rw [xy]
/--
warning: Try this: rw [xy, yz]
Note: This linter can be disabled with `set_option linter.tacticAnalysis.rwMerge false`
-/
#guard_msgs in
example : x = z := by
rw [xy]
rw [yz]
-- Definitions using `where` clauses did not get picked up by the framework,
-- since apparently their syntax bounds do not match the original.
structure Fact' (p : Prop) : Prop where
out : p
/--
warning: Try this: rw [xy, yz]
Note: This linter can be disabled with `set_option linter.tacticAnalysis.rwMerge false`
-/
#guard_msgs in
example : Fact' (x = z) where
out := by
rw [xy]
rw [yz]
-- Tactics inside `have ... := by ...` should be analyzed.
-- Previously these were missed because `have ... := by ...` is parsed as one node.
/--
warning: Try this: rw [xy, yz]
Note: This linter can be disabled with `set_option linter.tacticAnalysis.rwMerge false`
-/
#guard_msgs in
example : x = z := by
have _h : x = z := by
rw [xy]
rw [yz]
exact _h
-- Same for `let ... := by ...`
/--
warning: Try this: rw [xy, yz]
Note: This linter can be disabled with `set_option linter.tacticAnalysis.rwMerge false`
-/
#guard_msgs in
example : x = z := by
let _h : x = z := by
rw [xy]
rw [yz]
exact _h
universe u
def a : PUnit.{u} := ⟨⟩
def b : PUnit.{u} := ⟨⟩
def c : PUnit.{u} := ⟨⟩
theorem ab : a = b := rfl
theorem bc : b = c := rfl
/--
warning: Try this: rw [ab.{u}, bc.{u}]
Note: This linter can be disabled with `set_option linter.tacticAnalysis.rwMerge false`
-/
#guard_msgs in
example : a.{u} = c := by
rw [ab.{u}]
rw [bc.{u}]
theorem xyz (h : x = z → y = z) : x = y := by rw [h yz]; rfl
-- The next example tripped up `rwMerge` because `rw [xyz fun h => ?_, ← h, xy]` gives
-- an unknown identifier `h`.
example : x = y := by
rw [xyz fun h => ?_]
rw [← h, xy]
end rwMerge
section mergeWithGrind
set_option linter.tacticAnalysis.mergeWithGrind true
example : 1 + 1 = 2 := by
grind
/--
info: 'have : 1 + 1 < 3 := by omega; grind' can be replaced with 'grind'
Try this:
[apply] grind
Note: This linter can be disabled with `set_option linter.tacticAnalysis.mergeWithGrind false`
-/
#guard_msgs in
example : 1 + 1 = 2 := by
have : 1 + 1 < 3 := by omega
grind
-- `#adaptation_note` is ignored
example : 1 + 1 = 2 := by
#adaptation_note /-- -/
grind
set_option linter.unusedTactic false
/--
info: 'skip; grind' can be replaced with 'grind'
Try this:
[apply] grind
Note: This linter can be disabled with `set_option linter.tacticAnalysis.mergeWithGrind false`
-/
#guard_msgs in
example : 0 = 0 := by
intros
intros
intros
intros
skip
grind
set_option linter.unusedTactic true
-- This is a false positive. Before `convert_to`, there is an mvar for the `DecidableEq` instance
-- used with `Finset.instInsert` that is not properly handled
/--
info: 'convert_to Associated (∏ i ∈ insert j s, f i) (∏ i ∈ insert j s, g i); grind' can be replaced with 'grind'
Try this:
[apply] grind
Note: This linter can be disabled with `set_option linter.tacticAnalysis.mergeWithGrind false`
-/
#guard_msgs in
theorem Associated.prod' {M : Type*} [CommMonoid M] {ι : Type*} (s : Finset ι) (f : ι → M)
(g : ι → M) (h : ∀ i, i ∈ s → Associated (f i) (g i)) : Associated (∏ i ∈ s, f i) (∏ i ∈ s, g i) := by
induction s using Finset.induction with
| empty => grind [Associated.prod]
| insert j s hjs IH =>
classical
convert_to Associated (∏ i ∈ insert j s, f i) (∏ i ∈ insert j s, g i)
grind [Associated.mul_mul]
end mergeWithGrind
section replaceWithGrind
set_option linter.tacticAnalysis.terminalToGrind true
example : 1 + 1 = 2 := by
grind
/--
warning: replace the proof with 'grind': have : 1 + 1 < 3 := by omega;
have : 1 + 1 < 4 := by omega;
rfl
Note: This linter can be disabled with `set_option linter.tacticAnalysis.terminalToGrind false`
-/
#guard_msgs in
example : 1 + 1 = 2 := by
have : 1 + 1 < 3 := by omega
have : 1 + 1 < 4 := by omega
rfl
universe u v
-- This next example used to fail with `unknown universe level 'v'`.
/--
warning: replace the proof with 'grind': let T : Type max u v := Sigma f;
have : 1 + 1 = 2 := rfl;
rfl
Note: This linter can be disabled with `set_option linter.tacticAnalysis.terminalToGrind false`
-/
#guard_msgs in
example {α : Type u} (f : α → Type max u v) : 1 = 1 := by
let T : Type max u v := Sigma f
have : 1 + 1 = 2 := rfl -- Extra line to ensure the linter picks it up.
rfl
-- Ensure the effects of `classical` are picked up. Otherwise we get an error like:
-- failed to synthesize
-- Decidable b
theorem forall_imp_iff_exists_imp' {α : Type} {p : α → Prop} {b : Prop} [ha : Nonempty α] :
(∀ x, p x) → b ↔ ∃ x, p x → b := by
classical
let ⟨a⟩ := ha
refine ⟨fun h ↦ Decidable.not_forall_not.1 fun h' ↦ ?_, fun ⟨x, hx⟩ h ↦ hx (h x)⟩
exact if hb : b then h' a fun _ ↦ hb else hb <| h fun x ↦ (Decidable.not_imp_iff_and_not.1 (h' x)).1
end replaceWithGrind
section introMerge
set_option linter.tacticAnalysis.introMerge true
set_option linter.unusedVariables false
/--
warning: Try this: intro a b
Note: This linter can be disabled with `set_option linter.tacticAnalysis.introMerge false`
-/
#guard_msgs in
example : ∀ a b : Unit, a = b := by
intro a
intro b
rfl
/--
warning: Try this: intro _ b
Note: This linter can be disabled with `set_option linter.tacticAnalysis.introMerge false`
-/
#guard_msgs in
example : ∀ a b : Unit, a = b := by
intro
intro b
rfl
/--
warning: Try this: intro a _
Note: This linter can be disabled with `set_option linter.tacticAnalysis.introMerge false`
-/
#guard_msgs in
example : ∀ a b : Unit, a = b := by
intro a
intro _
rfl
/--
warning: Try this: intro a b
Note: This linter can be disabled with `set_option linter.tacticAnalysis.introMerge false`
-/
#guard_msgs in
example : ∀ a b : Unit, a = b := by
intros a
intros b
rfl
/--
warning: Try this: intro a b c
Note: This linter can be disabled with `set_option linter.tacticAnalysis.introMerge false`
-/
#guard_msgs in
example : ∀ a b c : Unit, True := by
intro a
intros b c
trivial
/--
warning: Try this: intro a b
Note: This linter can be disabled with `set_option linter.tacticAnalysis.introMerge false`
-/
#guard_msgs in
example : ∀ a b : Unit, a = b := by
rintro a
rintro b
rfl
/--
warning: Try this: intro a _ c
Note: This linter can be disabled with `set_option linter.tacticAnalysis.introMerge false`
-/
#guard_msgs in
example : ∀ a b c : Unit, True := by
intro a
rintro _ c
trivial
#guard_msgs in
example : ∀ a b : Unit, a = b := by
intro a b
rfl
#guard_msgs in
example : ∀ a b : Unit, a = b := by
intros a b
rfl
#guard_msgs in
example : ∀ a b : Unit, a = b := by
rintro a b
rfl
#guard_msgs in
example : ∀ a b c : Unit, True := by
intro a
intros
trivial
#guard_msgs in
example : (Unit × Unit) → True := by
rintro ⟨a, b⟩
trivial
#guard_msgs in
example : Unit ⊕ Unit → True := by
rintro (a | b)
all_goals trivial
#guard_msgs in
example : True → True := by
rintro (h : True)
trivial
#guard_msgs in
example : ∀ a : Unit, a = () → True := by
rintro a rfl
trivial
#guard_msgs in
example : ∀ a : Unit, a = () → True := by
intro a
rintro rfl
trivial
-- Intros separated by an intervening tactic should NOT be merged.
-- Regression test for a bug where tactics were incorrectly grouped across intervening tactics.
#guard_msgs in
example : True → ∀ n > 0, True := by
intro h
have := 0
intro n hn
trivial
#guard_msgs in
example : True → ∀ a b : Unit, True := by
intros h
have := 0
rintro a b
trivial
end introMerge
section tryAtEachStep
-- Disable timing in tests to avoid non-deterministic output
set_option linter.tacticAnalysis.tryAtEachStep.showTiming false
section
set_option linter.tacticAnalysis.tryAtEachStepGrind true
/--
info: `rfl` can be replaced with `grind`
Note: This linter can be disabled with `set_option linter.tacticAnalysis.tryAtEachStepGrind false`
-/
#guard_msgs in
example : 1 + 1 = 2 := by
rfl
/--
warning: Unused tactic linter: `skip` does not change the goal.
Note: This linter can be disabled with `set_option linter.unusedTactic false`
---
info: `skip` (+1 later steps) can be replaced with `grind`
Note: This linter can be disabled with `set_option linter.tacticAnalysis.tryAtEachStepGrind false`
---
info: `rfl` can be replaced with `grind`
Note: This linter can be disabled with `set_option linter.tacticAnalysis.tryAtEachStepGrind false`
-/
#guard_msgs in
example : 1 + 1 = 2 := by
skip
rfl
end
section
def P (_ : Nat) := True
theorem p : P 37 := trivial
set_library_suggestions fun _ _ => pure #[{ name := `p, score := 1.0 }]
example : P 37 := by
grind +suggestions
/--
info: Try this:
[apply] simp_all only [p]
-/
#guard_msgs in
example : P 37 := by
simp_all? +suggestions
set_option linter.tacticAnalysis.tryAtEachStepGrindSuggestions true in
-- FIXME: why is the dagger here?
/--
info: `trivial` can be replaced with `grind +suggestions✝`
Note: This linter can be disabled with `set_option linter.tacticAnalysis.tryAtEachStepGrindSuggestions false`
-/
#guard_msgs in
example : P 37 := by
trivial
set_option linter.tacticAnalysis.tryAtEachStepSimpAllSuggestions true in
/--
info: `trivial` can be replaced with `simp_all? +suggestions✝`
Note: This linter can be disabled with `set_option linter.tacticAnalysis.tryAtEachStepSimpAllSuggestions false`
-/
#guard_msgs in
example : P 37 := by
trivial
end
end tryAtEachStep
section selfReplacements
set_option linter.tacticAnalysis.tryAtEachStep.showTiming false
set_option linter.tacticAnalysis.tryAtEachStepGrind true
-- With selfReplacements true (default), grind replacing grind is reported
/--
info: `grind` can be replaced with `grind`
Note: This linter can be disabled with `set_option linter.tacticAnalysis.tryAtEachStepGrind false`
-/
#guard_msgs in
example : 1 + 1 = 2 := by
grind
section
set_option linter.tacticAnalysis.tryAtEachStep.selfReplacements false
-- With selfReplacements false, grind replacing grind is NOT reported
#guard_msgs in
example : 1 + 1 = 2 := by
grind
-- Non-self replacements are still reported when selfReplacements is false
/--
info: `rfl` can be replaced with `grind`
Note: This linter can be disabled with `set_option linter.tacticAnalysis.tryAtEachStepGrind false`
-/
#guard_msgs in
example : 1 + 1 = 2 := by
rfl
end
end selfReplacements
section laterSteps
set_option linter.tacticAnalysis.tryAtEachStep.showTiming false
set_option linter.tacticAnalysis.tryAtEachStepGrind true
set_option linter.unusedTactic false
-- Test that later steps are counted correctly
/--
info: `skip` (+3 later steps) can be replaced with `grind`
Note: This linter can be disabled with `set_option linter.tacticAnalysis.tryAtEachStepGrind false`
---
info: `skip` (+2 later steps) can be replaced with `grind`
Note: This linter can be disabled with `set_option linter.tacticAnalysis.tryAtEachStepGrind false`
---
info: `skip` (+1 later steps) can be replaced with `grind`
Note: This linter can be disabled with `set_option linter.tacticAnalysis.tryAtEachStepGrind false`
---
info: `rfl` can be replaced with `grind`
Note: This linter can be disabled with `set_option linter.tacticAnalysis.tryAtEachStepGrind false`
-/
#guard_msgs in
example : 1 + 1 = 2 := by
skip
skip
skip
rfl
end laterSteps
section grindReplacement
set_option linter.tacticAnalysis.regressions.omegaToLia true
-- We should not complain about `omega` (and others) failing in a `try` context.
example : x = y := by
try omega
rfl
-- Example with more than one tactic step:
example : x = y := by
try
symm
symm
lia
rfl
set_option linter.unusedVariables false in
theorem create_a_few_goals (h1 : 1 + 1 = 2) (h2 : y = z) : x = y := rfl
set_option linter.unusedTactic false
-- We should not complain about `omega` (and others) failing in an `any_goals` context.
example : x = y := by
apply create_a_few_goals
any_goals omega
rfl
-- Example with more than one tactic step:
example : x = y := by
apply create_a_few_goals
any_goals
symm
symm
lia
rfl
end grindReplacement
section unknownTactic
-- Test that tryAtEachStepFromStrings gracefully handles unknown tactics
-- (e.g., trying to run `aesop` before Aesop is imported)
register_option linter.tacticAnalysis.unknownTacticTest : Bool := {
defValue := false
}
@[tacticAnalysis linter.tacticAnalysis.unknownTacticTest]
meta def unknownTacticTest :=
Mathlib.TacticAnalysis.tryAtEachStepFromStrings "nonexistent" "nonexistent_tactic_xyz"
-- This should not crash - the unknown tactic should be silently skipped
set_option linter.tacticAnalysis.unknownTacticTest true in
#guard_msgs in
example : 1 + 1 = 2 := by rfl
end unknownTactic
section verifyGrindSuggestions
-- Test that verifyGrind doesn't crash and doesn't report false positives
-- When grind? succeeds and its suggestion works, no warning should be produced
-- (Info messages from grind? are suppressed; only failures produce warnings)
#guard_msgs in
set_option linter.tacticAnalysis.verifyGrind true in
example : 1 + 1 = 2 := by
rfl
-- Test with grind? +suggestions
#guard_msgs in
set_option linter.tacticAnalysis.verifyGrindSuggestions true in
example : 1 + 1 = 2 := by
rfl
-- Test: a failure of an applied try-this suggestion should print a warning
section
open Lean Meta Elab Tactic Mathlib.TacticAnalysis
/-- Suggests to close a goal with `rfl`, but actually closes it with `trivial`. -/
elab "fakeRfl?" : tactic => do
Lean.Meta.Tactic.TryThis.addSuggestion (← getRef) (← `(tactic| rfl))
evalTactic (← `(tactic| trivial))
@[tacticAnalysis linter.tacticAnalysis.dummy]
meta def verifyFakeRfl := verifyTryThisSuggestions
(fun _ _ => `(tactic| fakeRfl?))
"fakeRfl?"
/--
info: Try this:
[apply] rfl
---
warning: `fakeRfl?` suggestion failed: `rfl` did not close the goal
Note: This linter can be disabled with `set_option linter.tacticAnalysis.dummy false`
-/
#guard_msgs in
set_option linter.tacticAnalysis.dummy true in
example : True := by
fakeRfl?
end
end verifyGrindSuggestions
¤ Dauer der Verarbeitung: 0.24 Sekunden
¤
*© Formatika GbR, Deutschland
|
|
2026-11-24
|
|
|
|
|