@@ -9,6 +9,8 @@ module TypeFun.Data.List
12 -- * Elements lookup
14 -- * Elements lookup
14 , IndexOfMay
16 , IndexOfMay
@@ -33,10 +35,14 @@ module TypeFun.Data.List
33 , UnionList
35 , UnionList
34 , AppendUniq
36 , AppendUniq
35 , Intersect
37 , Intersect
36 -- * Uniqueness checking
39 -- * Uniqueness checking
37 , ElementIsUniq
40 , ElementIsUniq
38 , UniqElements
41 , UniqElements
39 , UniqElements'
42 , UniqElements'
42import Data.Type.Bool
48import Data.Type.Bool
@@ -45,6 +51,7 @@ import GHC.TypeLits
45import TypeFun.Data.Eq
51import TypeFun.Data.Eq
46import TypeFun.Data.Maybe
52import TypeFun.Data.Maybe
47import TypeFun.Data.Peano
53import TypeFun.Data.Peano
49------------------------------------------------------------------------
56------------------------------------------------------------------------
50-- NOTE: Errors type classes. These type classes are not in separate --
57-- NOTE: Errors type classes. These type classes are not in separate --
@@ -91,6 +98,23 @@ type family Delete (a :: k) (s :: [k]) :: [k] where
91 Delete a (a ': as) = Delete a as
98 Delete a (a ': as) = Delete a as
92 Delete a (b ': as) = b ': (Delete a as)
99 Delete a (b ': as) = b ': (Delete a as)
101+-- | Remove index from the list
102+type family Remove (i :: N) (a :: [k]) :: [k] where
104+ Remove 'Z (a ': as) = as
105+ Remove ('S i) (a ': as) = a ': (Remove i as)
107+type family (:++:) (a :: [k]) (b :: [k]) :: [k] where
109+ (a ': as) :++: b = a ': (as :++: b)
114+ -> (l ~ (l :++: '[]) => r)
116+appendId = unsafeCoerce id
94type IndexOf a s = FromJust (IndexOfMay a s)
118type IndexOf a s = FromJust (IndexOfMay a s)
96type IndexOfMay a s = IndexOfMay' Z a s
120type IndexOfMay a s = IndexOfMay' Z a s
@@ -144,6 +168,12 @@ type family SubList (a :: [k]) (b :: [k]) :: Constraint where
144 SubList '[] bs = ()
168 SubList '[] bs = ()
145 SubList (a ': as) bs = (Elem a bs, SubList as bs)
169 SubList (a ': as) bs = (Elem a bs, SubList as bs)
174+ -> (SubList l l => r) -> r
175+subListId _ = unsafeCoerce id
147type family NotSubList (a :: [k]) (b :: [k]) :: Constraint where
177type family NotSubList (a :: [k]) (b :: [k]) :: Constraint where
148 NotSubList '[] bs = ()
178 NotSubList '[] bs = ()
149 NotSubList (a ': as) bs = (NotElem a bs, NotSubList as bs)
179 NotSubList (a ': as) bs = (NotElem a bs, NotSubList as bs)
@@ -183,6 +213,12 @@ type family AppendUniq (a :: k) (s :: [k]) :: [k] where
183-- taken from first list
213-- taken from first list
184type Intersect a b = (Indices (CatMaybes (IndicesOfMay a b)) b)
214type Intersect a b = (Indices (CatMaybes (IndicesOfMay a b)) b)
216+-- | Removes from first list all elements occured in second
217+type family Substract (a :: [k]) (b :: [k]) :: [k] where
218+ Substract '[] b = '[]
220+ Substract a (b ': bs) = Substract (Delete b a) bs
186-- | Checks that element 'a' occurs in a list just once
222-- | Checks that element 'a' occurs in a list just once
187type ElementIsUniq a s = If (Equal (S Z) (Count a s))
223type ElementIsUniq a s = If (Equal (S Z) (Count a s))
188 (() :: Constraint)
224 (() :: Constraint)