chore: clean up TC inference implementation
This PR optimizes the type class search algorithm
-
We don't want trace messages to slow down type class search when trace is not enabled, so I made sure none of these computations are executed unnecessarily. #4282
-
For any new goal,
instantiateMVars (← inferType mvar)is computed at least twice in the current implementation. This is fixed. #4284 -
When passing around metavariable contexts, the current implementation uses a lot of
withMCtx, which is unnecessary. I fixed it so that eachMetavarContextonly needs to be set once at a time. Additionally, I removed the explicit returning ofMetavarContextintryResolveandtryAnswer, because these can instead be passed more naturally through theMetaMstate. #4286 -
The trace messages for
Meta.synthInstance.tryResolveandMeta.synthInstance.resumeand the main/outer trace message have the problem that they have instantiated metavariables in their message that have been assigned during thetryResolveor thetryAnsweror instantiatingoutParams. InisDefEqthe same problem arises (you want to see the unification problem before the unification has happened) and there it is solved usingwithTraceNodeBefore. This has also been fixed, using eitherwithTraceNodeBefore, or an explicitwithMCtx. - one of the tests shows this change. #4285 -
mkTableKeyhas a redundant call tosetMCtx, which I removed. #4281 -
Calls to
instantiateMVarsin trace messages are redundant, so I removed them. #4283 -
The unused function
MapMetaMhas been removed. #4287
The result is a noticeable improvement of TC search speed in Mathlib: 2.8%
Mathlib CI status (docs):
- ❌ Mathlib branch lean-pr-testing-4277 built against this PR, but testing failed. (2024-05-25 13:34:19) View Log
- ❌ Mathlib branch lean-pr-testing-4277 built against this PR, but testing failed. (2024-05-25 15:18:58) View Log
- 🟡 Mathlib branch lean-pr-testing-4277 build against this PR was cancelled. (2024-05-25 17:30:13) View Log
- 💥 Mathlib branch lean-pr-testing-4277 build failed against this PR. (2024-05-25 17:43:42) View Log
- ❌ Mathlib branch lean-pr-testing-4277 built against this PR, but testing failed. (2024-05-25 21:07:09) View Log
- ❌ Mathlib branch lean-pr-testing-4277 built against this PR, but testing failed. (2024-05-26 01:04:46) View Log
- ❌ Mathlib branch lean-pr-testing-4277 built against this PR, but testing failed. (2024-05-26 02:27:03) View Log
- ❌ Mathlib branch lean-pr-testing-4277 built against this PR, but testing failed. (2024-05-26 14:26:19) View Log
- ❌ Mathlib branch lean-pr-testing-4277 built against this PR, but the archive failed. (2024-05-26 16:35:24) View Log
- 🟡 Mathlib branch lean-pr-testing-4277 build against this PR was cancelled. (2024-06-06 23:09:58) View Log
- ❌ Mathlib branch lean-pr-testing-4277 built against this PR, but testing failed. (2024-06-07 00:09:47) View Log
Closing as per reasoning in #4379