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
Cache.int : Cache sℤ := { rα := some q(inferInstance), dsα := none, czα := some q(inferInstance) }
def
Tactic
[ "Mathlib.Tactic.NormNum.Inv", "Mathlib.Tactic.NormNum.Pow", "Mathlib.Util.AtomM" ]
Mathlib/Tactic/Ring/Basic.lean
Cache.int
A precomputed `Cache` for `ℤ`.
partial eval {u : Lean.Level} {α : Q(Type u)} (sα : Q(CommSemiring $α)) (c : Cache sα) (e : Q($α)) : AtomM (Result (ExSum sα) e) := Lean.withIncRecDepth do let els := do try evalCast sα (← derive e) catch _ => evalAtom sα e let .const n _ := (← withReducible <| whnf e).getAppFn | els match n, c.rα, c.dsα with | ``HAdd.hAdd, _, _ | ``Add.add, _, _ => match e with | ~q($a + $b) => let ⟨_, va, pa⟩ ← eval sα c a let ⟨_, vb, pb⟩ ← eval sα c b let ⟨c, vc, p⟩ ← evalAdd sα va vb pure ⟨c, vc, q(add_congr $pa $pb $p)⟩ | _ => els | ``HMul.hMul, _, _ | ``Mul.mul, _, _ => match e with | ~q($a * $b) => let ⟨_, va, pa⟩ ← eval sα c a let ⟨_, vb, pb⟩ ← eval sα c b let ⟨c, vc, p⟩ ← evalMul sα va vb pure ⟨c, vc, q(mul_congr $pa $pb $p)⟩ | _ => els | ``HSMul.hSMul, rα, _ => match e, rα with | ~q(($a : ℕ) • ($b : «$α»)), _ => let ⟨_, va, pa⟩ ← eval sℕ .nat a let ⟨_, vb, pb⟩ ← eval sα c b let ⟨c, vc, p⟩ ← evalNSMul sα va vb pure ⟨c, vc, q(nsmul_congr $pa $pb $p)⟩ | ~q(@HSMul.hSMul ℤ _ _ $i $a $b), some rα => let b : Q($α) := b let ⟨_, va, pa⟩ ← eval sℤ .int a let ⟨_, vb, pb⟩ ← eval sα c b let ⟨c, vc, p⟩ ← evalZSMul sα rα va vb let pf ← mkAppM ``zsmul_congr #[pa, pb, p] pure ⟨c, vc, pf⟩ | _, _ => els | ``HPow.hPow, _, _ | ``Pow.pow, _, _ => match e with | ~q($a ^ $b) => let ⟨_, va, pa⟩ ← eval sα c a let ⟨_, vb, pb⟩ ← eval sℕ .nat b let ⟨c, vc, p⟩ ← evalPow sα va vb pure ⟨c, vc, q(pow_congr $pa $pb $p)⟩ | _ => els | ``Neg.neg, some rα, _ => match e with | ~q(-$a) => let ⟨_, va, pa⟩ ← eval sα c a let ⟨b, vb, p⟩ ← evalNeg sα rα va pure ⟨b, vb, q(neg_congr $pa $p)⟩ | _ => els | ``HSub.hSub, some rα, _ | ``Sub.sub, some rα, _ => match e with | ~q($a - $b) => do let ⟨_, va, pa⟩ ← eval sα c a ...
def
Tactic
[ "Mathlib.Tactic.NormNum.Inv", "Mathlib.Tactic.NormNum.Pow", "Mathlib.Util.AtomM" ]
Mathlib/Tactic/Ring/Basic.lean
eval
Checks whether `e` would be processed by `eval` as a ring expression, or otherwise if it is an atom or something simplifiable via `norm_num`. We use this in `ring_nf` to avoid rewriting atoms unnecessarily. Returns: * `none` if `eval` would process `e` as an algebraic ring expression * `some none` if `eval` would treat `e` as an atom. * `some (some r)` if `eval` would not process `e` as an algebraic ring expression, but `NormNum.derive` can nevertheless simplify `e`, with result `r`. -/ -- Note this is not the same as whether the result of `eval` is an atom. (e.g. consider `x + 0`.) def isAtomOrDerivable {u} {α : Q(Type u)} (sα : Q(CommSemiring $α)) (c : Cache sα) (e : Q($α)) : AtomM (Option (Option (Result (ExSum sα) e))) := do let els := try pure <| some (evalCast sα (← derive e)) catch _ => pure (some none) let .const n _ := (← withReducible <| whnf e).getAppFn | els match n, c.rα, c.dsα with | ``HAdd.hAdd, _, _ | ``Add.add, _, _ | ``HMul.hMul, _, _ | ``Mul.mul, _, _ | ``HSMul.hSMul, _, _ | ``HPow.hPow, _, _ | ``Pow.pow, _, _ | ``Neg.neg, some _, _ | ``HSub.hSub, some _, _ | ``Sub.sub, some _, _ | ``Inv.inv, _, some _ | ``HDiv.hDiv, _, some _ | ``Div.div, _, some _ => pure none | _, _, _ => els /-- Evaluates expression `e` of type `α` into a normalized representation as a polynomial. This is the main driver of `ring`, which calls out to `evalAdd`, `evalMul` etc.
CSLift (α : Type u) (β : outParam (Type u)) where /-- `lift` is the "canonical injection" from `α` to `β` -/ lift : α → β /-- `lift` is an injective function -/ inj : Function.Injective lift
class
Tactic
[ "Mathlib.Tactic.NormNum.Inv", "Mathlib.Tactic.NormNum.Pow", "Mathlib.Util.AtomM" ]
Mathlib/Tactic/Ring/Basic.lean
CSLift
`CSLift α β` is a typeclass used by `ring` for lifting operations from `α` (which is not a commutative semiring) into a commutative semiring `β` by using an injective map `lift : α → β`.
CSLiftVal {α} {β : outParam (Type u)} [CSLift α β] (a : α) (b : outParam β) : Prop where /-- The output value `b` is equal to the lift of `a`. This can be supplied by the default instance which sets `b := lift a`, but `ring` will treat this as an atom so it is more useful when there are other instances which distribute addition or multiplication. -/ eq : b = CSLift.lift a
class
Tactic
[ "Mathlib.Tactic.NormNum.Inv", "Mathlib.Tactic.NormNum.Pow", "Mathlib.Util.AtomM" ]
Mathlib/Tactic/Ring/Basic.lean
CSLiftVal
`CSLiftVal a b` means that `b = lift a`. This is used by `ring` to construct an expression `b` from the input expression `a`, and then run the usual ring algorithm on `b`.
of_lift {α β} [inst : CSLift α β] {a b : α} {a' b' : β} [h1 : CSLiftVal a a'] [h2 : CSLiftVal b b'] (h : a' = b') : a = b := inst.2 <| by rwa [← h1.1, ← h2.1] open Lean Parser.Tactic Elab Command Elab.Tactic
theorem
Tactic
[ "Mathlib.Tactic.NormNum.Inv", "Mathlib.Tactic.NormNum.Pow", "Mathlib.Util.AtomM" ]
Mathlib/Tactic/Ring/Basic.lean
of_lift
null
of_eq {α} {a b c : α} (_ : (a : α) = c) (_ : b = c) : a = b := by subst_vars; rfl
theorem
Tactic
[ "Mathlib.Tactic.NormNum.Inv", "Mathlib.Tactic.NormNum.Pow", "Mathlib.Util.AtomM" ]
Mathlib/Tactic/Ring/Basic.lean
of_eq
null
proveEq (g : MVarId) : AtomM Unit := do let some (α, e₁, e₂) := (← whnfR <|← instantiateMVars <|← g.getType).eq? | throwError "ring failed: not an equality" let .sort u ← whnf (← inferType α) | unreachable! let v ← try u.dec catch _ => throwError "not a type{indentExpr α}" have α : Q(Type v) := α let sα ← try Except.ok <$> synthInstanceQ q(CommSemiring $α) catch e => pure (.error e) have e₁ : Q($α) := e₁; have e₂ : Q($α) := e₂ let eq ← match sα with | .ok sα => ringCore sα e₁ e₂ | .error e => let β ← mkFreshExprMVarQ q(Type v) let e₁' ← mkFreshExprMVarQ q($β) let e₂' ← mkFreshExprMVarQ q($β) let (sβ, (pf : Q($e₁' = $e₂' → $e₁ = $e₂))) ← try let _l ← synthInstanceQ q(CSLift $α $β) let sβ ← synthInstanceQ q(CommSemiring $β) let _ ← synthInstanceQ q(CSLiftVal $e₁ $e₁') let _ ← synthInstanceQ q(CSLiftVal $e₂ $e₂') pure (sβ, q(of_lift (a := $e₁) (b := $e₂))) catch _ => throw e pure q($pf $(← ringCore sβ e₁' e₂')) g.assign eq where /-- The core of `proveEq` takes expressions `e₁ e₂ : α` where `α` is a `CommSemiring`, and returns a proof that they are equal (or fails). -/ ringCore {v : Level} {α : Q(Type v)} (sα : Q(CommSemiring $α)) (e₁ e₂ : Q($α)) : AtomM Q($e₁ = $e₂) := do let c ← mkCache sα profileitM Exception "ring" (← getOptions) do let ⟨a, va, pa⟩ ← eval sα c e₁ let ⟨b, vb, pb⟩ ← eval sα c e₂ unless va.eq vb do let g ← mkFreshExprMVar (← (← ringCleanupRef.get) q($a = $b)) throwError "ring failed, ring expressions not equal\n{g.mvarId!}" let pb : Q($e₂ = $a) := pb return q(of_eq $pa $pb)
def
Tactic
[ "Mathlib.Tactic.NormNum.Inv", "Mathlib.Tactic.NormNum.Pow", "Mathlib.Util.AtomM" ]
Mathlib/Tactic/Ring/Basic.lean
proveEq
This is a routine which is used to clean up the unsolved subgoal of a failed `ring1` application. It is overridden in `Mathlib/Tactic/Ring/RingNF.lean` to apply the `ring_nf` simp set to the goal. -/ initialize ringCleanupRef : IO.Ref (Expr → MetaM Expr) ← IO.mkRef pure /-- Frontend of `ring1`: attempt to close a goal `g`, assuming it is an equation of semirings.
amwo_of_cs (α : Type*) [CommSemiring α] : AddMonoidWithOne α := inferInstance
abbrev
Tactic
[ "Mathlib.Tactic.Ring.Basic", "Mathlib.Tactic.NormNum.Ineq" ]
Mathlib/Tactic/Ring/Compare.lean
amwo_of_cs
`CommSemiring` implies `AddMonoidWithOne`.
le_of_po (α : Type*) [PartialOrder α] : LE α := inferInstance
abbrev
Tactic
[ "Mathlib.Tactic.Ring.Basic", "Mathlib.Tactic.NormNum.Ineq" ]
Mathlib/Tactic/Ring/Compare.lean
le_of_po
`PartialOrder` implies `LE`.
lt_of_po (α : Type*) [PartialOrder α] : LT α := inferInstance
abbrev
Tactic
[ "Mathlib.Tactic.Ring.Basic", "Mathlib.Tactic.NormNum.Ineq" ]
Mathlib/Tactic/Ring/Compare.lean
lt_of_po
`PartialOrder` implies `LT`.
add_le_add_right {α : Type*} [CommSemiring α] [PartialOrder α] [IsOrderedRing α] {b c : α} (bc : b ≤ c) (a : α) : b + a ≤ c + a := _root_.add_le_add_right bc a
theorem
Tactic
[ "Mathlib.Tactic.Ring.Basic", "Mathlib.Tactic.NormNum.Ineq" ]
Mathlib/Tactic/Ring/Compare.lean
add_le_add_right
null
add_le_of_nonpos_left {α : Type*} [CommSemiring α] [PartialOrder α] [IsOrderedRing α] (a : α) {b : α} (h : b ≤ 0) : b + a ≤ a := _root_.add_le_of_nonpos_left h
theorem
Tactic
[ "Mathlib.Tactic.Ring.Basic", "Mathlib.Tactic.NormNum.Ineq" ]
Mathlib/Tactic/Ring/Compare.lean
add_le_of_nonpos_left
null
le_add_of_nonneg_left {α : Type*} [CommSemiring α] [PartialOrder α] [IsOrderedRing α] (a : α) {b : α} (h : 0 ≤ b) : a ≤ b + a := _root_.le_add_of_nonneg_left h
theorem
Tactic
[ "Mathlib.Tactic.Ring.Basic", "Mathlib.Tactic.NormNum.Ineq" ]
Mathlib/Tactic/Ring/Compare.lean
le_add_of_nonneg_left
null
add_lt_add_right {α : Type*} [CommSemiring α] [PartialOrder α] [IsStrictOrderedRing α] {b c : α} (bc : b < c) (a : α) : b + a < c + a := _root_.add_lt_add_right bc a
theorem
Tactic
[ "Mathlib.Tactic.Ring.Basic", "Mathlib.Tactic.NormNum.Ineq" ]
Mathlib/Tactic/Ring/Compare.lean
add_lt_add_right
null
add_lt_of_neg_left {α : Type*} [CommSemiring α] [PartialOrder α] [IsStrictOrderedRing α] (a : α) {b : α} (h : b < 0) : b + a < a := _root_.add_lt_of_neg_left a h
theorem
Tactic
[ "Mathlib.Tactic.Ring.Basic", "Mathlib.Tactic.NormNum.Ineq" ]
Mathlib/Tactic/Ring/Compare.lean
add_lt_of_neg_left
null
lt_add_of_pos_left {α : Type*} [CommSemiring α] [PartialOrder α] [IsStrictOrderedRing α] (a : α) {b : α} (h : 0 < b) : a < b + a := _root_.lt_add_of_pos_left a h
theorem
Tactic
[ "Mathlib.Tactic.Ring.Basic", "Mathlib.Tactic.NormNum.Ineq" ]
Mathlib/Tactic/Ring/Compare.lean
lt_add_of_pos_left
null
ExceptType | tooSmall | notComparable export ExceptType (tooSmall notComparable)
inductive
Tactic
[ "Mathlib.Tactic.Ring.Basic", "Mathlib.Tactic.NormNum.Ineq" ]
Mathlib/Tactic/Ring/Compare.lean
ExceptType
Inductive type carrying the two kinds of errors which can arise in the metaprograms `Mathlib.Tactic.Ring.evalLE.lean` and `Mathlib.Tactic.Ring.evalLT.lean`.
evalLE {v : Level} {α : Q(Type v)} (ics : Q(CommSemiring $α)) (_ : Q(PartialOrder $α)) (_ : Q(IsOrderedRing $α)) {a b : Q($α)} (va : Ring.ExSum q($ics) a) (vb : Ring.ExSum q($ics) b) : MetaM (Except ExceptType Q($a ≤ $b)) := do let lα : Q(LE $α) := q(le_of_po $α) assumeInstancesCommute let ⟨_, pz⟩ ← NormNum.mkOfNat α q(amwo_of_cs $α) q(nat_lit 0) let rz : NormNum.Result q((0:$α)) := NormNum.Result.isNat q(amwo_of_cs $α) q(nat_lit 0) (q(NormNum.isNat_ofNat $α $pz):) match va, vb with /- `0 ≤ 0` -/ | .zero, .zero => pure <| .ok (q(le_refl (0:$α)):) /- For numerals `ca` and `cb`, `ca + x ≤ cb + x` if `ca ≤ cb` -/ | .add (b := a') (.const (e := xa) ca hypa) va', .add (.const (e := xb) cb hypb) vb' => do unless va'.eq vb' do return .error notComparable let rxa := NormNum.Result.ofRawRat ca xa hypa let rxb := NormNum.Result.ofRawRat cb xb hypb let NormNum.Result.isTrue pf ← NormNum.evalLE.core lα rxa rxb | return .error tooSmall pure <| .ok (q(add_le_add_right (a := $a') $pf):) /- For a numeral `ca ≤ 0`, `ca + x ≤ x` -/ | .add (.const (e := xa) ca hypa) va', _ => do unless va'.eq vb do return .error notComparable let rxa := NormNum.Result.ofRawRat ca xa hypa let NormNum.Result.isTrue pf ← NormNum.evalLE.core lα rxa rz | return .error tooSmall pure <| .ok (q(add_le_of_nonpos_left (a := $b) $pf):) /- For a numeral `0 ≤ cb`, `x ≤ cb + x` -/ | _, .add (.const (e := xb) cb hypb) vb' => do unless va.eq vb' do return .error notComparable let rxb := NormNum.Result.ofRawRat cb xb hypb let NormNum.Result.isTrue pf ← NormNum.evalLE.core lα rz rxb | return .error tooSmall pure <| .ok (q(le_add_of_nonneg_left (a := $a) $pf):) | _, _ => unless va.eq vb do return .error notComparable pure <| .ok (q(le_refl $a):)
def
Tactic
[ "Mathlib.Tactic.Ring.Basic", "Mathlib.Tactic.NormNum.Ineq" ]
Mathlib/Tactic/Ring/Compare.lean
evalLE
In a commutative semiring, given `Ring.ExSum` objects `va`, `vb` which differ by a positive (additive) constant, construct a proof of `$a < $b`, where `a` (resp. `b`) is the expression in the semiring to which `va` (resp. `vb`) evaluates.
evalLT {v : Level} {α : Q(Type v)} (ics : Q(CommSemiring $α)) (_ : Q(PartialOrder $α)) (_ : Q(IsStrictOrderedRing $α)) {a b : Q($α)} (va : Ring.ExSum q($ics) a) (vb : Ring.ExSum q($ics) b) : MetaM (Except ExceptType Q($a < $b)) := do let lα : Q(LT $α) := q(lt_of_po $α) assumeInstancesCommute let ⟨_, pz⟩ ← NormNum.mkOfNat α q(amwo_of_cs $α) q(nat_lit 0) let rz : NormNum.Result q((0:$α)) := NormNum.Result.isNat q(amwo_of_cs $α) q(nat_lit 0) (q(NormNum.isNat_ofNat $α $pz):) match va, vb with /- `0 < 0` -/ | .zero, .zero => return .error tooSmall /- For numerals `ca` and `cb`, `ca + x < cb + x` if `ca < cb` -/ | .add (b := a') (.const (e := xa) ca hypa) va', .add (.const (e := xb) cb hypb) vb' => do unless va'.eq vb' do return .error notComparable let rxa := NormNum.Result.ofRawRat ca xa hypa let rxb := NormNum.Result.ofRawRat cb xb hypb let NormNum.Result.isTrue pf ← NormNum.evalLT.core lα rxa rxb | return .error tooSmall pure <| .ok (q(add_lt_add_right $pf $a'):) /- For a numeral `ca < 0`, `ca + x < x` -/ | .add (.const (e := xa) ca hypa) va', _ => do unless va'.eq vb do return .error notComparable let rxa := NormNum.Result.ofRawRat ca xa hypa let NormNum.Result.isTrue pf ← NormNum.evalLT.core lα rxa rz | return .error tooSmall have pf : Q($xa < 0) := pf pure <| .ok (q(add_lt_of_neg_left $b $pf):) /- For a numeral `0 < cb`, `x < cb + x` -/ | _, .add (.const (e := xb) cb hypb) vb' => do unless va.eq vb' do return .error notComparable let rxb := NormNum.Result.ofRawRat cb xb hypb let NormNum.Result.isTrue pf ← NormNum.evalLT.core lα rz rxb | return .error tooSmall pure <| .ok (q(lt_add_of_pos_left $a $pf):) | _, _ => return .error notComparable
def
Tactic
[ "Mathlib.Tactic.Ring.Basic", "Mathlib.Tactic.NormNum.Ineq" ]
Mathlib/Tactic/Ring/Compare.lean
evalLT
In a commutative semiring, given `Ring.ExSum` objects `va`, `vb` which differ by a positive (additive) constant, construct a proof of `$a < $b`, where `a` (resp. `b`) is the expression in the semiring to which `va` (resp. `vb`) evaluates.
le_congr {α : Type*} [LE α] {a b c d : α} (h1 : a = b) (h2 : b ≤ c) (h3 : d = c) : a ≤ d := by rwa [h1, h3]
theorem
Tactic
[ "Mathlib.Tactic.Ring.Basic", "Mathlib.Tactic.NormNum.Ineq" ]
Mathlib/Tactic/Ring/Compare.lean
le_congr
null
lt_congr {α : Type*} [LT α] {a b c d : α} (h1 : a = b) (h2 : b < c) (h3 : d = c) : a < d := by rwa [h1, h3] attribute [local instance] monadLiftOptionMetaM in
theorem
Tactic
[ "Mathlib.Tactic.Ring.Basic", "Mathlib.Tactic.NormNum.Ineq" ]
Mathlib/Tactic/Ring/Compare.lean
lt_congr
null
proveLE (g : MVarId) : MetaM Unit := do let some (α, e₁, e₂) := (← whnfR <|← instantiateMVars <|← g.getType).le? | throwError "ring failed: not of the form `A ≤ B`" let .sort u ← whnf (← inferType α) | unreachable! let v ← try u.dec catch _ => throwError "not a type{indentExpr α}" have α : Q(Type v) := α let ics ← synthInstanceQ q(CommSemiring $α) let ipo ← synthInstanceQ q(PartialOrder $α) let sα ← synthInstanceQ q(IsOrderedRing $α) assumeInstancesCommute have e₁ : Q($α) := e₁; have e₂ : Q($α) := e₂ let c ← mkCache q($ics) let (⟨a, va, pa⟩, ⟨b, vb, pb⟩) ← AtomM.run .instances do pure (← eval q($ics) c e₁, ← eval q($ics) c e₂) match ← evalLE ics ipo sα va vb with | .ok p => g.assign q(le_congr $pa $p $pb) | .error e => let g' ← mkFreshExprMVar (← (← ringCleanupRef.get) q($a ≤ $b)) match e with | notComparable => throwError "ring failed, ring expressions not equal up to an additive constant\n{g'.mvarId!}" | tooSmall => throwError "comparison failed, LHS is larger\n{g'.mvarId!}" attribute [local instance] monadLiftOptionMetaM in
def
Tactic
[ "Mathlib.Tactic.Ring.Basic", "Mathlib.Tactic.NormNum.Ineq" ]
Mathlib/Tactic/Ring/Compare.lean
proveLE
Prove goals of the form `A ≤ B` in an ordered commutative semiring, if the ring-normal forms of `A` and `B` differ by a nonnegative (additive) constant.
proveLT (g : MVarId) : MetaM Unit := do let some (α, e₁, e₂) := (← whnfR <|← instantiateMVars <|← g.getType).lt? | throwError "ring failed: not of the form `A < B`" let .sort u ← whnf (← inferType α) | unreachable! let v ← try u.dec catch _ => throwError "not a type{indentExpr α}" have α : Q(Type v) := α let ics ← synthInstanceQ q(CommSemiring $α) let ipo ← synthInstanceQ q(PartialOrder $α) let sα ← synthInstanceQ q(IsStrictOrderedRing $α) assumeInstancesCommute have e₁ : Q($α) := e₁; have e₂ : Q($α) := e₂ let c ← mkCache q($ics) let (⟨a, va, pa⟩, ⟨b, vb, pb⟩) ← AtomM.run .instances do pure (← eval q($ics) c e₁, ← eval q($ics) c e₂) match ← evalLT ics ipo sα va vb with | .ok p => g.assign q(lt_congr $pa $p $pb) | .error e => let g' ← mkFreshExprMVar (← (← ringCleanupRef.get) q($a < $b)) match e with | notComparable => throwError "ring failed, ring expressions not equal up to an additive constant\n{g'.mvarId!}" | tooSmall => throwError "comparison failed, LHS is at least as large\n{g'.mvarId!}"
def
Tactic
[ "Mathlib.Tactic.Ring.Basic", "Mathlib.Tactic.NormNum.Ineq" ]
Mathlib/Tactic/Ring/Compare.lean
proveLT
Prove goals of the form `A < B` in an ordered commutative semiring, if the ring-normal forms of `A` and `B` differ by a positive (additive) constant.
@[command_elab namePolyVarsOver, inherit_doc namePolyVarsOver] elabNameVariablesOver : CommandElab | `(command|name_poly_vars $vars:ident,* over $R:term) => do let vars := vars.getElems let size := vars.size let sizeStx : TSyntax `term := quote size for h : idx in [:size] do let var := vars[idx] let var := quote s!"{var.getId}" let idx : TSyntax `term ← `(($(quote idx) : Fin $sizeStx)) let cmd ← `(command|local notation3 $var:str => MvPolynomial.X (R := $R) (σ := Fin $sizeStx) $idx) elabCommand cmd | _ => throwUnsupportedSyntax
def
Tactic
[ "Mathlib.Algebra.MvPolynomial.Basic" ]
Mathlib/Tactic/Ring/NamePolyVars.lean
elabNameVariablesOver
null
ExBase.isAtom : ExBase sα a → Bool | .atom _ => true | _ => false
def
Tactic
[ "Mathlib.Tactic.Ring.Basic", "Mathlib.Tactic.TryThis", "Mathlib.Tactic.Conv", "Mathlib.Util.AtLocation", "Mathlib.Util.AtomM.Recurse", "Mathlib.Util.Qq" ]
Mathlib/Tactic/Ring/RingNF.lean
ExBase.isAtom
True if this represents an atomic expression.
ExProd.isAtom : ExProd sα a → Bool | .mul va₁ (.const 1 _) (.const 1 _) => va₁.isAtom | _ => false
def
Tactic
[ "Mathlib.Tactic.Ring.Basic", "Mathlib.Tactic.TryThis", "Mathlib.Tactic.Conv", "Mathlib.Util.AtLocation", "Mathlib.Util.AtomM.Recurse", "Mathlib.Util.Qq" ]
Mathlib/Tactic/Ring/RingNF.lean
ExProd.isAtom
True if this represents an atomic expression.
ExSum.isAtom : ExSum sα a → Bool | .add va₁ va₂ => match va₂ with -- FIXME: this takes a while to compile as one match | .zero => va₁.isAtom | _ => false | _ => false
def
Tactic
[ "Mathlib.Tactic.Ring.Basic", "Mathlib.Tactic.TryThis", "Mathlib.Tactic.Conv", "Mathlib.Util.AtLocation", "Mathlib.Util.AtomM.Recurse", "Mathlib.Util.Qq" ]
Mathlib/Tactic/Ring/RingNF.lean
ExSum.isAtom
True if this represents an atomic expression.
RingMode where /-- Sum-of-products form, like `x + x * y * 2 + z ^ 2`. -/ | SOP /-- Raw form: the representation `ring` uses internally. -/ | raw deriving Inhabited, BEq, Repr
inductive
Tactic
[ "Mathlib.Tactic.Ring.Basic", "Mathlib.Tactic.TryThis", "Mathlib.Tactic.Conv", "Mathlib.Util.AtLocation", "Mathlib.Util.AtomM.Recurse", "Mathlib.Util.Qq" ]
Mathlib/Tactic/Ring/RingNF.lean
RingMode
The normalization style for `ring_nf`.
Config extends AtomM.Recurse.Config where /-- if true, then fail if no progress is made -/ failIfUnchanged := true /-- The normalization style. -/ mode := RingMode.SOP deriving Inhabited, BEq, Repr attribute [nolint unusedArguments] Mathlib.Tactic.RingNF.instReprConfig.repr
structure
Tactic
[ "Mathlib.Tactic.Ring.Basic", "Mathlib.Tactic.TryThis", "Mathlib.Tactic.Conv", "Mathlib.Util.AtLocation", "Mathlib.Util.AtomM.Recurse", "Mathlib.Util.Qq" ]
Mathlib/Tactic/Ring/RingNF.lean
Config
Configuration for `ring_nf`.
evalExpr (e : Expr) : AtomM Simp.Result := do let e ← withReducible <| whnf e guard e.isApp -- all interesting ring expressions are applications let ⟨u, α, e⟩ ← inferTypeQ' e let sα ← synthInstanceQ q(CommSemiring $α) let c ← mkCache sα let ⟨a, _, pa⟩ ← match ← isAtomOrDerivable q($sα) c q($e) with | none => eval sα c e -- `none` indicates that `eval` will find something algebraic. | some none => failure -- No point rewriting atoms | some (some r) => pure r -- Nothing algebraic for `eval` to use, but `norm_num` simplifies. pure { expr := a, proof? := pa } variable {R : Type*} [CommSemiring R] {n d : ℕ}
def
Tactic
[ "Mathlib.Tactic.Ring.Basic", "Mathlib.Tactic.TryThis", "Mathlib.Tactic.Conv", "Mathlib.Util.AtLocation", "Mathlib.Util.AtomM.Recurse", "Mathlib.Util.Qq" ]
Mathlib/Tactic/Ring/RingNF.lean
evalExpr
Function elaborating `RingNF.Config`. -/ declare_config_elab elabConfig Config /-- Evaluates an expression `e` into a normalized representation as a polynomial. This is a variant of `Mathlib.Tactic.Ring.eval`, the main driver of the `ring` tactic. It differs in * operating on `Expr` (input) and `Simp.Result` (output), rather than typed `Qq` versions of these; * throwing an error if the expression `e` is an atom for the `ring` tactic.
add_assoc_rev (a b c : R) : a + (b + c) = a + b + c := (add_assoc ..).symm
theorem
Tactic
[ "Mathlib.Tactic.Ring.Basic", "Mathlib.Tactic.TryThis", "Mathlib.Tactic.Conv", "Mathlib.Util.AtLocation", "Mathlib.Util.AtomM.Recurse", "Mathlib.Util.Qq" ]
Mathlib/Tactic/Ring/RingNF.lean
add_assoc_rev
null
mul_assoc_rev (a b c : R) : a * (b * c) = a * b * c := (mul_assoc ..).symm
theorem
Tactic
[ "Mathlib.Tactic.Ring.Basic", "Mathlib.Tactic.TryThis", "Mathlib.Tactic.Conv", "Mathlib.Util.AtLocation", "Mathlib.Util.AtomM.Recurse", "Mathlib.Util.Qq" ]
Mathlib/Tactic/Ring/RingNF.lean
mul_assoc_rev
null
mul_neg {R} [Ring R] (a b : R) : a * -b = -(a * b) := by simp
theorem
Tactic
[ "Mathlib.Tactic.Ring.Basic", "Mathlib.Tactic.TryThis", "Mathlib.Tactic.Conv", "Mathlib.Util.AtLocation", "Mathlib.Util.AtomM.Recurse", "Mathlib.Util.Qq" ]
Mathlib/Tactic/Ring/RingNF.lean
mul_neg
null
add_neg {R} [Ring R] (a b : R) : a + -b = a - b := (sub_eq_add_neg ..).symm
theorem
Tactic
[ "Mathlib.Tactic.Ring.Basic", "Mathlib.Tactic.TryThis", "Mathlib.Tactic.Conv", "Mathlib.Util.AtLocation", "Mathlib.Util.AtomM.Recurse", "Mathlib.Util.Qq" ]
Mathlib/Tactic/Ring/RingNF.lean
add_neg
null
nat_rawCast_0 : (Nat.rawCast 0 : R) = 0 := by simp
theorem
Tactic
[ "Mathlib.Tactic.Ring.Basic", "Mathlib.Tactic.TryThis", "Mathlib.Tactic.Conv", "Mathlib.Util.AtLocation", "Mathlib.Util.AtomM.Recurse", "Mathlib.Util.Qq" ]
Mathlib/Tactic/Ring/RingNF.lean
nat_rawCast_0
null
nat_rawCast_1 : (Nat.rawCast 1 : R) = 1 := by simp
theorem
Tactic
[ "Mathlib.Tactic.Ring.Basic", "Mathlib.Tactic.TryThis", "Mathlib.Tactic.Conv", "Mathlib.Util.AtLocation", "Mathlib.Util.AtomM.Recurse", "Mathlib.Util.Qq" ]
Mathlib/Tactic/Ring/RingNF.lean
nat_rawCast_1
null
nat_rawCast_2 [Nat.AtLeastTwo n] : (Nat.rawCast n : R) = OfNat.ofNat n := rfl
theorem
Tactic
[ "Mathlib.Tactic.Ring.Basic", "Mathlib.Tactic.TryThis", "Mathlib.Tactic.Conv", "Mathlib.Util.AtLocation", "Mathlib.Util.AtomM.Recurse", "Mathlib.Util.Qq" ]
Mathlib/Tactic/Ring/RingNF.lean
nat_rawCast_2
null
int_rawCast_neg {R} [Ring R] : (Int.rawCast (.negOfNat n) : R) = -Nat.rawCast n := by simp
theorem
Tactic
[ "Mathlib.Tactic.Ring.Basic", "Mathlib.Tactic.TryThis", "Mathlib.Tactic.Conv", "Mathlib.Util.AtLocation", "Mathlib.Util.AtomM.Recurse", "Mathlib.Util.Qq" ]
Mathlib/Tactic/Ring/RingNF.lean
int_rawCast_neg
null
nnrat_rawCast {R} [DivisionSemiring R] : (NNRat.rawCast n d : R) = Nat.rawCast n / Nat.rawCast d := by simp
theorem
Tactic
[ "Mathlib.Tactic.Ring.Basic", "Mathlib.Tactic.TryThis", "Mathlib.Tactic.Conv", "Mathlib.Util.AtLocation", "Mathlib.Util.AtomM.Recurse", "Mathlib.Util.Qq" ]
Mathlib/Tactic/Ring/RingNF.lean
nnrat_rawCast
null
rat_rawCast_neg {R} [DivisionRing R] : (Rat.rawCast (.negOfNat n) d : R) = Int.rawCast (.negOfNat n) / Nat.rawCast d := by simp
theorem
Tactic
[ "Mathlib.Tactic.Ring.Basic", "Mathlib.Tactic.TryThis", "Mathlib.Tactic.Conv", "Mathlib.Util.AtLocation", "Mathlib.Util.AtomM.Recurse", "Mathlib.Util.Qq" ]
Mathlib/Tactic/Ring/RingNF.lean
rat_rawCast_neg
null
cleanup (cfg : RingNF.Config) (r : Simp.Result) : MetaM Simp.Result := do match cfg.mode with | .raw => pure r | .SOP => do let thms : SimpTheorems := {} let thms ← [``add_zero, ``add_assoc_rev, ``_root_.mul_one, ``mul_assoc_rev, ``_root_.pow_one, ``mul_neg, ``add_neg].foldlM (·.addConst ·) thms let thms ← [``nat_rawCast_0, ``nat_rawCast_1, ``nat_rawCast_2, ``int_rawCast_neg, ``nnrat_rawCast, ``rat_rawCast_neg].foldlM (·.addConst · (post := false)) thms let ctx ← Simp.mkContext { zetaDelta := cfg.zetaDelta } (simpTheorems := #[thms]) (congrTheorems := ← getSimpCongrTheorems) pure <| ← r.mkEqTrans (← Simp.main r.expr ctx (methods := Lean.Meta.Simp.mkDefaultMethodsCore {})).1
def
Tactic
[ "Mathlib.Tactic.Ring.Basic", "Mathlib.Tactic.TryThis", "Mathlib.Tactic.Conv", "Mathlib.Util.AtLocation", "Mathlib.Util.AtomM.Recurse", "Mathlib.Util.Qq" ]
Mathlib/Tactic/Ring/RingNF.lean
cleanup
A cleanup routine, which simplifies normalized polynomials to a more human-friendly format.
@[tactic ringNFConv] elabRingNFConv : Tactic := fun stx ↦ match stx with | `(conv| ring_nf $[!%$tk]? $cfg:optConfig) => withMainContext do let mut cfg ← elabConfig cfg if tk.isSome then cfg := { cfg with red := .default, zetaDelta := true } let s ← IO.mkRef {} Conv.applySimpResult (← AtomM.recurse s cfg.toConfig evalExpr (cleanup cfg) (← instantiateMVars (← Conv.getLhs))) | _ => Elab.throwUnsupportedSyntax @[inherit_doc ringNF] macro "ring_nf!" cfg:optConfig : conv => `(conv| ring_nf ! $cfg:optConfig)
def
Tactic
[ "Mathlib.Tactic.Ring.Basic", "Mathlib.Tactic.TryThis", "Mathlib.Tactic.Conv", "Mathlib.Util.AtLocation", "Mathlib.Util.AtomM.Recurse", "Mathlib.Util.Qq" ]
Mathlib/Tactic/Ring/RingNF.lean
elabRingNFConv
Overrides the default error message in `ring1` to use a prettified version of the goal. -/ initialize ringCleanupRef.set fun e => do return (← cleanup {} { expr := e }).expr open Elab.Tactic Parser.Tactic /-- Simplification tactic for expressions in the language of commutative (semi)rings, which rewrites all ring expressions into a normal form. * `ring_nf!` will use a more aggressive reducibility setting to identify atoms. * `ring_nf (config := cfg)` allows for additional configuration: * `red`: the reducibility setting (overridden by `!`) * `zetaDelta`: if true, local let variables can be unfolded (overridden by `!`) * `recursive`: if true, `ring_nf` will also recurse into atoms * `ring_nf` works as both a tactic and a conv tactic. In tactic mode, `ring_nf at h` can be used to rewrite in a hypothesis. This can be used non-terminally to normalize ring expressions in the goal such as `⊢ P (x + x + x)` ~> `⊢ P (x * 3)`, as well as being able to prove some equations that `ring` cannot because they involve ring reasoning inside a subterm, such as `sin (x + y) + sin (y + x) = 2 * sin (x + y)`. -/ elab (name := ringNF) "ring_nf" tk:"!"? cfg:optConfig loc:(location)? : tactic => do let mut cfg ← elabConfig cfg if tk.isSome then cfg := { cfg with red := .default, zetaDelta := true } let loc := (loc.map expandLocation).getD (.targets #[] true) let s ← IO.mkRef {} let m := AtomM.recurse s cfg.toConfig evalExpr (cleanup cfg) transformAtLocation (m ·) "ring_nf" loc cfg.failIfUnchanged false @[inherit_doc ringNF] macro "ring_nf!" cfg:optConfig loc:(location)? : tactic => `(tactic| ring_nf ! $cfg:optConfig $(loc)?) @[inherit_doc ringNF] syntax (name := ringNFConv) "ring_nf" "!"? optConfig : conv /-- Tactic for solving equations of *commutative* (semi)rings, allowing variables in the exponent. * This version of `ring1` uses `ring_nf` to simplify in atoms. * The variant `ring1_nf!` will use a more aggressive reducibility setting to determine equality of atoms. -/ elab (name := ring1NF) "ring1_nf" tk:"!"? cfg:optConfig : tactic => do let mut cfg ← elabConfig cfg if tk.isSome then cfg := { cfg with red := .default, zetaDelta := true } let s ← IO.mkRef {} liftMetaMAtMain fun g ↦ AtomM.RecurseM.run s cfg.toConfig evalExpr (cleanup cfg) <| proveEq g @[inherit_doc ring1NF] macro "ring1_nf!" cfg:optConfig : tactic => `(tactic| ring1_nf ! $cfg:optConfig) /-- Elaborator for the `ring_nf` tactic.
Literal | pos : Nat → Literal | neg : Nat → Literal
inductive
Tactic
[ "Mathlib.Algebra.Group.Nat.Defs", "Mathlib.Tactic.ByContra" ]
Mathlib/Tactic/Sat/FromLRAT.lean
Literal
A literal is a positive or negative occurrence of an atomic propositional variable. Note that unlike DIMACS, 0 is a valid variable index.
Literal.ofInt (i : Int) : Literal := if i < 0 then Literal.neg (-i-1).toNat else Literal.pos (i-1).toNat
def
Tactic
[ "Mathlib.Algebra.Group.Nat.Defs", "Mathlib.Tactic.ByContra" ]
Mathlib/Tactic/Sat/FromLRAT.lean
Literal.ofInt
Construct a literal. Positive numbers are translated to positive literals, and negative numbers become negative literals. The input is assumed to be nonzero.
Literal.negate : Literal → Literal | pos i => neg i | neg i => pos i
def
Tactic
[ "Mathlib.Algebra.Group.Nat.Defs", "Mathlib.Tactic.ByContra" ]
Mathlib/Tactic/Sat/FromLRAT.lean
Literal.negate
Swap the polarity of a literal.
Clause := List Literal
def
Tactic
[ "Mathlib.Algebra.Group.Nat.Defs", "Mathlib.Tactic.ByContra" ]
Mathlib/Tactic/Sat/FromLRAT.lean
Clause
A clause is a list of literals, thought of as a disjunction like `a ∨ b ∨ ¬c`.
Clause.nil : Clause := []
def
Tactic
[ "Mathlib.Algebra.Group.Nat.Defs", "Mathlib.Tactic.ByContra" ]
Mathlib/Tactic/Sat/FromLRAT.lean
Clause.nil
The empty clause
Clause.cons : Literal → Clause → Clause := List.cons
def
Tactic
[ "Mathlib.Algebra.Group.Nat.Defs", "Mathlib.Tactic.ByContra" ]
Mathlib/Tactic/Sat/FromLRAT.lean
Clause.cons
Append a literal to a clause.
Fmla := List Clause
abbrev
Tactic
[ "Mathlib.Algebra.Group.Nat.Defs", "Mathlib.Tactic.ByContra" ]
Mathlib/Tactic/Sat/FromLRAT.lean
Fmla
A formula is a list of clauses, thought of as a conjunction like `(a ∨ b) ∧ c ∧ (¬c ∨ ¬d)`.
Fmla.one (c : Clause) : Fmla := [c]
def
Tactic
[ "Mathlib.Algebra.Group.Nat.Defs", "Mathlib.Tactic.ByContra" ]
Mathlib/Tactic/Sat/FromLRAT.lean
Fmla.one
A single clause as a formula.
Fmla.and (a b : Fmla) : Fmla := a ++ b
def
Tactic
[ "Mathlib.Algebra.Group.Nat.Defs", "Mathlib.Tactic.ByContra" ]
Mathlib/Tactic/Sat/FromLRAT.lean
Fmla.and
A conjunction of formulas.
Fmla.subsumes (f f' : Fmla) : Prop where prop : ∀ x, x ∈ f' → x ∈ f
structure
Tactic
[ "Mathlib.Algebra.Group.Nat.Defs", "Mathlib.Tactic.ByContra" ]
Mathlib/Tactic/Sat/FromLRAT.lean
Fmla.subsumes
Formula `f` subsumes `f'` if all the clauses in `f'` are in `f`. We use this to prove that all clauses in the formula are subsumed by it.
Fmla.subsumes_self (f : Fmla) : f.subsumes f := ⟨fun _ h ↦ h⟩
theorem
Tactic
[ "Mathlib.Algebra.Group.Nat.Defs", "Mathlib.Tactic.ByContra" ]
Mathlib/Tactic/Sat/FromLRAT.lean
Fmla.subsumes_self
null
Fmla.subsumes_left (f f₁ f₂ : Fmla) (H : f.subsumes (f₁.and f₂)) : f.subsumes f₁ := ⟨fun _ h ↦ H.1 _ <| List.mem_append.2 <| Or.inl h⟩
theorem
Tactic
[ "Mathlib.Algebra.Group.Nat.Defs", "Mathlib.Tactic.ByContra" ]
Mathlib/Tactic/Sat/FromLRAT.lean
Fmla.subsumes_left
null
Fmla.subsumes_right (f f₁ f₂ : Fmla) (H : f.subsumes (f₁.and f₂)) : f.subsumes f₂ := ⟨fun _ h ↦ H.1 _ <| List.mem_append.2 <| Or.inr h⟩
theorem
Tactic
[ "Mathlib.Algebra.Group.Nat.Defs", "Mathlib.Tactic.ByContra" ]
Mathlib/Tactic/Sat/FromLRAT.lean
Fmla.subsumes_right
null
Valuation := Nat → Prop
def
Tactic
[ "Mathlib.Algebra.Group.Nat.Defs", "Mathlib.Tactic.ByContra" ]
Mathlib/Tactic/Sat/FromLRAT.lean
Valuation
A valuation is an assignment of values to all the propositional variables.
Valuation.neg (v : Valuation) : Literal → Prop | Literal.pos i => ¬ v i | Literal.neg i => v i
def
Tactic
[ "Mathlib.Algebra.Group.Nat.Defs", "Mathlib.Tactic.ByContra" ]
Mathlib/Tactic/Sat/FromLRAT.lean
Valuation.neg
`v.neg lit` asserts that literal `lit` is falsified in the valuation.
Valuation.satisfies (v : Valuation) : Clause → Prop | [] => False | l::c => v.neg l → v.satisfies c
def
Tactic
[ "Mathlib.Algebra.Group.Nat.Defs", "Mathlib.Tactic.ByContra" ]
Mathlib/Tactic/Sat/FromLRAT.lean
Valuation.satisfies
`v.satisfies c` asserts that clause `c` satisfied by the valuation. It is written in a negative way: A clause like `a ∨ ¬b ∨ c` is rewritten as `¬a → b → ¬c → False`, so we are asserting that it is not the case that all literals in the clause are falsified.
Valuation.satisfies_fmla (v : Valuation) (f : Fmla) : Prop where prop : ∀ c, c ∈ f → v.satisfies c
structure
Tactic
[ "Mathlib.Algebra.Group.Nat.Defs", "Mathlib.Tactic.ByContra" ]
Mathlib/Tactic/Sat/FromLRAT.lean
Valuation.satisfies_fmla
`v.satisfies_fmla f` asserts that formula `f` is satisfied by the valuation. A formula is satisfied if all clauses in it are satisfied.
Fmla.proof (f : Fmla) (c : Clause) : Prop := ∀ v : Valuation, v.satisfies_fmla f → v.satisfies c
def
Tactic
[ "Mathlib.Algebra.Group.Nat.Defs", "Mathlib.Tactic.ByContra" ]
Mathlib/Tactic/Sat/FromLRAT.lean
Fmla.proof
`f.proof c` asserts that `c` is derivable from `f`.
Fmla.proof_of_subsumes {f : Fmla} {c : Clause} (H : Fmla.subsumes f (Fmla.one c)) : f.proof c := fun _ h ↦ h.1 _ <| H.1 _ <| List.Mem.head ..
theorem
Tactic
[ "Mathlib.Algebra.Group.Nat.Defs", "Mathlib.Tactic.ByContra" ]
Mathlib/Tactic/Sat/FromLRAT.lean
Fmla.proof_of_subsumes
If `f` subsumes `c` (i.e. `c ∈ f`), then `f.proof c`.
Valuation.by_cases {v : Valuation} {l} (h₁ : v.neg l.negate → False) (h₂ : v.neg l → False) : False := match l with | Literal.pos _ => h₂ h₁ | Literal.neg _ => h₁ h₂
theorem
Tactic
[ "Mathlib.Algebra.Group.Nat.Defs", "Mathlib.Tactic.ByContra" ]
Mathlib/Tactic/Sat/FromLRAT.lean
Valuation.by_cases
The core unit-propagation step. We have a local context of assumptions `¬l'` (sometimes called an assignment) and we wish to add `¬l` to the context, that is, we want to prove `l` is also falsified. This is because there is a clause `a ∨ b ∨ ¬l` in the global context such that all literals in the clause are falsified except for `¬l`; so in the context `h₁` where we suppose that `¬l` is falsified, the clause itself is falsified so we can prove `False`. We continue the proof in `h₂`, with the assumption that `l` is falsified.
Valuation.implies (v : Valuation) (p : Prop) : List Prop → Nat → Prop | [], _ => p | a::as, n => (v n ↔ a) → v.implies p as (n + 1)
def
Tactic
[ "Mathlib.Algebra.Group.Nat.Defs", "Mathlib.Tactic.ByContra" ]
Mathlib/Tactic/Sat/FromLRAT.lean
Valuation.implies
`v.implies p [a, b, c] 0` definitionally unfolds to `(v 0 ↔ a) → (v 1 ↔ b) → (v 2 ↔ c) → p`. This is used to introduce assumptions about the first `n` values of `v` during reification.
Valuation.mk : List Prop → Valuation | [], _ => False | a::_, 0 => a | _::as, n + 1 => mk as n
def
Tactic
[ "Mathlib.Algebra.Group.Nat.Defs", "Mathlib.Tactic.ByContra" ]
Mathlib/Tactic/Sat/FromLRAT.lean
Valuation.mk
`Valuation.mk [a, b, c]` is a valuation which is `a` at 0, `b` at 1 and `c` at 2, and false everywhere else.
Valuation.mk_implies {p} {as ps} (as₁) : as = List.reverseAux as₁ ps → (Valuation.mk as).implies p ps as₁.length → p := by induction ps generalizing as₁ with | nil => exact fun _ ↦ id | cons a as ih => refine fun e H ↦ @ih (a::as₁) e (H ?_) subst e; clear ih H suffices ∀ n n', n' = List.length as₁ + n → ∀ bs, mk (as₁.reverseAux bs) n' ↔ mk bs n from this 0 _ rfl (a::as) induction as₁ with | nil => simp | cons b as₁ ih => simpa using fun n bs ↦ ih (n + 1) _ (Nat.succ_add ..) _
theorem
Tactic
[ "Mathlib.Algebra.Group.Nat.Defs", "Mathlib.Tactic.ByContra" ]
Mathlib/Tactic/Sat/FromLRAT.lean
Valuation.mk_implies
The fundamental relationship between `mk` and `implies`: `(mk ps).implies p ps 0` is equivalent to `p`.
Fmla.reify (v : Valuation) (f : Fmla) (p : Prop) : Prop where prop : ¬ v.satisfies_fmla f → p variable {v : Valuation}
structure
Tactic
[ "Mathlib.Algebra.Group.Nat.Defs", "Mathlib.Tactic.ByContra" ]
Mathlib/Tactic/Sat/FromLRAT.lean
Fmla.reify
Asserts that `¬⟦f⟧_v` implies `p`.
Fmla.refute {p : Prop} {ps} (f : Fmla) (hf : f.proof []) (hv : ∀ v, Valuation.implies v (Fmla.reify v f p) ps 0) : p := (Valuation.mk_implies [] rfl (hv _)).1 (hf _)
theorem
Tactic
[ "Mathlib.Algebra.Group.Nat.Defs", "Mathlib.Tactic.ByContra" ]
Mathlib/Tactic/Sat/FromLRAT.lean
Fmla.refute
If `f` is unsatisfiable, and every `v` which agrees with `ps` implies `¬⟦f⟧_v → p`, then `p`. Equivalently, there exists a valuation `v` which agrees with `ps`, and every such valuation yields `¬⟦f⟧_v` because `f` is unsatisfiable.
Fmla.reify_or {f₁ : Fmla} {a : Prop} {f₂ : Fmla} {b : Prop} (h₁ : Fmla.reify v f₁ a) (h₂ : Fmla.reify v f₂ b) : Fmla.reify v (f₁.and f₂) (a ∨ b) := by refine ⟨fun H ↦ by_contra fun hn ↦ H ⟨fun c h ↦ by_contra fun hn' ↦ ?_⟩⟩ rcases List.mem_append.1 h with h | h · exact hn <| Or.inl <| h₁.1 fun Hc ↦ hn' <| Hc.1 _ h · exact hn <| Or.inr <| h₂.1 fun Hc ↦ hn' <| Hc.1 _ h
theorem
Tactic
[ "Mathlib.Algebra.Group.Nat.Defs", "Mathlib.Tactic.ByContra" ]
Mathlib/Tactic/Sat/FromLRAT.lean
Fmla.reify_or
Negation turns AND into OR, so `¬⟦f₁ ∧ f₂⟧_v ≡ ¬⟦f₁⟧_v ∨ ¬⟦f₂⟧_v`.
Clause.reify (v : Valuation) (c : Clause) (p : Prop) : Prop where prop : ¬ v.satisfies c → p
structure
Tactic
[ "Mathlib.Algebra.Group.Nat.Defs", "Mathlib.Tactic.ByContra" ]
Mathlib/Tactic/Sat/FromLRAT.lean
Clause.reify
Asserts that `¬⟦c⟧_v` implies `p`.
Fmla.reify_one {c : Clause} {a : Prop} (h : Clause.reify v c a) : Fmla.reify v (Fmla.one c) a := ⟨fun H ↦ h.1 fun h ↦ H ⟨fun | _, List.Mem.head .. => h⟩⟩
theorem
Tactic
[ "Mathlib.Algebra.Group.Nat.Defs", "Mathlib.Tactic.ByContra" ]
Mathlib/Tactic/Sat/FromLRAT.lean
Fmla.reify_one
Reification of a single clause formula.
Literal.reify (v : Valuation) (l : Literal) (p : Prop) : Prop where prop : v.neg l → p
structure
Tactic
[ "Mathlib.Algebra.Group.Nat.Defs", "Mathlib.Tactic.ByContra" ]
Mathlib/Tactic/Sat/FromLRAT.lean
Literal.reify
Asserts that `¬⟦l⟧_v` implies `p`.
Clause.reify_and {l : Literal} {a : Prop} {c : Clause} {b : Prop} (h₁ : Literal.reify v l a) (h₂ : Clause.reify v c b) : Clause.reify v (Clause.cons l c) (a ∧ b) := ⟨fun H ↦ ⟨h₁.1 (by_contra fun hn ↦ H hn.elim), h₂.1 fun h ↦ H fun _ ↦ h⟩⟩
theorem
Tactic
[ "Mathlib.Algebra.Group.Nat.Defs", "Mathlib.Tactic.ByContra" ]
Mathlib/Tactic/Sat/FromLRAT.lean
Clause.reify_and
Negation turns OR into AND, so `¬⟦l ∨ c⟧_v ≡ ¬⟦l⟧_v ∧ ¬⟦c⟧_v`.
Clause.reify_zero : Clause.reify v Clause.nil True := ⟨fun _ ↦ trivial⟩
theorem
Tactic
[ "Mathlib.Algebra.Group.Nat.Defs", "Mathlib.Tactic.ByContra" ]
Mathlib/Tactic/Sat/FromLRAT.lean
Clause.reify_zero
The reification of the empty clause is `True`: `¬⟦⊥⟧_v ≡ True`.
Clause.reify_one {l : Literal} {a : Prop} (h₁ : Literal.reify v l a) : Clause.reify v (Clause.nil.cons l) a := ⟨fun H ↦ ((Clause.reify_and h₁ Clause.reify_zero).1 H).1⟩
theorem
Tactic
[ "Mathlib.Algebra.Group.Nat.Defs", "Mathlib.Tactic.ByContra" ]
Mathlib/Tactic/Sat/FromLRAT.lean
Clause.reify_one
The reification of a singleton clause `¬⟦l⟧_v ≡ ¬⟦l⟧_v`.
Literal.reify_pos {a : Prop} {n : ℕ} (h : v n ↔ a) : (Literal.pos n).reify v ¬a := ⟨mt h.2⟩
theorem
Tactic
[ "Mathlib.Algebra.Group.Nat.Defs", "Mathlib.Tactic.ByContra" ]
Mathlib/Tactic/Sat/FromLRAT.lean
Literal.reify_pos
The reification of a positive literal `¬⟦a⟧_v ≡ ¬a`.
Literal.reify_neg {a : Prop} {n : ℕ} (h : v n ↔ a) : (Literal.neg n).reify v a := ⟨h.1⟩
theorem
Tactic
[ "Mathlib.Algebra.Group.Nat.Defs", "Mathlib.Tactic.ByContra" ]
Mathlib/Tactic/Sat/FromLRAT.lean
Literal.reify_neg
The reification of a negative literal `¬⟦¬a⟧_v ≡ a`.
Clause where /-- The list of literals as read from the input file -/ lits : Array Int /-- The clause expression of type `Clause` -/ expr : Expr /-- A proof of `⊢ ctx.proof c`. Note that we do not use `have` statements to cache these proofs: this is literally the proof expression itself. As a result, the proof terms rely heavily on dag-like sharing of the expression, and printing these proof terms directly is likely to crash lean for larger examples. -/ proof : Expr
structure
Tactic
[ "Mathlib.Algebra.Group.Nat.Defs", "Mathlib.Tactic.ByContra" ]
Mathlib/Tactic/Sat/FromLRAT.lean
Clause
The representation of a global clause.
buildClause (arr : Array Int) : Expr := let nil := mkConst ``Sat.Clause.nil let cons := mkConst ``Sat.Clause.cons arr.foldr (fun i e ↦ mkApp2 cons (toExpr <| Sat.Literal.ofInt i) e) nil
def
Tactic
[ "Mathlib.Algebra.Group.Nat.Defs", "Mathlib.Tactic.ByContra" ]
Mathlib/Tactic/Sat/FromLRAT.lean
buildClause
Construct the clause expression from the input list. For example `[1, -2]` is translated to `Clause.cons (Literal.pos 1) (Clause.cons (Literal.neg 2) Clause.nil)`.
partial buildConj (arr : Array (Array Int)) (start stop : Nat) : Expr := match stop - start with | 0 => panic! "empty" | 1 => mkApp (mkConst ``Sat.Fmla.one) (buildClause arr[start]!) | len => let mid := start + len / 2 mkApp2 (mkConst ``Sat.Fmla.and) (buildConj arr start mid) (buildConj arr mid stop)
def
Tactic
[ "Mathlib.Algebra.Group.Nat.Defs", "Mathlib.Tactic.ByContra" ]
Mathlib/Tactic/Sat/FromLRAT.lean
buildConj
Constructs the formula expression from the input CNF, as a balanced tree of `Fmla.and` nodes.
partial buildClauses (arr : Array (Array Int)) (ctx : Expr) (start stop : Nat) (f p : Expr) (accum : Nat × HashMap Nat Clause) : Nat × HashMap Nat Clause := match stop - start with | 0 => panic! "empty" | 1 => let c := f.appArg! let proof := mkApp3 (mkConst ``Sat.Fmla.proof_of_subsumes) ctx c p let n := accum.1 + 1 (n, accum.2.insert n { lits := arr[start]!, expr := c, proof }) | len => let mid := start + len / 2 let f₁ := f.appFn!.appArg! let f₂ := f.appArg! let p₁ := mkApp4 (mkConst ``Sat.Fmla.subsumes_left) ctx f₁ f₂ p let p₂ := mkApp4 (mkConst ``Sat.Fmla.subsumes_right) ctx f₁ f₂ p let accum := buildClauses arr ctx start mid f₁ p₁ accum buildClauses arr ctx mid stop f₂ p₂ accum
def
Tactic
[ "Mathlib.Algebra.Group.Nat.Defs", "Mathlib.Tactic.ByContra" ]
Mathlib/Tactic/Sat/FromLRAT.lean
buildClauses
Constructs the proofs of `⊢ ctx.proof c` for each clause `c` in `ctx`. The proofs are stashed in a `HashMap` keyed on the clause ID.
LClause where /-- The list of literals as read from the input file -/ lits : Array Int /-- The clause expression of type `Clause` -/ expr : Expr /-- The bound variable index of the hypothesis asserting `⊢ ctx.proof c`, _counting from the outside and 1-based_. (We use this numbering because we will need to reference the variable from multiple binder depths.) -/ depth : Nat
structure
Tactic
[ "Mathlib.Algebra.Group.Nat.Defs", "Mathlib.Tactic.ByContra" ]
Mathlib/Tactic/Sat/FromLRAT.lean
LClause
A localized clause reference. It is the same as `Clause` except that the proof is now a local variable.
partial buildProofStep (db : HashMap Nat Clause) (ns pf : Array Int) (ctx clause : Expr) : Except String Expr := Id.run do let mut lams := #[] let mut args := #[] let mut gctx : HashMap Nat LClause := {} for i in pf do let i := i.natAbs let some cl := db[i]? | return Except.error "missing clause" if !gctx.contains i then lams := lams.push (mkApp2 (mkConst ``Sat.Fmla.proof) ctx cl.expr) args := args.push cl.proof gctx := gctx.insert i { lits := cl.lits expr := cl.expr depth := args.size } let n := args.size let mut f := (mkAppN · args) ∘ lams.foldr (mkLambda `c default) ∘ mkLambda `v default (mkConst ``Sat.Valuation) ∘ mkLambda `hv default (mkApp2 (mkConst ``Sat.Valuation.satisfies_fmla) (mkBVar 0) ctx) let v depth := mkBVar (depth + 1) let hv depth := mkBVar depth lams := #[] let mut clause := clause let mut depth := 0 let mut lctx : HashMap Int Nat := {} for i in ns do let l := clause.appFn!.appArg! clause := clause.appArg! lams := lams.push (mkApp2 (mkConst ``Sat.Valuation.neg) (v depth) l) depth := depth.succ lctx := lctx.insert i depth f := f ∘ lams.foldr (mkLambda `h default) for (step : Int) in pf do if step < 0 then return Except.error "unimplemented: RAT step" let some cl := gctx[step.toNat]? | return Except.error "missing clause" let mut unit := none for i in cl.lits do unless lctx.contains i do if unit.isSome then return Except.error s!"not unit: {cl.lits}" depth := depth.succ unit := some i let mut pr := mkApp2 (mkBVar (depth + n + 2 - cl.depth)) (v depth) (hv depth) for i in cl.lits do pr := mkApp pr <| mkBVar (match lctx[i]? with | some k => depth - k | _ => 0) let some u := unit | return Except.ok <| f pr let lit := toExpr <| Sat.Literal.ofInt u let nlit := toExpr <| Sat.Literal.ofInt (-u) let d1 := depth-1 ...
def
Tactic
[ "Mathlib.Algebra.Group.Nat.Defs", "Mathlib.Tactic.ByContra" ]
Mathlib/Tactic/Sat/FromLRAT.lean
buildProofStep
Construct an individual proof step `⊢ ctx.proof c`. * `db`: the current global context * `ns`, `clause`: the new clause * `pf`: the LRAT proof trace * `ctx`: the main formula The proof has three steps: 1. Introduce local assumptions `have h1 : ctx.proof c1 := p1` for each clause `c1` referenced in the proof. We actually do all the introductions at once, as in `(fun h1 h2 h3 ↦ ...) p1 p2 p3`, because we want `p_i` to not be under any binders to avoid the cost of `instantiate` during typechecking and get the benefits of dag-like sharing in the `pi` (which are themselves previous proof steps which may be large terms). The hypotheses are in `gctx`, keyed on the clause ID. 2. Unfold `⊢ ctx.proof [a, b, c]` to `∀ v, v.satisfies_fmla ctx → v.neg a → v.neg b → v.neg c → False` and `intro v hv ha hb hc`, storing each `ha : v.neg a` in `lctx`, keyed on the literal `a`. 3. For each LRAT step `hc : ctx.proof [x, y]`, `hc v hv : v.neg x → v.neg y → False`. We look for a literal that is not falsified in the clause. Since it is a unit propagation step, there can be at most one such literal. * If `x` is the non-falsified clause, let `x'` denote the negated literal of `x`. Then `x'.negate` reduces to `x`, so `hnx : v.neg x'.negate |- hc v hv hnx hy : False`, so we construct the term `by_cases (fun hnx : v.neg x'.negate ↦ hc v hv hnx hy) (fun hx : v.neg x ↦ ...)` and `hx` is added to the local context. * If all clauses are falsified, then we are done: `hc v hv hx hy : False`.
LRATStep | /-- An addition step, with the clause ID, the clause literal list, and the proof trace -/ add (id : Nat) (lits : Array Int) (proof : Array Int) : LRATStep | /-- A (multiple) deletion step, which deletes all the listed clause IDs from the context -/ del (ids : Array Nat) : LRATStep
inductive
Tactic
[ "Mathlib.Algebra.Group.Nat.Defs", "Mathlib.Tactic.ByContra" ]
Mathlib/Tactic/Sat/FromLRAT.lean
LRATStep
An LRAT step is either an addition or a deletion step.
partial buildProof (arr : Array (Array Int)) (ctx ctx' : Expr) (steps : Array LRATStep) : MetaM Expr := do let p := mkApp (mkConst ``Sat.Fmla.subsumes_self) ctx let mut db := (buildClauses arr ctx 0 arr.size ctx' p default).2 for step in steps do match step with | LRATStep.del ds => db := ds.foldl (·.erase ·) db | LRATStep.add i ns pf => let e := buildClause ns match buildProofStep db ns pf ctx e with | Except.ok proof => if ns.isEmpty then return proof db := db.insert i { lits := ns, expr := e, proof } | Except.error msg => throwError msg throwError "failed to prove empty clause"
def
Tactic
[ "Mathlib.Algebra.Group.Nat.Defs", "Mathlib.Tactic.ByContra" ]
Mathlib/Tactic/Sat/FromLRAT.lean
buildProof
Build the main proof of `⊢ ctx.proof []` using the LRAT proof trace. * `arr`: The input CNF * `ctx`: The abbreviated formula, a constant like `foo.ctx_1` * `ctx'`: The definitional expansion of the formula, a tree of `Fmla.and` nodes * `steps`: The input LRAT proof trace
partial buildReify (ctx ctx' proof : Expr) (nvars : Nat) : Expr × Expr := Id.run do let (e, pr) := reifyFmla ctx' let mut pr := pr for i in [0:nvars] do let j := nvars-i-1 let ty := mkApp2 (mkConst ``Iff) (mkApp (mkBVar j) (mkRawNatLit j)) (mkBVar nvars) pr := mkLambda `h default ty pr pr := mkLambda `v default (mkConst ``Sat.Valuation) pr let mut e := e.lowerLooseBVars (nvars+1) (nvars+1) let cons := mkApp (mkConst ``List.cons [levelZero]) (mkSort levelZero) let nil := mkApp (mkConst ``List.nil [levelZero]) (mkSort levelZero) let rec mkPS depth e | 0 => e | n + 1 => mkPS (depth+1) (mkApp2 cons (mkBVar depth) e) n pr := mkApp5 (mkConst ``Sat.Fmla.refute) e (mkPS 0 nil nvars) ctx proof pr for _ in [0:nvars] do e := mkForall `a default (mkSort levelZero) e pr := mkLambda `a default (mkSort levelZero) pr pure (e, pr) where /-- The `v` variable under the `a1 ... an, v, h1 ... hn` context -/ v := mkBVar nvars /-- Returns `a` and `pr : reify v f a` given a formula `f` -/ reifyFmla f := match f.getAppFn.constName! with | ``Sat.Fmla.and => let f₁ := f.appFn!.appArg! let f₂ := f.appArg! let (e₁, h₁) := reifyFmla f₁ let (e₂, h₂) := reifyFmla f₂ (mkApp2 (mkConst ``Or) e₁ e₂, mkApp7 (mkConst ``Sat.Fmla.reify_or) v f₁ e₁ f₂ e₂ h₁ h₂) | ``Sat.Fmla.one => let c := f.appArg! let (e, h) := reifyClause c (e, mkApp4 (mkConst ``Sat.Fmla.reify_one) v c e h) | _ => panic! "not a valid formula" /-- Returns `a` and `pr : reify v c a` given a clause `c` -/ reifyClause c := if c.appFn!.isConst then (mkConst ``True, mkApp (mkConst ``Sat.Clause.reify_zero) v) else reifyClause1 c /-- Returns `a` and `pr : reify v c a` given a nonempty clause `c` -/ reifyClause1 c := let l := c.appFn!.appArg! let c := c.appArg! let (e₁, h₁) := reifyLiteral l if c.isConst then (e₁, mkApp4 (mkConst ``Sat.Clause.reify_one) v l e₁ h₁) else let (e₂, h₂) := reifyClause1 c (mkApp2 (mkConst ``And) e₁ e₂, mkApp7 (mkConst ``Sat.Clause.reify_and) v l e₁ c e₂ h₁ h₂) ...
def
Tactic
[ "Mathlib.Algebra.Group.Nat.Defs", "Mathlib.Tactic.ByContra" ]
Mathlib/Tactic/Sat/FromLRAT.lean
buildReify
Build the type and value of the reified theorem. This rewrites all the SAT definitions into standard operators on `Prop`, for example if the formula is `[[1, 2], [-1, 2], [-2]]` then this produces a proof of `⊢ ∀ a b : Prop, (a ∧ b) ∨ (¬a ∧ b) ∨ ¬b`. We use the input `nvars` to decide how many quantifiers to use. Most of the proof is under `2 * nvars + 1` quantifiers `a1 .. an : Prop, v : Valuation, h1 : v 0 ↔ a1, ... hn : v (n-1) ↔ an ⊢ ...`, and we do the index arithmetic by hand. 1. First, we call `reifyFormula ctx'` which returns `a` and `pr : reify v ctx' a` 2. Then we build `fun (v : Valuation) (h1 : v 0 ↔ a1) ... (hn : v (n-1) ↔ an) ↦ pr` 3. We have to lower expression `a` from step 1 out of the quantifiers by lowering all variable indices by `nvars+1`. This is okay because `v` and `h1..hn` do not appear in `a`. 4. We construct the expression `ps`, which is `a1 .. an : Prop ⊢ [a1, ..., an] : List Prop` 5. `refute ctx (hf : ctx.proof []) (fun v h1 .. hn ↦ pr) : a` forces some definitional unfolding since `fun h1 .. hn ↦ pr` should have type `implies v (reify v ctx a) [a1, ..., an] a`, which involves unfolding `implies` n times as well as `ctx ↦ ctx'`. 6. Finally, we `intro a1 ... an` so that we have a proof of `∀ a1 ... an, a`.
parseNat : String.Parser Nat := Json.Parser.natMaybeZero
def
Tactic
[ "Mathlib.Algebra.Group.Nat.Defs", "Mathlib.Tactic.ByContra" ]
Mathlib/Tactic/Sat/FromLRAT.lean
parseNat
Parse a natural number
parseInt : String.Parser Int := do if (← peek!) = '-' then skip; pure <| -(← parseNat) else parseNat
def
Tactic
[ "Mathlib.Algebra.Group.Nat.Defs", "Mathlib.Tactic.ByContra" ]
Mathlib/Tactic/Sat/FromLRAT.lean
parseInt
Parse an integer
partial parseInts (arr : Array Int := #[]) : String.Parser (Array Int) := do match ← parseInt <* ws with | 0 => pure arr | n => parseInts (arr.push n)
def
Tactic
[ "Mathlib.Algebra.Group.Nat.Defs", "Mathlib.Tactic.ByContra" ]
Mathlib/Tactic/Sat/FromLRAT.lean
parseInts
Parse a list of integers terminated by 0
partial parseNats (arr : Array Nat := #[]) : String.Parser (Array Nat) := do match ← parseNat <* ws with | 0 => pure arr | n => parseNats (arr.push n)
def
Tactic
[ "Mathlib.Algebra.Group.Nat.Defs", "Mathlib.Tactic.ByContra" ]
Mathlib/Tactic/Sat/FromLRAT.lean
parseNats
Parse a list of natural numbers terminated by 0
parseDimacs : String.Parser (Nat × Array (Array Int)) := do pstring "p cnf" *> ws let nvars ← parseNat <* ws let nclauses ← parseNat <* ws let mut clauses := Array.mkEmpty nclauses for _ in [:nclauses] do clauses := clauses.push (← parseInts) pure (nvars, clauses)
def
Tactic
[ "Mathlib.Algebra.Group.Nat.Defs", "Mathlib.Tactic.ByContra" ]
Mathlib/Tactic/Sat/FromLRAT.lean
parseDimacs
Parse a DIMACS format `.cnf` file. This is not very robust; we assume the file has had comments stripped.
parseLRAT : String.Parser (Array LRATStep) := many do let step ← parseNat <* ws if (← peek!) = 'd' then skip <* ws; pure <| LRATStep.del (← parseNats) else ws; pure <| LRATStep.add step (← parseInts) (← parseInts)
def
Tactic
[ "Mathlib.Algebra.Group.Nat.Defs", "Mathlib.Tactic.ByContra" ]
Mathlib/Tactic/Sat/FromLRAT.lean
parseLRAT
Parse an LRAT file into a list of steps.
fromLRATAux (cnf lrat : String) (name : Name) : MetaM (Nat × Expr × Expr × Expr) := do let Parsec.ParseResult.success _ (nvars, arr) := Parser.parseDimacs cnf.mkIterator | throwError "parse CNF failed" if arr.isEmpty then throwError "empty CNF" let ctx' := buildConj arr 0 arr.size let ctxName ← mkAuxDeclName (name ++ `ctx) addDecl <| Declaration.defnDecl { name := ctxName levelParams := [] type := mkConst ``Sat.Fmla value := ctx' hints := ReducibilityHints.regular 0 safety := DefinitionSafety.safe } let ctx := mkConst ctxName let Parsec.ParseResult.success _ steps := Parser.parseLRAT lrat.mkIterator | throwError "parse LRAT failed" let proof ← buildProof arr ctx ctx' steps let declName ← mkAuxDeclName (name ++ `proof) addDecl <| Declaration.thmDecl { name := declName levelParams := [] type := mkApp2 (mkConst ``Sat.Fmla.proof) ctx (buildClause #[]) value := proof } return (nvars, ctx, ctx', mkConst declName)
def
Tactic
[ "Mathlib.Algebra.Group.Nat.Defs", "Mathlib.Tactic.ByContra" ]
Mathlib/Tactic/Sat/FromLRAT.lean
fromLRATAux
Core of `fromLRAT`. Constructs the context and main proof definitions, but not the reification theorem. Returns: * `nvars`: the number of variables specified in the CNF file * `ctx`: The abbreviated formula, a constant like `foo.ctx_1` * `ctx'`: The definitional expansion of the formula, a tree of `Fmla.and` nodes * `proof`: A proof of `ctx.proof []`
fromLRAT (cnf lrat : String) (name : Name) : MetaM Unit := do let (nvars, ctx, ctx', proof) ← fromLRATAux cnf lrat name let (type, value) := buildReify ctx ctx' proof nvars addDecl <| Declaration.thmDecl { name, levelParams := [], type, value } open Elab Term
def
Tactic
[ "Mathlib.Algebra.Group.Nat.Defs", "Mathlib.Tactic.ByContra" ]
Mathlib/Tactic/Sat/FromLRAT.lean
fromLRAT
Main entry point. Given strings `cnf` and `lrat` with unparsed file data, and a name `name`, adds `theorem name : type := proof` where `type` is a propositional theorem like `∀ (a a_1 : Prop), (¬a ∧ ¬a_1 ∨ a ∧ ¬a_1) ∨ ¬a ∧ a_1 ∨ a ∧ a_1`. Also creates auxiliaries named `name.ctx_1` (for the CNF formula) and `name.proof_1` (for the LRAT proof), with `name` itself containing the reification proof.
foo := from_lrat "p cnf 2 4 1 2 0 -1 2 0 1 -2 0 -1 -2 0" "5 -2 0 4 3 0 5 d 3 4 0 6 1 0 5 1 0 6 d 1 0 7 0 5 2 6 0" ``` produces a theorem: ``` foo : ∀ (a a_1 : Prop), (¬a ∧ ¬a_1 ∨ a ∧ ¬a_1) ∨ ¬a ∧ a_1 ∨ a ∧ a_1 ``` * You can use this term after `have :=` or in `def foo :=` to produce the term without constraining the type. * You can use it when a specific type is expected, but it currently does not pay any attention to the shape of the goal and always produces the same theorem, so you can only use this to do alpha renaming. * You can use the `include_str` macro in place of the two strings to load CNF / LRAT files from disk. -/ elab "from_lrat " cnf:term:max ppSpace lrat:term:max : term => do let cnf ← unsafe evalTerm String (mkConst ``String) cnf let lrat ← unsafe evalTerm String (mkConst ``String) lrat let name ← mkAuxName `lrat fromLRAT cnf lrat name return mkConst name
def
Tactic
[ "Mathlib.Algebra.Group.Nat.Defs", "Mathlib.Tactic.ByContra" ]
Mathlib/Tactic/Sat/FromLRAT.lean
foo
null
private exists_of_imp_eq {α : Sort u} {p : α → Prop} (a : α) (h : ∀ b, p b → a = b) : (∃ b, p b) = p a := by grind
theorem
Tactic
[ "Mathlib.Init" ]
Mathlib/Tactic/Simproc/ExistsAndEq.lean
exists_of_imp_eq
null
partial findImpEqProof (p : Expr) : MetaM <| Option (Expr × Expr) := do lambdaTelescope p fun xs body => do let #[x] := xs | return none withLocalDecl .anonymous .default body fun h => withNewMCtxDepth do let some ⟨res, proof⟩ ← go x h | return none let pf1 ← mkLambdaFVars #[x, h] proof return some ⟨res, ← mkAppM ``exists_of_imp_eq #[res, pf1]⟩ where /-- Traverses the expression `h`, branching at each `And`, to find a proof of `x = a` for some `a`. -/ go (x h : Expr) : MetaM <| Option (Expr × Expr) := do match (← inferType h).getAppFnArgs with | (``Eq, #[β, a, b]) => if !(← isDefEq (← inferType x) β) then return none if (← isDefEq x a) && !(b.containsFVar x.fvarId!) then return some ⟨b, ← mkAppM ``Eq.symm #[h]⟩ if (← isDefEq x b) && !(a.containsFVar x.fvarId!) then return some ⟨a, h⟩ else return none | (``And, #[_, _]) => if let some res ← go x (← mkAppM ``And.left #[h]) then return res if let some res ← go x (← mkAppM ``And.right #[h]) then return res return none | _ => return none
def
Tactic
[ "Mathlib.Init" ]
Mathlib/Tactic/Simproc/ExistsAndEq.lean
findImpEqProof
For an expression `p` of the form `fun (x : α) ↦ (body : Prop)`, checks whether `body` implies `x = a` for some `a`, and constructs a proof of `(∃ x, p x) = p a` using `exists_of_imp_eq`.
FactorsHelper (n p : ℕ) (l : List ℕ) : Prop := p.Prime → (p :: l).IsChain (· ≤ ·) ∧ (∀ a ∈ l, Nat.Prime a) ∧ l.prod = n /-! The argument explicitness in this section is chosen to make only the numerals in the factors list appear in the proof term. -/
def
Tactic
[ "Mathlib.Data.Nat.Factors", "Mathlib.Tactic.NormNum.Prime" ]
Mathlib/Tactic/Simproc/Factors.lean
FactorsHelper
A proof of the partial computation of `primeFactorsList`. Asserts that `l` is a sorted list of primes multiplying to `n` and lower bounded by a prime `p`.
FactorsHelper.nil {a : ℕ} : FactorsHelper 1 a [] := fun _ => ⟨.singleton _, List.forall_mem_nil _, List.prod_nil⟩
theorem
Tactic
[ "Mathlib.Data.Nat.Factors", "Mathlib.Tactic.NormNum.Prime" ]
Mathlib/Tactic/Simproc/Factors.lean
FactorsHelper.nil
null
FactorsHelper.cons_of_le {n m : ℕ} (a : ℕ) {b : ℕ} {l : List ℕ} (h₁ : IsNat (b * m) n) (h₂ : a ≤ b) (h₃ : minFac b = b) (H : FactorsHelper m b l) : FactorsHelper n a (b :: l) := fun pa => have pb : b.Prime := Nat.prime_def_minFac.2 ⟨le_trans pa.two_le h₂, h₃⟩ let ⟨f₁, f₂, f₃⟩ := H pb ⟨List.IsChain.cons_cons h₂ f₁, fun _ h => (List.eq_or_mem_of_mem_cons h).elim (fun e => e.symm ▸ pb) (f₂ _), by rw [List.prod_cons, f₃, h₁.out, cast_id]⟩
theorem
Tactic
[ "Mathlib.Data.Nat.Factors", "Mathlib.Tactic.NormNum.Prime" ]
Mathlib/Tactic/Simproc/Factors.lean
FactorsHelper.cons_of_le
null
FactorsHelper.cons {n m : ℕ} {a : ℕ} (b : ℕ) {l : List ℕ} (h₁ : IsNat (b * m) n) (h₂ : Nat.blt a b) (h₃ : IsNat (minFac b) b) (H : FactorsHelper m b l) : FactorsHelper n a (b :: l) := H.cons_of_le _ h₁ (Nat.blt_eq.mp h₂).le h₃.out
theorem
Tactic
[ "Mathlib.Data.Nat.Factors", "Mathlib.Tactic.NormNum.Prime" ]
Mathlib/Tactic/Simproc/Factors.lean
FactorsHelper.cons
null