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
lintFile (opts : LinterOptions) (path : FilePath) (exceptions : Array ErrorContext) : IO (Array ErrorContext × Option (Array String)) := do let mut errors := #[] let mut changes_made := false let contents ← IO.FS.readFile path let replaced := contents.crlfToLf if replaced != contents then changes_made := true errors := errors.push (ErrorContext.mk StyleError.windowsLineEnding 1 path) let lines := (replaced.splitOn "\n").toArray if isImportsOnlyFile lines then return (errors, if changes_made then some lines else none) let mut allOutput := #[] let mut changed := lines for lint in allLinters do let (err, changes) := lint opts changed allOutput := allOutput.append (Array.map (fun (e, n) ↦ #[(ErrorContext.mk e n path)]) err) if let some c := changes then changed := c changes_made := true errors := errors.append (allOutput.flatten.filter (fun e ↦ (e.find?_comparable exceptions).isNone)) return (errors, if changes_made then some changed else none)
def
Tactic
[ "Batteries.Data.String.Matcher", "Mathlib.Data.Nat.Notation", "Lake.Util.Casing" ]
Mathlib/Tactic/Linter/TextBased.lean
lintFile
Read a file and apply all text-based linters. Return a list of all unexpected errors, and, if some errors could be fixed automatically, the collection of all lines with every automatic fix applied. `exceptions` are any pre-existing style exceptions for this file.
lintModules (opts : LinterOptions) (nolints : Array String) (moduleNames : Array Lean.Name) (style : ErrorFormat) (fix : Bool) : IO UInt32 := do let styleExceptions := parseStyleExceptions nolints let mut numberErrorFiles : UInt32 := 0 let mut allUnexpectedErrors := #[] for module in moduleNames do let path := mkFilePath (module.components.map toString)|>.addExtension "lean" let (errors, changed) := ← lintFile opts path styleExceptions if let some c := changed then if fix then let _ := ← IO.FS.writeFile path ("\n".intercalate c.toList) if errors.size > 0 then allUnexpectedErrors := allUnexpectedErrors.append errors numberErrorFiles := numberErrorFiles + 1 if getLinterValue linter.pythonStyle opts then let args := if fix then #["--fix"] else #[] let output ← IO.Process.output { cmd := "./scripts/print-style-errors.sh", args := args } if output.exitCode != 0 then numberErrorFiles := numberErrorFiles + 1 IO.eprintln s!"error: `print-style-error.sh` exited with code {output.exitCode}" IO.eprint output.stderr else if output.stdout != "" then numberErrorFiles := numberErrorFiles + 1 IO.eprint output.stdout formatErrors allUnexpectedErrors style if allUnexpectedErrors.size > 0 then IO.eprintln s!"error: found {allUnexpectedErrors.size} new style error(s)" return numberErrorFiles
def
Tactic
[ "Batteries.Data.String.Matcher", "Mathlib.Data.Nat.Notation", "Lake.Util.Casing" ]
Mathlib/Tactic/Linter/TextBased.lean
lintModules
Enables the old Python-based style linters. -/ register_option linter.pythonStyle : Bool := { defValue := true } /-- Lint a collection of modules for style violations. Print formatted errors for all unexpected style violations to standard output; correct automatically fixable style errors if configured so. Return the number of files which had new style errors. `opts` contains the options defined in the Lakefile, determining which linters to enable. `nolints` is a list of style exceptions to take into account. `moduleNames` are the names of all the modules to lint, `mode` specifies what kind of output this script should produce, `fix` configures whether fixable errors should be corrected in-place.
modulesNotUpperCamelCase (opts : LinterOptions) (modules : Array Lean.Name) : IO Nat := do unless getLinterValue linter.modulesUpperCamelCase opts do return 0 let exceptions := [ `Mathlib.Analysis.CStarAlgebra.lpSpace, `Mathlib.Analysis.InnerProductSpace.l2Space, `Mathlib.Analysis.Normed.Lp.lpSpace ] let badNames := modules.filter fun name ↦ let upperCamelName := Lake.toUpperCamelCase name !exceptions.contains name && upperCamelName != name && s!"{upperCamelName}_" != name.toString for bad in badNames do let upperCamelName := Lake.toUpperCamelCase bad let good := if bad.toString.endsWith "_" then s!"{upperCamelName}_" else upperCamelName.toString IO.eprintln s!"error: module name '{bad}' is not in 'UpperCamelCase': it should be '{good}' instead" return badNames.size
def
Tactic
[ "Batteries.Data.String.Matcher", "Mathlib.Data.Nat.Notation", "Lake.Util.Casing" ]
Mathlib/Tactic/Linter/TextBased.lean
modulesNotUpperCamelCase
Verify that all modules are named in `UpperCamelCase` -/ register_option linter.modulesUpperCamelCase : Bool := { defValue := true } /-- Verifies that all modules in `modules` are named in `UpperCamelCase` (except for explicitly discussed exceptions, which are hard-coded here). Return the number of modules violating this.
modulesOSForbidden (opts : LinterOptions) (modules : Array Lean.Name) : IO Nat := do unless getLinterValue linter.modulesUpperCamelCase opts do return 0 let forbiddenNames := [ "CON", "PRN", "AUX", "NUL", "COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8", "COM9", "COM¹", "COM²", "COM³", "LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9", "LPT¹", "LPT²", "LPT³" ] let forbiddenCharacters := [ '<', '>', '"', '/', '\\', '|', '?', '*', ] let mut badNamesNum := 0 for name in modules do let mut isBad := false let mut badComps : List String := [] let mut badChars : List String := [] for comp in name.componentsRev do let .str .anonymous s := comp | continue if forbiddenNames.contains s.toUpper then badComps := s :: badComps else if s.contains '!' then isBad := true IO.eprintln s!"error: module name '{name}' contains forbidden character '!'" else if s.contains '.' then isBad := true IO.eprintln s!"error: module name '{name}' contains forbidden character '.'" else if s.contains ' ' || s.contains '\t' || s.contains '\n' then isBad := true IO.eprintln s!"error: module name '{name}' contains a whitespace character" for c in forbiddenCharacters do if s.contains c then badChars := c.toString :: badChars if !badComps.isEmpty || !badChars.isEmpty then isBad := true if isBad then badNamesNum := badNamesNum + 1 if !badComps.isEmpty then IO.eprintln s!"error: module name '{name}' contains \ component{if badComps.length > 1 then "s" else ""} '{badComps}', \ which {if badComps.length > 1 then "are" else "is"} forbidden in Windows filenames." if !badChars.isEmpty then IO.eprintln s!"error: module name '{name}' contains \ character{if badChars.length > 1 then "s" else ""} '{badChars}', \ which {if badChars.length > 1 then "are" else "is"} forbidden in Windows filenames." return badNamesNum
def
Tactic
[ "Batteries.Data.String.Matcher", "Mathlib.Data.Nat.Notation", "Lake.Util.Casing" ]
Mathlib/Tactic/Linter/TextBased.lean
modulesOSForbidden
Verify that no module name is forbidden according to Windows' filename rules. -/ register_option linter.modulesForbiddenWindows : Bool := { defValue := true } /-- Verifies that no module in `modules` contains CON, PRN, AUX, NUL, COM1, COM2, COM3, COM4, COM5, COM6, COM7, COM8, COM9, COM¹, COM², COM³, LPT1, LPT2, LPT3, LPT4, LPT5, LPT6, LPT7, LPT8, LPT9, LPT¹, LPT² or LPT³ in its filename, as these are forbidden on Windows. Also verify that module names contain no forbidden characters such as `*`, `?` (Windows), `!` (forbidden on Nix OS) or `.` (might result from confusion with a module name). Source: https://learn.microsoft.com/en-gb/windows/win32/fileio/naming-a-file. Return the number of module names violating this rule.
M := StateRefT (Std.HashMap String.Range Syntax) IO
abbrev
Tactic
[ "Lean.Parser.Syntax", "Batteries.Tactic.Unreachable", "Mathlib.Tactic.Linter.Header", "Mathlib.Tactic.Linter.UnusedTacticExtension" ]
Mathlib/Tactic/Linter/UnusedTactic.lean
M
The unused tactic linter makes sure that every tactic call actually changes *something*. -/ register_option linter.unusedTactic : Bool := { defValue := true descr := "enable the unused tactic linter" } namespace UnusedTactic /-- The monad for collecting the ranges of the syntaxes that do not modify any goal.
isIgnoreTacticKind (ignoreTacticKinds : NameHashSet) (k : SyntaxNodeKind) : Bool := k.components.contains `Conv || "slice".isPrefixOf k.toString || k matches .str _ "quot" || ignoreTacticKinds.contains k
def
Tactic
[ "Lean.Parser.Syntax", "Batteries.Tactic.Unreachable", "Mathlib.Tactic.Linter.Header", "Mathlib.Tactic.Linter.UnusedTacticExtension" ]
Mathlib/Tactic/Linter/UnusedTactic.lean
isIgnoreTacticKind
A list of blacklisted syntax kinds, which are expected to have subterms that contain unevaluated tactics. -/ initialize ignoreTacticKindsRef : IO.Ref NameHashSet ← IO.mkRef <| .ofArray #[ `Mathlib.Tactic.Says.says, ``Parser.Term.binderTactic, ``Lean.Parser.Term.dynamicQuot, ``Lean.Parser.Tactic.quotSeq, ``Lean.Parser.Tactic.tacticStop_, ``Lean.Parser.Command.notation, ``Lean.Parser.Command.mixfix, ``Lean.Parser.Tactic.discharger, ``Lean.Parser.Tactic.Conv.conv, `Batteries.Tactic.seq_focus, `Mathlib.Tactic.Hint.registerHintStx, `Mathlib.Tactic.LinearCombination.linearCombination, `Mathlib.Tactic.LinearCombination'.linearCombination', `Aesop.Frontend.Parser.addRules, `Aesop.Frontend.Parser.aesopTactic, `Aesop.Frontend.Parser.aesopTactic?, -- the following `SyntaxNodeKind`s play a role in silencing `test`s ``Lean.Parser.Tactic.failIfSuccess, `Mathlib.Tactic.successIfFailWithMsg, `Mathlib.Tactic.failIfNoProgress ] /-- Is this a syntax kind that contains intentionally unused tactic subterms?
addIgnoreTacticKind (kind : SyntaxNodeKind) : IO Unit := ignoreTacticKindsRef.modify (·.insert kind) variable (ignoreTacticKinds : NameHashSet) (isTacKind : SyntaxNodeKind → Bool) in
def
Tactic
[ "Lean.Parser.Syntax", "Batteries.Tactic.Unreachable", "Mathlib.Tactic.Linter.Header", "Mathlib.Tactic.Linter.UnusedTacticExtension" ]
Mathlib/Tactic/Linter/UnusedTactic.lean
addIgnoreTacticKind
Adds a new syntax kind whose children will be ignored by the `unusedTactic` linter. This should be called from an `initialize` block.
@[specialize] partial getTactics (stx : Syntax) : M Unit := do if let .node _ k args := stx then if !isIgnoreTacticKind ignoreTacticKinds k then args.forM getTactics if isTacKind k then if let some r := stx.getRange? true then modify fun m => m.insert r stx
def
Tactic
[ "Lean.Parser.Syntax", "Batteries.Tactic.Unreachable", "Mathlib.Tactic.Linter.Header", "Mathlib.Tactic.Linter.UnusedTacticExtension" ]
Mathlib/Tactic/Linter/UnusedTactic.lean
getTactics
Accumulates the set of tactic syntaxes that should be evaluated at least once.
getNames (mctx : MetavarContext) : List Name := let lcts := mctx.decls.toList.map (MetavarDecl.lctx ∘ Prod.snd) let locDecls := (lcts.map (PersistentArray.toList ∘ LocalContext.decls)).flatten.reduceOption locDecls.map LocalDecl.userName mutual
def
Tactic
[ "Lean.Parser.Syntax", "Batteries.Tactic.Unreachable", "Mathlib.Tactic.Linter.Header", "Mathlib.Tactic.Linter.UnusedTacticExtension" ]
Mathlib/Tactic/Linter/UnusedTactic.lean
getNames
`getNames mctx` extracts the names of all the local declarations implied by the `MetavarContext` `mctx`.
partial eraseUsedTacticsList (exceptions : Std.HashSet SyntaxNodeKind) (trees : PersistentArray InfoTree) : M Unit := trees.forM (eraseUsedTactics exceptions)
def
Tactic
[ "Lean.Parser.Syntax", "Batteries.Tactic.Unreachable", "Mathlib.Tactic.Linter.Header", "Mathlib.Tactic.Linter.UnusedTacticExtension" ]
Mathlib/Tactic/Linter/UnusedTactic.lean
eraseUsedTacticsList
Search for tactic executions in the info tree and remove the syntax of the tactics that changed something.
partial eraseUsedTactics (exceptions : Std.HashSet SyntaxNodeKind) : InfoTree → M Unit | .node i c => do if let .ofTacticInfo i := i then let stx := i.stx let kind := stx.getKind if let some r := stx.getRange? true then if exceptions.contains kind then modify (·.erase r) else if i.goalsAfter != i.goalsBefore then modify (·.erase r) else if (kind == `Mathlib.Tactic.«tacticSwap_var__,,») && (getNames i.mctxBefore != getNames i.mctxAfter) then modify (·.erase r) eraseUsedTacticsList exceptions c | .context _ t => eraseUsedTactics exceptions t | .hole _ => pure ()
def
Tactic
[ "Lean.Parser.Syntax", "Batteries.Tactic.Unreachable", "Mathlib.Tactic.Linter.Header", "Mathlib.Tactic.Linter.UnusedTacticExtension" ]
Mathlib/Tactic/Linter/UnusedTactic.lean
eraseUsedTactics
Search for tactic executions in the info tree and remove the syntax of the tactics that changed something.
unusedTacticLinter : Linter where run := withSetOptionIn fun stx => do unless getLinterValue linter.unusedTactic (← getLinterOptions) && (← getInfoState).enabled do return if (← get).messages.hasErrors then return if stx.isOfKind ``Mathlib.Linter.UnusedTactic.«command#show_kind_» then return let env ← getEnv let cats := (Parser.parserExtension.getState env).categories let some tactics := Parser.ParserCategory.kinds <$> cats.find? `tactic | return let some convs := Parser.ParserCategory.kinds <$> cats.find? `conv | return let trees ← getInfoTrees let exceptions := (← allowedRef.get).union <| allowedUnusedTacticExt.getState env let go : M Unit := do getTactics (← ignoreTacticKindsRef.get) (fun k => tactics.contains k || convs.contains k) stx eraseUsedTacticsList exceptions trees let (_, map) ← go.run {} let unused := map.toArray let key (r : String.Range) := (r.start.byteIdx, (-r.stop.byteIdx : Int)) let mut last : String.Range := ⟨0, 0⟩ for (r, stx) in let _ := @lexOrd; let _ := @ltOfOrd.{0}; unused.qsort (key ·.1 < key ·.1) do if stx.getKind ∈ [``Batteries.Tactic.unreachable, ``Batteries.Tactic.unreachableConv] then continue if last.start ≤ r.start && r.stop ≤ last.stop then continue Linter.logLint linter.unusedTactic stx m!"'{stx}' tactic does nothing" last := r initialize addLinter unusedTacticLinter
def
Tactic
[ "Lean.Parser.Syntax", "Batteries.Tactic.Unreachable", "Mathlib.Tactic.Linter.Header", "Mathlib.Tactic.Linter.UnusedTacticExtension" ]
Mathlib/Tactic/Linter/UnusedTactic.lean
unusedTacticLinter
The main entry point to the unused tactic linter.
addAllowedUnusedTactic {m : Type → Type} [Monad m] [MonadEnv m] (stxNodes : Std.HashSet SyntaxNodeKind) : m Unit := stxNodes.foldM (init := ()) fun _ d => modifyEnv (allowedUnusedTacticExt.addEntry · d)
def
Tactic
[ "Mathlib.Tactic.Linter.Header" ]
Mathlib/Tactic/Linter/UnusedTacticExtension.lean
addAllowedUnusedTactic
Defines the `allowedUnusedTacticExt` extension for adding a `HashSet` of `allowedUnusedTactic`s to the environment. -/ initialize allowedUnusedTacticExt : SimplePersistentEnvExtension SyntaxNodeKind (Std.HashSet SyntaxNodeKind) ← registerSimplePersistentEnvExtension { addImportedFn := fun as => as.foldl Std.HashSet.insertMany {} addEntryFn := .insert } /-- `addAllowedUnusedTactic stxNodes` takes as input a `HashSet` of `SyntaxNodeKind`s and extends the `allowedUnusedTacticExt` environment extension with its content. These are tactics that the unused tactic linter will ignore, since they are expected to not change the tactic state. See the `#allow_unused_tactic! ids` command for dynamically extending the extension as a user-facing command.
Lean.Name.isLocal (env : Environment) (decl : Name) : Bool := (env.getModuleIdxFor? decl).isNone open Mathlib.Command.MinImports
def
Tactic
[ "ImportGraph.Imports", "Mathlib.Init" ]
Mathlib/Tactic/Linter/UpstreamableDecl.lean
Lean.Name.isLocal
Does this declaration come from the current file?
Lean.Environment.localDefinitionDependencies (env : Environment) (stx id : Syntax) : CommandElabM Bool := do let declName ← getDeclName stx let immediateDeps ← getAllDependencies stx id let immediateDeps : NameSet := immediateDeps.foldl (init := ∅) fun s n => if (env.find? n).isSome then s.insert n else s let deps ← liftCoreM <| immediateDeps.transitivelyUsedConstants let constInfos := deps.toList.filterMap env.find? let defs := constInfos.filter (fun constInfo => !(constInfo matches .thmInfo _ | .ctorInfo _)) return defs.any fun constInfo => declName != constInfo.name && constInfo.name.isLocal env
def
Tactic
[ "ImportGraph.Imports", "Mathlib.Init" ]
Mathlib/Tactic/Linter/UpstreamableDecl.lean
Lean.Environment.localDefinitionDependencies
Does the declaration with this name depend on definitions in the current file? Here, "definition" means everything that is not a theorem, and so includes `def`, `structure`, `inductive`, etc.
on purpose. -/ register_option linter.upstreamableDecl.defs : Bool := { defValue := false descr := "upstreamableDecl warns on definitions" }
def
Tactic
[ "ImportGraph.Imports", "Mathlib.Init" ]
Mathlib/Tactic/Linter/UpstreamableDecl.lean
on
null
@[inherit_doc Mathlib.Linter.linter.upstreamableDecl] upstreamableDeclLinter : Linter where run := withSetOptionIn fun stx ↦ do unless getLinterValue linter.upstreamableDecl (← getLinterOptions) do return if (← get).messages.hasErrors then return let skipDef := !getLinterValue linter.upstreamableDecl.defs (← getLinterOptions) let skipPrivate := !getLinterValue linter.upstreamableDecl.private (← getLinterOptions) if stx == (← `(command| set_option $(mkIdent `linter.upstreamableDecl) true)) then return let env ← getEnv let id ← getId stx if id != .missing then let name ← getDeclName stx if (skipDef && if let some constInfo := env.find? name then !(constInfo matches .thmInfo _ | .ctorInfo _) else true) || (skipPrivate && isPrivateName name) then return let minImports := getIrredundantImports env (← getAllImports stx id) match minImports.size, minImports.min? with | 1, some upstream => do if !(← env.localDefinitionDependencies stx id) then let p : GoToModuleLinkProps := { modName := upstream } let widget : MessageData := .ofWidget (← liftCoreM <| Widget.WidgetInstance.ofHash GoToModuleLink.javascriptHash <| Server.RpcEncodable.rpcEncode p) (toString upstream) Linter.logLint linter.upstreamableDecl id m!"Consider moving this declaration to the module {widget}." | _, _ => pure () initialize addLinter upstreamableDeclLinter
def
Tactic
[ "ImportGraph.Imports", "Mathlib.Init" ]
Mathlib/Tactic/Linter/UpstreamableDecl.lean
upstreamableDeclLinter
null
subsingleton_or_nontrivial_elim {p : Prop} {α : Type u} (h₁ : Subsingleton α → p) (h₂ : Nontrivial α → p) : p := (subsingleton_or_nontrivial α).elim @h₁ @h₂
theorem
Tactic
[ "Qq.MetaM", "Mathlib.Logic.Nontrivial.Basic", "Mathlib.Tactic.Attr.Core" ]
Mathlib/Tactic/Nontriviality/Core.lean
subsingleton_or_nontrivial_elim
null
nontrivialityByElim {u : Level} (α : Q(Type u)) (g : MVarId) (simpArgs : Array Syntax) : MetaM MVarId := do let p : Q(Prop) ← g.getType guard (← instantiateMVars (← inferType p)).isProp g.withContext do let g₁ ← mkFreshExprMVarQ q(Subsingleton $α → $p) let (_, g₁') ← g₁.mvarId!.intro1 g₁'.withContext try (do g₁'.assign (← synthInstance (← g₁'.getType))) <|> do let simpArgs := simpArgs.push (Unhygienic.run `(Parser.Tactic.simpLemma| nontriviality)) let stx := open TSyntax.Compat in Unhygienic.run `(tactic| simp [$simpArgs,*]) let ([], _) ← runTactic g₁' stx | failure catch _ => throwError "Could not prove goal assuming `{q(Subsingleton $α)}`\n{MessageData.ofGoal g₁'}" let g₂ : Q(Nontrivial $α → $p) ← mkFreshExprMVarQ q(Nontrivial $α → $p) g.assign q(subsingleton_or_nontrivial_elim $g₁ $g₂) pure g₂.mvarId! open Lean.Elab.Tactic.SolveByElim in
def
Tactic
[ "Qq.MetaM", "Mathlib.Logic.Nontrivial.Basic", "Mathlib.Tactic.Attr.Core" ]
Mathlib/Tactic/Nontriviality/Core.lean
nontrivialityByElim
Tries to generate a `Nontrivial α` instance by performing case analysis on `subsingleton_or_nontrivial α`, attempting to discharge the subsingleton branch using lemmas with `@[nontriviality]` attribute, including `Subsingleton.le` and `eq_iff_true_of_subsingleton`.
nontrivialityByAssumption (g : MVarId) : MetaM Unit := do g.inferInstance <|> do _ ← processSyntax {maxDepth := 6} false false [← `(nontrivial_of_ne), ← `(nontrivial_of_lt)] [] #[] [g]
def
Tactic
[ "Qq.MetaM", "Mathlib.Logic.Nontrivial.Basic", "Mathlib.Tactic.Attr.Core" ]
Mathlib/Tactic/Nontriviality/Core.lean
nontrivialityByAssumption
Tries to generate a `Nontrivial α` instance using `nontrivial_of_ne` or `nontrivial_of_lt` and local hypotheses.
@[tactic nontriviality] elabNontriviality : Tactic := fun stx => do let g ← getMainGoal let α ← match stx[1].getOptional? with | some e => Term.elabType e | none => (do let mut tgt ← withReducible g.getType' if let some tgt' := tgt.not? then tgt ← withReducible (whnf tgt') if let some (α, _) := tgt.eq? then return α if let some (α, _) := tgt.app4? ``LE.le then return α if let some (α, _) := tgt.app4? ``LT.lt then return α throwError "The goal is not an (in)equality, so you'll need to specify the desired \ `Nontrivial α` instance by invoking `nontriviality α`.") let .sort u ← whnf (← inferType α) | unreachable! let some v := u.dec | throwError "not a type{indentExpr α}" let α : Q(Type v) := α let tac := do let ty := q(Nontrivial $α) let m ← mkFreshExprMVar (some ty) nontrivialityByAssumption m.mvarId! g.assert `inst ty m let g ← liftM <| tac <|> nontrivialityByElim α g stx[2][1].getSepArgs replaceMainGoal [(← g.intro1).2]
def
Tactic
[ "Qq.MetaM", "Mathlib.Logic.Nontrivial.Basic", "Mathlib.Tactic.Attr.Core" ]
Mathlib/Tactic/Nontriviality/Core.lean
elabNontriviality
Attempts to generate a `Nontrivial α` hypothesis. The tactic first checks to see that there is not already a `Nontrivial α` instance before trying to synthesize one using other techniques. If the goal is an (in)equality, the type `α` is inferred from the goal. Otherwise, the type needs to be specified in the tactic invocation, as `nontriviality α`. The `nontriviality` tactic will first look for strict inequalities amongst the hypotheses, and use these to derive the `Nontrivial` instance directly. Otherwise, it will perform a case split on `Subsingleton α ∨ Nontrivial α`, and attempt to discharge the `Subsingleton` goal using `simp [h₁, h₂, ..., hₙ, nontriviality]`, where `[h₁, h₂, ..., hₙ]` is a list of additional `simp` lemmas that can be passed to `nontriviality` using the syntax `nontriviality α using h₁, h₂, ..., hₙ`. ``` example {R : Type} [OrderedRing R] {a : R} (h : 0 < a) : 0 < a := by nontriviality -- There is now a `Nontrivial R` hypothesis available. assumption ``` ``` example {R : Type} [CommRing R] {r s : R} : r * s = s * r := by nontriviality -- There is now a `Nontrivial R` hypothesis available. apply mul_comm ``` ``` example {R : Type} [OrderedRing R] {a : R} (h : 0 < a) : (2 : ℕ) ∣ 4 := by nontriviality R -- there is now a `Nontrivial R` hypothesis available. dec_trivial ``` ``` def myeq {α : Type} (a b : α) : Prop := a = b example {α : Type} (a b : α) (h : a = b) : myeq a b := by success_if_fail nontriviality α -- Fails nontriviality α using myeq -- There is now a `Nontrivial α` hypothesis available assumption ``` -/ syntax (name := nontriviality) "nontriviality" (ppSpace colGt term)? (" using " Parser.Tactic.simpArg,+)? : tactic /-- Elaborator for the `nontriviality` tactic.
isNat_abs_nonneg {α : Type*} [Ring α] [Lattice α] [IsOrderedRing α] {a : α} {na : ℕ} (pa : IsNat a na) : IsNat |a| na := by rw [pa.out, Nat.abs_cast] constructor rfl
theorem
Tactic
[ "Mathlib.Tactic.NormNum.Basic", "Mathlib.Data.Nat.Cast.Order.Ring" ]
Mathlib/Tactic/NormNum/Abs.lean
isNat_abs_nonneg
null
isNat_abs_neg {α : Type*} [Ring α] [Lattice α] [IsOrderedRing α] {a : α} {na : ℕ} (pa : IsInt a (.negOfNat na)) : IsNat |a| na := by rw [pa.out] constructor simp
theorem
Tactic
[ "Mathlib.Tactic.NormNum.Basic", "Mathlib.Data.Nat.Cast.Order.Ring" ]
Mathlib/Tactic/NormNum/Abs.lean
isNat_abs_neg
null
isNNRat_abs_nonneg {α : Type*} [DivisionRing α] [LinearOrder α] [IsStrictOrderedRing α] {a : α} {num den : ℕ} (ra : IsNNRat a num den) : IsNNRat |a| num den := by obtain ⟨ha1, rfl⟩ := ra refine ⟨ha1, abs_of_nonneg ?_⟩ apply mul_nonneg · exact Nat.cast_nonneg' num · simp only [invOf_eq_inv, inv_nonneg, Nat.cast_nonneg]
theorem
Tactic
[ "Mathlib.Tactic.NormNum.Basic", "Mathlib.Data.Nat.Cast.Order.Ring" ]
Mathlib/Tactic/NormNum/Abs.lean
isNNRat_abs_nonneg
null
isNNRat_abs_neg {α : Type*} [DivisionRing α] [LinearOrder α] [IsStrictOrderedRing α] {a : α} {num den : ℕ} (ra : IsRat a (.negOfNat num) den) : IsNNRat |a| num den := by obtain ⟨ha1, rfl⟩ := ra simp only [Int.cast_negOfNat, neg_mul, abs_neg] refine ⟨ha1, abs_of_nonneg ?_⟩ apply mul_nonneg · exact Nat.cast_nonneg' num · simp only [invOf_eq_inv, inv_nonneg, Nat.cast_nonneg]
theorem
Tactic
[ "Mathlib.Tactic.NormNum.Basic", "Mathlib.Data.Nat.Cast.Order.Ring" ]
Mathlib/Tactic/NormNum/Abs.lean
isNNRat_abs_neg
null
@[norm_num |_|] evalAbs : NormNumExt where eval {u α} | ~q(@abs _ $instLattice $instAddGroup $a) => do match ← derive a with | .isBool .. => failure | .isNat sα na pa => let rα : Q(Ring $α) ← synthInstanceQ q(Ring $α) let iorα : Q(IsOrderedRing $α) ← synthInstanceQ q(IsOrderedRing $α) assumeInstancesCommute return .isNat sα na q(isNat_abs_nonneg $pa) | .isNegNat sα na pa => let rα : Q(Ring $α) ← synthInstanceQ q(Ring $α) let iorα : Q(IsOrderedRing $α) ← synthInstanceQ q(IsOrderedRing $α) assumeInstancesCommute return .isNat _ _ q(isNat_abs_neg $pa) | .isNNRat dsα' qe' nume' dene' pe' => let rα : Q(DivisionRing $α) ← synthInstanceQ q(DivisionRing $α) let loα : Q(LinearOrder $α) ← synthInstanceQ q(LinearOrder $α) let isorα : Q(IsStrictOrderedRing $α) ← synthInstanceQ q(IsStrictOrderedRing $α) assumeInstancesCommute return .isNNRat _ qe' _ _ q(isNNRat_abs_nonneg $pe') | .isNegNNRat dα' qe' nume' dene' pe' => let loα : Q(LinearOrder $α) ← synthInstanceQ q(LinearOrder $α) let isorα : Q(IsStrictOrderedRing $α) ← synthInstanceQ q(IsStrictOrderedRing $α) assumeInstancesCommute return .isNNRat _ (-qe') _ _ q(isNNRat_abs_neg $pe')
def
Tactic
[ "Mathlib.Tactic.NormNum.Basic", "Mathlib.Data.Nat.Cast.Order.Ring" ]
Mathlib/Tactic/NormNum/Abs.lean
evalAbs
The `norm_num` extension which identifies expressions of the form `|a|`, such that `norm_num` successfully recognises `a`.
IsInt.raw_refl (n : ℤ) : IsInt n n := ⟨rfl⟩ /-! # Constructors and constants -/
theorem
Tactic
[ "Mathlib.Algebra.GroupWithZero.Invertible", "Mathlib.Algebra.Ring.Int.Defs", "Mathlib.Data.Nat.Cast.Basic", "Mathlib.Data.Nat.Cast.Commute", "Mathlib.Tactic.NormNum.Core", "Mathlib.Tactic.HaveI", "Mathlib.Tactic.ClearExclamation" ]
Mathlib/Tactic/NormNum/Basic.lean
IsInt.raw_refl
null
isNat_zero (α) [AddMonoidWithOne α] : IsNat (Zero.zero : α) (nat_lit 0) := ⟨Nat.cast_zero.symm⟩
theorem
Tactic
[ "Mathlib.Algebra.GroupWithZero.Invertible", "Mathlib.Algebra.Ring.Int.Defs", "Mathlib.Data.Nat.Cast.Basic", "Mathlib.Data.Nat.Cast.Commute", "Mathlib.Tactic.NormNum.Core", "Mathlib.Tactic.HaveI", "Mathlib.Tactic.ClearExclamation" ]
Mathlib/Tactic/NormNum/Basic.lean
isNat_zero
null
@[norm_num Zero.zero] evalZero : NormNumExt where eval {u α} e := do let sα ← inferAddMonoidWithOne α match e with | ~q(Zero.zero) => return .isNat sα q(nat_lit 0) q(isNat_zero $α)
def
Tactic
[ "Mathlib.Algebra.GroupWithZero.Invertible", "Mathlib.Algebra.Ring.Int.Defs", "Mathlib.Data.Nat.Cast.Basic", "Mathlib.Data.Nat.Cast.Commute", "Mathlib.Tactic.NormNum.Core", "Mathlib.Tactic.HaveI", "Mathlib.Tactic.ClearExclamation" ]
Mathlib/Tactic/NormNum/Basic.lean
evalZero
The `norm_num` extension which identifies the expression `Zero.zero`, returning `0`.
isNat_one (α) [AddMonoidWithOne α] : IsNat (One.one : α) (nat_lit 1) := ⟨Nat.cast_one.symm⟩
theorem
Tactic
[ "Mathlib.Algebra.GroupWithZero.Invertible", "Mathlib.Algebra.Ring.Int.Defs", "Mathlib.Data.Nat.Cast.Basic", "Mathlib.Data.Nat.Cast.Commute", "Mathlib.Tactic.NormNum.Core", "Mathlib.Tactic.HaveI", "Mathlib.Tactic.ClearExclamation" ]
Mathlib/Tactic/NormNum/Basic.lean
isNat_one
null
@[norm_num One.one] evalOne : NormNumExt where eval {u α} e := do let sα ← inferAddMonoidWithOne α match e with | ~q(One.one) => return .isNat sα q(nat_lit 1) q(isNat_one $α)
def
Tactic
[ "Mathlib.Algebra.GroupWithZero.Invertible", "Mathlib.Algebra.Ring.Int.Defs", "Mathlib.Data.Nat.Cast.Basic", "Mathlib.Data.Nat.Cast.Commute", "Mathlib.Tactic.NormNum.Core", "Mathlib.Tactic.HaveI", "Mathlib.Tactic.ClearExclamation" ]
Mathlib/Tactic/NormNum/Basic.lean
evalOne
The `norm_num` extension which identifies the expression `One.one`, returning `1`.
isNat_ofNat (α : Type u) [AddMonoidWithOne α] {a : α} {n : ℕ} (h : n = a) : IsNat a n := ⟨h.symm⟩
theorem
Tactic
[ "Mathlib.Algebra.GroupWithZero.Invertible", "Mathlib.Algebra.Ring.Int.Defs", "Mathlib.Data.Nat.Cast.Basic", "Mathlib.Data.Nat.Cast.Commute", "Mathlib.Tactic.NormNum.Core", "Mathlib.Tactic.HaveI", "Mathlib.Tactic.ClearExclamation" ]
Mathlib/Tactic/NormNum/Basic.lean
isNat_ofNat
null
@[norm_num OfNat.ofNat _] evalOfNat : NormNumExt where eval {u α} e := do let sα ← inferAddMonoidWithOne α match e with | ~q(@OfNat.ofNat _ $n $oα) => let n : Q(ℕ) ← whnf n guard n.isRawNatLit let ⟨a, (pa : Q($n = $e))⟩ ← mkOfNat α sα n guard <|← isDefEq a e return .isNat sα n q(isNat_ofNat $α $pa)
def
Tactic
[ "Mathlib.Algebra.GroupWithZero.Invertible", "Mathlib.Algebra.Ring.Int.Defs", "Mathlib.Data.Nat.Cast.Basic", "Mathlib.Data.Nat.Cast.Commute", "Mathlib.Tactic.NormNum.Core", "Mathlib.Tactic.HaveI", "Mathlib.Tactic.ClearExclamation" ]
Mathlib/Tactic/NormNum/Basic.lean
evalOfNat
The `norm_num` extension which identifies an expression `OfNat.ofNat n`, returning `n`.
isNat_intOfNat : {n n' : ℕ} → IsNat n n' → IsNat (Int.ofNat n) n' | _, _, ⟨rfl⟩ => ⟨rfl⟩
theorem
Tactic
[ "Mathlib.Algebra.GroupWithZero.Invertible", "Mathlib.Algebra.Ring.Int.Defs", "Mathlib.Data.Nat.Cast.Basic", "Mathlib.Data.Nat.Cast.Commute", "Mathlib.Tactic.NormNum.Core", "Mathlib.Tactic.HaveI", "Mathlib.Tactic.ClearExclamation" ]
Mathlib/Tactic/NormNum/Basic.lean
isNat_intOfNat
null
@[norm_num Int.ofNat _] evalIntOfNat : NormNumExt where eval {u α} e := do let .app (.const ``Int.ofNat _) (n : Q(ℕ)) ← whnfR e | failure haveI' : u =QL 0 := ⟨⟩; haveI' : $α =Q Int := ⟨⟩ let sℕ : Q(AddMonoidWithOne ℕ) := q(instAddMonoidWithOneNat) let sℤ : Q(AddMonoidWithOne ℤ) := q(instAddMonoidWithOne) let ⟨n', p⟩ ← deriveNat n sℕ haveI' x : $e =Q Int.ofNat $n := ⟨⟩ return .isNat sℤ n' q(isNat_intOfNat $p)
def
Tactic
[ "Mathlib.Algebra.GroupWithZero.Invertible", "Mathlib.Algebra.Ring.Int.Defs", "Mathlib.Data.Nat.Cast.Basic", "Mathlib.Data.Nat.Cast.Commute", "Mathlib.Tactic.NormNum.Core", "Mathlib.Tactic.HaveI", "Mathlib.Tactic.ClearExclamation" ]
Mathlib/Tactic/NormNum/Basic.lean
evalIntOfNat
The `norm_num` extension which identifies the constructor application `Int.ofNat n` such that `norm_num` successfully recognizes `n`, returning `n`.
isNat_natAbs_pos : {n : ℤ} → {a : ℕ} → IsNat n a → IsNat n.natAbs a | _, _, ⟨rfl⟩ => ⟨rfl⟩
theorem
Tactic
[ "Mathlib.Algebra.GroupWithZero.Invertible", "Mathlib.Algebra.Ring.Int.Defs", "Mathlib.Data.Nat.Cast.Basic", "Mathlib.Data.Nat.Cast.Commute", "Mathlib.Tactic.NormNum.Core", "Mathlib.Tactic.HaveI", "Mathlib.Tactic.ClearExclamation" ]
Mathlib/Tactic/NormNum/Basic.lean
isNat_natAbs_pos
null
isNat_natAbs_neg : {n : ℤ} → {a : ℕ} → IsInt n (.negOfNat a) → IsNat n.natAbs a | _, _, ⟨rfl⟩ => ⟨by simp⟩
theorem
Tactic
[ "Mathlib.Algebra.GroupWithZero.Invertible", "Mathlib.Algebra.Ring.Int.Defs", "Mathlib.Data.Nat.Cast.Basic", "Mathlib.Data.Nat.Cast.Commute", "Mathlib.Tactic.NormNum.Core", "Mathlib.Tactic.HaveI", "Mathlib.Tactic.ClearExclamation" ]
Mathlib/Tactic/NormNum/Basic.lean
isNat_natAbs_neg
null
@[norm_num Int.natAbs (_ : ℤ)] evalIntNatAbs : NormNumExt where eval {u α} e := do let .app (.const ``Int.natAbs _) (x : Q(ℤ)) ← whnfR e | failure haveI' : u =QL 0 := ⟨⟩; haveI' : $α =Q ℕ := ⟨⟩ haveI' : $e =Q Int.natAbs $x := ⟨⟩ let sℕ : Q(AddMonoidWithOne ℕ) := q(instAddMonoidWithOneNat) match ← derive (u := .zero) x with | .isNat _ a p => assumeInstancesCommute; return .isNat sℕ a q(isNat_natAbs_pos $p) | .isNegNat _ a p => assumeInstancesCommute; return .isNat sℕ a q(isNat_natAbs_neg $p) | _ => failure /-! # Casts -/
def
Tactic
[ "Mathlib.Algebra.GroupWithZero.Invertible", "Mathlib.Algebra.Ring.Int.Defs", "Mathlib.Data.Nat.Cast.Basic", "Mathlib.Data.Nat.Cast.Commute", "Mathlib.Tactic.NormNum.Core", "Mathlib.Tactic.HaveI", "Mathlib.Tactic.ClearExclamation" ]
Mathlib/Tactic/NormNum/Basic.lean
evalIntNatAbs
The `norm_num` extension which identifies the expression `Int.natAbs n` such that `norm_num` successfully recognizes `n`.
isNat_natCast {R} [AddMonoidWithOne R] (n m : ℕ) : IsNat n m → IsNat (n : R) m := by rintro ⟨⟨⟩⟩; exact ⟨rfl⟩
theorem
Tactic
[ "Mathlib.Algebra.GroupWithZero.Invertible", "Mathlib.Algebra.Ring.Int.Defs", "Mathlib.Data.Nat.Cast.Basic", "Mathlib.Data.Nat.Cast.Commute", "Mathlib.Tactic.NormNum.Core", "Mathlib.Tactic.HaveI", "Mathlib.Tactic.ClearExclamation" ]
Mathlib/Tactic/NormNum/Basic.lean
isNat_natCast
null
@[norm_num Nat.cast _, NatCast.natCast _] evalNatCast : NormNumExt where eval {u α} e := do let sα ← inferAddMonoidWithOne α let .app n (a : Q(ℕ)) ← whnfR e | failure guard <|← withNewMCtxDepth <| isDefEq n q(Nat.cast (R := $α)) let ⟨na, pa⟩ ← deriveNat a q(instAddMonoidWithOneNat) haveI' : $e =Q $a := ⟨⟩ return .isNat sα na q(isNat_natCast $a $na $pa)
def
Tactic
[ "Mathlib.Algebra.GroupWithZero.Invertible", "Mathlib.Algebra.Ring.Int.Defs", "Mathlib.Data.Nat.Cast.Basic", "Mathlib.Data.Nat.Cast.Commute", "Mathlib.Tactic.NormNum.Core", "Mathlib.Tactic.HaveI", "Mathlib.Tactic.ClearExclamation" ]
Mathlib/Tactic/NormNum/Basic.lean
evalNatCast
The `norm_num` extension which identifies an expression `Nat.cast n`, returning `n`.
isNat_intCast {R} [Ring R] (n : ℤ) (m : ℕ) : IsNat n m → IsNat (n : R) m := by rintro ⟨⟨⟩⟩; exact ⟨by simp⟩
theorem
Tactic
[ "Mathlib.Algebra.GroupWithZero.Invertible", "Mathlib.Algebra.Ring.Int.Defs", "Mathlib.Data.Nat.Cast.Basic", "Mathlib.Data.Nat.Cast.Commute", "Mathlib.Tactic.NormNum.Core", "Mathlib.Tactic.HaveI", "Mathlib.Tactic.ClearExclamation" ]
Mathlib/Tactic/NormNum/Basic.lean
isNat_intCast
null
isintCast {R} [Ring R] (n m : ℤ) : IsInt n m → IsInt (n : R) m := by rintro ⟨⟨⟩⟩; exact ⟨rfl⟩
theorem
Tactic
[ "Mathlib.Algebra.GroupWithZero.Invertible", "Mathlib.Algebra.Ring.Int.Defs", "Mathlib.Data.Nat.Cast.Basic", "Mathlib.Data.Nat.Cast.Commute", "Mathlib.Tactic.NormNum.Core", "Mathlib.Tactic.HaveI", "Mathlib.Tactic.ClearExclamation" ]
Mathlib/Tactic/NormNum/Basic.lean
isintCast
null
@[norm_num Int.cast _, IntCast.intCast _] evalIntCast : NormNumExt where eval {u α} e := do let rα ← inferRing α let .app i (a : Q(ℤ)) ← whnfR e | failure guard <|← withNewMCtxDepth <| isDefEq i q(Int.cast (R := $α)) match ← derive (α := q(ℤ)) a with | .isNat _ na pa => assumeInstancesCommute haveI' : $e =Q Int.cast $a := ⟨⟩ return .isNat _ na q(isNat_intCast $a $na $pa) | .isNegNat _ na pa => assumeInstancesCommute haveI' : $e =Q Int.cast $a := ⟨⟩ return .isNegNat _ na q(isintCast $a (.negOfNat $na) $pa) | _ => failure /-! # Arithmetic -/ library_note "norm_num lemma function equality"/-- Note: Many of the lemmas in this file use a function equality hypothesis like `f = HAdd.hAdd` below. The reason for this is that when this is applied, to prove e.g. `100 + 200 = 300`, the `+` here is `HAdd.hAdd` with an instance that may not be syntactically equal to the one supplied by the `AddMonoidWithOne` instance, and rather than attempting to prove the instances equal lean will sometimes decide to evaluate `100 + 200` directly (into whatever `+` is defined to do in this ring), which is definitely not what we want; if the subterms are expensive to kernel-reduce then this could cause a `(kernel) deep recursion detected` error (see https://github.com/leanprover/lean4/issues/2171, https://github.com/leanprover-community/mathlib4/pull/4048). By using an equality for the unapplied `+` function and proving it by `rfl` we take away the opportunity for lean to unfold the numerals (and the instance defeq problem is usually comparatively easy). -/
def
Tactic
[ "Mathlib.Algebra.GroupWithZero.Invertible", "Mathlib.Algebra.Ring.Int.Defs", "Mathlib.Data.Nat.Cast.Basic", "Mathlib.Data.Nat.Cast.Commute", "Mathlib.Tactic.NormNum.Core", "Mathlib.Tactic.HaveI", "Mathlib.Tactic.ClearExclamation" ]
Mathlib/Tactic/NormNum/Basic.lean
evalIntCast
The `norm_num` extension which identifies an expression `Int.cast n`, returning `n`.
isNat_add {α} [AddMonoidWithOne α] : ∀ {f : α → α → α} {a b : α} {a' b' c : ℕ}, f = HAdd.hAdd → IsNat a a' → IsNat b b' → Nat.add a' b' = c → IsNat (f a b) c | _, _, _, _, _, _, rfl, ⟨rfl⟩, ⟨rfl⟩, rfl => ⟨(Nat.cast_add _ _).symm⟩
theorem
Tactic
[ "Mathlib.Algebra.GroupWithZero.Invertible", "Mathlib.Algebra.Ring.Int.Defs", "Mathlib.Data.Nat.Cast.Basic", "Mathlib.Data.Nat.Cast.Commute", "Mathlib.Tactic.NormNum.Core", "Mathlib.Tactic.HaveI", "Mathlib.Tactic.ClearExclamation" ]
Mathlib/Tactic/NormNum/Basic.lean
isNat_add
null
isInt_add {α} [Ring α] : ∀ {f : α → α → α} {a b : α} {a' b' c : ℤ}, f = HAdd.hAdd → IsInt a a' → IsInt b b' → Int.add a' b' = c → IsInt (f a b) c | _, _, _, _, _, _, rfl, ⟨rfl⟩, ⟨rfl⟩, rfl => ⟨(Int.cast_add ..).symm⟩
theorem
Tactic
[ "Mathlib.Algebra.GroupWithZero.Invertible", "Mathlib.Algebra.Ring.Int.Defs", "Mathlib.Data.Nat.Cast.Basic", "Mathlib.Data.Nat.Cast.Commute", "Mathlib.Tactic.NormNum.Core", "Mathlib.Tactic.HaveI", "Mathlib.Tactic.ClearExclamation" ]
Mathlib/Tactic/NormNum/Basic.lean
isInt_add
null
invertibleOfMul {α} [Semiring α] (k : ℕ) (b : α) : ∀ (a : α) [Invertible a], a = k * b → Invertible b | _, ⟨c, hc1, hc2⟩, rfl => by rw [← mul_assoc] at hc1 rw [Nat.cast_commute k, mul_assoc, Nat.cast_commute k] at hc2 exact ⟨_, hc1, hc2⟩
def
Tactic
[ "Mathlib.Algebra.GroupWithZero.Invertible", "Mathlib.Algebra.Ring.Int.Defs", "Mathlib.Data.Nat.Cast.Basic", "Mathlib.Data.Nat.Cast.Commute", "Mathlib.Tactic.NormNum.Core", "Mathlib.Tactic.HaveI", "Mathlib.Tactic.ClearExclamation" ]
Mathlib/Tactic/NormNum/Basic.lean
invertibleOfMul
If `b` divides `a` and `a` is invertible, then `b` is invertible.
invertibleOfMul' {α} [Semiring α] {a k b : ℕ} [Invertible (a : α)] (h : a = k * b) : Invertible (b : α) := invertibleOfMul k (b:α) ↑a (by simp [h])
def
Tactic
[ "Mathlib.Algebra.GroupWithZero.Invertible", "Mathlib.Algebra.Ring.Int.Defs", "Mathlib.Data.Nat.Cast.Basic", "Mathlib.Data.Nat.Cast.Commute", "Mathlib.Tactic.NormNum.Core", "Mathlib.Tactic.HaveI", "Mathlib.Tactic.ClearExclamation" ]
Mathlib/Tactic/NormNum/Basic.lean
invertibleOfMul'
If `b` divides `a` and `a` is invertible, then `b` is invertible.
isNNRat_add {α} [Semiring α] {f : α → α → α} {a b : α} {na nb nc : ℕ} {da db dc k : ℕ} : f = HAdd.hAdd → IsNNRat a na da → IsNNRat b nb db → Nat.add (Nat.mul na db) (Nat.mul nb da) = Nat.mul k nc → Nat.mul da db = Nat.mul k dc → IsNNRat (f a b) nc dc := by rintro rfl ⟨_, rfl⟩ ⟨_, rfl⟩ (h₁ : na * db + nb * da = k * nc) (h₂ : da * db = k * dc) have : Invertible (↑(da * db) : α) := by simpa using invertibleMul (da:α) db have := invertibleOfMul' (α := α) h₂ use this have H := (Nat.cast_commute (α := α) da db).invOf_left.invOf_right.right_comm have h₁ := congr_arg (↑· * (⅟↑da * ⅟↑db : α)) h₁ simp only [Nat.cast_add, Nat.cast_mul, ← mul_assoc, add_mul, mul_invOf_cancel_right] at h₁ have h₂ := congr_arg (↑nc * ↑· * (⅟↑da * ⅟↑db * ⅟↑dc : α)) h₂ simp only [H, mul_invOf_cancel_right', Nat.cast_mul, ← mul_assoc] at h₁ h₂ rw [h₁, h₂, Nat.cast_commute] simp only [mul_invOf_cancel_right, (Nat.cast_commute (α := α) da dc).invOf_left.invOf_right.right_comm, (Nat.cast_commute (α := α) db dc).invOf_left.invOf_right.right_comm]
theorem
Tactic
[ "Mathlib.Algebra.GroupWithZero.Invertible", "Mathlib.Algebra.Ring.Int.Defs", "Mathlib.Data.Nat.Cast.Basic", "Mathlib.Data.Nat.Cast.Commute", "Mathlib.Tactic.NormNum.Core", "Mathlib.Tactic.HaveI", "Mathlib.Tactic.ClearExclamation" ]
Mathlib/Tactic/NormNum/Basic.lean
isNNRat_add
null
isRat_add {α} [Ring α] {f : α → α → α} {a b : α} {na nb nc : ℤ} {da db dc k : ℕ} : f = HAdd.hAdd → IsRat a na da → IsRat b nb db → Int.add (Int.mul na db) (Int.mul nb da) = Int.mul k nc → Nat.mul da db = Nat.mul k dc → IsRat (f a b) nc dc := by rintro rfl ⟨_, rfl⟩ ⟨_, rfl⟩ (h₁ : na * db + nb * da = k * nc) (h₂ : da * db = k * dc) have : Invertible (↑(da * db) : α) := by simpa using invertibleMul (da:α) db have := invertibleOfMul' (α := α) h₂ use this have H := (Nat.cast_commute (α := α) da db).invOf_left.invOf_right.right_comm have h₁ := congr_arg (↑· * (⅟↑da * ⅟↑db : α)) h₁ simp only [Int.cast_add, Int.cast_mul, Int.cast_natCast, ← mul_assoc, add_mul, mul_invOf_cancel_right] at h₁ have h₂ := congr_arg (↑nc * ↑· * (⅟↑da * ⅟↑db * ⅟↑dc : α)) h₂ simp only [H, mul_invOf_cancel_right', Nat.cast_mul, ← mul_assoc] at h₁ h₂ rw [h₁, h₂, Nat.cast_commute] simp only [mul_invOf_cancel_right, (Nat.cast_commute (α := α) da dc).invOf_left.invOf_right.right_comm, (Nat.cast_commute (α := α) db dc).invOf_left.invOf_right.right_comm]
theorem
Tactic
[ "Mathlib.Algebra.GroupWithZero.Invertible", "Mathlib.Algebra.Ring.Int.Defs", "Mathlib.Data.Nat.Cast.Basic", "Mathlib.Data.Nat.Cast.Commute", "Mathlib.Tactic.NormNum.Core", "Mathlib.Tactic.HaveI", "Mathlib.Tactic.ClearExclamation" ]
Mathlib/Tactic/NormNum/Basic.lean
isRat_add
null
_root_.Mathlib.Meta.monadLiftOptionMetaM : MonadLift Option MetaM where monadLift | none => failure | some e => pure e attribute [local instance] monadLiftOptionMetaM in
def
Tactic
[ "Mathlib.Algebra.GroupWithZero.Invertible", "Mathlib.Algebra.Ring.Int.Defs", "Mathlib.Data.Nat.Cast.Basic", "Mathlib.Data.Nat.Cast.Commute", "Mathlib.Tactic.NormNum.Core", "Mathlib.Tactic.HaveI", "Mathlib.Tactic.ClearExclamation" ]
Mathlib/Tactic/NormNum/Basic.lean
_root_.Mathlib.Meta.monadLiftOptionMetaM
Consider an `Option` as an object in the `MetaM` monad, by throwing an error on `none`.
Result.add {u : Level} {α : Q(Type u)} {a b : Q($α)} (ra : Result q($a)) (rb : Result q($b)) (inst : Q(Add $α) := by exact q(delta% inferInstance)) : MetaM (Result q($a + $b)) := do let rec intArm (rα : Q(Ring $α)) := do assumeInstancesCommute let ⟨za, na, pa⟩ ← ra.toInt _; let ⟨zb, nb, pb⟩ ← rb.toInt _ let zc := za + zb have c := mkRawIntLit zc haveI' : Int.add $na $nb =Q $c := ⟨⟩ return .isInt rα c zc q(isInt_add (.refl _) $pa $pb (.refl $c)) let rec nnratArm (dsα : Q(DivisionSemiring $α)) : MetaM (Result _) := do assumeInstancesCommute let ⟨qa, na, da, pa⟩ ← ra.toNNRat' dsα; let ⟨qb, nb, db, pb⟩ ← rb.toNNRat' dsα let qc := qa + qb let dd := qa.den * qb.den let k := dd / qc.den have t1 : Q(ℕ) := mkRawNatLit (k * qc.num.toNat) have t2 : Q(ℕ) := mkRawNatLit dd have nc : Q(ℕ) := mkRawNatLit qc.num.toNat have dc : Q(ℕ) := mkRawNatLit qc.den have k : Q(ℕ) := mkRawNatLit k let r1 : Q(Nat.add (Nat.mul $na $db) (Nat.mul $nb $da) = Nat.mul $k $nc) := (q(Eq.refl $t1) : Expr) let r2 : Q(Nat.mul $da $db = Nat.mul $k $dc) := (q(Eq.refl $t2) : Expr) return .isNNRat' dsα qc nc dc q(isNNRat_add (.refl _) $pa $pb $r1 $r2) let rec ratArm (dα : Q(DivisionRing $α)) : MetaM (Result _) := do assumeInstancesCommute let ⟨qa, na, da, pa⟩ ← ra.toRat' dα; let ⟨qb, nb, db, pb⟩ ← rb.toRat' dα let qc := qa + qb let dd := qa.den * qb.den let k := dd / qc.den have t1 : Q(ℤ) := mkRawIntLit (k * qc.num) have t2 : Q(ℕ) := mkRawNatLit dd have nc : Q(ℤ) := mkRawIntLit qc.num have dc : Q(ℕ) := mkRawNatLit qc.den have k : Q(ℕ) := mkRawNatLit k let r1 : Q(Int.add (Int.mul $na $db) (Int.mul $nb $da) = Int.mul $k $nc) := (q(Eq.refl $t1) : Expr) let r2 : Q(Nat.mul $da $db = Nat.mul $k $dc) := (q(Eq.refl $t2) : Expr) return .isRat dα qc nc dc q(isRat_add (.refl _) $pa $pb $r1 $r2) match ra, rb with | .isBool .., _ | _, .isBool .. => failure | .isNegNNRat dα .., _ | _, .isNegNNRat dα .. => ratArm dα | .isNNRat _dsα .., .isNegNat _rα .. | .isNegNat _rα .., .isNNRat _dsα .. => let dα ← synthInstanceQ q(DivisionRing $α) assumeInstancesCommute ratArm q($dα) | .isNNRat dsα .., _ | _, .isNNRat dsα .. => nnratArm dsα | .isNegNat rα .., _ | _, .isNegNat rα .. => intArm rα | .isNat _ na pa, .isNat sα nb pb => assumeInstancesCommute ...
def
Tactic
[ "Mathlib.Algebra.GroupWithZero.Invertible", "Mathlib.Algebra.Ring.Int.Defs", "Mathlib.Data.Nat.Cast.Basic", "Mathlib.Data.Nat.Cast.Commute", "Mathlib.Tactic.NormNum.Core", "Mathlib.Tactic.HaveI", "Mathlib.Tactic.ClearExclamation" ]
Mathlib/Tactic/NormNum/Basic.lean
Result.add
The result of adding two norm_num results.
@[norm_num _ + _] evalAdd : NormNumExt where eval {u α} e := do let .app (.app (f : Q($α → $α → $α)) (a : Q($α))) (b : Q($α)) ← whnfR e | failure let ra ← derive a; let rb ← derive b match ra, rb with | .isBool .., _ | _, .isBool .. => failure | .isNat _ .., .isNat _ .. | .isNat _ .., .isNegNat _ .. | .isNat _ .., .isNNRat _ .. | .isNat _ .., .isNegNNRat _ .. | .isNegNat _ .., .isNat _ .. | .isNegNat _ .., .isNegNat _ .. | .isNegNat _ .., .isNNRat _ .. | .isNegNat _ .., .isNegNNRat _ .. | .isNNRat _ .., .isNat _ .. | .isNNRat _ .., .isNegNat _ .. | .isNNRat _ .., .isNNRat _ .. | .isNNRat _ .., .isNegNNRat _ .. | .isNegNNRat _ .., .isNat _ .. | .isNegNNRat _ .., .isNegNat _ .. | .isNegNNRat _ .., .isNNRat _ .. | .isNegNNRat _ .., .isNegNNRat _ .. => guard <|← withNewMCtxDepth <| isDefEq f q(HAdd.hAdd (α := $α)) ra.add rb
def
Tactic
[ "Mathlib.Algebra.GroupWithZero.Invertible", "Mathlib.Algebra.Ring.Int.Defs", "Mathlib.Data.Nat.Cast.Basic", "Mathlib.Data.Nat.Cast.Commute", "Mathlib.Tactic.NormNum.Core", "Mathlib.Tactic.HaveI", "Mathlib.Tactic.ClearExclamation" ]
Mathlib/Tactic/NormNum/Basic.lean
evalAdd
The `norm_num` extension which identifies expressions of the form `a + b`, such that `norm_num` successfully recognises both `a` and `b`.
isInt_neg {α} [Ring α] : ∀ {f : α → α} {a : α} {a' b : ℤ}, f = Neg.neg → IsInt a a' → Int.neg a' = b → IsInt (-a) b | _, _, _, _, rfl, ⟨rfl⟩, rfl => ⟨(Int.cast_neg ..).symm⟩
theorem
Tactic
[ "Mathlib.Algebra.GroupWithZero.Invertible", "Mathlib.Algebra.Ring.Int.Defs", "Mathlib.Data.Nat.Cast.Basic", "Mathlib.Data.Nat.Cast.Commute", "Mathlib.Tactic.NormNum.Core", "Mathlib.Tactic.HaveI", "Mathlib.Tactic.ClearExclamation" ]
Mathlib/Tactic/NormNum/Basic.lean
isInt_neg
null
isRat_neg {α} [Ring α] : ∀ {f : α → α} {a : α} {n n' : ℤ} {d : ℕ}, f = Neg.neg → IsRat a n d → Int.neg n = n' → IsRat (-a) n' d | _, _, _, _, _, rfl, ⟨h, rfl⟩, rfl => ⟨h, by rw [← neg_mul, ← Int.cast_neg]; rfl⟩ attribute [local instance] monadLiftOptionMetaM in
theorem
Tactic
[ "Mathlib.Algebra.GroupWithZero.Invertible", "Mathlib.Algebra.Ring.Int.Defs", "Mathlib.Data.Nat.Cast.Basic", "Mathlib.Data.Nat.Cast.Commute", "Mathlib.Tactic.NormNum.Core", "Mathlib.Tactic.HaveI", "Mathlib.Tactic.ClearExclamation" ]
Mathlib/Tactic/NormNum/Basic.lean
isRat_neg
null
Result.neg {u : Level} {α : Q(Type u)} {a : Q($α)} (ra : Result q($a)) (rα : Q(Ring $α) := by exact q(delta% inferInstance)) : MetaM (Result q(-$a)) := do let intArm (rα : Q(Ring $α)) := do assumeInstancesCommute let ⟨za, na, pa⟩ ← ra.toInt rα let zb := -za have b := mkRawIntLit zb haveI' : Int.neg $na =Q $b := ⟨⟩ return .isInt rα b zb q(isInt_neg (.refl _) $pa (.refl $b)) let ratArm (dα : Q(DivisionRing $α)) : Option (Result _) := do assumeInstancesCommute let ⟨qa, na, da, pa⟩ ← ra.toRat' dα let qb := -qa have nb := mkRawIntLit qb.num haveI' : Int.neg $na =Q $nb := ⟨⟩ return .isRat dα qb nb da q(isRat_neg (.refl _) $pa (.refl $nb)) match ra with | .isBool _ .. => failure | .isNat _ .. => intArm rα | .isNegNat rα .. => intArm rα | .isNNRat _dsα .. => ratArm (← synthInstanceQ q(DivisionRing $α)) | .isNegNNRat dα .. => ratArm dα attribute [local instance] monadLiftOptionMetaM in
def
Tactic
[ "Mathlib.Algebra.GroupWithZero.Invertible", "Mathlib.Algebra.Ring.Int.Defs", "Mathlib.Data.Nat.Cast.Basic", "Mathlib.Data.Nat.Cast.Commute", "Mathlib.Tactic.NormNum.Core", "Mathlib.Tactic.HaveI", "Mathlib.Tactic.ClearExclamation" ]
Mathlib/Tactic/NormNum/Basic.lean
Result.neg
The result of negating a norm_num result.
@[norm_num -_] evalNeg : NormNumExt where eval {u α} e := do let .app (f : Q($α → $α)) (a : Q($α)) ← whnfR e | failure let ra ← derive a let rα ← inferRing α let ⟨(_f_eq : $f =Q Neg.neg)⟩ ← withNewMCtxDepth <| assertDefEqQ _ _ haveI' _e_eq : $e =Q -$a := ⟨⟩ ra.neg
def
Tactic
[ "Mathlib.Algebra.GroupWithZero.Invertible", "Mathlib.Algebra.Ring.Int.Defs", "Mathlib.Data.Nat.Cast.Basic", "Mathlib.Data.Nat.Cast.Commute", "Mathlib.Tactic.NormNum.Core", "Mathlib.Tactic.HaveI", "Mathlib.Tactic.ClearExclamation" ]
Mathlib/Tactic/NormNum/Basic.lean
evalNeg
The `norm_num` extension which identifies expressions of the form `-a`, such that `norm_num` successfully recognises `a`.
isInt_sub {α} [Ring α] : ∀ {f : α → α → α} {a b : α} {a' b' c : ℤ}, f = HSub.hSub → IsInt a a' → IsInt b b' → Int.sub a' b' = c → IsInt (f a b) c | _, _, _, _, _, _, rfl, ⟨rfl⟩, ⟨rfl⟩, rfl => ⟨(Int.cast_sub ..).symm⟩
theorem
Tactic
[ "Mathlib.Algebra.GroupWithZero.Invertible", "Mathlib.Algebra.Ring.Int.Defs", "Mathlib.Data.Nat.Cast.Basic", "Mathlib.Data.Nat.Cast.Commute", "Mathlib.Tactic.NormNum.Core", "Mathlib.Tactic.HaveI", "Mathlib.Tactic.ClearExclamation" ]
Mathlib/Tactic/NormNum/Basic.lean
isInt_sub
null
isRat_sub {α} [Ring α] {f : α → α → α} {a b : α} {na nb nc : ℤ} {da db dc k : ℕ} (hf : f = HSub.hSub) (ra : IsRat a na da) (rb : IsRat b nb db) (h₁ : Int.sub (Int.mul na db) (Int.mul nb da) = Int.mul k nc) (h₂ : Nat.mul da db = Nat.mul k dc) : IsRat (f a b) nc dc := by rw [hf, sub_eq_add_neg] refine isRat_add rfl ra (isRat_neg (n' := -nb) rfl rb rfl) (k := k) (nc := nc) ?_ h₂ rw [show Int.mul (-nb) _ = _ from neg_mul ..]; exact h₁ attribute [local instance] monadLiftOptionMetaM in
theorem
Tactic
[ "Mathlib.Algebra.GroupWithZero.Invertible", "Mathlib.Algebra.Ring.Int.Defs", "Mathlib.Data.Nat.Cast.Basic", "Mathlib.Data.Nat.Cast.Commute", "Mathlib.Tactic.NormNum.Core", "Mathlib.Tactic.HaveI", "Mathlib.Tactic.ClearExclamation" ]
Mathlib/Tactic/NormNum/Basic.lean
isRat_sub
null
Result.sub {u : Level} {α : Q(Type u)} {a b : Q($α)} (ra : Result q($a)) (rb : Result q($b)) (inst : Q(Ring $α) := by exact q(delta% inferInstance)) : MetaM (Result q($a - $b)) := do let intArm (rα : Q(Ring $α)) := do assumeInstancesCommute let ⟨za, na, pa⟩ ← ra.toInt rα; let ⟨zb, nb, pb⟩ ← rb.toInt rα let zc := za - zb have c := mkRawIntLit zc haveI' : Int.sub $na $nb =Q $c := ⟨⟩ return Result.isInt rα c zc q(isInt_sub (.refl _) $pa $pb (.refl $c)) let ratArm (dα : Q(DivisionRing $α)) : MetaM (Result _) := do assumeInstancesCommute let ⟨qa, na, da, pa⟩ ← ra.toRat' dα; let ⟨qb, nb, db, pb⟩ ← rb.toRat' dα let qc := qa - qb let dd := qa.den * qb.den let k := dd / qc.den have t1 : Q(ℤ) := mkRawIntLit (k * qc.num) have t2 : Q(ℕ) := mkRawNatLit dd have nc : Q(ℤ) := mkRawIntLit qc.num have dc : Q(ℕ) := mkRawNatLit qc.den have k : Q(ℕ) := mkRawNatLit k let r1 : Q(Int.sub (Int.mul $na $db) (Int.mul $nb $da) = Int.mul $k $nc) := (q(Eq.refl $t1) : Expr) let r2 : Q(Nat.mul $da $db = Nat.mul $k $dc) := (q(Eq.refl $t2) : Expr) return .isRat dα qc nc dc q(isRat_sub (.refl _) $pa $pb $r1 $r2) match ra, rb with | .isBool .., _ | _, .isBool .. => failure | .isNegNNRat dα .., _ | _, .isNegNNRat dα .. => ratArm dα | _, .isNNRat _dsα .. | .isNNRat _dsα .., _ => ratArm (← synthInstanceQ q(DivisionRing $α)) | .isNegNat _rα .., _ | _, .isNegNat _rα .. | .isNat _ .., .isNat _ .. => intArm inst attribute [local instance] monadLiftOptionMetaM in
def
Tactic
[ "Mathlib.Algebra.GroupWithZero.Invertible", "Mathlib.Algebra.Ring.Int.Defs", "Mathlib.Data.Nat.Cast.Basic", "Mathlib.Data.Nat.Cast.Commute", "Mathlib.Tactic.NormNum.Core", "Mathlib.Tactic.HaveI", "Mathlib.Tactic.ClearExclamation" ]
Mathlib/Tactic/NormNum/Basic.lean
Result.sub
The result of subtracting two norm_num results.
@[norm_num _ - _] evalSub : NormNumExt where eval {u α} e := do let .app (.app (f : Q($α → $α → $α)) (a : Q($α))) (b : Q($α)) ← whnfR e | failure let rα ← inferRing α let ⟨(_f_eq : $f =Q HSub.hSub)⟩ ← withNewMCtxDepth <| assertDefEqQ _ _ let ra ← derive a; let rb ← derive b haveI' _e_eq : $e =Q $a - $b := ⟨⟩ ra.sub rb
def
Tactic
[ "Mathlib.Algebra.GroupWithZero.Invertible", "Mathlib.Algebra.Ring.Int.Defs", "Mathlib.Data.Nat.Cast.Basic", "Mathlib.Data.Nat.Cast.Commute", "Mathlib.Tactic.NormNum.Core", "Mathlib.Tactic.HaveI", "Mathlib.Tactic.ClearExclamation" ]
Mathlib/Tactic/NormNum/Basic.lean
evalSub
The `norm_num` extension which identifies expressions of the form `a - b` in a ring, such that `norm_num` successfully recognises both `a` and `b`.
isNat_mul {α} [Semiring α] : ∀ {f : α → α → α} {a b : α} {a' b' c : ℕ}, f = HMul.hMul → IsNat a a' → IsNat b b' → Nat.mul a' b' = c → IsNat (a * b) c | _, _, _, _, _, _, rfl, ⟨rfl⟩, ⟨rfl⟩, rfl => ⟨(Nat.cast_mul ..).symm⟩
theorem
Tactic
[ "Mathlib.Algebra.GroupWithZero.Invertible", "Mathlib.Algebra.Ring.Int.Defs", "Mathlib.Data.Nat.Cast.Basic", "Mathlib.Data.Nat.Cast.Commute", "Mathlib.Tactic.NormNum.Core", "Mathlib.Tactic.HaveI", "Mathlib.Tactic.ClearExclamation" ]
Mathlib/Tactic/NormNum/Basic.lean
isNat_mul
null
isInt_mul {α} [Ring α] : ∀ {f : α → α → α} {a b : α} {a' b' c : ℤ}, f = HMul.hMul → IsInt a a' → IsInt b b' → Int.mul a' b' = c → IsInt (a * b) c | _, _, _, _, _, _, rfl, ⟨rfl⟩, ⟨rfl⟩, rfl => ⟨(Int.cast_mul ..).symm⟩
theorem
Tactic
[ "Mathlib.Algebra.GroupWithZero.Invertible", "Mathlib.Algebra.Ring.Int.Defs", "Mathlib.Data.Nat.Cast.Basic", "Mathlib.Data.Nat.Cast.Commute", "Mathlib.Tactic.NormNum.Core", "Mathlib.Tactic.HaveI", "Mathlib.Tactic.ClearExclamation" ]
Mathlib/Tactic/NormNum/Basic.lean
isInt_mul
null
isNNRat_mul {α} [Semiring α] {f : α → α → α} {a b : α} {na nb nc : ℕ} {da db dc k : ℕ} : f = HMul.hMul → IsNNRat a na da → IsNNRat b nb db → Nat.mul na nb = Nat.mul k nc → Nat.mul da db = Nat.mul k dc → IsNNRat (f a b) nc dc := by rintro rfl ⟨_, rfl⟩ ⟨_, rfl⟩ (h₁ : na * nb = k * nc) (h₂ : da * db = k * dc) have : Invertible (↑(da * db) : α) := by simpa using invertibleMul (da:α) db have := invertibleOfMul' (α := α) h₂ refine ⟨this, ?_⟩ have H := (Nat.cast_commute (α := α) da db).invOf_left.invOf_right.right_comm have h₁ := congr_arg (Nat.cast (R := α)) h₁ simp only [Nat.cast_mul] at h₁ simp only [← mul_assoc, (Nat.cast_commute (α := α) da nb).invOf_left.right_comm, h₁] have h₂ := congr_arg (↑nc * ↑· * (⅟↑da * ⅟↑db * ⅟↑dc : α)) h₂ simp only [Nat.cast_mul, ← mul_assoc] at h₂; rw [H] at h₂ simp only [mul_invOf_cancel_right'] at h₂; rw [h₂, Nat.cast_commute] simp only [mul_invOf_cancel_right', (Nat.cast_commute (α := α) da dc).invOf_left.invOf_right.right_comm, (Nat.cast_commute (α := α) db dc).invOf_left.invOf_right.right_comm]
theorem
Tactic
[ "Mathlib.Algebra.GroupWithZero.Invertible", "Mathlib.Algebra.Ring.Int.Defs", "Mathlib.Data.Nat.Cast.Basic", "Mathlib.Data.Nat.Cast.Commute", "Mathlib.Tactic.NormNum.Core", "Mathlib.Tactic.HaveI", "Mathlib.Tactic.ClearExclamation" ]
Mathlib/Tactic/NormNum/Basic.lean
isNNRat_mul
null
isRat_mul {α} [Ring α] {f : α → α → α} {a b : α} {na nb nc : ℤ} {da db dc k : ℕ} : f = HMul.hMul → IsRat a na da → IsRat b nb db → Int.mul na nb = Int.mul k nc → Nat.mul da db = Nat.mul k dc → IsRat (f a b) nc dc := by rintro rfl ⟨_, rfl⟩ ⟨_, rfl⟩ (h₁ : na * nb = k * nc) (h₂ : da * db = k * dc) have : Invertible (↑(da * db) : α) := by simpa using invertibleMul (da:α) db have := invertibleOfMul' (α := α) h₂ refine ⟨this, ?_⟩ have H := (Nat.cast_commute (α := α) da db).invOf_left.invOf_right.right_comm have h₁ := congr_arg (Int.cast (R := α)) h₁ simp only [Int.cast_mul, Int.cast_natCast] at h₁ simp only [← mul_assoc, (Nat.cast_commute (α := α) da nb).invOf_left.right_comm, h₁] have h₂ := congr_arg (↑nc * ↑· * (⅟↑da * ⅟↑db * ⅟↑dc : α)) h₂ simp only [Nat.cast_mul, ← mul_assoc] at h₂; rw [H] at h₂ simp only [mul_invOf_cancel_right'] at h₂; rw [h₂, Nat.cast_commute] simp only [mul_invOf_cancel_right, (Nat.cast_commute (α := α) da dc).invOf_left.invOf_right.right_comm, (Nat.cast_commute (α := α) db dc).invOf_left.invOf_right.right_comm] attribute [local instance] monadLiftOptionMetaM in
theorem
Tactic
[ "Mathlib.Algebra.GroupWithZero.Invertible", "Mathlib.Algebra.Ring.Int.Defs", "Mathlib.Data.Nat.Cast.Basic", "Mathlib.Data.Nat.Cast.Commute", "Mathlib.Tactic.NormNum.Core", "Mathlib.Tactic.HaveI", "Mathlib.Tactic.ClearExclamation" ]
Mathlib/Tactic/NormNum/Basic.lean
isRat_mul
null
Result.mul {u : Level} {α : Q(Type u)} {a b : Q($α)} (ra : Result q($a)) (rb : Result q($b)) (inst : Q(Semiring $α) := by exact q(delta% inferInstance)) : MetaM (Result q($a * $b)) := do let intArm (rα : Q(Ring $α)) := do assumeInstancesCommute let ⟨za, na, pa⟩ ← ra.toInt rα; let ⟨zb, nb, pb⟩ ← rb.toInt rα let zc := za * zb have c := mkRawIntLit zc haveI' : Int.mul $na $nb =Q $c := ⟨⟩ return .isInt rα c zc q(isInt_mul (.refl _) $pa $pb (.refl $c)) let nnratArm (dsα : Q(DivisionSemiring $α)) : Option (Result _) := do assumeInstancesCommute let ⟨qa, na, da, pa⟩ ← ra.toNNRat' dsα; let ⟨qb, nb, db, pb⟩ ← rb.toNNRat' dsα let qc := qa * qb let dd := qa.den * qb.den let k := dd / qc.den have nc : Q(ℕ) := mkRawNatLit qc.num.toNat have dc : Q(ℕ) := mkRawNatLit qc.den have k : Q(ℕ) := mkRawNatLit k let r1 : Q(Nat.mul $na $nb = Nat.mul $k $nc) := (q(Eq.refl (Nat.mul $na $nb)) : Expr) have t2 : Q(ℕ) := mkRawNatLit dd let r2 : Q(Nat.mul $da $db = Nat.mul $k $dc) := (q(Eq.refl $t2) : Expr) return .isNNRat' dsα qc nc dc q(isNNRat_mul (.refl _) $pa $pb $r1 $r2) let rec ratArm (dα : Q(DivisionRing $α)) : Option (Result _) := do assumeInstancesCommute let ⟨qa, na, da, pa⟩ ← ra.toRat' dα; let ⟨qb, nb, db, pb⟩ ← rb.toRat' dα let qc := qa * qb let dd := qa.den * qb.den let k := dd / qc.den have nc : Q(ℤ) := mkRawIntLit qc.num have dc : Q(ℕ) := mkRawNatLit qc.den have k : Q(ℕ) := mkRawNatLit k let r1 : Q(Int.mul $na $nb = Int.mul $k $nc) := (q(Eq.refl (Int.mul $na $nb)) : Expr) have t2 : Q(ℕ) := mkRawNatLit dd let r2 : Q(Nat.mul $da $db = Nat.mul $k $dc) := (q(Eq.refl $t2) : Expr) return .isRat dα qc nc dc q(isRat_mul (.refl _) $pa $pb $r1 $r2) match ra, rb with | .isBool .., _ | _, .isBool .. => failure | .isNegNNRat dα .., _ | _, .isNegNNRat dα .. => ratArm dα | .isNNRat dsα .., .isNegNat rα .. | .isNegNat rα .., .isNNRat dsα .. => ratArm (←synthInstanceQ q(DivisionRing $α)) | .isNNRat dsα .., _ | _, .isNNRat dsα .. => nnratArm dsα | .isNegNat rα .., _ | _, .isNegNat rα .. => intArm rα | .isNat mα' na pa, .isNat mα nb pb => do haveI' : $mα =Q by clear! $mα $mα'; apply AddCommMonoidWithOne.toAddMonoidWithOne := ⟨⟩ assumeInstancesCommute have c : Q(ℕ) := mkRawNatLit (na.natLit! * nb.natLit!) ...
def
Tactic
[ "Mathlib.Algebra.GroupWithZero.Invertible", "Mathlib.Algebra.Ring.Int.Defs", "Mathlib.Data.Nat.Cast.Basic", "Mathlib.Data.Nat.Cast.Commute", "Mathlib.Tactic.NormNum.Core", "Mathlib.Tactic.HaveI", "Mathlib.Tactic.ClearExclamation" ]
Mathlib/Tactic/NormNum/Basic.lean
Result.mul
The result of multiplying two norm_num results.
@[norm_num _ * _] evalMul : NormNumExt where eval {u α} e := do let .app (.app (f : Q($α → $α → $α)) (a : Q($α))) (b : Q($α)) ← whnfR e | failure let sα ← inferSemiring α let ra ← derive a; let rb ← derive b guard <|← withNewMCtxDepth <| isDefEq f q(HMul.hMul (α := $α)) haveI' : $f =Q HMul.hMul := ⟨⟩ haveI' : $e =Q $a * $b := ⟨⟩ ra.mul rb
def
Tactic
[ "Mathlib.Algebra.GroupWithZero.Invertible", "Mathlib.Algebra.Ring.Int.Defs", "Mathlib.Data.Nat.Cast.Basic", "Mathlib.Data.Nat.Cast.Commute", "Mathlib.Tactic.NormNum.Core", "Mathlib.Tactic.HaveI", "Mathlib.Tactic.ClearExclamation" ]
Mathlib/Tactic/NormNum/Basic.lean
evalMul
The `norm_num` extension which identifies expressions of the form `a * b`, such that `norm_num` successfully recognises both `a` and `b`.
isNNRat_div {α : Type u} [DivisionSemiring α] : {a b : α} → {cn : ℕ} → {cd : ℕ} → IsNNRat (a * b⁻¹) cn cd → IsNNRat (a / b) cn cd | _, _, _, _, h => by simpa [div_eq_mul_inv] using h
theorem
Tactic
[ "Mathlib.Algebra.GroupWithZero.Invertible", "Mathlib.Algebra.Ring.Int.Defs", "Mathlib.Data.Nat.Cast.Basic", "Mathlib.Data.Nat.Cast.Commute", "Mathlib.Tactic.NormNum.Core", "Mathlib.Tactic.HaveI", "Mathlib.Tactic.ClearExclamation" ]
Mathlib/Tactic/NormNum/Basic.lean
isNNRat_div
null
isRat_div {α : Type u} [DivisionRing α] : {a b : α} → {cn : ℤ} → {cd : ℕ} → IsRat (a * b⁻¹) cn cd → IsRat (a / b) cn cd | _, _, _, _, h => by simpa [div_eq_mul_inv] using h
theorem
Tactic
[ "Mathlib.Algebra.GroupWithZero.Invertible", "Mathlib.Algebra.Ring.Int.Defs", "Mathlib.Data.Nat.Cast.Basic", "Mathlib.Data.Nat.Cast.Commute", "Mathlib.Tactic.NormNum.Core", "Mathlib.Tactic.HaveI", "Mathlib.Tactic.ClearExclamation" ]
Mathlib/Tactic/NormNum/Basic.lean
isRat_div
null
inferDivisionSemiring {u : Level} (α : Q(Type u)) : MetaM Q(DivisionSemiring $α) := return ← synthInstanceQ q(DivisionSemiring $α) <|> throwError "not a division semiring"
def
Tactic
[ "Mathlib.Algebra.GroupWithZero.Invertible", "Mathlib.Algebra.Ring.Int.Defs", "Mathlib.Data.Nat.Cast.Basic", "Mathlib.Data.Nat.Cast.Commute", "Mathlib.Tactic.NormNum.Core", "Mathlib.Tactic.HaveI", "Mathlib.Tactic.ClearExclamation" ]
Mathlib/Tactic/NormNum/Basic.lean
inferDivisionSemiring
Helper function to synthesize a typed `DivisionSemiring α` expression.
inferDivisionRing {u : Level} (α : Q(Type u)) : MetaM Q(DivisionRing $α) := return ← synthInstanceQ q(DivisionRing $α) <|> throwError "not a division ring" attribute [local instance] monadLiftOptionMetaM in
def
Tactic
[ "Mathlib.Algebra.GroupWithZero.Invertible", "Mathlib.Algebra.Ring.Int.Defs", "Mathlib.Data.Nat.Cast.Basic", "Mathlib.Data.Nat.Cast.Commute", "Mathlib.Tactic.NormNum.Core", "Mathlib.Tactic.HaveI", "Mathlib.Tactic.ClearExclamation" ]
Mathlib/Tactic/NormNum/Basic.lean
inferDivisionRing
Helper function to synthesize a typed `DivisionRing α` expression.
@[norm_num _ / _] evalDiv : NormNumExt where eval {u α} e := do let .app (.app f (a : Q($α))) (b : Q($α)) ← whnfR e | failure let dsα ← inferDivisionSemiring α haveI' : $e =Q $a / $b := ⟨⟩ guard <| ← withNewMCtxDepth <| isDefEq f q(HDiv.hDiv (α := $α)) let rab ← derive (q($a * $b⁻¹) : Q($α)) if let some ⟨qa, na, da, pa⟩ := rab.toNNRat' dsα then assumeInstancesCommute return .isNNRat' dsα qa na da q(isNNRat_div $pa) else let dα ← inferDivisionRing α let ⟨qa, na, da, pa⟩ ← rab.toRat' dα assumeInstancesCommute return .isRat dα qa na da q(isRat_div $pa) /-! # Logic -/
def
Tactic
[ "Mathlib.Algebra.GroupWithZero.Invertible", "Mathlib.Algebra.Ring.Int.Defs", "Mathlib.Data.Nat.Cast.Basic", "Mathlib.Data.Nat.Cast.Commute", "Mathlib.Tactic.NormNum.Core", "Mathlib.Tactic.HaveI", "Mathlib.Tactic.ClearExclamation" ]
Mathlib/Tactic/NormNum/Basic.lean
evalDiv
The `norm_num` extension which identifies expressions of the form `a / b`, such that `norm_num` successfully recognises both `a` and `b`.
@[norm_num True] evalTrue : NormNumExt where eval {u α} e := return (.isTrue q(True.intro) : Result q(True))
def
Tactic
[ "Mathlib.Algebra.GroupWithZero.Invertible", "Mathlib.Algebra.Ring.Int.Defs", "Mathlib.Data.Nat.Cast.Basic", "Mathlib.Data.Nat.Cast.Commute", "Mathlib.Tactic.NormNum.Core", "Mathlib.Tactic.HaveI", "Mathlib.Tactic.ClearExclamation" ]
Mathlib/Tactic/NormNum/Basic.lean
evalTrue
The `norm_num` extension which identifies `True`.
@[norm_num False] evalFalse : NormNumExt where eval {u α} e := return (.isFalse q(not_false) : Result q(False))
def
Tactic
[ "Mathlib.Algebra.GroupWithZero.Invertible", "Mathlib.Algebra.Ring.Int.Defs", "Mathlib.Data.Nat.Cast.Basic", "Mathlib.Data.Nat.Cast.Commute", "Mathlib.Tactic.NormNum.Core", "Mathlib.Tactic.HaveI", "Mathlib.Tactic.ClearExclamation" ]
Mathlib/Tactic/NormNum/Basic.lean
evalFalse
The `norm_num` extension which identifies `False`.
@[norm_num ¬_] evalNot : NormNumExt where eval {u α} e := do let .app (.const ``Not _) (a : Q(Prop)) ← whnfR e | failure guard <|← withNewMCtxDepth <| isDefEq α q(Prop) let ⟨b, p⟩ ← deriveBool q($a) match b with | true => return .isFalse q(not_not_intro $p) | false => return .isTrue q($p) /-! # (In)equalities -/ variable {α : Type u}
def
Tactic
[ "Mathlib.Algebra.GroupWithZero.Invertible", "Mathlib.Algebra.Ring.Int.Defs", "Mathlib.Data.Nat.Cast.Basic", "Mathlib.Data.Nat.Cast.Commute", "Mathlib.Tactic.NormNum.Core", "Mathlib.Tactic.HaveI", "Mathlib.Tactic.ClearExclamation" ]
Mathlib/Tactic/NormNum/Basic.lean
evalNot
The `norm_num` extension which identifies expressions of the form `¬a`, such that `norm_num` successfully recognises `a`.
isNat_eq_true [AddMonoidWithOne α] : {a b : α} → {c : ℕ} → IsNat a c → IsNat b c → a = b | _, _, _, ⟨rfl⟩, ⟨rfl⟩ => rfl
theorem
Tactic
[ "Mathlib.Algebra.GroupWithZero.Invertible", "Mathlib.Algebra.Ring.Int.Defs", "Mathlib.Data.Nat.Cast.Basic", "Mathlib.Data.Nat.Cast.Commute", "Mathlib.Tactic.NormNum.Core", "Mathlib.Tactic.HaveI", "Mathlib.Tactic.ClearExclamation" ]
Mathlib/Tactic/NormNum/Basic.lean
isNat_eq_true
null
ble_eq_false {x y : ℕ} : x.ble y = false ↔ y < x := by rw [← Nat.not_le, ← Bool.not_eq_true, Nat.ble_eq]
theorem
Tactic
[ "Mathlib.Algebra.GroupWithZero.Invertible", "Mathlib.Algebra.Ring.Int.Defs", "Mathlib.Data.Nat.Cast.Basic", "Mathlib.Data.Nat.Cast.Commute", "Mathlib.Tactic.NormNum.Core", "Mathlib.Tactic.HaveI", "Mathlib.Tactic.ClearExclamation" ]
Mathlib/Tactic/NormNum/Basic.lean
ble_eq_false
null
isInt_eq_true [Ring α] : {a b : α} → {z : ℤ} → IsInt a z → IsInt b z → a = b | _, _, _, ⟨rfl⟩, ⟨rfl⟩ => rfl
theorem
Tactic
[ "Mathlib.Algebra.GroupWithZero.Invertible", "Mathlib.Algebra.Ring.Int.Defs", "Mathlib.Data.Nat.Cast.Basic", "Mathlib.Data.Nat.Cast.Commute", "Mathlib.Tactic.NormNum.Core", "Mathlib.Tactic.HaveI", "Mathlib.Tactic.ClearExclamation" ]
Mathlib/Tactic/NormNum/Basic.lean
isInt_eq_true
null
isNNRat_eq_true [Semiring α] : {a b : α} → {n : ℕ} → {d : ℕ} → IsNNRat a n d → IsNNRat b n d → a = b | _, _, _, _, ⟨_, rfl⟩, ⟨_, rfl⟩ => by congr; apply Subsingleton.elim
theorem
Tactic
[ "Mathlib.Algebra.GroupWithZero.Invertible", "Mathlib.Algebra.Ring.Int.Defs", "Mathlib.Data.Nat.Cast.Basic", "Mathlib.Data.Nat.Cast.Commute", "Mathlib.Tactic.NormNum.Core", "Mathlib.Tactic.HaveI", "Mathlib.Tactic.ClearExclamation" ]
Mathlib/Tactic/NormNum/Basic.lean
isNNRat_eq_true
null
isRat_eq_true [Ring α] : {a b : α} → {n : ℤ} → {d : ℕ} → IsRat a n d → IsRat b n d → a = b | _, _, _, _, ⟨_, rfl⟩, ⟨_, rfl⟩ => by congr; apply Subsingleton.elim
theorem
Tactic
[ "Mathlib.Algebra.GroupWithZero.Invertible", "Mathlib.Algebra.Ring.Int.Defs", "Mathlib.Data.Nat.Cast.Basic", "Mathlib.Data.Nat.Cast.Commute", "Mathlib.Tactic.NormNum.Core", "Mathlib.Tactic.HaveI", "Mathlib.Tactic.ClearExclamation" ]
Mathlib/Tactic/NormNum/Basic.lean
isRat_eq_true
null
eq_of_true {a b : Prop} (ha : a) (hb : b) : a = b := propext (iff_of_true ha hb)
theorem
Tactic
[ "Mathlib.Algebra.GroupWithZero.Invertible", "Mathlib.Algebra.Ring.Int.Defs", "Mathlib.Data.Nat.Cast.Basic", "Mathlib.Data.Nat.Cast.Commute", "Mathlib.Tactic.NormNum.Core", "Mathlib.Tactic.HaveI", "Mathlib.Tactic.ClearExclamation" ]
Mathlib/Tactic/NormNum/Basic.lean
eq_of_true
null
ne_of_false_of_true {a b : Prop} (ha : ¬a) (hb : b) : a ≠ b := mt (· ▸ hb) ha
theorem
Tactic
[ "Mathlib.Algebra.GroupWithZero.Invertible", "Mathlib.Algebra.Ring.Int.Defs", "Mathlib.Data.Nat.Cast.Basic", "Mathlib.Data.Nat.Cast.Commute", "Mathlib.Tactic.NormNum.Core", "Mathlib.Tactic.HaveI", "Mathlib.Tactic.ClearExclamation" ]
Mathlib/Tactic/NormNum/Basic.lean
ne_of_false_of_true
null
ne_of_true_of_false {a b : Prop} (ha : a) (hb : ¬b) : a ≠ b := mt (· ▸ ha) hb
theorem
Tactic
[ "Mathlib.Algebra.GroupWithZero.Invertible", "Mathlib.Algebra.Ring.Int.Defs", "Mathlib.Data.Nat.Cast.Basic", "Mathlib.Data.Nat.Cast.Commute", "Mathlib.Tactic.NormNum.Core", "Mathlib.Tactic.HaveI", "Mathlib.Tactic.ClearExclamation" ]
Mathlib/Tactic/NormNum/Basic.lean
ne_of_true_of_false
null
eq_of_false {a b : Prop} (ha : ¬a) (hb : ¬b) : a = b := propext (iff_of_false ha hb) /-! # Nat operations -/
theorem
Tactic
[ "Mathlib.Algebra.GroupWithZero.Invertible", "Mathlib.Algebra.Ring.Int.Defs", "Mathlib.Data.Nat.Cast.Basic", "Mathlib.Data.Nat.Cast.Commute", "Mathlib.Tactic.NormNum.Core", "Mathlib.Tactic.HaveI", "Mathlib.Tactic.ClearExclamation" ]
Mathlib/Tactic/NormNum/Basic.lean
eq_of_false
null
isNat_natSucc : {a : ℕ} → {a' c : ℕ} → IsNat a a' → Nat.succ a' = c → IsNat (a.succ) c | _, _,_, ⟨rfl⟩, rfl => ⟨by simp⟩
theorem
Tactic
[ "Mathlib.Algebra.GroupWithZero.Invertible", "Mathlib.Algebra.Ring.Int.Defs", "Mathlib.Data.Nat.Cast.Basic", "Mathlib.Data.Nat.Cast.Commute", "Mathlib.Tactic.NormNum.Core", "Mathlib.Tactic.HaveI", "Mathlib.Tactic.ClearExclamation" ]
Mathlib/Tactic/NormNum/Basic.lean
isNat_natSucc
null
@[norm_num Nat.succ _] evalNatSucc : NormNumExt where eval {u α} e := do let .app f (a : Q(ℕ)) ← whnfR e | failure guard <|← withNewMCtxDepth <| isDefEq f q(Nat.succ) haveI' : u =QL 0 := ⟨⟩; haveI' : $α =Q ℕ := ⟨⟩ haveI' : $e =Q Nat.succ $a := ⟨⟩ let sℕ : Q(AddMonoidWithOne ℕ) := q(instAddMonoidWithOneNat) let ⟨na, pa⟩ ← deriveNat a sℕ have nc : Q(ℕ) := mkRawNatLit (na.natLit!.succ) haveI' : $nc =Q ($na).succ := ⟨⟩ return .isNat sℕ nc q(isNat_natSucc $pa (.refl $nc))
def
Tactic
[ "Mathlib.Algebra.GroupWithZero.Invertible", "Mathlib.Algebra.Ring.Int.Defs", "Mathlib.Data.Nat.Cast.Basic", "Mathlib.Data.Nat.Cast.Commute", "Mathlib.Tactic.NormNum.Core", "Mathlib.Tactic.HaveI", "Mathlib.Tactic.ClearExclamation" ]
Mathlib/Tactic/NormNum/Basic.lean
evalNatSucc
The `norm_num` extension which identifies expressions of the form `Nat.succ a`, such that `norm_num` successfully recognises `a`.
isNat_natSub : {a b : ℕ} → {a' b' c : ℕ} → IsNat a a' → IsNat b b' → Nat.sub a' b' = c → IsNat (a - b) c | _, _, _, _, _, ⟨rfl⟩, ⟨rfl⟩, rfl => ⟨by simp⟩
theorem
Tactic
[ "Mathlib.Algebra.GroupWithZero.Invertible", "Mathlib.Algebra.Ring.Int.Defs", "Mathlib.Data.Nat.Cast.Basic", "Mathlib.Data.Nat.Cast.Commute", "Mathlib.Tactic.NormNum.Core", "Mathlib.Tactic.HaveI", "Mathlib.Tactic.ClearExclamation" ]
Mathlib/Tactic/NormNum/Basic.lean
isNat_natSub
null
@[norm_num (_ : ℕ) - _] evalNatSub : NormNumExt where eval {u α} e := do let .app (.app f (a : Q(ℕ))) (b : Q(ℕ)) ← whnfR e | failure guard <|← withNewMCtxDepth <| isDefEq f q(HSub.hSub (α := ℕ)) haveI' : u =QL 0 := ⟨⟩; haveI' : $α =Q ℕ := ⟨⟩ haveI' : $e =Q $a - $b := ⟨⟩ let sℕ : Q(AddMonoidWithOne ℕ) := q(instAddMonoidWithOneNat) let ⟨na, pa⟩ ← deriveNat a sℕ; let ⟨nb, pb⟩ ← deriveNat b sℕ have nc : Q(ℕ) := mkRawNatLit (na.natLit! - nb.natLit!) haveI' : Nat.sub $na $nb =Q $nc := ⟨⟩ return .isNat sℕ nc q(isNat_natSub $pa $pb (.refl $nc))
def
Tactic
[ "Mathlib.Algebra.GroupWithZero.Invertible", "Mathlib.Algebra.Ring.Int.Defs", "Mathlib.Data.Nat.Cast.Basic", "Mathlib.Data.Nat.Cast.Commute", "Mathlib.Tactic.NormNum.Core", "Mathlib.Tactic.HaveI", "Mathlib.Tactic.ClearExclamation" ]
Mathlib/Tactic/NormNum/Basic.lean
evalNatSub
The `norm_num` extension which identifies expressions of the form `Nat.sub a b`, such that `norm_num` successfully recognises both `a` and `b`.
isNat_natMod : {a b : ℕ} → {a' b' c : ℕ} → IsNat a a' → IsNat b b' → Nat.mod a' b' = c → IsNat (a % b) c | _, _, _, _, _, ⟨rfl⟩, ⟨rfl⟩, rfl => ⟨by aesop⟩
theorem
Tactic
[ "Mathlib.Algebra.GroupWithZero.Invertible", "Mathlib.Algebra.Ring.Int.Defs", "Mathlib.Data.Nat.Cast.Basic", "Mathlib.Data.Nat.Cast.Commute", "Mathlib.Tactic.NormNum.Core", "Mathlib.Tactic.HaveI", "Mathlib.Tactic.ClearExclamation" ]
Mathlib/Tactic/NormNum/Basic.lean
isNat_natMod
null
@[norm_num (_ : ℕ) % _] evalNatMod : NormNumExt where eval {u α} e := do let .app (.app f (a : Q(ℕ))) (b : Q(ℕ)) ← whnfR e | failure haveI' : u =QL 0 := ⟨⟩; haveI' : $α =Q ℕ := ⟨⟩ haveI' : $e =Q $a % $b := ⟨⟩ guard <|← withNewMCtxDepth <| isDefEq f q(HMod.hMod (α := ℕ)) let sℕ : Q(AddMonoidWithOne ℕ) := q(instAddMonoidWithOneNat) let ⟨na, pa⟩ ← deriveNat a sℕ; let ⟨nb, pb⟩ ← deriveNat b sℕ have nc : Q(ℕ) := mkRawNatLit (na.natLit! % nb.natLit!) haveI' : Nat.mod $na $nb =Q $nc := ⟨⟩ return .isNat sℕ nc q(isNat_natMod $pa $pb (.refl $nc))
def
Tactic
[ "Mathlib.Algebra.GroupWithZero.Invertible", "Mathlib.Algebra.Ring.Int.Defs", "Mathlib.Data.Nat.Cast.Basic", "Mathlib.Data.Nat.Cast.Commute", "Mathlib.Tactic.NormNum.Core", "Mathlib.Tactic.HaveI", "Mathlib.Tactic.ClearExclamation" ]
Mathlib/Tactic/NormNum/Basic.lean
evalNatMod
The `norm_num` extension which identifies expressions of the form `Nat.mod a b`, such that `norm_num` successfully recognises both `a` and `b`.
isNat_natDiv : {a b : ℕ} → {a' b' c : ℕ} → IsNat a a' → IsNat b b' → Nat.div a' b' = c → IsNat (a / b) c | _, _, _, _, _, ⟨rfl⟩, ⟨rfl⟩, rfl => ⟨by aesop⟩
theorem
Tactic
[ "Mathlib.Algebra.GroupWithZero.Invertible", "Mathlib.Algebra.Ring.Int.Defs", "Mathlib.Data.Nat.Cast.Basic", "Mathlib.Data.Nat.Cast.Commute", "Mathlib.Tactic.NormNum.Core", "Mathlib.Tactic.HaveI", "Mathlib.Tactic.ClearExclamation" ]
Mathlib/Tactic/NormNum/Basic.lean
isNat_natDiv
null
@[norm_num (_ : ℕ) / _] evalNatDiv : NormNumExt where eval {u α} e := do let .app (.app f (a : Q(ℕ))) (b : Q(ℕ)) ← whnfR e | failure haveI' : u =QL 0 := ⟨⟩; haveI' : $α =Q ℕ := ⟨⟩ haveI' : $e =Q $a / $b := ⟨⟩ guard <|← withNewMCtxDepth <| isDefEq f q(HDiv.hDiv (α := ℕ)) let sℕ : Q(AddMonoidWithOne ℕ) := q(instAddMonoidWithOneNat) let ⟨na, pa⟩ ← deriveNat a sℕ; let ⟨nb, pb⟩ ← deriveNat b sℕ have nc : Q(ℕ) := mkRawNatLit (na.natLit! / nb.natLit!) haveI' : Nat.div $na $nb =Q $nc := ⟨⟩ return .isNat sℕ nc q(isNat_natDiv $pa $pb (.refl $nc))
def
Tactic
[ "Mathlib.Algebra.GroupWithZero.Invertible", "Mathlib.Algebra.Ring.Int.Defs", "Mathlib.Data.Nat.Cast.Basic", "Mathlib.Data.Nat.Cast.Commute", "Mathlib.Tactic.NormNum.Core", "Mathlib.Tactic.HaveI", "Mathlib.Tactic.ClearExclamation" ]
Mathlib/Tactic/NormNum/Basic.lean
evalNatDiv
The `norm_num` extension which identifies expressions of the form `Nat.div a b`, such that `norm_num` successfully recognises both `a` and `b`.
isNat_dvd_true : {a b : ℕ} → {a' b' : ℕ} → IsNat a a' → IsNat b b' → Nat.mod b' a' = nat_lit 0 → a ∣ b | _, _, _, _, ⟨rfl⟩, ⟨rfl⟩, e => Nat.dvd_of_mod_eq_zero e
theorem
Tactic
[ "Mathlib.Algebra.GroupWithZero.Invertible", "Mathlib.Algebra.Ring.Int.Defs", "Mathlib.Data.Nat.Cast.Basic", "Mathlib.Data.Nat.Cast.Commute", "Mathlib.Tactic.NormNum.Core", "Mathlib.Tactic.HaveI", "Mathlib.Tactic.ClearExclamation" ]
Mathlib/Tactic/NormNum/Basic.lean
isNat_dvd_true
null
isNat_dvd_false : {a b : ℕ} → {a' b' c : ℕ} → IsNat a a' → IsNat b b' → Nat.mod b' a' = Nat.succ c → ¬a ∣ b | _, _, _, _, c, ⟨rfl⟩, ⟨rfl⟩, e => mt Nat.mod_eq_zero_of_dvd (e.symm ▸ Nat.succ_ne_zero c :)
theorem
Tactic
[ "Mathlib.Algebra.GroupWithZero.Invertible", "Mathlib.Algebra.Ring.Int.Defs", "Mathlib.Data.Nat.Cast.Basic", "Mathlib.Data.Nat.Cast.Commute", "Mathlib.Tactic.NormNum.Core", "Mathlib.Tactic.HaveI", "Mathlib.Tactic.ClearExclamation" ]
Mathlib/Tactic/NormNum/Basic.lean
isNat_dvd_false
null
@[norm_num (_ : ℕ) ∣ _] evalNatDvd : NormNumExt where eval {u α} e := do let .app (.app f (a : Q(ℕ))) (b : Q(ℕ)) ← whnfR e | failure guard <|← withNewMCtxDepth <| isDefEq f q(Dvd.dvd (α := ℕ)) let sℕ : Q(AddMonoidWithOne ℕ) := q(instAddMonoidWithOneNat) let ⟨na, pa⟩ ← deriveNat a sℕ; let ⟨nb, pb⟩ ← deriveNat b sℕ match nb.natLit! % na.natLit! with | 0 => have : Q(Nat.mod $nb $na = nat_lit 0) := (q(Eq.refl (nat_lit 0)) : Expr) return .isTrue q(isNat_dvd_true $pa $pb $this) | c+1 => have nc : Q(ℕ) := mkRawNatLit c have : Q(Nat.mod $nb $na = Nat.succ $nc) := (q(Eq.refl (Nat.succ $nc)) : Expr) return .isFalse q(isNat_dvd_false $pa $pb $this)
def
Tactic
[ "Mathlib.Algebra.GroupWithZero.Invertible", "Mathlib.Algebra.Ring.Int.Defs", "Mathlib.Data.Nat.Cast.Basic", "Mathlib.Data.Nat.Cast.Commute", "Mathlib.Tactic.NormNum.Core", "Mathlib.Tactic.HaveI", "Mathlib.Tactic.ClearExclamation" ]
Mathlib/Tactic/NormNum/Basic.lean
evalNatDvd
The `norm_num` extension which identifies expressions of the form `(a : ℕ) | b`, such that `norm_num` successfully recognises both `a` and `b`.
Nat.UnifyZeroOrSuccResult (n : Q(ℕ)) /-- `n` unifies with `0` -/ | zero (pf : $n =Q 0) /-- `n` unifies with `succ n'` for this specific `n'` -/ | succ (n' : Q(ℕ)) (pf : $n =Q Nat.succ $n')
inductive
Tactic
[ "Mathlib.Tactic.NormNum.Basic", "Mathlib.Data.List.FinRange", "Mathlib.Algebra.BigOperators.Group.Finset.Basic" ]
Mathlib/Tactic/NormNum/BigOperators.lean
Nat.UnifyZeroOrSuccResult
This represents the result of trying to determine whether the given expression `n : Q(ℕ)` is either `zero` or `succ`.
Nat.unifyZeroOrSucc (n : Q(ℕ)) : MetaM (Nat.UnifyZeroOrSuccResult n) := do match ← isDefEqQ n q(0) with | .defEq pf => return .zero pf | .notDefEq => do let n' : Q(ℕ) ← mkFreshExprMVar q(ℕ) let ⟨(_pf : $n =Q Nat.succ $n')⟩ ← assertDefEqQ n q(Nat.succ $n') let (.some (n'_val : Q(ℕ))) ← getExprMVarAssignment? n'.mvarId! | throwError "could not figure out value of `?n` from `{n} =?= Nat.succ ?n`" pure (.succ n'_val ⟨⟩)
def
Tactic
[ "Mathlib.Tactic.NormNum.Basic", "Mathlib.Data.List.FinRange", "Mathlib.Algebra.BigOperators.Group.Finset.Basic" ]
Mathlib/Tactic/NormNum/BigOperators.lean
Nat.unifyZeroOrSucc
Determine whether the expression `n : Q(ℕ)` unifies with `0` or `Nat.succ n'`. We do not use `norm_num` functionality because we want definitional equality, not propositional equality, for use in dependent types. Fails if neither of the options succeed.
List.ProveNilOrConsResult {α : Q(Type u)} (s : Q(List $α)) /-- The set is Nil. -/ | nil (pf : Q($s = [])) /-- The set equals `a` inserted into the strict subset `s'`. -/ | cons (a : Q($α)) (s' : Q(List $α)) (pf : Q($s = List.cons $a $s'))
inductive
Tactic
[ "Mathlib.Tactic.NormNum.Basic", "Mathlib.Data.List.FinRange", "Mathlib.Algebra.BigOperators.Group.Finset.Basic" ]
Mathlib/Tactic/NormNum/BigOperators.lean
List.ProveNilOrConsResult
This represents the result of trying to determine whether the given expression `s : Q(List $α)` is either empty or consists of an element inserted into a strict subset.
List.ProveNilOrConsResult.uncheckedCast {α : Q(Type u)} {β : Q(Type v)} (s : Q(List $α)) (t : Q(List $β)) : List.ProveNilOrConsResult s → List.ProveNilOrConsResult t | .nil pf => .nil pf | .cons a s' pf => .cons a s' pf
def
Tactic
[ "Mathlib.Tactic.NormNum.Basic", "Mathlib.Data.List.FinRange", "Mathlib.Algebra.BigOperators.Group.Finset.Basic" ]
Mathlib/Tactic/NormNum/BigOperators.lean
List.ProveNilOrConsResult.uncheckedCast
If `s` unifies with `t`, convert a result for `s` to a result for `t`. If `s` does not unify with `t`, this results in a type-incorrect proof.
List.ProveNilOrConsResult.eq_trans {α : Q(Type u)} {s t : Q(List $α)} (eq : Q($s = $t)) : List.ProveNilOrConsResult t → List.ProveNilOrConsResult s | .nil pf => .nil q(Eq.trans $eq $pf) | .cons a s' pf => .cons a s' q(Eq.trans $eq $pf)
def
Tactic
[ "Mathlib.Tactic.NormNum.Basic", "Mathlib.Data.List.FinRange", "Mathlib.Algebra.BigOperators.Group.Finset.Basic" ]
Mathlib/Tactic/NormNum/BigOperators.lean
List.ProveNilOrConsResult.eq_trans
If `s = t` and we can get the result for `t`, then we can get the result for `s`.
List.range_zero' {n : ℕ} (pn : NormNum.IsNat n 0) : List.range n = [] := by rw [pn.out, Nat.cast_zero, List.range_zero]
lemma
Tactic
[ "Mathlib.Tactic.NormNum.Basic", "Mathlib.Data.List.FinRange", "Mathlib.Algebra.BigOperators.Group.Finset.Basic" ]
Mathlib/Tactic/NormNum/BigOperators.lean
List.range_zero'
null