Zulip Chat Archive

Stream: Is there code for X?

Topic: list.join.take


Martin Dvořák (Oct 22 2022 at 13:51):

Is there a lemma for the following example?

example {α : Type} (l : list (list α)) (n : ) :
   k q : ,  k_lt : k < l.length,
    l.join.take n = (l.take k).join ++ (l.nth_le k k_lt).take q :=
begin
  sorry,
end

I know about list.take_sum_join but I hope for something closer to my goal.

Eric Wieser (Oct 22 2022 at 14:28):

(docs#list.take_sum_join for reference)

Eric Rodriguez (Oct 22 2022 at 14:29):

(the list has to be nontrivial)

Eric Wieser (Oct 22 2022 at 14:46):

I think it would be better to state it without the existential

Martin Dvořák (Oct 22 2022 at 14:47):

On the outside, I don't care how k and q relate to length of respective parts. How would you state my lemma instead?

Eric Wieser (Oct 22 2022 at 14:48):

By directly computing the k you need

Eric Wieser (Oct 22 2022 at 14:48):

And putting that in the lemma statement

Martin Dvořák (Oct 22 2022 at 14:51):

At this point, I should probably prove my lemma and see whether I can easily extract a formula for k from it.

Eric Wieser (Oct 22 2022 at 15:33):

Something like

/-- Produces something akin to `(quotient, remainder)`, but unlike division the entry being subtracted is not the same each time -/
def nat.sub_list :   list    × 
| n [] := (0, n)
| n (x :: xs) := if n < x then (0, n) else
  let r := nat.sub_list (n - x) xs in (r.1 + 1, r.2)

Eric Wieser (Oct 22 2022 at 15:42):

I guess this generalizes to tsub

Patrick Johnson (Oct 23 2022 at 08:16):

Proof

Martin Dvořák (Oct 24 2022 at 06:47):

Wow! Thanks a lot!

Martin Dvořák (Oct 24 2022 at 06:48):

Would you like to add it to mathlib? I can PR it if you want.

Martin Dvořák (Oct 24 2022 at 06:51):

I would also add something similar for list.drop.

Patrick Johnson (Oct 24 2022 at 07:13):

Feel free to PR it.

Martin Dvořák (Oct 24 2022 at 07:14):

OK, I will. Thanks a lot for your big help!!

Martin Dvořák (Oct 24 2022 at 08:17):

I would like to make it a tiny bit stronger by adding k < (l.nth_le m hh).length to:

lemma list.take_join {l : list (list α)} {n : } (h : l  []) :
   (m k : ) hh, l.join.take n = (l.take m).join ++ (l.nth_le m hh).take k

Martin Dvořák (Oct 24 2022 at 08:19):

(if we were to take all of the mth part, we would instead increment m)

Martin Dvořák (Oct 24 2022 at 08:22):

(I don't have a use for k < (l.nth_le m hh).length here, but I want to add this property to the list.drop version)

Martin Dvořák (Oct 24 2022 at 09:45):

Patrick Johnson said:

Proof

Is import tactic.induction redundant?

Patrick Johnson (Oct 24 2022 at 09:59):

Is import tactic.induction redundant?

Seems so.

I would like to make it a tiny bit stronger by adding k < (l.nth_le m hh).length

In that case you would also need n < l.join.length. Besides that, keep in mind that l may also contain empty lists.

Martin Dvořák (Oct 24 2022 at 10:06):

Oh! I overlooked that I would then have to take 0 elements from the non-existent l.lengthth list.

Martin Dvořák (Oct 24 2022 at 10:37):

Patrick Johnson said:

Feel free to PR it.

https://leanprover.zulipchat.com/#narrow/stream/113488-general/topic/list_take_join.20into.20mathlib

Martin Dvořák (Oct 24 2022 at 11:50):

I think that instead of list.take_join_of_lt_length_join and list.take_join I will make one "export" lemma:

lemma list.take_join_of_lt {α : Type*} {l : list (list α)} {n : } (notall : n < l.join.length) :
   m k : ,  mlt : m < l.length, k < (l.nth_le m mlt).length 
    l.join.take n = (l.take m).join ++ (l.nth_le m mlt).take k :=
begin
  sorry
end

And similarly:

lemma list.drop_join_of_lt {α : Type*} {l : list (list α)} {n : } (notall : n < l.join.length) :
   m k : ,  mlt : m < l.length, k < (l.nth_le m mlt).length 
    l.join.drop n = (l.nth_le m mlt).drop k ++ (l.drop m).join :=
begin
  sorry
end

Martin Dvořák (Oct 24 2022 at 12:59):

[fixed an off-by-one error in the last statement]

Patrick Johnson (Oct 24 2022 at 23:06):

Adding k < (l.nth_le m mlt).length is easy. The m and k from the proof already satisfy it.

The proof has been updated.

Martin Dvořák (Oct 26 2022 at 09:00):

Thanks a lot! I'll finally proceed to PR it (when I attempted it first, I got lost in imports, but I'll work harder this time to resolve it).


Last updated: Dec 20 2023 at 11:08 UTC