Skip to content

[Merged by Bors] - chore(scripts): add module-level set_option helpers and simp-arg / long-line fixers - #38602

Closed
kim-em wants to merge 8 commits into
leanprover-community:masterfrom
kim-em:scripts-from-lean-pr-13492
Closed

kim-em wants to merge 8 commits into
leanprover-community:masterfrom
kim-em:scripts-from-lean-pr-13492

Conversation

@kim-em

@kim-em kim-em commented Apr 28, 2026

Copy link
Copy Markdown
Contributor

This PR cherry-picks a set of adaptation-helper scripts that @nomeata developed on the lean-pr-testing-13492 branch during the adaptation for leanprover/lean4#13492 (now merged). He flagged on Zulip (thread) that he plans to delete them from the adaptation branch and offered them up for adoption.

Changes:

  • scripts/add_set_option.py, scripts/rm_set_option.py: new --value flag, so the scripts work for options whose default is false (where you want to insert set_option X true in) as well as the existing false case.
  • scripts/add_module_set_option.py (new): file-level variant of add_set_option.py — inserts a module-scoped set_option after the imports (and module docstring, if present) rather than scoped to a declaration. Needed for eqn-affecting options.
  • scripts/rm_module_set_option.py (new): file-level variant of rm_set_option.py. Traverses the import DAG backward, with resume support keyed on toolchain.
  • scripts/fix_nonlocal_set_option.py (new): given failing modules, adds the option to all transitive deps then binary-search minimizes back to the smallest set whose modules need it.
  • scripts/fix_unused_simp_args.py (new): parses This simp argument is unused: X warnings from lake build and removes X from the corresponding simp / simp only call. For ← X it rewrites to - X because the ← has simp-set side effects.
  • scripts/fix_long_lines.py (new): breaks too-long lines at the last comma before column 100 and indents the continuation. Useful as a follow-up to scripts that rewrite simp lists.
  • scripts/README.md: documents all of the above.

The defaults for --option in the new module-level scripts point at backward.defeq.atInstanceTransparency (the option from leanprover/lean4#13492); for current options pass --option <name> explicitly.

Original commits from lean-pr-testing-13492:

  • 6f1a3512906 chore(scripts): add --value flag to add/rm_set_option.py (had a small conflict with [Merged by Bors] - fix(scripts/rm_set_option.py): make more usable in downstream projects #38532's lakefile.lean existence check; resolved by combining)
  • 36625b2a675 chore(scripts): bring back module-level / nonlocal scripts
  • de2a59b5839 chore(scripts): thread --value through fix_nonlocal_set_option.py
  • 45f3ee41b94 (extracted) fix_unused_simp_args.py
  • 4278b885305 (extracted) fix_long_lines.py
  • 80852596213 doc(scripts): document new helpers

I sanity-checked each script: all parse, all --helps render, and fix_long_lines.py / fix_unused_simp_args.py produce the expected output on synthetic inputs.

🤖 Prepared with Claude Code

nomeata and others added 6 commits April 28, 2026 02:51
…ption.py`

Previously the scripts assumed `set_option X false in` — fine for backward
options that default to `true` (e.g. `backward.isDefEq.respectTransparency`).
The new `backward.defeqAttrib.useBackward` option has the opposite shape
(default `false`, set `true` to opt into legacy behavior), so the helpers
need to parametrize the value they insert and match.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
Restore `add_module_set_option.py` / `rm_module_set_option.py` /
`fix_nonlocal_set_option.py` from the previous adaptation branch, with
the `--value` flag threaded through so they work for options whose
default is `false` (like `backward.defeqAttrib.useBackward`).

These are needed for the subset of modules where the option must be
set at file level (not `set_option ... in`), because
`backward.defeqAttrib.useBackward` is now an eqn-affecting option — its
value at a defining module's compile time is stored and replayed at
equation realization time in downstream modules.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
Allow `--value true` (matching add_set_option.py). Without this, the
script hard-coded 'false' and was unusable for restoring legacy behavior.
Extracted from 45f3ee4 on the lean-pr-testing-13492 branch.

Parses lake build warnings of the form 'This simp argument is unused: X'
and rewrites the simp call to drop X. For '← X' arguments it rewrites
to '- X' instead, because the '←' form has side effects on the simp
set even when the rewrite itself is unused.

Usage: lake build 2>&1 | scripts/fix_unused_simp_args.py

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
Extracted from 4278b88 on the lean-pr-testing-13492 branch.

Breaks too-long lines at the last comma before column 100 and indents
the continuation. Takes path:line pairs as args; intended as a
follow-up to scripts that rewrite simp lists (which occasionally leave
lines over the 100-char limit).

Usage: scripts/fix_long_lines.py path:line ...

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
…_lines.py, *_module_set_option.py, fix_nonlocal_set_option.py)
@github-actions

github-actions Bot commented Apr 28, 2026 •

Copy link
Copy Markdown

PR summary 9ccb29abfa

Import changes for modified files

No significant changes to the import graph

Import changes for all files
Files Import difference

Declarations diff

+ FileResult:
+ _RemoveDisplay(Display):
+ _bare_name(s:
+ _collect_all_dependencies(dag:
+ _collect_all_dependents(dag:
+ _current_toolchain()
+ add_to_file(filepath:
+ arg_matches(item_text:
+ bisect_remove(
+ collect_all_dependencies(dag:
+ file_sha256(path:
+ find_insert_point(lines:
+ find_simp_call(
+ fix_line(text:
+ has_option(filepath:
+ init_progress():
+ lake_build_modules(modules:
+ load_progress()
+ make_process_file(
+ module_set_option_pattern(option:
+ parse_log(lines:
+ remove_arg(
+ remove_option(filepath:
+ restore_file(filepath:
+ save_progress(module:
+ split_top_level(args_text:
++ main()
+++ main():
+-+ scan_files(dag:

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.


No changes to technical debt.

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.sh 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).

⚠️ Scripts folder reminder

This PR adds files under scripts/.
Please consider whether each added script belongs in this repository or in leanprover-community/mathlib-ci.

A script belongs in mathlib-ci if it is a CI automation script that interacts with GitHub (e.g. managing labels, posting comments, triggering bots), runs from a trusted external checkout in CI, or requires access to secrets.

A script belongs in this repository (scripts/) if it is a developer or maintainer tool to be run locally, a code maintenance or analysis utility, a style linting tool, or a data file used by the library's own linters.

See the mathlib-ci README for more details.

Added scripts files:

  • scripts/add_module_set_option.py
  • scripts/fix_long_lines.py
  • scripts/fix_nonlocal_set_option.py
  • scripts/fix_unused_simp_args.py
  • scripts/rm_module_set_option.py

@github-actions github-actions Bot added the CI Modifies the continuous integration setup or other automation label Apr 28, 2026
@bryangingechen

Copy link
Copy Markdown
Contributor

Could you make sure these are usable to some extent by downstream projects? @kbuzzard was running rm_set_option.py at least on FLT and I can imagine some of the new scripts being useful for downstream adaptations at some point.

kim-em and others added 2 commits April 29, 2026 03:39
Address Bryan's review on leanprover-community#38602.  Drops the hardcoded `Mathlib/`
filters from the new module-level helpers, threads `--directories`
through them (mirroring `rm_set_option.py`), and skips `.lake/` in
the shared DAG walk so the whole-project default doesn't touch
vendored dependencies.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
…users

Cribbed from kbuzzard's hands-on Zulip diary of getting `rm_set_option.py`
running on FLT — turning each thing he tripped on into a fix:

- friendlier ImportError when sibling Python files are missing from the
  user's `scripts/` (the "this is a mathlib script, not a pip package"
  hint kbuzzard had to discover by trial)
- detect `lakefile.toml` and print a one-liner reminding the user to
  remove option entries from `leanOptions` manually (we only edit
  `lakefile.lean`)
- warn when `from_directories` finds no `.lean` files at all, suggesting
  `--directories <root>` rather than silently producing "0 modules parsed"
- replace the prose portability paragraph in `scripts/README.md` with a
  numbered downstream recipe

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@kim-em

kim-em commented Apr 29, 2026

Copy link
Copy Markdown
Contributor Author

Good call. Done in 1992790 + d4b48cc, after reading @kbuzzard's saga getting rm_set_option.py running on FLT. Your #38532 already handled two of his blockers — thanks! Remaining deltas:

  • New helpers no longer hardcode "Mathlib/" (fix_nonlocal_set_option.py was a silent no-op outside mathlib).
  • --directories <root> on every script that touches the DAG.
  • Friendlier ModuleNotFoundError ("this is a mathlib script, not a pip package").
  • lakefile.toml notice when lakefile.lean is absent (we don't try to edit the TOML).
  • "0 modules parsed" now suggests --directories.
  • README has a numbered downstream recipe.

Caveats: add_module_set_option.py no longer restricts to Mathlib/, so it now also touches Archive/Counterexamples/MathlibTest/Cache (~500 extra files); pass --directories Mathlib for the old scope. And lean_lib Foo where srcDir := "src" projects still get wrong module names — documented, but a proper fix means teaching dag_traversal about Lake source roots, which feels like separate scope.

@bryangingechen

Copy link
Copy Markdown
Contributor

Thanks!
bors r+

mathlib-bors Bot pushed a commit that referenced this pull request Apr 29, 2026
…ng-line fixers (#38602)

This PR cherry-picks a set of adaptation-helper scripts that @nomeata developed on the `lean-pr-testing-13492` branch during the adaptation for leanprover/lean4#13492 (now merged). He flagged on Zulip ([thread](https://leanprover.zulipchat.com/#narrow/channel/428973-nightly-testing/topic/lean.2313492.2C.20stricter.20.40.5Bdefeq.5D.20inference/near/591078800)) that he plans to delete them from the adaptation branch and offered them up for adoption.

Changes:

- `scripts/add_set_option.py`, `scripts/rm_set_option.py`: new `--value` flag, so the scripts work for options whose default is `false` (where you want to insert `set_option X true in`) as well as the existing `false` case.
- `scripts/add_module_set_option.py` (new): file-level variant of `add_set_option.py` — inserts a module-scoped `set_option` after the imports (and module docstring, if present) rather than scoped to a declaration. Needed for eqn-affecting options.
- `scripts/rm_module_set_option.py` (new): file-level variant of `rm_set_option.py`. Traverses the import DAG backward, with resume support keyed on toolchain.
- `scripts/fix_nonlocal_set_option.py` (new): given failing modules, adds the option to all transitive deps then binary-search minimizes back to the smallest set whose modules need it.
- `scripts/fix_unused_simp_args.py` (new): parses `This simp argument is unused: X` warnings from `lake build` and removes `X` from the corresponding `simp` / `simp only` call. For `← X` it rewrites to `- X` because the `←` has simp-set side effects.
- `scripts/fix_long_lines.py` (new): breaks too-long lines at the last comma before column 100 and indents the continuation. Useful as a follow-up to scripts that rewrite simp lists.
- `scripts/README.md`: documents all of the above.

The defaults for `--option` in the new module-level scripts point at `backward.defeq.atInstanceTransparency` (the option from leanprover/lean4#13492); for current options pass `--option <name>` explicitly.

Original commits from `lean-pr-testing-13492`:
- `6f1a3512906` chore(scripts): add `--value` flag to add/rm_set_option.py (had a small conflict with #38532's `lakefile.lean` existence check; resolved by combining)
- `36625b2a675` chore(scripts): bring back module-level / nonlocal scripts
- `de2a59b5839` chore(scripts): thread `--value` through `fix_nonlocal_set_option.py`
- `45f3ee41b94` (extracted) `fix_unused_simp_args.py`
- `4278b885305` (extracted) `fix_long_lines.py`
- `80852596213` doc(scripts): document new helpers

I sanity-checked each script: all parse, all `--help`s render, and `fix_long_lines.py` / `fix_unused_simp_args.py` produce the expected output on synthetic inputs.

🤖 Prepared with Claude Code

Co-authored-by: Joachim Breitner <[email protected]>
@mathlib-triage mathlib-triage Bot added the ready-to-merge This PR has been sent to bors. label Apr 29, 2026
@mathlib-bors

mathlib-bors Bot commented Apr 29, 2026

Copy link
Copy Markdown
Contributor

Pull request successfully merged into master.

Build succeeded:

@mathlib-bors mathlib-bors Bot changed the title chore(scripts): add module-level set_option helpers and simp-arg / long-line fixers [Merged by Bors] - chore(scripts): add module-level set_option helpers and simp-arg / long-line fixers Apr 29, 2026
@mathlib-bors mathlib-bors Bot closed this Apr 29, 2026
xroblot pushed a commit to xroblot/mathlib4 that referenced this pull request May 2, 2026
…ng-line fixers (leanprover-community#38602)

This PR cherry-picks a set of adaptation-helper scripts that @nomeata developed on the `lean-pr-testing-13492` branch during the adaptation for leanprover/lean4#13492 (now merged). He flagged on Zulip ([thread](https://leanprover.zulipchat.com/#narrow/channel/428973-nightly-testing/topic/lean.2313492.2C.20stricter.20.40.5Bdefeq.5D.20inference/near/591078800)) that he plans to delete them from the adaptation branch and offered them up for adoption.

Changes:

- `scripts/add_set_option.py`, `scripts/rm_set_option.py`: new `--value` flag, so the scripts work for options whose default is `false` (where you want to insert `set_option X true in`) as well as the existing `false` case.
- `scripts/add_module_set_option.py` (new): file-level variant of `add_set_option.py` — inserts a module-scoped `set_option` after the imports (and module docstring, if present) rather than scoped to a declaration. Needed for eqn-affecting options.
- `scripts/rm_module_set_option.py` (new): file-level variant of `rm_set_option.py`. Traverses the import DAG backward, with resume support keyed on toolchain.
- `scripts/fix_nonlocal_set_option.py` (new): given failing modules, adds the option to all transitive deps then binary-search minimizes back to the smallest set whose modules need it.
- `scripts/fix_unused_simp_args.py` (new): parses `This simp argument is unused: X` warnings from `lake build` and removes `X` from the corresponding `simp` / `simp only` call. For `← X` it rewrites to `- X` because the `←` has simp-set side effects.
- `scripts/fix_long_lines.py` (new): breaks too-long lines at the last comma before column 100 and indents the continuation. Useful as a follow-up to scripts that rewrite simp lists.
- `scripts/README.md`: documents all of the above.

The defaults for `--option` in the new module-level scripts point at `backward.defeq.atInstanceTransparency` (the option from leanprover/lean4#13492); for current options pass `--option <name>` explicitly.

Original commits from `lean-pr-testing-13492`:
- `6f1a3512906` chore(scripts): add `--value` flag to add/rm_set_option.py (had a small conflict with leanprover-community#38532's `lakefile.lean` existence check; resolved by combining)
- `36625b2a675` chore(scripts): bring back module-level / nonlocal scripts
- `de2a59b5839` chore(scripts): thread `--value` through `fix_nonlocal_set_option.py`
- `45f3ee41b94` (extracted) `fix_unused_simp_args.py`
- `4278b885305` (extracted) `fix_long_lines.py`
- `80852596213` doc(scripts): document new helpers

I sanity-checked each script: all parse, all `--help`s render, and `fix_long_lines.py` / `fix_unused_simp_args.py` produce the expected output on synthetic inputs.

🤖 Prepared with Claude Code

Co-authored-by: Joachim Breitner <[email protected]>
gaetanserre pushed a commit to gaetanserre/mathlib4 that referenced this pull request May 5, 2026
…ng-line fixers (leanprover-community#38602)

This PR cherry-picks a set of adaptation-helper scripts that @nomeata developed on the `lean-pr-testing-13492` branch during the adaptation for leanprover/lean4#13492 (now merged). He flagged on Zulip ([thread](https://leanprover.zulipchat.com/#narrow/channel/428973-nightly-testing/topic/lean.2313492.2C.20stricter.20.40.5Bdefeq.5D.20inference/near/591078800)) that he plans to delete them from the adaptation branch and offered them up for adoption.

Changes:

- `scripts/add_set_option.py`, `scripts/rm_set_option.py`: new `--value` flag, so the scripts work for options whose default is `false` (where you want to insert `set_option X true in`) as well as the existing `false` case.
- `scripts/add_module_set_option.py` (new): file-level variant of `add_set_option.py` — inserts a module-scoped `set_option` after the imports (and module docstring, if present) rather than scoped to a declaration. Needed for eqn-affecting options.
- `scripts/rm_module_set_option.py` (new): file-level variant of `rm_set_option.py`. Traverses the import DAG backward, with resume support keyed on toolchain.
- `scripts/fix_nonlocal_set_option.py` (new): given failing modules, adds the option to all transitive deps then binary-search minimizes back to the smallest set whose modules need it.
- `scripts/fix_unused_simp_args.py` (new): parses `This simp argument is unused: X` warnings from `lake build` and removes `X` from the corresponding `simp` / `simp only` call. For `← X` it rewrites to `- X` because the `←` has simp-set side effects.
- `scripts/fix_long_lines.py` (new): breaks too-long lines at the last comma before column 100 and indents the continuation. Useful as a follow-up to scripts that rewrite simp lists.
- `scripts/README.md`: documents all of the above.

The defaults for `--option` in the new module-level scripts point at `backward.defeq.atInstanceTransparency` (the option from leanprover/lean4#13492); for current options pass `--option <name>` explicitly.

Original commits from `lean-pr-testing-13492`:
- `6f1a3512906` chore(scripts): add `--value` flag to add/rm_set_option.py (had a small conflict with leanprover-community#38532's `lakefile.lean` existence check; resolved by combining)
- `36625b2a675` chore(scripts): bring back module-level / nonlocal scripts
- `de2a59b5839` chore(scripts): thread `--value` through `fix_nonlocal_set_option.py`
- `45f3ee41b94` (extracted) `fix_unused_simp_args.py`
- `4278b885305` (extracted) `fix_long_lines.py`
- `80852596213` doc(scripts): document new helpers

I sanity-checked each script: all parse, all `--help`s render, and `fix_long_lines.py` / `fix_unused_simp_args.py` produce the expected output on synthetic inputs.

🤖 Prepared with Claude Code

Co-authored-by: Joachim Breitner <[email protected]>
gasparattila pushed a commit to gasparattila/mathlib4 that referenced this pull request May 9, 2026
…ng-line fixers (leanprover-community#38602)

This PR cherry-picks a set of adaptation-helper scripts that @nomeata developed on the `lean-pr-testing-13492` branch during the adaptation for leanprover/lean4#13492 (now merged). He flagged on Zulip ([thread](https://leanprover.zulipchat.com/#narrow/channel/428973-nightly-testing/topic/lean.2313492.2C.20stricter.20.40.5Bdefeq.5D.20inference/near/591078800)) that he plans to delete them from the adaptation branch and offered them up for adoption.

Changes:

- `scripts/add_set_option.py`, `scripts/rm_set_option.py`: new `--value` flag, so the scripts work for options whose default is `false` (where you want to insert `set_option X true in`) as well as the existing `false` case.
- `scripts/add_module_set_option.py` (new): file-level variant of `add_set_option.py` — inserts a module-scoped `set_option` after the imports (and module docstring, if present) rather than scoped to a declaration. Needed for eqn-affecting options.
- `scripts/rm_module_set_option.py` (new): file-level variant of `rm_set_option.py`. Traverses the import DAG backward, with resume support keyed on toolchain.
- `scripts/fix_nonlocal_set_option.py` (new): given failing modules, adds the option to all transitive deps then binary-search minimizes back to the smallest set whose modules need it.
- `scripts/fix_unused_simp_args.py` (new): parses `This simp argument is unused: X` warnings from `lake build` and removes `X` from the corresponding `simp` / `simp only` call. For `← X` it rewrites to `- X` because the `←` has simp-set side effects.
- `scripts/fix_long_lines.py` (new): breaks too-long lines at the last comma before column 100 and indents the continuation. Useful as a follow-up to scripts that rewrite simp lists.
- `scripts/README.md`: documents all of the above.

The defaults for `--option` in the new module-level scripts point at `backward.defeq.atInstanceTransparency` (the option from leanprover/lean4#13492); for current options pass `--option <name>` explicitly.

Original commits from `lean-pr-testing-13492`:
- `6f1a3512906` chore(scripts): add `--value` flag to add/rm_set_option.py (had a small conflict with leanprover-community#38532's `lakefile.lean` existence check; resolved by combining)
- `36625b2a675` chore(scripts): bring back module-level / nonlocal scripts
- `de2a59b5839` chore(scripts): thread `--value` through `fix_nonlocal_set_option.py`
- `45f3ee41b94` (extracted) `fix_unused_simp_args.py`
- `4278b885305` (extracted) `fix_long_lines.py`
- `80852596213` doc(scripts): document new helpers

I sanity-checked each script: all parse, all `--help`s render, and `fix_long_lines.py` / `fix_unused_simp_args.py` produce the expected output on synthetic inputs.

🤖 Prepared with Claude Code

Co-authored-by: Joachim Breitner <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CI Modifies the continuous integration setup or other automation ready-to-merge This PR has been sent to bors.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants