Repository navigation
feat: add confluence predicates for relations - #40368
Sanghyeok0 wants to merge 14 commits into
Conversation
Welcome new contributor!Thank you for contributing to Mathlib! If you haven't done so already, please review our contribution guidelines, as well as the style guide and naming conventions. In particular, we kindly remind contributors that we have guidelines regarding the use of AI when making pull requests. We use a review queue to manage reviews. If your PR does not appear there, it is probably because it is not successfully building (i.e., it doesn't have a green checkmark), has the If you haven't already done so, please come to https://leanprover.zulipchat.com/, introduce yourself, and mention your new PR. Thank you again for joining our community. |
PR summary eed83db7a5Import changes for modified filesNo significant changes to the import graph Import changes for all files
|
|
Hi! Please note that we also have this in CSLib in a touch more detail. I'd not made an attempt to upstream anything since it was suggested on Zulip that this was at the edge of Mathlib's scope. I'm working on some more relation properties at the moment, so please feel welcome to discuss in the CSLib channel if you have more interest in this area. |
Thanks for the pointer! I had noticed CSLib, but I had not realized that it already contains this much relation-level rewriting API. For context, my downstream goal is polynomial reduction for For example, the intended setup has the following shape: def MonomialOrder.ReducesToSet
(m : MonomialOrder σ)
(P : Set (MvPolynomial σ R))
(f g : MvPolynomial σ R) : Prop :=
∃ p ∈ P, m.ReducesToPoly p f g
scoped[Reduction] notation:50 f:51 " ⟶*[" m ", " P "] " g:51 =>
Relation.ReflTransGen (MonomialOrder.ReducesToSet m P) f g
scoped[Reduction] notation:50 f:51 " ⟷*[" m ", " P "] " g:51 =>
Relation.EqvGen (MonomialOrder.ReducesToSet m P) f g
scoped[Reduction] notation:50 f:51 " ↓[" m ", " P "] " g:51 =>
Relation.Join
(Relation.ReflTransGen (MonomialOrder.ReducesToSet m P)) f gThen the polynomial-specific theorems are naturally stated using the general relation predicates, for example: theorem MonomialOrder.ReducesToSet.wellFounded
(P : Set (MvPolynomial σ R)) :
WellFounded (flip (m.ReducesToSet P))
theorem MonomialOrder.ReducesToSet.locallyConfluent_singleton
{p : MvPolynomial σ R}
(hpLC : IsUnit (m.leadingCoeff p)) :
Relation.LocallyConfluent
(m.ReducesToSet ({p} : Set (MvPolynomial σ R)))and later one wants to use So my motivation for this PR was to add only a small relation-level layer around the existing mathlib theorem I also have a WIP development continuing from this polynomial-reduction layer up to Buchberger's criterion. I would be happy to share the branch or a more detailed outline if that would help clarify which relation-level declarations are actually needed downstream, and which parts would be better kept polynomial-specific or discussed first in CSLib. That said, I understand the concern about overlap with CSLib. If the Would it be useful for me to ask about the intended boundary between mathlib and CSLib for this particular use case on Lean Zulip, perhaps in the existing |
|
Thanks for the context. Looking at master...Sanghyeok0:mathlib4:relation-normal-forms I see a lot more overlap, so I think discussing on Zulip is a good idea. I would ask about this in the |
|
This pull request has conflicts, please merge |
f7a64d0 to
a35d1de
Compare
SnirBroshi
left a comment
There was a problem hiding this comment.
Hey! Could we use the Is prefix to indicate these are Props?
IsDiamond / IsConfluent / IsChurchRosser
|
Oh also, could you make these classes, like other relation properties? |
625ba50 to
12650f2
Compare
e0062e7 to
12650f2
Compare
I'll ask more directly: are you using an LLM to write your GitHub comments? |
For the code itself, I disclosed my use of AI in the PR comment, as you may have already seen. |
|
@Sanghyeok0 Could you click the "resolve conversation" button on those reviews / suggestions you have resolved / applied? I think this would make it easier to figure out the current status and focus on things to be done. |
Thank you! I haven't resolved two of them because I think we need more discussion. |
| /-- **Church-Rosser theorem** for word reduction: If `w1 w2 w3` are words such that `w1` reduces | ||
| to `w2` and `w3` respectively, then there is a word `w4` such that `w2` and `w3` reduce to `w4` | ||
| respectively. This is also known as Newman's diamond lemma. -/] | ||
| theorem church_rosser : Red L₁ L₂ → Red L₁ L₃ → Join Red L₂ L₃ := |
There was a problem hiding this comment.
This looks like it should be
| theorem church_rosser : Red L₁ L₂ → Red L₁ L₃ → Join Red L₂ L₃ := | |
| instance : Relation.IsDiamond Red := |
which fits the name "Newman's diamond lemma".
That should probably be a followup PR, since there are some usages of this.
There was a problem hiding this comment.
I'll leave the instance conversion for a followup PR and keep the existing theorem statement in this PR.
This refactor was split out from #40368 following the review discussion there. Following further review, I moved all lemmas from the `Relation.EqvGen` namespace to `Relation`, except for `is_equivalence`, `setoid`, and `mono`, which retain their original names. The old names are preserved as deprecated aliases for backward compatibility. I also updated the reference in `Mathlib/Order/JordanHolder.lean` to use `Relation.eqvGen_le` instead of the deprecated name. **Changes:** * `Relation.EqvGen.eqvGen_le` → `Relation.eqvGen_le` * `Relation.EqvGen.eqvGen_mono` → `Relation.eqvGen_mono` * `Relation.EqvGen.reflGen_le_eqvGen` → `Relation.reflGen_le_eqvGen` * `Relation.EqvGen.symmGen_le_eqvGen` → `Relation.symmGen_le_eqvGen` * `Relation.EqvGen.transGen_le_eqvGen` → `Relation.transGen_le_eqvGen` * `Relation.EqvGen.reflTransGen_le_eqvGen` → `Relation.reflTransGen_le_eqvGen` * `Relation.EqvGen.eqvGen_reflGen` → `Relation.eqvGen_reflGen` * `Relation.EqvGen.eqvGen_transGen` → `Relation.eqvGen_transGen` * `Relation.EqvGen.eqvGen_symmGen` → `Relation.eqvGen_symmGen` * `Relation.EqvGen.eqvGen_reflTransGen` → `Relation.eqvGen_reflTransGen` * `Relation.EqvGen.eqvGen_eq_reflTransGen` → `Relation.eqvGen_eq_reflTransGen` * `Relation.EqvGen.reflTransGen_symmGen` → `Relation.reflTransGen_symmGen` Co-authored-by: unknown <[email protected]>
|
-awaiting-author |
| by zero or more rewrite steps, each taken in either direction, there exists a `c` to which | ||
| both can be rewritten in zero or more forward steps. | ||
| This property is expressed as `EqvGen r ≤ Join (ReflTransGen r)`. -/ | ||
| theorem eqvGen_le_join_reflTransGen_iff : |
There was a problem hiding this comment.
In general, I think it'd be nice to be a bit more explicit about the confluence equivalents. See this private theorem in CSLib where we list them out in a TFAE.
| theorem IsConfluent.of_reflGen_reflTransGen | ||
| (h : ∀ a b c, r a b → r a c → ∃ d, ReflGen r b d ∧ ReflTransGen r c d) : | ||
| IsConfluent r where | ||
| join_of_rel_of_rel a b c hab hac := by | ||
| induction hab with | ||
| | refl => exact ⟨c, hac, refl⟩ | ||
| | @tail d e _ hde ih => | ||
| rcases ih with ⟨b, hdb, hcb⟩ | ||
| have : ∃ a, ReflTransGen r e a ∧ ReflGen r b a := by | ||
| clear hcb | ||
| induction hdb with | ||
| | refl => exact ⟨e, refl, ReflGen.single hde⟩ | ||
| | @tail f b _ hfb ih => | ||
| rcases ih with ⟨a, hea, hfa⟩ | ||
| cases hfa with | ||
| | refl => exact ⟨b, hea.tail hfb, ReflGen.refl⟩ | ||
| | single hfa => | ||
| rcases h _ _ _ hfb hfa with ⟨c, hbc, hac⟩ | ||
| exact ⟨c, hea.trans hac, hbc⟩ | ||
| rcases this with ⟨a, hea, hba⟩ | ||
| cases hba with | ||
| | refl => exact ⟨b, hea, hcb⟩ | ||
| | single hba => exact ⟨a, hea, hcb.tail hba⟩ |
There was a problem hiding this comment.
This looks odd to me for a couple of reasons. Firstly, the hypothesis h that you have is what is called "strongly confluent" in the literature. I'd go ahead and add this class if you're going to use it.
Secondly, I'd extract the existential you're constructing as a lemma to make it more clear what's happening. The idea here is that when a relation is strongly confluent, you can extend the reflexive transitive closure by one more strongly confluent step.
All together, what you get is the much nicer:
| theorem IsConfluent.of_reflGen_reflTransGen | |
| (h : ∀ a b c, r a b → r a c → ∃ d, ReflGen r b d ∧ ReflTransGen r c d) : | |
| IsConfluent r where | |
| join_of_rel_of_rel a b c hab hac := by | |
| induction hab with | |
| | refl => exact ⟨c, hac, refl⟩ | |
| | @tail d e _ hde ih => | |
| rcases ih with ⟨b, hdb, hcb⟩ | |
| have : ∃ a, ReflTransGen r e a ∧ ReflGen r b a := by | |
| clear hcb | |
| induction hdb with | |
| | refl => exact ⟨e, refl, ReflGen.single hde⟩ | |
| | @tail f b _ hfb ih => | |
| rcases ih with ⟨a, hea, hfa⟩ | |
| cases hfa with | |
| | refl => exact ⟨b, hea.tail hfb, ReflGen.refl⟩ | |
| | single hfa => | |
| rcases h _ _ _ hfb hfa with ⟨c, hbc, hac⟩ | |
| exact ⟨c, hea.trans hac, hbc⟩ | |
| rcases this with ⟨a, hea, hba⟩ | |
| cases hba with | |
| | refl => exact ⟨b, hea, hcb⟩ | |
| | single hba => exact ⟨a, hea, hcb.tail hba⟩ | |
| class IsStronglyConfluent (r : α → α → Prop) : Prop where | |
| exists_reflGen_reflTransGen : | |
| ∀ ⦃a b c : α⦄, r a b → r a c → ∃ d, ReflGen r b d ∧ ReflTransGen r c d | |
| theorem ReflTransGen.exists_reflGen_reflTransGen [IsStronglyConfluent r] | |
| (hab : ReflTransGen r a b) (hac : r a c) : | |
| ∃ d, ReflGen r b d ∧ ReflTransGen r c d := by | |
| induction hab with | |
| | refl => exact ⟨c, .single hac, refl⟩ | |
| | @tail d e _ hde ih => | |
| rcases ih with ⟨f, hdf, hcf⟩ | |
| cases hdf with | |
| | refl => exact ⟨e, .refl, hcf.tail hde⟩ | |
| | single hdf => | |
| rcases IsStronglyConfluent.exists_reflGen_reflTransGen hde hdf with ⟨g, heg, hfg⟩ | |
| exact ⟨g, heg, hcf.trans hfg⟩ | |
| /-- A relation is confluent if, whenever `a` rewrites in one step to both `b` and `c`, there | |
| exists a `d` to which `b` can be rewritten in at most one step and `c` in zero or more steps. -/ | |
| theorem IsConfluent.of_reflGen_reflTransGen [IsStronglyConfluent r] : IsConfluent r where | |
| join_of_rel_of_rel a b c hab hac := by | |
| induction hab with | |
| | refl => exact ⟨c, hac, refl⟩ | |
| | @tail d e _ hde ih => | |
| rcases ih with ⟨b, hdb, hcb⟩ | |
| rcases hdb.exists_reflGen_reflTransGen hde with ⟨a, hba, hea⟩ | |
| exact ⟨a, hea, hcb.trans hba.to_reflTransGen⟩ |
There was a problem hiding this comment.
Thanks for the suggestion. An earlier version of this PR had a StronglyConfluent predicate. After our discussion about overlap with CSLib, I removed it to keep the PR focused. Looking at the proof now, though, I agree that it’s clearer to state the hypothesis as IsStronglyConfluent r.
Now that the theorem takes [IsStronglyConfluent r], what do you think of renaming IsConfluent.of_reflGen_reflTransGen to IsStronglyConfluent.isConfluent? CSLib calls the corresponding theorem StronglyConfluent.to_confluent, and I like how the name makes the implication clear.
There was a problem hiding this comment.
I'll defer to Mathlib reviewers/maintainers on naming, I'm not very good at following these conventions.
There was a problem hiding this comment.
Now that the diff is much easier to read, I left some additional comments about nicer proofs. It would be very much appreciated going forward if you can closely look for these in CSLib when adding material that we've had for quite some time. If you take the first proof an LLM gives you, it will often do an induction over the relation closure that isn't needed.
|
-awaiting-author |
| (h : ∀ a b c, r a b → r a c → ∃ d, ReflGen r b d ∧ ReflTransGen r c d) : | ||
| Equivalence (Join (ReflTransGen r)) := | ||
| equivalence_join fun _ _ _ ↦ church_rosser h | ||
| have : IsStronglyConfluent r := ⟨h⟩ |
There was a problem hiding this comment.
Why not just have [IsStronglyConfluent r] as a hypothesis?
There was a problem hiding this comment.
You're right that we can take the typeclass as a hypothesis directly. Looking back at the CSLib confluent_equivalents theorem you mentioned, I think [IsConfluent r] is already enough here. What do you think of using that weaker hypothesis?
theorem equivalence_join_reflTransGen [IsConfluent r] :
Equivalence (Join (ReflTransGen r)) :=
equivalence_join (IsDiamond.join_of_rel_of_rel (r := ReflTransGen r))…nfluent relations
This PR adds a minimal relation-level API for confluence-style properties:
Relation.IsDiamondRelation.IsConfluentIt also adds the basic API connecting these predicates:
Relation.ReflTransGen.to_eqvGenRelation.join_reflTransGen_le_eqvGenRelation.eqvGen_le_join_reflTransGen_iffRelation.join_reflTransGen_eq_eqvGenRelation.IsConfluent.of_reflGen_reflTransGenThe main downstream application is the polynomial-reduction development in
#41475. There, polynomial reduction is treated as an ordinary relation on
MvPolynomial, and this API is used to formulate confluence and theChurch–Rosser property.
A more general rewriting API exists in CSLib. This PR does not attempt to
upstream that API wholesale; its scope is intentionally limited to the
declarations needed by the downstream polynomial application.
The earlier version of this PR also introduced
Relation.StronglyConfluentand additional equivalence packaging. Thosedeclarations have been removed to keep this PR small and application-driven.
The separate
Relation.IsChurchRosserclass has also been removed,and the equivalence between the Church–Rosser property and confluence is
now stated in
Relation.eqvGen_le_join_reflTransGen_iff.Reference: Becker, Weispfenning, and Kredel,
Gröbner Bases: A Computational Approach to Commutative Algebra.
AI usage disclosure:
I used OpenAI Codex to generate initial drafts of the definitions, theorem statements, proof scripts, and docstrings. I made the final decisions on the mathematical scope and API design, reviewed the generated code and final changes, and made a few edits to statements and docstrings.