幻影类型使图案匹配无可辩驳,但似乎在内部不起作用。
请查看代码。我相信使用Phantom类型使模式匹配不可撤销,因此在MonadFail
实例中无需。
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE GADTs #-}
{-# OPTIONS_GHC -Wall #-}
{-# OPTIONS_GHC -Wincomplete-uni-patterns #-}
data Type = I | S
data T t where
TI :: Int -> T 'I
TS :: String -> T 'S
ti :: T 'I
ti = TI 42
test :: Monad m => m Int
test = do
(TI v) <- return ti
return v
但是我遇到了这个错误:
• Could not deduce (MonadFail m)
arising from a do statement
with the failable pattern ‘(TI v)’
这种方法怎么了?
我用GHC 9.0.2和8.10.4进行了检查。
顺便说一句,即使在存在-wincomplete-uni-patterns
选项的情况下,在中匹配它也不会产生任何警告。
Please look at the code. I believe using phantom type makes the pattern matching irrefutable so there is no need in MonadFail
instance.
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE GADTs #-}
{-# OPTIONS_GHC -Wall #-}
{-# OPTIONS_GHC -Wincomplete-uni-patterns #-}
data Type = I | S
data T t where
TI :: Int -> T 'I
TS :: String -> T 'S
ti :: T 'I
ti = TI 42
test :: Monad m => m Int
test = do
(TI v) <- return ti
return v
But I am getting this error:
• Could not deduce (MonadFail m)
arising from a do statement
with the failable pattern ‘(TI v)’
What's wrong with this approach?
I checked this with ghc 9.0.2 and 8.10.4.
By the way, matching it in let
does not produce any warning even in the presence of -Wincomplete-uni-patterns
option.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的方法没有错;它应该按照您的期望工作。在GHC中,它目前尚未: nofollow noreferrer“考虑到类型的信息(#25085)
在修复它之前,您可以通过编写
〜(ti v)&lt; - 返回ti
而不是(ti V) )&lt; - 返回ti
。There's nothing wrong with your approach; it should work as you expect it to. It's a known bug in GHC that it currently doesn't: MonadFail exhaustivity checking does not take into account type information (#25085)
Until they fix it, you can work around it by writing
~(TI v) <- return ti
instead of(TI v) <- return ti
.