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 ⌀ |
|---|---|---|---|---|---|---|
addACEq (e₁ e₂ : Expr) : CCM Unit := do
dbgTraceACEq "cc eq:" e₁ e₂
modifyACTodo fun acTodo => acTodo.push (e₁, e₂, .eqProof e₁ e₂)
processAC
dbgTraceACState | def | Tactic | [
"Mathlib.Data.Option.Defs",
"Mathlib.Tactic.CC.MkProof"
] | Mathlib/Tactic/CC/Addition.lean | addACEq | Given AC variables `e₁` and `e₂` which are in the same equivalence class, add the proof of
`e₁ = e₂` to the AC module. |
setACVar (e : Expr) : CCM Unit := do
let eRoot ← getRoot e
let some rootEntry ← getEntry eRoot | failure
if let some acVar := rootEntry.acVar then
addACEq acVar e
else
let newRootEntry := { rootEntry with acVar := some e }
modify fun ccs => { ccs with entries := ccs.entries.insert eRoot newRootEntry } | def | Tactic | [
"Mathlib.Data.Option.Defs",
"Mathlib.Tactic.CC.MkProof"
] | Mathlib/Tactic/CC/Addition.lean | setACVar | If the root expression of `e` is AC variable, add equality to AC module. If not, register the
AC variable to the root entry. |
internalizeACVar (e : Expr) : CCM Bool := do
let ccs ← get
if ccs.acEntries.contains e then return false
modify fun _ =>
{ ccs with
acEntries := ccs.acEntries.insert e { idx := ccs.acEntries.size } }
setACVar e
return true | def | Tactic | [
"Mathlib.Data.Option.Defs",
"Mathlib.Tactic.CC.MkProof"
] | Mathlib/Tactic/CC/Addition.lean | internalizeACVar | If `e` isn't an AC variable, set `e` as an new AC variable. |
isAC (e : Expr) : CCM (Option Expr) := do
let .app (.app op _) _ := e | return none
let ccs ← get
if let some cop := ccs.canOps[op]? then
let some b := ccs.opInfo[cop]?
| throwError "opInfo should contain all canonical operators in canOps"
return bif b then some cop else none
for (cop, b) in ccs.opInfo do
if ← pureIsDefEq op cop then
modify fun _ => { ccs with canOps := ccs.canOps.insert op cop }
return bif b then some cop else none
let b ←
try
let aop ← mkAppM ``Std.Associative #[op]
let some _ ← synthInstance? aop | failure
let cop ← mkAppM ``Std.Commutative #[op]
let some _ ← synthInstance? cop | failure
pure true
catch _ =>
pure false
modify fun _ =>
{ ccs with
canOps := ccs.canOps.insert op op
opInfo := ccs.opInfo.insert op b }
return bif b then some op else none | def | Tactic | [
"Mathlib.Data.Option.Defs",
"Mathlib.Tactic.CC.MkProof"
] | Mathlib/Tactic/CC/Addition.lean | isAC | If `e` is of the form `op e₁ e₂` where `op` is an associative and commutative binary operator,
return the canonical form of `op`. |
partial convertAC (op e : Expr) (args : Array Expr := #[]) : CCM (Array Expr × Expr) := do
if let some currOp ← isAC e then
if op == currOp then
let (args, arg₁) ← convertAC op e.appFn!.appArg! args
let (args, arg₂) ← convertAC op e.appArg! args
return (args, mkApp2 op arg₁ arg₂)
let _ ← internalizeACVar e
return (args.push e, e) | def | Tactic | [
"Mathlib.Data.Option.Defs",
"Mathlib.Tactic.CC.MkProof"
] | Mathlib/Tactic/CC/Addition.lean | convertAC | Given `e := op₁ (op₂ a₁ a₂) (op₃ a₃ a₄)` where `opₙ`s are canonicalized to `op`, internalize
`aₙ`s as AC variables and return `(op (op a₁ a₂) (op a₃ a₄), args ++ #[a₁, a₂, a₃, a₄])`. |
internalizeAC (e : Expr) (parent? : Option Expr) : CCM Unit := do
let some op ← isAC e | return
let parentOp? ← parent?.casesOn (pure none) isAC
if parentOp?.any (· == op) then return
unless (← internalizeACVar e) do return
let (args, norme) ← convertAC op e
let rep := ACApps.mkApps op args
let some true := (← get).opInfo[op]? | failure
let some repe := rep.toExpr | failure
let pr ← mkACProof norme repe
let ccs ← get
trace[Debug.Meta.Tactic.cc.ac] group <|
let d := paren (ccs.ppACApps e ++ ofFormat (" :=" ++ Format.line) ++ ofExpr e)
"new term: " ++ d ++ ofFormat (Format.line ++ "===>" ++ .line) ++ ccs.ppACApps rep
modifyACTodo fun todo => todo.push (e, rep, pr)
processAC
dbgTraceACState
mutual | def | Tactic | [
"Mathlib.Data.Option.Defs",
"Mathlib.Tactic.CC.MkProof"
] | Mathlib/Tactic/CC/Addition.lean | internalizeAC | Internalize `e` so that the AC module can deal with the given expression.
If the expression does not contain an AC operator, or the parent expression
is already processed by `internalizeAC`, this operation does nothing. |
partial internalizeAppLit (e : Expr) : CCM Unit := do
if ← isInterpretedValue e then
mkEntry e true
if (← get).values then return -- we treat values as atomic symbols
else
mkEntry e false
if (← get).values && isValue e then return -- we treat values as atomic symbols
unless e.isApp do return
if let some (_, lhs, rhs) ← e.relSidesIfSymm? then
internalizeCore lhs (some e)
internalizeCore rhs (some e)
addOccurrence e lhs true
addOccurrence e rhs true
addSymmCongruenceTable e
else if (← mkCCCongrTheorem e).isSome then
let fn := e.getAppFn
let apps := e.getAppApps
guard (apps.size > 0)
guard (apps.back! == e)
let mut pinfo : List ParamInfo := []
let state ← get
if state.ignoreInstances then
pinfo := (← getFunInfoNArgs fn apps.size).paramInfo.toList
if state.hoFns.isSome && fn.isConst && !(state.hoFns.iget.contains fn.constName) then
for h : i in [:apps.size] do
let arg := apps[i].appArg!
addOccurrence e arg false
if pinfo.head?.any ParamInfo.isInstImplicit then
mkEntry arg false
propagateInstImplicit arg
else
internalizeCore arg (some e)
unless pinfo.isEmpty do
pinfo := pinfo.tail
internalizeCore fn (some e)
addOccurrence e fn false
setFO e
addCongruenceTable e
else
for h : i in [:apps.size] do
let curr := apps[i]
let .app currFn currArg := curr | unreachable!
if i < apps.size - 1 then
mkEntry curr false
for h : j in [i:apps.size] do
addOccurrence apps[j] currArg false
addOccurrence apps[j] currFn false
if pinfo.head?.any ParamInfo.isInstImplicit then
mkEntry currArg false
mkEntry currFn false
propagateInstImplicit currArg
... | def | Tactic | [
"Mathlib.Data.Option.Defs",
"Mathlib.Tactic.CC.MkProof"
] | Mathlib/Tactic/CC/Addition.lean | internalizeAppLit | The specialized `internalizeCore` for applications or literals. |
partial internalizeCore (e : Expr) (parent? : Option Expr) : CCM Unit := do
guard !e.hasLooseBVars
/- We allow metavariables after partitions have been frozen. -/
if e.hasExprMVar && !(← get).frozePartitions then
return
/- Check whether `e` has already been internalized. -/
if (← getEntry e).isNone then
match e with
| .bvar _ => unreachable!
| .sort _ => pure ()
| .const _ _ | .mvar _ => mkEntry e false
| .lam _ _ _ _ | .letE _ _ _ _ _ => mkEntry e false
| .fvar f =>
mkEntry e false
if let some v ← f.getValue? then
pushReflEq e v
| .mdata _ e' =>
mkEntry e false
internalizeCore e' e
addOccurrence e e' false
pushReflEq e e'
| .forallE _ t b _ =>
if e.isArrow then
if ← isProp t <&&> isProp b then
internalizeCore t e
internalizeCore b e
addOccurrence e t false
addOccurrence e b false
propagateImpUp e
if ← isProp e then
mkEntry e false
| .app _ _ | .lit _ => internalizeAppLit e
| .proj sn i pe =>
mkEntry e false
let some fn := (getStructureFields (← getEnv) sn)[i]? | failure
let e' ← pe.mkDirectProjection fn
internalizeAppLit e'
pushReflEq e e'
/- Remark: if should invoke `internalizeAC` even if the test `(← getEntry e).isNone` above failed.
Reason, the first time `e` was visited, it may have been visited with a different parent. -/
if (← get).ac then
internalizeAC e parent? | def | Tactic | [
"Mathlib.Data.Option.Defs",
"Mathlib.Tactic.CC.MkProof"
] | Mathlib/Tactic/CC/Addition.lean | internalizeCore | Internalize `e` so that the congruence closure can deal with the given expression. Don't forget
to process the tasks in the `todo` field later. |
partial propagateIffUp (e : Expr) : CCM Unit := do
let some (a, b) := e.iff? | failure
if ← isEqTrue a then
pushEq e b (mkApp3 (.const ``iff_eq_of_eq_true_left []) a b (← getEqTrueProof a))
else if ← isEqTrue b then
pushEq e a (mkApp3 (.const ``iff_eq_of_eq_true_right []) a b (← getEqTrueProof b))
else if ← isEqv a b then
pushEq e (.const ``True []) (mkApp3 (.const ``iff_eq_true_of_eq []) a b (← getPropEqProof a b)) | def | Tactic | [
"Mathlib.Data.Option.Defs",
"Mathlib.Tactic.CC.MkProof"
] | Mathlib/Tactic/CC/Addition.lean | propagateIffUp | Propagate equality from `a` and `b` to `a ↔ b`. |
partial propagateAndUp (e : Expr) : CCM Unit := do
let some (a, b) := e.and? | failure
if ← isEqTrue a then
pushEq e b (mkApp3 (.const ``and_eq_of_eq_true_left []) a b (← getEqTrueProof a))
else if ← isEqTrue b then
pushEq e a (mkApp3 (.const ``and_eq_of_eq_true_right []) a b (← getEqTrueProof b))
else if ← isEqFalse a then
pushEq e (.const ``False [])
(mkApp3 (.const ``and_eq_of_eq_false_left []) a b (← getEqFalseProof a))
else if ← isEqFalse b then
pushEq e (.const ``False [])
(mkApp3 (.const ``and_eq_of_eq_false_right []) a b (← getEqFalseProof b))
else if ← isEqv a b then
pushEq e a (mkApp3 (.const ``and_eq_of_eq []) a b (← getPropEqProof a b)) | def | Tactic | [
"Mathlib.Data.Option.Defs",
"Mathlib.Tactic.CC.MkProof"
] | Mathlib/Tactic/CC/Addition.lean | propagateAndUp | Propagate equality from `a` and `b` to `a ∧ b`. |
partial propagateOrUp (e : Expr) : CCM Unit := do
let some (a, b) := e.app2? ``Or | failure
if ← isEqTrue a then
pushEq e (.const ``True [])
(mkApp3 (.const ``or_eq_of_eq_true_left []) a b (← getEqTrueProof a))
else if ← isEqTrue b then
pushEq e (.const ``True [])
(mkApp3 (.const ``or_eq_of_eq_true_right []) a b (← getEqTrueProof b))
else if ← isEqFalse a then
pushEq e b (mkApp3 (.const ``or_eq_of_eq_false_left []) a b (← getEqFalseProof a))
else if ← isEqFalse b then
pushEq e a (mkApp3 (.const ``or_eq_of_eq_false_right []) a b (← getEqFalseProof b))
else if ← isEqv a b then
pushEq e a (mkApp3 (.const ``or_eq_of_eq []) a b (← getPropEqProof a b)) | def | Tactic | [
"Mathlib.Data.Option.Defs",
"Mathlib.Tactic.CC.MkProof"
] | Mathlib/Tactic/CC/Addition.lean | propagateOrUp | Propagate equality from `a` and `b` to `a ∨ b`. |
partial propagateNotUp (e : Expr) : CCM Unit := do
let some a := e.not? | failure
if ← isEqTrue a then
pushEq e (.const ``False []) (mkApp2 (.const ``not_eq_of_eq_true []) a (← getEqTrueProof a))
else if ← isEqFalse a then
pushEq e (.const ``True []) (mkApp2 (.const ``not_eq_of_eq_false []) a (← getEqFalseProof a))
else if ← isEqv a e then
let falsePr := mkApp2 (.const ``false_of_a_eq_not_a []) a (← getPropEqProof a e)
let H := Expr.app (.const ``true_eq_false_of_false []) falsePr
pushEq (.const ``True []) (.const ``False []) H | def | Tactic | [
"Mathlib.Data.Option.Defs",
"Mathlib.Tactic.CC.MkProof"
] | Mathlib/Tactic/CC/Addition.lean | propagateNotUp | Propagate equality from `a` to `¬a`. |
partial propagateImpUp (e : Expr) : CCM Unit := do
guard e.isArrow
let .forallE _ a b _ := e | unreachable!
if ← isEqTrue a then
pushEq e b (mkApp3 (.const ``imp_eq_of_eq_true_left []) a b (← getEqTrueProof a))
else if ← isEqFalse a then
pushEq e (.const ``True [])
(mkApp3 (.const ``imp_eq_of_eq_false_left []) a b (← getEqFalseProof a))
else if ← isEqTrue b then
pushEq e (.const ``True [])
(mkApp3 (.const ``imp_eq_of_eq_true_right []) a b (← getEqTrueProof b))
else if ← isEqFalse b then
let isNot : Expr → Bool × Expr
| .app (.const ``Not []) a => (true, a)
| .forallE _ a (.const ``False []) _ => (true, a)
| e => (false, e)
if let (true, arg) := isNot a then
if (← get).em then
pushEq e arg
(mkApp3 (.const ``not_imp_eq_of_eq_false_right []) arg b (← getEqFalseProof b))
else
let notA := mkApp (.const ``Not []) a
internalizeCore notA none
pushEq e notA
(mkApp3 (.const ``imp_eq_of_eq_false_right []) a b (← getEqFalseProof b))
else if ← isEqv a b then
pushEq e (.const ``True [])
(mkApp3 (.const ``imp_eq_true_of_eq []) a b (← getPropEqProof a b)) | def | Tactic | [
"Mathlib.Data.Option.Defs",
"Mathlib.Tactic.CC.MkProof"
] | Mathlib/Tactic/CC/Addition.lean | propagateImpUp | Propagate equality from `a` and `b` to `a → b`. |
partial propagateIteUp (e : Expr) : CCM Unit := do
let .app (.app (.app (.app (.app (.const ``ite [lvl]) A) c) d) a) b := e | failure
if ← isEqTrue c then
pushEq e a (mkApp6 (.const ``if_eq_of_eq_true [lvl]) c d A a b (← getEqTrueProof c))
else if ← isEqFalse c then
pushEq e b (mkApp6 (.const ``if_eq_of_eq_false [lvl]) c d A a b (← getEqFalseProof c))
else if ← isEqv a b then
pushEq e a (mkApp6 (.const ``if_eq_of_eq [lvl]) c d A a b (← getPropEqProof a b)) | def | Tactic | [
"Mathlib.Data.Option.Defs",
"Mathlib.Tactic.CC.MkProof"
] | Mathlib/Tactic/CC/Addition.lean | propagateIteUp | Propagate equality from `p`, `a` and `b` to `if p then a else b`. |
partial propagateEqUp (e : Expr) : CCM Unit := do
let some (_, a, b) := e.eq? | failure
let ra ← getRoot a
let rb ← getRoot b
if ra != rb then
let mut raNeRb : Option Expr := none
/-
We disprove inequality for interpreted values here.
The possible types of interpreted values are in `{String, Char, Int, Nat}`.
1- `String`
`ra` & `rb` are string literals, so if `ra != rb`, `ra.int?.isNone` is `true` and we can
prove `$ra ≠ $rb`.
2- `Char`
`ra` & `rb` are the form of `Char.ofNat (nat_lit n)`, so if `ra != rb`, `ra.int?.isNone` is
`true` and we can prove `$ra ≠ $rb` (assuming that `n` is not pathological value, i.e.
`n.isValidChar`).
3- `Int`, `Nat`
`ra` & `rb` are the form of `@OfNat.ofNat ℤ (nat_lit n) i` or
`@Neg.neg ℤ i' (@OfNat.ofNat ℤ (nat_lit n) i)`, so even if `ra != rb`, `$ra ≠ $rb` can be
false when `i` or `i'` in `ra` & `rb` are not alpha-equivalent but def-eq.
If `ra.int? != rb.int?`, we can prove `$ra ≠ $rb` (assuming that `i` & `i'` are not
pathological instances).
-/
if ← isInterpretedValue ra <&&> isInterpretedValue rb <&&>
pure (ra.int?.isNone || ra.int? != rb.int?) then
raNeRb := some
(Expr.app (.proj ``Iff 0 (← mkAppOptM ``bne_iff_ne #[none, none, none, ra, rb]))
(← mkEqRefl (.const ``true [])))
else
if let some c₁ ← isConstructorApp? ra then
if let some c₂ ← isConstructorApp? rb then
if c₁.name != c₂.name then
raNeRb ← withLocalDeclD `h (← mkEq ra rb) fun h => do
mkLambdaFVars #[h] (← mkNoConfusion (.const ``False []) h)
if let some raNeRb' := raNeRb then
if let some aNeRb ← mkNeOfEqOfNe a ra raNeRb' then
if let some aNeB ← mkNeOfNeOfEq aNeRb rb b then
pushEq e (.const ``False []) (← mkEqFalse aNeB) | def | Tactic | [
"Mathlib.Data.Option.Defs",
"Mathlib.Tactic.CC.MkProof"
] | Mathlib/Tactic/CC/Addition.lean | propagateEqUp | Propagate equality from `a` and `b` to *disprove* `a = b`. |
partial propagateUp (e : Expr) : CCM Unit := do
if (← get).inconsistent then return
if e.isAppOfArity ``Iff 2 then
propagateIffUp e
else if e.isAppOfArity ``And 2 then
propagateAndUp e
else if e.isAppOfArity ``Or 2 then
propagateOrUp e
else if e.isAppOfArity ``Not 1 then
propagateNotUp e
else if e.isArrow then
propagateImpUp e
else if e.isIte then
propagateIteUp e
else if e.isEq then
propagateEqUp e | def | Tactic | [
"Mathlib.Data.Option.Defs",
"Mathlib.Tactic.CC.MkProof"
] | Mathlib/Tactic/CC/Addition.lean | propagateUp | Propagate equality from subexpressions of `e` to `e`. |
partial applySimpleEqvs (e : Expr) : CCM Unit := do
if let .app (.app (.app (.app (.const ``cast [l₁]) A) B) H) a := e then
/-
```
cast H a ≍ a
theorem cast_heq.{l₁} : ∀ {A B : Sort l₁} (H : A = B) (a : A), @cast.{l₁} A B H a ≍ a
```
-/
let proof := mkApp4 (.const ``cast_heq [l₁]) A B H a
pushHEq e a proof
if let .app (.app (.app (.app (.app (.app (.const ``Eq.rec [l₁, l₂]) A) a) P) p) a') H := e then
/-
```
t ▸ p ≍ p
theorem eqRec_heq_self.{l₁, l₂} : ∀ {A : Sort l₁} {a : A} {P : (a' : A) → a = a' → Sort l₂}
(p : P a rfl) {a' : A} (H : a = a'), HEq (@Eq.rec.{l₁ l₂} A a P p a' H) p
```
-/
let proof := mkApp6 (.const ``eqRec_heq_self [l₁, l₂]) A a P p a' H
pushHEq e p proof
if let .app (.app (.app (.const ``Ne [l₁]) α) a) b := e then
let newE := Expr.app (.const ``Not []) (mkApp3 (.const ``Eq [l₁]) α a b)
internalizeCore newE none
pushReflEq e newE
if let some r ← e.reduceProjStruct? then
pushReflEq e r
let fn := e.getAppFn
if fn.isLambda then
let reducedE := e.headBeta
if let some phandler := (← get).phandler then
phandler.newAuxCCTerm reducedE
internalizeCore reducedE none
pushReflEq e reducedE
let mut revArgs : Array Expr := #[]
let mut it := e
while it.isApp do
revArgs := revArgs.push it.appArg!
let fn := it.appFn!
let rootFn ← getRoot fn
let en ← getEntry rootFn
if en.any Entry.hasLambdas then
let lambdas ← getEqcLambdas rootFn
let newLambdaApps ← propagateBeta fn revArgs lambdas
for newApp in newLambdaApps do
internalizeCore newApp none
it := fn
propagateUp e | def | Tactic | [
"Mathlib.Data.Option.Defs",
"Mathlib.Tactic.CC.MkProof"
] | Mathlib/Tactic/CC/Addition.lean | applySimpleEqvs | This method is invoked during internalization and eagerly apply basic equivalences for term `e`
Examples:
- If `e := cast H e'`, then it merges the equivalence classes of `cast H e'` and `e'`
In principle, we could mark theorems such as `cast_eq` as simplification rules, but this created
problems with the builtin support for cast-introduction in the ematching module in Lean 3.
TODO: check if this is now possible in Lean 4.
Eagerly merging the equivalence classes is also more efficient. |
partial processSubsingletonElem (e : Expr) : CCM Unit := do
let type ← inferType e
let ss ← synthInstance? (← mkAppM ``FastSubsingleton #[type])
if ss.isNone then return -- type is not a subsingleton
let type ← normalize type
internalizeCore type none
if let some it := (← get).subsingletonReprs[type]? then
pushSubsingletonEq e it
else
modify fun ccs =>
{ ccs with
subsingletonReprs := ccs.subsingletonReprs.insert type e }
let typeRoot ← getRoot type
if typeRoot == type then return
if let some it2 := (← get).subsingletonReprs[typeRoot]? then
pushSubsingletonEq e it2
else
modify fun ccs =>
{ ccs with
subsingletonReprs := ccs.subsingletonReprs.insert typeRoot e } | def | Tactic | [
"Mathlib.Data.Option.Defs",
"Mathlib.Tactic.CC.MkProof"
] | Mathlib/Tactic/CC/Addition.lean | processSubsingletonElem | If `e` is a subsingleton element, push the equality proof between `e` and its canonical form
to the todo list or register `e` as the canonical form of itself. |
partial mkEntry (e : Expr) (interpreted : Bool) : CCM Unit := do
if (← getEntry e).isSome then return
let constructor ← isConstructorApp e
modify fun ccs =>
{ ccs with toCCState := ccs.toCCState.mkEntryCore e interpreted constructor }
processSubsingletonElem e | def | Tactic | [
"Mathlib.Data.Option.Defs",
"Mathlib.Tactic.CC.MkProof"
] | Mathlib/Tactic/CC/Addition.lean | mkEntry | Add an new entry for `e` to the congruence closure. |
mayPropagate (e : Expr) : Bool :=
e.isAppOfArity ``Iff 2 || e.isAppOfArity ``And 2 || e.isAppOfArity ``Or 2 ||
e.isAppOfArity ``Not 1 || e.isArrow || e.isIte | def | Tactic | [
"Mathlib.Data.Option.Defs",
"Mathlib.Tactic.CC.MkProof"
] | Mathlib/Tactic/CC/Addition.lean | mayPropagate | Can we propagate equality from subexpressions of `e` to `e`? |
removeParents (e : Expr) (parentsToPropagate : Array Expr := #[]) : CCM (Array Expr) := do
let some ps := (← get).parents[e]? | return parentsToPropagate
let mut parentsToPropagate := parentsToPropagate
for pocc in ps do
let p := pocc.expr
trace[Debug.Meta.Tactic.cc] "remove parent: {p}"
if mayPropagate p then
parentsToPropagate := parentsToPropagate.push p
if p.isApp then
if pocc.symmTable then
let some (rel, lhs, rhs) ← p.relSidesIfSymm? | failure
let k' ← mkSymmCongruencesKey lhs rhs
if let some lst := (← get).symmCongruences[k']? then
let k := (p, rel)
let newLst ← lst.filterM fun k₂ => (!·) <$> compareSymm k k₂
if !newLst.isEmpty then
modify fun ccs =>
{ ccs with symmCongruences := ccs.symmCongruences.insert k' newLst }
else
modify fun ccs =>
{ ccs with symmCongruences := ccs.symmCongruences.erase k' }
else
let k' ← mkCongruencesKey p
if let some es := (← get).congruences[k']? then
let newEs := es.erase p
if !newEs.isEmpty then
modify fun ccs =>
{ ccs with congruences := ccs.congruences.insert k' newEs }
else
modify fun ccs =>
{ ccs with congruences := ccs.congruences.erase k' }
return parentsToPropagate | def | Tactic | [
"Mathlib.Data.Option.Defs",
"Mathlib.Tactic.CC.MkProof"
] | Mathlib/Tactic/CC/Addition.lean | removeParents | Remove parents of `e` from the congruence table and the symm congruence table, and append
parents to propagate equality, to `parentsToPropagate`.
Returns the new value of `parentsToPropagate`. |
partial invertTrans (e : Expr) (newFlipped : Bool := false) (newTarget : Option Expr := none)
(newProof : Option EntryExpr := none) : CCM Unit := do
let some n ← getEntry e | failure
if let some t := n.target then
invertTrans t (!n.flipped) (some e) n.proof
let newN : Entry :=
{ n with
flipped := newFlipped
target := newTarget
proof := newProof }
modify fun ccs => { ccs with entries := ccs.entries.insert e newN } | def | Tactic | [
"Mathlib.Data.Option.Defs",
"Mathlib.Tactic.CC.MkProof"
] | Mathlib/Tactic/CC/Addition.lean | invertTrans | The fields `target` and `proof` in `e`'s entry are encoding a transitivity proof
Let `e.rootTarget` and `e.rootProof` denote these fields.
```lean
e = e.rootTarget := e.rootProof
_ = e.rootTarget.rootTarget := e.rootTarget.rootProof
...
_ = e.root := ...
```
The transitivity proof eventually reaches the root of the equivalence class.
This method "inverts" the proof. That is, the `target` goes from `e.root` to e after
we execute it. |
collectFnRoots (root : Expr) (fnRoots : Array Expr := #[]) : CCM (Array Expr) := do
guard ((← getRoot root) == root)
let mut fnRoots : Array Expr := fnRoots
let mut visited : ExprSet := ∅
let mut it := root
repeat
let fnRoot ← getRoot (it.getAppFn)
if !visited.contains fnRoot then
visited := visited.insert fnRoot
fnRoots := fnRoots.push fnRoot
let some itN ← getEntry it | failure
it := itN.next
until it == root
return fnRoots | def | Tactic | [
"Mathlib.Data.Option.Defs",
"Mathlib.Tactic.CC.MkProof"
] | Mathlib/Tactic/CC/Addition.lean | collectFnRoots | Traverse the `root`'s equivalence class, and for each function application,
collect the function's equivalence class root. |
reinsertParents (e : Expr) : CCM Unit := do
let some ps := (← get).parents[e]? | return ()
for p in ps do
trace[Debug.Meta.Tactic.cc] "reinsert parent: {p.expr}"
if p.expr.isApp then
if p.symmTable then
addSymmCongruenceTable p.expr
else
addCongruenceTable p.expr | def | Tactic | [
"Mathlib.Data.Option.Defs",
"Mathlib.Tactic.CC.MkProof"
] | Mathlib/Tactic/CC/Addition.lean | reinsertParents | Reinsert parents of `e` to the congruence table and the symm congruence table.
Together with `removeParents`, this allows modifying parents of an expression. |
checkInvariant : CCM Unit := do
guard (← get).checkInvariant | def | Tactic | [
"Mathlib.Data.Option.Defs",
"Mathlib.Tactic.CC.MkProof"
] | Mathlib/Tactic/CC/Addition.lean | checkInvariant | Check for integrity of the `CCStructure`. |
propagateBetaToEqc (fnRoots lambdas : Array Expr) (newLambdaApps : Array Expr := #[]) :
CCM (Array Expr) := do
if lambdas.isEmpty then return newLambdaApps
let mut newLambdaApps := newLambdaApps
let lambdaRoot ← getRoot lambdas.back!
guard (← lambdas.allM fun l => pure l.isLambda <&&> (· == lambdaRoot) <$> getRoot l)
for fnRoot in fnRoots do
if let some ps := (← get).parents[fnRoot]? then
for { expr := p,.. } in ps do
let mut revArgs : Array Expr := #[]
let mut it₂ := p
while it₂.isApp do
let fn := it₂.appFn!
revArgs := revArgs.push it₂.appArg!
if (← getRoot fn) == lambdaRoot then
newLambdaApps ← propagateBeta fn revArgs lambdas newLambdaApps
break
it₂ := it₂.appFn!
return newLambdaApps | def | Tactic | [
"Mathlib.Data.Option.Defs",
"Mathlib.Tactic.CC.MkProof"
] | Mathlib/Tactic/CC/Addition.lean | propagateBetaToEqc | For each `fnRoot` in `fnRoots` traverse its parents, and look for a parent prefix that is
in the same equivalence class of the given lambdas.
remark All expressions in lambdas are in the same equivalence class |
propagateProjectionConstructor (p c : Expr) : CCM Unit := do
guard (← isConstructorApp c)
p.withApp fun pFn pArgs => do
let some pFnN := pFn.constName? | return
let some info ← getProjectionFnInfo? pFnN | return
let mkidx := info.numParams
if h : mkidx < pArgs.size then
unless ← isEqv (pArgs[mkidx]'h) c do return
unless ← pureIsDefEq (← inferType (pArgs[mkidx]'h)) (← inferType c) do return
/- Create new projection application using c (e.g., `(x, y).fst`), and internalize it.
The internalizer will add the new equality. -/
let pArgs := pArgs.set mkidx c
let newP := mkAppN pFn pArgs
internalizeCore newP none
else
return | def | Tactic | [
"Mathlib.Data.Option.Defs",
"Mathlib.Tactic.CC.MkProof"
] | Mathlib/Tactic/CC/Addition.lean | propagateProjectionConstructor | Given `c` a constructor application, if `p` is a projection application (not `.proj _ _ _`, but
`.app (.const projName _) _`) such that major premise is
equal to `c`, then propagate new equality.
Example: if `p` is of the form `b.fst`, `c` is of the form `(x, y)`, and `b = c`, we add the
equality `(x, y).fst = x` |
partial propagateConstructorEq (e₁ e₂ : Expr) : CCM Unit := do
let env ← getEnv
let some c₁ ← isConstructorApp? e₁ | failure
let some c₂ ← isConstructorApp? e₂ | failure
unless ← pureIsDefEq (← inferType e₁) (← inferType e₂) do
return
let some h ← getEqProof e₁ e₂ | failure
if c₁.name == c₂.name then
if 0 < c₁.numFields then
let name := mkInjectiveTheoremNameFor c₁.name
if env.contains name then
let rec
/-- Given an injective theorem `val : type`, whose `type` is the form of
`a₁ = a₂ ∧ b₁ ≍ b₂ ∧ ..`, destruct `val` and push equality proofs to the todo list. -/
go (type val : Expr) : CCM Unit := do
let push (type val : Expr) : CCM Unit :=
match type.eq? with
| some (_, lhs, rhs) => pushEq lhs rhs val
| none =>
match type.heq? with
| some (_, _, lhs, rhs) => pushHEq lhs rhs val
| none => failure
match type.and? with
| some (l, r) =>
push l (.proj ``And 0 val)
go r (.proj ``And 1 val)
| none =>
push type val
let val ← mkAppM name #[h]
let type ← inferType val
go type val
else
let falsePr ← mkNoConfusion (.const ``False []) h
let H := Expr.app (.const ``true_eq_false_of_false []) falsePr
pushEq (.const ``True []) (.const ``False []) H | def | Tactic | [
"Mathlib.Data.Option.Defs",
"Mathlib.Tactic.CC.MkProof"
] | Mathlib/Tactic/CC/Addition.lean | propagateConstructorEq | Given a new equality `e₁ = e₂`, where `e₁` and `e₂` are constructor applications.
Implement the following implications:
```lean
c a₁ ... aₙ = c b₁ ... bₙ => a₁ = b₁, ..., aₙ = bₙ
c₁ ... = c₂ ... => False
```
where `c`, `c₁` and `c₂` are constructors |
propagateValueInconsistency (e₁ e₂ : Expr) : CCM Unit := do
guard (← isInterpretedValue e₁)
guard (← isInterpretedValue e₂)
let some eqProof ← getEqProof e₁ e₂ | failure
let trueEqFalse ← mkEq (.const ``True []) (.const ``False [])
let neProof :=
Expr.app (.proj ``Iff 0 (← mkAppOptM ``bne_iff_ne #[none, none, none, e₁, e₂]))
(← mkEqRefl (.const ``true []))
let H ← mkAbsurd trueEqFalse eqProof neProof
pushEq (.const ``True []) (.const ``False []) H | def | Tactic | [
"Mathlib.Data.Option.Defs",
"Mathlib.Tactic.CC.MkProof"
] | Mathlib/Tactic/CC/Addition.lean | propagateValueInconsistency | Derive contradiction if we can get equality between different values. |
propagateAndDown (e : Expr) : CCM Unit := do
if ← isEqTrue e then
let some (a, b) := e.and? | failure
let h ← getEqTrueProof e
pushEq a (.const ``True []) (mkApp3 (.const ``eq_true_of_and_eq_true_left []) a b h)
pushEq b (.const ``True []) (mkApp3 (.const ``eq_true_of_and_eq_true_right []) a b h) | def | Tactic | [
"Mathlib.Data.Option.Defs",
"Mathlib.Tactic.CC.MkProof"
] | Mathlib/Tactic/CC/Addition.lean | propagateAndDown | Propagate equality from `a ∧ b = True` to `a = True` and `b = True`. |
propagateOrDown (e : Expr) : CCM Unit := do
if ← isEqFalse e then
let some (a, b) := e.app2? ``Or | failure
let h ← getEqFalseProof e
pushEq a (.const ``False []) (mkApp3 (.const ``eq_false_of_or_eq_false_left []) a b h)
pushEq b (.const ``False []) (mkApp3 (.const ``eq_false_of_or_eq_false_right []) a b h) | def | Tactic | [
"Mathlib.Data.Option.Defs",
"Mathlib.Tactic.CC.MkProof"
] | Mathlib/Tactic/CC/Addition.lean | propagateOrDown | Propagate equality from `a ∨ b = False` to `a = False` and `b = False`. |
propagateNotDown (e : Expr) : CCM Unit := do
if ← isEqTrue e then
let some a := e.not? | failure
pushEq a (.const ``False [])
(mkApp2 (.const ``eq_false_of_not_eq_true []) a (← getEqTrueProof e))
else if ← (·.em) <$> get <&&> isEqFalse e then
let some a := e.not? | failure
pushEq a (.const ``True [])
(mkApp2 (.const ``eq_true_of_not_eq_false []) a (← getEqFalseProof e)) | def | Tactic | [
"Mathlib.Data.Option.Defs",
"Mathlib.Tactic.CC.MkProof"
] | Mathlib/Tactic/CC/Addition.lean | propagateNotDown | Propagate equality from `¬a` to `a`. |
propagateEqDown (e : Expr) : CCM Unit := do
if ← isEqTrue e then
let some (a, b) := e.eqOrIff? | failure
pushEq a b (← mkAppM ``of_eq_true #[← getEqTrueProof e]) | def | Tactic | [
"Mathlib.Data.Option.Defs",
"Mathlib.Tactic.CC.MkProof"
] | Mathlib/Tactic/CC/Addition.lean | propagateEqDown | Propagate equality from `(a = b) = True` to `a = b`. |
propagateExistsDown (e : Expr) : CCM Unit := do
if ← isEqFalse e then
let hNotE ← mkAppM ``of_eq_false #[← getEqFalseProof e]
let (all, hAll) ← e.forallNot_of_notExists hNotE
internalizeCore all none
pushEq all (.const ``True []) (← mkEqTrue hAll) | def | Tactic | [
"Mathlib.Data.Option.Defs",
"Mathlib.Tactic.CC.MkProof"
] | Mathlib/Tactic/CC/Addition.lean | propagateExistsDown | Propagate equality from `¬∃ x, p x` to `∀ x, ¬p x`. |
propagateDown (e : Expr) : CCM Unit := do
if e.isAppOfArity ``And 2 then
propagateAndDown e
else if e.isAppOfArity ``Or 2 then
propagateOrDown e
else if e.isAppOfArity ``Not 1 then
propagateNotDown e
else if e.isEq || e.isAppOfArity ``Iff 2 then
propagateEqDown e
else if e.isAppOfArity ``Exists 2 then
propagateExistsDown e | def | Tactic | [
"Mathlib.Data.Option.Defs",
"Mathlib.Tactic.CC.MkProof"
] | Mathlib/Tactic/CC/Addition.lean | propagateDown | Propagate equality from `e` to subexpressions of `e`. |
addEqvStep (e₁ e₂ : Expr) (H : EntryExpr) (heqProof : Bool) : CCM Unit := do
let some n₁ ← getEntry e₁ | return -- `e₁` have not been internalized
let some n₂ ← getEntry e₂ | return -- `e₂` have not been internalized
if n₁.root == n₂.root then return -- they are already in the same equivalence class.
let some r₁ ← getEntry n₁.root | failure
let some r₂ ← getEntry n₂.root | failure
/-
We swap `(e₁,n₁,r₁)` with `(e₂,n₂,r₂)` when
1- `r₁.interpreted && !r₂.interpreted`.
Reason: to decide when to propagate we check whether the root of the equivalence class
is `True`/`False`. So, this condition is to make sure if `True`/`False` is in an equivalence
class, then one of them is the root. If both are, it doesn't matter, since the state is
inconsistent anyway.
2- `r₁.constructor && !r₂.interpreted && !r₂.constructor`
Reason: we want constructors to be the representative of their equivalence classes.
3- `r₁.size > r₂.size && !r₂.interpreted && !r₂.constructor`
Reason: performance.
-/
if (r₁.interpreted && !r₂.interpreted) ||
(r₁.constructor && !r₂.interpreted && !r₂.constructor) ||
(decide (r₁.size > r₂.size) && !r₂.interpreted && !r₂.constructor) then
go e₂ e₁ n₂ n₁ r₂ r₁ true H heqProof
else
go e₁ e₂ n₁ n₂ r₁ r₂ false H heqProof
where
/-- The auxiliary definition for `addEqvStep` to flip the input. -/
go (e₁ e₂: Expr) (n₁ n₂ r₁ r₂ : Entry) (flipped : Bool) (H : EntryExpr) (heqProof : Bool) :
CCM Unit := do
let mut valueInconsistency := false
if r₁.interpreted && r₂.interpreted then
if n₁.root.isConstOf ``True || n₂.root.isConstOf ``True then
modify fun ccs => { ccs with inconsistent := true }
else if n₁.root.int?.isSome && n₂.root.int?.isSome then
valueInconsistency := n₁.root.int? != n₂.root.int?
else
valueInconsistency := true
let e₁Root := n₁.root
let e₂Root := n₂.root
trace[Debug.Meta.Tactic.cc] "merging\n{e₁} ==> {e₁Root}\nwith\n{e₂Root} <== {e₂}"
/-
Following target/proof we have
`e₁ → ... → r₁`
`e₂ → ... → r₂`
We want
`r₁ → ... → e₁ → e₂ → ... → r₂`
-/
invertTrans e₁
let newN₁ : Entry :=
{ n₁ with
target := e₂
proof := H
... | def | Tactic | [
"Mathlib.Data.Option.Defs",
"Mathlib.Tactic.CC.MkProof"
] | Mathlib/Tactic/CC/Addition.lean | addEqvStep | Performs one step in the process when the new equation is added.
Here, `H` contains the proof that `e₁ = e₂` (if `heqProof` is false)
or `e₁ ≍ e₂` (if `heqProof` is true). |
processTodo : CCM Unit := do
repeat
let todo ← getTodo
let some (lhs, rhs, H, heqProof) := todo.back? | return
if (← get).inconsistent then
modifyTodo fun _ => #[]
return
modifyTodo Array.pop
addEqvStep lhs rhs H heqProof | def | Tactic | [
"Mathlib.Data.Option.Defs",
"Mathlib.Tactic.CC.MkProof"
] | Mathlib/Tactic/CC/Addition.lean | processTodo | Process the tasks in the `todo` field. |
internalize (e : Expr) : CCM Unit := do
internalizeCore e none
processTodo | def | Tactic | [
"Mathlib.Data.Option.Defs",
"Mathlib.Tactic.CC.MkProof"
] | Mathlib/Tactic/CC/Addition.lean | internalize | Internalize `e` so that the congruence closure can deal with the given expression. |
addEqvCore (lhs rhs H : Expr) (heqProof : Bool) : CCM Unit := do
pushTodo lhs rhs H heqProof
processTodo | def | Tactic | [
"Mathlib.Data.Option.Defs",
"Mathlib.Tactic.CC.MkProof"
] | Mathlib/Tactic/CC/Addition.lean | addEqvCore | Add `H : lhs = rhs` or `H : lhs ≍ rhs` to the congruence closure. Don't forget to internalize
`lhs` and `rhs` beforehand. |
add (type : Expr) (proof : Expr) : CCM Unit := do
if (← get).inconsistent then return
modifyTodo fun _ => #[]
let (isNeg, p) :=
match type with
| .app (.const ``Not []) a => (true, a)
| .forallE _ a (.const ``False []) _ => (true, a)
| .app (.app (.app (.const ``Ne [u]) α) lhs) rhs =>
(true, .app (.app (.app (.const ``Eq [u]) α) lhs) rhs)
| e => (false, e)
match p with
| .app (.app (.app (.const ``Eq _) _) lhs) rhs =>
if isNeg then
internalizeCore p none
addEqvCore p (.const ``False []) (← mkEqFalse proof) false
else
internalizeCore lhs none
internalizeCore rhs none
addEqvCore lhs rhs proof false
| .app (.app (.app (.app (.const ``HEq _) _) lhs) _) rhs =>
if isNeg then
internalizeCore p none
addEqvCore p (.const ``False []) (← mkEqFalse proof) false
else
internalizeCore lhs none
internalizeCore rhs none
addEqvCore lhs rhs proof true
| .app (.app (.const ``Iff _) lhs) rhs =>
if isNeg then
let neqProof ← mkAppM ``neq_of_not_iff #[proof]
internalizeCore p none
addEqvCore p (.const ``False []) (← mkEqFalse neqProof) false
else
internalizeCore lhs none
internalizeCore rhs none
addEqvCore lhs rhs (mkApp3 (.const ``propext []) lhs rhs proof) false
| _ =>
if ← pure isNeg <||> isProp p then
internalizeCore p none
if isNeg then
addEqvCore p (.const ``False []) (← mkEqFalse proof) false
else
addEqvCore p (.const ``True []) (← mkEqTrue proof) false | def | Tactic | [
"Mathlib.Data.Option.Defs",
"Mathlib.Tactic.CC.MkProof"
] | Mathlib/Tactic/CC/Addition.lean | add | Add `proof : type` to the congruence closure. |
isValue (e : Expr) : Bool :=
e.int?.isSome || e.isCharLit || e.isStringLit | def | Tactic | [
"Batteries.Classes.Order",
"Mathlib.Lean.Meta.Basic",
"Mathlib.Lean.Meta.CongrTheorems",
"Mathlib.Data.Ordering.Basic"
] | Mathlib/Tactic/CC/Datatypes.lean | isValue | Return true if `e` represents a constant value (numeral, character, or string). |
isInterpretedValue (e : Expr) : MetaM Bool := do
if e.isCharLit || e.isStringLit then
return true
else if e.int?.isSome then
let type ← inferType e
pureIsDefEq type (.const ``Nat []) <||> pureIsDefEq type (.const ``Int [])
else
return false | def | Tactic | [
"Batteries.Classes.Order",
"Mathlib.Lean.Meta.Basic",
"Mathlib.Lean.Meta.CongrTheorems",
"Mathlib.Data.Ordering.Basic"
] | Mathlib/Tactic/CC/Datatypes.lean | isInterpretedValue | Return true if `e` represents a value (nat/int numeral, character, or string).
In addition to the conditions in `Mathlib.Tactic.CC.isValue`, this also checks that
kernel computation can compare the values for equality. |
liftFromEq (R : Name) (H : Expr) : MetaM Expr := do
if R == ``Eq then return H
let HType ← whnf (← inferType H)
let some (A, a, _) := HType.eq?
| throwError "failed to build liftFromEq equality proof expected: {H}"
let motive ←
withLocalDeclD `x A fun x => do
let hType ← mkEq a x
withLocalDeclD `h hType fun h =>
mkRel R a x >>= mkLambdaFVars #[x, h]
let minor ← do
let mt ← mkRel R a a
let m ← mkFreshExprSyntheticOpaqueMVar mt
m.mvarId!.applyRfl
instantiateMVars m
mkEqRec motive minor H | def | Tactic | [
"Batteries.Classes.Order",
"Mathlib.Lean.Meta.Basic",
"Mathlib.Lean.Meta.CongrTheorems",
"Mathlib.Data.Ordering.Basic"
] | Mathlib/Tactic/CC/Datatypes.lean | liftFromEq | Given a reflexive relation `R`, and a proof `H : a = b`, build a proof for `R a b` |
ExprMap (α : Type u) := Std.TreeMap Expr α compare | abbrev | Tactic | [
"Batteries.Classes.Order",
"Mathlib.Lean.Meta.Basic",
"Mathlib.Lean.Meta.CongrTheorems",
"Mathlib.Data.Ordering.Basic"
] | Mathlib/Tactic/CC/Datatypes.lean | ExprMap | Ordering on `Expr`. -/
scoped instance : Ord Expr where
compare a b := bif Expr.lt a b then .lt else bif Expr.eqv b a then .eq else .gt
/-- Red-black maps whose keys are `Expr`s.
TODO: the choice between `TreeMap` and `HashMap` is not obvious:
once the `cc` tactic is used a lot in Mathlib, we should profile and see
if `HashMap` could be more optimal. |
ExprSet := Std.TreeSet Expr compare | abbrev | Tactic | [
"Batteries.Classes.Order",
"Mathlib.Lean.Meta.Basic",
"Mathlib.Lean.Meta.CongrTheorems",
"Mathlib.Data.Ordering.Basic"
] | Mathlib/Tactic/CC/Datatypes.lean | ExprSet | Red-black sets of `Expr`s.
TODO: the choice between `TreeSet` and `HashSet` is not obvious:
once the `cc` tactic is used a lot in Mathlib, we should profile and see
if `HashSet` could be more optimal. |
CCCongrTheorem extends CongrTheorem where
/-- If `heqResult` is true, then lemma is based on heterogeneous equality
and the conclusion is a heterogeneous equality. -/
heqResult : Bool := false
/-- If `hcongrTheorem` is true, then lemma was created using `mkHCongrWithArity`. -/
hcongrTheorem : Bool := false | structure | Tactic | [
"Batteries.Classes.Order",
"Mathlib.Lean.Meta.Basic",
"Mathlib.Lean.Meta.CongrTheorems",
"Mathlib.Data.Ordering.Basic"
] | Mathlib/Tactic/CC/Datatypes.lean | CCCongrTheorem | `CongrTheorem`s equipped with additional infos used by congruence closure modules. |
mkCCHCongrWithArity (fn : Expr) (nargs : Nat) : MetaM (Option CCCongrTheorem) := do
let eqCongr ← try mkHCongrWithArity fn nargs catch _ => return none
return some { eqCongr with
heqResult := true
hcongrTheorem := true } | def | Tactic | [
"Batteries.Classes.Order",
"Mathlib.Lean.Meta.Basic",
"Mathlib.Lean.Meta.CongrTheorems",
"Mathlib.Data.Ordering.Basic"
] | Mathlib/Tactic/CC/Datatypes.lean | mkCCHCongrWithArity | Automatically generated congruence lemma based on heterogeneous equality.
This returns an annotated version of the result from `Lean.Meta.mkHCongrWithArity`. |
CCCongrTheoremKey where
/-- The function of the given `CCCongrTheorem`. -/
fn : Expr
/-- The number of arguments of `fn`. -/
nargs : Nat
deriving BEq, Hashable | structure | Tactic | [
"Batteries.Classes.Order",
"Mathlib.Lean.Meta.Basic",
"Mathlib.Lean.Meta.CongrTheorems",
"Mathlib.Data.Ordering.Basic"
] | Mathlib/Tactic/CC/Datatypes.lean | CCCongrTheoremKey | Keys used to find corresponding `CCCongrTheorem`s. |
CCCongrTheoremCache := Std.HashMap CCCongrTheoremKey (Option CCCongrTheorem) | abbrev | Tactic | [
"Batteries.Classes.Order",
"Mathlib.Lean.Meta.Basic",
"Mathlib.Lean.Meta.CongrTheorems",
"Mathlib.Data.Ordering.Basic"
] | Mathlib/Tactic/CC/Datatypes.lean | CCCongrTheoremCache | Caches used to find corresponding `CCCongrTheorem`s. |
CCConfig where
/-- If `true`, congruence closure will treat implicit instance arguments as constants.
This means that setting `ignoreInstances := false` will fail to unify two definitionally equal
instances of the same class. -/
ignoreInstances : Bool := true
/-- If `true`, congruence closure modulo Associativity and Commutativity. -/
ac : Bool := true
/-- If `hoFns` is `some fns`, then full (and more expensive) support for higher-order functions is
*only* considered for the functions in fns and local functions. The performance overhead is
described in the paper "Congruence Closure in Intensional Type Theory". If `hoFns` is `none`,
then full support is provided for *all* constants. -/
hoFns : Option (List Name) := none
/-- If `true`, then use excluded middle -/
em : Bool := true
/-- If `true`, we treat values as atomic symbols -/
values : Bool := false
deriving Inhabited | structure | Tactic | [
"Batteries.Classes.Order",
"Mathlib.Lean.Meta.Basic",
"Mathlib.Lean.Meta.CongrTheorems",
"Mathlib.Data.Ordering.Basic"
] | Mathlib/Tactic/CC/Datatypes.lean | CCConfig | Configs used in congruence closure modules. |
ACApps where
/-- An `ACApps` of just an `Expr`. -/
| ofExpr (e : Expr) : ACApps
/-- An `ACApps` of applications of a binary operator. `args` are assumed to be sorted.
See also `ACApps.mkApps` if `args` are not yet sorted. -/
| apps (op : Expr) (args : Array Expr) : ACApps
deriving Inhabited, BEq | inductive | Tactic | [
"Batteries.Classes.Order",
"Mathlib.Lean.Meta.Basic",
"Mathlib.Lean.Meta.CongrTheorems",
"Mathlib.Data.Ordering.Basic"
] | Mathlib/Tactic/CC/Datatypes.lean | ACApps | An `ACApps` represents either just an `Expr` or applications of an associative and commutative
binary operator. |
ACApps.isSubset : (e₁ e₂ : ACApps) → Bool
| .ofExpr a, .ofExpr b => a == b
| .ofExpr a, .apps _ args => args.contains a
| .apps _ _, .ofExpr _ => false
| .apps op₁ args₁, .apps op₂ args₂ =>
if op₁ == op₂ then
if args₁.size ≤ args₂.size then Id.run do
let mut i₁ := 0
let mut i₂ := 0
while h : i₁ < args₁.size ∧ i₂ < args₂.size do
if args₁[i₁] == args₂[i₂] then
i₁ := i₁ + 1
i₂ := i₂ + 1
else if Expr.lt args₂[i₂] args₁[i₁] then
i₂ := i₂ + 1
else return false
return i₁ == args₁.size
else false
else false | def | Tactic | [
"Batteries.Classes.Order",
"Mathlib.Lean.Meta.Basic",
"Mathlib.Lean.Meta.CongrTheorems",
"Mathlib.Data.Ordering.Basic"
] | Mathlib/Tactic/CC/Datatypes.lean | ACApps.isSubset | Ordering on `ACApps` sorts `.ofExpr` before `.apps`, and sorts `.apps` by function symbol,
then by shortlex order. -/
scoped instance : Ord ACApps where
compare
| .ofExpr a, .ofExpr b => compare a b
| .ofExpr _, .apps _ _ => .lt
| .apps _ _, .ofExpr _ => .gt
| .apps op₁ args₁, .apps op₂ args₂ =>
compare op₁ op₂ |>.then <| compare args₁.size args₂.size |>.dthen fun hs => Id.run do
have hs := eq_of_beq <| Std.LawfulBEqCmp.compare_eq_iff_beq.mp hs
for hi : i in [:args₁.size] do
have hi := hi.right
let o := compare args₁[i] (args₂[i]'(hs ▸ hi.1))
if o != .eq then return o
return .eq
/-- Return true iff `e₁` is a "subset" of `e₂`.
Example: The result is `true` for `e₁ := a*a*a*b*d` and `e₂ := a*a*a*a*b*b*c*d*d`.
The result is also `true` for `e₁ := a` and `e₂ := a*a*a*b*c`. |
ACApps.diff (e₁ e₂ : ACApps) (r : Array Expr := #[]) : Array Expr :=
match e₁ with
| .apps op₁ args₁ => Id.run do
let mut r := r
match e₂ with
| .apps op₂ args₂ =>
if op₁ == op₂ then
let mut i₂ := 0
for h : i₁ in [:args₁.size] do
if i₂ == args₂.size then
r := r.push args₁[i₁]
else if args₁[i₁] == args₂[i₂]! then
i₂ := i₂ + 1
else
r := r.push args₁[i₁]
| .ofExpr e₂ =>
let mut found := false
for h : i in [:args₁.size] do
if !found && args₁[i] == e₂ then
found := true
else
r := r.push args₁[i]
return r
| .ofExpr e => if e₂ == e then r else r.push e | def | Tactic | [
"Batteries.Classes.Order",
"Mathlib.Lean.Meta.Basic",
"Mathlib.Lean.Meta.CongrTheorems",
"Mathlib.Data.Ordering.Basic"
] | Mathlib/Tactic/CC/Datatypes.lean | ACApps.diff | Appends elements of the set difference `e₁ \ e₂` to `r`.
Example: given `e₁ := a*a*a*a*b*b*c*d*d*d` and `e₂ := a*a*a*b*b*d`,
the result is `#[a, c, d, d]`
Precondition: `e₂.isSubset e₁` |
ACApps.append (op : Expr) (e : ACApps) (r : Array Expr := #[]) : Array Expr :=
match e with
| .apps op' args =>
if op' == op then r ++ args else r
| .ofExpr e =>
r.push e | def | Tactic | [
"Batteries.Classes.Order",
"Mathlib.Lean.Meta.Basic",
"Mathlib.Lean.Meta.CongrTheorems",
"Mathlib.Data.Ordering.Basic"
] | Mathlib/Tactic/CC/Datatypes.lean | ACApps.append | Appends arguments of `e` to `r`. |
ACApps.intersection (e₁ e₂ : ACApps) (r : Array Expr := #[]) : Array Expr :=
match e₁, e₂ with
| .apps _ args₁, .apps _ args₂ => Id.run do
let mut r := r
let mut i₁ := 0
let mut i₂ := 0
while h : i₁ < args₁.size ∧ i₂ < args₂.size do
if args₁[i₁] == args₂[i₂] then
r := r.push args₁[i₁]
i₁ := i₁ + 1
i₂ := i₂ + 1
else if Expr.lt args₂[i₂] args₁[i₁] then
i₂ := i₂ + 1
else
i₁ := i₁ + 1
return r
| _, _ => r | def | Tactic | [
"Batteries.Classes.Order",
"Mathlib.Lean.Meta.Basic",
"Mathlib.Lean.Meta.CongrTheorems",
"Mathlib.Data.Ordering.Basic"
] | Mathlib/Tactic/CC/Datatypes.lean | ACApps.intersection | Appends elements in the intersection of `e₁` and `e₂` to `r`. |
ACApps.mkApps (op : Expr) (args : Array Expr) : ACApps :=
.apps op (args.qsort Expr.lt) | def | Tactic | [
"Batteries.Classes.Order",
"Mathlib.Lean.Meta.Basic",
"Mathlib.Lean.Meta.CongrTheorems",
"Mathlib.Data.Ordering.Basic"
] | Mathlib/Tactic/CC/Datatypes.lean | ACApps.mkApps | Sorts `args` and applies them to `ACApps.apps`. |
ACApps.mkFlatApps (op : Expr) (e₁ e₂ : ACApps) : ACApps :=
let newArgs := ACApps.append op e₁
let newArgs := ACApps.append op e₂ newArgs
ACApps.mkApps op newArgs | def | Tactic | [
"Batteries.Classes.Order",
"Mathlib.Lean.Meta.Basic",
"Mathlib.Lean.Meta.CongrTheorems",
"Mathlib.Data.Ordering.Basic"
] | Mathlib/Tactic/CC/Datatypes.lean | ACApps.mkFlatApps | Flattens given two `ACApps`. |
ACApps.toExpr : ACApps → Option Expr
| .apps _ ⟨[]⟩ => none
| .apps op ⟨arg₀ :: args⟩ => some <| args.foldl (fun e arg => mkApp2 op e arg) arg₀
| .ofExpr e => some e | def | Tactic | [
"Batteries.Classes.Order",
"Mathlib.Lean.Meta.Basic",
"Mathlib.Lean.Meta.CongrTheorems",
"Mathlib.Data.Ordering.Basic"
] | Mathlib/Tactic/CC/Datatypes.lean | ACApps.toExpr | Converts an `ACApps` to an `Expr`. This returns `none` when the empty applications are given. |
ACAppsMap (α : Type u) := Std.TreeMap ACApps α compare | abbrev | Tactic | [
"Batteries.Classes.Order",
"Mathlib.Lean.Meta.Basic",
"Mathlib.Lean.Meta.CongrTheorems",
"Mathlib.Data.Ordering.Basic"
] | Mathlib/Tactic/CC/Datatypes.lean | ACAppsMap | Red-black maps whose keys are `ACApps`es.
TODO: the choice between `TreeMap` and `HashMap` is not obvious:
once the `cc` tactic is used a lot in Mathlib, we should profile and see
if `HashMap` could be more optimal. |
ACAppsSet := Std.TreeSet ACApps compare | abbrev | Tactic | [
"Batteries.Classes.Order",
"Mathlib.Lean.Meta.Basic",
"Mathlib.Lean.Meta.CongrTheorems",
"Mathlib.Data.Ordering.Basic"
] | Mathlib/Tactic/CC/Datatypes.lean | ACAppsSet | Red-black sets of `ACApps`es.
TODO: the choice between `TreeSet` and `HashSet` is not obvious:
once the `cc` tactic is used a lot in Mathlib, we should profile and see
if `HashSet` could be more optimal. |
DelayedExpr where
/-- A `DelayedExpr` of just an `Expr`. -/
| ofExpr (e : Expr) : DelayedExpr
/-- A placeholder as an equality proof between given two terms which will be generated by non-AC
congruence closure modules later. -/
| eqProof (lhs rhs : Expr) : DelayedExpr
/-- Will be applied to `congr_arg`. -/
| congrArg (f : Expr) (h : DelayedExpr) : DelayedExpr
/-- Will be applied to `congr_fun`. -/
| congrFun (h : DelayedExpr) (a : ACApps) : DelayedExpr
/-- Will be applied to `Eq.symm`. -/
| eqSymm (h : DelayedExpr) : DelayedExpr
/-- Will be applied to `Eq.symm`. -/
| eqSymmOpt (a₁ a₂ : ACApps) (h : DelayedExpr) : DelayedExpr
/-- Will be applied to `Eq.trans`. -/
| eqTrans (h₁ h₂ : DelayedExpr) : DelayedExpr
/-- Will be applied to `Eq.trans`. -/
| eqTransOpt (a₁ a₂ a₃ : ACApps) (h₁ h₂ : DelayedExpr) : DelayedExpr
/-- Will be applied to `heq_of_eq`. -/
| heqOfEq (h : DelayedExpr) : DelayedExpr
/-- Will be applied to `HEq.symm`. -/
| heqSymm (h : DelayedExpr) : DelayedExpr
deriving Inhabited | inductive | Tactic | [
"Batteries.Classes.Order",
"Mathlib.Lean.Meta.Basic",
"Mathlib.Lean.Meta.CongrTheorems",
"Mathlib.Data.Ordering.Basic"
] | Mathlib/Tactic/CC/Datatypes.lean | DelayedExpr | For proof terms generated by AC congruence closure modules, we want a placeholder as an equality
proof between given two terms which will be generated by non-AC congruence closure modules later.
`DelayedExpr` represents it using `eqProof`. |
EntryExpr
/-- An `EntryExpr` of just an `Expr`. -/
| ofExpr (e : Expr) : EntryExpr
/-- dummy congruence proof, it is just a placeholder. -/
| congr : EntryExpr
/-- dummy eq_true proof, it is just a placeholder -/
| eqTrue : EntryExpr
/-- dummy refl proof, it is just a placeholder. -/
| refl : EntryExpr
/-- An `EntryExpr` of a `DelayedExpr`. -/
| ofDExpr (e : DelayedExpr) : EntryExpr
deriving Inhabited | inductive | Tactic | [
"Batteries.Classes.Order",
"Mathlib.Lean.Meta.Basic",
"Mathlib.Lean.Meta.CongrTheorems",
"Mathlib.Data.Ordering.Basic"
] | Mathlib/Tactic/CC/Datatypes.lean | EntryExpr | This is used as a proof term in `Entry`s instead of `Expr`. |
Entry where
/-- next element in the equivalence class. -/
next : Expr
/-- root (aka canonical) representative of the equivalence class. -/
root : Expr
/-- root of the congruence class, it is meaningless if `e` is not an application. -/
cgRoot : Expr
/-- When `e` was added to this equivalence class because of an equality `(H : e = tgt)`, then
we store `tgt` at `target`, and `H` at `proof`. Both fields are none if `e == root` -/
target : Option Expr := none
/-- When `e` was added to this equivalence class because of an equality `(H : e = tgt)`, then
we store `tgt` at `target`, and `H` at `proof`. Both fields are none if `e == root` -/
proof : Option EntryExpr := none
/-- Variable in the AC theory. -/
acVar : Option Expr := none
/-- proof has been flipped -/
flipped : Bool
/-- `true` if the node should be viewed as an abstract value -/
interpreted : Bool
/-- `true` if head symbol is a constructor -/
constructor : Bool
/-- `true` if equivalence class contains lambda expressions -/
hasLambdas : Bool
/-- `heqProofs == true` iff some proofs in the equivalence class are based on heterogeneous
equality. We represent equality and heterogeneous equality in a single equivalence class. -/
heqProofs : Bool
/-- If `fo == true`, then the expression associated with this entry is an application, and we are
using first-order approximation to encode it. That is, we ignore its partial applications. -/
fo : Bool
/-- number of elements in the equivalence class, it is meaningless if `e != root` -/
size : Nat
/-- The field `mt` is used to implement the mod-time optimization introduce by the Simplify
theorem prover. The basic idea is to introduce a counter gmt that records the number of
heuristic instantiation that have occurred in the current branch. It is incremented after each
round of heuristic instantiation. The field `mt` records the last time any proper descendant
of this entry was involved in a merge. -/
mt : Nat
deriving Inhabited | structure | Tactic | [
"Batteries.Classes.Order",
"Mathlib.Lean.Meta.Basic",
"Mathlib.Lean.Meta.CongrTheorems",
"Mathlib.Data.Ordering.Basic"
] | Mathlib/Tactic/CC/Datatypes.lean | Entry | Equivalence class data associated with an expression `e`. |
Entries := ExprMap Entry | abbrev | Tactic | [
"Batteries.Classes.Order",
"Mathlib.Lean.Meta.Basic",
"Mathlib.Lean.Meta.CongrTheorems",
"Mathlib.Data.Ordering.Basic"
] | Mathlib/Tactic/CC/Datatypes.lean | Entries | Stores equivalence class data associated with an expression `e`. |
ACEntry where
/-- Natural number associated to an expression. -/
idx : Nat
/-- AC variables that occur on the left-hand side of an equality which `e` occurs as the left-hand
side of in `CCState.acR`. -/
RLHSOccs : ACAppsSet := ∅
/-- AC variables that occur on the **left**-hand side of an equality which `e` occurs as the
right-hand side of in `CCState.acR`. Don't confuse. -/
RRHSOccs : ACAppsSet := ∅
deriving Inhabited | structure | Tactic | [
"Batteries.Classes.Order",
"Mathlib.Lean.Meta.Basic",
"Mathlib.Lean.Meta.CongrTheorems",
"Mathlib.Data.Ordering.Basic"
] | Mathlib/Tactic/CC/Datatypes.lean | ACEntry | Equivalence class data associated with an expression `e` used by AC congruence closure
modules. |
ACEntry.ROccs (ent : ACEntry) : (inLHS : Bool) → ACAppsSet
| true => ent.RLHSOccs
| false => ent.RRHSOccs | def | Tactic | [
"Batteries.Classes.Order",
"Mathlib.Lean.Meta.Basic",
"Mathlib.Lean.Meta.CongrTheorems",
"Mathlib.Data.Ordering.Basic"
] | Mathlib/Tactic/CC/Datatypes.lean | ACEntry.ROccs | Returns the occurrences of this entry in either the LHS or RHS. |
ParentOcc where
expr : Expr
/-- If `symmTable` is true, then we should use the `symmCongruences`, otherwise `congruences`.
Remark: this information is redundant, it can be inferred from `expr`. We use store it for
performance reasons. -/
symmTable : Bool | structure | Tactic | [
"Batteries.Classes.Order",
"Mathlib.Lean.Meta.Basic",
"Mathlib.Lean.Meta.CongrTheorems",
"Mathlib.Data.Ordering.Basic"
] | Mathlib/Tactic/CC/Datatypes.lean | ParentOcc | Used to record when an expression processed by `cc` occurs in another expression. |
ParentOccSet := Std.TreeSet ParentOcc (Ordering.byKey ParentOcc.expr compare) | abbrev | Tactic | [
"Batteries.Classes.Order",
"Mathlib.Lean.Meta.Basic",
"Mathlib.Lean.Meta.CongrTheorems",
"Mathlib.Data.Ordering.Basic"
] | Mathlib/Tactic/CC/Datatypes.lean | ParentOccSet | Red-black sets of `ParentOcc`s. |
Parents := ExprMap ParentOccSet | abbrev | Tactic | [
"Batteries.Classes.Order",
"Mathlib.Lean.Meta.Basic",
"Mathlib.Lean.Meta.CongrTheorems",
"Mathlib.Data.Ordering.Basic"
] | Mathlib/Tactic/CC/Datatypes.lean | Parents | Used to map an expression `e` to another expression that contains `e`.
When `e` is normalized, its parents should also change. |
CongruencesKey
/-- `fn` is First-Order: we do not consider all partial applications. -/
| fo (fn : Expr) (args : Array Expr) : CongruencesKey
/-- `fn` is Higher-Order. -/
| ho (fn : Expr) (arg : Expr) : CongruencesKey
deriving BEq, Hashable | inductive | Tactic | [
"Batteries.Classes.Order",
"Mathlib.Lean.Meta.Basic",
"Mathlib.Lean.Meta.CongrTheorems",
"Mathlib.Data.Ordering.Basic"
] | Mathlib/Tactic/CC/Datatypes.lean | CongruencesKey | null |
Congruences := Std.HashMap CongruencesKey (List Expr) | abbrev | Tactic | [
"Batteries.Classes.Order",
"Mathlib.Lean.Meta.Basic",
"Mathlib.Lean.Meta.CongrTheorems",
"Mathlib.Data.Ordering.Basic"
] | Mathlib/Tactic/CC/Datatypes.lean | Congruences | Maps each expression (via `mkCongruenceKey`) to expressions it might be congruent to. |
SymmCongruencesKey where
(h₁ h₂ : Expr)
deriving BEq, Hashable | structure | Tactic | [
"Batteries.Classes.Order",
"Mathlib.Lean.Meta.Basic",
"Mathlib.Lean.Meta.CongrTheorems",
"Mathlib.Data.Ordering.Basic"
] | Mathlib/Tactic/CC/Datatypes.lean | SymmCongruencesKey | null |
SymmCongruences := Std.HashMap SymmCongruencesKey (List (Expr × Name)) | abbrev | Tactic | [
"Batteries.Classes.Order",
"Mathlib.Lean.Meta.Basic",
"Mathlib.Lean.Meta.CongrTheorems",
"Mathlib.Data.Ordering.Basic"
] | Mathlib/Tactic/CC/Datatypes.lean | SymmCongruences | The symmetric variant of `Congruences`.
The `Name` identifies which relation the congruence is considered for.
Note that this only works for two-argument relations: `ModEq n` and `ModEq m` are considered the
same. |
SubsingletonReprs := ExprMap Expr | abbrev | Tactic | [
"Batteries.Classes.Order",
"Mathlib.Lean.Meta.Basic",
"Mathlib.Lean.Meta.CongrTheorems",
"Mathlib.Data.Ordering.Basic"
] | Mathlib/Tactic/CC/Datatypes.lean | SubsingletonReprs | Stores the root representatives of subsingletons, this uses `FastSingleton` instead of
`Subsingleton`. |
InstImplicitReprs := ExprMap (List Expr) | abbrev | Tactic | [
"Batteries.Classes.Order",
"Mathlib.Lean.Meta.Basic",
"Mathlib.Lean.Meta.CongrTheorems",
"Mathlib.Data.Ordering.Basic"
] | Mathlib/Tactic/CC/Datatypes.lean | InstImplicitReprs | Stores the root representatives of `.instImplicit` arguments. |
TodoEntry := Expr × Expr × EntryExpr × Bool | abbrev | Tactic | [
"Batteries.Classes.Order",
"Mathlib.Lean.Meta.Basic",
"Mathlib.Lean.Meta.CongrTheorems",
"Mathlib.Data.Ordering.Basic"
] | Mathlib/Tactic/CC/Datatypes.lean | TodoEntry | null |
ACTodoEntry := ACApps × ACApps × DelayedExpr | abbrev | Tactic | [
"Batteries.Classes.Order",
"Mathlib.Lean.Meta.Basic",
"Mathlib.Lean.Meta.CongrTheorems",
"Mathlib.Data.Ordering.Basic"
] | Mathlib/Tactic/CC/Datatypes.lean | ACTodoEntry | null |
CCState extends CCConfig where
/-- Maps known expressions to their equivalence class data. -/
entries : Entries := ∅
/-- Maps an expression `e` to the expressions `e` occurs in. -/
parents : Parents := ∅
/-- Maps each expression to a set of expressions it might be congruent to. -/
congruences : Congruences := ∅
/-- Maps each expression to a set of expressions it might be congruent to,
via the symmetrical relation. -/
symmCongruences : SymmCongruences := ∅
subsingletonReprs : SubsingletonReprs := ∅
/-- Records which instances of the same class are defeq. -/
instImplicitReprs : InstImplicitReprs := ∅
/-- The congruence closure module has a mode where the root of each equivalence class is marked as
an interpreted/abstract value. Moreover, in this mode proof production is disabled.
This capability is useful for heuristic instantiation. -/
frozePartitions : Bool := false
/-- Mapping from operators occurring in terms and their canonical
representation in this module -/
canOps : ExprMap Expr := ∅
/-- Whether the canonical operator is supported by AC. -/
opInfo : ExprMap Bool := ∅
/-- Extra `Entry` information used by the AC part of the tactic. -/
acEntries : ExprMap ACEntry := ∅
/-- Records equality between `ACApps`. -/
acR : ACAppsMap (ACApps × DelayedExpr) := ∅
/-- Returns true if the `CCState` is inconsistent. For example if it had both `a = b` and `a ≠ b`
in it. -/
inconsistent : Bool := false
/-- "Global Modification Time". gmt is a number stored on the `CCState`,
it is compared with the modification time of a cc_entry in e-matching. See `CCState.mt`. -/
gmt : Nat := 0
deriving Inhabited
attribute [inherit_doc SubsingletonReprs] CCState.subsingletonReprs | structure | Tactic | [
"Batteries.Classes.Order",
"Mathlib.Lean.Meta.Basic",
"Mathlib.Lean.Meta.CongrTheorems",
"Mathlib.Data.Ordering.Basic"
] | Mathlib/Tactic/CC/Datatypes.lean | CCState | Congruence closure state.
This may be considered to be a set of expressions and an equivalence class over this set.
The equivalence class is generated by the equational rules that are added to the `CCState` and
congruence, that is, if `a = b` then `f(a) = f(b)` and so on. |
CCState.mkEntryCore (ccs : CCState) (e : Expr) (interpreted : Bool) (constructor : Bool) :
CCState :=
assert! ccs.entries[e]? |>.isNone
let n : Entry :=
{ next := e
root := e
cgRoot := e
size := 1
flipped := false
interpreted
constructor
hasLambdas := e.isLambda
heqProofs := false
mt := ccs.gmt
fo := false }
{ ccs with entries := ccs.entries.insert e n } | def | Tactic | [
"Batteries.Classes.Order",
"Mathlib.Lean.Meta.Basic",
"Mathlib.Lean.Meta.CongrTheorems",
"Mathlib.Data.Ordering.Basic"
] | Mathlib/Tactic/CC/Datatypes.lean | CCState.mkEntryCore | Update the `CCState` by constructing and inserting a new `Entry`. |
root (ccs : CCState) (e : Expr) : Expr :=
match ccs.entries[e]? with
| some n => n.root
| none => e | def | Tactic | [
"Batteries.Classes.Order",
"Mathlib.Lean.Meta.Basic",
"Mathlib.Lean.Meta.CongrTheorems",
"Mathlib.Data.Ordering.Basic"
] | Mathlib/Tactic/CC/Datatypes.lean | root | Get the root representative of the given expression. |
next (ccs : CCState) (e : Expr) : Expr :=
match ccs.entries[e]? with
| some n => n.next
| none => e | def | Tactic | [
"Batteries.Classes.Order",
"Mathlib.Lean.Meta.Basic",
"Mathlib.Lean.Meta.CongrTheorems",
"Mathlib.Data.Ordering.Basic"
] | Mathlib/Tactic/CC/Datatypes.lean | next | Get the next element in the equivalence class.
Note that if the given `Expr` `e` is not in the graph then it will just return `e`. |
isCgRoot (ccs : CCState) (e : Expr) : Bool :=
match ccs.entries[e]? with
| some n => e == n.cgRoot
| none => true | def | Tactic | [
"Batteries.Classes.Order",
"Mathlib.Lean.Meta.Basic",
"Mathlib.Lean.Meta.CongrTheorems",
"Mathlib.Data.Ordering.Basic"
] | Mathlib/Tactic/CC/Datatypes.lean | isCgRoot | Check if `e` is the root of the congruence class. |
mt (ccs : CCState) (e : Expr) : Nat :=
match ccs.entries[e]? with
| some n => n.mt
| none => ccs.gmt | def | Tactic | [
"Batteries.Classes.Order",
"Mathlib.Lean.Meta.Basic",
"Mathlib.Lean.Meta.CongrTheorems",
"Mathlib.Data.Ordering.Basic"
] | Mathlib/Tactic/CC/Datatypes.lean | mt | "Modification Time". The field `mt` is used to implement the mod-time optimization introduced by the
Simplify theorem prover. The basic idea is to introduce a counter `gmt` that records the number of
heuristic instantiation that have occurred in the current branch. It is incremented after each round
of heuristic instantiation. The field `mt` records the last time any proper descendant of this
entry was involved in a merge. |
inSingletonEqc (ccs : CCState) (e : Expr) : Bool :=
match ccs.entries[e]? with
| some it => it.next == e
| none => true | def | Tactic | [
"Batteries.Classes.Order",
"Mathlib.Lean.Meta.Basic",
"Mathlib.Lean.Meta.CongrTheorems",
"Mathlib.Data.Ordering.Basic"
] | Mathlib/Tactic/CC/Datatypes.lean | inSingletonEqc | Is the expression in an equivalence class with only one element (namely, itself)? |
getRoots (ccs : CCState) (roots : Array Expr) (nonsingletonOnly : Bool) : Array Expr :=
Id.run do
let mut roots := roots
for (k, n) in ccs.entries do
if k == n.root && (!nonsingletonOnly || !ccs.inSingletonEqc k) then
roots := roots.push k
return roots | def | Tactic | [
"Batteries.Classes.Order",
"Mathlib.Lean.Meta.Basic",
"Mathlib.Lean.Meta.CongrTheorems",
"Mathlib.Data.Ordering.Basic"
] | Mathlib/Tactic/CC/Datatypes.lean | getRoots | Append to `roots` all the roots of equivalence classes in `ccs`.
If `nonsingletonOnly` is true, we skip all the singleton equivalence classes. |
checkEqc (ccs : CCState) (e : Expr) : Bool :=
toBool <| Id.run <| OptionT.run do
let root := ccs.root e
let mut size : Nat := 0
let mut it := e
repeat
let some itN := ccs.entries[it]? | failure
guard (itN.root == root)
let mut it₂ := it
repeat
let it₂N := ccs.entries[it₂]?
match it₂N.bind Entry.target with
| some it₃ => it₂ := it₃
| none => break
guard (it₂ == root)
it := itN.next
size := size + 1
until it == e
guard (ccs.entries[root]? |>.any (·.size == size)) | def | Tactic | [
"Batteries.Classes.Order",
"Mathlib.Lean.Meta.Basic",
"Mathlib.Lean.Meta.CongrTheorems",
"Mathlib.Data.Ordering.Basic"
] | Mathlib/Tactic/CC/Datatypes.lean | checkEqc | Check for integrity of the `CCState`. |
checkInvariant (ccs : CCState) : Bool :=
ccs.entries.all fun k n => k != n.root || checkEqc ccs k | def | Tactic | [
"Batteries.Classes.Order",
"Mathlib.Lean.Meta.Basic",
"Mathlib.Lean.Meta.CongrTheorems",
"Mathlib.Data.Ordering.Basic"
] | Mathlib/Tactic/CC/Datatypes.lean | checkInvariant | Check for integrity of the `CCState`. |
getNumROccs (ccs : CCState) (e : Expr) (inLHS : Bool) : Nat :=
match ccs.acEntries[e]? with
| some ent => (ent.ROccs inLHS).size
| none => 0 | def | Tactic | [
"Batteries.Classes.Order",
"Mathlib.Lean.Meta.Basic",
"Mathlib.Lean.Meta.CongrTheorems",
"Mathlib.Data.Ordering.Basic"
] | Mathlib/Tactic/CC/Datatypes.lean | getNumROccs | null |
getVarWithLeastOccs (ccs : CCState) (e : ACApps) (inLHS : Bool) : Option Expr :=
match e with
| .apps _ args => Id.run do
let mut r := args[0]?
let mut numOccs := r.casesOn 0 fun r' => ccs.getNumROccs r' inLHS
for hi : i in [1:args.size] do
if args[i] != (args[i - 1]'(Nat.lt_of_le_of_lt (i.sub_le 1) hi.2.1)) then
let currOccs := ccs.getNumROccs args[i] inLHS
if currOccs < numOccs then
r := args[i]
numOccs := currOccs
return r
| .ofExpr e => e | def | Tactic | [
"Batteries.Classes.Order",
"Mathlib.Lean.Meta.Basic",
"Mathlib.Lean.Meta.CongrTheorems",
"Mathlib.Data.Ordering.Basic"
] | Mathlib/Tactic/CC/Datatypes.lean | getVarWithLeastOccs | Search for the AC-variable (`Entry.acVar`) with the least occurrences in the state. |
getVarWithLeastLHSOccs (ccs : CCState) (e : ACApps) : Option Expr :=
ccs.getVarWithLeastOccs e true | def | Tactic | [
"Batteries.Classes.Order",
"Mathlib.Lean.Meta.Basic",
"Mathlib.Lean.Meta.CongrTheorems",
"Mathlib.Data.Ordering.Basic"
] | Mathlib/Tactic/CC/Datatypes.lean | getVarWithLeastLHSOccs | Search for the AC-variable (`Entry.acVar`) with the fewest occurrences in the LHS. |
getVarWithLeastRHSOccs (ccs : CCState) (e : ACApps) : Option Expr :=
ccs.getVarWithLeastOccs e false
open MessageData | def | Tactic | [
"Batteries.Classes.Order",
"Mathlib.Lean.Meta.Basic",
"Mathlib.Lean.Meta.CongrTheorems",
"Mathlib.Data.Ordering.Basic"
] | Mathlib/Tactic/CC/Datatypes.lean | getVarWithLeastRHSOccs | Search for the AC-variable (`Entry.acVar`) with the fewest occurrences in the RHS. |
ppEqc (ccs : CCState) (e : Expr) : MessageData := Id.run do
let mut lr : List MessageData := []
let mut it := e
repeat
let some itN := ccs.entries[it]? | break
let mdIt : MessageData :=
if it.isForall || it.isLambda || it.isLet then paren (ofExpr it) else ofExpr it
lr := mdIt :: lr
it := itN.next
until it == e
let l := lr.reverse
return bracket "{" (group <| joinSep l (ofFormat ("," ++ .line))) "}" | def | Tactic | [
"Batteries.Classes.Order",
"Mathlib.Lean.Meta.Basic",
"Mathlib.Lean.Meta.CongrTheorems",
"Mathlib.Data.Ordering.Basic"
] | Mathlib/Tactic/CC/Datatypes.lean | ppEqc | Pretty print the entry associated with the given expression. |
ppEqcs (ccs : CCState) (nonSingleton : Bool := true) : MessageData :=
let roots := ccs.getRoots #[] nonSingleton
let a := roots.map (fun root => ccs.ppEqc root)
let l := a.toList
bracket "{" (group <| joinSep l (ofFormat ("," ++ .line))) "}" | def | Tactic | [
"Batteries.Classes.Order",
"Mathlib.Lean.Meta.Basic",
"Mathlib.Lean.Meta.CongrTheorems",
"Mathlib.Data.Ordering.Basic"
] | Mathlib/Tactic/CC/Datatypes.lean | ppEqcs | Pretty print the entire cc graph.
If the `nonSingleton` argument is set to `true` then singleton equivalence classes will be
omitted. |
ppParentOccsAux (ccs : CCState) (e : Expr) : MessageData :=
match ccs.parents[e]? with
| some poccs =>
let r := ofExpr e ++ ofFormat (.line ++ ":=" ++ .line)
let ps := poccs.toList.map fun o => ofExpr o.expr
group (r ++ bracket "{" (group <| joinSep ps (ofFormat ("," ++ .line))) "}")
| none => ofFormat .nil | def | Tactic | [
"Batteries.Classes.Order",
"Mathlib.Lean.Meta.Basic",
"Mathlib.Lean.Meta.CongrTheorems",
"Mathlib.Data.Ordering.Basic"
] | Mathlib/Tactic/CC/Datatypes.lean | ppParentOccsAux | null |
ppParentOccs (ccs : CCState) : MessageData :=
let r := ccs.parents.toList.map fun (k, _) => ccs.ppParentOccsAux k
bracket "{" (group <| joinSep r (ofFormat ("," ++ .line))) "}" | def | Tactic | [
"Batteries.Classes.Order",
"Mathlib.Lean.Meta.Basic",
"Mathlib.Lean.Meta.CongrTheorems",
"Mathlib.Data.Ordering.Basic"
] | Mathlib/Tactic/CC/Datatypes.lean | ppParentOccs | null |
ppACDecl (ccs : CCState) (e : Expr) : MessageData :=
match ccs.acEntries[e]? with
| some it => group (ofFormat (s!"x_{it.idx}" ++ .line ++ ":=" ++ .line) ++ ofExpr e)
| none => nil | def | Tactic | [
"Batteries.Classes.Order",
"Mathlib.Lean.Meta.Basic",
"Mathlib.Lean.Meta.CongrTheorems",
"Mathlib.Data.Ordering.Basic"
] | Mathlib/Tactic/CC/Datatypes.lean | ppACDecl | null |
ppACDecls (ccs : CCState) : MessageData :=
let r := ccs.acEntries.toList.map fun (k, _) => ccs.ppACDecl k
bracket "{" (joinSep r (ofFormat ("," ++ .line))) "}" | def | Tactic | [
"Batteries.Classes.Order",
"Mathlib.Lean.Meta.Basic",
"Mathlib.Lean.Meta.CongrTheorems",
"Mathlib.Data.Ordering.Basic"
] | Mathlib/Tactic/CC/Datatypes.lean | ppACDecls | null |
ppACExpr (ccs : CCState) (e : Expr) : MessageData :=
if let some it := ccs.acEntries[e]? then
s!"x_{it.idx}"
else
ofExpr e | def | Tactic | [
"Batteries.Classes.Order",
"Mathlib.Lean.Meta.Basic",
"Mathlib.Lean.Meta.CongrTheorems",
"Mathlib.Data.Ordering.Basic"
] | Mathlib/Tactic/CC/Datatypes.lean | ppACExpr | null |
partial ppACApps (ccs : CCState) : ACApps → MessageData
| .apps op args =>
let r := ofExpr op :: args.toList.map fun arg => ccs.ppACExpr arg
sbracket (joinSep r (ofFormat .line))
| .ofExpr e => ccs.ppACExpr e | def | Tactic | [
"Batteries.Classes.Order",
"Mathlib.Lean.Meta.Basic",
"Mathlib.Lean.Meta.CongrTheorems",
"Mathlib.Data.Ordering.Basic"
] | Mathlib/Tactic/CC/Datatypes.lean | ppACApps | null |
ppACR (ccs : CCState) : MessageData :=
let r := ccs.acR.toList.map fun (k, p, _) => group <|
ccs.ppACApps k ++ ofFormat (Format.line ++ "--> ") ++ nest 4 (Format.line ++ ccs.ppACApps p)
bracket "{" (joinSep r (ofFormat ("," ++ .line))) "}" | def | Tactic | [
"Batteries.Classes.Order",
"Mathlib.Lean.Meta.Basic",
"Mathlib.Lean.Meta.CongrTheorems",
"Mathlib.Data.Ordering.Basic"
] | Mathlib/Tactic/CC/Datatypes.lean | ppACR | null |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.