Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Merged by Bors] - feat: tweak calc widget #22170

Closed
wants to merge 10 commits into from

Conversation

PatrickMassot
Copy link
Member

@PatrickMassot PatrickMassot commented Feb 21, 2025

First make the help text clearer for people who do not know how to select stuff in the info view (in VSCode or lean4web) by mentioning Shift-click.

Then make the indentation handling more robust to various calc layouts.

Also introduces a calc? tactic to complement the calc code action. The issues with the calc code action are:

  1. it requires a valid calc syntax so writing calc is not enough. I
    typically write calc _ to get the code action. But when you write
    calc and there is already something below that could vaguely be
    mistaken for a calc then the code action triggers and the replacement
    swallows the following syntax.
  2. A code action is less visible than a TryThis (but this wouldn’t be
    enough motivation for me without the previous point).

Also get rid of two unreachable! since experience show that those branches can be reached when handling malformed inputs, and the panic is awful for users.


Open in Gitpod

First make the help text clearer for people who do not know how to
select stuff in the info view (in VSCode or lean4web) by mentioning
Shift-click.

Then make the indentation handling more robust to various calc layouts.

Also introduces a calc? tactic to complement the calc code action.
The issues with the calc code action are:
1) it requires a valid calc syntax so writing calc is not enough. I
   typically write calc _ to get the code action. But when you write
   calc and there is already something below that could vaguely be
   mistaken for a calc then the code action triggers and the replacement
   swallows the following syntax.
2) A code action is less visible than a TryThis (but this wouldn’t be
   enough motivation for me without the previous point).
@PatrickMassot PatrickMassot added the t-meta Tactics, attributes or user commands label Feb 21, 2025
@github-actions github-actions bot added the large-import Automatically added label for PRs with a significant increase in transitive imports label Feb 21, 2025
Copy link

github-actions bot commented Feb 21, 2025

PR summary 295dd76460

Import changes exceeding 2%

% File
+3.23% Mathlib.Tactic.Widget.Calc

Import changes for modified files

Dependency changes

File Base Count Head Count Change
Mathlib.Tactic.Widget.Calc 31 32 +1 (+3.23%)
Import changes for all files
Files Import difference
Mathlib.Tactic.Widget.Calc 1

Declarations diff

No declarations were harmed in the making of this PR! 🐙

You can run this locally as follows
## summary with just the declaration names:
./scripts/declarations_diff.sh <optional_commit>

## more verbose report:
./scripts/declarations_diff.sh long <optional_commit>

The doc-module for script/declarations_diff.sh contains some details about this script.


No changes to technical debt.

You can run this locally as

./scripts/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).


open Lean Meta Tactic TryThis in
/-- Create a `calc` proof. -/
elab stx:"calc?" : tactic => do
Copy link
Member

Choose a reason for hiding this comment

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

Can you add a test for this with guard_msgs?

Copy link
Member Author

Choose a reason for hiding this comment

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

Done.

Comment on lines 140 to 141
let s := s!"calc\n{spc}{goalFmt} := by sorry"
addSuggestions (← getRef) #[.suggestion s] (header := "Create calc tactic:")
Copy link
Member

Choose a reason for hiding this comment

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

I think it would be better to construct this from a TSyntax object than from a string; was there a reason you didn't do that?

Copy link
Member

@eric-wieser eric-wieser Feb 22, 2025

Choose a reason for hiding this comment

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

That is

Suggested change
let s := s!"calc\n{spc}{goalFmt} := by sorry"
addSuggestions (← getRef) #[.suggestion s] (header := "Create calc tactic:")
let s ← `(tactic| calc $(← Lean.PrettyPrinter.delab (← getMainTarget)) := by sorry)
addSuggestions (← getRef) #[.suggestion s] (header := "Create calc tactic:")

(and remove all the unused code prior to this line)

Copy link
Member

@eric-wieser eric-wieser Feb 22, 2025

Choose a reason for hiding this comment

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

(there is a bug, leanprover/lean4#7191, in the #guard_msgs result, but the widget works correctly)

Copy link
Member Author

Choose a reason for hiding this comment

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

I specifically want to handle spaces here.

Copy link
Member

Choose a reason for hiding this comment

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

According to my test, the Syntax version already handles the spaces correctly for you in all the ways that matter (everything but guard_msgs).

Copy link
Collaborator

Choose a reason for hiding this comment

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

In case it is helpful, my local test also displays the wrong spacing, but the action really inserts the correct indentation.

Copy link
Member

@eric-wieser eric-wieser Feb 23, 2025

Choose a reason for hiding this comment

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

In case it is helpful, my local test also displays the wrong spacing,

I think it is worth reporting this upstream, but I don't think it is worth working around in this PR. In fact, the workaround here is actually broken if goalFmt contains a newline, but (I assume) works correctly with my suggestion.

@eric-wieser
Copy link
Member

Also get rid of two unreachable! since experience show that those branches can be reached when handling malformed inputs, and the panic is awful for users.

Can you add tests for those cases?

@PatrickMassot
Copy link
Member Author

Also get rid of two unreachable! since experience show that those branches can be reached when handling malformed inputs, and the panic is awful for users.

Can you add tests for those cases?

I’m afraid I don’t have such case at hand. You need student superpowers to trigger such bugs.

@adomani
Copy link
Collaborator

adomani commented Feb 23, 2025

I am happy with the current state of the PR. I have not used widgets much myself, but I imagine that the current PR at least fixes some issues that Patrick saw!

The only remaining issue is the indention: I prefer Eric's solution, but given that it is interlaced with a displaying bug that appears to produce the wrong indentation, maybe you can leave the code as is, but add a comment mentioning leanprover/lean4#7191 and the TSyntax approach?

This results in correct wrapping of the inserted text for multiline suggestions.
Unfortunately, `#guard_msgs` renders things misleading, but there is an upstream patch that will fix that.
@eric-wieser
Copy link
Member

The only remaining issue is the indention: I prefer Eric's solution

To make things easier for you, the last commit I pushed switches to my version, and shows the effect of it on some new tests I added. If you feel really strongly, then feel free to revert that commit (and add the comments @adomani requests in its place).

@PatrickMassot
Copy link
Member Author

I don’t care about TSyntax or String, I simply need this merged.

@eric-wieser
Copy link
Member

In that case

bors merge

@leanprover-community-mathlib4-bot leanprover-community-mathlib4-bot added the ready-to-merge This PR has been sent to bors. label Feb 23, 2025
mathlib-bors bot pushed a commit that referenced this pull request Feb 23, 2025
First make the help text clearer for people who do not know how to select stuff in the info view (in VSCode or lean4web) by mentioning Shift-click.

Then make the indentation handling more robust to various calc layouts.

Also introduces a calc? tactic to complement the calc code action. The issues with the calc code action are:
1) it requires a valid calc syntax so writing calc is not enough. I
   typically write calc _ to get the code action. But when you write
   calc and there is already something below that could vaguely be
   mistaken for a calc then the code action triggers and the replacement
   swallows the following syntax.
2) A code action is less visible than a TryThis (but this wouldn’t be
   enough motivation for me without the previous point).

Also get rid of two `unreachable!` since experience show that those branches can be reached when handling malformed inputs, and the panic is awful for users.



Co-authored-by: Eric Wieser <[email protected]>
@mathlib-bors
Copy link
Contributor

mathlib-bors bot commented Feb 23, 2025

Pull request successfully merged into master.

Build succeeded:

