Skip to content

feat(Translate): better relevant_arg heuristic - #43801

Open
JovanGerb wants to merge 4 commits into
leanprover-community:masterfrom
JovanGerb:Jovan-relevant_arg-3
Open

JovanGerb wants to merge 4 commits into
leanprover-community:masterfrom
JovanGerb:Jovan-relevant_arg-3

Conversation

@JovanGerb

Copy link
Copy Markdown
Contributor

This PR improves the heuristic around relevant_arg that to_dual and to_additive use. Currently, the we eagerly set (relevant_arg := 1) whenever we cannot find any relevant argument. This is problematic in a setting where there really should be no relevant argument, such as for ComplexShape.

After this PR, we only default to (relevant_arg := 1) for types (i.e. anything whose type is Sort _). For terms that aren't types, we instead purely rely on the algorithm that determines the relevant argument (which was added in #35318). In order for this algorithm to work in all cases, relevant_arg needs to be set on all relevant types. This includes some types that are dual to themselves, like LE and LinearOrder, and hence do not have a to_dual attribute (and hence also don't have a relevant_arg). To fix this, I added the following trick: when we add a translation for a class projection or class constructor, and if that class has no translation, then we first add a translation from that class to itself.

As a result, classes like LE and LinearOrder are now part of the translation dictionary, and are being translated into themselves.


Open in Gitpod

@github-actions

github-actions Bot commented Sep 14, 2026

Copy link
Copy Markdown

PR summary 8077d16145

Import changes for modified files

No significant changes to the import graph

Import changes for all files
Files Import difference

Declarations diff (regex)

+ SomeClass
+ SomeStructure
+ ensureClassTranslated

You can run this locally as follows
## from your `mathlib4` directory:
git clone https://github.com/leanprover-community/mathlib-ci.git ../mathlib-ci

## summary with just the declaration names:
../mathlib-ci/scripts/pr_summary/declarations_diff.sh <optional_commit>

## more verbose report:
../mathlib-ci/scripts/pr_summary/declarations_diff.sh long <optional_commit>

The doc-module for scripts/pr_summary/declarations_diff.sh in the mathlib-ci repository contains some details about this script.

Declarations diff (Lean -- stale, waiting for the new build)

Lean-aware diff — post-build, computed from the Lean environment (commit df1ccc6).

  • +1 new declarations
  • −0 removed declarations
+Mathlib.Tactic.Translate.ensureClassTranslated

No changes to strong technical debt.
No changes to weak technical debt.

Current commit 8077d16145
Reference commit 3a33b9d429

This script lives in the mathlib-ci repository. To run it locally, from your mathlib4 directory:

git clone https://github.com/leanprover-community/mathlib-ci.git ../mathlib-ci
../mathlib-ci/scripts/reporting/technical-debt-metrics.py 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-meta Tactics, attributes or user commands label Sep 14, 2026
@JovanGerb

Copy link
Copy Markdown
Contributor Author

!radar

@leanprover-radar

leanprover-radar commented Sep 14, 2026

Copy link
Copy Markdown

Benchmark results for df1ccc6 against 3a33b9d are in. No significant results found. @JovanGerb

  • build//instructions: -50.3M (-0.00%)

No significant changes detected.

@Vierkantor Vierkantor left a comment

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'm not completely sure about the default behaviour. But the heuristic seems to be a strict improvement at least, so I can easily be convinced that we are in a local maximum :)

Comment thread Mathlib/Tactic/Translate/Core.lean Outdated
with `relevant_arg` defaulting to the first argument. -/
translateClass (cls : Name) : CoreM Unit := do
if (findTranslation? (← getEnv) t cls).isNone then
modifyEnv (t.translations.addEntry · (cls, { translation := cls, relevantArg := .arg 0 }))

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 can imagine that the first argument is often the right one, but it seems like it might misbehave on something unbundled like IsOrderedSMul? (Or maybe even IsOrderConnected?)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks, this could indeed go wrong (although I don't think there are examples in practice). I've fixed it by running the translation from the type of cls to itself, and using the relevant_arg inferred from that.

@Vierkantor Vierkantor added the awaiting-author Reply -awaiting-author to remove the label on your PR once you have addressed all comments. label Sep 16, 2026
@Vierkantor Vierkantor self-assigned this Sep 16, 2026
@JovanGerb JovanGerb removed the awaiting-author Reply -awaiting-author to remove the label on your PR once you have addressed all comments. label Sep 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

t-meta Tactics, attributes or user commands

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants