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.Reorder

49 declarations. Mathlib docs · Source

Reordering arguments in a translation

This module defines reorders, which can be specified with to_dual (reorder := ...) or to_additive (reorder := ...), to deal with definitions and theorems that need to have their arguments and/or universe parameters reordered.

A reordering is specified using disjoint cycle notation. For example, 1 2 3, 4 5 will move the 1st argument to the 2nd, move the 2nd to the 3rd, and the 3rd to the 1st, and it will swap the 4th and 5th arguments.

Instead of using numbers to refer to argument, you can also refer to them by name. For example a b will swap the arguments named a and b. This is implemented in elabArgStx.

To specify reordering arguments of arguments, the syntax is recursive. For example, 4 (1 2) will reorder the first two arguments of the fourth argument.

If the declaration is translated to itself or to an existing declaration, the heuristic in guessReorder tries to predict the argument reorder. This is done with a syntactic comparison of which variable is moved where.

The universe reordering is always inferred automatically, using guessUnivReorder.

Examples

  • to_dual needs to swap the arguments of some definitions to translate them, such as a ≤ b ↦ b ≤ a and a ⇨ b ↦ b \ a. In these cases, we reorder the 3rd and 4th arguments, which can be specified using @[to_dual (reorder := 3 4)].

  • to_additive needs to swap the arguments when translating a ^ n ↦ n • a.

  • Some theorems are dual to themselves only after reordering some arguments. For example, le_total : ∀ a b : α, a ≤ b ∨ b ≤ a is dual to itself after swapping a and b. Thanks to the heuristic in guessReorder, it suffices to write @[to_dual self].

Implementation notes

Permutation are stored as their disjoint cycle representation (Permutation). This allows efficiently permuting an array/list of arguments/universes (Permutation.permute!/Permutation.permuteList!).

Reordering an expression

Guessing the reorder given the reordered expression

Syntax for specifying a reorder

def Mathlib.Tactic.Translate.instInhabitedArgReorder.default E1 #

Mathlib.Tactic.Translate.ArgReorder

def Mathlib.Tactic.Translate.Reorder.ctorIdx E1 #

Mathlib.Tactic.Translate.Reorder → ℕ

abbrev Mathlib.Tactic.Translate.ArgReorder.brecOn_2.go E1 #

{motive_1 : Mathlib.Tactic.Translate.ArgReorder → Sort u} →
  {motive_2 : Array (ℕ × Mathlib.Tactic.Translate.ArgReorder) → Sort u} →
    {motive_3 : List (ℕ × Mathlib.Tactic.Translate.ArgReorder) → Sort u} →
      {motive_4 : ℕ × Mathlib.Tactic.Translate.ArgReorder → Sort u} →
        (t : List (ℕ × Mathlib.Tactic.Translate.ArgReorder)) →
          ((t : Mathlib.Tactic.Translate.ArgReorder) → t.below → motive_1 t) →
            ((t : Array (ℕ × Mathlib.Tactic.Translate.ArgReorder)) →
                Mathlib.Tactic.Translate.ArgReorder.below_1 t → motive_2 t) →
              ((t : List (ℕ × Mathlib.Tactic.Translate.ArgReorder)) →
                  Mathlib.Tactic.Translate.ArgReorder.below_2 t → motive_3 t) →
                ((t : ℕ × Mathlib.Tactic.Translate.ArgReorder) →
                    Mathlib.Tactic.Translate.ArgReorder.below_3 t → motive_4 t) →
                  motive_3 t ×' Mathlib.Tactic.Translate.ArgReorder.below_2 t

theorem Mathlib.Tactic.Translate.ArgReorder.brecOn_2.eq E1 #

∀ {motive_1 : Mathlib.Tactic.Translate.ArgReorder → Sort u}
  {motive_2 : Array (ℕ × Mathlib.Tactic.Translate.ArgReorder) → Sort u}
  {motive_3 : List (ℕ × Mathlib.Tactic.Translate.ArgReorder) → Sort u}
  {motive_4 : ℕ × Mathlib.Tactic.Translate.ArgReorder → Sort u} (t : List (ℕ × Mathlib.Tactic.Translate.ArgReorder))
  (F_1 : (t : Mathlib.Tactic.Translate.ArgReorder) → t.below → motive_1 t)
  (F_2 :
    (t : Array (ℕ × Mathlib.Tactic.Translate.ArgReorder)) → Mathlib.Tactic.Translate.ArgReorder.below_1 t → motive_2 t)
  (F_3 :
    (t : List (ℕ × Mathlib.Tactic.Translate.ArgReorder)) → Mathlib.Tactic.Translate.ArgReorder.below_2 t → motive_3 t)
  (F_4 : (t : ℕ × Mathlib.Tactic.Translate.ArgReorder) → Mathlib.Tactic.Translate.ArgReorder.below_3 t → motive_4 t),
  Mathlib.Tactic.Translate.ArgReorder.brecOn_2 t F_1 F_2 F_3 F_4 =
    F_3 t (Mathlib.Tactic.Translate.ArgReorder.brecOn_2.go t F_1 F_2 F_3 F_4).2

theorem Mathlib.Tactic.Translate.ArgReorder.mk.injEq E0 #

∀ (perm : Mathlib.Tactic.Translate.Permutation) (argReorders : Array (ℕ × Mathlib.Tactic.Translate.ArgReorder))
  (perm_1 : Mathlib.Tactic.Translate.Permutation) (argReorders_1 : Array (ℕ × Mathlib.Tactic.Translate.ArgReorder)),
  ({ perm := perm, argReorders := argReorders } = { perm := perm_1, argReorders := argReorders_1 }) =
    (perm = perm_1 ∧ argReorders = argReorders_1)

theorem Mathlib.Tactic.Translate.ArgReorder.brecOn_1.eq E1 #

∀ {motive_1 : Mathlib.Tactic.Translate.ArgReorder → Sort u}
  {motive_2 : Array (ℕ × Mathlib.Tactic.Translate.ArgReorder) → Sort u}
  {motive_3 : List (ℕ × Mathlib.Tactic.Translate.ArgReorder) → Sort u}
  {motive_4 : ℕ × Mathlib.Tactic.Translate.ArgReorder → Sort u} (t : Array (ℕ × Mathlib.Tactic.Translate.ArgReorder))
  (F_1 : (t : Mathlib.Tactic.Translate.ArgReorder) → t.below → motive_1 t)
  (F_2 :
    (t : Array (ℕ × Mathlib.Tactic.Translate.ArgReorder)) → Mathlib.Tactic.Translate.ArgReorder.below_1 t → motive_2 t)
  (F_3 :
    (t : List (ℕ × Mathlib.Tactic.Translate.ArgReorder)) → Mathlib.Tactic.Translate.ArgReorder.below_2 t → motive_3 t)
  (F_4 : (t : ℕ × Mathlib.Tactic.Translate.ArgReorder) → Mathlib.Tactic.Translate.ArgReorder.below_3 t → motive_4 t),
  Mathlib.Tactic.Translate.ArgReorder.brecOn_1 t F_1 F_2 F_3 F_4 =
    F_2 t (Mathlib.Tactic.Translate.ArgReorder.brecOn_1.go t F_1 F_2 F_3 F_4).2

abbrev Mathlib.Tactic.Translate.ArgReorder.brecOn_3.go E1 #

{motive_1 : Mathlib.Tactic.Translate.ArgReorder → Sort u} →
  {motive_2 : Array (ℕ × Mathlib.Tactic.Translate.ArgReorder) → Sort u} →
    {motive_3 : List (ℕ × Mathlib.Tactic.Translate.ArgReorder) → Sort u} →
      {motive_4 : ℕ × Mathlib.Tactic.Translate.ArgReorder → Sort u} →
        (t : ℕ × Mathlib.Tactic.Translate.ArgReorder) →
          ((t : Mathlib.Tactic.Translate.ArgReorder) → t.below → motive_1 t) →
            ((t : Array (ℕ × Mathlib.Tactic.Translate.ArgReorder)) →
                Mathlib.Tactic.Translate.ArgReorder.below_1 t → motive_2 t) →
              ((t : List (ℕ × Mathlib.Tactic.Translate.ArgReorder)) →
                  Mathlib.Tactic.Translate.ArgReorder.below_2 t → motive_3 t) →
                ((t : ℕ × Mathlib.Tactic.Translate.ArgReorder) →
                    Mathlib.Tactic.Translate.ArgReorder.below_3 t → motive_4 t) →
                  motive_4 t ×' Mathlib.Tactic.Translate.ArgReorder.below_3 t

def Mathlib.Tactic.Translate.ArgReorder.ctorIdx E1 #

Mathlib.Tactic.Translate.ArgReorder → ℕ

theorem Mathlib.Tactic.Translate.ArgReorder.brecOn.eq E1 #

∀ {motive_1 : Mathlib.Tactic.Translate.ArgReorder → Sort u}
  {motive_2 : Array (ℕ × Mathlib.Tactic.Translate.ArgReorder) → Sort u}
  {motive_3 : List (ℕ × Mathlib.Tactic.Translate.ArgReorder) → Sort u}
  {motive_4 : ℕ × Mathlib.Tactic.Translate.ArgReorder → Sort u} (t : Mathlib.Tactic.Translate.ArgReorder)
  (F_1 : (t : Mathlib.Tactic.Translate.ArgReorder) → t.below → motive_1 t)
  (F_2 :
    (t : Array (ℕ × Mathlib.Tactic.Translate.ArgReorder)) → Mathlib.Tactic.Translate.ArgReorder.below_1 t → motive_2 t)
  (F_3 :
    (t : List (ℕ × Mathlib.Tactic.Translate.ArgReorder)) → Mathlib.Tactic.Translate.ArgReorder.below_2 t → motive_3 t)
  (F_4 : (t : ℕ × Mathlib.Tactic.Translate.ArgReorder) → Mathlib.Tactic.Translate.ArgReorder.below_3 t → motive_4 t),
  t.brecOn F_1 F_2 F_3 F_4 = F_1 t (Mathlib.Tactic.Translate.ArgReorder.brecOn.go t F_1 F_2 F_3 F_4).2

abbrev Mathlib.Tactic.Translate.ArgReorder.brecOn_1.go E1 #

{motive_1 : Mathlib.Tactic.Translate.ArgReorder → Sort u} →
  {motive_2 : Array (ℕ × Mathlib.Tactic.Translate.ArgReorder) → Sort u} →
    {motive_3 : List (ℕ × Mathlib.Tactic.Translate.ArgReorder) → Sort u} →
      {motive_4 : ℕ × Mathlib.Tactic.Translate.ArgReorder → Sort u} →
        (t : Array (ℕ × Mathlib.Tactic.Translate.ArgReorder)) →
          ((t : Mathlib.Tactic.Translate.ArgReorder) → t.below → motive_1 t) →
            ((t : Array (ℕ × Mathlib.Tactic.Translate.ArgReorder)) →
                Mathlib.Tactic.Translate.ArgReorder.below_1 t → motive_2 t) →
              ((t : List (ℕ × Mathlib.Tactic.Translate.ArgReorder)) →
                  Mathlib.Tactic.Translate.ArgReorder.below_2 t → motive_3 t) →
                ((t : ℕ × Mathlib.Tactic.Translate.ArgReorder) →
                    Mathlib.Tactic.Translate.ArgReorder.below_3 t → motive_4 t) →
                  motive_2 t ×' Mathlib.Tactic.Translate.ArgReorder.below_1 t

theorem Mathlib.Tactic.Translate.Reorder.mk.injEq E0 #

∀ (univReorder : Mathlib.Tactic.Translate.Permutation) (reorder : Mathlib.Tactic.Translate.ArgReorder)
  (univReorder_1 : Mathlib.Tactic.Translate.Permutation) (reorder_1 : Mathlib.Tactic.Translate.ArgReorder),
  ({ univReorder := univReorder, reorder := reorder } = { univReorder := univReorder_1, reorder := reorder_1 }) =
    (univReorder = univReorder_1 ∧ reorder = reorder_1)

theorem Mathlib.Tactic.Translate.ArgReorder.brecOn_3.eq E1 #

∀ {motive_1 : Mathlib.Tactic.Translate.ArgReorder → Sort u}
  {motive_2 : Array (ℕ × Mathlib.Tactic.Translate.ArgReorder) → Sort u}
  {motive_3 : List (ℕ × Mathlib.Tactic.Translate.ArgReorder) → Sort u}
  {motive_4 : ℕ × Mathlib.Tactic.Translate.ArgReorder → Sort u} (t : ℕ × Mathlib.Tactic.Translate.ArgReorder)
  (F_1 : (t : Mathlib.Tactic.Translate.ArgReorder) → t.below → motive_1 t)
  (F_2 :
    (t : Array (ℕ × Mathlib.Tactic.Translate.ArgReorder)) → Mathlib.Tactic.Translate.ArgReorder.below_1 t → motive_2 t)
  (F_3 :
    (t : List (ℕ × Mathlib.Tactic.Translate.ArgReorder)) → Mathlib.Tactic.Translate.ArgReorder.below_2 t → motive_3 t)
  (F_4 : (t : ℕ × Mathlib.Tactic.Translate.ArgReorder) → Mathlib.Tactic.Translate.ArgReorder.below_3 t → motive_4 t),
  Mathlib.Tactic.Translate.ArgReorder.brecOn_3 t F_1 F_2 F_3 F_4 =
    F_4 t (Mathlib.Tactic.Translate.ArgReorder.brecOn_3.go t F_1 F_2 F_3 F_4).2

theorem Mathlib.Tactic.Translate.Reorder.mk.inj E1 #

∀ {univReorder : Mathlib.Tactic.Translate.Permutation} {reorder : Mathlib.Tactic.Translate.ArgReorder}
  {univReorder_1 : Mathlib.Tactic.Translate.Permutation} {reorder_1 : Mathlib.Tactic.Translate.ArgReorder},
  { univReorder := univReorder, reorder := reorder } = { univReorder := univReorder_1, reorder := reorder_1 } →
    univReorder = univReorder_1 ∧ reorder = reorder_1

theorem Mathlib.Tactic.Translate.ArgReorder.mk.inj E1 #

∀ {perm : Mathlib.Tactic.Translate.Permutation} {argReorders : Array (ℕ × Mathlib.Tactic.Translate.ArgReorder)}
  {perm_1 : Mathlib.Tactic.Translate.Permutation} {argReorders_1 : Array (ℕ × Mathlib.Tactic.Translate.ArgReorder)},
  { perm := perm, argReorders := argReorders } = { perm := perm_1, argReorders := argReorders_1 } →
    perm = perm_1 ∧ argReorders = argReorders_1

abbrev Mathlib.Tactic.Translate.ArgReorder.brecOn.go E1 #

{motive_1 : Mathlib.Tactic.Translate.ArgReorder → Sort u} →
  {motive_2 : Array (ℕ × Mathlib.Tactic.Translate.ArgReorder) → Sort u} →
    {motive_3 : List (ℕ × Mathlib.Tactic.Translate.ArgReorder) → Sort u} →
      {motive_4 : ℕ × Mathlib.Tactic.Translate.ArgReorder → Sort u} →
        (t : Mathlib.Tactic.Translate.ArgReorder) →
          ((t : Mathlib.Tactic.Translate.ArgReorder) → t.below → motive_1 t) →
            ((t : Array (ℕ × Mathlib.Tactic.Translate.ArgReorder)) →
                Mathlib.Tactic.Translate.ArgReorder.below_1 t → motive_2 t) →
              ((t : List (ℕ × Mathlib.Tactic.Translate.ArgReorder)) →
                  Mathlib.Tactic.Translate.ArgReorder.below_2 t → motive_3 t) →
                ((t : ℕ × Mathlib.Tactic.Translate.ArgReorder) →
                    Mathlib.Tactic.Translate.ArgReorder.below_3 t → motive_4 t) →
                  motive_1 t ×' t.below

