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

Elaboration of optional parameters does not take into account expected type #4823

Closed
kmill opened this issue Jul 24, 2024 · 2 comments
Closed
Labels
bug Something isn't working

Comments

@kmill
Copy link
Collaborator

kmill commented Jul 24, 2024

Description

When using a function that has optional parameters, it can sometimes be surprising how eager optional parameters are filled in when arguments could come from the expected type.

Steps to Reproduce

In the following, one might expect the first example to work. Instead, one needs to write fun n => f n or (f ·) to inhibit filling in the optional parameter.

def f (n : Nat := 0) : Nat := n

example : Nat → Nat := f
/-
type mismatch
  f
has type
  Nat : Type
but is expected to have type
  Nat → Nat : Type
-/

example : Nat → Nat := (f ·) -- ok

Versions

Lean 4.11.0 nightly from 7/22/2024

Additional Information

It is unclear whether this first example should work, or whether expected-type-directed optional parameters would lead to worse confusion, but I wanted to file an issue to record a momentary surprise I had recently.

Impact

Add 👍 to issues you consider important. If others are impacted by this issue, please ask them to add 👍 to it.

@Kha
Copy link
Member

Kha commented Jul 26, 2024

Thanks for logging this issue. In the current system, this should be considered by design. If we see evidence for such a change that outweighs that against, an RFC would be the next step.

@Kha Kha closed this as not planned Won't fix, can't repro, duplicate, stale Jul 26, 2024
@kmill
Copy link
Collaborator Author

kmill commented Jul 26, 2024

I'll leave just a couple more thoughts here:

In the current system, there is a single class of optional parameter, positional parameters with default values. I see there being two different use cases for optional parameters: (1) filling in positional parameters with contextually relevant default values (like for example optional parameters to constructors) and (2) configuration (like for example boolean flags to monadic functions). If there were a distinction between these two types, then it would make sense for type-1 parameters to take the expected type into account, since they would be more like implicit parameters that can optionally be supplied positionally, and arguably if they're not present they could be taken from the expected type. Type-2 parameters though should not be, and I believe it would be very confusing if they were.

So, an RFC would also have to take into account separating these types of optional parameters.

Incidentally, if there were also non-positional optional parameters like in Python (ones that must be set using named arguments), then that would naturally distinguish these two use cases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants