Zulip Chat Archive

Stream: Is there code for X?

Topic: Trivialities about lists


Martin Dvořák (Feb 15 2024 at 14:29):

Do we have them?

import Mathlib

variable {α : Type}

lemma nmem_replicate_of_neq {a b : α} (hab : a  b) (n : ) :
    a  List.replicate n b := by
  sorry

open scoped List

lemma sublist_append_nmem_left {d : α} {l x y : List α}
    (dnix : d  x) (subl : d :: l <+ x ++ y) :
    d :: l <+ y := by
  sorry

Damiano Testa (Feb 15 2024 at 14:49):

I could not find them (which does not mean much), but discovered docs#List.replicate_subset_singleton which gives this proof for the first:

lemma nmem_replicate_of_neq {a b : α} (hab : a  b) (n : ) :
    a  List.replicate n b := by
  apply Set.not_mem_subset (List.replicate_subset_singleton n b)
  erw [List.mem_singleton]
  assumption

where

  • apply cannot be changed to refine,
  • erw cannot be changed to rw.

This makes me suspect that something weird is going on.

Timo Carlin-Burns (Feb 15 2024 at 18:30):

nmem_replicate_of_neq is the contrapositive of docs#eq_of_mem_replicate

Martin Dvořák (Feb 15 2024 at 19:24):

Oh there is also docs#List.mem_replicate for the most general statement.

Kevin Buzzard (Feb 15 2024 at 19:26):

Damiano Testa said:

I could not find them (which does not mean much), but discovered docs#List.replicate_subset_singleton which gives this proof for the first:

lemma nmem_replicate_of_neq {a b : α} (hab : a  b) (n : ) :
    a  List.replicate n b := by
  apply Set.not_mem_subset (List.replicate_subset_singleton n b)
  erw [List.mem_singleton]
  assumption

where

  • apply cannot be changed to refine,
  • erw cannot be changed to rw.

This makes me suspect that something weird is going on.

The proof abuses defeq: List.replicate_subset_singleton n b uses \subseteq on lists, but you're using Set.not_mem_subset on it, which expects \subseteq on sets. Everything is thus unfolded and the chaos thus ensues.

import Mathlib

variable {α : Type*}

lemma List.not_mem_subset {α : Type*} {a : α} {s t : List α} (h : s  t) (ha : a  t) : a  s := by
  sorry

lemma nmem_replicate_of_neq {a b : α} (hab : a  b) (n : ) :
    a  List.replicate n b := by
  refine List.not_mem_subset (List.replicate_subset_singleton n b) ?_
  rw [List.mem_singleton]
  assumption

Damiano Testa (Feb 15 2024 at 20:45):

Kevin, thanks: I had completely overlooked the Set namespace! Now everything is clear!

Martin Dvořák (Feb 16 2024 at 09:32):

Here is a proof of the second lemma:

lemma List.sublist_append_nmem_left {α : Type} {d : α} {l x y : List α}
    (dnix : d  x) (subl : d :: l <+ x ++ y) :
    d :: l <+ y := by
  induction x with
  | nil => exact subl
  | cons a _ ih =>
    apply ih (dnix  List.Mem.tail a)
    cases subl <;> aesop

Do we want it in Mathlib?

Yaël Dillies (Feb 16 2024 at 09:36):

Surely that can be made an iff?

Martin Dvořák (Feb 16 2024 at 09:42):

Yes! Do you want to suggest a name for it?

Yaël Dillies (Feb 16 2024 at 09:46):

List.cons_sublist_append_iff_right?

Martin Dvořák (Feb 16 2024 at 09:49):

Did you intentionally avoid d ∉ x in the lemma name?

Yaël Dillies (Feb 16 2024 at 09:49):

I think it's pretty clear from context but could be added if disambiguation is needed

Martin Dvořák (Feb 16 2024 at 09:50):

All right, I'll use the name you suggested.

Martin Dvořák (Feb 16 2024 at 10:04):

#10629

Eric Wieser (Feb 16 2024 at 10:22):

Can you add a more general version, (h : l₂.Disjoint l₄) (h' : l₂ ≠ []) : l₁ ++ l₂ <+ l₃ ++ l₄ ↔ l₁ ++ l₂ <+ l₃?

Eric Wieser (Feb 16 2024 at 10:22):

(and then the obvious symmetric version)

Martin Dvořák (Feb 16 2024 at 10:26):

docs#List.Disjoint

Eric Wieser (Feb 16 2024 at 10:33):

I guess ¬l₂ <+ l₄ and ¬l₄ <+ l₂ would be a tighter constraint

Martin Dvořák (Feb 16 2024 at 10:34):

Eric Wieser said:

Can you add a more general version

Maybe in addition to my lemma, not as its replacement. Even when I add both

lemma lllllllllllll {l₃ l₄ : List α} (h : l₂.Disjoint l₄) (h' : l₂  []) :
    l₁ ++ l₂ <+ l₃ ++ l₄  l₁ ++ l₂ <+ l₃ :=
  sorry

lemma llllllllllllll {l₃ l₄ : List α} (h : l₁.Disjoint l₃) (h' : l₁  []) :
    l₁ ++ l₂ <+ l₃ ++ l₄  l₁ ++ l₂ <+ l₄ :=
  sorry

exact? still won't solve my original problem.

Eric Wieser (Feb 16 2024 at 10:34):

Indeed, I'm suggesting you add those lemmas to provide a one- or two- line proof of your version

Martin Dvořák (Feb 16 2024 at 10:38):

Eric Wieser said:

I guess ¬l₂ <+ l₄ and ¬l₄ <+ l₂ would be a tighter constraint

What about them?

Eric Wieser (Feb 16 2024 at 10:43):

They should work instead of l₂.Disjoint l₄, right?

Patrick Massot (Feb 16 2024 at 12:16):

Aren't all those list lemmas better suited for Std than for Mathlib?

Eric Wieser (Feb 17 2024 at 08:18):

It's probably more pleasant to prove them in mathlib first, then check if they still compile in Std once you're done


Last updated: May 02 2025 at 03:31 UTC