1 {-# LANGUAGE PolyKinds #-}
2 {-# LANGUAGE UndecidableInstances #-}
3 -- | List utilities at the type-level.
4 module Language.Symantic.Helper.Data.Type.List where
6 import GHC.Exts (Constraint)
8 import Data.Bool (Bool(..))
9 import Language.Symantic.Helper.Data.Type.Peano
12 -- | Return the position of a type within a list of them.
13 -- This is useful to work around @OverlappingInstances@.
14 type family Index xs x where
15 Index (x ': xs) x = Zero
16 Index (not_x ': xs) x = Succ (Index xs x)
18 -- * Type family @(++)@
19 type family (++) xs ys where
22 (x ': xs) ++ ys = x ': xs ++ ys
25 -- * Type family 'Concat'
26 type family Concat (xs::[[k]]) :: [k] where
28 Concat (x ': xs) = x ++ Concat xs
30 -- * Type family 'Concat_Constraints'
31 type family Concat_Constraints (cs::[Constraint]) :: Constraint where
32 Concat_Constraints '[] = ()
33 Concat_Constraints (c ': cs) = (c, Concat_Constraints cs)
35 -- * Type family 'DeleteAll'
36 type family DeleteAll (x::k) (xs::[k]) :: [k] where
38 DeleteAll x (x ': xs) = DeleteAll x xs
39 DeleteAll x (y ': xs) = y ': DeleteAll x xs
41 -- * Type family 'Head'
42 type family Head (xs::[k]) :: k where
45 -- * Type family 'Tail'
46 type family Tail (xs::[k]) :: [k] where
49 -- * Type family 'Map'
50 type family Map (f::a -> b) (cs::[a]) :: [b] where
52 Map f (c ': cs) = f c ': Map f cs
54 -- * Type family 'Nub'
55 type family Nub (xs::[k]) :: [k] where
57 Nub (x ': xs) = x ': Nub (DeleteAll x xs)
59 -- * Type family 'Exists'
60 type family Exists (x::k) (xs::[k]) :: Bool where
62 Exists x (x ': xs) = 'True
63 Exists x (y ': xs) = Exists x xs
66 type family If (b::Bool) (ok::k) (ko::k) :: k where