Zulip Chat Archive

Stream: Is there code for X?

Topic: nat.antidiagonal for pi types


Eric Wieser (Mar 29 2022 at 08:16):

Do we have a variant of docs#finset.nat.antidiagonal that produces a k-way split?

/-- When k=2 this is the "same" as finset.nat.antidiagonal -/
def finset.nat.antidiagonal' (n : ) (k : ) :
  finset (fin k  ) := sorry

def finset.nat.mem_antidiagonal' {n : } {k : } (x : fin k  ) :
  x  finset.nat.antidiagonal' n k   i : fin k, x i = n := sorry

Yaël Dillies (Mar 29 2022 at 08:19):

We have docs#std_simplex, which is pretty close.

Eric Wieser (Mar 29 2022 at 08:25):

I was thinking something like

import data.finset
import data.fin.tuple
import data.fin.vec_notation

open_locale big_operators

def list.nat.antidiagonal' :   Π k, list (fin k  )
| n 1 := [![n]]
| n (k + 1) := (list.range n.succ).reverse.bind $ λ ni,
  (list.nat.antidiagonal' ni k).map $ λ x, fin.cons (n - ni) x

#eval list.nat.antidiagonal' 2 3
-- [![0, 0, 2], ![0, 1, 1], ![0, 2, 0], ![1, 0, 1], ![1, 1, 0], ![2, 0, 0]]

Yaël Dillies (Mar 29 2022 at 08:28):

Why not simply filtering out fintype.pi_finset (λ _, range n)?

Eric Wieser (Mar 29 2022 at 08:31):

Can you make a #mwe?

Eric Wieser (Mar 29 2022 at 08:32):

But I think one answer is "that's way slower to compute"

Yaël Dillies (Mar 29 2022 at 08:40):

import algebra.big_operators.order

open finset fintype
open_locale big_operators

variables (ι : Type*) {α : Type*} [fintype ι] [decidable_eq ι] (n : )

def finset.nat.antidiagonal' (n : ) : finset (ι  ) :=
(pi_finset $ λ _, range $ n + 1).filter $ λ f,  i, f i = n

variables {ι n} {f : ι  }

lemma finset.nat.mem_antidiagonal'_iff : f  finset.nat.antidiagonal' ι n   i, f i = n :=
begin
  refine mem_filter.trans (and_iff_right_of_imp $ λ h, _),
  simp_rw [mem_pi_finset, mem_range, nat.lt_succ_iff],
  exact λ i, (single_le_sum (λ j _, nat.zero_le _) (mem_univ _)).trans h.le,
end

Eric Wieser (Mar 29 2022 at 08:40):

Admittedly the proof is easier there

Yaël Dillies (Mar 29 2022 at 08:40):

Why do you care about computation?

Eric Wieser (Mar 29 2022 at 08:41):

We cared about it for finset.nat.antidiagonal

Eric Wieser (Mar 29 2022 at 08:41):

it seems sensible to be consistent

Yaël Dillies (Mar 29 2022 at 08:41):

Why did we care back then?

Eric Wieser (Mar 29 2022 at 08:41):

It's entirely conceivable that norm_num would want to actually iterate over an antidiagonal?

Yaël Dillies (Mar 29 2022 at 08:42):

Note that @Alex J. Best recently made stuff less computable in this area. #10891 and #10930

Alex J. Best (Mar 29 2022 at 10:08):

Well I only changed the definition of trunc? So I don't think I changed too much. Certainly it could be that the new definition is less computable in some sense? But the target of trunc is highly noncomputable anyway so I'm not sure I made any difference at all

Eric Wieser (Mar 29 2022 at 12:35):

PRd as #13031


Last updated: Dec 20 2023 at 11:08 UTC