幻影类型使图案匹配无可辩驳,但似乎在内部不起作用。

发布于 2025-02-11 18:38:56 字数 711 浏览 1 评论 0原文

请查看代码。我相信使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倾城泪 2025-02-18 18:38:56

您的方法没有错;它应该按照您的期望工作。在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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文