Zulip Chat Archive
Stream: mathlib4
Topic: should one explicit coercions?
Antoine Chambert-Loir (Nov 23 2025 at 17:24):
In many cases, mathlib has a coercion function that applies whenever needed (and appropriately detected).
Example in algebra : a docs#RingEquiv f : M ≃+* N has an associated docs#RingHom, f.toRingHom, but it can also be obtained by coercion, as (f : M →+* N) and is indicated by an uparrow ↑f.
Is there a preferred form?
Indeed, this sometimes brings the necessity to have two theorems, one where the coercion is applied, and one with the explicit original function. Maybe this reflects a lack of a simp lemma that would prefer one form to the other.
Junyan Xu (Nov 23 2025 at 18:05):
I think we're getting rid of the coercions in favor of explicit invocation of .ofClass, see . I think we should probably also get rid of RingEquiv.toRingHom etc. It's been observed before that docs#LinearEquiv.toLinearMap isn't reducibly defeq to the coercion and it caused problems, for example.
Christian Merten (Nov 23 2025 at 18:44):
No, the plan is to get rid of the coercions and make RingEquiv.toRingHom the simp NF. The .ofClass constructors should only be used in general lemmas about FooHomClass.
Junyan Xu (Nov 23 2025 at 19:17):
Are you sure? We certainly won't be able to get rid of structure projections, but RingEquiv.toRingHom is not one; if we want to keep it I think we should make it an abbrev of .ofClass, and the same applies to LinearEquiv.toLinearMap.
Riccardo Brasca (Nov 23 2025 at 19:28):
I think that the coercions we want to get rid of are those from to FooHomClass to FooHom, not those from FooHom to BarHom.
Junyan Xu (Nov 23 2025 at 19:58):
The coercions from FooHom to BarHom are given by the BarHomClass FooHom instances, no? Structure projections (from extends) like docs#RingHom.toAddMonoidHom or manually defined RingEquiv.toRingHom/LinearEquiv.toLinearMap are not coercions.
Junyan Xu (Nov 23 2025 at 20:03):
Oh, I finally remembered what is particularly egregious about the LinearEquiv/LinearMap coercion: there's a manually defined LinearEquiv.instCoeLinearMap which is not reducibly defeq to the coercion from LinearEquiv being LinearMapClass ...
Riccardo Brasca (Nov 23 2025 at 20:14):
Let me ping @Jireh Loreaux that surely know what is the plan
Yaël Dillies (Nov 23 2025 at 20:49):
Indeed the plan is to get rid of the generic (ie the ones that trigger in the presence of a hom class instance) coercions and add the specific (ie between two concrete types) coercions
Yaël Dillies (Nov 23 2025 at 20:49):
Once that is done, Antoine's original problem is no more: the coercion will syntactically be the same as .toRingHom
Antoine Chambert-Loir (Nov 23 2025 at 21:08):
That was one of the coercions I had in mind, the other one is related to quotients, I rapidly fell onto equalities such as ⟦x⟧ = ↑x where rfl showed they coincided but rw? found nothing.
Yaël Dillies (Nov 23 2025 at 21:14):
That's a completely separate refactor which @Yuyang Zhao was doing
Last updated: Dec 20 2025 at 21:32 UTC