Zulip Chat Archive

Stream: mathlib4

Topic: docstrings of `nth_rewrite` and `nth_rw`


Shreyas Srinivas (Jun 15 2024 at 11:03):

Currently the docstring of nth_rewrite gives no indicatinn of the syntax for specifying the occurrence number. I propose to add examples to it. nth_rw simply tells the user that nth_rw is the same as nth_rewrite with some rfl stuff. So a new user must first hunt around for nth_rewrite (or type it in to check the docstring). I suggest copying and adapting the docstring of nth_rewrite.

Kim Morrison (Jun 16 2024 at 00:14):

Please do!

Shreyas Srinivas (Jun 16 2024 at 13:35):

mathlib4#13877

Shreyas Srinivas (Jun 17 2024 at 09:43):

Found what might be a potential footgun for an unsuspecting user:

import Mathlib


-- footgun demo
example {a b : } (h : a = b + a) : a + a + a + a + a = b := by
  nth_rw 2 [h, h, h, h] -- footnote on infoview
  sorry

/-
footnote:
ab: ℝ
h: a = b + a
⊢ a + (b + (b + (b + (b + a)))) + a + a + a = b
-/

if the LHS of h is contained as a subterm in the RHS of h, then in successive nth_rws the new subterm will be rewritten over and over (or affect the occurrence count). I can document this too, but it sounds like a massive footgun either way.

Richard Copley (Jun 17 2024 at 09:54):

What is the desired behaviour? (I don't understand the problem. It seems to be doing exactly what it was asked to do.)

Shreyas Srinivas (Jun 17 2024 at 09:59):

It is tough to reason about the behaviour when you have complex equations and many of them instead of the four simple hs I have put in there. For example, if there's an unexpected occurrence of a in some long theorem statement it can heavily confuse users. Instead of eliminating the a's in the goal one by one you are simply substituting into the new a you have accidentally introduced. Basically binder substitution is hard.

Shreyas Srinivas (Jun 17 2024 at 10:00):

Anyway, I am just goiing to document this now and leave it at that. This might be worth a separate discussion.

Kim Morrison (Jun 17 2024 at 10:42):

Using nth_rw with multiple lemmas is kind of insane to begin with.

Shreyas Srinivas (Jun 17 2024 at 10:43):

I found at least one such occurrence of nth_rewrite in mathlib

Shreyas Srinivas (Jun 17 2024 at 11:54):

I added a brief one-liner to explain how this works. The PR is ready for review (It's building but I guess docstrings shouldn't affect the build).

Shreyas Srinivas (Jun 17 2024 at 11:59):

Some occurrences of nth_rewrite with multiples lemmas: here, here, here, and a large one here

Shreyas Srinivas (Jun 17 2024 at 12:03):

And one for nth_rw here

Shreyas Srinivas (Jun 24 2024 at 11:20):

I updated the docstrings of the PR (mathlib4#13877) this morning to add some examples.

Shreyas Srinivas (Jun 24 2024 at 11:21):

I request feedback on these. I just picked the simplest example that I think showed how it all works

Jon Eugster (Jun 24 2024 at 12:35):

I left you some comments :) not an official reviewer though, so take it with a grain of salt

Shreyas Srinivas (Jun 24 2024 at 12:35):

I liked the comments. I am responding to them right away.

Shreyas Srinivas (Jun 24 2024 at 12:44):

About that Nat.add_comm example: Is it correct to say nth_rw rewrites the nth occurrence of an expression in precedence order?

Shreyas Srinivas (Jun 24 2024 at 12:45):

I am trying to write things in a less computer sciencey way, so referring to things like expression trees is probably not a good way to go EDIT : made the necessary changes and used your example to explain why order of precedence can make thigns weird.

Shreyas Srinivas (Jun 24 2024 at 12:50):

About the extendDocs solution for the identical copies of docstrings for nth_rewrite and nth_rw, it would work better if one could hygienically substitute parameters representing the tactic name in this case into a docstring, like the s!"..{}" format strings. Is that possible in docstring comments?

Shreyas Srinivas (Jun 24 2024 at 12:56):

Also, @Kim Morrison : The note about the change of occurrence count from 0 to 1 between mathlib3 and mathlib4 appears redundant and pointless after the port. Shall I remove it?

Jon Eugster (Jun 24 2024 at 14:00):

Shreyas Srinivas said:

About the extendDocs solution for the identical copies of docstrings for nth_rewrite and nth_rw, it would work better if one could hygienically substitute parameters representing the tactic name in this case into a docstring, like the s!"..{}" format strings. Is that possible in docstring comments?

I don't think that's possible unfortunately, and that would need a feature request to core, I'm afraid. Would be a nice feature though.

Shreyas Srinivas (Jun 27 2024 at 12:16):

I think this PR is ready to merge. It has 3 approving reviews and passes all checks

Ruben Van de Velde (Jun 27 2024 at 12:18):

I assume you refer to #13877

Shreyas Srinivas (Jun 27 2024 at 12:18):

yes

Shreyas Srinivas (Jun 27 2024 at 12:21):

It was already labelled maintainer merge

Ruben Van de Velde (Jun 27 2024 at 12:25):

Yes, but the value of the comment is not in the label, but in the bot that posts to a private zulip stream


Last updated: May 02 2025 at 03:31 UTC