The translation attribute. #
Implementation of the translation attribute. This is used for @[to_additive] and @[to_dual].
See the docstring of to_additive for more information.
(attr := ...) applies the given attributes to the original and the translated declaration.
In the case of to_additive, we may want to apply it multiple times,
(such as in a ^ n -> n • a -> n +ᵥ a). In this case, you should use the syntax
to_additive (attr := some_other_attr, to_additive), which will apply some_other_attr to all
three generated declarations.
Equations
- One or more equations did not get rendered due to their size.
Instances For
(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 argumentsxandythat appear in the hypothesisH.
If the translated declaration already exists (i.e. when using existing or self), the reorder
argument is automatically inferred using the function guessReorder.
Equations
- One or more equations did not get rendered due to their size.
Instances For
the (relevant_arg := ...) option tells which argument to look at to determine whether to
translate this constant. This is inferred automatically,
but it can also be overwritten using this syntax.
If there are multiple possible arguments, we typically tag the first one.
If this argument contains a fixed type, this declaration will not be translated.
See the Heuristics section of the to_additive doc-string for more details.
When it cannot be inferred automatically, it is presumed that the first argument is relevant.
Use (relevant_arg := _) to indicate that there is no relevant argument.
Implementation note: we only allow exactly 1 relevant argument, even though some declarations
(like Prod.instGroup) have multiple relevant arguments.
The reason is that whether we translate a declaration is an all-or-nothing decision, and
we will not be able to translate declarations that (e.g.) talk about multiplication on ℕ × α
anyway.
Equations
- One or more equations did not get rendered due to their size.
Instances For
(dont_translate := ...) takes a list of type variables (separated by spaces) that should not be
considered for translation. For example in
lemma foo {α β : Type} [Group α] [Group β] (a : α) (b : β) : a * a⁻¹ = 1 ↔ b * b⁻¹ = 1
we can choose to only translate α by writing to_additive (dont_translate := β).
Equations
- One or more equations did not get rendered due to their size.
Instances For
Equations
- One or more equations did not get rendered due to their size.
Instances For
The (rename := ...) option takes a comma-separated list of rename rules of the form
oldName → newName specifying the argument names of the translated constant. The syntax
firstName ↔ secondName can also be used for swapping two argument names.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Equations
- One or more equations did not get rendered due to their size.
Instances For
A hint about the translated declaration
existingindicates that the translated form of the declaration is a pre-existing declaration. This is useful when the value cannot be translated, either because of a limitation in the translation heuristics, or because the value/proof is genuinely different.selfindicates that the declaration translates to itself, up to some reordering of arguments. If no arguments are reordered then the attribute is redundant, which thetranslateRedundantlinter will warn about.noneindicates that the translated declaration should not get a user-facing name, instead being named like an auxiliary declaration. This is particularly useful forto_dualwhen using thereassocattribute, because the dual of a right associated term is left associated, but we only want user-facing lemmas with right associated terms.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Equations
- One or more equations did not get rendered due to their size.
Instances For
Linter, mostly used by translate attributes, that checks that the source declaration doesn't have certain attributes
Linter used by translate attributes that checks if the given declaration name is equal to the automatically generated name
Linter to check whether the user correctly specified that the translated declaration already exists
Linter used by translate attributes that checks if the given reorder is equal to the automatically generated name
Linter used by translate attributes that checks if the relevant_arg is
automatically generated.
Linter used by translate attributes that checks if the attribute was already applied
Linter used by translate attributes that checks if the attribute is redundant
Linter used by translate attributes that warns when a translation was not added because of being invalid.
Add a translation to the translations map. If the translation attribute is dual, also add the reverse translation.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Config is the type of the arguments that can be provided to to_additive.
- trace : Bool
View the trace of the translation procedure. Equivalent to
set_option trace.translate true. - target : Lean.Name
The given name of the target: an anonymous name means no explicit name was provided, and the translate tactic auto-generates a name instead
- doc : Option (Lean.TSyntax `Lean.Parser.Command.docComment)
An optional doc string.
- allowAutoName : Bool
If
allowAutoNameisfalse(default) then we check whether the given name can be auto-generated. - reorder? : Option ArgReorder
The arguments that should be reordered when translating, using cycle notation.
- relevantArg? : Option RelevantArg
The argument used to determine whether this constant should be translated.
- attrs : Array Lean.Syntax
The attributes which we want to give to the original and translated declaration. For
simpsthis will also add generated lemmas to the translation dictionary. A list of positions of type variables that should not be translated.
- ref : Lean.Syntax
The
Syntaxelement corresponding to the translation attribute, which we need for adding definition ranges, and for logging messages. - existing : Bool
An optional flag stating that the translated declaration already exists. If this flag is wrong about whether the translated declaration exists, we raise a linter error. Note: the linter will never raise an error for inductive types and structures.
- self : Bool
- none : Bool
An optional flag for not giving the new declaration a user-facing name. This is achieved by appending e.g.
_to_dual_1to the name of the original declaration. - rename : Lean.NameMap Lean.Name
A map specifying the binder names of the translated declaration.
Instances For
Run applyReplacementFun on the given srcDecl to make a new declaration with name tgt.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Translate the source declaration and then run addDecl. If the kernel throws an error,
try to emit a better error message.
For efficiency in to_dual, we first run updateDecl without any UnfoldBoundaries,
and only if that fails do we try to include them.
The reason is that in the most common case, to_dual succeeds without needing to insert
unfold boundaries, and figuring out whether to insert them can be quite expensive.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Unfold simp, gcongr and hcongr/congr_simp auxlemmas in the type and value.
The reason why we can't just translate them is that they are generated by the @[simp] attribute,
so it would require a change in the implementation of @[simp] to add these translations.
Additionally, these lemmas have very short proofs, so unfolding them is not costly.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Find the target name of src, which is assumed to have been selected by findAuxDecls.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Returns a NameSet of auxiliary constants in decl that might have been generated
when adding pre to the environment, and which hence might need to be translated.
Examples include pre.match_5, pre._proof_2, someOtherDeclaration._proof_2 and wrapped✝.
The reason why we have to include _proof_i lemmas from other declarations is that there is a
cache of such proofs, and previous such auxiliary proofs are reused when possible.
These auxiliary declarations may be private or not, independent of whether pre is private.
wrapped✝ is generated by irreducible_def, and it has macro scopes.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Return the relevant_arg option based on the computed relevantArg
and the given cfg.relevantArg?.
For terms, relevantArg can always be inferred from the type, so we trust relevantArg.
For types, .noArg is replaced with .arg 0.
This heuristic is useful for types like Mul, LinearOrder and WithTop.
Equations
- One or more equations did not get rendered due to their size.
Instances For
If src is a class projection or class constructor, ensure that the class itself
has a translation. This ensures that relevant_arg will be inferred correctly.
For example, for LE.le and GE.ge we infer (relevant_arg := α) because α appears in LE α,
where it is a relevant argument.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Translate the declaration src and recursively all declarations rootSrc._proof_i
occurring in src using the translations dictionary.
rootSrcis the declaration that got the translation attribute androotTgtis its target.srcis assumed to have a value available in the environment.reorderandrenameare only used for the translation ofsrc.
Copy the instance attribute in a to_additive
[todo] it seems not to work when the to_additive is added as an attribute later.
Equations
- One or more equations did not get rendered due to their size.
Instances For
If src was declared with alias, then record tgt as an alias,
and give it an alias-style docstring if it doesn't have a doc-string already
Equations
- One or more equations did not get rendered due to their size.
Instances For
Warn the user when the declaration has an attribute.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Warn the user when the declaration has a simple scoped attribute.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Warn the user when the declaration has a parametric attribute.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Return the provided target name or autogenerate one if one was not provided.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Verify that the type of srcDecl translates to that of tgtDecl.
Also try to autogenerate the reorder and relevant_arg options for this translation.
Equations
- One or more equations did not get rendered due to their size.
Instances For
A version of insertTranslation that checks whether the translation is valid,
and throws an error if it is invalid.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Add translations from srcs to tgts, trying all pairings until one
succeeds.
Equations
- One or more equations did not get rendered due to their size.
Instances For
translateLemmas runs runAttr on all elements of names and adds translations between
the generated lemmas (the output of t). names must be non-empty.
Equations
- One or more equations did not get rendered due to their size.
Instances For
if f src = #[a_1, ..., a_n] and f tgt = #[b_1, ... b_n] then proceedFieldsAux src tgt f
will insert translations from a_i to b_i.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Add the structure fields of src to the translations dictionary
so that they will be translated correctly.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Elaboration of the (rename := ...) option.
Equations
- Mathlib.Tactic.Translate.elabRename stx declName argNames = Array.foldlM (Mathlib.Tactic.Translate.elabRename.elabRule✝ declName argNames) ∅ stx
Instances For
Elaboration of the configuration options for a translation attribute. It is assumed that
stx[0]is the attribute (e.g.to_additive)stx[1]is the optional tracing?stx[2]is the remainingattrArgs
TODO: Currently, we don't deduce any dont_translate arguments based on the type of declName.
In the future we would like that the presence of MonoidAlgebra k G will automatically
flag k as a type to not be translated.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Apply attributes to the original and translated declarations.
Copies equation lemmas and attributes from src to tgt.
addTranslationAttr src cfg adds a translation attribute to src with configuration cfg.
See the attribute implementation for more details.
It returns an array with names of translated declarations (usually 1, but more if there are nested
to_additive calls).