@mathlib-bors mathlib-bors bot changed the title feat: tweak calc widget [Merged by Bors] - feat: tweak calc widget Feb 23, 2025
@mathlib-bors mathlib-bors bot closed this Feb 23, 2025
@mathlib-bors mathlib-bors bot deleted the pm_calc_widget_tweaks branch February 23, 2025 21:42
Julian added a commit that referenced this pull request Feb 24, 2025
* origin/master:
  feat(Polynomial): polynomial sequences are bases for R[X] (#20846)
  feat: monoidal structure on Hopf algebras (#12011)
  feat(DiscreteValuationRing): addVal_eq_zero_iff (#21154)
  refactor(Cache): refactor getPackageDir to not use manually provided package directories (#21817)
  feat(CategoryTheory): categories of homological complexes have a separator (#20229)
  chore(Data/Complex): deprecate `Complex.abs` (#21995)
  feat: uncountable instances for `Ordinal` and isomorphic types (#18547)
  feat(Data/Set/Card): a few missing lemmas (#22186)
  feat: discrete topological spaces are 0-manifolds (#22105)
  feat(Data/Matroid/Loop): matroid loops (#22045)
  feat(SetTheory/Ordinal/Nimber/Field): Nimber division (#19066)
  feat(LinearAlgebra/Pi): add `pi_proj` and `pi_proj_comp` (#22162)
  feat(Data/Matroid/Circuit): matroid cocircuits (#21692)
  feat(Topology/Compactification/OnePoint): generalize instance (#22179)
  feat(Combinatorics/SimpleGraph): takeUntil properties (#21250)
  feat(Tactic): `pnat_to_nat` and `enat_to_nat` tactics (#21602)
  refactor: move `Polynomial.coeffs` and related results (#22225)
  chore: add AlgHom.ker_coe_equiv, resolve porting notes and erws (#22019)
  refactor(Order/Category): `ConcreteCategory` instance for `\omegaCPO` (#21478)
  feat(CategoryTheory): Grothendieck categories have a coseparator (#22224)
  feat: tweak calc widget (#22170)
  feat(CategoryTheory): the Freyd-Mitchell embedding theorem (#22222)
  chore(CategoryTheory): turn more `simp` into `simps!` (#22223)
  feat(CategoryTheory): the category of ind-objects is Grothendieck abelian (#21606)
  feat(AlgebraicTopology/SimplexCategory/GeneratorsRelations/EpiMono): epi-mono factorisation in `SimplexCategoryGenRel` (#21743)
  chore(CategoryTheory/DiscreteCategory): turn `simp` to `simps!` (#22217)
  feat(Analysis/Asymptotics): exponential growth of a sequence (#21178)
  feat(CategoryTheory): sigmaConst preserves monomorphisms (#21599)
  feat(RingTheory/Cotangent): `liftBaseChange` is injective for localizations (#21037)
  chore(CategoryTheory): fix incorrect name (#22210)
  feat(CategoryTheory): `IsPullback` version of 'pullback of iso is iso' (#22211)
  feat(CategoryTheory): pullbacks in functor categories (#22209)
  feat(CategoryTheory): detecting limit cones over connected diagrams (#22192)
  feat(LinearAlgebra): add theorems for injective/surjective/bijective compositions of bilinear maps (#21491)
Champitoad pushed a commit that referenced this pull request Feb 25, 2025
First make the help text clearer for people who do not know how to select stuff in the info view (in VSCode or lean4web) by mentioning Shift-click.

Then make the indentation handling more robust to various calc layouts.

Also introduces a calc? tactic to complement the calc code action. The issues with the calc code action are:
1) it requires a valid calc syntax so writing calc is not enough. I
   typically write calc _ to get the code action. But when you write
   calc and there is already something below that could vaguely be
   mistaken for a calc then the code action triggers and the replacement
   swallows the following syntax.
2) A code action is less visible than a TryThis (but this wouldn’t be
   enough motivation for me without the previous point).

Also get rid of two `unreachable!` since experience show that those branches can be reached when handling malformed inputs, and the panic is awful for users.



Co-authored-by: Eric Wieser <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
large-import Automatically added label for PRs with a significant increase in transitive imports ready-to-merge This PR has been sent to bors. t-meta Tactics, attributes or user commands
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants