feat(Combinatorics/GraphLike): introduce GraphLike typeclass#36743
feat(Combinatorics/GraphLike): introduce GraphLike typeclass#36743Jun2M wants to merge 62 commits into
GraphLike typeclass#36743Conversation
PR summary 18c5d4a69cImport changes for modified filesNo significant changes to the import graph Import changes for all files
|
lauramonk
left a comment
There was a problem hiding this comment.
Thank you! This seems great to me, maybe a few names to pick carefully. Notably I agree with the idea of calling the notion GraphLike. I am not certain about prodDart and in particular I do not fully understand the point of having the two different objects (but this is my own ignorance).
lauramonk
left a comment
There was a problem hiding this comment.
Looks great, have added a few comments.
| /-- The step from `u` to `v` is a dart from `u` to `v`. -/ | ||
| @[expose] | ||
| def step (G : Gr) [GraphLike V D E Gr] (u v : V) := | ||
| {d : D // d ∈ D(G) ∧ src Gr d = u ∧ tgt Gr d = v} |
There was a problem hiding this comment.
Is this a concept in the informal literature? Why do we need it?
There was a problem hiding this comment.
Mathematically speaking, they are exactly darts, therefore not a separate concept in informal literature. The only difference with darts is that it has its endpoints (src & tgt) in its type.
I made this as a replacement for what Adj does in the def of walk in SimpleGraph. Walk in SimpleGraph is constructed from a vertex by iteratively adding Adj : V → V → Prop. Mathematically, they should be replaced with darts, d : D, but they do not have their endpoints in the type. To minimize the changes needed, I created Step : V → V → Type* as a endpoint-typed version of darts to replace the role of Adj in walks.
So Step is the way I achieved "endpoint-type version of darts". If you are not happy with the way it is done, it can easily be changed.
If you think we should not have "endpoint-type version of darts", that would mean I have to rewrite #36756, the definition of walk, and #36971, migrating Simplegraph to use GraphLike walk. Migration PR is already changing ~1000 lines and I have no idea how much worse it will get if I don't have something like Step. Nevertheless, I could try if you want.
There was a problem hiding this comment.
Why do we have one global type of darts, instead of one type of darts per pair of vertices?
There was a problem hiding this comment.
I think SimpleGraph.Dart poses a problem with getting rid of "darts as global type" and sticking with "darts as vertex-indexed type".
As GraphLike has darts, or mathematically equivalent one as vertex-indexed version, in the definition, I would argue, SimpleGraph.Dart would obviously be generalised and handled in GraphLike level.
Also, in the combinatorial map that dart comes from, dart is the primitive type and vertices are derived from the orbit of a function on darts, φᵥ, whereas dart being a type indexed by vertices implies you already have vertices before having darts. Given the history, if a mathematician is looking for a Lean implementation of "darts", I don't think they would expect it to be vertex-indexed. Lastly, I think it is more ergonomic to talk about the single set of darts in a global type rather than indexed set of darts for each pair of vertices.
So, the decision to not have "darts as global type" would imply that either deprecating or migrating SimpleGraph.Dart and all theorems about it to be vertex indexed, along with some other issues.
There was a problem hiding this comment.
@YaelDillies, Step is only needed to be used in Walk so if you are happy to define dart to be globally typed, I can move Step and its lemmas to the PR that introduces Walk on GraphLike. If we do that, it would give me time to try and find out what removing Step would mean to the migration PR of SimpleGraph to use GraphLike.Walk.
There was a problem hiding this comment.
Yes, delaying sounds like a great idea here!
Co-authored-by: Yaël Dillies <yael.dillies@gmail.com>
|
Can you explain why adding a class is a better solution than, say, rebuilding |
|
It is kinda hard to contrast without a concrete suggestion but here are some of the worries. |
|
I understand but similarly this PR is a major shift in the way we formalise graphs in mathlib, and it will be painful if we decide to refactor away from it later on. We will not merge this PR until we have tried a reasonable range of implementations, and basing everything off a single type is a promising one that I haven't seen implemented yet. How many lines it takes to refactor mathlib is mostly irrelevant. We are looking for a long term solution here. Also 1k lines is really not that big. I got a 5k lines refactor merged over the course of two weeks three years ago, and many more that were around 2-3k. Good point about |
|
I understand the need for a lot of care with a PR like this. I would not want huge changes to Mathlib to feel rushed in without due considerations. If it is okay, I have some questions to clarify some things and make the most of this as a learning experience.
Thank you for your time and consideration. |
Per discussion at (#graph theory > HasAdj), This PR introduces the
GraphLiketypeclass to capture the notions likedartandwalkacross various graph objects, such asSimpleGraph,Graph, andDigraph.The goal is that by abstracting these core components into a typeclass, we can prove these results once for all graph-like structures rather than duplicating them across different graph types.
Main definitions
GraphLike V D E Gr: A typeclass parameterized by a vertex typeV, dart typeDand a graph typeGr(withV,D&Eas anoutParam).GraphLike.verts : Set V: The set of vertices of the graph.GraphLike.darts : Set D: The set of darts of the graph.GraphLike.edges : Set E: The set of edges of the graph.GraphLike.Adj : V → V → Prop: The adjacency relation, defined by default as∃ d ∈ darts, fst d = u ∧ snd d = v.This PR generalises #35776 to also unify
Graph.PRs depending on this PR are
#39047 (graphLike with no multi edges) => #39050 (Digraph is graphLike)
||
V
#36829 (undirected graphLike) => #39053 (Graph is graphLike) & #39054 (Simplegraph is graphLike)
||
V
#36756 (def of walk on GraphLike) => #36971 (change Simplegraph to use GraphLike walk)