Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Haskell中使用读取和显示时(缺少)不明确的类型_Haskell_Types_Functional Programming_Ambiguous Call - Fatal编程技术网

在Haskell中使用读取和显示时(缺少)不明确的类型

在Haskell中使用读取和显示时(缺少)不明确的类型,haskell,types,functional-programming,ambiguous-call,Haskell,Types,Functional Programming,Ambiguous Call,我写了一个非常简单的Haskell程序: main = print $ sum $ map read ["55", "99", "101"] 根据我过去的经验,我希望得到一个“不明确类型”错误,因为sum$map read[…]的签名是(read a,Num a)=>aNum是一个类,因此它本身无法实现Show类。但是,程序正确地输出了“255”。print如何确定生成输出的方法?(show也能够无误地生成正确的结果。)如果使用-fwarn type defaults选项,您将得到以下结果:

我写了一个非常简单的Haskell程序:

main = print $ sum $ map read ["55", "99", "101"]

根据我过去的经验,我希望得到一个“不明确类型”错误,因为
sum$map read[…]
的签名是
(read a,Num a)=>a
Num
是一个类,因此它本身无法实现
Show
类。但是,程序正确地输出了“255”。
print
如何确定生成输出的方法?(
show
也能够无误地生成正确的结果。)

如果使用
-fwarn type defaults
选项,您将得到以下结果:

$ ghc -O2 -fwarn-type-defaults ddd.hs
[1 of 1] Compiling Main             ( ddd.hs, ddd.o )

ddd.hs:2:8: Warning:
    Defaulting the following constraint(s) to type ‘Integer’
      (Show s0) arising from a use of ‘print’ at ddd.hs:2:8-12
      (Read s0) arising from a use of ‘read’ at ddd.hs:2:26-29
      (Num s0) arising from a use of ‘sum’ at ddd.hs:2:16-18
    In the expression: print
    In the expression: print $ sum $ map read ["55", "99", "101"]
    In an equation for ‘main’:
        main = print $ sum $ map read ["55", "99", "101"]
这就解释了发生了什么