@@ -9,6 +9,7 @@ module TypeFun.Data.List
99 , Drop
1010 , Take
1111 , Delete
12+ , Remove
1213 , (:++:)
1314 -- * Elements lookup
1415 , IndexOf
@@ -34,10 +35,14 @@ module TypeFun.Data.List
3435 , UnionList
3536 , AppendUniq
3637 , Intersect
38+ , Substract
3739 -- * Uniqueness checking
3840 , ElementIsUniq
3941 , UniqElements
4042 , UniqElements'
43+ -- * Unsafe helpers
44+ , appendId
45+ , subListId
4146 ) where
4247
4348import Data.Type.Bool
@@ -46,6 +51,7 @@ import GHC.TypeLits
4651import TypeFun.Data.Eq
4752import TypeFun.Data.Maybe
4853import TypeFun.Data.Peano
54+import Unsafe.Coerce
4955
5056------------------------------------------------------------------------
5157-- NOTE: Errors type classes. These type classes are not in separate --
@@ -92,10 +98,23 @@ type family Delete (a :: k) (s :: [k]) :: [k] where
9298 Delete a (a ': as) = Delete a as
9399 Delete a (b ': as) = b ': (Delete a as)
94100
101+-- | Remove index from the list
102+type family Remove (i :: N) (a :: [k]) :: [k] where
103+ Remove i '[] = '[]
104+ Remove 'Z (a ': as) = as
105+ Remove ('S i) (a ': as) = a ': (Remove i as)
106+
95107type family (:++:) (a :: [k]) (b :: [k]) :: [k] where
96108 '[] :++: b = b
97109 (a ': as) :++: b = a ': (as :++: b)
98110
111+appendId
112+ :: forall proxy l r
113+ . proxy l
114+ -> (l ~ (l :++: '[]) => r)
115+ -> r
116+appendId = unsafeCoerce id
117+
99118type IndexOf a s = FromJust (IndexOfMay a s)
100119
101120type IndexOfMay a s = IndexOfMay' Z a s
@@ -149,6 +168,12 @@ type family SubList (a :: [k]) (b :: [k]) :: Constraint where
149168 SubList '[] bs = ()
150169 SubList (a ': as) bs = (Elem a bs, SubList as bs)
151170
171+subListId
172+ :: forall proxy l r
173+ . proxy l
174+ -> (SubList l l => r) -> r
175+subListId _ = unsafeCoerce id
176+
152177type family NotSubList (a :: [k]) (b :: [k]) :: Constraint where
153178 NotSubList '[] bs = ()
154179 NotSubList (a ': as) bs = (NotElem a bs, NotSubList as bs)
@@ -188,6 +213,12 @@ type family AppendUniq (a :: k) (s :: [k]) :: [k] where
188213-- taken from first list
189214type Intersect a b = (Indices (CatMaybes (IndicesOfMay a b)) b)
190215
216+-- | Removes from first list all elements occured in second
217+type family Substract (a :: [k]) (b :: [k]) :: [k] where
218+ Substract '[] b = '[]
219+ Substract a '[] = a
220+ Substract a (b ': bs) = Substract (Delete b a) bs
221+
191222-- | Checks that element 'a' occurs in a list just once
192223type ElementIsUniq a s = If (Equal (S Z) (Count a s))
193224 (() :: Constraint)