@@ -1,52 +1,183 @@
1-- | Collection of type families on type lists. This module differs
1-- | Collection of type families on type lists. This module differs
2-- from __Data.Singletons.Prelude.List__ because works with __[*]__
2-- from __Data.Singletons.Prelude.List__ because works with __[*]__
3−-- and relies on GHC's type equality (~)
3+-- and relies on GHC's type equality (~). The rest of required
4−
4+-- operations like 'Reverse' or '(:++:)' you could find in singletons
5−module TypeFun.Data.List where
6+module TypeFun.Data.List
7+ ( -- * Primitive operations on lists
27+ -- * Operations with lists
39+ -- * Uniqueness checking
7import Data.Type.Bool
48import Data.Type.Bool
8import GHC.Exts
49import GHC.Exts
9import GHC.TypeLits
50import GHC.TypeLits
10import TypeFun.Data.Eq
51import TypeFun.Data.Eq
11−import TypeFun.Data.List.Errors
12import TypeFun.Data.Maybe
52import TypeFun.Data.Maybe
13import TypeFun.Data.Peano
53import TypeFun.Data.Peano
56+------------------------------------------------------------------------
57+-- NOTE: Errors type classes. These type classes are not in separate --
58+-- module because in this case user could import them and instantiate --
59+-- by mistake. The name of typeclass is an error message in fact. We --
60+-- have not any way to fail constraint explicitly, so this hack was --
62+------------------------------------------------------------------------
64+class ElementNotFoundInList a s
66+class ElementIsInList a s
68+class ListIsNotPrefixOf a b
70+class ListIsPrefixOf a b
72+class ElementIsNotUniqInList a s
74+class IndexNotFoundInList a s
76+----------------------
77+-- main module code --
78+----------------------
15type family Length (a :: [k]) :: N where
81type family Length (a :: [k]) :: N where
16 Length '[] = 'Z
82 Length '[] = 'Z
17 Length (a ': as) = 'S (Length as)
83 Length (a ': as) = 'S (Length as)
85+type family Drop (c :: N) (s :: [k]) :: [k] where
87+ Drop ('S c) '[] = '[]
88+ Drop ('S c) (a ': as) = Drop c as
90+type family Take (c :: N) (s :: [k]) :: [k] where
92+ Take ('S c) '[] = '[]
93+ Take ('S c) (a ': as) = a ': (Take c as)
95+-- | Remove first argument type from anywhere in a list.
96+type family Delete (a :: k) (s :: [k]) :: [k] where
98+ Delete a (a ': as) = 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
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
127+type family IndicesOfMay (a :: [k]) (b :: [k]) :: [Maybe N] where
128+ IndicesOfMay '[] bs = '[]
129+ IndicesOfMay (a ': as) bs = (IndexOfMay a bs) ': (IndicesOfMay as bs)
131+type family IndicesOf (a :: [k]) (b :: [k]) :: [N] where
132+ IndicesOf '[] bs = '[]
133+ IndicesOf (a ': as) bs = (IndexOf a bs) ': (IndicesOf as bs)
135+type Index idx s = FromJust (IndexMay idx s)
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
142+type family IndicesMay (idxs :: [N]) (a :: [k]) :: [Maybe k] where
143+ IndicesMay '[] 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)
19-- | Generates unresolvable constraint if fists element is not
150-- | Generates unresolvable constraint if fists element is not
20-- contained inside of second
151-- contained inside of second
21−type Elem a s = NothingToConstr (IndexOf a s)
152+type Elem a s = NothingToConstr (IndexOfMay a s)
22 (ElementNotFoundInList a s)
153 (ElementNotFoundInList a s)
24-- | Reverse of 'Elem'
155-- | Reverse of 'Elem'
25−type NotElem a s = JustToConstr (IndexOf a s)
156+type NotElem a s = JustToConstr (IndexOfMay a s)
26 (ElementIsInList a s)
157 (ElementIsInList a s)
159+-- | Count elements in a list
160+type family Count (a :: k) (s :: [k]) :: N where
162+ Count a (a ': as) = 'S (Count a as)
163+ Count a (b ': as) = Count a as
28-- | Constanints that first argument is a sublist of second. Reduces
165-- | Constanints that first argument is a sublist of second. Reduces
29-- to __(Elem a1 b, Elem a2 b, Elem a3 b, ...)__
166-- to __(Elem a1 b, Elem a2 b, Elem a3 b, ...)__
30type family SubList (a :: [k]) (b :: [k]) :: Constraint where
167type family SubList (a :: [k]) (b :: [k]) :: Constraint where
31 SubList '[] bs = ()
168 SubList '[] bs = ()
32 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
34type family NotSubList (a :: [k]) (b :: [k]) :: Constraint where
177type family NotSubList (a :: [k]) (b :: [k]) :: Constraint where
35 NotSubList '[] bs = ()
178 NotSubList '[] bs = ()
36 NotSubList (a ': as) bs = (NotElem a bs, NotSubList as bs)
179 NotSubList (a ': as) bs = (NotElem a bs, NotSubList as bs)
38−type IndexOf a s = IndexOf' Z a s
40−type family IndexOf' (acc :: N) (a :: k) (s :: [k]) :: Maybe N where
41− IndexOf' acc a '[] = 'Nothing
42− IndexOf' acc a (a ': as) = 'Just acc
43− IndexOf' acc a (b ': as) = IndexOf' (S acc) a as
45−type family Index (idx :: N) (s :: [k]) :: Maybe k where
46− Index idx '[] = 'Nothing
47− Index Z (a ': as) = 'Just a
48− Index (S idx) (a ': as) = Index idx as
50type IsPrefixOf a b = ( If (IsPrefixOfBool a b)
181type IsPrefixOf a b = ( If (IsPrefixOfBool a b)
51 (() :: Constraint)
182 (() :: Constraint)
52 (ListIsNotPrefixOf a b)
183 (ListIsNotPrefixOf a b)
@@ -62,30 +193,40 @@ type family IsPrefixOfBool (a :: [k]) (b :: [k]) :: Bool where
62 IsPrefixOfBool (a ': as) (a ': bs) = IsPrefixOfBool as bs
193 IsPrefixOfBool (a ': as) (a ': bs) = IsPrefixOfBool as bs
63 IsPrefixOfBool as bs = 'False
194 IsPrefixOfBool as bs = 'False
196+-- | Appends elements from first list to second if they are not
198+type family Union (a :: [k]) (b :: [k]) :: [k] where
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)
206+-- | Append element to list if element is not already presented in
207+type family AppendUniq (a :: k) (s :: [k]) :: [k] where
208+ AppendUniq a (a ': bs) = a ': bs
209+ AppendUniq a (b ': bs) = b ': (AppendUniq a bs)
210+ AppendUniq a '[] = '[a]
212+-- | Calculates intersection between two lists. Order of elements is
213+-- taken from first list
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
65-- | Checks that element 'a' occurs in a list just once
222-- | Checks that element 'a' occurs in a list just once
66type ElementIsUniq a s = If (Equal (S Z) (Count a s))
223type ElementIsUniq a s = If (Equal (S Z) (Count a s))
67 (() :: Constraint)
224 (() :: Constraint)
68 (ElementIsNotUniqInList a s)
225 (ElementIsNotUniqInList a s)
70−-- | Count elements in a list
71−type family Count (a :: k) (s :: [k]) :: N where
73− Count a (a ': as) = 'S (Count a as)
74− Count a (b ': as) = Count a as
76-- | Checks that all elements in list are unique
227-- | Checks that all elements in list are unique
77type UniqElements a = UniqElements' a a
228type UniqElements a = UniqElements' a a
79type family UniqElements' (a :: [k]) (self :: [k]) :: Constraint where
230type family UniqElements' (a :: [k]) (self :: [k]) :: Constraint where
80 UniqElements' '[] self = ()
231 UniqElements' '[] self = ()
81 UniqElements' (a ': as) self = (ElementIsUniq a self, UniqElements' as self)
232 UniqElements' (a ': as) self = (ElementIsUniq a self, UniqElements' as self)
83−type family Drop (c :: N) (s :: [k]) :: [k] where
85− Drop ('S c) '[] = '[]
86− Drop ('S c) (a ': as) = Drop c as
88−type family Take (c :: N) (s :: [k]) :: [k] where
90− Take ('S c) '[] = '[]
91− Take ('S c) (a ': as) = a ': (Take c as)