Zulip Chat Archive

Stream: Is there code for X?

Topic: how to spell tsum in a set?


Alex Kontorovich (Mar 02 2024 at 00:36):

We have Finset.sum that allows one to write things like ∑ n in Finset.Icc 1 100, and we have tsum that sums over a whole type ∑' (n : ℕ). Do we have tsums on sets, like ∑' (n : ℕ) in Set.Ici 1...? Thanks!

Alex Kontorovich (Mar 02 2024 at 00:37):

(I realize this can be implemented by shifting the indices; just wondering if there's a more "natural-looking" way to do it?)

llllvvuu (Mar 02 2024 at 01:14):

Here and here it is spelled like ∑' (n : Set.Ici 1)

Yaël Dillies (Mar 02 2024 at 02:05):

This is not great though, because that means you can't always rewrite the set as it's also a type

Yaël Dillies (Mar 02 2024 at 02:06):

Shouldn't we change tsum to be about sums restricted on a set, then have the current tsum become the new tsum on univ?

Yaël Dillies (Mar 02 2024 at 02:07):

This is how docs#Finset.sum does it and it works great

Alex Kontorovich (Mar 02 2024 at 04:05):

Hmm yeah I’m already hitting coercion hell… I imagine that refactoring tsum will also not be pleasant..?

Yury G. Kudryashov (Mar 02 2024 at 05:35):

In the meantime, we have docs#tsum_congr_set_coe

Yaël Dillies (Mar 02 2024 at 10:21):

I'm not sure refactoring it is hard. But I have no time to try doing it.

Yury G. Kudryashov (Mar 02 2024 at 17:18):

Another approach is to do the same trick we did for finsum to allow ∑' k ∈ s, f k

Alex Kontorovich (Mar 02 2024 at 18:31):

I'd be happy with any such refactor. But, e.g., using something like this:

∑' (n : (Set.Ici N)), 1 / ↑↑n ^ s

gets very painful. Here n is of type ↑(Set.Ici N), which is the coercion from the set Set.Ici N to a Type. Then ↑n coerces n "back" to , and finally ↑↑n is the coercion from to . Yuck!...

Michael Stoll (Mar 02 2024 at 18:44):

You could try to use ∑' (n : ℕ), Set.indicator (Set.Ici N) (fun n ↦ 1 / (n : ℂ) ^ s) instead. My experience is that this is less painful.

Yaël Dillies (Mar 02 2024 at 19:03):

Yury G. Kudryashov said:

Another approach is to do the same trick we did for finsum to allow ∑' k ∈ s, f k

What's that trick?

Timo Carlin-Burns (Mar 02 2024 at 20:40):

If tsum could be generalized to support indexing types in Prop so that ∑' n : ℕ, ∑' (hn : n ∈ Set.Icc 1 100), 1 / (n : ℂ) ^ s typechecked, that would be another way of spelling this

Kevin Buzzard (Mar 02 2024 at 20:43):

I guess that's the trick Yury is talking about.

Eric Wieser (Mar 02 2024 at 20:43):

Presumably that solution is immune to the issues that appear with ⨆ on conditionally complete lattices? Here that would manifest as a sum that converges as a Subtype but not as nested sums

Yaël Dillies (Mar 02 2024 at 21:26):

Timo, this is not a good solution because we want Lean to know for sure that the body of the sum doesn't depend on hn (that's what's needed to get rw to play ball). Your solution doesn't ensure that (things can get weird after a few rewrites).

Eric Wieser (Mar 02 2024 at 21:37):

Being able to depend on hn sounds like a feature to me

Yaël Dillies (Mar 02 2024 at 22:23):

No, it's really not. This is how you get into rewriting hell. If you want to depend on hn you do the same thing as we do for docs#Finset.sum, namely you write ∑' i : s (or maybe ∑' i (hi : i ∈ s), I haven't yet convinced myself it's equivalent).

Anatole Dedecker (Mar 02 2024 at 23:15):

I was about to advocate for the trick (I’ve been using it with finsum and I like it a lot), but the problem is that doesn’t work great with docs#HasSum, which is generally more useful than tsum. So I’m not sure it’s a good idea to

Michael Stoll (Mar 03 2024 at 07:45):

The Set.indicator version works well with HasSum and Summable, I think.

Eric Wieser (Mar 03 2024 at 08:08):

Anatole Dedecker said:

I was about to advocate for the trick (I’ve been using it with finsum and I like it a lot), but the problem is that doesn’t work great with docs#HasSum, which is generally more useful than tsum. So I’m not sure it’s a good idea to

I guess you could have HasSum f n a where n is the number of arguments to sum over

Yury G. Kudryashov (Mar 04 2024 at 02:29):

Note that ∑' (h : p), f h, where p : Prop is equal to (more precisely, will be equal to if we define it) if h : p then f h else 0.

Antoine Chambert-Loir (Mar 04 2024 at 08:42):

Michael Stoll said:

The Set.indicator version works well with HasSum and Summable, I think.

Then, a mere notation for that version could probably be useful.

Anatole Dedecker (Mar 04 2024 at 10:05):

Yury G. Kudryashov said:

Note that ∑' (h : p), f h, where p : Prop is equal to (more precisely, will be equal to if we define it) if h : p then f h else 0.

Yes, but while this is quite natural in a tsum expression, it isn't in a HasSum statement.

Michael Stoll (Mar 07 2024 at 19:17):

See #11189 for an example using the Set.indicator s f spelling.


Last updated: May 02 2025 at 03:31 UTC