我尝试使用generics-sop中的All来约束类型列表。一切都像All Typeable xs
之类的简单类一样有效,但我希望能够做到以下几点:
class (Typeable a) => TestClass (a :: k)
instance (Typeable a) => TestClass a
foo :: (All Typeable xs) => NP f xs -> z
foo = undefined
bar :: (All TestClass xs) => NP f xs -> z
bar = foo
这给出了错误
Could not deduce: Generics.SOP.Constraint.AllF Typeable xs
arising from a use of ‘foo’
from the context: All TestClass xs
generics-sop文档指出
"所有Eq' [Int,Bool,Char] 相当于约束 (Eq Int,Eq Bool,Eq Char)
但在这种情况下,它似乎不是,因为
foo2 :: (Typeable a, Typeable b) => NP f '[a,b] -> z
foo2 = undefined
bar2 :: (TestClass a, TestClass b) => NP f '[a,b] -> z
bar2 = foo2
编译好。
我的问题
1)这是预期的行为吗? 2)如果是,是否有解决方法?
我的用例是,我希望在单个类名称(如class (Typeable a, Eq a, Show a) => MyClass a
)下传递由一堆不同类约束的类型级别列表,但也可以调用不太专业的函数只需要这些类的某些子集。
正在搜索superclasses aren't considered的答案,但我认为这不是问题所在 - 我认为这与All
约束在{generics-sop
约束的方式有关{1}}。就好像编译器只是比较两个All
约束,而不是将它们扩展,然后进行类型检查。
答案 0 :(得分:4)
All f xs
实际上相当于(AllF f xs, SListI xs)
。 AllF
是一个类型系列:
type family AllF (c :: k -> Constraint) (xs :: [k]) :: Constraint where
AllF _ '[] = ()
AllF c (x:xs) = (c x, All c xs)
你知道除非xs
在WHNF中,否则它无法减少,所以它会陷入你的情况。您可以使用mapAll
:
import Generics.SOP.Dict
mapAll :: forall c d xs.
(forall a. Dict c a -> Dict d a) ->
Dict (All c) xs -> Dict (All d) xs
-- ::ish forall f g xs. (forall a. f a -> g a) -> All f xs -> All g xs
-- stores a constraint in a manipulatable way
data Dict (f :: k -> Constraint) (a :: k) where
Dict :: f a => Dict f a
bar :: forall xs f z. (All TestClass xs) => NP f xs -> z
bar = case mapAll @TestClass @Typeable @xs (\Dict -> Dict) Dict of
Dict -> foo
-- TestClass a -> Typeable a pretty trivially:
-- match Dict to reveal TestClass a
-- put the Typeable part of the TestClass instance into another Dict
-- We already know All TestClass xs; place that into a Dict
-- mapAll magic makes a Dict (All Typeable) xs
-- match on it to reveal
-- foo's constraint is satisfied