2021-06-28 22:34:39 +03:00
|
|
|
module Data.SnocList.Elem
|
|
|
|
|
|
|
|
import Data.SnocList
|
|
|
|
import Decidable.Equality
|
2021-11-26 13:55:17 +03:00
|
|
|
import Control.Function
|
2021-06-28 22:34:39 +03:00
|
|
|
|
|
|
|
||| A proof that some element is found in a list.
|
|
|
|
public export
|
|
|
|
data Elem : a -> SnocList a -> Type where
|
|
|
|
||| A proof that the element is at the head of the list
|
|
|
|
Here : Elem x (sx :< x)
|
|
|
|
||| A proof that the element is in the tail of the list
|
2021-11-26 13:55:17 +03:00
|
|
|
There : {0 x, y : a} -> Elem x sx -> Elem x (sx :< y)
|
2021-06-28 22:34:39 +03:00
|
|
|
|
|
|
|
export
|
|
|
|
Uninhabited (Here = There e) where
|
|
|
|
uninhabited Refl impossible
|
|
|
|
|
|
|
|
export
|
|
|
|
Uninhabited (There e = Here) where
|
|
|
|
uninhabited Refl impossible
|
|
|
|
|
|
|
|
export
|
|
|
|
Uninhabited (Elem {a} x [<]) where
|
|
|
|
uninhabited Here impossible
|
|
|
|
uninhabited (There p) impossible
|
|
|
|
|
|
|
|
export
|
2021-11-26 13:55:17 +03:00
|
|
|
Injective (There {x} {y} {sx}) where
|
|
|
|
injective Refl = Refl
|
2021-06-28 22:34:39 +03:00
|
|
|
|
|
|
|
export
|
|
|
|
DecEq (x `Elem` sx) where
|
|
|
|
decEq Here Here = Yes Refl
|
|
|
|
decEq Here (There _) = No absurd
|
|
|
|
decEq (There _) Here = No absurd
|
|
|
|
decEq (There y) (There z) with (decEq y z)
|
|
|
|
decEq (There y) (There y) | Yes Refl = Yes Refl
|
2021-11-26 13:55:17 +03:00
|
|
|
decEq (There y) (There z) | No neq = No $ neq . injective
|
2021-06-28 22:34:39 +03:00
|
|
|
|
|
|
|
||| Remove the element at the given position.
|
|
|
|
public export
|
|
|
|
dropElem : (sx : SnocList a) -> (p : Elem x sx) -> SnocList a
|
|
|
|
dropElem (sy :< _) Here = sy
|
|
|
|
dropElem (sy :< y) (There p) = (dropElem sy p) :< y
|
|
|
|
|
|
|
|
||| Erase the indices, returning the numeric position of the element
|
|
|
|
public export
|
|
|
|
elemToNat : Elem x sx -> Nat
|
|
|
|
elemToNat Here = Z
|
|
|
|
elemToNat (There p) = S (elemToNat p)
|
|
|
|
|
|
|
|
||| Find the element with a proof at a given position (in reverse), if it is valid
|
|
|
|
public export
|
|
|
|
indexElem : Nat -> (sx : SnocList a) -> Maybe (x ** Elem x sx)
|
|
|
|
indexElem _ [<] = Nothing
|
|
|
|
indexElem Z (_ :< y) = Just (y ** Here)
|
|
|
|
indexElem (S k) (sy :< _) = (\(y ** p) => (y ** There p)) `map` (indexElem k sy)
|
|
|
|
|
|
|
|
||| Lift the membership proof to a mapped list
|
|
|
|
export
|
|
|
|
elemMap : (0 f : a -> b) -> Elem x sx -> Elem (f x) (map f sx)
|
|
|
|
elemMap f Here = Here
|
|
|
|
elemMap f (There el) = There $ elemMap f el
|
|
|
|
|
|
|
|
||| An item not in the head and not in the tail is not in the list at all.
|
|
|
|
export
|
|
|
|
neitherHereNorThere : Not (x = y) -> Not (Elem x sx) -> Not (Elem x (sx :< y))
|
|
|
|
neitherHereNorThere xny _ Here = xny Refl
|
|
|
|
neitherHereNorThere _ xnxs (There xxs) = xnxs xxs
|