创建 GHC.Prim.Any,而不是自由变量 [在多态函数中]
这是一个错误吗?
{-# 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
并不真地;我相信这是符合报告的行为:模式中的完全多态类型变量被实例化为
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
()
.