-
Notifications
You must be signed in to change notification settings - Fork 717
feat: allow numeric literals to be coerced to option type #10585
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
base: nightly-with-mathlib
Are you sure you want to change the base?
Conversation
|
Mathlib CI status (docs):
|
|
Reference manual CI status:
|
src/Init/Data/Option/Coe.lean
Outdated
| literals or floating point literals because of the automatic insertion of `OfNat` and `OfScientific` | ||
| coercions around these literals. | ||
| This instance provides the corresponding coercion for natural number literals. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wouldn't refer to this as a coercion - I think it's more accurate to say "this instance enables natural number literals for Option when the contained type has natural number literals."
The interactions between coercions, overloaded literals, and type-based disambiguation are a bit subtle, and we don't do users a favor by being imprecise in our use of language and terminology.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree with your call for precision of language here. Your suggestion works, but I feel like it's missing a meaningful verb. To put that a different way, there's a Mad Lib I would like to complete that looks like this:
This change allows natural number literals to [be?] [to/as?]
Option Atype wheneverAis an allowed(?) type for that natural number literal.
You're making a very reasonable object to "to be coerced to." What about "to be promoted to", or "to be cast to". Or given your comment about denotation, perhaps "interpret" is a good verb that is close to "denote"
This change allows natural number literals to be interpreted as values of type
Option Atype whenever that literal could also be interpreted as a value of typeA.
Would that make sense and capture the core meaning of your more accurate statement?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the key thing to have here is that natural number literals don't really start off as a Nat to be later transformed into some other type, but rather guide the initial elaboration of the value. Your second text captures that well.
I don't think the docs should say "this change", though - that's a good PR description, but the docs should describe the software as it is, not as it was or is becoming.
Also, is "interpret" the right verb here? It's not terrible, but it's also highly overloaded (we're not invoking the interpreter here). What about this:
This allows natural number literals to be used for
Optiontypes. In particular, if the literaln
can be used for typeα, then it can also be used forOption α, with the value wrapped insome.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"Used" seems more overloaded to me than "interpret," and with less helpfully-leading semantic content. "Treat?"
This allows Lean to treat a natural number literal as an expression that has an
Optiontype. In particular, if Lean knows how to treat the natural numbernas an expression that has typeα, then Lean can also treatnas a expression with typeOption αby adding an additionalOption.some.
…able instability in some tests
829699b to
204c655
Compare
|
Making this a real PR, though there's no urgency of merging it until @kim-em is back in town and caught up. |
This PR allows adds new instances for the
OfNatandOfScientifictypeclass that allow numeric literals to be treated as havingOptiontype.This change fixes an apparent asymmetry between numerals and other Lean expressions. Thanks to the optionCoe typeclass, an expression that synthesizes the type
αcan be coerced toOption α. However, a numeral like5or4.2e3does not synthesize the typeNatorFloatbecause it expands to e.g.OfNat.ofNat (α := _) (nat_lit 5), and typeclass inference will fail to find an instance that letsαbe anOptiontype.Comparing this with the behavior of String makes the apparent asymmetry particularly egregious, because string literals don't get a similar typeclass wrapper.
Despite these instances not being coercions, the effect is quite similar to the
optionCoetypeclass that defines the coercionα → Option α, so by putting them inInit.Data.Option.Corewe ensure that these instances are similarly banned insideInitandStd.https://live.lean-lang.org/#codez=CYUwZgBAzhBcEGUAuAnAlgOwOZwLwQCIAWAgKBAA8BDAWwAcAbEOCAeTqTQHsNFVMcsfFHLV6TFu048+6bHggAKAgGYCLZHKwBKUbUbN4U7r00CFqsqVCRe8AHJUkConvGG2HExEfOhEDDcDSS8ZXwVFFRZfXUp9CSNQ3nD/KIBaNIgAMSo0BhgAYwBXFBQQDCQGAE8AGmgigoKQEGAYIoxQFAgkAAs0GAAFACVSa3AIYBYshi4nFwA6IITPaV5p2b98YCWPYxl1uf9I+amZp1ixYMTV7LPNiBUTjOzc/Ihi0vLK2vrG5taIO1Ot0+oMhkA