@@ -11,9 +11,13 @@ module TypeFun.Data.List
12 -- * Elements lookup
12 -- * Elements lookup
14− , IndexOf'
14+ , IndexOfMay
15 , IndicesOf
17 , IndicesOf
@@ -26,6 +30,7 @@ module TypeFun.Data.List
26 , IsPrefixOfBool
30 , IsPrefixOfBool
27 -- * Set operations
31 -- * Set operations
29 , AppendUniq
34 , AppendUniq
30 , Intersect
35 , Intersect
31 -- * Uniqueness checking
36 -- * Uniqueness checking
@@ -59,6 +64,8 @@ class ListIsPrefixOf a b
60class ElementIsNotUniqInList a s
65class ElementIsNotUniqInList a s
67+class IndexNotFoundInList a s
62----------------------
69----------------------
63-- main module code --
70-- main module code --
64----------------------
71----------------------
@@ -84,33 +91,45 @@ type family Delete (a :: k) (s :: [k]) :: [k] where
84 Delete a (a ': as) = Delete a as
91 Delete a (a ': as) = Delete a as
85 Delete a (b ': as) = b ': (Delete a as)
92 Delete a (b ': as) = b ': (Delete a as)
87−type IndexOf a s = IndexOf' Z a s
94+type IndexOf a s = FromJust (IndexOfMay a s)
96+type IndexOfMay a s = IndexOfMay' Z a s
98+type family IndexOfMay' (acc :: N) (a :: k) (s :: [k]) :: Maybe N where
99+ IndexOfMay' acc a '[] = 'Nothing
100+ IndexOfMay' acc a (a ': as) = 'Just acc
101+ IndexOfMay' acc a (b ': as) = IndexOfMay' (S acc) a as
89−type family IndexOf' (acc :: N) (a :: k) (s :: [k]) :: Maybe N where
103+type family IndicesOfMay (a :: [k]) (b :: [k]) :: [Maybe N] where
90− IndexOf' acc a '[] = 'Nothing
104+ IndicesOfMay '[] bs = '[]
91− IndexOf' acc a (a ': as) = 'Just acc
105+ IndicesOfMay (a ': as) bs = (IndexOfMay a bs) ': (IndicesOfMay as bs)
92− IndexOf' acc a (b ': as) = IndexOf' (S acc) a as
94−type family IndicesOf (a :: [k]) (b :: [k]) :: [Maybe N] where
107+type family IndicesOf (a :: [k]) (b :: [k]) :: [N] where
95 IndicesOf '[] bs = '[]
108 IndicesOf '[] bs = '[]
96 IndicesOf (a ': as) bs = (IndexOf a bs) ': (IndicesOf as bs)
109 IndicesOf (a ': as) bs = (IndexOf a bs) ': (IndicesOf as bs)
98−type family Index (idx :: N) (s :: [k]) :: Maybe k where
111+type Index idx s = FromJust (IndexMay idx s)
99− Index idx '[] = 'Nothing
100− Index Z (a ': as) = 'Just a
101− Index (S idx) (a ': as) = Index idx as
113+type family IndexMay (idx :: N) (s :: [k]) :: Maybe k where
114+ IndexMay idx '[] = 'Nothing
115+ IndexMay Z (a ': as) = 'Just a
116+ IndexMay (S idx) (a ': as) = IndexMay idx as
103−type family Indices (idxs :: [N]) (a :: [k]) :: [Maybe k] where
118+type family IndicesMay (idxs :: [N]) (a :: [k]) :: [Maybe k] where
104− Indices '[] as = '[]
119+ IndicesMay '[] as = '[]
120+ IndicesMay (i ': idxs) as = (IndexMay i as) ': (IndicesMay idxs as)
122+type family Indices (idxs :: [N]) (a :: [k]) :: [k] where
123+ Indices '[] as = '[]
105 Indices (i ': idxs) as = (Index i as) ': (Indices idxs as)
124 Indices (i ': idxs) as = (Index i as) ': (Indices idxs as)
107-- | Generates unresolvable constraint if fists element is not
126-- | Generates unresolvable constraint if fists element is not
108-- contained inside of second
127-- contained inside of second
109−type Elem a s = NothingToConstr (IndexOf a s)
128+type Elem a s = NothingToConstr (IndexOfMay a s)
110 (ElementNotFoundInList a s)
129 (ElementNotFoundInList a s)
112-- | Reverse of 'Elem'
131-- | Reverse of 'Elem'
113−type NotElem a s = JustToConstr (IndexOf a s)
132+type NotElem a s = JustToConstr (IndexOfMay a s)
114 (ElementIsInList a s)
133 (ElementIsInList a s)
116-- | Count elements in a list
135-- | Count elements in a list
@@ -150,6 +169,10 @@ type family Union (a :: [k]) (b :: [k]) :: [k] where
150 Union '[] bs = bs
169 Union '[] bs = bs
151 Union (a ': as) bs = Union as (AppendUniq a bs)
170 Union (a ': as) bs = Union as (AppendUniq a bs)
172+type family UnionList (l :: [[k]]) :: [k] where
174+ UnionList (a ': as) = Union a (UnionList as)
153-- | Append element to list if element is not already presented in
176-- | Append element to list if element is not already presented in
154type family AppendUniq (a :: k) (s :: [k]) :: [k] where
177type family AppendUniq (a :: k) (s :: [k]) :: [k] where
155 AppendUniq a (a ': bs) = a ': bs
178 AppendUniq a (a ': bs) = a ': bs
@@ -158,7 +181,7 @@ type family AppendUniq (a :: k) (s :: [k]) :: [k] where
159-- | Calculates intersection between two lists. Order of elements is
182-- | Calculates intersection between two lists. Order of elements is
160-- taken from first list
183-- taken from first list
161−type Intersect a b = (CatMaybes (Indices (CatMaybes (IndicesOf a b)) b))
184+type Intersect a b = (Indices (CatMaybes (IndicesOfMay a b)) b)
163-- | Checks that element 'a' occurs in a list just once
186-- | Checks that element 'a' occurs in a list just once
164type ElementIsUniq a s = If (Equal (S Z) (Count a s))
187type ElementIsUniq a s = If (Equal (S Z) (Count a s))