为什么这个表达式中的一元减号运算符有问题:(- 2) 1?

Why is the unary minus operator problematic in this expression: (- 2) 1?

以下所有表达式都能顺利求值:

(+2) 1 -- 3
(*2) 1 -- 2
((-)2) 1 -- 1
(2-) 1 -- 1   
(/2) 1 -- 0.5
(2/) 1 -- 2.0

但不是这个:

(-2) 1 -- the inferred type is ambiguous

GHC 抛出一些关于推断类型不明确的错误。为什么?

前六个带括号的表达式是 部分,即接受一个参数并“将其放在中缀运算符缺失的一侧”的函数(参见 haskell.org wiki ).相反,(-2)不是函数,而是数字(负2):

λ> :t (-2)
(-2) :: Num a => a

如果你写

λ> (-2) 1

看起来您正试图将 (-2)(一个数字)应用到 1(这是不可能的),GHCi 理所当然地抱怨:

Could not deduce (Num (a0 -> t))
  arising from the ambiguity check for ‘it’
from the context (Num (a -> t), Num a)
  bound by the inferred type for ‘it’: (Num (a -> t), Num a) => t
  at <interactive>:3:1-6
The type variable ‘a0’ is ambiguous
When checking that ‘it’
  has the inferred type ‘forall a t. (Num (a -> t), Num a) => t’
Probable cause: the inferred type is ambiguous

如果你想要一个从另一个数字中减去 2 的函数,你可以使用

(subtract 2)

比较它的类型,

λ> :t (subtract 2)
(subtract 2) :: Num a => a -> a

(-2)(见上文)。


术语附录(OP 编辑​​后)

将减号运算符括起来将其变成一个带有两个参数的普通(前缀)函数;因此 ((-) 2) 不是一个部分,而是一个部分应用的函数。