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