Skip to content

chore(Combinatorics/SimpleGraph): avoid structure relation predicates - #37878

Open
YaelDillies wants to merge 14 commits into
leanprover-community:masterfrom
YaelDillies:simple_graph_no_std_struct
Open

YaelDillies wants to merge 14 commits into
leanprover-community:masterfrom
YaelDillies:simple_graph_no_std_struct

Conversation

@YaelDillies

@YaelDillies YaelDillies commented Apr 10, 2026 •

Copy link
Copy Markdown
Contributor

Relation predicates used to be defs over a forall, which was optimal for usability in fields of other structures. This changed in #35591 and #35192 as a byproduct of reducing apparent code duplication. Unfortunately, the usability issue was overlooked.

This PR restores usability by dropping the use of those relation classes that are structures, ie Std.Irrefl and Std.Transitive.


I would also be happy to revert #35591 and #35192 since this demonstrates a good use case for the def versions of the relation predicates.

Open in Gitpod

@github-actions

github-actions Bot commented Apr 10, 2026 •

Copy link
Copy Markdown

PR summary 96df3354ba

Import changes for modified files

No significant changes to the import graph

Import changes for all files
Files Import difference

Declarations diff (regex)

+ instance : Std.Irrefl (α := α) (· ⋖ ·) where irrefl := by simp
+ symm
++ irrefl_adj
++- loopless
+-+- adj_comm
- adj_symm
- rel_irrefl
- rel_symm
-++ symm_adj

You can run this locally as follows
## from your `mathlib4` directory:
git clone https://github.com/leanprover-community/mathlib-ci.git ../mathlib-ci

## summary with just the declaration names:
../mathlib-ci/scripts/pr_summary/declarations_diff.sh <optional_commit>

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

The doc-module for scripts/pr_summary/declarations_diff.sh in the mathlib-ci repository contains some details about this script.

Declarations diff (Lean)

✅ Lean-aware diff — post-build, computed from the Lean environment (commit 96df335).

  • +6 new declarations
  • −2 removed declarations
+SimpleGraph.Subgraph.irrefl_adj
+SimpleGraph.Subgraph.symm_adj
-SimpleGraph.TripartiteFromTriangles.rel_irrefl
-SimpleGraph.TripartiteFromTriangles.rel_symm
+SimpleGraph.adj_irrefl
+SimpleGraph.irrefl_adj
+Std.irrefl_iff
+instIrreflCovBy

No changes to strong technical debt.
No changes to weak technical debt.

Current commit 96df3354ba
Reference commit 49037fd10e

This script lives in the mathlib-ci repository. To run it locally, from your mathlib4 directory:

git clone https://github.com/leanprover-community/mathlib-ci.git ../mathlib-ci
../mathlib-ci/scripts/reporting/technical-debt-metrics.py 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).

@YaelDillies YaelDillies added the t-combinatorics Combinatorics label Apr 10, 2026
@SnirBroshi

Copy link
Copy Markdown
Collaborator

Can you elaborate on the issue with relation classes? I thought the only issue was that I didn't know we can write loopless.irrefl := ... in where when making #35191, and we can switch all constructions of SimpleGraphs to use that instead of angle brackets.

Also @Vierkantor said that Std.Irrefl is probably better than ∀ v, ¬ Adj v v, though I'm not sure what are the pros and cons.

@YaelDillies

Copy link
Copy Markdown
Contributor Author

You can see from the diff that the predicate is largely more often unfolded than its API is used. Empirically, it is useful for the fields to be universal quantifiers, as then the variables can be introduced with where or given as arguments with no extra dot notation.

I get your point about loopless.irrefl, but I see the need for it as non-justifiable given how little it buys us compared to the loss of usability and discoverability.

I am teaching a Lean course in six months, which is why I am caring about usability and discoverability more now.

Adj : V → V → Prop
symm : Symmetric Adj := by aesop_graph
loopless : Std.Irrefl Adj := by aesop_graph
loopless (v : V) : ¬ Adj v v := by aesop_graph

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

So one apparent advantage of your change is you can close goals with the autoparams. But if we declare attribute [aesop_graph norm] Std.irrefl_iff then aesop_graph would have the same capabilities, so I don't believe it's a very strong argument.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Sure, my point is mostly to improve usability when providing a manual proof

@SnirBroshi SnirBroshi Apr 26, 2026 •

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.

Then how about a custom constructor? This sounds to me like the best of both worlds

SimpleGraph.fromIrreflSymmRel to match the existing SimpleGraph.fromRel?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Custom constructors suffer even worse usability

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.

I agree, but OTOH I don't see the usability issue with typing loopless.irrefl/symm.symm instead of loopless/symm. Is the issue the discoverability of those when using the code-action that generates a structure scaffolding?

@Vierkantor Vierkantor left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'm not so convinced by this change. It seems like code churn that maybe makes things available at a lower level of understanding, but then we lose all the advantages of working at high levels. I wouldn't be able to recognize (v : V) : ¬ Adj v v at a glance as irreflexivity, but I would be able to do so if it said : Irrefl Adj.

The API for Std.Irrefl is underdeveloped, I agree, and this makes proofs somewhat verbose. But why not instead write better API?

For transitivity, we also have the issue there are multiple non-defeq ways to state the bare proposition. To prevent issues with e.g. the hypotheses getting reordered, we should standardize on one, which is Std.Transitive.

On the other hand, I am not a combinatorics maintainer, so if combinatorics maintainers agree this PR has concrete benefits, then I am not going to block it.

Comment on lines +70 to +71
theorem bot_isCompleteMultipartite : (⊥ : SimpleGraph α).IsCompleteMultipartite := by
simp [IsCompleteMultipartite]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I don't really see this as much of an upgrade: we still need to unfold IsCompleteMultipartite but here we don't see the constructor which marks the abstraction boundaries.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Unfolding the new definition with simp is another standard way to mark the abstraction boundary, so I don't really see the issue

Comment thread Mathlib/Order/Cover.lean Resolved
@mathlib-dependent-issues mathlib-dependent-issues Bot added the blocked-by-other-PR This PR depends on another PR (this label is automatically managed by a bot) label Apr 26, 2026
@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 Apr 30, 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 May 1, 2026
@mathlib-dependent-issues mathlib-dependent-issues Bot removed the blocked-by-other-PR This PR depends on another PR (this label is automatically managed by a bot) label Jun 4, 2026
@mathlib-dependent-issues

Copy link
Copy Markdown

@YaelDillies
YaelDillies force-pushed the simple_graph_no_std_struct branch from 7569397 to 7070c99 Compare June 4, 2026 07:58
@YaelDillies
YaelDillies temporarily deployed to cache-upload-forks June 4, 2026 08:49 — with GitHub Actions Inactive
@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 Jun 11, 2026
@mathlib-merge-conflicts

Copy link
Copy Markdown

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

…ates

Relation predicates used to be `def`s over a `forall`, which was optimal for usability in fields of other structures.
This changed in leanprover-community#35591 and leanprover-community#35192 as a byproduct of reducing apparent code duplication. Unfortunately, the usability issue was overlooked.

This PR restores usability by dropping the use of those relation classes that are `structure`s, ie `Std.Irrefl` and `Std.Transitive`.
It also uses more widely the `simp` auto-param on `SimpleGraph.loopless`.
@YaelDillies
YaelDillies force-pushed the simple_graph_no_std_struct branch from 7070c99 to bfcad97 Compare June 13, 2026 07:44
@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 Jun 13, 2026
@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 Jul 14, 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 Jul 15, 2026
@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 Aug 26, 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 Aug 26, 2026
@mathlib-merge-conflicts

Copy link
Copy Markdown

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

@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 Sep 9, 2026
@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 Sep 9, 2026
@b-mehta

b-mehta commented Sep 9, 2026

Copy link
Copy Markdown
Contributor

For clarity, I agree with all of Anne's comments above.

@SnirBroshi

Copy link
Copy Markdown
Collaborator

While keeping in mind this PR, and GraphLike, and Digraph getting vertex sets, what do you all think about this:

structure SimpleGraph' (V : Type*) extends Digraph V, Std.Symm Adj, Std.Irrefl Adj where

example (V) : SimpleGraph' V where
  Adj := Ne
  toSymm := inferInstance
  irrefl _ := Ne.irrefl

-- with `aesop_graph`:
structure SimpleGraph'' (V : Type*) extends Digraph V, Std.Symm Adj, Std.Irrefl Adj where
  symm := by aesop_graph
  irrefl := by aesop_graph

example (V) : SimpleGraph'' V where
  Adj := Ne

Note that this means that there's no SimpleGraph.Adj anymore, only Digraph.Adj.

@YaelDillies

Copy link
Copy Markdown
Contributor Author

This sounds reasonable to me, but @Jun2M's plans will likely conflict with this vision anyway.

@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 Sep 15, 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 Sep 15, 2026
@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 Nov 19, 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 Nov 20, 2026

This branch was previously deployed

1 inactive (outdated) deployment
cache-upload-forks — 7070c992 Deployed Jun 4, 2026 by YaelDillies via ci (fork) / Upload to cache #302769
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