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.Widget.LibraryRewrite

71 declarations. Mathlib docs · Source

Point & click library rewriting

This file defines rw??, an interactive tactic that suggests rewrites for any expression selected by the user.

rw?? uses a (lazy) RefinedDiscrTree to lookup a list of candidate rewrite lemmas. It excludes lemmas that are automatically generated. Each lemma is then checked one by one to see whether it is applicable. For each lemma that works, the corresponding rewrite tactic is constructed and converted into a String that fits inside mathlib's 100 column limit, so that it can be pasted into the editor when selected by the user.

The RefinedDiscrTree lookup groups the results by match pattern and gives a score to each pattern. This is used to display the results in sections. The sections are ordered by this score. Within each section, the lemmas are sorted by

  • rewrites with fewer extra goals come first
  • left-to-right rewrites come first
  • shorter lemma names come first
  • shorter replacement expressions come first (when taken as a string)
  • alphabetically ordered by lemma name

The lemmas are optionally filtered to avoid duplicate rewrites, or trivial rewrites. This is controlled by the filter button on the top right of the results.

When a rewrite lemma introduces new goals, these are shown after a ⊢.

TODO

Ways to improve rw??:

  • Improve the logic around nth_rw and occurrences, and about when to pass explicit arguments to the rewrite lemma. For example, we could only pass explicit arguments if that avoids using nth_rw. Performance may be a limiting factor for this. Currently, the occurrence is computed by viewKAbstractSubExpr.
  • Modify the interface to allow creating a whole rw [.., ..] chain, without having to go into the editor in between. For this to work, we will need a more general syntax, something like rw [..]??, which would be pasted into the editor.
  • We could look for rewrites of partial applications of the selected expression. For example, when clicking on (f + g) x, there should still be an add_comm suggestion.

Ways to extend rw??:

  • Support generalized rewriting (grw)
  • Integrate rewrite search with the calc? widget so that a calc block can be created using just point & click.

Caching

Computing the Rewrites

Rewriting by hypotheses

Filtering out duplicate lemmas

User interface

theorem Mathlib.Tactic.LibraryRewrite.Rewrite.mk.inj E1 #

∀ {symm : Bool} {proof replacement : Lean.Expr} {stringLength : ℕ} {extraGoals : Array (Lean.MVarId × Lean.BinderInfo)}
  {makesNewMVars symm_1 : Bool} {proof_1 replacement_1 : Lean.Expr} {stringLength_1 : ℕ}
  {extraGoals_1 : Array (Lean.MVarId × Lean.BinderInfo)} {makesNewMVars_1 : Bool},
  { symm := symm, proof := proof, replacement := replacement, stringLength := stringLength, extraGoals := extraGoals,
        makesNewMVars := makesNewMVars } =
      { symm := symm_1, proof := proof_1, replacement := replacement_1, stringLength := stringLength_1,
        extraGoals := extraGoals_1, makesNewMVars := makesNewMVars_1 } →
    symm = symm_1 ∧
      proof = proof_1 ∧
        replacement = replacement_1 ∧
          stringLength = stringLength_1 ∧ extraGoals = extraGoals_1 ∧ makesNewMVars = makesNewMVars_1

def Mathlib.Tactic.LibraryRewrite.Kind.ctorIdx E1 #

Mathlib.Tactic.LibraryRewrite.Kind → ℕ

def Mathlib.Tactic.LibraryRewrite.RewriteLemma.ctorIdx E1 #

Mathlib.Tactic.LibraryRewrite.RewriteLemma → ℕ

def Mathlib.Tactic.LibraryRewrite.Rewrite.ctorIdx E1 #

Mathlib.Tactic.LibraryRewrite.Rewrite → ℕ

abbrev Mathlib.Tactic.LibraryRewrite.Kind.ctorElimType E1 #

{motive : Mathlib.Tactic.LibraryRewrite.Kind → Sort u} → ℕ → Sort (max 1 u)

theorem Mathlib.Tactic.LibraryRewrite.RewriteLemma.mk.inj E1 #

∀ {name : Lean.Name} {symm : Bool} {name_1 : Lean.Name} {symm_1 : Bool},
  { name := name, symm := symm } = { name := name_1, symm := symm_1 } → name = name_1 ∧ symm = symm_1

theorem Mathlib.Tactic.LibraryRewrite.RewriteInterface.mk.injEq E0 #

∀ (symm : Bool) (tactic : String) (replacement : Lean.Expr) (replacementString : String)
  (extraGoals : Array Lean.Widget.CodeWithInfos) (prettyLemma : Lean.Widget.CodeWithInfos) (lemmaType : Lean.Expr)
  (makesNewMVars symm_1 : Bool) (tactic_1 : String) (replacement_1 : Lean.Expr) (replacementString_1 : String)
  (extraGoals_1 : Array Lean.Widget.CodeWithInfos) (prettyLemma_1 : Lean.Widget.CodeWithInfos) (lemmaType_1 : Lean.Expr)
  (makesNewMVars_1 : Bool),
  ({ symm := symm, tactic := tactic, replacement := replacement, replacementString := replacementString,
        extraGoals := extraGoals, prettyLemma := prettyLemma, lemmaType := lemmaType, makesNewMVars := makesNewMVars } =
      { symm := symm_1, tactic := tactic_1, replacement := replacement_1, replacementString := replacementString_1,
        extraGoals := extraGoals_1, prettyLemma := prettyLemma_1, lemmaType := lemmaType_1,
        makesNewMVars := makesNewMVars_1 }) =
    (symm = symm_1 ∧
      tactic = tactic_1 ∧
        replacement = replacement_1 ∧
          replacementString = replacementString_1 ∧
            extraGoals = extraGoals_1 ∧
              prettyLemma = prettyLemma_1 ∧ lemmaType = lemmaType_1 ∧ makesNewMVars = makesNewMVars_1)

def Mathlib.Tactic.LibraryRewrite.instInhabitedRewriteLemma.default E1 #

Mathlib.Tactic.LibraryRewrite.RewriteLemma

theorem Mathlib.Tactic.LibraryRewrite.RewriteLemma.mk.injEq E0 #

∀ (name : Lean.Name) (symm : Bool) (name_1 : Lean.Name) (symm_1 : Bool),
  ({ name := name, symm := symm } = { name := name_1, symm := symm_1 }) = (name = name_1 ∧ symm = symm_1)

theorem Mathlib.Tactic.LibraryRewrite.RewriteInterface.mk.inj E1 #

∀ {symm : Bool} {tactic : String} {replacement : Lean.Expr} {replacementString : String}
  {extraGoals : Array Lean.Widget.CodeWithInfos} {prettyLemma : Lean.Widget.CodeWithInfos} {lemmaType : Lean.Expr}
  {makesNewMVars symm_1 : Bool} {tactic_1 : String} {replacement_1 : Lean.Expr} {replacementString_1 : String}
  {extraGoals_1 : Array Lean.Widget.CodeWithInfos} {prettyLemma_1 : Lean.Widget.CodeWithInfos} {lemmaType_1 : Lean.Expr}
  {makesNewMVars_1 : Bool},
  { symm := symm, tactic := tactic, replacement := replacement, replacementString := replacementString,
        extraGoals := extraGoals, prettyLemma := prettyLemma, lemmaType := lemmaType, makesNewMVars := makesNewMVars } =
      { symm := symm_1, tactic := tactic_1, replacement := replacement_1, replacementString := replacementString_1,
        extraGoals := extraGoals_1, prettyLemma := prettyLemma_1, lemmaType := lemmaType_1,
        makesNewMVars := makesNewMVars_1 } →
    symm = symm_1 ∧
      tactic = tactic_1 ∧
        replacement = replacement_1 ∧
          replacementString = replacementString_1 ∧
            extraGoals = extraGoals_1 ∧
              prettyLemma = prettyLemma_1 ∧ lemmaType = lemmaType_1 ∧ makesNewMVars = makesNewMVars_1

theorem Mathlib.Tactic.LibraryRewrite.Rewrite.mk.injEq E0 #

∀ (symm : Bool) (proof replacement : Lean.Expr) (stringLength : ℕ) (extraGoals : Array (Lean.MVarId × Lean.BinderInfo))
  (makesNewMVars symm_1 : Bool) (proof_1 replacement_1 : Lean.Expr) (stringLength_1 : ℕ)
  (extraGoals_1 : Array (Lean.MVarId × Lean.BinderInfo)) (makesNewMVars_1 : Bool),
  ({ symm := symm, proof := proof, replacement := replacement, stringLength := stringLength, extraGoals := extraGoals,
        makesNewMVars := makesNewMVars } =
      { symm := symm_1, proof := proof_1, replacement := replacement_1, stringLength := stringLength_1,
        extraGoals := extraGoals_1, makesNewMVars := makesNewMVars_1 }) =
    (symm = symm_1 ∧
      proof = proof_1 ∧
        replacement = replacement_1 ∧
          stringLength = stringLength_1 ∧ extraGoals = extraGoals_1 ∧ makesNewMVars = makesNewMVars_1)

def Mathlib.Tactic.LibraryRewrite.RewriteInterface.ctorIdx E1 #

Mathlib.Tactic.LibraryRewrite.RewriteInterface → ℕ

structure Mathlib.Tactic.LibraryRewrite.RewriteLemma E1 #

Type

The structure for rewrite lemmas stored in the RefinedDiscrTree.

ctor Mathlib.Tactic.LibraryRewrite.RewriteLemma.mk E0 #

Lean.Name → Bool → Mathlib.Tactic.LibraryRewrite.RewriteLemma

abbrev Mathlib.Tactic.LibraryRewrite.RewriteLemma.name E1 #

Mathlib.Tactic.LibraryRewrite.RewriteLemma → Lean.Name

The name of the lemma

abbrev Mathlib.Tactic.LibraryRewrite.RewriteLemma.symm E1 #

Mathlib.Tactic.LibraryRewrite.RewriteLemma → Bool

symm is true when rewriting from right to left

instance Mathlib.Tactic.LibraryRewrite.instInhabitedRewriteLemma E0 #

Inhabited Mathlib.Tactic.LibraryRewrite.RewriteLemma

def Mathlib.Tactic.LibraryRewrite.instBEqRewriteLemma.beq E1 #

Mathlib.Tactic.LibraryRewrite.RewriteLemma → Mathlib.Tactic.LibraryRewrite.RewriteLemma → Bool

instance Mathlib.Tactic.LibraryRewrite.instBEqRewriteLemma E0 #

BEq Mathlib.Tactic.LibraryRewrite.RewriteLemma

instance Mathlib.Tactic.LibraryRewrite.instToFormatRewriteLemma E0 #

Std.ToFormat Mathlib.Tactic.LibraryRewrite.RewriteLemma

def Mathlib.Tactic.LibraryRewrite.isMVarSwap E1 #

Lean.Expr → Lean.Expr → Bool

Return true if s and t are equal up to changing the MVarIds.

def Mathlib.Tactic.LibraryRewrite.eqOrIff? E1 #

Lean.Expr → Option (Lean.Expr × Lean.Expr)

Extract the left and right-hand sides of an equality or iff statement.

def Mathlib.Tactic.LibraryRewrite.addRewriteEntry E1 #

Lean.Name →
  Lean.ConstantInfo →
    Lean.MetaM
      (List
        (Mathlib.Tactic.LibraryRewrite.RewriteLemma ×
          List (Lean.Meta.RefinedDiscrTree.Key × Lean.Meta.RefinedDiscrTree.LazyEntry)))

Try adding the lemma to the RefinedDiscrTree.

def Mathlib.Tactic.LibraryRewrite.addLocalRewriteEntry E1 #

Lean.LocalDecl →
  Lean.MetaM
    (List ((Lean.FVarId × Bool) × List (Lean.Meta.RefinedDiscrTree.Key × Lean.Meta.RefinedDiscrTree.LazyEntry)))

Try adding the local hypothesis to the RefinedDiscrTree.

def Mathlib.Tactic.LibraryRewrite.getImportCandidates E1 #

Lean.Expr → Lean.MetaM (Array (Array Mathlib.Tactic.LibraryRewrite.RewriteLemma))

Get all potential rewrite lemmas from the imported environment. By setting the librarySearch.excludedModules option, all lemmas from certain modules can be excluded.

def Mathlib.Tactic.LibraryRewrite.getModuleCandidates E1 #

Lean.Expr → Lean.MetaM (Array (Array Mathlib.Tactic.LibraryRewrite.RewriteLemma))

Get all potential rewrite lemmas from the current file. Exclude lemmas from modules in the librarySearch.excludedModules option.

structure Mathlib.Tactic.LibraryRewrite.Rewrite E1 #

Type

A rewrite lemma that has been applied to an expression.

ctor Mathlib.Tactic.LibraryRewrite.Rewrite.mk E0 #

Bool → Lean.Expr → Lean.Expr → ℕ → Array (Lean.MVarId × Lean.BinderInfo) → Bool → Mathlib.Tactic.LibraryRewrite.Rewrite

abbrev Mathlib.Tactic.LibraryRewrite.Rewrite.symm E1 #

Mathlib.Tactic.LibraryRewrite.Rewrite → Bool

symm is true when rewriting from right to left

abbrev Mathlib.Tactic.LibraryRewrite.Rewrite.proof E1 #

Mathlib.Tactic.LibraryRewrite.Rewrite → Lean.Expr

The proof of the rewrite

abbrev Mathlib.Tactic.LibraryRewrite.Rewrite.replacement E1 #

Mathlib.Tactic.LibraryRewrite.Rewrite → Lean.Expr

The replacement expression obtained from the rewrite

abbrev Mathlib.Tactic.LibraryRewrite.Rewrite.stringLength E1 #

Mathlib.Tactic.LibraryRewrite.Rewrite → ℕ

The size of the replacement when printed

abbrev Mathlib.Tactic.LibraryRewrite.Rewrite.extraGoals E1 #

Mathlib.Tactic.LibraryRewrite.Rewrite → Array (Lean.MVarId × Lean.BinderInfo)

The extra goals created by the rewrite

abbrev Mathlib.Tactic.LibraryRewrite.Rewrite.makesNewMVars E1 #

Mathlib.Tactic.LibraryRewrite.Rewrite → Bool

Whether the rewrite introduces a new metavariable in the replacement expression.

def Mathlib.Tactic.LibraryRewrite.checkRewrite E1 #

Lean.Expr → Lean.Expr → Bool → Lean.MetaM (Option Mathlib.Tactic.LibraryRewrite.Rewrite)

If thm can be used to rewrite e, return the rewrite.

def Mathlib.Tactic.LibraryRewrite.checkAndSortRewriteLemmas E1 #

Lean.Expr →
  Array Mathlib.Tactic.LibraryRewrite.RewriteLemma →
    Lean.MetaM (Array (Mathlib.Tactic.LibraryRewrite.Rewrite × Lean.Name))

Try to rewrite e with each of the rewrite lemmas, and sort the resulting rewrites.

def Mathlib.Tactic.LibraryRewrite.getImportRewrites E1 #

Lean.Expr → Lean.MetaM (Array (Array (Mathlib.Tactic.LibraryRewrite.Rewrite × Lean.Name)))

Return all applicable library rewrites of e. Note that the result may contain duplicate rewrites. These can be removed with filterRewrites.

def Mathlib.Tactic.LibraryRewrite.getModuleRewrites E1 #

Lean.Expr → Lean.MetaM (Array (Array (Mathlib.Tactic.LibraryRewrite.Rewrite × Lean.Name)))

Same as getImportRewrites, but for lemmas from the current file.

def Mathlib.Tactic.LibraryRewrite.getHypotheses E1 #

Option Lean.FVarId → Lean.MetaM (Lean.Meta.RefinedDiscrTree (Lean.FVarId × Bool))

Construct the RefinedDiscrTree of all local hypotheses.

def Mathlib.Tactic.LibraryRewrite.getHypothesisRewrites E1 #

Lean.Expr → Option Lean.FVarId → Lean.MetaM (Array (Array (Mathlib.Tactic.LibraryRewrite.Rewrite × Lean.FVarId)))

Return all applicable hypothesis rewrites of e. Similar to getImportRewrites.

def Mathlib.Tactic.LibraryRewrite.getBinderInfos E1 #

Lean.Expr → Array Lean.Expr → Lean.MetaM (Array Lean.BinderInfo)

Get the BinderInfos for the arguments of mkAppN fn args.

opaque Mathlib.Tactic.LibraryRewrite.isExplicitEq E1 #

Lean.Expr → Lean.Expr → Lean.MetaM Bool

Determine whether the explicit parts of two expressions are equal, and the implicit parts are definitionally equal.

def Mathlib.Tactic.LibraryRewrite.filterRewrites E1 #

{α : Type} → Lean.Expr → Array α → (α → Lean.Expr) → (α → Bool) → Lean.MetaM (Array α)

Filter out duplicate rewrites, reflexive rewrites or rewrites that have metavariables in the replacement expression.

def Mathlib.Tactic.LibraryRewrite.mkRewrite E1 #

Option ℕ → Bool → Lean.Term → Option Lean.Name → Lean.CoreM (Lean.TSyntax `tactic)

Return syntax for the rewrite tactic rw [e].

def Mathlib.Tactic.LibraryRewrite.tacticPasteString E1 #

Lean.TSyntax `tactic → Lean.Lsp.Range → Lean.CoreM String

Given tactic syntax tac that we want to paste into the editor, return it as a string. This function respects the 100 character limit for long lines.

def Mathlib.Tactic.LibraryRewrite.tacticSyntax E1 #

Mathlib.Tactic.LibraryRewrite.Rewrite → Option ℕ → Option Lean.Name → Lean.MetaM (Lean.TSyntax `tactic)

Return the rewrite tactic that performs the rewrite.

structure Mathlib.Tactic.LibraryRewrite.RewriteInterface E1 #

Type

The structure with all data necessary for rendering a rewrite suggestion

ctor Mathlib.Tactic.LibraryRewrite.RewriteInterface.mk E0 #

Bool →
  String →
    Lean.Expr →
      String →
        Array Lean.Widget.CodeWithInfos →
          Lean.Widget.CodeWithInfos → Lean.Expr → Bool → Mathlib.Tactic.LibraryRewrite.RewriteInterface

abbrev Mathlib.Tactic.LibraryRewrite.RewriteInterface.symm E1 #

Mathlib.Tactic.LibraryRewrite.RewriteInterface → Bool

symm is true when rewriting from right to left

abbrev Mathlib.Tactic.LibraryRewrite.RewriteInterface.tactic E1 #

Mathlib.Tactic.LibraryRewrite.RewriteInterface → String

The rewrite tactic string that performs the rewrite

abbrev Mathlib.Tactic.LibraryRewrite.RewriteInterface.replacement E1 #

Mathlib.Tactic.LibraryRewrite.RewriteInterface → Lean.Expr

The replacement expression obtained from the rewrite

abbrev Mathlib.Tactic.LibraryRewrite.RewriteInterface.replacementString E1 #

Mathlib.Tactic.LibraryRewrite.RewriteInterface → String

The replacement expression obtained from the rewrite

abbrev Mathlib.Tactic.LibraryRewrite.RewriteInterface.extraGoals E1 #

Mathlib.Tactic.LibraryRewrite.RewriteInterface → Array Lean.Widget.CodeWithInfos

The extra goals created by the rewrite

abbrev Mathlib.Tactic.LibraryRewrite.RewriteInterface.prettyLemma E1 #

Mathlib.Tactic.LibraryRewrite.RewriteInterface → Lean.Widget.CodeWithInfos

The lemma name with hover information

abbrev Mathlib.Tactic.LibraryRewrite.RewriteInterface.lemmaType E1 #

Mathlib.Tactic.LibraryRewrite.RewriteInterface → Lean.Expr

The type of the lemma

abbrev Mathlib.Tactic.LibraryRewrite.RewriteInterface.makesNewMVars E1 #

Mathlib.Tactic.LibraryRewrite.RewriteInterface → Bool

Whether the rewrite introduces new metavariables with the replacement.

def Mathlib.Tactic.LibraryRewrite.Rewrite.toInterface E1 #

Mathlib.Tactic.LibraryRewrite.Rewrite →
  Lean.Name ⊕ Lean.FVarId →
    Option ℕ → Option Lean.Name → Lean.Lsp.Range → Lean.MetaM Mathlib.Tactic.LibraryRewrite.RewriteInterface

Construct the RewriteInterface from a Rewrite.

inductive Mathlib.Tactic.LibraryRewrite.Kind E1 #

Type

The kind of rewrite

ctor Mathlib.Tactic.LibraryRewrite.Kind.hypothesis E0 #

Mathlib.Tactic.LibraryRewrite.Kind

A rewrite with a local hypothesis

ctor Mathlib.Tactic.LibraryRewrite.Kind.fromFile E0 #

Mathlib.Tactic.LibraryRewrite.Kind

A rewrite with a lemma from the current file

ctor Mathlib.Tactic.LibraryRewrite.Kind.fromCache E0 #

Mathlib.Tactic.LibraryRewrite.Kind

A rewrite with a lemma from an imported file

def Mathlib.Tactic.LibraryRewrite.getRewriteInterfaces E1 #

Lean.Expr →
  Option ℕ →
    Option Lean.Name →
      Option Lean.FVarId →
        Lean.Lsp.Range →
          Lean.MetaM
            (Array (Array Mathlib.Tactic.LibraryRewrite.RewriteInterface × Mathlib.Tactic.LibraryRewrite.Kind) ×
              Array (Array Mathlib.Tactic.LibraryRewrite.RewriteInterface × Mathlib.Tactic.LibraryRewrite.Kind))

Return the Interfaces for rewriting e, both filtered and unfiltered.

def Mathlib.Tactic.LibraryRewrite.pattern E1 #

{α : Type} → Lean.Expr → Bool → (Lean.Expr → Lean.MetaM α) → Lean.MetaM α

Render the matching side of the rewrite lemma. This is shown at the header of each section of rewrite results.

def Mathlib.Tactic.LibraryRewrite.renderRewrites E1 #

Lean.Expr →
  Array (Array Mathlib.Tactic.LibraryRewrite.RewriteInterface × Mathlib.Tactic.LibraryRewrite.Kind) →
    Lean.Lsp.Range → Lean.Server.FileWorker.EditableDocument → Bool → Lean.MetaM ProofWidgets.Html

Render the given rewrite results.

def Mathlib.Tactic.LibraryRewrite.rpc E1 #

SelectInsertParams → Lean.Server.RequestM (Lean.Server.RequestTask ProofWidgets.Html)

The rpc method of the rw?? widget.

def Mathlib.Tactic.LibraryRewrite.LibraryRewriteComponent E1 #

ProofWidgets.Component SelectInsertParams

The component called by the rw?? tactic

def Mathlib.Tactic.LibraryRewrite.tacticRw?? E1 #

Lean.ParserDescr

rw?? is an interactive tactic that suggests rewrites for any expression selected by the user. To use it, shift-click an expression in the goal or a hypothesis that you want to rewrite. Clicking on one of the rewrite suggestions will paste the relevant rewrite tactic into the editor.

The rewrite suggestions are grouped and sorted by the pattern that the rewrite lemmas match with. Rewrites that don't change the goal and rewrites that create the same goal as another rewrite are filtered out, as well as rewrites that have new metavariables in the replacement expression. To see all suggestions, click on the filter button (▼) in the top right.

def Mathlib.Tactic.LibraryRewrite.Rewrite.toMessageData E1 #

Mathlib.Tactic.LibraryRewrite.Rewrite → Lean.Name → Lean.MetaM Lean.MessageData

Represent a Rewrite as MessageData.

def Mathlib.Tactic.LibraryRewrite.SectionToMessageData E1 #

Array (Mathlib.Tactic.LibraryRewrite.Rewrite × Lean.Name) × Bool → Lean.MetaM (Option Lean.MessageData)

Represent a section of rewrites as MessageData.

def Mathlib.Tactic.LibraryRewrite.rw??Command E1 #

Lean.ParserDescr

#rw?? e gives all possible rewrites of e. It is a testing command for the rw?? tactic

def Mathlib.Tactic.LibraryRewrite.elabrw??Command E1 #

Lean.Elab.Command.CommandElab

Elaborate a #rw?? command.