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 ⌀ |
|---|---|---|---|---|---|---|
GRewrite.dischargeMain (hrel : Expr) (goal : MVarId) : MetaM Unit := do
try
goal.gcongrForward #[hrel]
catch _ =>
throwTacticEx `grewrite goal m!"could not discharge {← goal.getType} using {← inferType hrel}" | def | Tactic | [
"Mathlib.Tactic.GCongr.Core"
] | Mathlib/Tactic/GRewrite/Core.lean | GRewrite.dischargeMain | Given a proof of `a ~ b`, close a goal of the form `a ~' b` or `b ~' a`
for some possibly different relation `~'`. |
GRewriteResult where
/-- The rewritten expression -/
eNew : Expr
/-- The proof of the implication. The direction depends on the argument `forwardImp`. -/
impProof : Expr
/-- The new side goals -/
mvarIds : List MVarId -- new goals | structure | Tactic | [
"Mathlib.Tactic.GCongr.Core"
] | Mathlib/Tactic/GRewrite/Core.lean | GRewriteResult | The result returned by `Lean.MVarId.grewrite`. |
GRewrite.Config extends Rewrite.Config where
/-- When `useRewrite = true`, switch to using the default `rewrite` tactic when the goal is
and equality or iff. -/
useRewrite : Bool := true
/-- When `implicationHyp = true`, interpret the rewrite rule as an implication. -/
implicationHyp : Bool := false | structure | Tactic | [
"Mathlib.Tactic.GCongr.Core"
] | Mathlib/Tactic/GRewrite/Core.lean | GRewrite.Config | Configures the behavior of the `rewrite` and `rw` tactics. |
_root_.Lean.MVarId.grewrite (goal : MVarId) (e : Expr) (hrel : Expr)
(forwardImp symm : Bool) (config : GRewrite.Config) : MetaM GRewriteResult :=
goal.withContext do
goal.checkNotAssigned `grewrite
let hrelType ← instantiateMVars (← inferType hrel)
let maxMVars? ←
if config.implicationHyp then
... | def | Tactic | [
"Mathlib.Tactic.GCongr.Core"
] | Mathlib/Tactic/GRewrite/Core.lean | _root_.Lean.MVarId.grewrite | Rewrite `e` using the relation `hrel : x ~ y`, and construct an implication proof
using the `gcongr` tactic to discharge this goal.
if `forwardImp = true`, we prove that `e → eNew`; otherwise `eNew → e`.
If `symm = false`, we rewrite `e` to `eNew := e[x/y]`; otherwise `eNew := e[y/x]`.
The code aligns with `Lean.MVa... |
grewriteTarget (stx : Syntax) (symm : Bool) (config : GRewrite.Config) : TacticM Unit := do
let goal ← getMainGoal
Term.withSynthesize <| goal.withContext do
let e ← elabTerm stx none true
if e.hasSyntheticSorry then
throwAbortTactic
let goal ← getMainGoal
let target ← goal.getType
let r ←... | def | Tactic | [
"Mathlib.Tactic.GRewrite.Core"
] | Mathlib/Tactic/GRewrite/Elab.lean | grewriteTarget | Apply the `grewrite` tactic to the current goal. |
grewriteLocalDecl (stx : Syntax) (symm : Bool) (fvarId : FVarId) (config : GRewrite.Config) :
TacticM Unit := withMainContext do
let goal ← getMainGoal
let r ← Term.withSynthesize <| withMainContext do
let e ← elabTerm stx none true
if e.hasSyntheticSorry then
throwAbortTactic
let localDecl ← ... | def | Tactic | [
"Mathlib.Tactic.GRewrite.Core"
] | Mathlib/Tactic/GRewrite/Elab.lean | grewriteLocalDecl | Apply the `grewrite` tactic to a local hypothesis. |
@[tactic grewriteSeq, inherit_doc grewriteSeq] evalGRewriteSeq : Tactic := fun stx => do
let cfg ← elabGRewriteConfig stx[1]
let loc := expandOptLocation stx[3]
withRWRulesSeq stx[0] stx[2] fun symm term => do
withLocation loc
(grewriteLocalDecl term symm · cfg)
(grewriteTarget term symm cfg)
... | def | Tactic | [
"Mathlib.Tactic.GRewrite.Core"
] | Mathlib/Tactic/GRewrite/Elab.lean | evalGRewriteSeq | null |
linarithGetProofsMessage (l : List Expr) : MetaM MessageData := do
return m!"{← l.mapM fun e => do instantiateMVars (← inferType e)}" | def | Tactic | [
"Mathlib.Tactic.Linarith.Lemmas",
"Mathlib.Tactic.NormNum.Basic",
"Mathlib.Util.SynthesizeUsing"
] | Mathlib/Tactic/Linarith/Datatypes.lean | linarithGetProofsMessage | A shorthand for getting the types of a list of proofs terms, to trace. |
linarithTraceProofs {α} [ToMessageData α] (s : α) (l : List Expr) : MetaM Unit := do
if ← isTracingEnabledFor `linarith then
addRawTrace <| .trace { cls := `linarith } (toMessageData s) #[← linarithGetProofsMessage l]
/-! ### Linear expressions -/ | def | Tactic | [
"Mathlib.Tactic.Linarith.Lemmas",
"Mathlib.Tactic.NormNum.Basic",
"Mathlib.Util.SynthesizeUsing"
] | Mathlib/Tactic/Linarith/Datatypes.lean | linarithTraceProofs | A shorthand for tracing the types of a list of proof terms
when the `trace.linarith` option is set to true. |
Linexp : Type := List (Nat × Int) | abbrev | Tactic | [
"Mathlib.Tactic.Linarith.Lemmas",
"Mathlib.Tactic.NormNum.Basic",
"Mathlib.Util.SynthesizeUsing"
] | Mathlib/Tactic/Linarith/Datatypes.lean | Linexp | A linear expression is a list of pairs of variable indices and coefficients,
representing the sum of the products of each coefficient with its corresponding variable.
Some functions on `Linexp` assume that `n : Nat` occurs at most once as the first element of a pair,
and that the list is sorted in decreasing order of ... |
partial add : Linexp → Linexp → Linexp
| [], a => a
| a, [] => a
| (a@(n1,z1)::t1), (b@(n2,z2)::t2) =>
if n1 < n2 then b::add (a::t1) t2
else if n2 < n1 then a::add t1 (b::t2)
else
let sum := z1 + z2
if sum = 0 then add t1 t2 else (n1, sum)::add t1 t2 | def | Tactic | [
"Mathlib.Tactic.Linarith.Lemmas",
"Mathlib.Tactic.NormNum.Basic",
"Mathlib.Util.SynthesizeUsing"
] | Mathlib/Tactic/Linarith/Datatypes.lean | add | Add two `Linexp`s together componentwise.
Preserves sorting and uniqueness of the first argument. |
scale (c : Int) (l : Linexp) : Linexp :=
if c = 0 then []
else if c = 1 then l
else l.map fun ⟨n, z⟩ => (n, z*c) | def | Tactic | [
"Mathlib.Tactic.Linarith.Lemmas",
"Mathlib.Tactic.NormNum.Basic",
"Mathlib.Util.SynthesizeUsing"
] | Mathlib/Tactic/Linarith/Datatypes.lean | scale | `l.scale c` scales the values in `l` by `c` without modifying the order or keys. |
get (n : Nat) : Linexp → Option Int
| [] => none
| ((a, b)::t) =>
if a < n then none
else if a = n then some b
else get n t | def | Tactic | [
"Mathlib.Tactic.Linarith.Lemmas",
"Mathlib.Tactic.NormNum.Basic",
"Mathlib.Util.SynthesizeUsing"
] | Mathlib/Tactic/Linarith/Datatypes.lean | get | `l.get n` returns the value in `l` associated with key `n`, if it exists, and `none` otherwise.
This function assumes that `l` is sorted in decreasing order of the first argument,
that is, it will return `none` as soon as it finds a key smaller than `n`. |
contains (n : Nat) : Linexp → Bool := Option.isSome ∘ get n | def | Tactic | [
"Mathlib.Tactic.Linarith.Lemmas",
"Mathlib.Tactic.NormNum.Basic",
"Mathlib.Util.SynthesizeUsing"
] | Mathlib/Tactic/Linarith/Datatypes.lean | contains | `l.contains n` is true iff `n` is the first element of a pair in `l`. |
zfind (n : Nat) (l : Linexp) : Int :=
match l.get n with
| none => 0
| some v => v | def | Tactic | [
"Mathlib.Tactic.Linarith.Lemmas",
"Mathlib.Tactic.NormNum.Basic",
"Mathlib.Util.SynthesizeUsing"
] | Mathlib/Tactic/Linarith/Datatypes.lean | zfind | `l.zfind n` returns the value associated with key `n` if there is one, and 0 otherwise. |
vars (l : Linexp) : List Nat :=
l.map Prod.fst | def | Tactic | [
"Mathlib.Tactic.Linarith.Lemmas",
"Mathlib.Tactic.NormNum.Basic",
"Mathlib.Util.SynthesizeUsing"
] | Mathlib/Tactic/Linarith/Datatypes.lean | vars | `l.vars` returns the list of variables that occur in `l`. |
cmp : Linexp → Linexp → Ordering
| [], [] => Ordering.eq
| [], _ => Ordering.lt
| _, [] => Ordering.gt
| ((n1,z1)::t1), ((n2,z2)::t2) =>
if n1 < n2 then Ordering.lt
else if n2 < n1 then Ordering.gt
else if z1 < z2 then Ordering.lt
else if z2 < z1 then Ordering.gt
else cmp t1 t2 | def | Tactic | [
"Mathlib.Tactic.Linarith.Lemmas",
"Mathlib.Tactic.NormNum.Basic",
"Mathlib.Util.SynthesizeUsing"
] | Mathlib/Tactic/Linarith/Datatypes.lean | cmp | Defines a lex ordering on `Linexp`. This function is performance critical. |
Comp : Type where
/-- The strength of the comparison, `<`, `≤`, or `=`. -/
str : Ineq
/-- The coefficients of the comparison, stored as list of pairs `(i, a)`,
where `i` is the index of a recorded atom, and `a` is the coefficient. -/
coeffs : Linexp
deriving Inhabited, Repr
attribute [nolint unusedArguments] ... | structure | Tactic | [
"Mathlib.Tactic.Linarith.Lemmas",
"Mathlib.Tactic.NormNum.Basic",
"Mathlib.Util.SynthesizeUsing"
] | Mathlib/Tactic/Linarith/Datatypes.lean | Comp | The main datatype for FM elimination.
Variables are represented by natural numbers, each of which has an integer coefficient.
Index 0 is reserved for constants, i.e. `coeffs.find 0` is the coefficient of 1.
The represented term is `coeffs.sum (fun ⟨k, v⟩ ↦ v * Var[k])`.
str determines the strength of the comparison -- ... |
Comp.vars : Comp → List Nat := Linexp.vars ∘ Comp.coeffs | def | Tactic | [
"Mathlib.Tactic.Linarith.Lemmas",
"Mathlib.Tactic.NormNum.Basic",
"Mathlib.Util.SynthesizeUsing"
] | Mathlib/Tactic/Linarith/Datatypes.lean | Comp.vars | `c.vars` returns the list of variables that appear in the linear expression contained in `c`. |
Comp.coeffOf (c : Comp) (a : Nat) : Int :=
c.coeffs.zfind a | def | Tactic | [
"Mathlib.Tactic.Linarith.Lemmas",
"Mathlib.Tactic.NormNum.Basic",
"Mathlib.Util.SynthesizeUsing"
] | Mathlib/Tactic/Linarith/Datatypes.lean | Comp.coeffOf | `c.coeffOf a` projects the coefficient of variable `a` out of `c`. |
Comp.scale (c : Comp) (n : Nat) : Comp :=
{ c with coeffs := c.coeffs.scale n } | def | Tactic | [
"Mathlib.Tactic.Linarith.Lemmas",
"Mathlib.Tactic.NormNum.Basic",
"Mathlib.Util.SynthesizeUsing"
] | Mathlib/Tactic/Linarith/Datatypes.lean | Comp.scale | `c.scale n` scales the coefficients of `c` by `n`. |
Comp.add (c1 c2 : Comp) : Comp :=
⟨c1.str.max c2.str, c1.coeffs.add c2.coeffs⟩ | def | Tactic | [
"Mathlib.Tactic.Linarith.Lemmas",
"Mathlib.Tactic.NormNum.Basic",
"Mathlib.Util.SynthesizeUsing"
] | Mathlib/Tactic/Linarith/Datatypes.lean | Comp.add | `Comp.add c1 c2` adds the expressions represented by `c1` and `c2`.
The coefficient of variable `a` in `c1.add c2`
is the sum of the coefficients of `a` in `c1` and `c2`. |
Comp.cmp : Comp → Comp → Ordering
| ⟨str1, coeffs1⟩, ⟨str2, coeffs2⟩ =>
match str1.cmp str2 with
| Ordering.lt => Ordering.lt
| Ordering.gt => Ordering.gt
| Ordering.eq => coeffs1.cmp coeffs2 | def | Tactic | [
"Mathlib.Tactic.Linarith.Lemmas",
"Mathlib.Tactic.NormNum.Basic",
"Mathlib.Util.SynthesizeUsing"
] | Mathlib/Tactic/Linarith/Datatypes.lean | Comp.cmp | `Comp` has a lex order. First the `ineq`s are compared, then the `coeff`s. |
Comp.isContr (c : Comp) : Bool := c.coeffs.isEmpty && c.str = Ineq.lt | def | Tactic | [
"Mathlib.Tactic.Linarith.Lemmas",
"Mathlib.Tactic.NormNum.Basic",
"Mathlib.Util.SynthesizeUsing"
] | Mathlib/Tactic/Linarith/Datatypes.lean | Comp.isContr | A `Comp` represents a contradiction if its expression has no coefficients and its strength is <,
that is, it represents the fact `0 < 0`. |
Comp.ToFormat : ToFormat Comp :=
⟨fun p => format p.coeffs ++ toString p.str ++ "0"⟩
/-! ### Parsing into linear form -/
/-! ### Control -/ | instance | Tactic | [
"Mathlib.Tactic.Linarith.Lemmas",
"Mathlib.Tactic.NormNum.Basic",
"Mathlib.Util.SynthesizeUsing"
] | Mathlib/Tactic/Linarith/Datatypes.lean | Comp.ToFormat | null |
PreprocessorBase : Type where
/-- The name of the preprocessor, populated automatically, to create linkable trace messages. -/
name : Name := by exact decl_name%
/-- The description of the preprocessor. -/
description : String | structure | Tactic | [
"Mathlib.Tactic.Linarith.Lemmas",
"Mathlib.Tactic.NormNum.Basic",
"Mathlib.Util.SynthesizeUsing"
] | Mathlib/Tactic/Linarith/Datatypes.lean | PreprocessorBase | Metadata about preprocessors, for trace output. |
Preprocessor : Type extends PreprocessorBase where
/-- Replace a hypothesis by a list of hypotheses. These expressions are the proof terms. -/
transform : Expr → MetaM (List Expr) | structure | Tactic | [
"Mathlib.Tactic.Linarith.Lemmas",
"Mathlib.Tactic.NormNum.Basic",
"Mathlib.Util.SynthesizeUsing"
] | Mathlib/Tactic/Linarith/Datatypes.lean | Preprocessor | A preprocessor transforms a proof of a proposition into a proof of a different proposition.
The return type is `List Expr`, since some preprocessing steps may create multiple new hypotheses,
and some may remove a hypothesis from the list.
A "no-op" preprocessor should return its input as a singleton list. |
GlobalPreprocessor : Type extends PreprocessorBase where
/-- Replace the collection of all hypotheses with new hypotheses.
These expressions are proof terms. -/
transform : List Expr → MetaM (List Expr) | structure | Tactic | [
"Mathlib.Tactic.Linarith.Lemmas",
"Mathlib.Tactic.NormNum.Basic",
"Mathlib.Util.SynthesizeUsing"
] | Mathlib/Tactic/Linarith/Datatypes.lean | GlobalPreprocessor | Some preprocessors need to examine the full list of hypotheses instead of working item by item.
As with `Preprocessor`, the input to a `GlobalPreprocessor` is replaced by, not added to, its
output. |
Branch : Type := MVarId × List Expr | def | Tactic | [
"Mathlib.Tactic.Linarith.Lemmas",
"Mathlib.Tactic.NormNum.Basic",
"Mathlib.Util.SynthesizeUsing"
] | Mathlib/Tactic/Linarith/Datatypes.lean | Branch | Some preprocessors perform branching case splits. A `Branch` is used to track one of these case
splits. The first component, an `MVarId`, is the goal corresponding to this branch of the split,
given as a metavariable. The `List Expr` component is the list of hypotheses for `linarith`
in this branch. |
GlobalBranchingPreprocessor : Type extends PreprocessorBase where
/-- Given a goal, and a list of hypotheses,
produce a list of pairs (consisting of a goal and list of hypotheses). -/
transform : MVarId → List Expr → MetaM (List Branch) | structure | Tactic | [
"Mathlib.Tactic.Linarith.Lemmas",
"Mathlib.Tactic.NormNum.Basic",
"Mathlib.Util.SynthesizeUsing"
] | Mathlib/Tactic/Linarith/Datatypes.lean | GlobalBranchingPreprocessor | Some preprocessors perform branching case splits.
A `GlobalBranchingPreprocessor` produces a list of branches to run.
Each branch is independent, so hypotheses that appear in multiple branches should be duplicated.
The preprocessor is responsible for making sure that each branch contains the correct goal
metavariable. |
Preprocessor.globalize (pp : Preprocessor) : GlobalPreprocessor where
__ := pp
transform := List.foldrM (fun e ret => do return (← pp.transform e) ++ ret) [] | def | Tactic | [
"Mathlib.Tactic.Linarith.Lemmas",
"Mathlib.Tactic.NormNum.Basic",
"Mathlib.Util.SynthesizeUsing"
] | Mathlib/Tactic/Linarith/Datatypes.lean | Preprocessor.globalize | A `Preprocessor` lifts to a `GlobalPreprocessor` by folding it over the input list. |
GlobalPreprocessor.branching (pp : GlobalPreprocessor) : GlobalBranchingPreprocessor where
__ := pp
transform := fun g l => do return [⟨g, ← pp.transform l⟩] | def | Tactic | [
"Mathlib.Tactic.Linarith.Lemmas",
"Mathlib.Tactic.NormNum.Basic",
"Mathlib.Util.SynthesizeUsing"
] | Mathlib/Tactic/Linarith/Datatypes.lean | GlobalPreprocessor.branching | A `GlobalPreprocessor` lifts to a `GlobalBranchingPreprocessor` by producing only one branch. |
GlobalBranchingPreprocessor.process (pp : GlobalBranchingPreprocessor)
(g : MVarId) (l : List Expr) : MetaM (List Branch) := g.withContext do
withTraceNode `linarith (fun e =>
return m!"{exceptEmoji e} {.ofConstName pp.name}: {pp.description}") do
let branches ← pp.transform g l
if branches.length >... | def | Tactic | [
"Mathlib.Tactic.Linarith.Lemmas",
"Mathlib.Tactic.NormNum.Basic",
"Mathlib.Util.SynthesizeUsing"
] | Mathlib/Tactic/Linarith/Datatypes.lean | GlobalBranchingPreprocessor.process | `process pp l` runs `pp.transform` on `l` and returns the result,
tracing the result if `trace.linarith` is on. |
PreprocessorToGlobalBranchingPreprocessor :
Coe Preprocessor GlobalBranchingPreprocessor :=
⟨GlobalPreprocessor.branching ∘ Preprocessor.globalize⟩ | instance | Tactic | [
"Mathlib.Tactic.Linarith.Lemmas",
"Mathlib.Tactic.NormNum.Basic",
"Mathlib.Util.SynthesizeUsing"
] | Mathlib/Tactic/Linarith/Datatypes.lean | PreprocessorToGlobalBranchingPreprocessor | null |
GlobalPreprocessorToGlobalBranchingPreprocessor :
Coe GlobalPreprocessor GlobalBranchingPreprocessor :=
⟨GlobalPreprocessor.branching⟩ | instance | Tactic | [
"Mathlib.Tactic.Linarith.Lemmas",
"Mathlib.Tactic.NormNum.Basic",
"Mathlib.Util.SynthesizeUsing"
] | Mathlib/Tactic/Linarith/Datatypes.lean | GlobalPreprocessorToGlobalBranchingPreprocessor | null |
CertificateOracle : Type where
/-- `produceCertificate hyps max_var` tries to derive a contradiction from the comparisons in
`hyps` by eliminating all variables ≤ `max_var`.
If successful, it returns a map `coeff : Nat → Nat` as a certificate.
This map represents that we can find a contradiction by taking the s... | structure | Tactic | [
"Mathlib.Tactic.Linarith.Lemmas",
"Mathlib.Tactic.NormNum.Basic",
"Mathlib.Util.SynthesizeUsing"
] | Mathlib/Tactic/Linarith/Datatypes.lean | CertificateOracle | A `CertificateOracle` provides a function
`produceCertificate : List Comp → Nat → MetaM (HashMap Nat Nat)`.
The default `CertificateOracle` used by `linarith` is
`Linarith.CertificateOracle.simplexAlgorithmSparse`.
`Linarith.CertificateOracle.simplexAlgorithmDense` and `Linarith.CertificateOracle.fourierMotzkin`
are a... |
parseCompAndExpr (e : Expr) : MetaM (Ineq × Expr) := do
let (rel, _, e, z) ← e.ineq?
if z.zero? then return (rel, e) else throwError "invalid comparison, rhs not zero: {z}" | def | Tactic | [
"Mathlib.Tactic.Linarith.Lemmas",
"Mathlib.Tactic.NormNum.Basic",
"Mathlib.Util.SynthesizeUsing"
] | Mathlib/Tactic/Linarith/Datatypes.lean | parseCompAndExpr | `parseCompAndExpr e` checks if `e` is of the form `t < 0`, `t ≤ 0`, or `t = 0`.
If it is, it returns the comparison along with `t`. |
mkSingleCompZeroOf (c : Nat) (h : Expr) : MetaM (Ineq × Expr) := do
let tp ← inferType h
let (iq, e) ← parseCompAndExpr tp
if c = 0 then do
let e' ← mkAppM ``zero_mul #[e]
return (Ineq.eq, e')
else if c = 1 then return (iq, h)
else do
let (_, tp, _) ← tp.ineq?
let cpos : Q(Prop) ← mkAppM ``GT.... | def | Tactic | [
"Mathlib.Tactic.Linarith.Lemmas",
"Mathlib.Tactic.NormNum.Basic",
"Mathlib.Util.SynthesizeUsing"
] | Mathlib/Tactic/Linarith/Datatypes.lean | mkSingleCompZeroOf | `mkSingleCompZeroOf c h` assumes that `h` is a proof of `t R 0`.
It produces a pair `(R', h')`, where `h'` is a proof of `c*t R' 0`.
Typically `R` and `R'` will be the same, except when `c = 0`, in which case `R'` is `=`.
If `c = 1`, `h'` is the same as `h` -- specifically, it does *not* change the type to `1*t R 0`. |
LinarithConfig : Type where
/-- Discharger to prove that a candidate linear combination of hypothesis is zero. -/
discharger : TacticM Unit := do evalTactic (← `(tactic| ring1))
/-- Prove goals which are not linear comparisons by first calling `exfalso`. -/
exfalso : Bool := true
/-- Transparency mode for ide... | structure | Tactic | [
"Mathlib.Control.Basic",
"Mathlib.Tactic.Linarith.Verification",
"Mathlib.Tactic.Linarith.Preprocessing",
"Mathlib.Tactic.Linarith.Oracle.SimplexAlgorithm",
"Mathlib.Tactic.Ring.Basic"
] | Mathlib/Tactic/Linarith/Frontend.lean | LinarithConfig | A configuration object for `linarith`. |
LinarithConfig.updateReducibility (cfg : LinarithConfig) (reduce_default : Bool) :
LinarithConfig :=
if reduce_default then
{ cfg with transparency := .default, discharger := do evalTactic (← `(tactic| ring1!)) }
else cfg | def | Tactic | [
"Mathlib.Control.Basic",
"Mathlib.Tactic.Linarith.Verification",
"Mathlib.Tactic.Linarith.Preprocessing",
"Mathlib.Tactic.Linarith.Oracle.SimplexAlgorithm",
"Mathlib.Tactic.Ring.Basic"
] | Mathlib/Tactic/Linarith/Frontend.lean | LinarithConfig.updateReducibility | `cfg.updateReducibility reduce_default` will change the transparency setting of `cfg` to
`default` if `reduce_default` is true. In this case, it also sets the discharger to `ring!`,
since this is typically needed when using stronger unification. |
getContrLemma (e : Expr) : MetaM (Name × Expr) := do
match ← e.ineqOrNotIneq? with
| (true, Ineq.lt, t, _) => pure (``lt_of_not_ge, t)
| (true, Ineq.le, t, _) => pure (``le_of_not_gt, t)
| (true, Ineq.eq, t, _) => pure (``eq_of_not_lt_of_not_gt, t)
| (false, _, t, _) => pure (``Not.intro, t) | def | Tactic | [
"Mathlib.Control.Basic",
"Mathlib.Tactic.Linarith.Verification",
"Mathlib.Tactic.Linarith.Preprocessing",
"Mathlib.Tactic.Linarith.Oracle.SimplexAlgorithm",
"Mathlib.Tactic.Ring.Basic"
] | Mathlib/Tactic/Linarith/Frontend.lean | getContrLemma | If `e` is a comparison `a R b` or the negation of a comparison `¬ a R b`, found in the target,
`getContrLemma e` returns the name of a lemma that will change the goal to an
implication, along with the type of `a` and `b`.
For example, if `e` is `(a : ℕ) < b`, returns ``(`lt_of_not_ge, ℕ)``. |
applyContrLemma (g : MVarId) : MetaM (Option (Expr × Expr) × MVarId) := do
try
let (nm, tp) ← getContrLemma (← withReducible g.getType')
let [g] ← g.apply (← mkConst' nm) | failure
let (f, g) ← g.intro1P
return (some (tp, .fvar f), g)
catch _ => return (none, g) | def | Tactic | [
"Mathlib.Control.Basic",
"Mathlib.Tactic.Linarith.Verification",
"Mathlib.Tactic.Linarith.Preprocessing",
"Mathlib.Tactic.Linarith.Oracle.SimplexAlgorithm",
"Mathlib.Tactic.Ring.Basic"
] | Mathlib/Tactic/Linarith/Frontend.lean | applyContrLemma | `applyContrLemma` inspects the target to see if it can be moved to a hypothesis by negation.
For example, a goal `⊢ a ≤ b` can become `b < a ⊢ false`.
If this is the case, it applies the appropriate lemma and introduces the new hypothesis.
It returns the type of the terms in the comparison (e.g. the type of `a` and `b`... |
ExprMultiMap α := Array (Expr × List α) | abbrev | Tactic | [
"Mathlib.Control.Basic",
"Mathlib.Tactic.Linarith.Verification",
"Mathlib.Tactic.Linarith.Preprocessing",
"Mathlib.Tactic.Linarith.Oracle.SimplexAlgorithm",
"Mathlib.Tactic.Ring.Basic"
] | Mathlib/Tactic/Linarith/Frontend.lean | ExprMultiMap | A map of keys to values, where the keys are `Expr` up to defeq and one key can be
associated to multiple values. |
ExprMultiMap.find {α : Type} (self : ExprMultiMap α) (k : Expr) : MetaM (Nat × List α) := do
for h : i in [:self.size] do
let (k', vs) := self[i]
if ← isDefEq k' k then
return (i, vs)
return (self.size, []) | def | Tactic | [
"Mathlib.Control.Basic",
"Mathlib.Tactic.Linarith.Verification",
"Mathlib.Tactic.Linarith.Preprocessing",
"Mathlib.Tactic.Linarith.Oracle.SimplexAlgorithm",
"Mathlib.Tactic.Ring.Basic"
] | Mathlib/Tactic/Linarith/Frontend.lean | ExprMultiMap.find | Retrieves the list of values at a key, as well as the index of the key for later modification.
(If the key is not in the map it returns `self.size` as the index.) |
ExprMultiMap.insert {α : Type} (self : ExprMultiMap α) (k : Expr) (v : α) :
MetaM (ExprMultiMap α) := do
for h : i in [:self.size] do
if ← isDefEq self[i].1 k then
return self.modify i fun (k, vs) => (k, v::vs)
return self.push (k, [v]) | def | Tactic | [
"Mathlib.Control.Basic",
"Mathlib.Tactic.Linarith.Verification",
"Mathlib.Tactic.Linarith.Preprocessing",
"Mathlib.Tactic.Linarith.Oracle.SimplexAlgorithm",
"Mathlib.Tactic.Ring.Basic"
] | Mathlib/Tactic/Linarith/Frontend.lean | ExprMultiMap.insert | Insert a new value into the map at key `k`. This does a defeq check with all other keys
in the map. |
partitionByType (l : List Expr) : MetaM (ExprMultiMap Expr) :=
l.foldlM (fun m h => do m.insert (← typeOfIneqProof h) h) #[] | def | Tactic | [
"Mathlib.Control.Basic",
"Mathlib.Tactic.Linarith.Verification",
"Mathlib.Tactic.Linarith.Preprocessing",
"Mathlib.Tactic.Linarith.Oracle.SimplexAlgorithm",
"Mathlib.Tactic.Ring.Basic"
] | Mathlib/Tactic/Linarith/Frontend.lean | partitionByType | `partitionByType l` takes a list `l` of proofs of comparisons. It sorts these proofs by
the type of the variables in the comparison, e.g. `(a : ℚ) < 1` and `(b : ℤ) > c` will be separated.
Returns a map from a type to a list of comparisons over that type. |
findLinarithContradiction (cfg : LinarithConfig) (g : MVarId) (ls : List (Expr × List Expr)) :
MetaM Expr :=
try
ls.firstM (fun ⟨α, L⟩ =>
withTraceNode `linarith (return m!"{exceptEmoji ·} running on type {α}") <|
proveFalseByLinarith cfg.transparency cfg.oracle cfg.discharger g L)
catch e => ... | def | Tactic | [
"Mathlib.Control.Basic",
"Mathlib.Tactic.Linarith.Verification",
"Mathlib.Tactic.Linarith.Preprocessing",
"Mathlib.Tactic.Linarith.Oracle.SimplexAlgorithm",
"Mathlib.Tactic.Ring.Basic"
] | Mathlib/Tactic/Linarith/Frontend.lean | findLinarithContradiction | Given a list `ls` of lists of proofs of comparisons, `findLinarithContradiction cfg ls` will try to
prove `False` by calling `linarith` on each list in succession. It will stop at the first proof of
`False`, and fail if no contradiction is found with any list. |
partial linarith (only_on : Bool) (hyps : List Expr) (cfg : LinarithConfig := {})
(g : MVarId) : MetaM Unit := g.withContext do
if (← whnfR (← instantiateMVars (← g.getType))).isEq then
trace[linarith] "target is an equality: splitting"
if let some [g₁, g₂] ← try? (g.apply (← mkConst' ``eq_of_not_lt_of_no... | def | Tactic | [
"Mathlib.Control.Basic",
"Mathlib.Tactic.Linarith.Verification",
"Mathlib.Tactic.Linarith.Preprocessing",
"Mathlib.Tactic.Linarith.Oracle.SimplexAlgorithm",
"Mathlib.Tactic.Ring.Basic"
] | Mathlib/Tactic/Linarith/Frontend.lean | linarith | Given a list `hyps` of proofs of comparisons, `runLinarith cfg hyps prefType`
preprocesses `hyps` according to the list of preprocessors in `cfg`.
This results in a list of branches (typically only one),
each of which must succeed in order to close the goal.
In each branch, we partition the list of hypotheses by type,... |
elabLinarithArg (tactic : Name) (t : Term) : TacticM Expr := Term.withoutErrToSorry do
let (e, mvars) ← elabTermWithHoles t none tactic
unless mvars.isEmpty do
throwErrorAt t "Argument passed to {tactic} has metavariables:{indentD e}"
return e | def | Tactic | [
"Mathlib.Control.Basic",
"Mathlib.Tactic.Linarith.Verification",
"Mathlib.Tactic.Linarith.Preprocessing",
"Mathlib.Tactic.Linarith.Oracle.SimplexAlgorithm",
"Mathlib.Tactic.Ring.Basic"
] | Mathlib/Tactic/Linarith/Frontend.lean | elabLinarithArg | Syntax for the arguments of `linarith`, after the optional `!`. -/
syntax linarithArgsRest := optConfig (&" only")? (" [" term,* "]")?
/--
`linarith` attempts to find a contradiction between hypotheses that are linear (in)equalities.
Equivalently, it can prove a linear inequality by assuming its negation and proving `... |
lt_irrefl {α : Type u} [Preorder α] {a : α} : ¬a < a := _root_.lt_irrefl a | theorem | Tactic | [
"Batteries.Tactic.Lint.Basic",
"Mathlib.Algebra.Order.Monoid.Unbundled.Basic",
"Mathlib.Algebra.Order.Ring.Defs",
"Mathlib.Algebra.Order.ZeroLEOne",
"Mathlib.Data.Nat.Cast.Order.Ring",
"Mathlib.Data.Int.Order.Basic",
"Mathlib.Data.Ineq"
] | Mathlib/Tactic/Linarith/Lemmas.lean | lt_irrefl | null |
eq_of_eq_of_eq {α} [Semiring α] {a b : α} (ha : a = 0) (hb : b = 0) : a + b = 0 := by
simp [*] | theorem | Tactic | [
"Batteries.Tactic.Lint.Basic",
"Mathlib.Algebra.Order.Monoid.Unbundled.Basic",
"Mathlib.Algebra.Order.Ring.Defs",
"Mathlib.Algebra.Order.ZeroLEOne",
"Mathlib.Data.Nat.Cast.Order.Ring",
"Mathlib.Data.Int.Order.Basic",
"Mathlib.Data.Ineq"
] | Mathlib/Tactic/Linarith/Lemmas.lean | eq_of_eq_of_eq | null |
zero_lt_one [IsStrictOrderedRing α] : (0:α) < 1 :=
_root_.zero_lt_one | theorem | Tactic | [
"Batteries.Tactic.Lint.Basic",
"Mathlib.Algebra.Order.Monoid.Unbundled.Basic",
"Mathlib.Algebra.Order.Ring.Defs",
"Mathlib.Algebra.Order.ZeroLEOne",
"Mathlib.Data.Nat.Cast.Order.Ring",
"Mathlib.Data.Int.Order.Basic",
"Mathlib.Data.Ineq"
] | Mathlib/Tactic/Linarith/Lemmas.lean | zero_lt_one | null |
le_of_eq_of_le {a b : α} (ha : a = 0) (hb : b ≤ 0) : a + b ≤ 0 := by
simp [*] | theorem | Tactic | [
"Batteries.Tactic.Lint.Basic",
"Mathlib.Algebra.Order.Monoid.Unbundled.Basic",
"Mathlib.Algebra.Order.Ring.Defs",
"Mathlib.Algebra.Order.ZeroLEOne",
"Mathlib.Data.Nat.Cast.Order.Ring",
"Mathlib.Data.Int.Order.Basic",
"Mathlib.Data.Ineq"
] | Mathlib/Tactic/Linarith/Lemmas.lean | le_of_eq_of_le | null |
lt_of_eq_of_lt {a b : α} (ha : a = 0) (hb : b < 0) : a + b < 0 := by
simp [*] | theorem | Tactic | [
"Batteries.Tactic.Lint.Basic",
"Mathlib.Algebra.Order.Monoid.Unbundled.Basic",
"Mathlib.Algebra.Order.Ring.Defs",
"Mathlib.Algebra.Order.ZeroLEOne",
"Mathlib.Data.Nat.Cast.Order.Ring",
"Mathlib.Data.Int.Order.Basic",
"Mathlib.Data.Ineq"
] | Mathlib/Tactic/Linarith/Lemmas.lean | lt_of_eq_of_lt | null |
le_of_le_of_eq {a b : α} (ha : a ≤ 0) (hb : b = 0) : a + b ≤ 0 := by
simp [*] | theorem | Tactic | [
"Batteries.Tactic.Lint.Basic",
"Mathlib.Algebra.Order.Monoid.Unbundled.Basic",
"Mathlib.Algebra.Order.Ring.Defs",
"Mathlib.Algebra.Order.ZeroLEOne",
"Mathlib.Data.Nat.Cast.Order.Ring",
"Mathlib.Data.Int.Order.Basic",
"Mathlib.Data.Ineq"
] | Mathlib/Tactic/Linarith/Lemmas.lean | le_of_le_of_eq | null |
lt_of_lt_of_eq {a b : α} (ha : a < 0) (hb : b = 0) : a + b < 0 := by
simp [*] | theorem | Tactic | [
"Batteries.Tactic.Lint.Basic",
"Mathlib.Algebra.Order.Monoid.Unbundled.Basic",
"Mathlib.Algebra.Order.Ring.Defs",
"Mathlib.Algebra.Order.ZeroLEOne",
"Mathlib.Data.Nat.Cast.Order.Ring",
"Mathlib.Data.Int.Order.Basic",
"Mathlib.Data.Ineq"
] | Mathlib/Tactic/Linarith/Lemmas.lean | lt_of_lt_of_eq | null |
add_nonpos [IsOrderedRing α] {a b : α} (ha : a ≤ 0) (hb : b ≤ 0) :
a + b ≤ 0 :=
_root_.add_nonpos ha hb | theorem | Tactic | [
"Batteries.Tactic.Lint.Basic",
"Mathlib.Algebra.Order.Monoid.Unbundled.Basic",
"Mathlib.Algebra.Order.Ring.Defs",
"Mathlib.Algebra.Order.ZeroLEOne",
"Mathlib.Data.Nat.Cast.Order.Ring",
"Mathlib.Data.Int.Order.Basic",
"Mathlib.Data.Ineq"
] | Mathlib/Tactic/Linarith/Lemmas.lean | add_nonpos | null |
add_lt_of_le_of_neg [IsStrictOrderedRing α] {a b c : α} (hbc : b ≤ c)
(ha : a < 0) : b + a < c :=
_root_.add_lt_of_le_of_neg hbc ha | theorem | Tactic | [
"Batteries.Tactic.Lint.Basic",
"Mathlib.Algebra.Order.Monoid.Unbundled.Basic",
"Mathlib.Algebra.Order.Ring.Defs",
"Mathlib.Algebra.Order.ZeroLEOne",
"Mathlib.Data.Nat.Cast.Order.Ring",
"Mathlib.Data.Int.Order.Basic",
"Mathlib.Data.Ineq"
] | Mathlib/Tactic/Linarith/Lemmas.lean | add_lt_of_le_of_neg | null |
add_lt_of_neg_of_le [IsStrictOrderedRing α] {a b c : α} (ha : a < 0)
(hbc : b ≤ c) : a + b < c :=
_root_.add_lt_of_neg_of_le ha hbc | theorem | Tactic | [
"Batteries.Tactic.Lint.Basic",
"Mathlib.Algebra.Order.Monoid.Unbundled.Basic",
"Mathlib.Algebra.Order.Ring.Defs",
"Mathlib.Algebra.Order.ZeroLEOne",
"Mathlib.Data.Nat.Cast.Order.Ring",
"Mathlib.Data.Int.Order.Basic",
"Mathlib.Data.Ineq"
] | Mathlib/Tactic/Linarith/Lemmas.lean | add_lt_of_neg_of_le | null |
add_neg [IsStrictOrderedRing α] {a b : α} (ha : a < 0)
(hb : b < 0) : a + b < 0 :=
_root_.add_neg ha hb
variable (α) in | theorem | Tactic | [
"Batteries.Tactic.Lint.Basic",
"Mathlib.Algebra.Order.Monoid.Unbundled.Basic",
"Mathlib.Algebra.Order.Ring.Defs",
"Mathlib.Algebra.Order.ZeroLEOne",
"Mathlib.Data.Nat.Cast.Order.Ring",
"Mathlib.Data.Int.Order.Basic",
"Mathlib.Data.Ineq"
] | Mathlib/Tactic/Linarith/Lemmas.lean | add_neg | null |
natCast_nonneg [IsOrderedRing α] (n : ℕ) : (0 : α) ≤ n := Nat.cast_nonneg n
@[nolint unusedArguments] | lemma | Tactic | [
"Batteries.Tactic.Lint.Basic",
"Mathlib.Algebra.Order.Monoid.Unbundled.Basic",
"Mathlib.Algebra.Order.Ring.Defs",
"Mathlib.Algebra.Order.ZeroLEOne",
"Mathlib.Data.Nat.Cast.Order.Ring",
"Mathlib.Data.Int.Order.Basic",
"Mathlib.Data.Ineq"
] | Mathlib/Tactic/Linarith/Lemmas.lean | natCast_nonneg | null |
mul_eq [IsOrderedRing α] {a b : α} (ha : a = 0) (_ : 0 < b) : b * a = 0 := by
simp [*] | theorem | Tactic | [
"Batteries.Tactic.Lint.Basic",
"Mathlib.Algebra.Order.Monoid.Unbundled.Basic",
"Mathlib.Algebra.Order.Ring.Defs",
"Mathlib.Algebra.Order.ZeroLEOne",
"Mathlib.Data.Nat.Cast.Order.Ring",
"Mathlib.Data.Int.Order.Basic",
"Mathlib.Data.Ineq"
] | Mathlib/Tactic/Linarith/Lemmas.lean | mul_eq | null |
mul_neg [IsStrictOrderedRing α] {a b : α} (ha : a < 0) (hb : 0 < b) : b * a < 0 :=
have : (-b)*a > 0 := mul_pos_of_neg_of_neg (neg_neg_of_pos hb) ha
neg_of_neg_pos (by simpa) | theorem | Tactic | [
"Batteries.Tactic.Lint.Basic",
"Mathlib.Algebra.Order.Monoid.Unbundled.Basic",
"Mathlib.Algebra.Order.Ring.Defs",
"Mathlib.Algebra.Order.ZeroLEOne",
"Mathlib.Data.Nat.Cast.Order.Ring",
"Mathlib.Data.Int.Order.Basic",
"Mathlib.Data.Ineq"
] | Mathlib/Tactic/Linarith/Lemmas.lean | mul_neg | null |
mul_nonpos [IsOrderedRing α] {a b : α} (ha : a ≤ 0) (hb : 0 < b) : b * a ≤ 0 :=
have : (-b)*a ≥ 0 := mul_nonneg_of_nonpos_of_nonpos (le_of_lt (neg_neg_of_pos hb)) ha
by simpa | theorem | Tactic | [
"Batteries.Tactic.Lint.Basic",
"Mathlib.Algebra.Order.Monoid.Unbundled.Basic",
"Mathlib.Algebra.Order.Ring.Defs",
"Mathlib.Algebra.Order.ZeroLEOne",
"Mathlib.Data.Nat.Cast.Order.Ring",
"Mathlib.Data.Int.Order.Basic",
"Mathlib.Data.Ineq"
] | Mathlib/Tactic/Linarith/Lemmas.lean | mul_nonpos | null |
sub_nonpos_of_le [IsOrderedRing α] {a b : α} : a ≤ b → a - b ≤ 0 :=
_root_.sub_nonpos_of_le | theorem | Tactic | [
"Batteries.Tactic.Lint.Basic",
"Mathlib.Algebra.Order.Monoid.Unbundled.Basic",
"Mathlib.Algebra.Order.Ring.Defs",
"Mathlib.Algebra.Order.ZeroLEOne",
"Mathlib.Data.Nat.Cast.Order.Ring",
"Mathlib.Data.Int.Order.Basic",
"Mathlib.Data.Ineq"
] | Mathlib/Tactic/Linarith/Lemmas.lean | sub_nonpos_of_le | null |
sub_neg_of_lt [IsOrderedRing α] {a b : α} : a < b → a - b < 0 :=
_root_.sub_neg_of_lt | theorem | Tactic | [
"Batteries.Tactic.Lint.Basic",
"Mathlib.Algebra.Order.Monoid.Unbundled.Basic",
"Mathlib.Algebra.Order.Ring.Defs",
"Mathlib.Algebra.Order.ZeroLEOne",
"Mathlib.Data.Nat.Cast.Order.Ring",
"Mathlib.Data.Int.Order.Basic",
"Mathlib.Data.Ineq"
] | Mathlib/Tactic/Linarith/Lemmas.lean | sub_neg_of_lt | null |
_root_.Mathlib.Ineq.toConstMulName : Ineq → Lean.Name
| .lt => ``mul_neg
| .le => ``mul_nonpos
| .eq => ``mul_eq | def | Tactic | [
"Batteries.Tactic.Lint.Basic",
"Mathlib.Algebra.Order.Monoid.Unbundled.Basic",
"Mathlib.Algebra.Order.Ring.Defs",
"Mathlib.Algebra.Order.ZeroLEOne",
"Mathlib.Data.Nat.Cast.Order.Ring",
"Mathlib.Data.Int.Order.Basic",
"Mathlib.Data.Ineq"
] | Mathlib/Tactic/Linarith/Lemmas.lean | _root_.Mathlib.Ineq.toConstMulName | Finds the name of a multiplicative lemma corresponding to an inequality strength. |
eq_of_not_lt_of_not_gt {α} [LinearOrder α] (a b : α) (h1 : ¬ a < b) (h2 : ¬ b < a) : a = b :=
le_antisymm (le_of_not_gt h2) (le_of_not_gt h1)
@[nolint unusedArguments] | lemma | Tactic | [
"Batteries.Tactic.Lint.Basic",
"Mathlib.Algebra.Order.Monoid.Unbundled.Basic",
"Mathlib.Algebra.Order.Ring.Defs",
"Mathlib.Algebra.Order.ZeroLEOne",
"Mathlib.Data.Nat.Cast.Order.Ring",
"Mathlib.Data.Int.Order.Basic",
"Mathlib.Data.Ineq"
] | Mathlib/Tactic/Linarith/Lemmas.lean | eq_of_not_lt_of_not_gt | null |
mul_zero_eq {α} {R : α → α → Prop} [Semiring α] {a b : α} (_ : R a 0) (h : b = 0) :
a * b = 0 := by
simp [h]
@[nolint unusedArguments] | lemma | Tactic | [
"Batteries.Tactic.Lint.Basic",
"Mathlib.Algebra.Order.Monoid.Unbundled.Basic",
"Mathlib.Algebra.Order.Ring.Defs",
"Mathlib.Algebra.Order.ZeroLEOne",
"Mathlib.Data.Nat.Cast.Order.Ring",
"Mathlib.Data.Int.Order.Basic",
"Mathlib.Data.Ineq"
] | Mathlib/Tactic/Linarith/Lemmas.lean | mul_zero_eq | null |
zero_mul_eq {α} {R : α → α → Prop} [Semiring α] {a b : α} (h : a = 0) (_ : R b 0) :
a * b = 0 := by
simp [h] | lemma | Tactic | [
"Batteries.Tactic.Lint.Basic",
"Mathlib.Algebra.Order.Monoid.Unbundled.Basic",
"Mathlib.Algebra.Order.Ring.Defs",
"Mathlib.Algebra.Order.ZeroLEOne",
"Mathlib.Data.Nat.Cast.Order.Ring",
"Mathlib.Data.Int.Order.Basic",
"Mathlib.Data.Ineq"
] | Mathlib/Tactic/Linarith/Lemmas.lean | zero_mul_eq | null |
@[deprecated GT.gt.lt (since := "2025-06-16")]
lt_zero_of_zero_gt {α : Type*} [Zero α] [LT α] {a : α} (h : 0 > a) : a < 0 := h
@[deprecated GE.ge.le (since := "2025-06-16")] | theorem | Tactic | [
"Batteries.Tactic.Lint.Basic",
"Mathlib.Algebra.Order.Monoid.Unbundled.Basic",
"Mathlib.Algebra.Order.Ring.Defs",
"Mathlib.Algebra.Order.ZeroLEOne",
"Mathlib.Data.Nat.Cast.Order.Ring",
"Mathlib.Data.Int.Order.Basic",
"Mathlib.Data.Ineq"
] | Mathlib/Tactic/Linarith/Lemmas.lean | lt_zero_of_zero_gt | null |
le_zero_of_zero_ge {α : Type*} [Zero α] [LE α] {a : α} (h : 0 ≥ a) : a ≤ 0 := h | theorem | Tactic | [
"Batteries.Tactic.Lint.Basic",
"Mathlib.Algebra.Order.Monoid.Unbundled.Basic",
"Mathlib.Algebra.Order.Ring.Defs",
"Mathlib.Algebra.Order.ZeroLEOne",
"Mathlib.Data.Nat.Cast.Order.Ring",
"Mathlib.Data.Int.Order.Basic",
"Mathlib.Data.Ineq"
] | Mathlib/Tactic/Linarith/Lemmas.lean | le_zero_of_zero_ge | null |
beq {α β : Type*} [BEq β] {c : α → α → Ordering} (m₁ m₂ : TreeMap α β c) : Bool :=
m₁.size == m₂.size && Id.run do
for (k, v) in m₁ do
if let some v' := m₂[k]? then
if v != v' then
return false
else
return false
return true | def | Tactic | [
"Mathlib.Tactic.Linarith.Datatypes"
] | Mathlib/Tactic/Linarith/Parsing.lean | beq | Returns true if the two maps have the same size and the same keys and values
(with keys compared using the ordering, and values compared using `BEq`). |
List.findDefeq {v : Type} (red : TransparencyMode) (m : List (Expr × v)) (e : Expr) :
MetaM v := do
if let some (_, n) ← m.findM? fun ⟨e', _⟩ => withTransparency red (isDefEq e e') then
return n
else
failure | def | Tactic | [
"Mathlib.Tactic.Linarith.Datatypes"
] | Mathlib/Tactic/Linarith/Parsing.lean | List.findDefeq | `findDefeq red m e` looks for a key in `m` that is defeq to `e` (up to transparency `red`),
and returns the value associated with this key if it exists.
Otherwise, it fails. |
Map (α β) [Ord α] := TreeMap α β Ord.compare
/-! ### Parsing datatypes -/ | abbrev | Tactic | [
"Mathlib.Tactic.Linarith.Datatypes"
] | Mathlib/Tactic/Linarith/Parsing.lean | Map | We introduce a local instance allowing addition of `TreeMap`s,
removing any keys with value zero.
We don't need to prove anything about this addition, as it is only used in meta code.
-/
local instance {α β : Type*} {c : α → α → Ordering} [Add β] [Zero β] [DecidableEq β] :
Add (TreeMap α β c) where
add := fun f g... |
Monom : Type := Map ℕ ℕ | abbrev | Tactic | [
"Mathlib.Tactic.Linarith.Datatypes"
] | Mathlib/Tactic/Linarith/Parsing.lean | Monom | Variables (represented by natural numbers) map to their power. |
Monom.one : Monom := TreeMap.empty | def | Tactic | [
"Mathlib.Tactic.Linarith.Datatypes"
] | Mathlib/Tactic/Linarith/Parsing.lean | Monom.one | `1` is represented by the empty monomial, the product of no variables. |
Monom.lt : Monom → Monom → Bool :=
fun a b =>
((a.keys : List ℕ) < b.keys) ||
(((a.keys : List ℕ) = b.keys) && ((a.values : List ℕ) < b.values)) | def | Tactic | [
"Mathlib.Tactic.Linarith.Datatypes"
] | Mathlib/Tactic/Linarith/Parsing.lean | Monom.lt | Compare monomials by first comparing their keys and then their powers. |
Sum : Type := Map Monom ℤ | abbrev | Tactic | [
"Mathlib.Tactic.Linarith.Datatypes"
] | Mathlib/Tactic/Linarith/Parsing.lean | Sum | Linear combinations of monomials are represented by mapping monomials to coefficients. |
Sum.one : Sum := TreeMap.empty.insert Monom.one 1 | def | Tactic | [
"Mathlib.Tactic.Linarith.Datatypes"
] | Mathlib/Tactic/Linarith/Parsing.lean | Sum.one | `1` is represented as the singleton sum of the monomial `Monom.one` with coefficient 1. |
Sum.scaleByMonom (s : Sum) (m : Monom) : Sum :=
s.foldr (fun m' coeff sm => sm.insert (m + m') coeff) TreeMap.empty | def | Tactic | [
"Mathlib.Tactic.Linarith.Datatypes"
] | Mathlib/Tactic/Linarith/Parsing.lean | Sum.scaleByMonom | `Sum.scaleByMonom s m` multiplies every monomial in `s` by `m`. |
Sum.mul (s1 s2 : Sum) : Sum :=
s1.foldr (fun mn coeff sm => sm + ((s2.scaleByMonom mn).map (fun _ v => v * coeff)))
TreeMap.empty | def | Tactic | [
"Mathlib.Tactic.Linarith.Datatypes"
] | Mathlib/Tactic/Linarith/Parsing.lean | Sum.mul | `sum.mul s1 s2` distributes the multiplication of two sums. |
partial Sum.pow (s : Sum) : ℕ → Sum
| 0 => Sum.one
| 1 => s
| n =>
let m := n >>> 1
let a := s.pow m
if n &&& 1 = 0 then
a.mul a
else
a.mul a |>.mul s | def | Tactic | [
"Mathlib.Tactic.Linarith.Datatypes"
] | Mathlib/Tactic/Linarith/Parsing.lean | Sum.pow | The `n`th power of `s : Sum` is the `n`-fold product of `s`, with `s.pow 0 = Sum.one`. |
SumOfMonom (m : Monom) : Sum :=
TreeMap.empty.insert m 1 | def | Tactic | [
"Mathlib.Tactic.Linarith.Datatypes"
] | Mathlib/Tactic/Linarith/Parsing.lean | SumOfMonom | `SumOfMonom m` lifts `m` to a sum with coefficient `1`. |
one : Monom := TreeMap.empty | def | Tactic | [
"Mathlib.Tactic.Linarith.Datatypes"
] | Mathlib/Tactic/Linarith/Parsing.lean | one | The unit monomial `one` is represented by the empty TreeMap. |
scalar (z : ℤ) : Sum :=
TreeMap.empty.insert one z | def | Tactic | [
"Mathlib.Tactic.Linarith.Datatypes"
] | Mathlib/Tactic/Linarith/Parsing.lean | scalar | A scalar `z` is represented by a `Sum` with coefficient `z` and monomial `one` |
var (n : ℕ) : Sum :=
TreeMap.empty.insert (TreeMap.empty.insert n 1) 1
/-! ### Parsing algorithms -/
open Lean Elab Tactic Meta | def | Tactic | [
"Mathlib.Tactic.Linarith.Datatypes"
] | Mathlib/Tactic/Linarith/Parsing.lean | var | A single variable `n` is represented by a sum with coefficient `1` and monomial `n`. |
linearFormOfAtom (red : TransparencyMode) (m : ExprMap) (e : Expr) : MetaM (ExprMap × Sum) := do
try
let k ← m.findDefeq red e
return (m, var k)
catch _ =>
let n := m.length + 1
return ((e, n)::m, var n) | def | Tactic | [
"Mathlib.Tactic.Linarith.Datatypes"
] | Mathlib/Tactic/Linarith/Parsing.lean | linearFormOfAtom | `ExprMap` is used to record atomic expressions which have been seen while processing inequality
expressions.
-/
-- The natural number is just the index in the list,
-- and we could reimplement to just use `List Expr` if desired.
abbrev ExprMap := List (Expr × ℕ)
/--
`linearFormOfAtom red map e` is the atomic case for ... |
partial linearFormOfExpr (red : TransparencyMode) (m : ExprMap) (e : Expr) :
MetaM (ExprMap × Sum) := do
let e ← whnfR e
match e.numeral? with
| some 0 => return ⟨m, TreeMap.empty⟩
| some (n + 1) => return ⟨m, scalar (n + 1)⟩
| none =>
match e.getAppFnArgs with
| (``HMul.hMul, #[_, _, _, _, e1, e2]) =... | def | Tactic | [
"Mathlib.Tactic.Linarith.Datatypes"
] | Mathlib/Tactic/Linarith/Parsing.lean | linearFormOfExpr | `linearFormOfExpr red map e` computes the linear form of `e`.
`map` is a lookup map from atomic expressions to variable numbers.
If a new atomic expression is encountered, it is added to the map with a new number.
It matches atomic expressions up to reducibility given by `red`.
Because it matches up to definitional e... |
elimMonom (s : Sum) (m : Map Monom ℕ) : Map Monom ℕ × Map ℕ ℤ :=
s.foldr (fun mn coeff ⟨map, out⟩ ↦
match map[mn]? with
| some n => ⟨map, out.insert n coeff⟩
| none =>
let n := map.size
⟨map.insert mn n, out.insert n coeff⟩)
(m, TreeMap.empty) | def | Tactic | [
"Mathlib.Tactic.Linarith.Datatypes"
] | Mathlib/Tactic/Linarith/Parsing.lean | elimMonom | `elimMonom s map` eliminates the monomial level of the `Sum` `s`.
`map` is a lookup map from monomials to variable numbers.
The output `TreeMap ℕ ℤ` has the same structure as `s : Sum`,
but each monomial key is replaced with its index according to `map`.
If any new monomials are encountered, they are assigned variable... |
toComp (red : TransparencyMode) (e : Expr) (e_map : ExprMap) (monom_map : Map Monom ℕ) :
MetaM (Comp × ExprMap × Map Monom ℕ) := do
let (iq, e) ← parseCompAndExpr e
let (m', comp') ← linearFormOfExpr red e_map e
let ⟨nm, mm'⟩ := elimMonom comp' monom_map
return ⟨⟨iq, mm'.toList.reverse⟩, m', nm⟩ | def | Tactic | [
"Mathlib.Tactic.Linarith.Datatypes"
] | Mathlib/Tactic/Linarith/Parsing.lean | toComp | `toComp red e e_map monom_map` converts an expression of the form `t < 0`, `t ≤ 0`, or `t = 0`
into a `comp` object.
`e_map` maps atomic expressions to indices; `monom_map` maps monomials to indices.
Both of these are updated during processing and returned. |
toCompFold (red : TransparencyMode) : ExprMap → List Expr → Map Monom ℕ →
MetaM (List Comp × ExprMap × Map Monom ℕ)
| m, [], mm => return ([], m, mm)
| m, (h::t), mm => do
let (c, m', mm') ← toComp red h m mm
let (l, mp, mm') ← toCompFold red m' t mm'
return (c::l, mp, mm') | def | Tactic | [
"Mathlib.Tactic.Linarith.Datatypes"
] | Mathlib/Tactic/Linarith/Parsing.lean | toCompFold | `toCompFold red e_map exprs monom_map` folds `toComp` over `exprs`,
updating `e_map` and `monom_map` as it goes. |
linearFormsAndMaxVar (red : TransparencyMode) (pfs : List Expr) :
MetaM (List Comp × ℕ) := do
let pftps ← (pfs.mapM inferType)
let (l, _, map) ← toCompFold red [] pftps TreeMap.empty
trace[linarith.detail] "monomial map: {map.toList.map fun ⟨k,v⟩ => (k.toList, v)}"
return (l, map.size - 1) | def | Tactic | [
"Mathlib.Tactic.Linarith.Datatypes"
] | Mathlib/Tactic/Linarith/Parsing.lean | linearFormsAndMaxVar | `linearFormsAndMaxVar red pfs` is the main interface for computing the linear forms of a list
of expressions. Given a list `pfs` of proofs of comparisons, it produces a list `c` of `Comp`s of
the same length, such that `c[i]` represents the linear form of the type of `pfs[i]`.
It also returns the largest variable inde... |
partial splitConjunctions : Preprocessor where
description := "split conjunctions"
transform := aux
where
/-- Implementation of the `splitConjunctions` preprocessor. -/
aux (proof : Expr) : MetaM (List Expr) := do
match (← instantiateMVars (← inferType proof)).getAppFnArgs with
| (``And, #[_, _]) =>
... | def | Tactic | [
"Mathlib.Control.Basic",
"Mathlib.Lean.Meta.Tactic.Rewrite",
"Mathlib.Tactic.CancelDenoms.Core",
"Mathlib.Tactic.Linarith.Datatypes",
"Mathlib.Tactic.Zify",
"Mathlib.Util.AtomM"
] | Mathlib/Tactic/Linarith/Preprocessing.lean | splitConjunctions | Processor that recursively replaces `P ∧ Q` hypotheses with the pair `P` and `Q`. |
partial filterComparisons : Preprocessor where
description := "filter terms that are not proofs of comparisons"
transform h := do
let tp ← instantiateMVars (← inferType h)
try
let (b, rel, _) ← tp.ineqOrNotIneq?
if b || rel != Ineq.eq then pure [h] else pure []
catch _ => pure [] | def | Tactic | [
"Mathlib.Control.Basic",
"Mathlib.Lean.Meta.Tactic.Rewrite",
"Mathlib.Tactic.CancelDenoms.Core",
"Mathlib.Tactic.Linarith.Datatypes",
"Mathlib.Tactic.Zify",
"Mathlib.Util.AtomM"
] | Mathlib/Tactic/Linarith/Preprocessing.lean | filterComparisons | Removes any expressions that are not proofs of inequalities, equalities, or negations thereof. |
flipNegatedComparison (prf : Expr) (e : Expr) : MetaM (Option Expr) :=
match e.getAppFnArgs with
| (``LE.le, #[_, _, _, _]) => try? <| mkAppM ``lt_of_not_ge #[prf]
| (``LT.lt, #[_, _, _, _]) => try? <| mkAppM ``le_of_not_gt #[prf]
| _ => throwError "Not a comparison (flipNegatedComparison): {e}" | def | Tactic | [
"Mathlib.Control.Basic",
"Mathlib.Lean.Meta.Tactic.Rewrite",
"Mathlib.Tactic.CancelDenoms.Core",
"Mathlib.Tactic.Linarith.Datatypes",
"Mathlib.Tactic.Zify",
"Mathlib.Util.AtomM"
] | Mathlib/Tactic/Linarith/Preprocessing.lean | flipNegatedComparison | If `prf` is a proof of `¬ e`, where `e` is a comparison,
`flipNegatedComparison prf e` flips the comparison in `e` and returns a proof.
For example, if `prf : ¬ a < b`, ``flipNegatedComparison prf q(a < b)`` returns a proof of `a ≥ b`. |
removeNegations : Preprocessor where
description := "replace negations of comparisons"
transform h := do
let t : Q(Prop) ← whnfR (← inferType h)
match t with
| ~q(¬ $p) =>
match ← flipNegatedComparison h (← whnfR p) with
| some h' =>
trace[linarith] "removing negation in {h}"
... | def | Tactic | [
"Mathlib.Control.Basic",
"Mathlib.Lean.Meta.Tactic.Rewrite",
"Mathlib.Tactic.CancelDenoms.Core",
"Mathlib.Tactic.Linarith.Datatypes",
"Mathlib.Tactic.Zify",
"Mathlib.Util.AtomM"
] | Mathlib/Tactic/Linarith/Preprocessing.lean | removeNegations | Replaces proofs of negations of comparisons with proofs of the reversed comparisons.
For example, a proof of `¬ a < b` will become a proof of `a ≥ b`. |
partial isNatProp (e : Expr) : MetaM Bool := succeeds <| do
let (_, _, .const ``Nat [], _, _) ← e.ineqOrNotIneq? | failure | def | Tactic | [
"Mathlib.Control.Basic",
"Mathlib.Lean.Meta.Tactic.Rewrite",
"Mathlib.Tactic.CancelDenoms.Core",
"Mathlib.Tactic.Linarith.Datatypes",
"Mathlib.Tactic.Zify",
"Mathlib.Util.AtomM"
] | Mathlib/Tactic/Linarith/Preprocessing.lean | isNatProp | `isNatProp tp` is true iff `tp` is an inequality or equality between natural numbers
or the negation thereof. |
isNatCoe (e : Expr) : Option (Expr × Expr) :=
match e.getAppFnArgs with
| (``Nat.cast, #[target, _, n]) => some ⟨n, target⟩
| _ => none | def | Tactic | [
"Mathlib.Control.Basic",
"Mathlib.Lean.Meta.Tactic.Rewrite",
"Mathlib.Tactic.CancelDenoms.Core",
"Mathlib.Tactic.Linarith.Datatypes",
"Mathlib.Tactic.Zify",
"Mathlib.Util.AtomM"
] | Mathlib/Tactic/Linarith/Preprocessing.lean | isNatCoe | If `e` is of the form `((n : ℕ) : C)`, `isNatCoe e` returns `⟨n, C⟩`. |
partial getNatComparisons (e : Expr) : List (Expr × Expr) :=
match isNatCoe e with
| some x => [x]
| none => match e.getAppFnArgs with
| (``HAdd.hAdd, #[_, _, _, _, a, b]) => getNatComparisons a ++ getNatComparisons b
| (``HMul.hMul, #[_, _, _, _, a, b]) => getNatComparisons a ++ getNatComparisons b
|... | def | Tactic | [
"Mathlib.Control.Basic",
"Mathlib.Lean.Meta.Tactic.Rewrite",
"Mathlib.Tactic.CancelDenoms.Core",
"Mathlib.Tactic.Linarith.Datatypes",
"Mathlib.Tactic.Zify",
"Mathlib.Util.AtomM"
] | Mathlib/Tactic/Linarith/Preprocessing.lean | getNatComparisons | `getNatComparisons e` returns a list of all subexpressions of `e` of the form `((t : ℕ) : C)`. |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.