@@ -7,9 +7,14 @@ module TypeFun.Data.List where
7import Data.Type.Bool
7import Data.Type.Bool
8import GHC.Exts
8import GHC.Exts
9import GHC.TypeLits
9import GHC.TypeLits
10+import TypeFun.Data.Eq
10import TypeFun.Data.List.Internal
11import TypeFun.Data.List.Internal
11import TypeFun.Data.Maybe
12import TypeFun.Data.Maybe
14+type family Length (a :: [k]) :: Nat where
16+ Length (a ': as) = 1 + (Length as)
13-- | Generates unresolvable constraint if fists element is not
18-- | Generates unresolvable constraint if fists element is not
14-- contained inside of second
19-- contained inside of second
15type Elem a s = NothingToConstr (IndexOf a s)
20type Elem a s = NothingToConstr (IndexOf a s)
@@ -19,20 +24,15 @@ type Elem a s = NothingToConstr (IndexOf a s)
19type NotElem a s = JustToConstr (IndexOf a s)
24type NotElem a s = JustToConstr (IndexOf a s)
20 (ElementIsInList a s)
25 (ElementIsInList a s)
23-- | Constanints that first argument is a sublist of second. Reduces
27-- | Constanints that first argument is a sublist of second. Reduces
24-- to __(Elem a1 b, Elem a2 b, Elem a3 b, ...)__
28-- to __(Elem a1 b, Elem a2 b, Elem a3 b, ...)__
25type family SubList (a :: [k]) (b :: [k]) :: Constraint where
29type family SubList (a :: [k]) (b :: [k]) :: Constraint where
26 SubList '[] bs = ()
30 SubList '[] bs = ()
27 SubList (a ': as) bs = (Elem a bs, SubList as bs)
31 SubList (a ': as) bs = (Elem a bs, SubList as bs)
29−-- type family SubListBool (a :: [k]) (b :: [k]) :: Bool where
33+type family NotSubList (a :: [k]) (b :: [k]) :: Constraint where
30−-- SubListBool '[] bs = 'True
34+ NotSubList '[] bs = ()
31−-- SubListBool (a ': as) bs = (ElemBool a bs) && (SubListBool as bs)
35+ NotSubList (a ': as) bs = (NotElem a bs, NotSubList as bs)
34−-- type family UniqElemsBool (a :: [k]) :: Bool where
37type IndexOf a s = IndexOf' 0 a s
37type IndexOf a s = IndexOf' 0 a s
@@ -55,3 +55,18 @@ type family IsPrefixOfBool (a :: [k]) (b :: [k]) :: Bool where
55 IsPrefixOfBool '[] b = 'True
55 IsPrefixOfBool '[] b = 'True
56 IsPrefixOfBool (a ': as) (a ': bs) = IsPrefixOfBool as bs
56 IsPrefixOfBool (a ': as) (a ': bs) = IsPrefixOfBool as bs
57 IsPrefixOfBool as bs = 'False
57 IsPrefixOfBool as bs = 'False
59+type ElementIsUniq a s = If (Equal 1 (Count a s))
61+ (ElementIsNotUniqInList a s)
63+type family Count (a :: k) (s :: [k]) :: Nat where
65+ Count a (a ': as) = 1 + (Count a as)
66+ Count a (b ': as) = Count a as
68+type UniqElements a = UniqElements' a a
70+type family UniqElements' (a :: [k]) (self :: [k]) :: Constraint where
71+ UniqElements' '[] self = ()
72+ UniqElements' (a ': as) self = (ElementIsUniq a self, UniqElements' as self)