我得到了错误
Ambiguous type variable `p0' in the constraints:
(Show p0) arising from a use of `print' at cwqr_0003.hs:31:6-10
(Ord p0) arising from a use of `PSQ.lookup'来自下面的代码。
我不知道如何分析这件事。这会是GHC中的问题还是某个模块中的问题?如果我尝试用putStr代替print,那么我会得到一个错误,它的预期类型是一个字符串,而不是p0。当我尝试fromMaybe时,它给我一个与我发送给fromMaybe的默认值literal zero相关的错误
import qualified Data.PSQueue as PSQ
import Data.Maybe
import Data.Label
import Control.Category
import Prelude hiding ((.))
--some tested code omitted here
let r = PSQ.lookup test' q
--putStr (show (r :: String))
print (r) 发布于 2011-12-31 02:03:32
错误消息实际上就是它所说的意思:您有一个不明确的类型。这是怎么发生的呢?通常,因为您有一些东西可以生成多态结果,所以可以对该结果应用一个带有多态参数的函数,这样中间值的类型就是未知的。
在简单的多态类型中,多义性并不重要:如果您生成某个内容的列表,然后获取长度,我们不需要知道列表元素的类型。
但是,如果歧义涉及到使用Show这样的类型类-- print就是这样做的--GHC就会卡住,因为它无法知道应该选择哪个实例。
有时,这也可能是因为强制特定的定义是单态的(除非您特别说明),这强制选择单个类型,而不是保留多态。我怀疑这可能是您的问题,但如果没有您删除的上下文,我就无法判断。
为了说明后者,请使用以下定义:
foo = print...with无类型签名,会导致如下错误:
Test.hs:12:7:
Ambiguous type variable `a0' in the constraint:
(Show a0) arising from a use of `print'
Possible cause: the monomorphism restriction applied to the following:
foo :: a0 -> IO () (bound at Test.hs:12:1)
Probable fix: give these definition(s) an explicit type signature
or use -XNoMonomorphismRestriction
In the expression: print
In an equation for `foo': foo = printhttps://stackoverflow.com/questions/8682364
复制相似问题