module Data.List.Membership where

Membership in lists🔗

We can inductively define a notion of membership between elements and lists we write However, note that this notion of membership is not a proposition! The type has at least as many inhabitants as there are occurrences of in and if the type is not a set, then each proof of can be acted on by a loop on to give a new proof.

data _∈ₗ_ {ℓ} {A : Type ℓ} (x : A) : List A → Type ℓ where
  here  : ∀ {x'} (p : x ≡ x') → x ∈ₗ (x' ∷ xs)
  there : (p : x ∈ₗ xs)       → x ∈ₗ (y ∷ xs)

There is a more (homotopically) straightforward characterisation of membership in lists: the fibres of the lookup function xs ! i. These are given by an index living in the standard finite set with as many elements as the list has positions, together with a proof that

The inverse equivalences between these notions is defined below; the proof that they are are inverses is a straightforward induction in both cases, so it’s omitted for space.

element→!-fibre : ∀ {x : A} {xs} → x ∈ xs → fibre (xs !_) x
element→!-fibre (here p) = fzero , sym p
element→!-fibre (there prf) with element→!-fibre prf
... | ix , p = fsuc ix , p

!-fibre→element : ∀ {x : A} {xs} → fibre (xs !_) x → x ∈ xs
!-fibre→element {A = A} {x = x} = λ (ix , p) → go ix p module !-fibre→element where
  go : ∀ {xs} (ix : Fin (length xs)) → xs ! ix ≡ x → x ∈ xs
  go {xs = x ∷ xs} fzero p     = here  (sym p)
  go {xs = x ∷ xs} (fsuc ix) p = there (go ix p)

The equivalence between these definitions explains why can be so complicated. First, it’s at least a set, since it stores the index. Second, it stores a path, which can be arbitrarily complicated depending on the type

Despite the potential complexity of we do note that if is a set, then all that matters is the index; If is moreover discrete, then is decidable.

elem? : ⩃ _ : Discrete A ⩄ (x : A) (xs : List A) → Dec (x ∈ xs)
elem? x [] = no λ ()
elem? x (y ∷ xs) with x ≡ᵢ? y
... | yes refláµ¢ = yes (here refl)
... | no ¬p with elem? x xs
... | yes p = yes (there p)
... | no ¬q = no λ { (here p) → ¬p (Id≃path.from p) ; (there q) → ¬q q }

Removing duplicates🔗

Still working with a discrete type, we can define a function nub which removes duplicates from a list. It is constructed by inductively deciding whether or not to keep the head of the list, discarding those which already appear further down. This has terrible the terrible time complexity but it works for an arbitrary discrete type, which is the best possible generality.

nub-cons : (x : A) (xs : List A) → Dec (x ∈ xs) → List A
nub-cons x xs (yes _) = xs
nub-cons x xs (no _)  = x ∷ xs

nub : ⩃ _ : Discrete A ⩄ → List A → List A
nub []       = []
nub (x ∷ xs) = nub-cons x (nub xs) (elem? x (nub xs))

The function nub is characterised by the following two facts. First, membership in nub is a proposition — each element appears at most once. Second, membership is (logically) preserved when nubbing a list — note that the function mapping old indices to new indices is not injective, since all occurrences of an element will be mapped to the same (first) occurrence in the deduplicated list.

member-nub-is-prop
  : ∀ ⩃ _ : Discrete A ⩄ {x : A} (xs : List A) → is-prop (x ∈ nub xs)
member→member-nub
  : ∀ ⩃ _ : Discrete A ⩄ {x : A} {xs : List A} → x ∈ xs → x ∈ nub xs
The proofs here are also straightforward inductive arguments.
member-nub-is-prop (x ∷ xs) p1 p2 with elem? x (nub xs) | p1 | p2
... | yes p | p1 | p2 = member-nub-is-prop xs p1 p2
... | no ¬p | here  p1 | here  p2 = ap _∈ₗ_.here (Discrete→is-set auto _ _ p1 p2)
... | no ¬p | here  p1 | there p2 = absurd (¬p (subst (_∈ nub xs) p1 p2))
... | no ¬p | there p1 | here  p2 = absurd (¬p (subst (_∈ nub xs) p2 p1))
... | no ¬p | there p1 | there p2 = ap there (member-nub-is-prop xs p1 p2)

member→member-nub {xs = x ∷ xs} (here p) with elem? x (nub xs)
... | yes x∈nub = subst (_∈ nub xs) (sym p) x∈nub
... | no ¬x∈nub = here p
member→member-nub {xs = x ∷ xs} (there α) with elem? x (nub xs)
... | yes x∈nub = member→member-nub α
... | no ¬x∈nub = there (member→member-nub α)