@@ -9,11 +9,17 @@ module TypeFun.Data.List
12 -- * Elements lookup
14 -- * Elements lookup
14− , IndexOf'
16+ , IndexOfMay
15 , IndicesOf
19 , IndicesOf
@@ -24,13 +30,19 @@ module TypeFun.Data.List
24 , IsPrefixOf
30 , IsPrefixOf
25 , IsNotPrefixOf
31 , IsNotPrefixOf
26 , IsPrefixOfBool
32 , IsPrefixOfBool
28 , AppendUniq
36 , AppendUniq
29 , Intersect
37 , Intersect
30 -- * Uniqueness checking
39 -- * Uniqueness checking
31 , ElementIsUniq
40 , ElementIsUniq
32 , UniqElements
41 , UniqElements
33 , UniqElements'
42 , UniqElements'
36import Data.Type.Bool
48import Data.Type.Bool
@@ -39,6 +51,7 @@ import GHC.TypeLits
39import TypeFun.Data.Eq
51import TypeFun.Data.Eq
40import TypeFun.Data.Maybe
52import TypeFun.Data.Maybe
41import TypeFun.Data.Peano
53import TypeFun.Data.Peano
43------------------------------------------------------------------------
56------------------------------------------------------------------------
44-- NOTE: Errors type classes. These type classes are not in separate --
57-- NOTE: Errors type classes. These type classes are not in separate --
@@ -58,6 +71,8 @@ class ListIsPrefixOf a b
59class ElementIsNotUniqInList a s
72class ElementIsNotUniqInList a s
74+class IndexNotFoundInList a s
61----------------------
76----------------------
62-- main module code --
77-- main module code --
63----------------------
78----------------------
@@ -83,34 +98,62 @@ type family Delete (a :: k) (s :: [k]) :: [k] where
83 Delete a (a ': as) = Delete a as
98 Delete a (a ': as) = Delete a as
84 Delete a (b ': as) = b ': (Delete a as)
99 Delete a (b ': as) = b ': (Delete a as)
86−type IndexOf a s = IndexOf' Z a s
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
118+type IndexOf a s = FromJust (IndexOfMay a s)
120+type IndexOfMay a s = IndexOfMay' Z a s
122+type family IndexOfMay' (acc :: N) (a :: k) (s :: [k]) :: Maybe N where
123+ IndexOfMay' acc a '[] = 'Nothing
124+ IndexOfMay' acc a (a ': as) = 'Just acc
125+ IndexOfMay' acc a (b ': as) = IndexOfMay' (S acc) a as
88−type family IndexOf' (acc :: N) (a :: k) (s :: [k]) :: Maybe N where
127+type family IndicesOfMay (a :: [k]) (b :: [k]) :: [Maybe N] where
89− IndexOf' acc a '[] = 'Nothing
128+ IndicesOfMay '[] bs = '[]
90− IndexOf' acc a (a ': as) = 'Just acc
129+ IndicesOfMay (a ': as) bs = (IndexOfMay a bs) ': (IndicesOfMay as bs)
91− IndexOf' acc a (b ': as) = IndexOf' (S acc) a as
93−type family IndicesOf (a :: [k]) (b :: [k]) :: [Maybe N] where
131+type family IndicesOf (a :: [k]) (b :: [k]) :: [N] where
94 IndicesOf '[] bs = '[]
132 IndicesOf '[] bs = '[]
95 IndicesOf (a ': as) bs = (IndexOf a bs) ': (IndicesOf as bs)
133 IndicesOf (a ': as) bs = (IndexOf a bs) ': (IndicesOf as bs)
97−type family Index (idx :: N) (s :: [k]) :: Maybe k where
135+type Index idx s = FromJust (IndexMay idx s)
98− Index idx '[] = 'Nothing
99− Index Z (a ': as) = 'Just a
100− Index (S idx) (a ': as) = Index idx as
137+type family IndexMay (idx :: N) (s :: [k]) :: Maybe k where
138+ IndexMay idx '[] = 'Nothing
139+ IndexMay Z (a ': as) = 'Just a
140+ IndexMay (S idx) (a ': as) = IndexMay idx as
102−type family Indices (idxs :: [N]) (a :: [k]) :: [Maybe k] where
142+type family IndicesMay (idxs :: [N]) (a :: [k]) :: [Maybe k] where
103− Indices '[] as = '[]
143+ IndicesMay '[] as = '[]
104− Indices (i ': idxs) as = (Index i as) ': (Indices idxs as)
144+ IndicesMay (i ': idxs) as = (IndexMay i as) ': (IndicesMay idxs as)
146+type family Indices (idxs :: [N]) (a :: [k]) :: [k] where
147+ Indices '[] as = '[]
148+ Indices (i ': idxs) as = (Index i as) ': (Indices idxs as)
107-- | Generates unresolvable constraint if fists element is not
150-- | Generates unresolvable constraint if fists element is not
108-- contained inside of second
151-- contained inside of second
109−type Elem a s = NothingToConstr (IndexOf a s)
152+type Elem a s = NothingToConstr (IndexOfMay a s)
110 (ElementNotFoundInList a s)
153 (ElementNotFoundInList a s)
112-- | Reverse of 'Elem'
155-- | Reverse of 'Elem'
113−type NotElem a s = JustToConstr (IndexOf a s)
156+type NotElem a s = JustToConstr (IndexOfMay a s)
114 (ElementIsInList a s)
157 (ElementIsInList a s)
116-- | Count elements in a list
159-- | Count elements in a list
@@ -125,6 +168,12 @@ type family SubList (a :: [k]) (b :: [k]) :: Constraint where
125 SubList '[] bs = ()
168 SubList '[] bs = ()
126 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
128type family NotSubList (a :: [k]) (b :: [k]) :: Constraint where
177type family NotSubList (a :: [k]) (b :: [k]) :: Constraint where
129 NotSubList '[] bs = ()
178 NotSubList '[] bs = ()
130 NotSubList (a ': as) bs = (NotElem a bs, NotSubList as bs)
179 NotSubList (a ': as) bs = (NotElem a bs, NotSubList as bs)
@@ -150,6 +199,10 @@ type family Union (a :: [k]) (b :: [k]) :: [k] where
150 Union '[] bs = bs
199 Union '[] bs = bs
151 Union (a ': as) bs = Union as (AppendUniq a bs)
200 Union (a ': as) bs = Union as (AppendUniq a bs)
202+type family UnionList (l :: [[k]]) :: [k] where
204+ UnionList (a ': as) = Union a (UnionList as)
153-- | Append element to list if element is not already presented in
206-- | Append element to list if element is not already presented in
154type family AppendUniq (a :: k) (s :: [k]) :: [k] where
207type family AppendUniq (a :: k) (s :: [k]) :: [k] where
155 AppendUniq a (a ': bs) = a ': bs
208 AppendUniq a (a ': bs) = a ': bs
@@ -158,7 +211,13 @@ type family AppendUniq (a :: k) (s :: [k]) :: [k] where
159-- | Calculates intersection between two lists. Order of elements is
212-- | Calculates intersection between two lists. Order of elements is
160-- taken from first list
213-- taken from first list
161−type Intersect a b = (CatMaybes (Indices (CatMaybes (IndicesOf a b)) b))
214+type 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
163-- | Checks that element 'a' occurs in a list just once
222-- | Checks that element 'a' occurs in a list just once
164type ElementIsUniq a s = If (Equal (S Z) (Count a s))
223type ElementIsUniq a s = If (Equal (S Z) (Count a s))