Skip to content

feat: reorder tc subgoals according to out-params - #2174

Merged
gebner merged 8 commits into
leanprover:masterfrom
gebner:tcreorder
Apr 10, 2023
Merged

gebner merged 8 commits into
leanprover:masterfrom
gebner:tcreorder

Conversation

@gebner

@gebner gebner commented Mar 28, 2023 •

Copy link
Copy Markdown
Member

Part of #2117, hopefully with less regression on core.

The order in which subgoals are synthesized is automatically determined when adding an instance to the environment. If you have subgoals like [Monoid A] [Monoid B] [MulHomClass F A B], then they are processed in the order 2, 0, 1. This removes the need to mark arguments to instances as implicit instead of instance-implicit.

To make it possible to compute this order, we add an additional semiOutParam gadget. One way to think of it is "like an out-param, but with multiple results." It means that all instances of the class need to provide a concrete value for this parameter. For example, the first argument of Coe is a semi-out-param, so instance : Coe α Nat where coe _ := 42 is not a valid instance because it does not assign the α (the instance needs to be a CoeHead instead).

@gebner

gebner commented Mar 28, 2023

Copy link
Copy Markdown
Member Author

!bench

@leanprover-bot

Copy link
Copy Markdown
Collaborator

Here are the benchmark results for commit 37d1449.
There were significant changes against commit 39f0fa6:

  Benchmark   Metric          Change
  ============================================
- stdlib      type checking     1.3% (227.2 σ)

@gebner
gebner marked this pull request as ready for review March 31, 2023 00:36
Comment thread src/Init/Prelude.lean Outdated
Semi-output parameters influence the order in which arguments to type class
instances are processed. Lean determines an order where all non-(semi-)output
parameters to the instance argument have to be figured out before attempting to
synthesize an argument (that is, they do not contain metavariables). This

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Just to clarify, this is not strictly true as the inputs to TC synth could already contain mvars (which may or may not make synth get stuck), no? We just don't want class input parameters to... add even more of them.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yes, this is indeed a bit tricky to describe. I've added the qualifier "assignable" and an explanation that these mvars are created during TC synthesis.

Comment thread src/Init/Prelude.lean Outdated
```
class Coe (α : semiOutParam (Type u)) (β : Type v)
```
This means that all `Coe` instances should provide a concrete value for `α`.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
This means that all `Coe` instances should provide a concrete value for `α`.
This means that all `Coe` instances should provide a concrete value for `α` given a concrete value for `β`.

Here again it's not quite clear to me what "concrete" means in presence of nested mvars. The Coe α (Option α) instance will not get stuck when α is an mvar, will it?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Right, it won't get stuck.

Comment thread src/Lean/Expr.lean Resolved
Comment thread src/Lean/Meta/Instances.lean Resolved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants