考虑下面的程序:摆脱的限制型家庭GADTs当“非详尽的彭定康匹配”警告
{-# LANGUAGE DataKinds, GADTs #-}
{-# LANGUAGE TypeFamilies #-}
data Foo = A | B
type family IsA (foo :: Foo) :: Bool
type instance IsA A = True
type instance IsA B = False
data Bar (foo :: Foo) where
BarA :: (IsA foo ~ True) => Int -> Bar foo
BarB :: (IsA foo ~ False) => String -> Bar foo
f :: Bar A -> Int
f bar = case bar of
BarA x -> x
使用-fwarn-incomplete-patterns
为定义的总功能f
当我从GHC 7.4.2此警告以上:
Warning: Pattern match(es) are non-exhaustive
In a case alternative: Patterns not matched: BarB _
当然,这没有任何意义,甚至尝试添加匹配BarB
:
Couldn't match type `'False' with `'True'
Inaccessible code in
a pattern with constructor
BarB :: forall (foo :: Foo). IsA foo ~ 'False => String -> Bar foo,
in a case alternative
In the pattern: BarB _
In a case alternative: BarB _ -> undefined
In the expression:
case bar of {
BarA x -> x
BarB _ -> undefined }
有没有办法让GHC相信f
是完全的?另外,这是GHC的一个bug,还是只是一个已知的限制;或者实际上有一个非常好的理由,为什么没有办法看到f
中的模式匹配是完整的?
不幸的是,我不能这么说,因为我的真实用例通过让'data Foo = A | B | C'和两个构造函数'BarA'和'BarBC'。 – Cactus
@Cactus'data BorC a where {IsB :: BorC B; IsC :: BorC C};数据Bar foo where BarBC :: BorC foo - > String - > Bar foo' –