abbrev Mathlib.Tactic.Translate.Permutation E1 #

Type

A permutation, represented using cycle notation.

def Mathlib.Tactic.Translate.Permutation.permute! E1 #

{α : Type u_1} → [Inhabited α] → Mathlib.Tactic.Translate.Permutation → Array α → Array α

Permute an array of arguments using the given reorder.

def Mathlib.Tactic.Translate.Permutation.permuteList! E1 #

{α : Type u_1} → [Inhabited α] → Mathlib.Tactic.Translate.Permutation → List α → List α

Permute a list of either universe levels or universe parameters.

def Mathlib.Tactic.Translate.Permutation.reverse E1 #

Mathlib.Tactic.Translate.Permutation → Mathlib.Tactic.Translate.Permutation

Return the inverse permutation.

def Mathlib.Tactic.Translate.Permutation.range E1 #

Mathlib.Tactic.Translate.Permutation → ℕ

Return the minimum size of an array on which the permutation is valid.

def Mathlib.Tactic.Translate.Permutation.beq E1 #

Mathlib.Tactic.Translate.Permutation → Mathlib.Tactic.Translate.Permutation → Bool

Two permutations are considered equal if they permute in the same way.

structure Mathlib.Tactic.Translate.ArgReorder E1 #

Type

ArgReorder represents a permutation of arguments in a translation.

ctor Mathlib.Tactic.Translate.ArgReorder.mk E0 #

Mathlib.Tactic.Translate.Permutation →
  Array (ℕ × Mathlib.Tactic.Translate.ArgReorder) → Mathlib.Tactic.Translate.ArgReorder

abbrev Mathlib.Tactic.Translate.ArgReorder.perm E1 #

Mathlib.Tactic.Translate.ArgReorder → Mathlib.Tactic.Translate.Permutation

The list of disjoint cycles that represents the permutation.

abbrev Mathlib.Tactic.Translate.ArgReorder.argReorders E1 #

Mathlib.Tactic.Translate.ArgReorder → Array (ℕ × Mathlib.Tactic.Translate.ArgReorder)

The recursive reorders for reordering arguments of arguments. For the purpose of checking equality between reorders, this should be sorted.

instance Mathlib.Tactic.Translate.instInhabitedArgReorder E0 #

Inhabited Mathlib.Tactic.Translate.ArgReorder

def Mathlib.Tactic.Translate.ArgReorder.isEmpty E1 #

Mathlib.Tactic.Translate.ArgReorder → Bool

Return true if the reorder doesn't do anything.

def Mathlib.Tactic.Translate.ArgReorder.permute! E1 #

{α : Type u_1} → [Inhabited α] → Mathlib.Tactic.Translate.ArgReorder → Array α → Array α

Permute an array of arguments using the given reorder.

def Mathlib.Tactic.Translate.ArgReorder.reverse E1 #

Mathlib.Tactic.Translate.ArgReorder → Mathlib.Tactic.Translate.ArgReorder

Return the reorder that reverses the action of the given reorder.

def Mathlib.Tactic.Translate.ArgReorder.range E1 #

Mathlib.Tactic.Translate.ArgReorder → ℕ

Return the minimum size of an array on which the given reorder is valid.

opaque Mathlib.Tactic.Translate.ArgReorder.beq E1 #

Mathlib.Tactic.Translate.ArgReorder → Mathlib.Tactic.Translate.ArgReorder → Bool

Two reorders are considered equal if all permutations are the same.

instance Mathlib.Tactic.Translate.ArgReorder.instBEq E0 #

BEq Mathlib.Tactic.Translate.ArgReorder

def Mathlib.Tactic.Translate.ArgReorder.toString E1 #

Mathlib.Tactic.Translate.ArgReorder → String

Print an ArgReorder, representing the arguments by their index.

instance Mathlib.Tactic.Translate.ArgReorder.instToString E0 #

ToString Mathlib.Tactic.Translate.ArgReorder

