Zulip Chat Archive

Stream: Is there code for X?

Topic: All lists with a maximum length and elements of a finset


Johannes Tantow (May 19 2025 at 18:44):

Hi,
I am looking for a function of the form def generateAll (X : Finset A) (n : ℕ) : Finset (List A) that fulfills the following property

theorem generateAll_def (X: Finset A) (l': List A) (n : ):
    l'  generateAllX n  l'.length  n   x  l', x  X :=  sorry

It would also be fine if X is a list instead of a finite set. Do we have anything like that?

Eric Wieser (May 19 2025 at 19:16):

is this almost docs#Finset.powersetCard ?

Johannes Tantow (May 19 2025 at 19:18):

Kind of. The powerset can be larger, which I can filter out,but the lists are allowed to contain duplicates in my case.

Eric Wieser (May 19 2025 at 19:22):

How's this, via docs#Vector.fintype ?

import Mathlib

def generateAll (X : Finset A) (n : ) : Finset (List A) :=
  (Finset.univ : Finset (List.Vector X n)).map <|
    .trans (.subtype _) ⟨_, List.map_injective_iff.2 Subtype.val_injective

Eric Wieser (May 19 2025 at 19:22):

Ah, I guess that's only the ones of fixed length

Eric Wieser (May 19 2025 at 19:23):

But you could take the disjiUnion over all i <= n

Johannes Tantow (May 19 2025 at 19:29):

The normal union should suffice as well for this as A is decidable. I'll build Mathlib and test that. Thanks

Eric Wieser (May 19 2025 at 19:41):

disjiUnion will be much faster to execute and kernel reduce

Eric Wieser (May 19 2025 at 19:42):

But also it might be easier to implement this from scratch rather than via my suggestion above!

Johannes Tantow (May 19 2025 at 19:42):

I don't really want to execute this and only need this to show decidability. disjUnion wants me to prove disjointness

Johannes Tantow (May 19 2025 at 19:43):

I tried this before from scratch by recursion, but didn't find a good way to combine the elements of the lists with the finset of lists with length le m-1.

Eric Wieser (May 19 2025 at 19:43):

Either you prove the disjointness now, or you'll find you probably need it later

Eric Wieser (May 19 2025 at 19:48):

Johannes Tantow said:

I tried this before from scratch by recursion, but didn't find a good way to combine the elements of the lists with the finset of lists with length le m-1.

Probably you should start with lists:

import Mathlib

def generateAll (l : List A) (n : ) : List (List A) :=
  match n with
  | 0 => [[]]
  | n + 1 =>
    let last := generateAll l n
    [] :: l.flatMap fun x => last.map fun ls => x :: ls

#eval generateAll [1, 2, 3] 2```
and then show that:
* permuting the input permutes the output (to get to multisets)
* if the input has no duplicate, then nor does the output (to get to finsets)

Johannes Tantow (May 19 2025 at 19:49):

I can also work with lists directly, but I thought the other form might be more general

Johannes Tantow (May 19 2025 at 19:50):

so that it would exist already somewhere

Eric Wieser (May 19 2025 at 19:50):

Unfortunately my code above doesn't actually work, it creates duplicates! fixed

Eric Wieser (May 19 2025 at 19:50):

But usually the pattern with these combinatorial constructions is to build them for Lists, then Multiset, then Finset

Eric Wieser (May 19 2025 at 19:50):

Since often it actually is useful to have the "less general" version

Eric Wieser (May 19 2025 at 19:51):

eg docs#List.sublists, docs#Multiset.powerset, docs#Finset.powerset

Eric Wieser (May 19 2025 at 19:53):

For instance, with my definition above, you could prove that if l is sorted then generateAll l 2 is sorted lexicographically

Johannes Tantow (May 19 2025 at 20:08):

Thanks, I managed to prove my result now.

Kevin Buzzard (May 20 2025 at 04:54):

Johannes Tantow said:

The normal union should suffice as well for this as A is decidable. I'll build Mathlib and test that. Thanks

Just to check -- you didn't mean "I'll spend at least an hour building mathlib on my computer because I don't know about lake exe cache get which downloads compiled binaries" do you?

Johannes Tantow (May 20 2025 at 05:00):

I theoretically know that, but didn't think of that in the moment and just pressed reload files and started the process


Last updated: Dec 20 2025 at 21:32 UTC