平等约束 [英] Equality on constraints
问题描述
{ - #LANGUAGE PolymorphicKinds,ConstraintKinds,TypeFamilies# - }
(以及更多,如果需要的话),(〜) 类型级别的操作符处理种类约束
的类型表达式?我试着用google搜索答案,但没有运气。 是的,这是可能的。因为类型 Constraint
是原子类型约束的有限集合,所以您可以非常容易地测试它们的等式。
但是, PolyKinds
扩展名不是必需的。另外,这种类型的平等实际上很有用的情况很少,因为我没有看到作为参数传递多态约束的实际方法 c1
, c2
到 Bla
,所以约束相等在每种情况下都是重言式( Show〜Show $
{ - #LANGUAGE ConstraintKinds,TypeFamilies# - }
type Bla c1 c2 a =(c1 a,c2 a,c1 c2)
foo :: Bla Show Show a => a - > IO()
foo = print
main = fooBla
Basically, given {-# LANGUAGE PolymorphicKinds, ConstraintKinds, TypeFamilies #-}
(and more, if necessary), does the (~)
type-level operator work on type-level expressions of kind Constraint
? I tried googling the answer, but had no luck.
解决方案 Yes, it is possible. Because types of kind Constraint
are finite sets of atomic type constraints, you can test their equality very easily.
The PolyKinds
extension is not necessary, however. Also, there's very few situations when this kind equality would actually be useful, because I don't see a practical way of passing polymorphic constraints as the arguments c1
, c2
to Bla
, so the constraint equality would be a tautology in every case (Show ~ Show
here):
{-# LANGUAGE ConstraintKinds, TypeFamilies #-}
type Bla c1 c2 a = (c1 a, c2 a, c1 ~ c2)
foo :: Bla Show Show a => a -> IO ()
foo = print
main = foo "Bla"
这篇关于平等约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!