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
if let arity + 1 := hrelType.getForallArity then
pure (some arity)
else
throwTacticEx `apply_rw goal m!"invalid implication {hrelType}"
else
pure none
let (newMVars, binderInfos, hrelType) ←
withReducible <| forallMetaTelescopeReducing hrelType maxMVars?
if (hrelType.isAppOfArity ``Iff 2 || hrelType.isAppOfArity ``Eq 3) && config.useRewrite then
let { eNew, eqProof, mvarIds } ← goal.rewrite e hrel symm config.toConfig
let mp := if forwardImp then ``Eq.mp else ``Eq.mpr
let impProof ← mkAppOptM mp #[e, eNew, eqProof]
return { eNew, impProof, mvarIds }
let hrelIn := hrel
let hrel := mkAppN hrel newMVars
let some (_, lhs, rhs) := GCongr.getRel hrelType |
throwTacticEx `grewrite goal m!"{hrelType} is not a relation"
let (lhs, rhs) := if symm then (rhs, lhs) else (lhs, rhs)
if lhs.getAppFn.isMVar then
throwTacticEx `grewrite goal
m!"pattern is a metavariable{indentExpr lhs}\nfrom relation{indentExpr hrelType}"
let e ← instantiateMVars e
let eAbst ←
withConfig (fun oldConfig => { config, oldConfig with }) <| kabstract e lhs config.occs
unless eAbst.hasLooseBVars do
throwTacticEx `grewrite goal
m!"did not find instance of the pattern in the target expression{indentExpr lhs}"
let eNew := eAbst.instantiate1 rhs
let eNew ← instantiateMVars eNew
try
check eNew
catch ex =>
throwTacticEx `grewrite goal m!"\
rewritten expression is not type correct:{indentD eNew}\nError: {ex.toMessageData}\
\n\n\
Possible solutions: use grewrite's 'occs' configuration option to limit which occurrences \
are rewritten, or specify what the rewritten expression should be and use 'gcongr'."
let eNew ← if rhs.hasBinderNameHint then eNew.resolveBinderNameHint else pure eNew
let hole ← mkFreshExprMVar default
let template := eAbst.instantiate1 hole
let mkImp (e₁ e₂ : Expr) : Expr := .forallE `_a e₁ e₂ .default
let imp := if forwardImp then mkImp e eNew else mkImp eNew e
let gcongrGoal ← mkFreshExprMVar imp
let (_, _, sideGoals) ← gcongrGoal.mvarId!.gcongr template [] (grewriteHole := hole.mvarId!)
... | 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.MVarId.rewrite` as much as possible. |
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 ← goal.grewrite target e (forwardImp := false) (symm := symm) (config := config)
let mvarNew ← mkFreshExprSyntheticOpaqueMVar r.eNew (← goal.getTag)
goal.assign (mkApp r.impProof mvarNew)
replaceMainGoal (mvarNew.mvarId! :: r.mvarIds) | 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 ← fvarId.getDecl
goal.grewrite localDecl.type e (forwardImp := true) (symm := symm) (config := config)
let proof := .app (r.impProof) (.fvar fvarId)
let { mvarId, .. } ← goal.replace fvarId proof r.eNew
replaceMainGoal (mvarId :: r.mvarIds) | 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)
(throwTacticEx `grewrite · "did not find instance of the pattern in the current goal") | 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 the first argument.
This is not enforced by the type but the operations here preserve it. |
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] Mathlib.Tactic.Linarith.instReprComp.repr | 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 -- is it < 0, ≤ 0, or = 0? |
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 > 1 then
trace[linarith] "Preprocessing: {pp.name} has branched, with branches:"
for ⟨goal, hyps⟩ in branches do
trace[linarith] (← goal.withContext <| linarithGetProofsMessage hyps)
return branches | 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 sum `∑ (coeff i) * hyps[i]`. -/
produceCertificate (hyps : List Comp) (max_var : Nat) : MetaM (Std.HashMap Nat Nat)
/-! | 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 also available (though the Fourier-Motzkin oracle has some bugs). |
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.gt #[(← tp.ofNat c), (← tp.ofNat 0)]
let ex ← synthesizeUsingTactic' cpos (← `(tactic| norm_num))
let e' ← mkAppM iq.toConstMulName #[h, ex]
return (iq, e') | 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 identifying atomic expressions in comparisons. -/
transparency : TransparencyMode := .reducible
/-- Split conjunctions in hypotheses. -/
splitHypotheses : Bool := true
/-- Split `≠` in hypotheses, by branching in cases `<` and `>`. -/
splitNe : Bool := false
/-- Override the list of preprocessors. -/
preprocessors : List GlobalBranchingPreprocessor := defaultPreprocessors
/-- Specify an oracle for identifying candidate contradictions.
`.simplexAlgorithmSparse`, `.simplexAlgorithmSparse`, and `.fourierMotzkin` are available. -/
oracle : CertificateOracle := .simplexAlgorithmSparse | 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` above) and the
newly introduced local constant.
Otherwise returns `none`. |
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 => throwError "linarith failed to find a contradiction\n{g}\n{e.toMessageData}" | 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_not_gt)) then
withTraceNode `linarith (return m!"{exceptEmoji ·} proving ≥") <| linarith only_on hyps cfg g₁
withTraceNode `linarith (return m!"{exceptEmoji ·} proving ≤") <| linarith only_on hyps cfg g₂
return
/- If we are proving a comparison goal (and not just `False`), we consider the type of the
elements in the comparison to be the "preferred" type. That is, if we find comparison
hypotheses in multiple types, we will run `linarith` on the goal type first.
In this case we also receive a new variable from moving the goal to a hypothesis.
Otherwise, there is no preferred type and no new variable; we simply change the goal to `False`.
-/
let (g, target_type, new_var) ← match ← applyContrLemma g with
| (none, g) =>
if cfg.exfalso then
trace[linarith] "using exfalso"
pure (← g.exfalso, none, none)
else
pure (g, none, none)
| (some (t, v), g) => pure (g, some t, some v)
g.withContext do
let hyps ← (if only_on then return new_var.toList ++ hyps
else return (← getLocalHyps).toList ++ hyps)
linarithTraceProofs "linarith is running on the following hypotheses:" hyps
runLinarith cfg target_type g hyps | 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, and run `linarith` on each class
in the partition; one of these must succeed in order for `linarith` to succeed on this branch.
If `prefType` is given, it will first use the class of proofs of comparisons over that type.
-/
-- If it succeeds, the passed metavariable should have been assigned.
def runLinarith (cfg : LinarithConfig) (prefType : Option Expr) (g : MVarId)
(hyps : List Expr) : MetaM Unit := do
let singleProcess (g : MVarId) (hyps : List Expr) : MetaM Expr := g.withContext do
linarithTraceProofs s!"after preprocessing, linarith has {hyps.length} facts:" hyps
let mut hyp_set ← partitionByType hyps
trace[linarith] "hypotheses appear in {hyp_set.size} different types"
-- If we have a preferred type, strip it from `hyp_set` and prepare a handler with a custom
-- trace message
let pref : MetaM _ ← do
if let some t := prefType then
let (i, vs) ← hyp_set.find t
hyp_set := hyp_set.eraseIdxIfInBounds i
pure <|
withTraceNode `linarith (return m!"{exceptEmoji ·} running on preferred type {t}") <|
proveFalseByLinarith cfg.transparency cfg.oracle cfg.discharger g vs
else
pure failure
pref <|> findLinarithContradiction cfg g hyp_set.toList
let mut preprocessors := cfg.preprocessors
if cfg.splitNe then
preprocessors := Linarith.removeNe :: preprocessors
if cfg.splitHypotheses then
preprocessors := Linarith.splitConjunctions.globalize.branching :: preprocessors
let branches ← preprocess preprocessors g hyps
for (g, es) in branches do
let r ← singleProcess g es
g.assign r
-- Verify that we closed the goal. Failure here should only result from a bad `Preprocessor`.
(Expr.mvar g).ensureHasNoMVars
-- /--
-- `filterHyps restr_type hyps` takes a list of proofs of comparisons `hyps`, and filters it
-- to only those that are comparisons over the type `restr_type`.
-- -/
-- def filterHyps (restr_type : Expr) (hyps : List Expr) : MetaM (List Expr) :=
-- hyps.filterM (fun h => do
-- let ht ← inferType h
-- match getContrLemma ht with
-- | some (_, htype) => isDefEq htype restr_type
-- | none => return false)
/--
`linarith only_on hyps cfg` tries to close the goal using linear arithmetic. It fails
if it does not succeed at doing this.
* `hyps` is a list of proofs of comparisons to include in the search.
* If `only_on` is true, the search will be restricted to `hyps`. Otherwise it will use all
comparisons in the local context.
* If `cfg.transparency := semireducible`,
it will unfold semireducible definitions when trying to match atomic expressions. |
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 `False`.
In theory, `linarith` should prove any goal that is true in the theory of linear arithmetic over
the rationals. While there is some special handling for non-dense orders like `Nat` and `Int`,
this tactic is not complete for these theories and will not prove every true goal. It will solve
goals over arbitrary types that instantiate `CommRing`, `LinearOrder` and `IsStrictOrderedRing`.
An example:
```lean
example (x y z : ℚ) (h1 : 2*x < 3*y) (h2 : -4*x + 2*z < 0)
(h3 : 12*y - 4* z < 0) : False := by
linarith
```
`linarith` will use all appropriate hypotheses and the negation of the goal, if applicable.
Disequality hypotheses require case splitting and are not normally considered
(see the `splitNe` option below).
`linarith [t1, t2, t3]` will additionally use proof terms `t1, t2, t3`.
`linarith only [h1, h2, h3, t1, t2, t3]` will use only the goal (if relevant), local hypotheses
`h1`, `h2`, `h3`, and proofs `t1`, `t2`, `t3`. It will ignore the rest of the local context.
`linarith!` will use a stronger reducibility setting to try to identify atoms. For example,
```lean
example (x : ℚ) : id x ≥ x := by
linarith
```
will fail, because `linarith` will not identify `x` and `id x`. `linarith!` will.
This can sometimes be expensive.
`linarith (config := { .. })` takes a config object with five
optional arguments:
* `discharger` specifies a tactic to be used for reducing an algebraic equation in the
proof stage. The default is `ring`. Other options include `simp` for basic
problems.
* `transparency` controls how hard `linarith` will try to match atoms to each other. By default
it will only unfold `reducible` definitions.
* If `splitHypotheses` is true, `linarith` will split conjunctions in the context into separate
hypotheses.
* If `splitNe` is `true`, `linarith` will case split on disequality hypotheses.
For a given `x ≠ y` hypothesis, `linarith` is run with both `x < y` and `x > y`,
and so this runs linarith exponentially many times with respect to the number of
disequality hypotheses. (`false` by default.)
* If `exfalso` is `false`, `linarith` will fail when the goal is neither an inequality nor `False`.
(`true` by default.)
* `restrict_type` (not yet implemented in mathlib4)
will only use hypotheses that are inequalities over `tp`. This is useful
if you have e.g. both integer- and rational-valued inequalities in the local context, which can
sometimes confuse the tactic.
A variant, `nlinarith`, does some basic preprocessing to handle some nonlinear goals.
The option `set_option trace.linarith true` will trace certain intermediate stages of the `linarith`
routine.
-/
syntax (name := linarith) "linarith" "!"? linarithArgsRest : tactic
@[inherit_doc linarith] macro "linarith!" rest:linarithArgsRest : tactic =>
`(tactic| linarith ! $rest:linarithArgsRest)
/--
An extension of `linarith` with some preprocessing to allow it to solve some nonlinear arithmetic
problems. (Based on Coq's `nra` tactic.) See `linarith` for the available syntax of options,
which are inherited by `nlinarith`; that is, `nlinarith!` and `nlinarith only [h1, h2]` all work as
in `linarith`. The preprocessing is as follows:
* For every subterm `a ^ 2` or `a * a` in a hypothesis or the goal,
the assumption `0 ≤ a ^ 2` or `0 ≤ a * a` is added to the context.
* For every pair of hypotheses `a1 R1 b1`, `a2 R2 b2` in the context, `R1, R2 ∈ {<, ≤, =}`,
the assumption `0 R' (b1 - a1) * (b2 - a2)` is added to the context (non-recursively),
where `R ∈ {<, ≤, =}` is the appropriate comparison derived from `R1, R2`.
-/
syntax (name := nlinarith) "nlinarith" "!"? linarithArgsRest : tactic
@[inherit_doc nlinarith] macro "nlinarith!" rest:linarithArgsRest : tactic =>
`(tactic| nlinarith ! $rest:linarithArgsRest)
/-- Elaborate `t` in a way that is suitable for linarith. |
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 => (f.mergeWith (fun _ b b' => b + b') g).filter (fun _ b => b ≠ 0)
namespace Mathlib.Tactic.Linarith
/-- A local abbreviation for `TreeMap` so we don't need to write `Ord.compare` each time. |
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 `linear_form_of_expr`.
If `e` appears with index `k` in `map`, it returns the singleton sum `var k`.
Otherwise it updates `map`, adding `e` with index `n`, and returns the singleton sum `var n`. |
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]) => do
let (m1, comp1) ← linearFormOfExpr red m e1
let (m2, comp2) ← linearFormOfExpr red m1 e2
return (m2, comp1.mul comp2)
| (``HAdd.hAdd, #[_, _, _, _, e1, e2]) => do
let (m1, comp1) ← linearFormOfExpr red m e1
let (m2, comp2) ← linearFormOfExpr red m1 e2
return (m2, comp1 + comp2)
| (``HSub.hSub, #[_, _, _, _, e1, e2]) => do
let (m1, comp1) ← linearFormOfExpr red m e1
let (m2, comp2) ← linearFormOfExpr red m1 e2
return (m2, comp1 + comp2.map (fun _ v => -v))
| (``Neg.neg, #[_, _, e]) => do
let (m1, comp) ← linearFormOfExpr red m e
return (m1, comp.map (fun _ v => -v))
| (``HPow.hPow, #[_, _, _, _, a, n]) => do
match n.numeral? with
| some n => do
let (m1, comp) ← linearFormOfExpr red m a
return (m1, comp.pow n)
| none => linearFormOfAtom red m e
| _ => linearFormOfAtom red m e | 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 equality, this function must be in the `MetaM` monad,
and forces some functions that call it into `MetaM` as well. |
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 numbers and `map` is updated. |
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 index that appears in comparisons in `c`. |
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, #[_, _]) =>
pure ((← aux (← mkAppM ``And.left #[proof])) ++
(← aux (← mkAppM ``And.right #[proof])))
| _ => pure [proof] | 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}"
return [h']
| _ => return [h]
| _ => return [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
| (``HSub.hSub, #[_, _, _, _, a, b]) => getNatComparisons a ++ getNatComparisons b
| (``Neg.neg, #[_, _, a]) => getNatComparisons a
| _ => [] | 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.