I work on lists of operating systems at the type level, and I wrote two functions at the type level, one to combine the two lists, and the other to intersect them. I have problems with the intersection function working correctly.
(ghc 7.10.3)
Here, the combine function works as expected:
*Main> (combineSupportedOS debian freeBSD) :: OSList '[OSDebian, OSFreeBSD]
OSList [OSDebian,OSFreeBSD]
Here's the intersection function, not quite working:
*Main> (intersectSupportedOS debian debian) :: OSList '[OSDebian]
Couldn't match expected type โIntersectOSList ['OSDebian] '['OSDebian]โ
with actual type โ'['OSDebian]โ
How can I convince a type check that it is well printed?
Full code:
{-
import Data.Typeable
import Data.String
import Data.Type.Bool
import Data.Type.Equality
data SupportedOS = OSDebian | OSFreeBSD
deriving (Show, Eq)
data OSList (os :: [SupportedOS]) = OSList [SupportedOS]
deriving (Show, Eq)
debian :: OSList '[OSDebian]
debian = typeOS OSDebian
freeBSD :: OSList '[OSFreeBSD]
freeBSD = typeOS OSFreeBSD
typeOS :: SupportedOS -> OSList os
typeOS o = OSList [o]
combineSupportedOS
:: (r ~ ConcatOSList l1 l2)
=> OSList l1
-> OSList l2
-> OSList r
combineSupportedOS (OSList l1) (OSList l2) = OSList (l1 ++ l2)
type family ConcatOSList (list1 :: [a]) (list2 :: [a]) :: [a]
type instance ConcatOSList '[] list2 = list2
type instance ConcatOSList (a ': rest) list2 = a ': ConcatOSList rest list2
intersectSupportedOS
:: (r ~ IntersectOSList l1 l2)
=> OSList l1
-> OSList l2
-> OSList r
intersectSupportedOS (OSList l1) (OSList l2) = OSList (filter (`elem` l2) l1)
type family IntersectOSList (list1 :: [a]) (list2 :: [a]) :: [a]
type instance IntersectOSList '[] list2 = list2
type instance IntersectOSList (a ': rest) list2 =
If (ElemOSList a list2)
(a ': IntersectOSList rest list2)
(IntersectOSList rest list2)
type family ElemOSList a (list :: [b]) :: Bool
type instance ElemOSList a '[] = False
type instance ElemOSList a (b ': bs) =
If (a == b)
True
(ElemOSList a bs)
type family EqOS (a :: SupportedOS) (b :: SupportedOS) where
EqOS a a = True
EqOS a b = False
type instance a == b = EqOS a b
source
share