The Mathlib Handbook

The volumes of the handbook are not written yet. This site shows the map of Mathlib that they will follow, at Mathlib 4472534b60.

The areas / V34. The tactics of Mathlib

Mathlib.Tactic.Linter.UnusedTactic

6 declarations. Mathlib docs · Source

The unused tactic linter

The unused linter makes sure that every tactic call actually changes something.

The inner workings of the linter are as follows.

The linter inspects the goals before and after each tactic execution. If they are not identical, the linter is happy. If they are identical, then the linter checks if the tactic is whitelisted. Possible reason for whitelisting are

  • tactics that emit messages, such as extract_goal or says;
  • tactics that are in place to assert something, such as guard;
  • tactics that allow to work on a specific goal, such as on_goal;
  • "flow control" tactics, such as success_if_fail and related.

Special cases

  • swap_var: the only change that swap_var makes is to rename local declarations. Thus, to check that swap_var was used, we inspect the names of all the local declarations before and after and see if there is some change.
  • try: the only thing try does is to do nothing if the inner tactic throws an error. So, to check that try was used, we check that the goals did not change.
  • <;>/all_goals/any_goals: if there is exactly 1 goal, then these are redundant. If there are no goals, then these could in principle not be redundant, but we lint against such cases as well. We also warn if any_goals can be replaced with all_goals, or with on_goal n =>.

Notable exclusions

  • The linter does not enter a "sequence tactic": upon finding tac <;> [tac1, tac2, ...] the linter assumes that the tactic is doing something and does not recurse into each tac1, tac2, .... This is just for lack of an implementation: it may not be hard to do this.

  • The tactic does not check the discharger for linear_combination, but checks linear_combination itself. The main reason is that skip is a common discharger tactic and the linter would then always fail whenever the user explicitly chose to pass skip as a discharger tactic.

TODO

  • The linter seems to be silenced by set_option ... in: maybe it should enter ins?

Implementation notes

Yet another linter copied from the unreachableTactic linter!

opaque Mathlib.Linter.linter.unusedTactic E1 #

Lean.Option Bool

The unused tactic linter makes sure that every tactic call actually changes something. It has special support for tactics <;>, try, all_goals, any_goals and swap_var.

def Mathlib.Linter.UnusedTactic.allowUnusedTactic E1 #

Lean.ParserDescr

allow_unused_tactic takes as input a space-separated list of identifiers. These identifiers are then allowed by the unused tactic linter: even if these tactics do not modify goals, there will be no warning emitted.

Note: for this to work, these identifiers should be the SyntaxNodeKind of each tactic.

For instance, you can allow the done and skip tactics using

allow_unused_tactic Lean.Parser.Tactic.done Lean.Parser.Tactic.skip

This change is global and persistent across imports. For a file-local or scoped change, use local allow_unused_tactic or scoped allow_unused_tactic, respectively.

The command #show_kind tac may help to find the SyntaxNodeKind.

def Mathlib.Linter.UnusedTactic.hashStx E1 #

Lean.ParserDescr

#allow_unused_tactic is deprecated in favour of allow_unused_tactic

def Mathlib.Linter.UnusedTactic.«command#show_kind_» E1 #

Lean.ParserDescr

#show_kind tac takes as input the syntax of a tactic and returns the SyntaxNodeKind at the head of the tactic syntax tree.

The input syntax needs to parse, though it can be extremely elided. For instance, to see the SyntaxNodeKind of the refine tactic, you could use

#show_kind refine _

The trailing underscore _ makes the syntax valid, since refine expects something else.

def Mathlib.Linter.UnusedTactic.isIgnoreTacticKind E1 #

Lean.NameHashSet → Lean.SyntaxNodeKind → Bool

Is this a syntax kind that contains intentionally unused tactic subterms?

def Mathlib.Linter.UnusedTactic.addIgnoreTacticKind E1 #

Lean.SyntaxNodeKind → BaseIO Unit

Adds a new syntax kind whose children will be ignored by the unusedTactic linter. This should be called from an initialize block.