I want to develop a safe type search function for the following data type:
data Attr (xs :: [(Symbol,*)]) where Nil :: Attr '[] (:*) :: KnownSymbol s => (Proxy s, t) -> Attr xs -> Attr ('(s , t) ': xs)
An obvious search function would look like this:
lookupAttr :: (KnownSymbol s, Lookup s env ~ 'Just t) => Proxy s -> Attr env -> t lookupAttr s ((s',t) :* env') = case sameSymbol ss' of Just Refl -> t Nothing -> lookupAttr s env'
where the Lookup type family is defined in the single library. This definition cannot enter the GHC 7.10.3 check with the following error message:
Could not deduce (Lookup s xs ~ 'Just t) from the context (KnownSymbol s, Lookup s env ~ 'Just t) bound by the type signature for lookupAttr :: (KnownSymbol s, Lookup s env ~ 'Just t) => Proxy s -> Attr env -> t
This message is generated to call lookupAttr s env' recursively. This is reasonable since we have that if
Lookup s ('(s',t') ': env) ~ 'Just t
and
s :~: s'
not provable then
Lookup s env ~ 'Just t
. My question is: how can I convince a Haskell type check that this is really so?
haskell type-level-computation dependent-type
Rodrigo Ribeiro
source share