-
Notifications
You must be signed in to change notification settings - Fork 850
feat: make Function generalized field notation more usable #13244
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
Open
kmill
wants to merge
1
commit into
master
Choose a base branch
from
kmill_1629
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| /-! | ||
| # Generalized field notation for functions | ||
|
|
||
| Functions use the `Function` namespace, and they always take the first explicit argument that's a function. | ||
| -/ | ||
|
|
||
| /-! | ||
| Motivating example for why it should only match explicit arguments. This `swap` function | ||
| is unusable using field notation in the intended way if it matched implicit arguments too. | ||
| https://github.com/leanprover/lean4/issues/1629 | ||
| -/ | ||
| def Function.swap {α β} {γ : α → β → Sort _} (f : (a : α) → (b : β) → γ a b) | ||
| (b : β) (a : α) : γ a b := f a b | ||
|
|
||
| def mul : Nat → Nat → Nat := (· * ·) | ||
| /-- info: Function.swap mul : Nat → Nat → Nat -/ | ||
| #guard_msgs in #check Function.swap mul -- works | ||
| /-- info: Function.swap mul : Nat → Nat → Nat -/ | ||
| #guard_msgs in #check mul.swap | ||
|
|
||
| example : mul.swap = Function.swap mul := rfl | ||
|
|
||
| /-! | ||
| Function field notation can be `open`ed into other namespaces. | ||
| https://leanprover.zulipchat.com/#narrow/channel/113489-new-members/topic/generalized.20field.20notation.20vs.20namespaces/near/582689850 | ||
| -/ | ||
| def MyNS.Function.apply {α} (a : α) {β : α → Sort _} (f : (x : α) → β x) : β a := f a | ||
|
|
||
| /-- error: Unknown constant `mul.apply` -/ | ||
| #guard_msgs in #check mul.apply 2 3 | ||
|
|
||
| /-- info: Function.apply 2 mul 3 : Nat -/ | ||
| #guard_msgs in open MyNS in #check mul.apply 2 3 | ||
|
|
||
| /-! | ||
| Function field notation can be used in recursive definitions. | ||
| -/ | ||
| def Function.iterate {α} (f : α → α) (n : Nat) (x : α) : α := | ||
| match n with | ||
| | 0 => x | ||
| | n+1 => f.iterate n (f x) | ||
|
|
||
| /-- info: 1024 -/ | ||
| #guard_msgs in #eval (·*2).iterate 10 1 | ||
|
|
||
| /-! | ||
| Another example of a definition that is reasonable to use with field notation. This is from Mathlib. | ||
| -/ | ||
| def Function.update {α : Sort u} {β : α → Sort v} [DecidableEq α] | ||
| (f : ∀ a, β a) (a' : α) (v : β a') (a : α) : β a := | ||
| if h : a = a' then Eq.ndrec v h.symm else f a | ||
|
|
||
| /-- info: 108 -/ | ||
| #guard_msgs in #eval (mul.update 2 (· + 100)) 2 8 | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
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.
Mathlib also has the
Pinamespace; would you advocate that it should move things into theFunctionnamespace, or that dot notation should tryPiafterFunction?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.
Great question Eric - I note that Core also uses
Piin a few places (e.g. Pi.instSubsingleton, Pi.instNonempty, Pi.instIhabited) - not sure if these should also beFunction.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.
(Fundamentally I don't understand the rationale for when one is used and not the other: e.g.
Function.dcompin Mathlib could bePi.compquite reasonably.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.
@eric-wieser I'm not sure — I think this PR needs some community discussion.
I remember in Lean 3 I had an experimental PR where it would resolve names in the
implies,function,forall, andpinamespaces depending on whether it was dependent and whether its type wasProp(e.g. an implication would try all four, a nondependent function would tryfunctionandpi, a dependentPropwould tryforallandpi, etc.). It's too much complexity though, and it won't work well for terms that are partially elaborated, where you don't know yet if it's dependent or a Prop.