I have this heterogenous list which has its type reflect the types of the values it contains. I can convert all of the elements to strings by checking that every type contained satisfies a constraint:
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
import GHC.Exts
import GHC.TypeLits
import Data.Proxy
type family AllSatisfy (f :: k -> Constraint) (xs :: [k]) :: Constraint where
AllSatisfy f '[] = ()
AllSatisfy f (x ': xs) = (f x, AllSatisfy f xs)
data HList (as :: [*]) where
HNil :: HList '[]
HCons :: a -> HList as -> HList (a ': as)
type family Keys (xs :: [(a, b)]) :: [a] where
Keys '[] = '[]
Keys ( '(a, b) ': xs) = a ': Keys xs
type family Values (xs :: [(a, b)]) :: [b] where
Values '[] = '[]
Values ( '(a, b) ': xs) = b ': Values xs
showHList :: AllSatisfy Show xs => HList xs -> [String]
showHList HNil = []
showHList (HCons x xs) = show x : showHList xs
What I'd like to be able to do is specify some extra information via a type level association list, which is indexed by the types in the HList. Something like:
showWithKey :: forall (keyMap :: [(*, Symbol)]) (b :: Symbol) (rest :: [(*, Symbol)]).
(AllSatisfy Show (Keys keyMap)
,AllSatisfy KnownSymbol (Values keyMap)
) =>
Proxy keyMap -> HList (Keys keyMap) -> [(String, String)]
showWithKey _ HNil = []
showWithKey _ (HCons (x :: a) (xs :: HList as)) =
let p = (Proxy @keyMap) :: Proxy ( '(a, b) ': rest )
in (show x, symbolVal (Proxy @b)) : (showWithKey (Proxy @rest) xs)
Now, it's clear to see that if (Keys keyMap) is non-empty, then keyMap, but GHC struggles to figured this out:
Could not deduce: keyMap ~ ('(a, b) : rest)
from the context: (AllSatisfy Show (Keys keyMap),
AllSatisfy KnownSymbol (Values keyMap))
bound by the type signature for:
showWithKey :: (AllSatisfy Show (Keys keyMap),
AllSatisfy KnownSymbol (Values keyMap)) =>
Proxy keyMap -> HList (Keys keyMap) -> [(String, String)]
or from: Keys keyMap ~ (a : as)
bound by a pattern with constructor:
HCons :: forall a (as :: [ghc-prim-0.5.0.0:GHC.Types.*]).
a -> HList as -> HList (a : as),
in an equation for ‘showWithKey’
‘keyMap’ is a rigid type variable bound by
the type signature for:
showWithKey :: forall (keyMap :: [(ghc-prim-0.5.0.0:GHC.Types.*,
Symbol)]) (b :: Symbol) (rest :: [(ghc-prim-0.5.0.0:GHC.Types.*,
Symbol)]).
(AllSatisfy Show (Keys keyMap),
AllSatisfy KnownSymbol (Values keyMap)) =>
Proxy keyMap -> HList (Keys keyMap) -> [(String, String)]
Expected type: Proxy ('(a, b) : rest)
Actual type: Proxy keyMap
How can I rewrite this so that GHC can figure things out?
Taking some cues what user2407038 said, I created a concrete representation of the depMap type and then created a type class to describe a not-quite-singleton-but-at-least-canonical value from the type.
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE AllowAmbiguousTypes #-}
import GHC.Exts
import GHC.TypeLits
import Data.Proxy
type family AllSatisfy (f :: k -> Constraint) (xs :: [k]) :: Constraint where
AllSatisfy f '[] = ()
AllSatisfy f (x ': xs) = (f x, AllSatisfy f xs)
data HList (as :: [*]) where
HNil :: HList '[]
HCons :: a -> HList as -> HList (a ': as)
type family Keys (xs :: [(a, b)]) :: [a] where
Keys '[] = '[]
Keys ( '(a, b) ': xs) = a ': Keys xs
type family Values (xs :: [(a, b)]) :: [b] where
Values '[] = '[]
Values ( '(a, b) ': xs) = b ': Values xs
showHList :: AllSatisfy Show xs => HList xs -> [String]
showHList HNil = []
showHList (HCons x xs) = show x : showHList xs
data SKeyMap :: [(*, Symbol)] -> * where
SKeyNil :: SKeyMap '[]
SKeyCons :: Proxy a -> Proxy s -> SKeyMap xs -> SKeyMap ( '(a, s) ': xs )
class KnownKeyMap (keyMap :: [(*, Symbol)]) where
sKeyMap :: SKeyMap keyMap
instance KnownKeyMap '[] where
sKeyMap = SKeyNil
instance KnownKeyMap keyMap => KnownKeyMap ( '(a, s) ': keyMap ) where
sKeyMap = SKeyCons Proxy Proxy sKeyMap
showWithKey' :: forall (keyMap :: [(*, Symbol)]) .
(AllSatisfy Show (Keys keyMap)
,AllSatisfy KnownSymbol (Values keyMap)
) =>
SKeyMap keyMap -> HList (Keys keyMap) -> [(String, String)]
showWithKey' SKeyNil HNil = []
showWithKey' (SKeyCons _ sp skRest) (HCons (x :: a) (xs :: HList as)) =
(show x, symbolVal sp) : (showWithKey' skRest xs)
showWithKey :: forall (keyMap :: [(*, Symbol)]) .
(KnownKeyMap keyMap
,AllSatisfy Show (Keys keyMap)
,AllSatisfy KnownSymbol (Values keyMap)
) =>
HList (Keys keyMap) -> [(String, String)]
showWithKey = showWithKey' (sKeyMap @keyMap)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With