@@ -35,10 +35,14 @@ module TypeFun.Data.List
3535 , UnionList
3636 , AppendUniq
3737 , Intersect
38+ , Substract
3839 -- * Uniqueness checking
3940 , ElementIsUniq
4041 , UniqElements
4142 , UniqElements'
43+ -- * Unsafe helpers
44+ , appendId
45+ , subListId
4246 ) where
4347
4448import Data.Type.Bool
@@ -47,6 +51,7 @@ import GHC.TypeLits
4751import TypeFun.Data.Eq
4852import TypeFun.Data.Maybe
4953import TypeFun.Data.Peano
54+import Unsafe.Coerce
5055
5156------------------------------------------------------------------------
5257-- NOTE: Errors type classes. These type classes are not in separate --
@@ -103,6 +108,13 @@ type family (:++:) (a :: [k]) (b :: [k]) :: [k] where
103108 '[] :++: b = b
104109 (a ': as) :++: b = a ': (as :++: b)
105110
111+appendId
112+ :: forall proxy l r
113+ . proxy l
114+ -> (l ~ (l :++: '[]) => r)
115+ -> r
116+appendId = unsafeCoerce id
117+
106118type IndexOf a s = FromJust (IndexOfMay a s)
107119
108120type IndexOfMay a s = IndexOfMay' Z a s
@@ -156,6 +168,12 @@ type family SubList (a :: [k]) (b :: [k]) :: Constraint where
156168 SubList '[] bs = ()
157169 SubList (a ': as) bs = (Elem a bs, SubList as bs)
158170
171+subListId
172+ :: forall proxy l r
173+ . proxy l
174+ -> (SubList l l => r) -> r
175+subListId _ = unsafeCoerce id
176+
159177type family NotSubList (a :: [k]) (b :: [k]) :: Constraint where
160178 NotSubList '[] bs = ()
161179 NotSubList (a ': as) bs = (NotElem a bs, NotSubList as bs)
@@ -195,6 +213,12 @@ type family AppendUniq (a :: k) (s :: [k]) :: [k] where
195213-- taken from first list
196214type Intersect a b = (Indices (CatMaybes (IndicesOfMay a b)) b)
197215
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+
198222-- | Checks that element 'a' occurs in a list just once
199223type ElementIsUniq a s = If (Equal (S Z) (Count a s))
200224 (() :: Constraint)