Zulip Chat Archive
Stream: lean4
Topic: rfc: show arity of current function
Kenny Lau (Oct 08 2025 at 07:33):
One thing that could make code-writing more convenient would be a way to show the arity of the current function, because functions in Mathlib often has 10+ typeclasses and 3+ implicit arguments, which makes it hard to count how many underscores I need to apply.
Consider the following mini-example:
theorem foo {α β γ : Type}
[Add α] [Mul α] [Add β] [Mul β] [Add γ] [Mul γ] (h₁ : α = β) (h₂ : β = γ) : α = γ :=
h₁.trans h₂
example : Nat = Nat :=
foo
Right now, Lean already has a very helpful feature, where if you put the cursor at the end of foo in Line 6, wait a while, and then press space, then it shows you all the (remaining) arguments of the current function:
so maybe a way to implement my suggestion would be just to use that existing window, but add a newline at the bottom with the text "2 explicit arguments remaining".
Eric Wieser (Oct 08 2025 at 09:32):
I think graying out the Implicit arguments would go a long way here
Marc Huisinga (Oct 08 2025 at 12:00):
Eric Wieser said:
I think graying out the Implicit arguments would go a long way here
This is not supported by either the signature help request in LSP or the VS Code API. I'd also prefer not to make this popup any larger than it needs to be by adding another line to it since it will then also cover more code.
I think a decent improvement would be to highlight the first explicit argument instead, which is supported by the protocol. Since the delaborator and the pretty-printer currently don't retain any of this position information, we'd have to delaborate the implicit prefix, the first explicit argument and everything after that separately and then join the outputs.
Eric Wieser (Oct 08 2025 at 12:35):
What's the mechanism to do the highlighting?
Marc Huisinga (Oct 08 2025 at 12:37):
SignatureInformation.activeParameter
Michael Rothgang (Oct 08 2025 at 14:32):
In #26221, Patrick and I got Kyle Miller to write a version of '#check` that only shows explicit arguments. Would this solve your need?
Michael Rothgang (Oct 08 2025 at 14:33):
(I share your pain of counting the remaining arguments, particularly in differential geometry, where a declaration easily has 10 arguments, but at most 2 or 3 explicit ones.)
Kenny Lau (Oct 08 2025 at 14:34):
well i would still have to copy it and then #check it outside as i can't #check in the middle of a sentence
Marc Huisinga (Oct 08 2025 at 14:36):
(deleted)
Michael Rothgang (Oct 08 2025 at 14:42):
Given there's a #check tactic (i.e., works in the middle of a proof), #check' could also become one.
Kenny Lau (Oct 08 2025 at 14:42):
maybe we'll need a check% in the middle of a term
Kenny Lau (Oct 08 2025 at 14:43):
like unironically
Julian Berman (Oct 08 2025 at 14:47):
Is using Code Lens a possibility here to consider? Maybe having an optional separate code lens which says the arity?
Kenny Lau (Oct 08 2025 at 14:47):
what's a code lens?
Julian Berman (Oct 08 2025 at 14:49):
I think the ones you see in VSCode today are related to git information, but here's a googled old list of few examples (from other languages clearly) https://code.visualstudio.com/blogs/2017/02/12/code-lens-roundup
Kenny Lau (Oct 08 2025 at 14:50):
what if when we right click on an identifier of a decl it just shows "function arity: 4"?
Kenny Lau (Oct 08 2025 at 14:51):
as in, above the whole menu
Kenny Lau (Oct 08 2025 at 14:51):
Kenny Lau (Oct 08 2025 at 14:51):
above the "Go to Definition"
Kenny Lau (Oct 08 2025 at 14:52):
also, maybe formatting is not supported in the "space info", but it's supported in the hover info right
Kenny Lau (Oct 08 2025 at 14:52):
Kenny Lau (Oct 08 2025 at 14:52):
since I see blue colour here
Kenny Lau (Oct 08 2025 at 14:52):
can we make the implicit arguments gray here?
Kenny Lau (Oct 08 2025 at 14:55):
related: #lean4 > rfc: on hover, show the type before the error
Marc Huisinga (Oct 08 2025 at 15:32):
Kenny Lau said:
can we make the implicit arguments gray here?
We serve the types in hovers as a markdown code block with language ID lean, the highlighting for which is provided by the Lean 4 VS Code extension TextMate grammar.
If you want implicit parameters to be highlighted differently in hovers, you have to define a custom TextMate grammar that parses arbitrary Lean pretty-printer output, identifies implicit arguments in a way that doesn't lead to false-positives with other notation using {} or [], contribute that to the VS Code extension with a custom language ID and then change the hovers provided by the language server to produce markdown code blocks with the new language ID. When the pretty-printer changes in a new Lean version, you will then also likely have to update the client-side TextMate grammar.
TextMate grammars are absolutely horrible to maintain as they are just bundles of unreadable regexes, so I don't think this is really worth the initial effort or the maintenance burden.
Kenny Lau (Oct 08 2025 at 15:37):
I see, that's another unfortunate situation.
Eric Wieser (Oct 08 2025 at 16:51):
Marc Huisinga said:
identifies implicit arguments in a way that doesn't lead to false-positives with other notation using
{}or[]
We essentially had this in Lean 3, but it was reverted, I think due to false positives for things like
(x : Fin "oops)".length)
Eric Wieser (Oct 08 2025 at 16:53):
We'd also need a textmate scope to attach to the implicit arguments, and one doesn't immediately spring to mind
Kenny Lau (Oct 08 2025 at 16:55):
Marc Huisinga said:
We serve the types in hovers as a markdown code block with language ID
lean, the highlighting for which is provided by the Lean 4 VS Code extension TextMate grammar.
can this be changed?
Kenny Lau (Oct 08 2025 at 16:55):
for example, can we have two code blocks?
Eric Wieser (Oct 08 2025 at 16:56):
What would the two blocks show?
Kenny Lau (Oct 08 2025 at 16:57):
it was a hypothetical question, I wanted to know what can be customised, because that response made it sound like it was not tweakable at all
Marc Huisinga (Oct 08 2025 at 18:59):
Eric Wieser said:
Marc Huisinga said:
identifies implicit arguments in a way that doesn't lead to false-positives with other notation using
{}or[]We essentially had this in Lean 3, but it was reverted, I think due to false positives for things like
(x : Fin "oops)".length)
There's another trick that I know of that could be used to get a more maintainable solution to at least part of this problem: while LSP itself only supports Markdown hovers, VS Code also allows rendering a safe subset of HTML tags in the Markdown hover you provide if you set a specific flag (e.g. as in this experiment).
The language server could then define a generic and simple non-HTML format for tagging implicit parameters in the Markdown it produces, which we'd then parse in the language client middleware and convert to HTML that styles implicit parameters accordingly. AFAICT, these HTML tags also have access to VS Code's theme CSS variables, so the styling could be consistent with the current theme.
All of this would have to be locked behind a client capability so that clients that don't support parsing this extended Markdown syntax still get the regular Markdown hovers.
The downside of this is that VS Code obviously won't render the HTML in Markdown code blocks, so while you gain the implicit parameter highlighting, you lose all highlighting that VS Code provides via our TextMate grammar for Lean. We'd have to re-implement much of the highlighting from this grammar in a custom semantic highlighting language for hovers to adequately replace the current implementation. Perhaps this will be the way to go at some point, but I'd need a few more good reasons than just implicit parameter highlighting for it to be worth it :-)
Kenny Lau (Oct 08 2025 at 19:02):
Marc Huisinga said:
I'd need a few more good reasons
May I present to you these reasons:
Eric Wieser (Oct 08 2025 at 19:19):
I don't understand the relevence of that screenshot; did you mean to capture the hover?
Kenny Lau (Oct 08 2025 at 19:25):
I meant, look at all these wonderful colours we can have
Aaron Liu (Oct 08 2025 at 19:26):
so colourful
Aaron Liu (Oct 08 2025 at 19:27):
but is all this really gonna show up in hover info
Kenny Lau (Oct 08 2025 at 19:28):
well if it shows up here then i take it to mean that the infrastructure already exists? i also kinda know (or at least i think) that these colours are in part server-generated and not based on grammar
Eric Wieser (Oct 08 2025 at 21:25):
Unfortunately the highlighting is a hybrid of those two systems, and vscode provides no API to execute the textmate system.
Kenny Lau (Oct 08 2025 at 21:29):
if it looks like monospace font and has spaces in the right places then it is a highlighted code(?)
Last updated: Dec 20 2025 at 21:32 UTC