Zulip Chat Archive

Stream: mathlib4

Topic: inferInstance vs. by infer_instance


Michael Stoll (Apr 12 2024 at 09:11):

In #12060, seeing several instances proved via by infer_instance, I replaced the proofs by inferInstance, thinking that the term proof is shorter and likely more efficient than a tactic proof. Then @Yaël Dillies asked me to revert, saying

This change means that any use of those instances comes at the cost of unfolding inferInstance. In general, please use infer_instance the tactic instead of inferInstance the term when defining instances

However, the tactic infer_instance is defined to be exact inferInstance, and some experiments showed that (in terms of heartbeats) the term is at least as efficient as the tactic.

So my question is whether there is a preference for the tactic over the term (and if so, on what grounds).

(It may be the case that in Lean3 there was a difference that no longer exists.)

Mario Carneiro (Apr 12 2024 at 09:12):

indeed there used to be a difference in lean 3 that no longer exists

Mario Carneiro (Apr 12 2024 at 09:12):

IMO it's a (minor) regression

Michael Stoll (Apr 12 2024 at 09:12):

What was better in Lean3?

Mario Carneiro (Apr 12 2024 at 09:13):

the difference is that apply_instance would directly insert the result of typeclass synthesis, while inferInstance or whatever it was called then is just an identity wrapper with a [x : A] argument which indirectly triggers typeclass synthesis

Mario Carneiro (Apr 12 2024 at 09:13):

the inferInstance identity function is needless cruft in the proof term

Michael Stoll (Apr 12 2024 at 09:14):

But apply_instance would need to get the result from somewhere, so how is it different?

Mario Carneiro (Apr 12 2024 at 09:14):

it would call the typeclass machinery function itself

Mario Carneiro (Apr 12 2024 at 09:15):

rather than relying on the app elaborator to see a [ ] argument and call the typeclass machinery

Michael Stoll (Apr 12 2024 at 09:16):

Are there any plans to change the implementation of the tactic infer_instance to recover the old behavior?

Mario Carneiro (Apr 12 2024 at 09:16):

I don't think it's on anyone's radar

Michael Stoll (Apr 12 2024 at 09:18):

So should I revert the change anyway, so that we can profit in case somebody does it after all?

Mario Carneiro (Apr 12 2024 at 09:20):

there is no difference between them, so for now I would say no preference. If we fix it we'll want to make both forms work anyway

Yaël Dillies (Apr 12 2024 at 17:38):

I'm happy with that

Michael Stoll (Apr 12 2024 at 17:45):

Can we then get #12060 merged?


Last updated: May 02 2025 at 03:31 UTC