``NS''类型的案例匹配

发布于 2025-02-10 09:49:33 字数 582 浏览 1 评论 0原文

sop-core键入型号,创建一个有趣的难题。在s :: ns f xs - > ns f(x':xs)其中xs〜'[]是指s的参数是居住的。然而,哈斯克尔的案件匹配迫使我对此进行匹配。这个问题有解决方案吗?

type T = NS I '[Int, String]

matchT :: T -> String
matchT = \case
  Z (I n) -> show n
  S (Z (I s)) -> s
  S (S _) -> error "FIXME" -- not reachable

如果您删除了最后一个情况(实际上不必必要),Haskell抱怨“模式匹配(ES)是无节制的”。

The NS type from sop-core creates an interesting conundrum. In S :: NS f xs -> NS f (x ': xs) where xs ~ '[] means that the argument to the S is inhabited. Yet Haskell's case matching forces me to match on it. Is there a resolution to this problem?

type T = NS I '[Int, String]

matchT :: T -> String
matchT = \case
  Z (I n) -> show n
  S (Z (I s)) -> s
  S (S _) -> error "FIXME" -- not reachable

If you remove that last case (which really shouldn't be necessary), Haskell complains "Pattern match(es) are non-exhaustive".

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

何必那么矫情 2025-02-17 09:49:33

使用 emptroy> emptroy> emptycoper

matchT :: T -> String
matchT = \case
  Z (I n) -> show n
  S (Z (I s)) -> s
  S (S impossible) -> case impossible of {}

Use EmptyCase!

matchT :: T -> String
matchT = \case
  Z (I n) -> show n
  S (Z (I s)) -> s
  S (S impossible) -> case impossible of {}

Real-world example

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