fact stringlengths 6 3.84k | type stringclasses 11
values | library stringclasses 32
values | imports listlengths 1 14 | filename stringlengths 20 95 | symbolic_name stringlengths 1 90 | docstring stringlengths 7 20k ⌀ |
|---|---|---|---|---|---|---|
FactorsHelper.singleton (n : ℕ) {a : ℕ} (h₁ : Nat.blt a n) (h₂ : IsNat (minFac n) n) :
FactorsHelper n a [n] :=
FactorsHelper.nil.cons _ ⟨mul_one _⟩ h₁ h₂ | theorem | Tactic | [
"Mathlib.Data.Nat.Factors",
"Mathlib.Tactic.NormNum.Prime"
] | Mathlib/Tactic/Simproc/Factors.lean | FactorsHelper.singleton | null |
FactorsHelper.cons_self {n m : ℕ} (a : ℕ) {l : List ℕ}
(h : IsNat (a * m) n) (H : FactorsHelper m a l) :
FactorsHelper n a (a :: l) := fun pa =>
H.cons_of_le _ h le_rfl (Nat.prime_def_minFac.1 pa).2 pa | theorem | Tactic | [
"Mathlib.Data.Nat.Factors",
"Mathlib.Tactic.NormNum.Prime"
] | Mathlib/Tactic/Simproc/Factors.lean | FactorsHelper.cons_self | null |
FactorsHelper.singleton_self (a : ℕ) : FactorsHelper a a [a] :=
FactorsHelper.nil.cons_self _ ⟨mul_one _⟩ | theorem | Tactic | [
"Mathlib.Data.Nat.Factors",
"Mathlib.Tactic.NormNum.Prime"
] | Mathlib/Tactic/Simproc/Factors.lean | FactorsHelper.singleton_self | null |
FactorsHelper.primeFactorsList_eq {n : ℕ} {l : List ℕ} (H : FactorsHelper n 2 l) :
Nat.primeFactorsList n = l :=
let ⟨h₁, h₂, h₃⟩ := H Nat.prime_two
have := List.isChain_iff_pairwise.1 (@List.IsChain.tail _ _ (_ :: _) h₁)
(List.eq_of_perm_of_sorted
(Nat.primeFactorsList_unique h₃ h₂) this (Nat.primeFactor... | theorem | Tactic | [
"Mathlib.Data.Nat.Factors",
"Mathlib.Tactic.NormNum.Prime"
] | Mathlib/Tactic/Simproc/Factors.lean | FactorsHelper.primeFactorsList_eq | null |
private partial evalPrimeFactorsListAux
{en enl : Q(ℕ)} {ea eal : Q(ℕ)} (ehn : Q(IsNat $en $enl)) (eha : Q(IsNat $ea $eal)) :
MetaM ((l : Q(List ℕ)) × Q(FactorsHelper $en $ea $l)) := do
/-
In this function we will use the convention that all `e` prefixed variables (proofs or otherwise)
contain `Expr`s. Th... | def | Tactic | [
"Mathlib.Data.Nat.Factors",
"Mathlib.Tactic.NormNum.Prime"
] | Mathlib/Tactic/Simproc/Factors.lean | evalPrimeFactorsListAux | Given `n` and `a` (in expressions `en` and `ea`) corresponding to literal numerals
(in `enl` and `eal`), returns `(l, ⊢ factorsHelper n a l)`. |
evalPrimeFactorsList
{en enl : Q(ℕ)} (hn : Q(IsNat $en $enl)) :
MetaM ((l : Q(List ℕ)) × Q(Nat.primeFactorsList $en = $l)) := do
match enl.natLit! with
| 0 =>
have _ : $enl =Q nat_lit 0 := ⟨⟩
have hen : Q($en = 0) := q($(hn).out)
return ⟨_, q($hen ▸ Nat.primeFactorsList_zero)⟩
| 1 =>
let _... | def | Tactic | [
"Mathlib.Data.Nat.Factors",
"Mathlib.Tactic.NormNum.Prime"
] | Mathlib/Tactic/Simproc/Factors.lean | evalPrimeFactorsList | Given a natural number `n`, returns `(l, ⊢ Nat.primeFactorsList n = l)`. |
private NameStruct where
/-- The namespace that the final name will reside in. -/
parent : Name
/-- A list of pieces to be joined by `toName`. -/
components : List String | structure | Tactic | [
"Lean.Elab.Tactic.Simp",
"Lean.Elab.App",
"Mathlib.Tactic.Simps.NotationClass",
"Mathlib.Lean.Expr.Basic"
] | Mathlib/Tactic/Simps/Basic.lean | NameStruct | An internal representation of a name to be used for a generated lemma. |
private NameStruct.toName (n : NameStruct) : Name :=
Name.mkStr n.parent <|
match n.components with
| [] => ""
| [x] => s!"{x}_def"
| e => "_".intercalate e | def | Tactic | [
"Lean.Elab.Tactic.Simp",
"Lean.Elab.App",
"Mathlib.Tactic.Simps.NotationClass",
"Mathlib.Lean.Expr.Basic"
] | Mathlib/Tactic/Simps/Basic.lean | NameStruct.toName | Join the components with `_`, or append `_def` if there is only one component. |
private NameStruct.update (nm : NameStruct) (s : String) (isPrefix : Bool := false) :
NameStruct :=
{ nm with components := if isPrefix then s :: nm.components else nm.components ++ [s] } | def | Tactic | [
"Lean.Elab.Tactic.Simp",
"Lean.Elab.App",
"Mathlib.Tactic.Simps.NotationClass",
"Mathlib.Lean.Expr.Basic"
] | Mathlib/Tactic/Simps/Basic.lean | NameStruct.update | `update nm s isPrefix` adds `s` to the last component of `nm`,
either as prefix or as suffix (specified by `isPrefix`).
Used by `simps_add_projections`. |
mkSimpContextResult (cfg : Meta.Simp.Config := {}) (simpOnly := false) (kind := SimpKind.simp)
(dischargeWrapper := DischargeWrapper.default) (hasStar := false) :
MetaM MkSimpContextResult := do
match dischargeWrapper with
| .default => pure ()
| _ =>
if kind == SimpKind.simpAll then
throwError ... | def | Tactic | [
"Lean.Elab.Tactic.Simp",
"Lean.Elab.App",
"Mathlib.Tactic.Simps.NotationClass",
"Mathlib.Lean.Expr.Basic"
] | Mathlib/Tactic/Simps/Basic.lean | mkSimpContextResult | Make `MkSimpContextResult` giving data instead of Syntax. Doesn't support arguments.
Intended to be very similar to `Lean.Elab.Tactic.mkSimpContext`
Todo: support arguments. |
mkSimpContext (cfg : Meta.Simp.Config := {}) (simpOnly := false) (kind := SimpKind.simp)
(dischargeWrapper := DischargeWrapper.default) (hasStar := false) :
MetaM Simp.Context := do
let data ← mkSimpContextResult cfg simpOnly kind dischargeWrapper hasStar
return data.ctx | def | Tactic | [
"Lean.Elab.Tactic.Simp",
"Lean.Elab.App",
"Mathlib.Tactic.Simps.NotationClass",
"Mathlib.Lean.Expr.Basic"
] | Mathlib/Tactic/Simps/Basic.lean | mkSimpContext | Make `Simp.Context` giving data instead of Syntax. Doesn't support arguments.
Intended to be very similar to `Lean.Elab.Tactic.mkSimpContext`
Todo: support arguments. |
ProjectionData where
/-- The name used in the generated `simp` lemmas -/
name : Name
/-- An Expression used by simps for the projection. It must be definitionally equal to an original
projection (or a composition of multiple projections).
These Expressions can contain the universe parameters specified in the ... | structure | Tactic | [
"Lean.Elab.Tactic.Simp",
"Lean.Elab.App",
"Mathlib.Tactic.Simps.NotationClass",
"Mathlib.Lean.Expr.Basic"
] | Mathlib/Tactic/Simps/Basic.lean | ProjectionData | An `(attr := ...)` option for `simps`. -/
syntax simpsOptAttrOption := atomic(" (" &"attr" " := " Parser.Term.attrInstance,* ")")?
/-- Arguments to `@[simps]` attribute.
Currently, a potential `(attr := ...)` argument has to come before other configuration options. -/
syntax simpsArgsRest := simpsOptAttrOption Tactic.... |
ParsedProjectionData where
/-- name for this projection used in the structure definition -/
strName : Name
/-- syntax that might have provided `strName` -/
strStx : Syntax := .missing
/-- name for this projection used in the generated `simp` lemmas -/
newName : Name
/-- syntax that provided `newName` -/
... | structure | Tactic | [
"Lean.Elab.Tactic.Simp",
"Lean.Elab.App",
"Mathlib.Tactic.Simps.NotationClass",
"Mathlib.Lean.Expr.Basic"
] | Mathlib/Tactic/Simps/Basic.lean | ParsedProjectionData | The `Simps.structureExt` environment extension specifies the preferred projections of the given
structure, used by the `@[simps]` attribute.
- You can generate this with the command `initialize_simps_projections`.
- If not generated, the `@[simps]` attribute will generate this automatically.
- To change the default val... |
ParsedProjectionData.toProjectionData (p : ParsedProjectionData) : ProjectionData :=
{ p with name := p.newName, expr := p.expr?.getD default, projNrs := p.projNrs.toList } | def | Tactic | [
"Lean.Elab.Tactic.Simp",
"Lean.Elab.App",
"Mathlib.Tactic.Simps.NotationClass",
"Mathlib.Lean.Expr.Basic"
] | Mathlib/Tactic/Simps/Basic.lean | ParsedProjectionData.toProjectionData | Turn `ParsedProjectionData` into `ProjectionData`. |
ProjectionRule where
/-- A renaming rule `before→after` or
Each name comes with the syntax used to write the rule,
which is used to declare hover information. -/
| rename (oldName : Name) (oldStx : Syntax) (newName : Name) (newStx : Syntax) :
ProjectionRule
/-- An adding rule `+fieldName` -/
| add... | inductive | Tactic | [
"Lean.Elab.Tactic.Simp",
"Lean.Elab.App",
"Mathlib.Tactic.Simps.NotationClass",
"Mathlib.Lean.Expr.Basic"
] | Mathlib/Tactic/Simps/Basic.lean | ProjectionRule | The type of rules that specify how metadata for projections in changes.
See `initialize_simps_projections`. |
projectionsInfo (l : List ProjectionData) (pref : String) (str : Name) : MessageData :=
let ⟨defaults, nondefaults⟩ := l.partition (·.isDefault)
let toPrint : List MessageData :=
defaults.map fun s ↦
let prefixStr := if s.isPrefix then "(prefix) " else ""
m!"Projection {prefixStr}{s.name}: {s.expr}"... | def | Tactic | [
"Lean.Elab.Tactic.Simp",
"Lean.Elab.App",
"Mathlib.Tactic.Simps.NotationClass",
"Mathlib.Lean.Expr.Basic"
] | Mathlib/Tactic/Simps/Basic.lean | projectionsInfo | Returns the projection information of a structure. |
findProjectionIndices (strName projName : Name) : MetaM (List Nat) := do
let env ← getEnv
let some baseStr := findField? env strName projName |
throwError "{strName} has no field {projName} in parent structure"
let some fullProjName := getProjFnForField? env baseStr projName |
throwError "no such field {p... | def | Tactic | [
"Lean.Elab.Tactic.Simp",
"Lean.Elab.App",
"Mathlib.Tactic.Simps.NotationClass",
"Mathlib.Lean.Expr.Basic"
] | Mathlib/Tactic/Simps/Basic.lean | findProjectionIndices | Find the indices of the projections that need to be applied to elaborate `$e.$projName`.
Example: If `e : α ≃+ β` and ``projName = `invFun`` then this returns `[0, 1]`, because the first
projection of `MulEquiv` is `toEquiv` and the second projection of `Equiv` is `invFun`. |
private dropPrefixIfNotNumber? (s : String) (pre : String) : Option Substring := do
let ret ← s.dropPrefix? pre
let flag := ret.toString.data.head?.elim false Char.isDigit
if flag then none else some ret | def | Tactic | [
"Lean.Elab.Tactic.Simp",
"Lean.Elab.App",
"Mathlib.Tactic.Simps.NotationClass",
"Mathlib.Lean.Expr.Basic"
] | Mathlib/Tactic/Simps/Basic.lean | dropPrefixIfNotNumber | A variant of `Substring.dropPrefix?` that does not consider `toFoo` to be a prefix to `toFoo_1`.
This is checked by inspecting whether the first character of the remaining part is a digit.
We use this variant because the latter is often a different field with an auto-generated name. |
private isPrefixOfAndNotNumber (s p : String) : Bool := (dropPrefixIfNotNumber? p s).isSome | def | Tactic | [
"Lean.Elab.Tactic.Simp",
"Lean.Elab.App",
"Mathlib.Tactic.Simps.NotationClass",
"Mathlib.Lean.Expr.Basic"
] | Mathlib/Tactic/Simps/Basic.lean | isPrefixOfAndNotNumber | A variant of `String.isPrefixOf` that does not consider `toFoo` to be a prefix to `toFoo_1`. |
private splitOnNotNumber (s delim : String) : List String :=
(process (s.splitOn delim).reverse "").reverse where
process (arr : List String) (tail : String) := match arr with
| [] => []
| (x :: xs) =>
let flag := x.data.head?.elim false Char.isDigit
if flag then
process xs (... | def | Tactic | [
"Lean.Elab.Tactic.Simp",
"Lean.Elab.App",
"Mathlib.Tactic.Simps.NotationClass",
"Mathlib.Lean.Expr.Basic"
] | Mathlib/Tactic/Simps/Basic.lean | splitOnNotNumber | A variant of `String.splitOn` that does not split `toFoo_1` into `toFoo` and `1`. |
partial getCompositeOfProjectionsAux (proj : String) (e : Expr) (pos : Array Nat)
(args : Array Expr) : MetaM (Expr × Array Nat) := do
let env ← getEnv
let .const structName _ := (← whnf (← inferType e)).getAppFn |
throwError "{e} doesn't have a structure as type"
let projs := getStructureFieldsFlattened ... | def | Tactic | [
"Lean.Elab.Tactic.Simp",
"Lean.Elab.App",
"Mathlib.Tactic.Simps.NotationClass",
"Mathlib.Lean.Expr.Basic"
] | Mathlib/Tactic/Simps/Basic.lean | getCompositeOfProjectionsAux | Auxiliary function of `getCompositeOfProjections`. |
getCompositeOfProjections (structName : Name) (proj : String) : MetaM (Expr × Array Nat) := do
let strExpr ← mkConstWithLevelParams structName
let type ← inferType strExpr
forallTelescopeReducing type fun typeArgs _ ↦
withLocalDeclD `x (mkAppN strExpr typeArgs) fun e ↦
getCompositeOfProjectionsAux (proj ++ "_... | def | Tactic | [
"Lean.Elab.Tactic.Simp",
"Lean.Elab.App",
"Mathlib.Tactic.Simps.NotationClass",
"Mathlib.Lean.Expr.Basic"
] | Mathlib/Tactic/Simps/Basic.lean | getCompositeOfProjections | Suppose we are given a structure `str` and a projection `proj`, that could be multiple nested
projections (separated by `_`), where each projection could be a projection of a parent structure.
This function returns an expression that is the composition of these projections and a
list of natural numbers, that are the pr... |
mkParsedProjectionData (structName : Name) : CoreM (Array ParsedProjectionData) := do
let env ← getEnv
let projs := getStructureFields env structName
if projs.size == 0 then
throwError "Declaration {structName} is not a structure."
let projData := projs.map fun fieldName ↦ {
strName := fieldName, newNam... | def | Tactic | [
"Lean.Elab.Tactic.Simp",
"Lean.Elab.App",
"Mathlib.Tactic.Simps.NotationClass",
"Mathlib.Lean.Expr.Basic"
] | Mathlib/Tactic/Simps/Basic.lean | mkParsedProjectionData | Get the default `ParsedProjectionData` for structure `str`.
It first returns the direct fields of the structure in the right order, and then
all (non-subobject fields) of all parent structures. The subobject fields are precisely the
non-default fields. |
applyProjectionRules (projs : Array ParsedProjectionData) (rules : Array ProjectionRule) :
CoreM (Array ParsedProjectionData) := do
let projs : Array ParsedProjectionData := rules.foldl (init := projs) fun projs rule ↦
match rule with
| .rename strName strStx newName newStx =>
if (projs.map (·.newNa... | def | Tactic | [
"Lean.Elab.Tactic.Simp",
"Lean.Elab.App",
"Mathlib.Tactic.Simps.NotationClass",
"Mathlib.Lean.Expr.Basic"
] | Mathlib/Tactic/Simps/Basic.lean | applyProjectionRules | Execute the projection renamings (and turning off projections) as specified by `rules`. |
findProjection (str : Name) (proj : ParsedProjectionData)
(rawUnivs : List Level) : CoreM ParsedProjectionData := do
let env ← getEnv
let (rawExpr, nrs) ← MetaM.run' <|
getCompositeOfProjections str proj.strName.lastComponentAsString
if !proj.strStx.isMissing then
_ ← MetaM.run' <| TermElabM.run' <| a... | def | Tactic | [
"Lean.Elab.Tactic.Simp",
"Lean.Elab.App",
"Mathlib.Tactic.Simps.NotationClass",
"Mathlib.Lean.Expr.Basic"
] | Mathlib/Tactic/Simps/Basic.lean | findProjection | Auxiliary function for `getRawProjections`.
Generates the default projection, and looks for a custom projection declared by the user,
and replaces the default projection with the custom one, if it can find it. |
checkForUnusedCustomProjs (stx : Syntax) (str : Name) (projs : Array ParsedProjectionData) :
CoreM Unit := do
let nrCustomProjections := projs.toList.countP (·.isCustom)
let env ← getEnv
let customDeclarations := env.constants.map₂.foldl (init := #[]) fun xs nm _ =>
if (str ++ `Simps).isPrefixOf nm && !nm... | def | Tactic | [
"Lean.Elab.Tactic.Simp",
"Lean.Elab.App",
"Mathlib.Tactic.Simps.NotationClass",
"Mathlib.Lean.Expr.Basic"
] | Mathlib/Tactic/Simps/Basic.lean | checkForUnusedCustomProjs | Checks if there are declarations in the current file in the namespace `{str}.Simps` that are
not used. |
findAutomaticProjectionsAux (str : Name) (proj : ParsedProjectionData) (args : Array Expr) :
TermElabM <| Option (Expr × Name) := do
if let some ⟨className, isNotation, findArgs⟩ :=
notationClassAttr.find? (← getEnv) proj.strName then
let findArgs ← unsafe evalConst findArgType findArgs
let classArgs ... | def | Tactic | [
"Lean.Elab.Tactic.Simp",
"Lean.Elab.App",
"Mathlib.Tactic.Simps.NotationClass",
"Mathlib.Lean.Expr.Basic"
] | Mathlib/Tactic/Simps/Basic.lean | findAutomaticProjectionsAux | If a structure has a field that corresponds to a coercion to functions or sets, or corresponds
to notation, find the custom projection that uses this coercion or notation.
Returns the custom projection and the name of the projection used.
We catch most errors this function causes, so that we don't fail if an unrel... |
findAutomaticProjections (str : Name) (projs : Array ParsedProjectionData) :
CoreM (Array ParsedProjectionData) := do
let strDecl ← getConstInfo str
trace[simps.debug] "debug: {projs}"
MetaM.run' <| TermElabM.run' (s := {levelNames := strDecl.levelParams}) <|
forallTelescope strDecl.type fun args _ ↦ do
l... | def | Tactic | [
"Lean.Elab.Tactic.Simp",
"Lean.Elab.App",
"Mathlib.Tactic.Simps.NotationClass",
"Mathlib.Lean.Expr.Basic"
] | Mathlib/Tactic/Simps/Basic.lean | findAutomaticProjections | Auxiliary function for `getRawProjections`.
Find custom projections, automatically found by simps.
These come from `DFunLike` and `SetLike` instances. |
getRawProjections (stx : Syntax) (str : Name) (traceIfExists : Bool := false)
(rules : Array ProjectionRule := #[]) (trc := false) :
CoreM (List Name × Array ProjectionData) := do
withOptions (· |>.updateBool `trace.simps.verbose (trc || ·)) <| do
let env ← getEnv
if let some data := (structureExt.getStat... | def | Tactic | [
"Lean.Elab.Tactic.Simp",
"Lean.Elab.App",
"Mathlib.Tactic.Simps.NotationClass",
"Mathlib.Lean.Expr.Basic"
] | Mathlib/Tactic/Simps/Basic.lean | getRawProjections | Get the projections used by `simps` associated to a given structure `str`.
The returned information is also stored in the environment extension `Simps.structureExt`, which
is given to `str`. If `str` already has this attribute, the information is read from this
extension instead. See the documentation for this extensi... |
elabSimpsRule : Syntax → CommandElabM ProjectionRule
| `(simpsRule| $id1 → $id2) => return .rename id1.getId id1.raw id2.getId id2.raw
| `(simpsRule| - $id) => return .erase id.getId id.raw
| `(simpsRule| + $id) => return .add id.getId id.raw
| `(simpsRule| as_prefix $id) => return .prefix id.... | def | Tactic | [
"Lean.Elab.Tactic.Simp",
"Lean.Elab.App",
"Mathlib.Tactic.Simps.NotationClass",
"Mathlib.Lean.Expr.Basic"
] | Mathlib/Tactic/Simps/Basic.lean | elabSimpsRule | Parse a rule for `initialize_simps_projections`. It is `<name>→<name>`, `-<name>`, `+<name>`
or `as_prefix <name>`. |
@[command_elab «initialize_simps_projections»] elabInitializeSimpsProjections : CommandElab
| stx@`(initialize_simps_projections $[?%$trc]? $id $[($stxs,*)]?) => do
let stxs := stxs.getD <| .mk #[]
let rules ← stxs.getElems.raw.mapM elabSimpsRule
let nm ← resolveGlobalConstNoOverload id
_ ← liftTermEl... | def | Tactic | [
"Lean.Elab.Tactic.Simp",
"Lean.Elab.App",
"Mathlib.Tactic.Simps.NotationClass",
"Mathlib.Lean.Expr.Basic"
] | Mathlib/Tactic/Simps/Basic.lean | elabInitializeSimpsProjections | Function elaborating `initialize_simps_projections`. |
Config where
/-- Make generated lemmas simp lemmas -/
isSimp := true
/-- Other attributes to apply to generated lemmas. -/
attrs : Array Syntax := #[]
/-- simplify the right-hand side of generated simp-lemmas using `dsimp, simp`. -/
simpRhs := false
/-- TransparencyMode used to reduce the type in order to... | structure | Tactic | [
"Lean.Elab.Tactic.Simp",
"Lean.Elab.App",
"Mathlib.Tactic.Simps.NotationClass",
"Mathlib.Lean.Expr.Basic"
] | Mathlib/Tactic/Simps/Basic.lean | Config | Configuration options for `@[simps]` |
@[deprecated "use `-fullyApplied` instead" (since := "2025-03-10")]
Config.asFn : Simps.Config where
fullyApplied := false | def | Tactic | [
"Lean.Elab.Tactic.Simp",
"Lean.Elab.App",
"Mathlib.Tactic.Simps.NotationClass",
"Mathlib.Lean.Expr.Basic"
] | Mathlib/Tactic/Simps/Basic.lean | Config.asFn | Function elaborating `Config` -/
declare_command_config_elab elabSimpsConfig Config
/-- A common configuration for `@[simps]`: generate equalities between functions instead equalities
between fully applied Expressions. Replaced by `@[simps -fullyApplied]`. |
@[deprecated "use `-isSimp` instead" (since := "2025-03-10")]
Config.lemmasOnly : Config where
isSimp := false | def | Tactic | [
"Lean.Elab.Tactic.Simp",
"Lean.Elab.App",
"Mathlib.Tactic.Simps.NotationClass",
"Mathlib.Lean.Expr.Basic"
] | Mathlib/Tactic/Simps/Basic.lean | Config.lemmasOnly | A common configuration for `@[simps]`: don't tag the generated lemmas with `@[simp]`.
Replaced by `@[simps -isSimp]`. |
partial _root_.Lean.Expr.instantiateLambdasOrApps (es : Array Expr) (e : Expr) : Expr :=
e.betaRev es.reverse true -- check if this is what I want | def | Tactic | [
"Lean.Elab.Tactic.Simp",
"Lean.Elab.App",
"Mathlib.Tactic.Simps.NotationClass",
"Mathlib.Lean.Expr.Basic"
] | Mathlib/Tactic/Simps/Basic.lean | _root_.Lean.Expr.instantiateLambdasOrApps | `instantiateLambdasOrApps es e` instantiates lambdas in `e` by expressions from `es`.
If the length of `es` is larger than the number of lambdas in `e`,
then the term is applied to the remaining terms.
Also reduces head let-expressions in `e`, including those after instantiating all lambdas.
This is very similar to `e... |
getProjectionExprs (stx : Syntax) (tgt : Expr) (rhs : Expr) (cfg : Config) :
MetaM <| Array <| Expr × ProjectionData := do
let params := tgt.getAppArgs
if cfg.debug && !(← (params.zip rhs.getAppArgs).allM fun ⟨a, b⟩ ↦ isDefEq a b) then
throwError "unreachable code: parameters are not definitionally equal"
... | def | Tactic | [
"Lean.Elab.Tactic.Simp",
"Lean.Elab.App",
"Mathlib.Tactic.Simps.NotationClass",
"Mathlib.Lean.Expr.Basic"
] | Mathlib/Tactic/Simps/Basic.lean | getProjectionExprs | Get the projections of a structure used by `@[simps]` applied to the appropriate arguments.
Returns a list of tuples
```
(corresponding right-hand-side, given projection name, projection Expression,
future projection numbers, used by default, is prefix)
```
(where all fields except the first are packed in a `Projecti... |
addProjection (declName : Name) (type lhs rhs : Expr) (args : Array Expr)
(cfg : Config) : MetaM Unit := do
trace[simps.debug] "Planning to add the equality{indentD m!"{lhs} = ({rhs} : {type})"}"
let env ← getEnv
let lvl ← getLevel type
let mut (rhs, prf) := (rhs, mkAppN (mkConst `Eq.refl [lvl]) #[type, lhs... | def | Tactic | [
"Lean.Elab.Tactic.Simp",
"Lean.Elab.App",
"Mathlib.Tactic.Simps.NotationClass",
"Mathlib.Lean.Expr.Basic"
] | Mathlib/Tactic/Simps/Basic.lean | addProjection | Add a lemma with `nm` stating that `lhs = rhs`. `type` is the type of both `lhs` and `rhs`,
`args` is the list of local constants occurring, and `univs` is the list of universe variables. |
partial headStructureEtaReduce (e : Expr) : MetaM Expr := do
let env ← getEnv
let (ctor, args) := e.getAppFnArgs
let some (.ctorInfo { induct := struct, numParams, ..}) := env.find? ctor | pure e
let some { fieldNames, .. } := getStructureInfo? env struct | pure e
let (params, fields) := args.toList.splitAt n... | def | Tactic | [
"Lean.Elab.Tactic.Simp",
"Lean.Elab.App",
"Mathlib.Tactic.Simps.NotationClass",
"Mathlib.Lean.Expr.Basic"
] | Mathlib/Tactic/Simps/Basic.lean | headStructureEtaReduce | Perform head-structure-eta-reduction on expression `e`. That is, if `e` is of the form
`⟨f.1, f.2, ..., f.n⟩` with `f` definitionally equal to `e`, then
`headStructureEtaReduce e = headStructureEtaReduce f` and `headStructureEtaReduce e = e` otherwise. |
partial addProjections (nm : NameStruct) (type lhs rhs : Expr)
(args : Array Expr) (mustBeStr : Bool) (cfg : Config)
(todo : List (String × Syntax)) (toApply : List Nat) : MetaM (Array Name) := do
trace[simps.debug] "Type of the Expression before normalizing: {type}"
withTransparency cfg.typeMd <| forallTel... | def | Tactic | [
"Lean.Elab.Tactic.Simp",
"Lean.Elab.App",
"Mathlib.Tactic.Simps.NotationClass",
"Mathlib.Lean.Expr.Basic"
] | Mathlib/Tactic/Simps/Basic.lean | addProjections | Derive lemmas specifying the projections of the declaration.
`nm`: name of the lemma
If `todo` is non-empty, it will generate exactly the names in `todo`.
`toApply` is non-empty after a custom projection that is a composition of multiple projections
was just used. In that case we need to apply these projections before ... |
simpsTac (ref : Syntax) (nm : Name) (cfg : Config := {})
(todo : List (String × Syntax) := []) (trc := false) : AttrM (Array Name) :=
withOptions (· |>.updateBool `trace.simps.verbose (trc || ·)) <| do
let env ← getEnv
let some d := env.find? nm | throwError "Declaration {nm} doesn't exist."
let lhs : Expr ... | def | Tactic | [
"Lean.Elab.Tactic.Simp",
"Lean.Elab.App",
"Mathlib.Tactic.Simps.NotationClass",
"Mathlib.Lean.Expr.Basic"
] | Mathlib/Tactic/Simps/Basic.lean | simpsTac | `simpsTac` derives `simp` lemmas for all (nested) non-Prop projections of the declaration.
If `todo` is non-empty, it will generate exactly the names in `todo`.
If `shortNm` is true, the generated names will only use the last projection name.
If `trc` is true, trace as if `trace.simps.verbose` is true. |
simpsTacFromSyntax (nm : Name) (stx : Syntax) : AttrM (Array Name) :=
match stx with
| `(attr| simps $[!%$bang]? $[?%$trc]? $attrs:simpsOptAttrOption $c:optConfig $[$ids]*) => do
let extraAttrs := match attrs with
| `(Attr.simpsOptAttrOption| (attr := $[$stxs],*)) => stxs
| _ => #[]
let cfg ← li... | def | Tactic | [
"Lean.Elab.Tactic.Simp",
"Lean.Elab.App",
"Mathlib.Tactic.Simps.NotationClass",
"Mathlib.Lean.Expr.Basic"
] | Mathlib/Tactic/Simps/Basic.lean | simpsTacFromSyntax | elaborate the syntax and run `simpsTac`. |
findArgType : Type := Name → Name → Array Expr → MetaM (Array (Option Expr)) | def | Tactic | [
"Mathlib.Init",
"Lean.Elab.Exception",
"Batteries.Lean.NameMapAttribute",
"Batteries.Tactic.Lint"
] | Mathlib/Tactic/Simps/NotationClass.lean | findArgType | The `@[notation_class]` attribute specifies that this is a notation class,
and this notation should be used instead of projections by `@[simps]`.
* This is only important if the projection is written differently using notation, e.g.
`+` uses `HAdd.hAdd`, not `Add.add` and `0` uses `OfNat.ofNat` not `Zero.zero`.
... |
defaultfindArgs : findArgType := fun _ className args ↦ do
let some classExpr := (← getEnv).find? className | throwError "no such class {className}"
let arity := classExpr.type.getNumHeadForalls
if arity == args.size then
return args.map some
else if h : args.size = 1 then
return .replicate arity args[0... | def | Tactic | [
"Mathlib.Init",
"Lean.Elab.Exception",
"Batteries.Lean.NameMapAttribute",
"Batteries.Tactic.Lint"
] | Mathlib/Tactic/Simps/NotationClass.lean | defaultfindArgs | Find arguments for a notation class |
copyFirst : findArgType := fun _ _ args ↦ return (args.push <| args[0]?.getD default).map some | def | Tactic | [
"Mathlib.Init",
"Lean.Elab.Exception",
"Batteries.Lean.NameMapAttribute",
"Batteries.Tactic.Lint"
] | Mathlib/Tactic/Simps/NotationClass.lean | copyFirst | Find arguments by duplicating the first argument. Used for `pow`. |
copySecond : findArgType := fun _ _ args ↦ return (args.push <| args[1]?.getD default).map some | def | Tactic | [
"Mathlib.Init",
"Lean.Elab.Exception",
"Batteries.Lean.NameMapAttribute",
"Batteries.Tactic.Lint"
] | Mathlib/Tactic/Simps/NotationClass.lean | copySecond | Find arguments by duplicating the first argument. Used for `smul`. |
nsmulArgs : findArgType := fun _ _ args ↦
return #[Expr.const `Nat [], args[0]?.getD default] ++ args |>.map some | def | Tactic | [
"Mathlib.Init",
"Lean.Elab.Exception",
"Batteries.Lean.NameMapAttribute",
"Batteries.Tactic.Lint"
] | Mathlib/Tactic/Simps/NotationClass.lean | nsmulArgs | Find arguments by prepending `ℕ` and duplicating the first argument. Used for `nsmul`. |
zsmulArgs : findArgType := fun _ _ args ↦
return #[Expr.const `Int [], args[0]?.getD default] ++ args |>.map some | def | Tactic | [
"Mathlib.Init",
"Lean.Elab.Exception",
"Batteries.Lean.NameMapAttribute",
"Batteries.Tactic.Lint"
] | Mathlib/Tactic/Simps/NotationClass.lean | zsmulArgs | Find arguments by prepending `ℤ` and duplicating the first argument. Used for `zsmul`. |
findZeroArgs : findArgType := fun _ _ args ↦
return #[some <| args[0]?.getD default, some <| mkRawNatLit 0] | def | Tactic | [
"Mathlib.Init",
"Lean.Elab.Exception",
"Batteries.Lean.NameMapAttribute",
"Batteries.Tactic.Lint"
] | Mathlib/Tactic/Simps/NotationClass.lean | findZeroArgs | Find arguments for the `Zero` class. |
findOneArgs : findArgType := fun _ _ args ↦
return #[some <| args[0]?.getD default, some <| mkRawNatLit 1] | def | Tactic | [
"Mathlib.Init",
"Lean.Elab.Exception",
"Batteries.Lean.NameMapAttribute",
"Batteries.Tactic.Lint"
] | Mathlib/Tactic/Simps/NotationClass.lean | findOneArgs | Find arguments for the `One` class. |
findCoercionArgs : findArgType := fun str className args ↦ do
let some classExpr := (← getEnv).find? className | throwError "no such class {className}"
let arity := classExpr.type.getNumHeadForalls
let eStr := mkAppN (← mkConstWithLevelParams str) args
let classArgs := .replicate (arity - 1) none
return #[som... | def | Tactic | [
"Mathlib.Init",
"Lean.Elab.Exception",
"Batteries.Lean.NameMapAttribute",
"Batteries.Tactic.Lint"
] | Mathlib/Tactic/Simps/NotationClass.lean | findCoercionArgs | Find arguments of a coercion class (`DFunLike` or `SetLike`) |
AutomaticProjectionData where
/-- `className` is the name of the class we are looking for. -/
className : Name
/-- `isNotation` is a Boolean that specifies whether this is notation
(false for the coercions `DFunLike` and `SetLike`). If this is set to true, we add the current
class as hypothesis during type-cl... | structure | Tactic | [
"Mathlib.Init",
"Lean.Elab.Exception",
"Batteries.Lean.NameMapAttribute",
"Batteries.Tactic.Lint"
] | Mathlib/Tactic/Simps/NotationClass.lean | AutomaticProjectionData | Data needed to generate automatic projections. This data is associated to a name of a projection
in a structure that must be used to trigger the search. |
private TerminalReplacementOutcome where
| success (stx : TSyntax `tactic)
| remainingGoals (stx : TSyntax `tactic) (goals : List MessageData)
| error (stx : TSyntax `tactic) (msg : MessageData)
open Elab.Command | inductive | Tactic | [
"Mathlib.Tactic.TacticAnalysis",
"Mathlib.Tactic.ExtractGoal",
"Mathlib.Tactic.MinImports",
"Lean.Elab.Tactic.Meta",
"Lean.Elab.Command"
] | Mathlib/Tactic/TacticAnalysis/Declarations.lean | TerminalReplacementOutcome | Helper structure for the return type of the `test` function in `terminalReplacement`. |
terminalReplacement (oldTacticName newTacticName : String) (oldTacticKind : SyntaxNodeKind)
(newTactic : Syntax → MVarId → MetaM (TSyntax `tactic))
(reportFailure : Bool := true) (reportSuccess : Bool := false)
(reportSlowdown : Bool := false) (maxSlowdown : Float := 1) :
TacticAnalysis.Config := .ofCom... | def | Tactic | [
"Mathlib.Tactic.TacticAnalysis",
"Mathlib.Tactic.ExtractGoal",
"Mathlib.Tactic.MinImports",
"Lean.Elab.Tactic.Meta",
"Lean.Elab.Command"
] | Mathlib/Tactic/TacticAnalysis/Declarations.lean | terminalReplacement | Define a pass that tries replacing one terminal tactic with another.
`newTacticName` is a human-readable name for the tactic, for example "linarith".
This can be used to group messages together, so that `ring`, `ring_nf`, `ring1`, ...
all produce the same message.
`oldTacticKind` is the `SyntaxNodeKind` for the tacti... |
grindReplacementWith (tacticName : String) (tacticKind : SyntaxNodeKind)
(reportFailure : Bool := true) (reportSuccess : Bool := false)
(reportSlowdown : Bool := false) (maxSlowdown : Float := 1) :
TacticAnalysis.Config :=
terminalReplacement tacticName "grind" tacticKind (fun _ _ => `(tactic| grind))
... | def | Tactic | [
"Mathlib.Tactic.TacticAnalysis",
"Mathlib.Tactic.ExtractGoal",
"Mathlib.Tactic.MinImports",
"Lean.Elab.Tactic.Meta",
"Lean.Elab.Command"
] | Mathlib/Tactic/TacticAnalysis/Declarations.lean | grindReplacementWith | Define a pass that tries replacing a specific tactic with `grind`.
`tacticName` is a human-readable name for the tactic, for example "linarith".
This can be used to group messages together, so that `ring`, `ring_nf`, `ring1`, ...
all produce the same message.
`tacticKind` is the `SyntaxNodeKind` for the tactic's main... |
@[tacticAnalysis linter.tacticAnalysis.regressions.linarithToGrind,
inherit_doc linter.tacticAnalysis.regressions.linarithToGrind]
linarithToGrindRegressions := grindReplacementWith "linarith" `Mathlib.Tactic.linarith | def | Tactic | [
"Mathlib.Tactic.TacticAnalysis",
"Mathlib.Tactic.ExtractGoal",
"Mathlib.Tactic.MinImports",
"Lean.Elab.Tactic.Meta",
"Lean.Elab.Command"
] | Mathlib/Tactic/TacticAnalysis/Declarations.lean | linarithToGrindRegressions | null |
@[tacticAnalysis linter.tacticAnalysis.regressions.ringToGrind,
inherit_doc linter.tacticAnalysis.regressions.ringToGrind]
ringToGrindRegressions := grindReplacementWith "ring" `Mathlib.Tactic.RingNF.ring | def | Tactic | [
"Mathlib.Tactic.TacticAnalysis",
"Mathlib.Tactic.ExtractGoal",
"Mathlib.Tactic.MinImports",
"Lean.Elab.Tactic.Meta",
"Lean.Elab.Command"
] | Mathlib/Tactic/TacticAnalysis/Declarations.lean | ringToGrindRegressions | null |
@[tacticAnalysis linter.tacticAnalysis.regressions.omegaToCutsat,
inherit_doc linter.tacticAnalysis.regressions.omegaToCutsat]
omegaToCutsatRegressions :=
terminalReplacement "omega" "cutsat" ``Lean.Parser.Tactic.omega (fun _ _ => `(tactic| cutsat))
(reportSuccess := false) (reportFailure := true) | def | Tactic | [
"Mathlib.Tactic.TacticAnalysis",
"Mathlib.Tactic.ExtractGoal",
"Mathlib.Tactic.MinImports",
"Lean.Elab.Tactic.Meta",
"Lean.Elab.Command"
] | Mathlib/Tactic/TacticAnalysis/Declarations.lean | omegaToCutsatRegressions | null |
@[tacticAnalysis linter.tacticAnalysis.omegaToCutsat,
inherit_doc linter.tacticAnalysis.omegaToCutsat]
omegaToCutsat :=
terminalReplacement "omega" "cutsat" ``Lean.Parser.Tactic.omega (fun _ _ => `(tactic| cutsat))
(reportSuccess := true) (reportFailure := false) | def | Tactic | [
"Mathlib.Tactic.TacticAnalysis",
"Mathlib.Tactic.ExtractGoal",
"Mathlib.Tactic.MinImports",
"Lean.Elab.Tactic.Meta",
"Lean.Elab.Command"
] | Mathlib/Tactic/TacticAnalysis/Declarations.lean | omegaToCutsat | null |
@[tacticAnalysis linter.tacticAnalysis.rwMerge, inherit_doc linter.tacticAnalysis.rwMerge]
rwMerge : TacticAnalysis.Config := .ofComplex {
out := (List MVarId × Array Syntax)
ctx := (Array (Array Syntax))
trigger ctx stx :=
match stx with
| `(tactic| rw [$args,*]) => .continue ((ctx.getD #[]).push args)
... | def | Tactic | [
"Mathlib.Tactic.TacticAnalysis",
"Mathlib.Tactic.ExtractGoal",
"Mathlib.Tactic.MinImports",
"Lean.Elab.Tactic.Meta",
"Lean.Elab.Command"
] | Mathlib/Tactic/TacticAnalysis/Declarations.lean | rwMerge | null |
@[tacticAnalysis linter.tacticAnalysis.mergeWithGrind,
inherit_doc linter.tacticAnalysis.mergeWithGrind]
mergeWithGrind : TacticAnalysis.Config where
run seq := do
if let #[(preCtx, preI), (_postCtx, postI)] := seq[0:2].array then
if postI.stx.getKind == ``Lean.Parser.Tactic.grind then
if let [goa... | def | Tactic | [
"Mathlib.Tactic.TacticAnalysis",
"Mathlib.Tactic.ExtractGoal",
"Mathlib.Tactic.MinImports",
"Lean.Elab.Tactic.Meta",
"Lean.Elab.Command"
] | Mathlib/Tactic/TacticAnalysis/Declarations.lean | mergeWithGrind | null |
@[tacticAnalysis linter.tacticAnalysis.terminalToGrind,
inherit_doc linter.tacticAnalysis.terminalToGrind]
terminalToGrind : TacticAnalysis.Config where
run seq := do
let threshold := 3
let mut replaced : List (TSyntax `tactic) := []
let mut success := false
let mut oldHeartbeats := 0
let mut ne... | def | Tactic | [
"Mathlib.Tactic.TacticAnalysis",
"Mathlib.Tactic.ExtractGoal",
"Mathlib.Tactic.MinImports",
"Lean.Elab.Tactic.Meta",
"Lean.Elab.Command"
] | Mathlib/Tactic/TacticAnalysis/Declarations.lean | terminalToGrind | null |
@[tacticAnalysis linter.tacticAnalysis.introMerge, inherit_doc linter.tacticAnalysis.introMerge]
introMerge : TacticAnalysis.Config := .ofComplex {
out := Option (TSyntax `tactic)
ctx := Array (Array Term)
trigger ctx stx :=
match stx with
| `(tactic| intro%$x $args*) => .continue ((ctx.getD #[]).push
... | def | Tactic | [
"Mathlib.Tactic.TacticAnalysis",
"Mathlib.Tactic.ExtractGoal",
"Mathlib.Tactic.MinImports",
"Lean.Elab.Tactic.Meta",
"Lean.Elab.Command"
] | Mathlib/Tactic/TacticAnalysis/Declarations.lean | introMerge | null |
@[to_additive /-- Addition is commutative -/]
mul_comm' {α} [CommSemigroup α] (x y : α) : x * y = y * x := CommSemigroup.mul_comm
```
The transport tries to do the right thing in most cases using several
heuristics described below. However, in some cases it fails, and
requires manual intervention.
Use the `to_additive... | theorem | Tactic | [
"Batteries.Tactic.Trans",
"Lean.Compiler.NoncomputableAttr",
"Lean.Elab.Tactic.Ext",
"Lean.Meta.Tactic.Rfl",
"Lean.Meta.Tactic.Symm",
"Lean.Meta.Tactic.TryThis",
"Mathlib.Data.Array.Defs",
"Mathlib.Data.Nat.Notation",
"Mathlib.Lean.Expr.ReplaceRec",
"Mathlib.Lean.Meta.Simp",
"Mathlib.Lean.Name",... | Mathlib/Tactic/ToAdditive/Frontend.lean | mul_comm' | An attribute that tells `@[to_additive]` that certain arguments of this definition are not
involved when using `@[to_additive]`.
This helps the heuristic of `@[to_additive]` by also transforming definitions if `ℕ` or another
fixed type occurs as one of these arguments. -/
syntax (name := to_additive_ignore_args) "to_ad... |
Pow_lemma ... :=
```
In the above example, the `simp` is added to all 3 lemmas. All other options to `to_additive`
(like the generated name or `(reorder := ...)`) are not passed down,
and can be given manually to each individual `to_additive` call. | lemma | Tactic | [
"Batteries.Tactic.Trans",
"Lean.Compiler.NoncomputableAttr",
"Lean.Elab.Tactic.Ext",
"Lean.Meta.Tactic.Rfl",
"Lean.Meta.Tactic.Symm",
"Lean.Meta.Tactic.TryThis",
"Mathlib.Data.Array.Defs",
"Mathlib.Data.Nat.Notation",
"Mathlib.Lean.Expr.ReplaceRec",
"Mathlib.Lean.Meta.Simp",
"Mathlib.Lean.Name",... | Mathlib/Tactic/ToAdditive/Frontend.lean | Pow_lemma | null |
findTranslation? (env : Environment) : Name → Option Name :=
(ToAdditive.translations.getState env).find? | def | Tactic | [
"Batteries.Tactic.Trans",
"Lean.Compiler.NoncomputableAttr",
"Lean.Elab.Tactic.Ext",
"Lean.Meta.Tactic.Rfl",
"Lean.Meta.Tactic.Symm",
"Lean.Meta.Tactic.TryThis",
"Mathlib.Data.Array.Defs",
"Mathlib.Data.Nat.Notation",
"Mathlib.Lean.Expr.ReplaceRec",
"Mathlib.Lean.Meta.Simp",
"Mathlib.Lean.Name",... | Mathlib/Tactic/ToAdditive/Frontend.lean | findTranslation | Linter, mostly used by `@[to_additive]`, that checks that the source declaration doesn't have
certain attributes -/
register_option linter.existingAttributeWarning : Bool := {
defValue := true
descr := "Linter, mostly used by `@[to_additive]`, that checks that the source declaration \
doesn't have certain attri... |
findPrefixTranslation (env : Environment) (nm : Name) : Name :=
nm.mapPrefix (findTranslation? env) | def | Tactic | [
"Batteries.Tactic.Trans",
"Lean.Compiler.NoncomputableAttr",
"Lean.Elab.Tactic.Ext",
"Lean.Meta.Tactic.Rfl",
"Lean.Meta.Tactic.Symm",
"Lean.Meta.Tactic.TryThis",
"Mathlib.Data.Array.Defs",
"Mathlib.Data.Nat.Notation",
"Mathlib.Lean.Expr.ReplaceRec",
"Mathlib.Lean.Meta.Simp",
"Mathlib.Lean.Name",... | Mathlib/Tactic/ToAdditive/Frontend.lean | findPrefixTranslation | Get the multiplicative → additive translation for the given name,
falling back to translating a prefix of the name if the full name can't be translated.
This allows translating automatically generated declarations such as `IsRegular.casesOn`. |
insertTranslation (src tgt : Name) (failIfExists := true) : CoreM Unit := do
if let some tgt' := findTranslation? (← getEnv) src then
if failIfExists then
throwError "The translation {src} ↦ {tgt'} already exists"
else
trace[to_additive] "The translation {src} ↦ {tgt'} already exists"
return... | def | Tactic | [
"Batteries.Tactic.Trans",
"Lean.Compiler.NoncomputableAttr",
"Lean.Elab.Tactic.Ext",
"Lean.Meta.Tactic.Rfl",
"Lean.Meta.Tactic.Symm",
"Lean.Meta.Tactic.TryThis",
"Mathlib.Data.Array.Defs",
"Mathlib.Data.Nat.Notation",
"Mathlib.Lean.Expr.ReplaceRec",
"Mathlib.Lean.Meta.Simp",
"Mathlib.Lean.Name",... | Mathlib/Tactic/ToAdditive/Frontend.lean | insertTranslation | Add a (multiplicative → additive) name translation to the translations map. |
ArgInfo where
/-- The arguments that should be reordered by `to_additive`, using cycle notation. -/
reorder : List (List Nat) := []
/-- The argument used to determine whether this constant should be translated. -/
relevantArg : Nat := 0 | structure | Tactic | [
"Batteries.Tactic.Trans",
"Lean.Compiler.NoncomputableAttr",
"Lean.Elab.Tactic.Ext",
"Lean.Meta.Tactic.Rfl",
"Lean.Meta.Tactic.Symm",
"Lean.Meta.Tactic.TryThis",
"Mathlib.Data.Array.Defs",
"Mathlib.Data.Nat.Notation",
"Mathlib.Lean.Expr.ReplaceRec",
"Mathlib.Lean.Meta.Simp",
"Mathlib.Lean.Name",... | Mathlib/Tactic/ToAdditive/Frontend.lean | ArgInfo | `ArgInfo` stores information about how a constant should be translated. |
insertTranslationAndInfo (src tgt : Name) (argInfo : ArgInfo) (failIfExists := true) :
CoreM Unit := do
insertTranslation src tgt failIfExists
if argInfo.reorder != [] then
trace[to_additive] "@[to_additive] will reorder the arguments of {tgt} by {argInfo.reorder}."
reorderAttr.add src argInfo.reorder
... | def | Tactic | [
"Batteries.Tactic.Trans",
"Lean.Compiler.NoncomputableAttr",
"Lean.Elab.Tactic.Ext",
"Lean.Meta.Tactic.Rfl",
"Lean.Meta.Tactic.Symm",
"Lean.Meta.Tactic.TryThis",
"Mathlib.Data.Array.Defs",
"Mathlib.Data.Nat.Notation",
"Mathlib.Lean.Expr.ReplaceRec",
"Mathlib.Lean.Meta.Simp",
"Mathlib.Lean.Name",... | Mathlib/Tactic/ToAdditive/Frontend.lean | insertTranslationAndInfo | Add a name translation to the translations map and add the `argInfo` information to `src`. |
Config : Type where
/-- View the trace of the to_additive procedure.
Equivalent to `set_option trace.to_additive true`. -/
trace : Bool := false
/-- The name of the target (the additive declaration). -/
tgt : Name := Name.anonymous
/-- An optional doc string. -/
doc : Option String := none
/-- If `allow... | structure | Tactic | [
"Batteries.Tactic.Trans",
"Lean.Compiler.NoncomputableAttr",
"Lean.Elab.Tactic.Ext",
"Lean.Meta.Tactic.Rfl",
"Lean.Meta.Tactic.Symm",
"Lean.Meta.Tactic.TryThis",
"Mathlib.Data.Array.Defs",
"Mathlib.Data.Nat.Notation",
"Mathlib.Lean.Expr.ReplaceRec",
"Mathlib.Lean.Meta.Simp",
"Mathlib.Lean.Name",... | Mathlib/Tactic/ToAdditive/Frontend.lean | Config | `Config` is the type of the arguments that can be provided to `to_additive`. |
etaExpandN (n : Nat) (e : Expr) : MetaM Expr := do
forallBoundedTelescope (← inferType e) (some n) fun xs _ ↦ mkLambdaFVars xs (mkAppN e xs) | def | Tactic | [
"Batteries.Tactic.Trans",
"Lean.Compiler.NoncomputableAttr",
"Lean.Elab.Tactic.Ext",
"Lean.Meta.Tactic.Rfl",
"Lean.Meta.Tactic.Symm",
"Lean.Meta.Tactic.TryThis",
"Mathlib.Data.Array.Defs",
"Mathlib.Data.Nat.Notation",
"Mathlib.Lean.Expr.ReplaceRec",
"Mathlib.Lean.Meta.Simp",
"Mathlib.Lean.Name",... | Mathlib/Tactic/ToAdditive/Frontend.lean | etaExpandN | Eta expands `e` at most `n` times. |
expand (e : Expr) : MetaM Expr := do
let env ← getEnv
let reorderFn : Name → List (List ℕ) := fun nm ↦ (reorderAttr.find? env nm |>.getD [])
let e₂ ← Lean.Meta.transform (input := e) (skipConstInApp := true)
(post := fun e => return .done e) fun e ↦
e.withApp fun f args ↦ do
match f with
| .proj n... | def | Tactic | [
"Batteries.Tactic.Trans",
"Lean.Compiler.NoncomputableAttr",
"Lean.Elab.Tactic.Ext",
"Lean.Meta.Tactic.Rfl",
"Lean.Meta.Tactic.Symm",
"Lean.Meta.Tactic.TryThis",
"Mathlib.Data.Array.Defs",
"Mathlib.Data.Nat.Notation",
"Mathlib.Lean.Expr.ReplaceRec",
"Mathlib.Lean.Meta.Simp",
"Mathlib.Lean.Name",... | Mathlib/Tactic/ToAdditive/Frontend.lean | expand | `e.expand` eta-expands all expressions that have as head a constant `n` in `reorder`.
They are expanded until they are applied to one more argument than the maximum in `reorder.find n`.
It also expands all kernel projections that have as head a constant `n` in `reorder`. |
unsafe additiveTestUnsafe (env : Environment) (e : Expr) (dontTranslate : Array FVarId) :
Option (Name ⊕ FVarId) :=
let rec visit (e : Expr) (inApp := false) : OptionT (StateM (PtrSet Expr)) (Name ⊕ FVarId) := do
if e.isConst then
if (dontTranslateAttr.find? env e.constName).isNone &&
(inApp || ... | def | Tactic | [
"Batteries.Tactic.Trans",
"Lean.Compiler.NoncomputableAttr",
"Lean.Elab.Tactic.Ext",
"Lean.Meta.Tactic.Rfl",
"Lean.Meta.Tactic.Symm",
"Lean.Meta.Tactic.TryThis",
"Mathlib.Data.Array.Defs",
"Mathlib.Data.Nat.Notation",
"Mathlib.Lean.Expr.ReplaceRec",
"Mathlib.Lean.Meta.Simp",
"Mathlib.Lean.Name",... | Mathlib/Tactic/ToAdditive/Frontend.lean | additiveTestUnsafe | Implementation function for `additiveTest`.
Failure means that in that subexpression there is no constant that blocks `e` from being translated.
We cache previous applications of the function, using an expression cache using ptr equality
to avoid visiting the same subexpression many times. Note that we only need to cac... |
additiveTest (env : Environment) (e : Expr) (dontTranslate : Array FVarId := #[]) :
Option (Name ⊕ FVarId) :=
unsafe additiveTestUnsafe env e dontTranslate | def | Tactic | [
"Batteries.Tactic.Trans",
"Lean.Compiler.NoncomputableAttr",
"Lean.Elab.Tactic.Ext",
"Lean.Meta.Tactic.Rfl",
"Lean.Meta.Tactic.Symm",
"Lean.Meta.Tactic.TryThis",
"Mathlib.Data.Array.Defs",
"Mathlib.Data.Nat.Notation",
"Mathlib.Lean.Expr.ReplaceRec",
"Mathlib.Lean.Meta.Simp",
"Mathlib.Lean.Name",... | Mathlib/Tactic/ToAdditive/Frontend.lean | additiveTest | `additiveTest e` tests whether the expression `e` contains a constant
`nm` that is not applied to any arguments, and such that `translations.find?[nm] = none`.
This is used in `@[to_additive]` for deciding which subexpressions to transform: we only transform
constants if `additiveTest` applied to their relevant argumen... |
_root_.List.swapFirstTwo {α : Type _} : List α → List α
| [] => []
| [x] => [x]
| x::y::l => y::x::l | def | Tactic | [
"Batteries.Tactic.Trans",
"Lean.Compiler.NoncomputableAttr",
"Lean.Elab.Tactic.Ext",
"Lean.Meta.Tactic.Rfl",
"Lean.Meta.Tactic.Symm",
"Lean.Meta.Tactic.TryThis",
"Mathlib.Data.Array.Defs",
"Mathlib.Data.Nat.Notation",
"Mathlib.Lean.Expr.ReplaceRec",
"Mathlib.Lean.Meta.Simp",
"Mathlib.Lean.Name",... | Mathlib/Tactic/ToAdditive/Frontend.lean | _root_.List.swapFirstTwo | Swap the first two elements of a list |
changeNumeral : Expr → Expr
| .lit (.natVal 1) => mkRawNatLit 0
| e => e | def | Tactic | [
"Batteries.Tactic.Trans",
"Lean.Compiler.NoncomputableAttr",
"Lean.Elab.Tactic.Ext",
"Lean.Meta.Tactic.Rfl",
"Lean.Meta.Tactic.Symm",
"Lean.Meta.Tactic.TryThis",
"Mathlib.Data.Array.Defs",
"Mathlib.Data.Nat.Notation",
"Mathlib.Lean.Expr.ReplaceRec",
"Mathlib.Lean.Meta.Simp",
"Mathlib.Lean.Name",... | Mathlib/Tactic/ToAdditive/Frontend.lean | changeNumeral | Change the numeral `nat_lit 1` to the numeral `nat_lit 0`.
Leave all other expressions unchanged. |
applyReplacementFun (e : Expr) (dontTranslate : Array FVarId := #[]) : MetaM Expr := do
let e' := aux (← getEnv) (← getBoolOption `trace.to_additive_detail) (← expand e)
e'.forEach fun
| .const n .. => do
if !(← hasConst (skipRealize := false) n) && isReservedName (← getEnv) n then
executeReserved... | def | Tactic | [
"Batteries.Tactic.Trans",
"Lean.Compiler.NoncomputableAttr",
"Lean.Elab.Tactic.Ext",
"Lean.Meta.Tactic.Rfl",
"Lean.Meta.Tactic.Symm",
"Lean.Meta.Tactic.TryThis",
"Mathlib.Data.Array.Defs",
"Mathlib.Data.Nat.Notation",
"Mathlib.Lean.Expr.ReplaceRec",
"Mathlib.Lean.Meta.Simp",
"Mathlib.Lean.Name",... | Mathlib/Tactic/ToAdditive/Frontend.lean | applyReplacementFun | `applyReplacementFun e` replaces the expression `e` with its additive counterpart.
It translates each identifier (inductive type, defined function etc) in an expression, unless
* The identifier occurs in an application with first argument `arg`; and
* `test arg` is false.
However, if `f` is in the dictionary `relevant`... |
renameBinderNames (src : Expr) : Expr :=
src.mapForallBinderNames fun
| .str p s => .str p (guessName s)
| n => n | def | Tactic | [
"Batteries.Tactic.Trans",
"Lean.Compiler.NoncomputableAttr",
"Lean.Elab.Tactic.Ext",
"Lean.Meta.Tactic.Rfl",
"Lean.Meta.Tactic.Symm",
"Lean.Meta.Tactic.TryThis",
"Mathlib.Data.Array.Defs",
"Mathlib.Data.Nat.Notation",
"Mathlib.Lean.Expr.ReplaceRec",
"Mathlib.Lean.Meta.Simp",
"Mathlib.Lean.Name",... | Mathlib/Tactic/ToAdditive/Frontend.lean | renameBinderNames | Rename binder names in pi type. |
reorderForall (reorder : List (List Nat)) (src : Expr) : MetaM Expr := do
if let some maxReorder := reorder.flatten.max? then
forallBoundedTelescope src (some (maxReorder + 1)) fun xs e => do
if xs.size = maxReorder + 1 then
mkForallFVars (xs.permute! reorder) e
else
throwError "the pe... | def | Tactic | [
"Batteries.Tactic.Trans",
"Lean.Compiler.NoncomputableAttr",
"Lean.Elab.Tactic.Ext",
"Lean.Meta.Tactic.Rfl",
"Lean.Meta.Tactic.Symm",
"Lean.Meta.Tactic.TryThis",
"Mathlib.Data.Array.Defs",
"Mathlib.Data.Nat.Notation",
"Mathlib.Lean.Expr.ReplaceRec",
"Mathlib.Lean.Meta.Simp",
"Mathlib.Lean.Name",... | Mathlib/Tactic/ToAdditive/Frontend.lean | reorderForall | Reorder pi-binders. See doc of `reorderAttr` for the interpretation of the argument |
reorderLambda (reorder : List (List Nat)) (src : Expr) : MetaM Expr := do
if let some maxReorder := reorder.flatten.max? then
let maxReorder := maxReorder + 1
lambdaBoundedTelescope src maxReorder fun xs e => do
if xs.size = maxReorder then
mkLambdaFVars (xs.permute! reorder) e
else
... | def | Tactic | [
"Batteries.Tactic.Trans",
"Lean.Compiler.NoncomputableAttr",
"Lean.Elab.Tactic.Ext",
"Lean.Meta.Tactic.Rfl",
"Lean.Meta.Tactic.Symm",
"Lean.Meta.Tactic.TryThis",
"Mathlib.Data.Array.Defs",
"Mathlib.Data.Nat.Notation",
"Mathlib.Lean.Expr.ReplaceRec",
"Mathlib.Lean.Meta.Simp",
"Mathlib.Lean.Name",... | Mathlib/Tactic/ToAdditive/Frontend.lean | reorderLambda | Reorder lambda-binders. See doc of `reorderAttr` for the interpretation of the argument |
applyReplacementForall (dontTranslate : List Nat) (e : Expr) :
MetaM Expr := do
if let some maxDont := dontTranslate.max? then
forallBoundedTelescope e (some (maxDont + 1)) fun xs e => do
let xs := xs.map (·.fvarId!)
let dontTranslate := dontTranslate.filterMap (xs[·]?) |>.toArray
let mut e ... | def | Tactic | [
"Batteries.Tactic.Trans",
"Lean.Compiler.NoncomputableAttr",
"Lean.Elab.Tactic.Ext",
"Lean.Meta.Tactic.Rfl",
"Lean.Meta.Tactic.Symm",
"Lean.Meta.Tactic.TryThis",
"Mathlib.Data.Array.Defs",
"Mathlib.Data.Nat.Notation",
"Mathlib.Lean.Expr.ReplaceRec",
"Mathlib.Lean.Meta.Simp",
"Mathlib.Lean.Name",... | Mathlib/Tactic/ToAdditive/Frontend.lean | applyReplacementForall | Run `applyReplacementFun` on an expression `∀ x₁ .. xₙ, e`,
making sure not to translate type-classes on `xᵢ` if `i` is in `dontTranslate`. |
applyReplacementLambda (dontTranslate : List Nat) (e : Expr) :
MetaM Expr := do
if let some maxDont := dontTranslate.max? then
lambdaBoundedTelescope e (maxDont + 1) fun xs e => do
let xs := xs.map (·.fvarId!)
let dontTranslate := dontTranslate.filterMap (xs[·]?) |>.toArray
let mut e ← apply... | def | Tactic | [
"Batteries.Tactic.Trans",
"Lean.Compiler.NoncomputableAttr",
"Lean.Elab.Tactic.Ext",
"Lean.Meta.Tactic.Rfl",
"Lean.Meta.Tactic.Symm",
"Lean.Meta.Tactic.TryThis",
"Mathlib.Data.Array.Defs",
"Mathlib.Data.Nat.Notation",
"Mathlib.Lean.Expr.ReplaceRec",
"Mathlib.Lean.Meta.Simp",
"Mathlib.Lean.Name",... | Mathlib/Tactic/ToAdditive/Frontend.lean | applyReplacementLambda | Run `applyReplacementFun` on an expression `fun x₁ .. xₙ ↦ e`,
making sure not to translate type-classes on `xᵢ` if `i` is in `dontTranslate`. |
declUnfoldAuxLemmas (decl : ConstantInfo) : MetaM ConstantInfo := do
let mut decl := decl
decl := decl.updateType <| ← unfoldAuxLemmas decl.type
if let some v := decl.value? then
trace[to_additive] "value before unfold:{indentExpr v}"
decl := decl.updateValue <| ← unfoldAuxLemmas v
trace[to_additive] ... | def | Tactic | [
"Batteries.Tactic.Trans",
"Lean.Compiler.NoncomputableAttr",
"Lean.Elab.Tactic.Ext",
"Lean.Meta.Tactic.Rfl",
"Lean.Meta.Tactic.Symm",
"Lean.Meta.Tactic.TryThis",
"Mathlib.Data.Array.Defs",
"Mathlib.Data.Nat.Notation",
"Mathlib.Lean.Expr.ReplaceRec",
"Mathlib.Lean.Meta.Simp",
"Mathlib.Lean.Name",... | Mathlib/Tactic/ToAdditive/Frontend.lean | declUnfoldAuxLemmas | Unfold auxlemmas in the type and value. |
getDontTranslates (given : List Ident) (type : Expr) : MetaM (List Nat) := do
forallTelescope type fun xs _ => do
given.mapM fun id => withRef id.raw <| do
let fvarId ← getFVarFromUserName id.getId
return (xs.idxOf? fvarId).get! | def | Tactic | [
"Batteries.Tactic.Trans",
"Lean.Compiler.NoncomputableAttr",
"Lean.Elab.Tactic.Ext",
"Lean.Meta.Tactic.Rfl",
"Lean.Meta.Tactic.Symm",
"Lean.Meta.Tactic.TryThis",
"Mathlib.Data.Array.Defs",
"Mathlib.Data.Nat.Notation",
"Mathlib.Lean.Expr.ReplaceRec",
"Mathlib.Lean.Meta.Simp",
"Mathlib.Lean.Name",... | Mathlib/Tactic/ToAdditive/Frontend.lean | getDontTranslates | Given a list of variable local identifiers that shouldn't be translated,
determine the arguments that shouldn't be translated.
TODO: Currently, this function doesn't deduce any `dont_translate` types from `type`.
In the future we would like that the presence of `MonoidAlgebra k G` will automatically
flag `k` as a type... |
updateDecl (tgt : Name) (srcDecl : ConstantInfo) (reorder : List (List Nat))
(dont : List Ident) : MetaM ConstantInfo := do
let mut decl := srcDecl.updateName tgt
if 0 ∈ reorder.flatten then
decl := decl.updateLevelParams decl.levelParams.swapFirstTwo
let dont ← getDontTranslates dont srcDecl.type
decl ... | def | Tactic | [
"Batteries.Tactic.Trans",
"Lean.Compiler.NoncomputableAttr",
"Lean.Elab.Tactic.Ext",
"Lean.Meta.Tactic.Rfl",
"Lean.Meta.Tactic.Symm",
"Lean.Meta.Tactic.TryThis",
"Mathlib.Data.Array.Defs",
"Mathlib.Data.Nat.Notation",
"Mathlib.Lean.Expr.ReplaceRec",
"Mathlib.Lean.Meta.Simp",
"Mathlib.Lean.Name",... | Mathlib/Tactic/ToAdditive/Frontend.lean | updateDecl | Run applyReplacementFun on the given `srcDecl` to make a new declaration with name `tgt` |
declAbstractNestedProofs (decl : ConstantInfo) : MetaM ConstantInfo := do
if decl matches .defnInfo _ then
return decl.updateValue <| ← withDeclNameForAuxNaming decl.name do
Meta.abstractNestedProofs decl.value!
else
return decl | def | Tactic | [
"Batteries.Tactic.Trans",
"Lean.Compiler.NoncomputableAttr",
"Lean.Elab.Tactic.Ext",
"Lean.Meta.Tactic.Rfl",
"Lean.Meta.Tactic.Symm",
"Lean.Meta.Tactic.TryThis",
"Mathlib.Data.Array.Defs",
"Mathlib.Data.Nat.Notation",
"Mathlib.Lean.Expr.ReplaceRec",
"Mathlib.Lean.Meta.Simp",
"Mathlib.Lean.Name",... | Mathlib/Tactic/ToAdditive/Frontend.lean | declAbstractNestedProofs | Abstracts the nested proofs in the value of `decl` if it is a def. |
findTargetName (env : Environment) (src pre tgt_pre : Name) : CoreM Name :=
/- This covers auxiliary declarations like `match_i` and `proof_i`. -/
if let some post := pre.isPrefixOf? src then
return tgt_pre ++ post
/- This covers equation lemmas (for other declarations). -/
else if let some post := privateT... | def | Tactic | [
"Batteries.Tactic.Trans",
"Lean.Compiler.NoncomputableAttr",
"Lean.Elab.Tactic.Ext",
"Lean.Meta.Tactic.Rfl",
"Lean.Meta.Tactic.Symm",
"Lean.Meta.Tactic.TryThis",
"Mathlib.Data.Array.Defs",
"Mathlib.Data.Nat.Notation",
"Mathlib.Lean.Expr.ReplaceRec",
"Mathlib.Lean.Meta.Simp",
"Mathlib.Lean.Name",... | Mathlib/Tactic/ToAdditive/Frontend.lean | findTargetName | Find the target name of `pre` and all created auxiliary declarations. |
findAuxDecls (e : Expr) (pre : Name) : NameSet :=
e.foldConsts ∅ fun n l ↦
if n.getPrefix == pre || isPrivateName n || n.hasMacroScopes then
l.insert n
else
l | def | Tactic | [
"Batteries.Tactic.Trans",
"Lean.Compiler.NoncomputableAttr",
"Lean.Elab.Tactic.Ext",
"Lean.Meta.Tactic.Rfl",
"Lean.Meta.Tactic.Symm",
"Lean.Meta.Tactic.TryThis",
"Mathlib.Data.Array.Defs",
"Mathlib.Data.Nat.Notation",
"Mathlib.Lean.Expr.ReplaceRec",
"Mathlib.Lean.Meta.Simp",
"Mathlib.Lean.Name",... | Mathlib/Tactic/ToAdditive/Frontend.lean | findAuxDecls | Returns a `NameSet` of all auxiliary constants in `e` that might have been generated
when adding `pre` to the environment.
Examples include `pre.match_5` and
`_private.Mathlib.MyFile.someOtherNamespace.someOtherDeclaration._eq_2`.
The last two examples may or may not have been generated by this declaration.
The last ex... |
partial transformDeclAux
(cfg : Config) (pre tgt_pre : Name) : Name → CoreM Unit := fun src ↦ do
let env ← getEnv
trace[to_additive_detail] "visiting {src}"
if (findTranslation? env src).isSome && src != pre then
return
if src != pre && !src.isInternalDetail then
throwError "The declaration {pre} ... | def | Tactic | [
"Batteries.Tactic.Trans",
"Lean.Compiler.NoncomputableAttr",
"Lean.Elab.Tactic.Ext",
"Lean.Meta.Tactic.Rfl",
"Lean.Meta.Tactic.Symm",
"Lean.Meta.Tactic.TryThis",
"Mathlib.Data.Array.Defs",
"Mathlib.Data.Nat.Notation",
"Mathlib.Lean.Expr.ReplaceRec",
"Mathlib.Lean.Meta.Simp",
"Mathlib.Lean.Name",... | Mathlib/Tactic/ToAdditive/Frontend.lean | transformDeclAux | Transform the declaration `src` and all declarations `pre._proof_i` occurring in `src`
using the transforms dictionary.
`replace_all`, `trace`, `ignore` and `reorder` are configuration options.
`pre` is the declaration that got the `@[to_additive]` attribute and `tgt_pre` is the target of this
declaration. |
copyInstanceAttribute (src tgt : Name) : CoreM Unit := do
if let some prio ← getInstancePriority? src then
let attr_kind := (← getInstanceAttrKind? src).getD .global
trace[to_additive_detail] "Making {tgt} an instance with priority {prio}."
addInstance tgt attr_kind prio |>.run' | def | Tactic | [
"Batteries.Tactic.Trans",
"Lean.Compiler.NoncomputableAttr",
"Lean.Elab.Tactic.Ext",
"Lean.Meta.Tactic.Rfl",
"Lean.Meta.Tactic.Symm",
"Lean.Meta.Tactic.TryThis",
"Mathlib.Data.Array.Defs",
"Mathlib.Data.Nat.Notation",
"Mathlib.Lean.Expr.ReplaceRec",
"Mathlib.Lean.Meta.Simp",
"Mathlib.Lean.Name",... | Mathlib/Tactic/ToAdditive/Frontend.lean | copyInstanceAttribute | Copy the instance attribute in a `to_additive`
[todo] it seems not to work when the `to_additive` is added as an attribute later. |
warnAttrCore (stx : Syntax) (f : Environment → Name → Bool)
(thisAttr attrName src tgt : Name) : CoreM Unit := do
if f (← getEnv) src then
Linter.logLintIf linter.existingAttributeWarning stx <|
m!"The source declaration {src} was given attribute {attrName} before calling @[{thisAttr}]. \
The p... | def | Tactic | [
"Batteries.Tactic.Trans",
"Lean.Compiler.NoncomputableAttr",
"Lean.Elab.Tactic.Ext",
"Lean.Meta.Tactic.Rfl",
"Lean.Meta.Tactic.Symm",
"Lean.Meta.Tactic.TryThis",
"Mathlib.Data.Array.Defs",
"Mathlib.Data.Nat.Notation",
"Mathlib.Lean.Expr.ReplaceRec",
"Mathlib.Lean.Meta.Simp",
"Mathlib.Lean.Name",... | Mathlib/Tactic/ToAdditive/Frontend.lean | warnAttrCore | Warn the user when the multiplicative declaration has an attribute. |
warnAttr {α β : Type} [Inhabited β] (stx : Syntax) (attr : SimpleScopedEnvExtension α β)
(f : β → Name → Bool) (thisAttr attrName src tgt : Name) : CoreM Unit :=
warnAttrCore stx (f <| attr.getState ·) thisAttr attrName src tgt | def | Tactic | [
"Batteries.Tactic.Trans",
"Lean.Compiler.NoncomputableAttr",
"Lean.Elab.Tactic.Ext",
"Lean.Meta.Tactic.Rfl",
"Lean.Meta.Tactic.Symm",
"Lean.Meta.Tactic.TryThis",
"Mathlib.Data.Array.Defs",
"Mathlib.Data.Nat.Notation",
"Mathlib.Lean.Expr.ReplaceRec",
"Mathlib.Lean.Meta.Simp",
"Mathlib.Lean.Name",... | Mathlib/Tactic/ToAdditive/Frontend.lean | warnAttr | Warn the user when the multiplicative declaration has a simple scoped attribute. |
warnParametricAttr {β : Type} [Inhabited β] (stx : Syntax) (attr : ParametricAttribute β)
(thisAttr attrName src tgt : Name) : CoreM Unit :=
warnAttrCore stx (attr.getParam? · · |>.isSome) thisAttr attrName src tgt | def | Tactic | [
"Batteries.Tactic.Trans",
"Lean.Compiler.NoncomputableAttr",
"Lean.Elab.Tactic.Ext",
"Lean.Meta.Tactic.Rfl",
"Lean.Meta.Tactic.Symm",
"Lean.Meta.Tactic.TryThis",
"Mathlib.Data.Array.Defs",
"Mathlib.Data.Nat.Notation",
"Mathlib.Lean.Expr.ReplaceRec",
"Mathlib.Lean.Meta.Simp",
"Mathlib.Lean.Name",... | Mathlib/Tactic/ToAdditive/Frontend.lean | warnParametricAttr | Warn the user when the multiplicative declaration has a parametric attribute. |
additivizeLemmas {m : Type → Type} [Monad m] [MonadError m] [MonadLiftT CoreM m]
(names : Array Name) (argInfo : ArgInfo) (desc : String) (t : Name → m (Array Name)) :
m Unit := do
let auxLemmas ← names.mapM t
let nLemmas := auxLemmas[0]!.size
for (nm, lemmas) in names.zip auxLemmas do
unless lemmas.s... | def | Tactic | [
"Batteries.Tactic.Trans",
"Lean.Compiler.NoncomputableAttr",
"Lean.Elab.Tactic.Ext",
"Lean.Meta.Tactic.Rfl",
"Lean.Meta.Tactic.Symm",
"Lean.Meta.Tactic.TryThis",
"Mathlib.Data.Array.Defs",
"Mathlib.Data.Nat.Notation",
"Mathlib.Lean.Expr.ReplaceRec",
"Mathlib.Lean.Meta.Simp",
"Mathlib.Lean.Name",... | Mathlib/Tactic/ToAdditive/Frontend.lean | additivizeLemmas | `additivizeLemmas names argInfo desc t` runs `t` on all elements of `names`
and adds translations between the generated lemmas (the output of `t`).
`names` must be non-empty. |
findMultiplicativeArg (nm : Name) : MetaM Nat := do
forallTelescopeReducing (← getConstInfo nm).type fun xs ty ↦ do
let env ← getEnv
let multArg? (tgt : Expr) : Option Nat := do
let c ← tgt.getAppFn.constName?
guard (findTranslation? env c).isSome
let relevantArg := (relevantArgAttr.find? en... | def | Tactic | [
"Batteries.Tactic.Trans",
"Lean.Compiler.NoncomputableAttr",
"Lean.Elab.Tactic.Ext",
"Lean.Meta.Tactic.Rfl",
"Lean.Meta.Tactic.Symm",
"Lean.Meta.Tactic.TryThis",
"Mathlib.Data.Array.Defs",
"Mathlib.Data.Nat.Notation",
"Mathlib.Lean.Expr.ReplaceRec",
"Mathlib.Lean.Meta.Simp",
"Mathlib.Lean.Name",... | Mathlib/Tactic/ToAdditive/Frontend.lean | findMultiplicativeArg | Find the argument of `nm` that appears in the first multiplicative (type-class) argument.
Returns 1 if there are no types with a multiplicative class as arguments.
E.g. `Prod.instGroup` returns 1, and `Pi.instOne` returns 2.
Note: we only consider the relevant argument (`(relevant_arg := ...)`) of each type-class.
E.g.... |
targetName (cfg : Config) (src : Name) : CoreM Name := do
if cfg.self then
if cfg.tgt != .anonymous then
throwError m!"`to_additive self` ignores the provided name {cfg.tgt}"
return src
let .str pre s := src | throwError "to_additive: can't transport {src}"
trace[to_additive_detail] "The name {s} sp... | def | Tactic | [
"Batteries.Tactic.Trans",
"Lean.Compiler.NoncomputableAttr",
"Lean.Elab.Tactic.Ext",
"Lean.Meta.Tactic.Rfl",
"Lean.Meta.Tactic.Symm",
"Lean.Meta.Tactic.TryThis",
"Mathlib.Data.Array.Defs",
"Mathlib.Data.Nat.Notation",
"Mathlib.Lean.Expr.ReplaceRec",
"Mathlib.Lean.Meta.Simp",
"Mathlib.Lean.Name",... | Mathlib/Tactic/ToAdditive/Frontend.lean | targetName | Return the provided target name or autogenerate one if one was not provided. |
proceedFieldsAux (src tgt : Name) (argInfo : ArgInfo) (f : Name → Array Name) : CoreM Unit := do
let srcFields := f src
let tgtFields := f tgt
if srcFields.size != tgtFields.size then
throwError "Failed to map fields of {src}, {tgt} with {srcFields} ↦ {tgtFields}.\n \
Lengths do not match."
for srcFie... | def | Tactic | [
"Batteries.Tactic.Trans",
"Lean.Compiler.NoncomputableAttr",
"Lean.Elab.Tactic.Ext",
"Lean.Meta.Tactic.Rfl",
"Lean.Meta.Tactic.Symm",
"Lean.Meta.Tactic.TryThis",
"Mathlib.Data.Array.Defs",
"Mathlib.Data.Nat.Notation",
"Mathlib.Lean.Expr.ReplaceRec",
"Mathlib.Lean.Meta.Simp",
"Mathlib.Lean.Name",... | Mathlib/Tactic/ToAdditive/Frontend.lean | proceedFieldsAux | 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`. |
proceedFields (src tgt : Name) (argInfo : ArgInfo) : CoreM Unit := do
let env ← getEnv
let aux := proceedFieldsAux src tgt argInfo
aux fun declName ↦
if isStructure env declName then
let info := getStructureInfo env declName
Array.ofFn (n := info.fieldNames.size) (info.getProjFn? · |>.get!)
el... | def | Tactic | [
"Batteries.Tactic.Trans",
"Lean.Compiler.NoncomputableAttr",
"Lean.Elab.Tactic.Ext",
"Lean.Meta.Tactic.Rfl",
"Lean.Meta.Tactic.Symm",
"Lean.Meta.Tactic.TryThis",
"Mathlib.Data.Array.Defs",
"Mathlib.Data.Nat.Notation",
"Mathlib.Lean.Expr.ReplaceRec",
"Mathlib.Lean.Meta.Simp",
"Mathlib.Lean.Name",... | Mathlib/Tactic/ToAdditive/Frontend.lean | proceedFields | Add the structure fields of `src` to the translations dictionary
so that future uses of `to_additive` will map them to the corresponding `tgt` fields. |
elabToAdditive : Syntax → CoreM Config
| `(attr| to_additive%$tk $[?%$trace]? $existing?
$[$opts:toAdditiveOption]* $[$tgt]? $[$doc]?) => do
let mut attrs := #[]
let mut reorder := []
let mut relevantArg? := none
let mut dontTranslate := []
for opt in opts do
match opt with
| `(t... | def | Tactic | [
"Batteries.Tactic.Trans",
"Lean.Compiler.NoncomputableAttr",
"Lean.Elab.Tactic.Ext",
"Lean.Meta.Tactic.Rfl",
"Lean.Meta.Tactic.Symm",
"Lean.Meta.Tactic.TryThis",
"Mathlib.Data.Array.Defs",
"Mathlib.Data.Nat.Notation",
"Mathlib.Lean.Expr.ReplaceRec",
"Mathlib.Lean.Meta.Simp",
"Mathlib.Lean.Name",... | Mathlib/Tactic/ToAdditive/Frontend.lean | elabToAdditive | Elaboration of the configuration options for `to_additive`. |
partial applyAttributes (stx : Syntax) (rawAttrs : Array Syntax) (thisAttr src tgt : Name)
(argInfo : ArgInfo) : TermElabM (Array Name) := do
copyInstanceAttribute src tgt
if src != tgt && linter.existingAttributeWarning.get (← getOptions) then
let appliedAttrs ← getAllSimpAttrs src
if appliedAttrs.size... | def | Tactic | [
"Batteries.Tactic.Trans",
"Lean.Compiler.NoncomputableAttr",
"Lean.Elab.Tactic.Ext",
"Lean.Meta.Tactic.Rfl",
"Lean.Meta.Tactic.Symm",
"Lean.Meta.Tactic.TryThis",
"Mathlib.Data.Array.Defs",
"Mathlib.Data.Nat.Notation",
"Mathlib.Lean.Expr.ReplaceRec",
"Mathlib.Lean.Meta.Simp",
"Mathlib.Lean.Name",... | Mathlib/Tactic/ToAdditive/Frontend.lean | applyAttributes | Apply attributes to the multiplicative and additive declarations. |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.