Zulip Chat Archive

Stream: Is there code for X?

Topic: Index from List membership?


Scott Morrison (Oct 05 2023 at 07:14):

Any suggestions for

def idx_of_mem' [DecidableEq α] (xs : List α) (w : y  xs) :
    { i : Nat // xs.get? i = some y } := by

I can probably get there by bashing at List.findIdx? for a bit, but it seems awkward.

Scott Morrison (Oct 05 2023 at 07:37):

Done, but it doesn't fit in the margin here... Still curious if anyone sees something less icky.

Kayla Thomas (Oct 05 2023 at 07:40):

List.mem_iff_get? ?

Kayla Thomas (Oct 05 2023 at 07:46):

Not sure if this is what you are looking for. Kind of hacked at it.

def idx_of_mem' {α : Type} [DecidableEq α] (y : α) (xs : List α) (w : y  xs) :
    { i : Nat // xs.get? i = some y } := by
    simp only [List.mem_iff_get?] at w
    exact Encodable.chooseX w

Kayla Thomas (Oct 05 2023 at 07:46):

Added y as a param.

Scott Morrison (Oct 05 2023 at 07:55):

Ah, nice. I'd use Nat.find and Nat.find_spec as they are less deeply embedded in Mathlib than Encodable.chooseX.

Kayla Thomas (Oct 05 2023 at 07:56):

Cool. I just did an apply? and it gave me the last line :)

Eric Wieser (Oct 05 2023 at 09:20):

Would it be better to return the element of Fin xs.length here?

Ioannis Konstantoulas (Oct 05 2023 at 09:35):

Indeed, I would find the following more convenient:

def fidx_of_mem {α : Type} [DecidableEq α] (x : α) (xs : List α) (h : x  xs) :
    {i : Fin xs.length // xs[i] = x} :=
  sorry

I don't see any shortcuts on how to write this function compared to Kayla's suggestion, though.


Last updated: Dec 20 2023 at 11:08 UTC