@@ -7,9 +7,14 @@ module TypeFun.Data.List where
77import Data.Type.Bool
88import GHC.Exts
99import GHC.TypeLits
10+import TypeFun.Data.Eq
1011import TypeFun.Data.List.Internal
1112import TypeFun.Data.Maybe
1213
14+type family Length (a :: [k]) :: Nat where
15+ Length '[] = 0
16+ Length (a ': as) = 1 + (Length as)
17+
1318-- | Generates unresolvable constraint if fists element is not
1419-- contained inside of second
1520type Elem a s = NothingToConstr (IndexOf a s)
@@ -19,20 +24,15 @@ type Elem a s = NothingToConstr (IndexOf a s)
1924type NotElem a s = JustToConstr (IndexOf a s)
2025 (ElementIsInList a s)
2126
22−
2327-- | Constanints that first argument is a sublist of second. Reduces
2428-- to __(Elem a1 b, Elem a2 b, Elem a3 b, ...)__
2529type family SubList (a :: [k]) (b :: [k]) :: Constraint where
2630 SubList '[] bs = ()
2731 SubList (a ': as) bs = (Elem a bs, SubList as bs)
2832
29−-- type family SubListBool (a :: [k]) (b :: [k]) :: Bool where
30−-- SubListBool '[] bs = 'True
31−-- SubListBool (a ': as) bs = (ElemBool a bs) && (SubListBool as bs)
32−
33−
34−-- type family UniqElemsBool (a :: [k]) :: Bool where
35−-- UniqElemsBool
33+type family NotSubList (a :: [k]) (b :: [k]) :: Constraint where
34+ NotSubList '[] bs = ()
35+ NotSubList (a ': as) bs = (NotElem a bs, NotSubList as bs)
3636
3737type IndexOf a s = IndexOf' 0 a s
3838
@@ -55,3 +55,18 @@ type family IsPrefixOfBool (a :: [k]) (b :: [k]) :: Bool where
5555 IsPrefixOfBool '[] b = 'True
5656 IsPrefixOfBool (a ': as) (a ': bs) = IsPrefixOfBool as bs
5757 IsPrefixOfBool as bs = 'False
58+
59+type ElementIsUniq a s = If (Equal 1 (Count a s))
60+ (() :: Constraint)
61+ (ElementIsNotUniqInList a s)
62+
63+type family Count (a :: k) (s :: [k]) :: Nat where
64+ Count a '[] = 0
65+ Count a (a ': as) = 1 + (Count a as)
66+ Count a (b ': as) = Count a as
67+
68+type UniqElements a = UniqElements' a a
69+
70+type family UniqElements' (a :: [k]) (self :: [k]) :: Constraint where
71+ UniqElements' '[] self = ()
72+ UniqElements' (a ': as) self = (ElementIsUniq a self, UniqElements' as self)