Haskell 为什么使用Double而不是给我一个模糊错误?

Haskell 为什么使用Double而不是给我一个模糊错误?,haskell,Haskell,对于代码 class Boomable a where boom :: a instance Boomable Int where boom = 100 instance Boomable Double where boom = 1.2 为什么 boom + 1 给我2.2 为什么它使用Double版本,而不是像我预期的那样给我一个模糊错误 我希望必须在boom或1上执行::Int或:Double,才能工作。您可以使用ghci-Wall启用警告: $ ghci -Wall

对于代码

class Boomable a where
  boom :: a

instance Boomable Int where
  boom = 100

instance Boomable Double where
  boom = 1.2
为什么

boom + 1
给我
2.2

为什么它使用
Double
版本,而不是像我预期的那样给我一个模糊错误


我希望必须在
boom
1
上执行
::Int
:Double
,才能工作。

您可以使用
ghci-Wall
启用警告:

$ ghci -Wall

Prelude> :set +m
Prelude> class Boomable a where
Prelude|   boom :: a
Prelude| 
Prelude> instance Boomable Int where
Prelude|   boom = 100
Prelude| 
Prelude> instance Boomable Double where
Prelude|   boom = 1.2
Prelude| 
Prelude> boom + 1

<interactive>:12:6: Warning:
    Defaulting the following constraint(s) to type `Double'
      (Num a0) arising from a use of `+' at <interactive>:12:6
      (Boomable a0) arising from a use of `boom' at <interactive>:12:1-4
    In the expression: boom + 1
    In an equation for `it': it = boom + 1

<interactive>:12:6: Warning:
    Defaulting the following constraint(s) to type `Double'
      (Num a0) arising from a use of `+' at <interactive>:12:6
      (Show a0) arising from a use of `print' at <interactive>:12:1-8
      (Boomable a0) arising from a use of `boom' at <interactive>:12:1-4
    In the expression: boom + 1
    In an equation for `it': it = boom + 1
2.2
$ghci-墙
前奏曲>:set+m
前奏曲>一节课
前奏曲|繁荣::a
前奏曲
前奏曲>实例Boomable Int,其中
前奏曲|吊杆=100
前奏曲
前奏曲>实例Boomable Double,其中
前奏曲|吊杆=1.2
前奏曲
前奏曲>动臂+1
:12:6:警告:
将以下约束默认为键入'Double'
(Num a0)因在12:6使用“+”而产生
(boom ABLE a0)因在12:1-4使用“boom”而产生
在表达式中:动臂+1
在‘it’的方程式中:it=boom+1
:12:6:警告:
将以下约束默认为键入'Double'
(Num a0)因在12:6使用“+”而产生
(显示a0)因在12:1-8使用“打印”而产生
(boom ABLE a0)因在12:1-4使用“boom”而产生
在表达式中:动臂+1
在‘it’的方程式中:it=boom+1
2.2

您是如何测试它的?在GHCi中?GHCi不输入默认值。可能会有一些标志来禁用它,但我不知道。好吧,那就解释它了?你说得对,如果我在GHCi中执行
foo=boom+1
,它将使用
Double
,但是如果我在
.hs
文件中执行相同的操作,它会给我一个模糊错误。