为什么 GHC 抱怨类型错误?

发布于 2024-09-15 20:06:35 字数 1074 浏览 1 评论 0原文

这个小函数检查(有限)Brainfuck 字符串的有效性。它检查 [] 是否平衡。代码非常简单,并且编写为尾递归:

-- checks Brainfuck for validity.
validateBrainfuck :: Monad m => String -> m String
validateBrainfuck s = maybe (return s) (fail . fromJust) (validate s 0) where
  validate :: String -> Int -> Maybe String -- Here inversed: String means error
  validate (']':_ ) 0 = Just "Too many closing brackets"
  validate (']':xs) c = validate xs (pred c)
  validate ('[':xs) c = validate xs (succ c)
  validate ( x :xs) c = validate xs       c
  validate []       0 = Nothing
  validate []       _ = Just "Too many opening brackets"

现在,GHC 抱怨打字问题:

Brainfuck.hs:62:58:
    Couldn't match expected type `Maybe String'
           against inferred type `[Char]'
      Expected type: Maybe (Maybe String)
      Inferred type: Maybe String
    In the third argument of `maybe', namely `(validate s 0)'
    In the expression:
        maybe (return s) (fail . fromJust) (validate s 0)

也许我太傻了,无法弄清楚出了什么问题,但这对我来说看起来很奇怪。

This little function checks a (finite) Brainfuck string for validity. It check's whether the [ and ] are balanced. The code is very straightforward and written to be tail-recursive:

-- checks Brainfuck for validity.
validateBrainfuck :: Monad m => String -> m String
validateBrainfuck s = maybe (return s) (fail . fromJust) (validate s 0) where
  validate :: String -> Int -> Maybe String -- Here inversed: String means error
  validate (']':_ ) 0 = Just "Too many closing brackets"
  validate (']':xs) c = validate xs (pred c)
  validate ('[':xs) c = validate xs (succ c)
  validate ( x :xs) c = validate xs       c
  validate []       0 = Nothing
  validate []       _ = Just "Too many opening brackets"

Now, GHC complains about typing issues:

Brainfuck.hs:62:58:
    Couldn't match expected type `Maybe String'
           against inferred type `[Char]'
      Expected type: Maybe (Maybe String)
      Inferred type: Maybe String
    In the third argument of `maybe', namely `(validate s 0)'
    In the expression:
        maybe (return s) (fail . fromJust) (validate s 0)

Maybe I'm just too silly to figure out what went wrong, but this looks very weird for me.

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

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

发布评论

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

评论(1

一影成城 2024-09-22 20:06:35

查看 maybe 的类型并思考它应该做什么:

maybe :: b -> (a -> b) -> Maybe a -> b

如果 Maybe 值不包含结果(即 Nothing),maybe 返回 <代码>b 参数。

否则 - 当给出 Just a 时 - 它将给定的函数应用于有效结果。我们在这里不需要任何 fromJust 提取。

你的代码就变成了

maybe (return s) fail (validate s 0)

Look at the type of maybe and think what it should do:

maybe :: b -> (a -> b) -> Maybe a -> b

If the maybe value contains no result (i.e. Nothing), maybe returns the b argument.

Otherwise - when Just a is given - it applies the given function to the valid result. We don't need any fromJust extraction here.

Your code just becomes

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