Zulip Chat Archive

Stream: Is there code for X?

Topic: single version of List.removeAll?


Malvin Gattinger (May 27 2024 at 19:19):

I am wondering: List.erase is to List.diff like ??? is to List.removeAll?

import Batteries.Data.List.Basic

-- only removing first occurrence:
example : List.erase [1,2,2,1] 1 == [2,2,1] := by simp; decide -- single element to be removed
example : List.diff [1,2,2,1] [1] == [2,2,1] := by simp; decide -- list to be removed

-- removing all occurrences:
example : List.removeAll [1,2,2,1] [1] == [2,2] := by simp; decide -- list to be removed

-- Is this in Mathlib with a different name?
def List.remove [BEq α] (l : List α) (x : α) : List α := l.filter (fun y => x != y)

example : List.remove [1,2,2,1] 1 == [2,2] := by simp; decide

Mario Carneiro (May 27 2024 at 19:22):

l.filter (. != a)?

Malvin Gattinger (May 27 2024 at 19:30):

Okay, that is short enough :smile: I was just expecting a named function ;)

SnO2WMaN (Jun 05 2024 at 21:01):

Originally there was a List.remove, but for some reason it was removed in this PR. https://github.com/leanprover-community/mathlib4/pull/11846

I want to re-add this and some lemma about it since commonly used in our project, how about?

Kim Morrison (Jun 05 2024 at 21:35):

My preference is not to. Adding to the List API comes at huge expense if we actually want to reach a complete and mostly-confluent simp set. Keeping the API surface area smaller in the meantime is valuable.

Malvin Gattinger (Jun 06 2024 at 07:10):

Ah, thanks. So to make my definitions simp-able easily, should I use docs#List.removeAll with a singleton list as argument, or something like l.filter (. != a) from Mario above?

Kim Morrison (Jun 06 2024 at 13:29):

l.filter (. != a) is the preferred spelling, I think.


Last updated: May 02 2025 at 03:31 UTC