【问题标题】:Haskell, No instance for (Show ([String, Int)] -> IntHaskell,没有 (Show ([String, Int)] -> Int 的实例
【发布时间】:2014-06-17 12:23:21
【问题描述】:

尝试在这里编译 Haskell.Happy 示例:http://www.haskell.org/happy/doc/html/sec-using.html#sec-other-datatypes,在生成 .hs 文件后,我得到了

No instance for (Show ([String, Int)] -> Int

错误,正在尝试编译它,而示例的第一部分已编译并运行良好

我做错了什么?

编辑:完整的错误消息

No instance for (Show ([(String, Int)] -> Int))
  arising from a use of `print'
Possible fix:
  add an instance declaration for (Show ([(String, Int)] -> Int))
In the first argument of `(.)', namely `print'
In the second argument of `(>>=)', namely `print . calc . lexer'
In the expression: getContents >>= print . calc . lexer

我在跑步

ghc example.hs

而这条线是

main = getContents >>= print . calc . lexer

可能重复的问题的答案是传递一个环境,但我怎么能在这里做呢?

【问题讨论】:

  • 我认为链接的答案是正确的,但这意味着 Happy 文档中存在错误。在ghci 中加载您的example.hs 并询问calc 是什么类型(:t calc)。我怀疑你会发现它需要一个不满足的 [(String, Int)] 参数。

标签: haskell happy


【解决方案1】:

calc 可能是 [Token] -> [(String,Int)] -> Int 类型,因此 main 应该是:

main = getContents >>= print . ($ []) . calc . lexer

注意:($ []) 中的列表是环境

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-05
    • 1970-01-01
    • 1970-01-01
    • 2014-11-20
    • 2011-02-16
    相关资源
    最近更新 更多