Skip to content

feat: QuotType - #30668

Open
astrainfinita wants to merge 37 commits into
leanprover-community:masterfrom
astrainfinita:FR_quotlike
Open

astrainfinita wants to merge 37 commits into
leanprover-community:masterfrom
astrainfinita:FR_quotlike

Conversation

@astrainfinita

@astrainfinita astrainfinita commented Oct 19, 2025 •

Copy link
Copy Markdown
Collaborator

This typeclass is primarily for use by type synonyms of Quot and Quotient. Using QuotType API for type synonyms of Quot and Quotient will avoid defeq abuse caused by directly using Quot and Quotient APIs.

This PR also adds some typeclasses to support different ways to find the quotient map that should be used. See the documentation comments of these typeclasses for examples of usage.


It's not a typical design to use these auxiliary typeclasses and term elaborators, but I haven't found a better way to support these notations.

Some of the naming may need to be discussed. For example:

This PR continues the work from #16421.

Original PR: #16421

@astrainfinita

Copy link
Copy Markdown
Collaborator Author

Comments from Original PR #16421

This section contains 1 comment(s) from the original PR, excluding bot comments.


@YaelDillies (2024-11-02 15:10 UTC):
Thanks for the PR! This feels like a big change. Can you please discuss it on Zulip?

@astrainfinita astrainfinita mentioned this pull request Oct 19, 2025
@github-actions

github-actions Bot commented Oct 19, 2025 •

Copy link
Copy Markdown

PR summary d7c8cc9054

Import changes for modified files

No significant changes to the import graph

Import changes for all files
Files Import difference
Mathlib.Data.QuotType (new file) 57

Declarations diff

+ HintClass
+ Q
+ Quot.exact
+ QuotType
+ QuotTypeOut
+ eq
+ eq_iff_quotMk
+ eq_mkQ_iff_out
+ exact
+ exists_rep
+ hrecOnQ
+ hrecOnQ_mkQ
+ hrecOnQ₂
+ hrecOnQ₂_mkQ
+ ind
+ indep
+ indepCoherent
+ inductionOn
+ inductionOnPi
+ inductionOn₂
+ inductionOn₃
+ ind₂
+ ind₃
+ instance (priority := 800)
+ instance (priority := 800) (f : α → Prop) [hf : DecidablePred f] (h : ∀ a b, r a b → f a = f b) :
+ instance (priority := 800) [Inhabited α] : Inhabited Q
+ instance (priority := 800) [IsEquiv α r] [dec : DecidableRel r] : DecidableEq Q
+ instance (priority := 800) [Nonempty α] : Nonempty Q
+ instance (priority := 800) [Subsingleton α] : Subsingleton Q
+ instance (priority := 800) [Unique α] : Unique Q := Unique.mk' _
+ instance : QuotType (Q A B) B (HintClass.r A)
+ instance : QuotTypeOut ZFSet PSet PSet.Equiv
+ liftOnQ
+ liftOnQ_mkQ
+ liftOnQ_surjective
+ liftOnQ₂
+ liftOnQ₂'
+ liftOnQ₂'_mkQ
+ liftOnQ₂_mkQ
+ liftQ
+ liftQIndepPr1
+ liftQ_comp_mkQ
+ liftQ_mkQ
+ liftQ_surjective
+ liftQ₂
+ liftQ₂'
+ liftQ₂'_mkQ
+ liftQ₂_mkQ
+ mapQ
+ mapQ_mkQ
+ mapQ₂
+ mapQ₂_mkQ
+ mkQ_eq_iff_out
+ mkQ_eq_mkQ
+ mkQ_out
+ nonempty_quotient_iff
+ ofQuot
+ ofQuot_toQuot
+ out
+ out_inj
+ out_injective
+ out_mkQ_rel
+ out_rel_out
+ recOnQ
+ recOnSubsingletonQ
+ recOnSubsingletonQ₂
+ recQ
+ recQ_mkQ
+ rel_out_mkQ
+ toQuot_injective
+ toQuot_ofQuot
+ «exists»
+ «forall»
++ instQuotType

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

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

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


No changes to technical debt.

You can run this locally as

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

@github-actions github-actions Bot added the t-data Data (lists, quotients, numbers, etc) label Oct 19, 2025
@astrainfinita astrainfinita added the RFC Request for comment label Oct 19, 2025
@astrainfinita astrainfinita added awaiting-zulip There is a Zulip discussion; the author should await and report/implement the decision reached there migrated-to-fork and removed migrated-to-fork labels Oct 19, 2025
Comment thread Mathlib/Data/QuotLike.lean Resolved

@vihdzp vihdzp left a comment

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.

This new design seems really good. I agree that using ⟦x⟧ with any type that isn't syn-eq to Quotient r just leads to trouble, and having to duplicate basic quotient API for every new quotient type is quite annoying. I do have a few questions about this design, however.

