Skip to content

feat(Combinatorics/GraphLike): introduce GraphLike typeclass#36743

Open
Jun2M wants to merge 34 commits intoleanprover-community:masterfrom
Jun2M:HasDartDef
Open

feat(Combinatorics/GraphLike): introduce GraphLike typeclass#36743
Jun2M wants to merge 34 commits intoleanprover-community:masterfrom
Jun2M:HasDartDef

Conversation

@Jun2M
Copy link
Copy Markdown
Collaborator

@Jun2M Jun2M commented Mar 16, 2026

Per discussion at (#graph theory > HasAdj), This PR introduces the GraphLike typeclass to capture the notions like dart and walk across various graph objects, such as SimpleGraph, Graph, and Digraph.

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.

This PR generalises #35776 to also unify Graph.

Main definitions

  • GraphLike α β Gr: A typeclass parameterized by a vertex type α, dart type β and a graph type Gr (with α as an outParam).
    • GraphLike.verts : Set α: The set of vertices of the graph.
    • GraphLike.darts : Set β: The set of darts of the graph.
    • GraphLike.Adj : α → α → Prop: The adjacency relation, defined by default as Nonempty (darts G u v).

Open in Gitpod

@github-actions github-actions bot added the t-combinatorics Combinatorics label Mar 16, 2026
@github-actions
Copy link
Copy Markdown

github-actions bot commented Mar 16, 2026

PR summary e960b84129

Import changes for modified files

No significant changes to the import graph

Import changes for all files
Files Import difference
Mathlib.Combinatorics.GraphLike.Basic (new file) 525
Mathlib.Combinatorics.Digraph.GraphLike (new file) 527
Mathlib.Combinatorics.SimpleGraph.GraphLike (new file) 545

Declarations diff

+ Adj.left_mem
+ Adj.right_mem
+ Adj.toStep
+ DartAdj
+ DartLike
+ DartLike.toProd
+ GraphLike
+ adj_of_mem_darts
+ dartStep
+ dartStep_val
+ darts_ext
+ fst_eq
+ instance : DartLike α (α × α)
+ instance : GraphLike α (α × α) (Digraph α)
+ instance : GraphLike α (α × α) (SimpleGraph α)
+ instance : Subsingleton (step G u v)
+ instance [DecidableEq β] : DecidableEq (step G u v) := Subtype.instDecidableEq
+ instance [DecidableRel (Adj G)] : DecidablePred (· ∈ darts G)
+ mem_darts_iff_adj
+ snd_eq
+ step
+ step.adj
+ step.ext
+ step.ext_HEq
+ step.fst
+ step.left_eq_of_val_eq
+ step.left_mem
+ step.right_eq_of_val_eq
+ step.right_mem
+ step.snd
+ step.todart
+ step.todart_fst
+ step.todart_snd
+ step.todart_val
+ toProd_eq
+ val_step_eq
++ darts_def

You can run this locally as follows
## summary with just the declaration names:
./scripts/pr_summary/declarations_diff.sh <optional_commit>

## more verbose report:
./scripts/pr_summary/declarations_diff.sh long <optional_commit>

The doc-module for scripts/pr_summary/declarations_diff.sh contains some details about this script.


No changes to technical debt.

You can run this locally as

./scripts/reporting/technical-debt-metrics.sh pr_summary
  • The relative value is the weighted sum of the differences with weight given by the inverse of the current value of the statistic.
  • The absolute value is the relative value divided by the total sum of the inverses of the current values (i.e. the weighted average of the differences).

Copy link
Copy Markdown
Contributor

@lauramonk lauramonk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

@Jun2M
Copy link
Copy Markdown
Collaborator Author

Jun2M commented Mar 20, 2026

@IvanRenison, I have moved the instance of α × α being a SymmDartLike to #36829, PR for SymmDartLike and SymmGraphLike. For SimpleGraph, for example, I didn't want to introduce instance of GraphLike in this PR knowing that it will be immediately changed to SymmGraphLike in the following PR.

@IvanRenison
Copy link
Copy Markdown
Collaborator

Okay
And what about the instance for Graph? In that PR I see the instance for Digraph but not the instance for Graph

@IvanRenison
Copy link
Copy Markdown
Collaborator

Digraph is anyway not symmetric, so I think it should be here

@lauramonk
Copy link
Copy Markdown
Contributor

Okay And what about the instance for Graph? In that PR I see the instance for Digraph but not the instance for Graph

Hi Ivan, the problem with Graph is that Darts have not yet been added for them. I have a repo with a notion of Dart of Graph, with the inversion and basic results, here. I need to adapt it to Jun2M's set-up to prove the instance for Graph; it should be fairly straightforward but I have not yet managed to (as I am a beginner).

@Jun2M
Copy link
Copy Markdown
Collaborator Author

Jun2M commented Mar 20, 2026

@IvanRenison, For Graph, I asked @lauramonk to make a PR for it after some coordination in the zulip thread. Do you think it is a mistake to split it like that? I could do it myself pretty quickly to be honest. (and given that I changed the definition of GraphLike quite a bit, maybe that is the right thing to do)

For Digraph, proving GraphLike α (α × α) (Digraph α) requires DartLike α (α × α). However, the full generality is SymmDartLike α (α × α). If you want me to introduce temporary instances, I could do that. Otherwise, Digraph should be in that PR.

@lauramonk
Copy link
Copy Markdown
Contributor

@Jun2M Please feel free to work on Dart for Graph, as you are a lot faster. I clearly cannot do it until next week. If our work on https://github.com/lauramonk/mathlib4/tree/GraphDart is of any use please also feel free to take it from there. I feel it would make sense to define Dart for Graph before GraphLike.

@lauramonk
Copy link
Copy Markdown
Contributor

I can also PR Dart for Graph without the instance and let you write the instance.

@IvanRenison
Copy link
Copy Markdown
Collaborator

Ah, I didn't thought that there are still no Darts in Graph.

I think adding temporary instances here is a good idea. And later they could be moved, but also they can simply be extended

@Jun2M
Copy link
Copy Markdown
Collaborator Author

Jun2M commented Mar 20, 2026

@IvanRenison, by adding temporary instances, do you mean Digraph or all 3 graphlikes?

@IvanRenison
Copy link
Copy Markdown
Collaborator

IvanRenison commented Mar 20, 2026

I would add Digraph and SimpleGraph, as you mention that we still not have darts for Graph

@Jun2M
Copy link
Copy Markdown
Collaborator Author

Jun2M commented Mar 20, 2026

@lauramonk, I am realising I should have coordinated things much better. Perhaps there should be a separate Zulip thread to discuss the GraphLike implementation for Graph.

I did say I like your Dart definition and would be good to modify it to use for the GraphLike instance. Then, I experimented more and the definition of GraphLike has diverged quite a bit from when we discussed. (You may notice @IvanRenison's DarkLike class is revived among other things) For this new definition, we need a Graph-unaware dart type, where each graph can pick some set of darts as their darts. The Graph-aware dart type like yours is not quite the right thing.

I thought I would finish making PR for moving SimpleGraph stuff to be based on GraphLike and come back to Zulip. But perhaps that was a mistake. I will write something for Zulip that describes the changes made & pros and cons. I have an idea for what dart-type for Graph should be but I would be very happy to discuss it as I, admittedly, haven't explored that side so much yet.

@mathlib-merge-conflicts mathlib-merge-conflicts bot added the merge-conflict The PR has a merge conflict with master, and needs manual merging. (this label is managed by a bot) label Mar 21, 2026
@mathlib-merge-conflicts
Copy link
Copy Markdown

This pull request has conflicts, please merge master and resolve them.

@github-actions github-actions bot removed the merge-conflict The PR has a merge conflict with master, and needs manual merging. (this label is managed by a bot) label Mar 21, 2026
Copy link
Copy Markdown
Collaborator

@IvanRenison IvanRenison left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some suggestions

rfl

/-- Convert a step to a dart. -/
def step.todart (h : step G u v) : darts G := ⟨h.val, h.prop.1⟩
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we want this to return β? I don't know, but it is is worth asking I think

Copy link
Copy Markdown
Collaborator Author

@Jun2M Jun2M Mar 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was initially defined as that but so many of the theorems in SimpleGraph expected the darts to be a type so it was changed. I think there is an argument to have whether there should be a function that returns β along with this function but, unless we want ⟨step.todart h, step.todart_mem h⟩ everywhere in SimpleGraph, I think we should have a function that returns darts G.

Comment on lines +75 to +76
lemma adj_of_mem_darts (hd : d ∈ darts G) : Adj G (fst d) (snd d) :=
exists_darts_iff_adj.mp ⟨d, hd, rfl, rfl⟩
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you make this an iff?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The other direction is not true as there can be d that does not belong in darts G yet shares fst and snd with a dart that is in darts G.

@Jun2M Jun2M requested a review from SnirBroshi April 1, 2026 20:34
@[inherit_doc verts]
scoped notation "V(" G ")" => verts G

variable {α β Gr : Type*} {G : Gr} {u u' v v' w : α} {d : β}
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
variable {α β Gr : Type*} {G : Gr} {u u' v v' w : α} {d : β}
variable {V β Gr : Type*} {G : Gr} {u u' v v' w : V} {d : β}

From to the style guidelines:

Types with a mathematical content are expressed with the usual mathematical notation, often with an upper case letter (G for a group, R for a ring, K or 𝕜 for a field, E for a vector space, ...). This convention is not followed in older files, where greek letters are used for all types. Pull requests renaming type variables in these files are welcome.

In SimpleGraph the use is mixed, sometimes with α, β, etc. and some times with U, V, etc. but maybe for new things we should use U, V, etc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

t-combinatorics Combinatorics

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants