3 | module Data.CheckedEmpty.List.Lazy.Elem
5 | import Data.CheckedEmpty.List.Lazy
7 | import Data.Singleton
8 | import Decidable.Equality
9 | import Control.Function
19 | data Elem : a -> LazyLst ne a -> Type where
21 | Here : {0 u0 : _} -> {0 u1 : _} ->
22 | {0 xs : Lazy (LazyLst _ _)} ->
23 | Elem x $
(x :: xs) {ne} @{u0} @{u1}
25 | There : {0 u0 : _} -> {0 u1 : _} ->
26 | {0 xs : Lazy (LazyLst _ _)} ->
27 | Elem x xs -> Elem x $
(y :: xs) @{u0} @{u1}
30 | Uninhabited (Here {u0} {u1} {x} {xs} = There {u0=u0'} {u1=u1'} {x = x'} {y} {xs=xs'} e) where
31 | uninhabited Refl
impossible
34 | Uninhabited (There {u0} {u1} {x} {y} {xs} e = Here {u0=u0'} {u1=u1'} {x=x'} {xs=xs'}) where
35 | uninhabited Refl
impossible
38 | Injective (There {u0} {u1} {x} {y} {xs}) where
39 | injective Refl = Refl
42 | DecEq (Elem x xs) where
43 | decEq Here Here = Yes Refl
44 | decEq (There this) (There that) = decEqCong $
decEq this that
45 | decEq Here (There later) = No absurd
46 | decEq (There later) Here = No absurd
49 | Uninhabited (Elem x []) where
50 | uninhabited $ Here
impossible
51 | uninhabited $ There p
impossible
54 | Uninhabited (x = z) => Uninhabited (Elem z xs) =>
55 | Uninhabited (Elem z $
(x :: xs) @{u0} @{u1}) where
56 | uninhabited Here @{xz} = uninhabited Refl @{xz}
57 | uninhabited $
There y = uninhabited y
61 | neitherHereNorThere : {0 xs : LazyLst ne a} ->
62 | Not (x = y) -> Not (Elem x xs) ->
63 | Not (Elem x $
(y :: xs) @{u0} @{u1})
64 | neitherHereNorThere xny _ $
Here = xny Refl
65 | neitherHereNorThere _ xnxs $
There xxs = xnxs xxs
69 | isElem : DecEq a => (x : a) -> (xs : LazyLst ne a) -> Dec (Elem x xs)
70 | isElem x $
[] = No absurd
71 | isElem x $
y :: xs with (decEq x y)
72 | isElem x $
x :: xs | Yes Refl = Yes Here
73 | isElem x $
y :: xs | No xny with (isElem x xs)
74 | isElem x $
y :: xs | No xny | Yes xxs = Yes $
There xxs
75 | isElem x $
y :: Delay xs | No xny | No xnxs = No $
neitherHereNorThere xny xnxs
79 | get : (xs : LazyLst ne a) -> (p : Elem x xs) -> a
80 | get (x :: _) $
Here = x
81 | get (_ :: xs) $
There p = get xs p
85 | lookup : (xs : LazyLst ne a) -> (p : Elem x xs) -> Singleton x
86 | lookup (x :: _) Here = Val x
87 | lookup (_ :: xs) $
There p = lookup xs p
91 | dropElem : (xs : LazyLst ne a) -> (p : Elem x xs) -> LazyLst0 a
92 | dropElem (_ :: ys) $
Here = relaxF ys
93 | dropElem (x :: ys) $
There p = x :: dropElem ys p
97 | elemToNat : Elem x xs -> Nat
99 | elemToNat (There p) = S $
elemToNat p
103 | indexElem : Nat -> (xs : LazyLst ne a) -> Maybe (
x ** Elem x xs)
104 | indexElem _ [] = Nothing
105 | indexElem Z (y :: _) = Just (
y ** Here)
106 | indexElem (S n) (_ :: ys) = indexElem n ys <&> \(
x ** p)
=> (
x ** There p)
110 | elemMap : {0 xs : LazyLst ne a} -> (0 f : a -> b) ->
111 | Elem x xs -> Elem (f x) (map f xs)
112 | elemMap f $
Here = Here
113 | elemMap f $
There e = There $
elemMap f e