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

RFC: add the option to add a prefix and a suffix to a doc-string when using inherit_doc #2622

Open
adomani opened this issue Oct 1, 2023 · 3 comments

Comments

@adomani
Copy link

adomani commented Oct 1, 2023

Proposal

Currently, @[inherit_doc foo] def bar ... copies the doc-string of foo to bar.

The proposal is to add the option of extending the docs of foo by prefixing and suffixing extra strings.

Here is a sample usage

/-- `foo` is `1` -/
def foo : ℕ := 1

@[inherit_doc foo
  before "`fooPlusOne` looks like `foo`:"
  after "... except that it adds one to it."]
def fooPlusOne : ℕ := foo + 1

#check fooPlusOne
-- hover over `fooPlusOne` shows
/-
`fooPlusOne` looks like `foo`:

`foo` is 1

... except that it adds one to it.
-/
  • User Experience: This allows to copy over docs from "parent" declarations and highlight how children differ from their parents.

  • Beneficiaries: The proposal arose from discussions in Mathlib. Relevant Zulip thread.

  • Maintainability: I have no expertise in answering this question, but my naive impression is that it would not require much maintenance.

Community Feedback

The Zulip thread mentioned above originated from the current doc-string for rw, which mentions rewrite but not what rw actually does.

With the current proposal, the docs for rw could be inherited from rewrite, preceded with the sentence that is currently the doc-string for rw.

Impact

Add 👍 to issues you consider important. If others benefit from the changes in this proposal being added, please ask them to add 👍 to it.

@semorrison
Copy link
Collaborator

@adomani, I don't think this belongs in core. Is there any reason it can't go in Std?

@semorrison semorrison self-assigned this Oct 2, 2023
@adomani
Copy link
Author

adomani commented Oct 2, 2023

@semorrison, I was simply going by what I thought that Mario had suggested in the Zulip chat. I do not mind if it goes in Std or core, though.

@semorrison semorrison removed their assignment Oct 3, 2023
@semorrison semorrison transferred this issue from leanprover/lean4 Oct 3, 2023
@digama0
Copy link
Collaborator

digama0 commented Oct 4, 2023

@semorrison Yes, this has to be implemented in core, because inherit_doc is specifically handled by some other definitions like notation. If you try to implement a copy of inherit_doc with additional syntax and behavior (as Damiano did), you get errors when using it with notation in the same way as inherit_doc. To fix this you would have to replace notation, which will cause more knock on effects requiring even more replacements.

@semorrison semorrison transferred this issue from leanprover-community/batteries Oct 4, 2023
mathlib-bors bot pushed a commit to leanprover-community/mathlib4 that referenced this issue Dec 27, 2023
This PR is related to [Issue leanprover/lean4#2622](leanprover/lean4#2622).

In the file where declaration `decl` is defined, writing
```lean
extend_doc decl
  before "I will be added as a prefix to the docs of `decl`"
  after "I will be added as a suffix to the docs of `decl`"
```

does what is probably clear: it extends the doc-string of `decl` by adding the string immediately following `before` at the beginning and the string immediately following`after` at the end.

This is useful, for instance, when the docs of `decl` are obtained via `inherit_doc`.

[Zulip discussion](https://leanprover.zulipchat.com/#narrow/stream/270676-lean4/topic/tactic.20docstrings)

By way of example, I redefine the docs over `rw`.  The new doc-string is

```lean
`rw` is like `rewrite` (see below), but also tries to close the goal by "cheap" (reducible) `rfl` afterwards.

`rewrite [e]` applies identity `e` as a rewrite rule to the target of the main goal.
If `e` is preceded by left arrow (`←` or `<-`), the rewrite is applied in the reverse direction.
If `e` is a defined constant, then the equational theorems associated with `e` are used.
This provides a convenient way to unfold `e`.
- `rewrite [e₁, ..., eₙ]` applies the given rules sequentially.
- `rewrite [e] at l` rewrites `e` at location(s) `l`, where `l` is either `*` or a
  list of hypotheses in the local context. In the latter case, a turnstile `⊢` or `|-`
  can also be used, to signify the target of the goal.

Using `rw (config := {occs := .pos L}) [e]`,
where `L : List Nat`, you can control which "occurrences" are rewritten.
(This option applies to each rule, so usually this will only be used with a single rule.)
Occurrences count from `1`.
At the first occurrence, whether allowed or not,
arguments of the rewrite rule `e` may be instantiated,
restricting which later rewrites can be found.
`{occs := .neg L}` allows skipping specified occurrences.
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants