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.Translate.ToDual

17 declarations. Mathlib docs · Source

The @[to_dual] attribute.

The @[to_dual] attribute is used to translate declarations to their dual equivalent. See the docstrings of to_dual and to_additive for more information.

Known limitations:

  • When combining to_additive and to_dual, we need to make sure that all translations are added. For example attribute [to_dual (attr := to_additive) le_mul] mul_le should generate le_mul, le_add and add_le, and in particular should realize that le_add and add_le are dual to each other. Currently, this requires writing attribute [to_dual existing le_add] add_le.
  • It is currently not possible for a constant to have multiple possible duals. This would be useful for constants that have orders on different types, such as Monotone f. If the domain and codomain of f are both dualized, then Monotone f is simply dual to itself. But there are also cases where only the domain or only the codomain should be dualized. Then, Monotone f would be dual to Antitone f. We may also want this feature for dualizing results about bicategories.

def Mathlib.Tactic.ToDual.to_dual_ignore_args E1 #

Lean.ParserDescr

This attribute has been deprecated.

def Mathlib.Tactic.ToDual.to_dual_do_translate E1 #

Lean.ParserDescr

The global do_translate/dont_translate attributes specify whether operations on a given type should be translated. dont_translate can be used for types that are translated, such as MonoidAlgebra -> AddMonoidAlgebra, or for fixed types, such as Fin n/ZMod n. do_translate is for types without arguments, like Unit and Empty, where the structure on it can be translated.

Note: The name generation is not aware of dont_translate, so if some part of a lemma is not translated thanks to this, you generally have to specify the translated name manually.

def Mathlib.Tactic.ToDual.to_dual_dont_translate E1 #

Lean.ParserDescr

The global do_translate/dont_translate attributes specify whether operations on a given type should be translated. dont_translate can be used for types that are translated, such as MonoidAlgebra -> AddMonoidAlgebra, or for fixed types, such as Fin n/ZMod n. do_translate is for types without arguments, like Unit and Empty, where the structure on it can be translated.

Note: The name generation is not aware of dont_translate, so if some part of a lemma is not translated thanks to this, you generally have to specify the translated name manually.

def Mathlib.Tactic.ToDual.to_dual E1 #

Lean.ParserDescr

The attribute to_dual can be used to automatically transport theorems and definitions (but not inductive types and structures) to their dual version. It uses the same implementation as to_additive.

To use this attribute, just write:

@[to_dual]
theorem max_comm' {α} [LinearOrder α] (x y : α) : max x y = max y x := max_comm x y

This code will generate a theorem named min_comm'. It is also possible to manually specify the name of the new declaration:

@[to_dual le_max_left]
lemma min_le_left (a b : α) : min a b ≤ a := sorry

An existing documentation string will not be automatically used, so if the theorem or definition has a doc string, a doc string for the dual version should be passed explicitly to to_dual.

/-- The maximum is commutative. -/
@[to_dual /-- The minimum is commutative. -/]
theorem max_comm' {α} [LinearOrder α] (x y : α) : max x y = max y x := max_comm x y

Use the (reorder := ...) syntax to reorder the arguments compared to the dual declaration. This is specified using cycle notation. For example (reorder := α β, 5 6) swaps the arguments α and β with each other and the fifth and the sixth argument and (reorder := 3 4 5) will move the fifth argument before the third argument. For example, this is used when tagging LE.le with to_dual self (reorder := 3 4), so that a ≤ b gets transformed into b ≤ a.

Use the to_dual self syntax to mark the lemma as its own dual. This is needed if the lemma is its own dual, up to a reordering of its arguments. to_dual self (and to_dual existing) tries to autogenerate the (reorder := ...) argument, so it is usually not necessary to give it explicitly.

Use the to_dual existing syntax to use an existing dual declaration, instead of automatically generating it.

Use the (attr := ...) syntax to apply attributes to both the original and the dual version:

@[to_dual (attr := simp)] lemma min_self (a : α) : min a a = a := sorry

The reassoc attribute in category theory interacts with to_dual in a unique way, because it generates _assoc theorems that aren't dual to any other theorem. To deal with this, the reassoc attribute will add a to_dual none tag to an _assoc theorem if the original theorem was already tagged with to_dual. This also works with to_dual (attr := reassoc).

The (rename := ...) syntax can be used for specifying the argument names of the generated declaration, overriding the automatic translation of names. For example, (rename := x → a, y ↔ z) will translate lemma min_foo (x y z : α) ... to lemma max_foo (a z y : α) ....

Some definitions are dual to something other than the dual of their value. Some examples:

  • Ico a b := { x | a ≤ x ∧ x < b } is dual to Ioc b a := { x | b < x ∧ x ≤ a }.
  • Monotone f := ∀ ⦃a b⦄, a ≤ b → f a ≤ f b is dual to itself.
  • DecidableLE α := ∀ a b : α, Decidable (a ≤ b) is dual to itself.

To be able to translate a term involving such constants, to_dual needs to insert casts, so that the term's correctness doesn't rely on unfolding them. You can instruct to_dual to do this using the to_dual_insert_cast or to_dual_insert_cast_fun commands.

When troubleshooting to_dual, you can see what it is doing by replacing it with to_dual? and/or by using set_option trace.translate_detail true.

def Mathlib.Tactic.ToDual.attrTo_dual?_ E1 #

Lean.ParserDescr

The attribute to_dual can be used to automatically transport theorems and definitions (but not inductive types and structures) to their dual version. It uses the same implementation as to_additive.

To use this attribute, just write:

@[to_dual]
theorem max_comm' {α} [LinearOrder α] (x y : α) : max x y = max y x := max_comm x y

This code will generate a theorem named min_comm'. It is also possible to manually specify the name of the new declaration:

@[to_dual le_max_left]
lemma min_le_left (a b : α) : min a b ≤ a := sorry

An existing documentation string will not be automatically used, so if the theorem or definition has a doc string, a doc string for the dual version should be passed explicitly to to_dual.

/-- The maximum is commutative. -/
@[to_dual /-- The minimum is commutative. -/]
theorem max_comm' {α} [LinearOrder α] (x y : α) : max x y = max y x := max_comm x y

Use the (reorder := ...) syntax to reorder the arguments compared to the dual declaration. This is specified using cycle notation. For example (reorder := α β, 5 6) swaps the arguments α and β with each other and the fifth and the sixth argument and (reorder := 3 4 5) will move the fifth argument before the third argument. For example, this is used when tagging LE.le with to_dual self (reorder := 3 4), so that a ≤ b gets transformed into b ≤ a.

Use the to_dual self syntax to mark the lemma as its own dual. This is needed if the lemma is its own dual, up to a reordering of its arguments. to_dual self (and to_dual existing) tries to autogenerate the (reorder := ...) argument, so it is usually not necessary to give it explicitly.

Use the to_dual existing syntax to use an existing dual declaration, instead of automatically generating it.

Use the (attr := ...) syntax to apply attributes to both the original and the dual version:

@[to_dual (attr := simp)] lemma min_self (a : α) : min a a = a := sorry

The reassoc attribute in category theory interacts with to_dual in a unique way, because it generates _assoc theorems that aren't dual to any other theorem. To deal with this, the reassoc attribute will add a to_dual none tag to an _assoc theorem if the original theorem was already tagged with to_dual. This also works with to_dual (attr := reassoc).

The (rename := ...) syntax can be used for specifying the argument names of the generated declaration, overriding the automatic translation of names. For example, (rename := x → a, y ↔ z) will translate lemma min_foo (x y z : α) ... to lemma max_foo (a z y : α) ....

Some definitions are dual to something other than the dual of their value. Some examples:

  • Ico a b := { x | a ≤ x ∧ x < b } is dual to Ioc b a := { x | b < x ∧ x ≤ a }.
  • Monotone f := ∀ ⦃a b⦄, a ≤ b → f a ≤ f b is dual to itself.
  • DecidableLE α := ∀ a b : α, Decidable (a ≤ b) is dual to itself.

To be able to translate a term involving such constants, to_dual needs to insert casts, so that the term's correctness doesn't rely on unfolding them. You can instruct to_dual to do this using the to_dual_insert_cast or to_dual_insert_cast_fun commands.

