module TypeFun.Data.List
(
Length
, Drop
, Take
, Delete
, Remove
, (:++:)
, IndexOf
, IndexOfMay
, IndexOfMay'
, IndicesOfMay
, IndicesOf
, Index
, IndexMay
, IndicesMay
, Indices
, Elem
, NotElem
, Count
, SubList
, NotSubList
, IsPrefixOf
, IsNotPrefixOf
, IsPrefixOfBool
, Union
, UnionList
, AppendUniq
, Intersect
, Substract
, ElementIsUniq
, UniqElements
, UniqElements'
, appendId
, subListId
) where
import Data.Type.Bool
import GHC.Exts
import GHC.TypeLits
import TypeFun.Data.Eq
import TypeFun.Data.Maybe
import TypeFun.Data.Peano
import Unsafe.Coerce
class ElementNotFoundInList a s
class ElementIsInList a s
class ListIsNotPrefixOf a b
class ListIsPrefixOf a b
class ElementIsNotUniqInList a s
class IndexNotFoundInList a s
type family Length (a :: [k]) :: N where
Length '[] = 'Z
Length (a ': as) = 'S (Length as)
type family Drop (c :: N) (s :: [k]) :: [k] where
Drop 'Z s = s
Drop ('S c) '[] = '[]
Drop ('S c) (a ': as) = Drop c as
type family Take (c :: N) (s :: [k]) :: [k] where
Take 'Z s = '[]
Take ('S c) '[] = '[]
Take ('S c) (a ': as) = a ': (Take c as)
type family Delete (a :: k) (s :: [k]) :: [k] where
Delete a '[] = '[]
Delete a (a ': as) = Delete a as
Delete a (b ': as) = b ': (Delete a as)
type family Remove (i :: N) (a :: [k]) :: [k] where
Remove i '[] = '[]
Remove 'Z (a ': as) = as
Remove ('S i) (a ': as) = a ': (Remove i as)
type family (:++:) (a :: [k]) (b :: [k]) :: [k] where
'[] :++: b = b
(a ': as) :++: b = a ': (as :++: b)
appendId
:: forall proxy l r
. proxy l
-> (l ~ (l :++: '[]) => r)
-> r
appendId = unsafeCoerce id
type IndexOf a s = FromJust (IndexOfMay a s)
type IndexOfMay a s = IndexOfMay' Z a s
type family IndexOfMay' (acc :: N) (a :: k) (s :: [k]) :: Maybe N where
IndexOfMay' acc a '[] = 'Nothing
IndexOfMay' acc a (a ': as) = 'Just acc
IndexOfMay' acc a (b ': as) = IndexOfMay' (S acc) a as
type family IndicesOfMay (a :: [k]) (b :: [k]) :: [Maybe N] where
IndicesOfMay '[] bs = '[]
IndicesOfMay (a ': as) bs = (IndexOfMay a bs) ': (IndicesOfMay as bs)
type family IndicesOf (a :: [k]) (b :: [k]) :: [N] where
IndicesOf '[] bs = '[]
IndicesOf (a ': as) bs = (IndexOf a bs) ': (IndicesOf as bs)
type Index idx s = FromJust (IndexMay idx s)
type family IndexMay (idx :: N) (s :: [k]) :: Maybe k where
IndexMay idx '[] = 'Nothing
IndexMay Z (a ': as) = 'Just a
IndexMay (S idx) (a ': as) = IndexMay idx as
type family IndicesMay (idxs :: [N]) (a :: [k]) :: [Maybe k] where
IndicesMay '[] as = '[]
IndicesMay (i ': idxs) as = (IndexMay i as) ': (IndicesMay idxs as)
type family Indices (idxs :: [N]) (a :: [k]) :: [k] where
Indices '[] as = '[]
Indices (i ': idxs) as = (Index i as) ': (Indices idxs as)
type Elem a s = NothingToConstr (IndexOfMay a s)
(ElementNotFoundInList a s)
type NotElem a s = JustToConstr (IndexOfMay a s)
(ElementIsInList a s)
type family Count (a :: k) (s :: [k]) :: N where
Count a '[] = 'Z
Count a (a ': as) = 'S (Count a as)
Count a (b ': as) = Count a as
type family SubList (a :: [k]) (b :: [k]) :: Constraint where
SubList '[] bs = ()
SubList (a ': as) bs = (Elem a bs, SubList as bs)
subListId
:: forall proxy l r
. proxy l
-> (SubList l l => r) -> r
subListId _ = unsafeCoerce id
type family NotSubList (a :: [k]) (b :: [k]) :: Constraint where
NotSubList '[] bs = ()
NotSubList (a ': as) bs = (NotElem a bs, NotSubList as bs)
type IsPrefixOf a b = ( If (IsPrefixOfBool a b)
(() :: Constraint)
(ListIsNotPrefixOf a b)
, SubList a b )
type IsNotPrefixOf a b = If (IsPrefixOfBool a b)
(ListIsPrefixOf a b)
(() :: Constraint)
type family IsPrefixOfBool (a :: [k]) (b :: [k]) :: Bool where
IsPrefixOfBool '[] b = 'True
IsPrefixOfBool (a ': as) (a ': bs) = IsPrefixOfBool as bs
IsPrefixOfBool as bs = 'False
type family Union (a :: [k]) (b :: [k]) :: [k] where
Union '[] bs = bs
Union (a ': as) bs = Union as (AppendUniq a bs)
type family UnionList (l :: [[k]]) :: [k] where
UnionList '[] = '[]
UnionList (a ': as) = Union a (UnionList as)
type family AppendUniq (a :: k) (s :: [k]) :: [k] where
AppendUniq a (a ': bs) = a ': bs
AppendUniq a (b ': bs) = b ': (AppendUniq a bs)
AppendUniq a '[] = '[a]
type Intersect a b = (Indices (CatMaybes (IndicesOfMay a b)) b)
type family Substract (a :: [k]) (b :: [k]) :: [k] where
Substract '[] b = '[]
Substract a '[] = a
Substract a (b ': bs) = Substract (Delete b a) bs
type ElementIsUniq a s = If (Equal (S Z) (Count a s))
(() :: Constraint)
(ElementIsNotUniqInList a s)
type UniqElements a = UniqElements' a a
type family UniqElements' (a :: [k]) (self :: [k]) :: Constraint where
UniqElements' '[] self = ()
UniqElements' (a ': as) self = (ElementIsUniq a self, UniqElements' as self)