-
Notifications
You must be signed in to change notification settings - Fork 251
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
[Merged by Bors] - feat: ∑ x ∈ s, f x
to replace ∑ x in s, f x
in the future
#6795
Conversation
| `(finset% $t) => precheck t | ||
| _ => Elab.throwUnsupportedSyntax | ||
|
||
-- TODO: contribute this modification back to `extBinder` |
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.
The changes to the extBinder
syntax are in https://github.com/leanprover/std4/pull/234/files and in the meantime it's given as new syntax as bigOpBinder
here.
- `∑ x ∈ s, f x` is notation for `Finset.sum s f`. It is the sum of `f x`, | ||
where `x` ranges over the finite set `s` (either a `Finset` or a `Set` with a `Fintype` instance). | ||
- `∑ x ∈ s with p x, f x` is notation for `Finset.sum (Finset.filter p s) f`. | ||
- `∑ (x ∈ s) (y ∈ t), f x y` is notation for `Finset.sum (s ×ˢ t) (fun ⟨x, y⟩ ↦ f x y)`. |
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.
Is it a valid alternative to elaborate this into a nested sum? That would automatically handle the case of t
depending on x
, right?
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.
The thought was that if you wanted a nested sum then ∑ (x ∈ s), ∑ (y ∈ t), f x y
is available. Yaël and Bhavik were telling me about how this sort of behavior is helpful for related notations such as for expectation, so it might be worth being consistent here.
A downside though is that this PR is not supporting dependent products, i.e., you can't do ∑ (x ∈ s) (y ∈ t x), f x y
, and it's not very clear to me how to support them in a low-confusion way.
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 really want this notation to land. I am using it all over the place in LeanAPAP. The elaborator isn't perfect yet (no dependent products, unspecified interaction between nested and filtered products...) but it would already be a great step forward.
/-- `finset% t` elaborates `t` as a `Finset`. | ||
If `t` is a `Set`, then inserts `Set.toFinset`. | ||
Does not make use of the expected type; useful for big operators over finsets. | ||
``` | ||
#check finset% Finset.range 2 -- Finset Nat | ||
#check finset% (Set.univ : Set Bool) -- Finset Bool | ||
``` | ||
-/ | ||
elab (name := finsetStx) "finset% " t:term : term => do | ||
let u ← mkFreshLevelMVar | ||
let ty ← mkFreshExprMVar (mkSort (.succ u)) | ||
let x ← Elab.Term.elabTerm t (mkApp (.const ``Finset [u]) ty) | ||
let xty ← whnfR (← inferType x) | ||
if xty.isAppOfArity ``Set 1 then | ||
Elab.Term.elabAppArgs (.const ``Set.toFinset [u]) #[] #[.expr x] none false false | ||
else | ||
return x |
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.
Shouldn't this be moved to an earlier file?
else | ||
let ss ← withNaryArg 3 <| delab | ||
`(∏ $(.mk i):ident in $ss, $body) | ||
`(∏ $(.mk i):ident ∈ $ss, $body) | ||
|
||
/-- Delaborator for `Finset.prod`. The `pp.piBinderTypes` option controls whether |
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.
/-- Delaborator for `Finset.prod`. The `pp.piBinderTypes` option controls whether | |
/-- Delaborator for `Finset.sum`. The `pp.piBinderTypes` option controls whether |
@YaelDillies I'm not so sure about this being the sum/prod notation, but I do support having some variant notation that constructs these (dependent) products for the domain. In #7227 I've been working on a more flexible binder notation to upgrade all Potentially the variant notation could be a new binder type. Just making one up quick for sake of illustration, |
What I really care about is the |
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 this is great overall and deserves to be merged even if a few details still need ironing out. We need to try out the new syntaxes to eg decide what the correct behavior for nested sums is (I think the one in this PR is already correct, but who knows).
maintainer merge
🚀 Pull request has been placed on the maintainer queue by YaelDillies. |
∑ x ∈ s, f x
instead of ∑ x in s, f x
∑ x ∈ s, f x
to replace ∑ x in s, f x
in the future
This looks very nice from a readability standpoint. Also, the reactions on Zulip are overwhelmingly positive. @eric-wieser still had some reservations that he was planning to share (and I do worry a bit about the "can I rewrite the |
Yes, we are still the old elaborators here: https://github.com/leanprover-community/mathlib4/pull/6795/files#diff-daef0dcb628557383518bbc8aaabe4b289271a26e17ffd78ab3c1aae7ba1d3b2R201-R215 |
I've shared these in the original zulip thread. |
Ah, thanks for the link. bors d+ |
✌️ kmill can now approve this pull request. To approve and merge a pull request, simply reply with |
✌️ YaelDillies can now approve this pull request. To approve and merge a pull request, simply reply with |
bors merge |
👎 Rejected by label |
bors merge |
Adds new syntax for sum/product big operators for `∑ x ∈ s, f x`. The set `s` can either be a `Finset` or a `Set` with a `Fintype` instance, in which case it is equivalent to `∑ x ∈ s.toFinset, f x`. Also supports `∑ (x ∈ s) (y ∈ t), f x y` for `Finset.sum (s ×ˢ t) (fun ⟨x, y⟩ ↦ f x y)`. Note that these are not dependent products at the moment. Adds `with` clauses, so for example `∑ (x ∈ Finset.range 5) (y ∈ Finset.range 5) with x < y, x * y`, which inserts a `Finset.filter` over the domain set. Supports pattern matching in the variable position. This is by creating an experimental version of `extBinder` that uses `term:max` instead of `binderIdent`. The new `∑ x ∈ s, f x` notation is used in `Algebra.BigOperators.Basic` for illustration, but the old `∑ x in s, f x` still works for backwards compatibility. Zulip threads [here](https://leanprover.zulipchat.com/#narrow/stream/287929-mathlib4/topic/.E2.88.91.20x.20.E2.88.88.20s.2C.20f.20x.20instead.20of.20.E2.88.91.20x.20in.20s.2C.20f.20x/near/387352818) and [here](https://leanprover.zulipchat.com/#narrow/stream/287929-mathlib4/topic/Destruction.20and.20big.20operators/near/387366894) Co-authored-by: Yaël Dillies <yael.dillies@gmail.com>
Pull request successfully merged into master. Build succeeded: |
∑ x ∈ s, f x
to replace ∑ x in s, f x
in the future∑ x ∈ s, f x
to replace ∑ x in s, f x
in the future
Adds new syntax for sum/product big operators for `∑ x ∈ s, f x`. The set `s` can either be a `Finset` or a `Set` with a `Fintype` instance, in which case it is equivalent to `∑ x ∈ s.toFinset, f x`. Also supports `∑ (x ∈ s) (y ∈ t), f x y` for `Finset.sum (s ×ˢ t) (fun ⟨x, y⟩ ↦ f x y)`. Note that these are not dependent products at the moment. Adds `with` clauses, so for example `∑ (x ∈ Finset.range 5) (y ∈ Finset.range 5) with x < y, x * y`, which inserts a `Finset.filter` over the domain set. Supports pattern matching in the variable position. This is by creating an experimental version of `extBinder` that uses `term:max` instead of `binderIdent`. The new `∑ x ∈ s, f x` notation is used in `Algebra.BigOperators.Basic` for illustration, but the old `∑ x in s, f x` still works for backwards compatibility. Zulip threads [here](https://leanprover.zulipchat.com/#narrow/stream/287929-mathlib4/topic/.E2.88.91.20x.20.E2.88.88.20s.2C.20f.20x.20instead.20of.20.E2.88.91.20x.20in.20s.2C.20f.20x/near/387352818) and [here](https://leanprover.zulipchat.com/#narrow/stream/287929-mathlib4/topic/Destruction.20and.20big.20operators/near/387366894) Co-authored-by: Yaël Dillies <yael.dillies@gmail.com>
Adds new syntax for sum/product big operators for `∑ x ∈ s, f x`. The set `s` can either be a `Finset` or a `Set` with a `Fintype` instance, in which case it is equivalent to `∑ x ∈ s.toFinset, f x`. Also supports `∑ (x ∈ s) (y ∈ t), f x y` for `Finset.sum (s ×ˢ t) (fun ⟨x, y⟩ ↦ f x y)`. Note that these are not dependent products at the moment. Adds `with` clauses, so for example `∑ (x ∈ Finset.range 5) (y ∈ Finset.range 5) with x < y, x * y`, which inserts a `Finset.filter` over the domain set. Supports pattern matching in the variable position. This is by creating an experimental version of `extBinder` that uses `term:max` instead of `binderIdent`. The new `∑ x ∈ s, f x` notation is used in `Algebra.BigOperators.Basic` for illustration, but the old `∑ x in s, f x` still works for backwards compatibility. Zulip threads [here](https://leanprover.zulipchat.com/#narrow/stream/287929-mathlib4/topic/.E2.88.91.20x.20.E2.88.88.20s.2C.20f.20x.20instead.20of.20.E2.88.91.20x.20in.20s.2C.20f.20x/near/387352818) and [here](https://leanprover.zulipchat.com/#narrow/stream/287929-mathlib4/topic/Destruction.20and.20big.20operators/near/387366894) Co-authored-by: Yaël Dillies <yael.dillies@gmail.com>
Adds new syntax for sum/product big operators for
∑ x ∈ s, f x
. The sets
can either be aFinset
or aSet
with aFintype
instance, in which case it is equivalent to∑ x ∈ s.toFinset, f x
.Also supports
∑ (x ∈ s) (y ∈ t), f x y
forFinset.sum (s ×ˢ t) (fun ⟨x, y⟩ ↦ f x y)
. Note that these are not dependent products at the moment.Adds
with
clauses, so for example∑ (x ∈ Finset.range 5) (y ∈ Finset.range 5) with x < y, x * y
, which inserts aFinset.filter
over the domain set.Supports pattern matching in the variable position. This is by creating an experimental version of
extBinder
that usesterm:max
instead ofbinderIdent
.The new
∑ x ∈ s, f x
notation is used inAlgebra.BigOperators.Basic
for illustration, but the old∑ x in s, f x
still works for backwards compatibility.Zulip threads here and here