When troubleshooting to_dual, you can see what it is doing by replacing it with to_dual? and/or by using set_option trace.translate_detail true.

opaque Mathlib.Tactic.ToDual.unfoldBoundaries E1 #

Mathlib.Tactic.UnfoldBoundary.UnfoldBoundaryExt

The insert_cast/insert_cast_fun attributes create an abstraction boundary for the tagged constant when translating it. For example, Set.Icc, Monotone, DecidableLT, WCovBy are all morally self-dual, but their definition is not self-dual. So, in order to allow these constants to be self-dual, we need to not unfold their definition in the proof term that we translate.

opaque Mathlib.Tactic.ToDual.dontTranslateAttr E1 #

Lean.NameMapExtension Unit

The global do_translate/dont_translate attributes specify whether operations on a given type should be translated. dont_translate can be used for types that are translated, such as MonoidAlgebra -> AddMonoidAlgebra, or for fixed types, such as Fin n/ZMod n. do_translate is for types without arguments, like Unit and Empty, where the structure on it can be translated.

Note: The name generation is not aware of dont_translate, so if some part of a lemma is not translated thanks to this, you generally have to specify the translated name manually.

opaque Mathlib.Tactic.ToDual.translations E1 #

Lean.NameMapExtension Mathlib.Tactic.Translate.TranslationInfo

Maps names to their dual counterparts.

def Mathlib.Tactic.ToDual.nameDict E1 #

Std.HashMap String (List String)

Dictionary used by guessName to autogenerate names. This only transforms single name components, unlike abbreviationDict.

Note: guessName capitalizes the output according to the capitalization of the input. In order for this to work, the input should always start with a lower case letter, and the output should always start with an upper case letter.

def Mathlib.Tactic.ToDual.abbreviationDict E1 #

Std.HashMap String String

We need to fix a few abbreviations after applying nameDict, i.e. replacing ZeroLE by Nonneg. This dictionary contains these fixes. The input should contain entries that is in lowerCamelCase (e.g. ltzero; the initial sequence of capital letters should be lower-cased) and the output should be in UpperCamelCase (e.g. LTZero). When applying the dictionary, we lower-case the output if the input was also given in lower-case.

opaque Mathlib.Tactic.ToDual.guessNameExt E1 #

Mathlib.Tactic.GuessName.GuessNameExt

Environment extension used for guessing the translation of a name.

def Mathlib.Tactic.ToDual.data E1 #

Mathlib.Tactic.Translate.TranslateData

The bundle of environment extensions for to_dual

def Mathlib.Tactic.ToDual.«commandTo_dual_insert_cast_:=_» E1 #

Lean.ParserDescr

The to_dual_insert_cast attribute is used to tag declarations foo that should not be unfolded in a proof that is translated. Instead, a rewrite with an equality theorem is inserted. This equality theorem can then be translated by the translation attribute.

def Mathlib.Tactic.ToDual.«commandTo_dual_insert_cast_fun_:=_,_» E1 #

Lean.ParserDescr

The to_dual_insert_cast_fun attribute is used to tag types that should not be unfolded in a proof that is translated. Instead, a casting function is inserted. This casting function then may be translated by the translation attribute.

def Mathlib.Tactic.ToDual.commandInsert_to_dual_translation__ E1 #

Lean.ParserDescr

insert_to_dual_translation name dualName inserts the translation name ↔ dualName into the to_dual dictionary. This is useful for translating namespaces that don't (yet) have a corresponding translated declaration.

def Mathlib.Tactic.ToDual.«commandTo_dual_name_hint__,,» E1 #

Lean.ParserDescr

to_dual_name_hint src₁ tgt₁, ..., srcₙ tgtₙ lets to_dual translate between the name segments srcᵢ and tgtᵢ for the rest of the file current. The name segments should be capitalized.

def Mathlib.Tactic.ToDual.«commandTo_dual_for_:=_» E1 #

Lean.ParserDescr

to_dual_for src := e tells to_dual to translate the constant src to e, where e can be an arbitrary expression. To see the expected type of e and/or provide a tactic proof, use to_dual_for src := by ...

TODO: this currently doesn't accept the (dont_translate := ...)/(relevant_arg := ...) syntax. This can be added if necessary.