Zulip Chat Archive

Stream: lean4

Topic: Go to definition for overloaded functions


Tomas Skrivan (Dec 08 2025 at 08:42):

A common pattern for overloading functions is to have one field class like this

class Foo (α : Type) where
  foo : α  α

instance instFooNat : Foo Nat where
  foo x := 10 * x

instance instFooFloat : Foo Float where
  foo x := 10 * x

Then using LSP function go to definition on Foo.foo, it takes me to the class definition but that is rarely where I want to go. Ideally I would like to go to the particular instance which has been elaborated at a particular point or at least I would like to get a list of all the instances and I pick the right one.

I have a custom syntax to define overloaded functions in a bit less verbose way

declfun foo {α} (x : α) : α

defun foo (x : Float) := x.sin
defun foo (x : Float32) := x.cos

and I have tried for each defun to call addDeclarationRangesFromSyntax ``Foo.foo stx. However it does not work as I would like and no wonder as declRangeExt stores only one declaration range per declaration name.

Is there a way to deal with this problem?

Tomas Skrivan (Dec 08 2025 at 08:46):

mwe

code

Tomas Skrivan (Dec 08 2025 at 10:25):

Ohh nevermind, the whole question is completely moot.

#check foo (42 : Nat)
#check foo (42 : Float)

using go-to-definition does take me to the right spot. There is some dark magic at play already! it seems like that it gives me an option to go to any of the instances that are applied to the current constant.

Tomas Skrivan (Dec 08 2025 at 10:28):

Adding

instance [Add α] [Mul α] : Foo α where
  foo x := x * x + x

#check foo (42 : Int)

and go-to-definition on foo (42 : Int) offers me to see the Add Int, Mul Int and Add α → Mul α → Foo α instances

Tomas Skrivan (Dec 08 2025 at 10:29):

This is really nice! Now recent is this? I havn't noticed this behavior before.

Henrik Böving (Dec 08 2025 at 10:30):

A few months old at best, but yes quite recent.

Marc Huisinga (Dec 08 2025 at 10:56):

lean4#9040

Floris van Doorn (Dec 08 2025 at 15:05):

I will echo my support for this great feature!
Another useful feature is that Right click > Go to Declaration on notation will give you to the line where the notation was declared as an option to jump to.


Last updated: Dec 20 2025 at 21:32 UTC