on bf4e535 help sign in
38 lines 1.3 kB history raw
1module Data.List
3%access public
5||| A proof that some element is found in a list.
6|||
7||| Example: `the (Elem "bar" ["foo", "bar", "baz"]) (tactics { search })`
8data Elem : a -> List a -> Type where
9 ||| A proof that the element is at the front of the list.
10 |||
11 ||| Example: `the (Elem "a" ["a", "b"]) Here`
12 Here : Elem x (x :: xs)
13 ||| A proof that the element is after the front of the list
14 |||
15 ||| Example: `the (Elem "b" ["a", "b"]) (There Here)`
16 There : Elem x xs -> Elem x (y :: xs)
18instance Uninhabited (Elem {a} x []) where
19 uninhabited Here impossible
20 uninhabited (There p) impossible
22||| Is the given element a member of the given list.
23|||
24||| @x The element to be tested.
25||| @xs The list to be checked against
26isElem : DecEq a => (x : a) -> (xs : List a) -> Dec (Elem x xs)
27isElem x [] = No absurd
28isElem x (y :: xs) with (decEq x y)
29 isElem x (x :: xs) | (Yes Refl) = Yes Here
30 isElem x (y :: xs) | (No contra) with (isElem x xs)
31 isElem x (y :: xs) | (No contra) | (Yes prf) = Yes (There prf)
32 isElem x (y :: xs) | (No contra) | (No f) = No (mkNo contra f)
33 where
34 mkNo : {xs' : List a} ->
35 ((x' = y') -> Void) -> (Elem x' xs' -> Void) ->
36 Elem x' (y' :: xs') -> Void
37 mkNo f g Here = f Refl
38 mkNo f g (There x) = g x