创建 GHC.Prim.Any,而不是自由变量 [在多态函数中]

发布于 2024-12-29 22:31:27 字数 1449 浏览 3 评论 0原文

这是一个错误吗?

{-# LANGUAGE NoMonomorphismRestriction #-}
import qualified Text.Parsec.Token as P
import Text.Parsec.Language (haskellDef)
(P.TokenParser { P.identifier = ident }) = P.makeTokenParser haskellDef

产生 Text.Parsec.Prim.ParsecT String GHC.Prim.Any Data.Functor.Identity.Identity String 类型的 ident,而定义则

haskell = P.makeTokenParser haskellDef
ident = P.identifier haskell

产生 Text 类型之一.Parsec.Prim.ParsecT String u Data.Functor.Identity.Identity String

edit

ghci 中的行为不相同,

infixl 4 <++>
(<++>) = liftM2 (++)

(P.TokenParser { P.identifier = ident }) = P.makeTokenParser haskellDef
suitable_macro = ident

parseMacro = many space *> suitable_macro

parseMacro' =
    try (string "{{" *> parseMacro <* string "}}")

parseAll = many (noneOf "{") <++>
    option "" (parseMacro' <|> (string "{" <++> parseAll))

然后尝试运行它,

*Hz2.Preproc> parseTest parseAll "asdf{{b}}"

<interactive>:0:11:
    Couldn't match expected type `()' with actual type `GHC.Prim.Any'
    Expected type: Parsec String () a0
    Actual type: ParsecT
                    String GHC.Prim.Any Data.Functor.Identity.Identity [Char]
    In the first argument of `parseTest', namely `parseAll'
    In the expression: parseTest parseAll "asdf{{b}}"

Is this a bug?

{-# LANGUAGE NoMonomorphismRestriction #-}
import qualified Text.Parsec.Token as P
import Text.Parsec.Language (haskellDef)
(P.TokenParser { P.identifier = ident }) = P.makeTokenParser haskellDef

yields ident of type Text.Parsec.Prim.ParsecT String GHC.Prim.Any Data.Functor.Identity.Identity String, whereas defining

haskell = P.makeTokenParser haskellDef
ident = P.identifier haskell

yields one of type Text.Parsec.Prim.ParsecT String u Data.Functor.Identity.Identity String

edit

The behavior is not identical in ghci,

infixl 4 <++>
(<++>) = liftM2 (++)

(P.TokenParser { P.identifier = ident }) = P.makeTokenParser haskellDef
suitable_macro = ident

parseMacro = many space *> suitable_macro

parseMacro' =
    try (string "{{" *> parseMacro <* string "}}")

parseAll = many (noneOf "{") <++>
    option "" (parseMacro' <|> (string "{" <++> parseAll))

Then, try to run it,

*Hz2.Preproc> parseTest parseAll "asdf{{b}}"

<interactive>:0:11:
    Couldn't match expected type `()' with actual type `GHC.Prim.Any'
    Expected type: Parsec String () a0
    Actual type: ParsecT
                    String GHC.Prim.Any Data.Functor.Identity.Identity [Char]
    In the first argument of `parseTest', namely `parseAll'
    In the expression: parseTest parseAll "asdf{{b}}"

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

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

发布评论

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

评论(1

尴尬癌患者 2025-01-05 22:31:27

并不真地;我相信这是符合报告的行为:模式中的完全多态类型变量被实例化为Any。然而,从 GHC 7.2 开始,这个的工作原理就像你所期望的(特别参见最后的提交消息)。

至于 GHCi 行为,这是因为 GHCi 的 扩展默认规则将完全多态变量默认为()

Not really; I believe it's Report-compliant behaviour: fully polymorphic type variables in patterns are instantiated to Any. However, in GHC 7.2 onwards, this works like you'd expect (see especially the commit message at the end).

As for the GHCi behaviour, this is because GHCi's extended defaulting rules default fully-polymorphic variables to ().

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