structure Mathlib.Tactic.Translate.Reorder E1 #

Type

Reorder represents a permutation of arguments and universe levels for translating a given constant.

ctor Mathlib.Tactic.Translate.Reorder.mk E0 #

Mathlib.Tactic.Translate.Permutation → Mathlib.Tactic.Translate.ArgReorder → Mathlib.Tactic.Translate.Reorder

abbrev Mathlib.Tactic.Translate.Reorder.univReorder E1 #

Mathlib.Tactic.Translate.Reorder → Mathlib.Tactic.Translate.Permutation

The reordering of universe levels.

abbrev Mathlib.Tactic.Translate.Reorder.reorder E1 #

Mathlib.Tactic.Translate.Reorder → Mathlib.Tactic.Translate.ArgReorder

The reordering of arguments.

def Mathlib.Tactic.Translate.Reorder.reverse E1 #

Mathlib.Tactic.Translate.Reorder → Mathlib.Tactic.Translate.Reorder

Return the reorder that reverses the action of the given reorder.

opaque Mathlib.Tactic.Translate.reorderForall E1 #

Mathlib.Tactic.Translate.ArgReorder → Lean.Expr → Lean.MetaM Lean.Expr

Reorder the arguments of a function type using the given ArgReorder.

opaque Mathlib.Tactic.Translate.reorderLambda E1 #

Mathlib.Tactic.Translate.ArgReorder → Lean.Expr → Lean.MetaM Lean.Expr

Reorder the arguments of a function using the given ArgReorder.

def Mathlib.Tactic.Translate.getPermutation E1 #

{α : Type u_1} → [BEq α] → Array α → Array α → Option Mathlib.Tactic.Translate.Permutation

Return the permutation that sends src to tgt, if it exists.

opaque Mathlib.Tactic.Translate.guessReorder E1 #

Lean.Expr → Lean.Expr → Lean.MetaM Mathlib.Tactic.Translate.ArgReorder

Try to determine the value of the (reorder := ...) option that would be needed to translate type e₁ to type e₂. If there is no good guess, default to []. The heuristic that we use is to compare the conclusions of e₁ and e₂, and to observe which variables are swapped. We also apply this heuristic recursively in hypotheses.

def Mathlib.Tactic.Translate.translateReorder.quot E1 #

Lean.ParserDescr

def Lean.Parser.Category.translateReorder E1 #

Lean.Parser.Category

The syntax category for the reorder syntax.

def Mathlib.Tactic.Translate.reorderPart E1 #

Lean.ParserDescr

def Mathlib.Tactic.Translate.reorder E1 #

Lean.ParserDescr

(reorder := ...) reorders the arguments/hypotheses in the generated declaration. This is used in to_dual to swap the arguments in ≤, < and ⟶, and it is used in to_additive to translate from a ^ n to n • a. It uses disjoint cycle notation for the permutation. For reordering arguments of an argument a, it uses the notation a (...) where ... can be any reorder.

For example:

  • (reorder := α β, 5 6) swaps the arguments α and β with each other and the fifth and the sixth argument.
  • (reorder := 3 4 5) will move the fifth argument before the third argument.
  • (reorder := H (x y)) will swap the arguments x and y that appear in the hypothesis H.

If the translated declaration already exists (i.e. when using existing or self), the reorder argument is automatically inferred using the function guessReorder.

def Mathlib.Tactic.Translate.elabArgStx E1 #

Lean.TSyntax [`ident, `num] → Array Lean.Name → Array Lean.Expr → Lean.MessageData → Lean.MetaM ℕ

Elaborate syntax that refers to an argument of a declaration or hypothesis. This is either a 1-indexed number, or a name from argNames.

  • fvars is only used to add hover information to stx
  • head is only used for the error message.

opaque Mathlib.Tactic.Translate.elabReorder E1 #

Lean.TSyntax `translateReorder →
  Array Lean.Name → Array Lean.Expr → Lean.MessageData → Lean.MetaM Mathlib.Tactic.Translate.ArgReorder

Elaborate the argument of a (reorder := ...) option.