Zulip Chat Archive

Stream: new members

Topic: Equiv.perm.sum_comp and Finsets


Nathan (Sep 29 2025 at 14:30):

How do I get this working?

example (n : ) :  i : Fin n, (n-i.val-1) = (n-1) * n / 2 :=
  let f :    := λ x => if x < n then n-1-x else x
  let σ : Equiv.Perm  := f, f, sorry, sorry
  have swap := Equiv.Perm.sum_comp σ (Fin n) f sorry
  sorry

I get this error
Screenshot 2025-09-29 at 10.29.05 AM.png
I can see that (Fin n) is not a Finset but can I use something like Finset.univ to convert it to a Finset? Or should I avoid Fin completely and just use a Finset instead?

Robin Arnez (Sep 29 2025 at 14:43):

You might want docs#Finset.range and docs#Finset.sum_range to transfer between the two

Nathan (Sep 29 2025 at 14:53):

ah, but I want to use i.prop inside the sum (the code i posted is a toy example) so i don't think this will work

Nathan (Sep 29 2025 at 14:56):

let me post more code...

Nathan (Sep 29 2025 at 15:13):

idk what code to post actually. but i feel like i might need a Finset of Fin

Nathan (Sep 29 2025 at 15:15):

that way, hopefully i can use prop inside the sum, and i can also use sum_comp

Nathan (Sep 29 2025 at 15:16):

how do i construct a Finset?

Nathan (Sep 29 2025 at 15:20):

someone said

If your subtype is finite, you can prove this and register it as an instance of the typeclass FinType.

Nathan (Sep 29 2025 at 16:08):

does lean have list comprehension?

Ruben Van de Velde (Sep 29 2025 at 16:09):

docs#Equiv.Perm.sum_comp

Nathan (Sep 29 2025 at 16:10):

yeah i need a finset to use that

Ruben Van de Velde (Sep 29 2025 at 16:18):

As far as I can tell, you should be able to make it work with Finset.range n if you replace i.val by i

Nathan (Sep 29 2025 at 16:18):

i won't be able to use i.prop in that case

Ruben Van de Velde (Sep 29 2025 at 16:19):

But you're not using that in your example

Nathan (Sep 29 2025 at 16:19):

i know, it's a toy example

Nathan (Sep 29 2025 at 16:28):

This is the real thing

def d (n : ) := n.divisors.sort (·  ·)
def S (n : ) :=  i : (Fin ((d n).length-1)), (d n)[i.val] * (d n)[i.val+1]

theorem t1 (n : ) : S n < n^2 :=
  let k := (d n).length - 1
  let f :    := λ x => if x < k then k-1-x else x
  let σ : Equiv.Perm  := f, f, sorry, sorry
  have swap := Equiv.Perm.sum_comp σ (Fin ((d n).length-1)) f sorry
  sorry

Nathan (Sep 29 2025 at 16:32):

it's not explicitly using i.prop but i think it is implicitly. if you change Fin to range in the definition of S, then you can't index into (d n).

Nathan (Sep 29 2025 at 16:41):

actually i guess it's more like this

def d (n : ) := n.divisors.sort (·  ·)
def S (n : ) :=  i : Fin ((d n).length-1), (d n)[i.val] * (d n)[i.val+1]

theorem t1 (n : ) : S n < n^2 :=
  let k := (d n).length - 1
  let f₁ :    := λ x => if x < k then k-1-x else x
  let σ : Equiv.Perm  := f₁, f₁, sorry, sorry
  let f₂ : (i : Fin k)   := λ i => (d n)[i.val] * (d n)[i.val+1]
  have swap := Equiv.Perm.sum_comp σ (Fin ((d n).length-1)) f₂ sorry
  sorry

Aaron Liu (Sep 29 2025 at 16:46):

Nathan said:

How do I get this working?

example (n : ) :  i : Fin n, (n-i.val-1) = (n-1) * n / 2 :=
  let f :    := λ x => if x < n then n-1-x else x
  let σ : Equiv.Perm  := f, f, sorry, sorry
  have swap := Equiv.Perm.sum_comp σ (Fin n) f sorry
  sorry

I get this error
Screenshot 2025-09-29 at 10.29.05 AM.png
I can see that (Fin n) is not a Finset but can I use something like Finset.univ to convert it to a Finset? Or should I avoid Fin completely and just use a Finset instead?

Yeah just put in Finset.univ should work

Nathan (Sep 29 2025 at 16:46):

I couldn't figure out how to use Finset.univ

Nathan (Sep 29 2025 at 16:47):

I can take another look at Finset.univ after lunch

Aaron Liu (Sep 29 2025 at 18:18):

I can show you how once I get home in an hour

Nathan (Sep 29 2025 at 18:25):

thanks

Ruben Van de Velde (Sep 29 2025 at 18:26):

I'm not sure how it helps with your goal, but maybe something like

import Mathlib

def d (n : ) := n.divisors.sort (·  ·)
def S (n : ) :=  i : Fin ((d n).length-1), (d n)[i.val] * (d n)[i.val+1]

theorem t1 (n : ) : S n < n^2 := by
  let k := (d n).length - 1
  let f₁ : Fin k  Fin k := λ x => -x
  let σ : Equiv.Perm (Fin k) := f₁, f₁, fun _ => by simp [f₁], fun _ => by simp [f₁]
  let f₂ : (i : Fin k)   := λ i => (d n)[i.val] * (d n)[i.val+1]
  have swap := Equiv.Perm.sum_comp σ Finset.univ f₂ sorry
  sorry

Nathan (Sep 29 2025 at 18:38):

i think you're right to change the type of f₁ to Fin k → Fin k

Nathan (Sep 29 2025 at 18:41):

oh wow Finset.univ goes there?

Nathan (Sep 29 2025 at 18:43):

wait this seems good. also, what's the behavior of λ x => -x?

Aaron Liu (Sep 29 2025 at 18:44):

@loogle Equiv, Fintype, Finset.sum

loogle (Sep 29 2025 at 18:44):

:search: Equiv.sum_comp, Fintype.sum_equiv, and 7 more

Aaron Liu (Sep 29 2025 at 18:46):

Nathan said:

wait this seems good. also, what's the behavior of λ x => -x?

takes input x and returns its negative (mod n) -x

Nathan (Sep 29 2025 at 18:46):

oh perfect

Aaron Liu (Sep 29 2025 at 19:01):

here we go

import Mathlib

def d (n : ) := n.divisors.sort (·  ·)
def S (n : ) :=  i : Fin ((d n).length-1), (d n)[i.val] * (d n)[i.val+1]

theorem t1 (n : ) : S n < n^2 :=
  let k := (d n).length - 1
  let σ : Equiv.Perm (Fin k) := Fin.revPerm
  let f₂ (i : Fin k) :  := (d n)[i.val] * (d n)[i.val+1]
  have swap :  i : Fin k, f₂ (σ i) =  i : Fin k, f₂ i := Equiv.sum_comp σ f₂
  sorry

Aaron Liu (Sep 29 2025 at 19:02):

wait no probably not Fin.revPerm

Aaron Liu (Sep 29 2025 at 19:02):

like this

import Mathlib

def d (n : ) := n.divisors.sort (·  ·)
def S (n : ) :=  i : Fin ((d n).length-1), (d n)[i.val] * (d n)[i.val+1]

theorem t1 (n : ) : S n < n^2 :=
  let k := (d n).length - 1
  let σ : Equiv.Perm (Fin k) := Equiv.neg (Fin k)
  let f₂ (i : Fin k) :  := (d n)[i.val] * (d n)[i.val+1]
  have swap :  i : Fin k, f₂ (σ i) =  i : Fin k, f₂ i := Equiv.sum_comp σ f₂
  sorry

Nathan (Sep 29 2025 at 19:08):

yeah i just realized it can be done with Equiv.sum_comp which takes fewer inputs

Nathan (Sep 29 2025 at 19:09):

Nice, thank you so much :pray:

Nathan (Sep 29 2025 at 19:15):

so clean :ok:


Last updated: Dec 20 2025 at 21:32 UTC