Comment thread Mathlib/Data/QuotLike.lean Outdated
scoped instance {α} [s : Setoid α] : QuotLike.HasQuot (Quotient s) α (· ≈ ·) where
```
-/
class HasQuot (Q : outParam Sort*) (α : Sort*) (r : outParam (α → α → Prop))

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.

Why do we need this, as well as QuotLike? And why do we need both the ⟦x⟧ notation as well as the mkQ' x notation?

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.

Why do we need this, as well as QuotLike?

This actually provides a quotient map notation similar to Lean3, where the meaning of ⟦(x : A)⟧ is determined by searching the typeclass Setoid A (like Quotient.mk' in Lean4). That is, sometimes the output type may be unavailable, or the quotient map cannot be determined from the output type. In such cases, using a (maybe local or scoped) instance of this typeclass can help.

And why do we need both the ⟦x⟧ notation as well as the mkQ' x notation?

It's simply because this makes ⟦x⟧ the notation for the term mkQ x (without a custom term elaborator). I forget if only using the ⟦x⟧ notation would cause any problems, but if it doesn't, then we may make ⟦x⟧ try to find the quotient map using the input type on failure.

Comment thread Mathlib/Data/QuotLike.lean Outdated
instance (p : Submodule R M) : QuotLike.Hint p (M ⧸ p) M p.quotientRel where
```
-/
class Hint {Hint : Sort*} (hint : Hint)

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 don't understand, what is the hint used for, and how is it used?

@astrainfinita astrainfinita Oct 20, 2025 •

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.

For quotient groups, quotient rings, and quotient modules, we need to know the subobjects used to distinguish different quotient objects. For example, we would write ⟦(x : M)⟧_p : M ⧸ p. Perhaps this shouldn't be called a hint, but I haven't come up with a better name.

@vihdzp vihdzp Oct 20, 2025 •

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.

This is an interesting design. I do wonder if we could get away with just writing (⟦x⟧ : M ⧸ p), though.

@astrainfinita astrainfinita Dec 9, 2025 •

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.

My idea is to replace all existing quotient maps with the new notations. IIRC we need more type annotations than before without hinted notation. But maybe we can split the notation into another PR and give it a try. (upd: removed this part)

Comment thread Mathlib/Data/QuotLike.lean Outdated

/-- The canonical quotient map. Inferred from the hint via typeclass `QuotLike.Hint` or
the hint and the input type via typeclass `QuotLike.HasQuotHint`. -/
syntax:max (name := mkQ_) "mkQ_" term:max : term

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.

mkQ' and mkQ_ have very weird names. Are they supposed to be implementation details, or proper public API? In the former case I'd like the docstrings to explain this; in the latter I'd like to come up with better names for this.

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.

I've already explained the purpose of these notations in the module docstring.

Comment thread Mathlib/Data/QuotLike.lean Outdated
fun h ↦ Quot.exact (eq_iff_quotMk.mp h)

@[simp]
theorem eq [IsEquiv α r] {a b : α} : (⟦a⟧ : Q) = ⟦b⟧ ↔ r a b :=

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 don't believe this should be a simp theorem. The reason we often work with quotients is because equality is nicer to reason about than an arbitrary equivalence relation; this theorem only helps to undo this.

I don't think Quotient.eq should be simp either, but that's a question for another time.

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.

But at the same time, this is about any equality in the quotient, this is about equality of two values of the quotient map. This lemma looks like a good simp lemma to me

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.

Fair enough. I guess we can make it simp and later see if it causes any problems.

@YaelDillies YaelDillies added the awaiting-author Reply -awaiting-author to remove the label on your PR once you have addressed all comments. label Oct 25, 2025
@mathlib4-merge-conflict-bot mathlib4-merge-conflict-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, 2025
@mathlib4-merge-conflict-bot

Copy link
Copy Markdown

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

@astrainfinita astrainfinita changed the title feat: QuotLike feat: QuotType Dec 9, 2025
@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 Dec 9, 2025
Comment on lines +81 to +82
@[inherit_doc mkQ]
elab "⟦" a:term "⟧" : term <= Q => do

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.

When attempting to replace existing mathlib APIs with QuotType, I discovered that Lean attempts to synthesize QuotType (SomeQuotientOf α) (Subtype (_ : α → Prop)) ?_. The original API could automatically insert the coercion.

I don't know if this can be considered a core issue. (AFAIK core is unlikely to change features related to typeclasses now) I'm also not sure if this issue is worth resolving. But I made this elaborator to ensure that the quotient map is derived from the output type.

@astrainfinita astrainfinita removed the awaiting-author Reply -awaiting-author to remove the label on your PR once you have addressed all comments. label Dec 10, 2025
Comment thread Mathlib/Data/QuotType.lean Outdated

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.

Can we not use Eq.ndrec? It doesn't have as many theorems about it as Eq.rec.

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.

Quot.rec and Quotient.rec use Eq.ndrec, so I used it too. Did it cause trouble in mathlib?

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 think I'll open a Zulip.

@dagurtomas

Copy link
Copy Markdown
Contributor

@astrainfinita what's the status here, is the discussion stuck on something in particular? What is the relation to #30750?

@dagurtomas dagurtomas added the awaiting-author Reply -awaiting-author to remove the label on your PR once you have addressed all comments. label May 7, 2026
@SnirBroshi

Copy link
Copy Markdown
Collaborator

(these are mostly unrelated, but #30750 defines stuff twice for Quot/Quotient so using a unifying class makes more sense, and maybe possibly when this lands people will be more open to extend the API with things like equivalence classes)

@plp127

plp127 commented Aug 7, 2026

Copy link
Copy Markdown
Contributor

What are we waiting on here?

This branch has not been deployed

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

Labels

awaiting-author Reply -awaiting-author to remove the label on your PR once you have addressed all comments. awaiting-zulip There is a Zulip discussion; the author should await and report/implement the decision reached there RFC Request for comment t-data Data (lists, quotients, numbers, etc)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants