我打开了GHC/Num.lhs
文件,可以找到一些代码,如下所示:
instance Num Int where
I# x + I# y = I# (x +# y)
I# x - I# y = I# (x -# y)
negate (I# x) = I# (negateInt# x)
I# x * I# y = I# (x *# y)
abs n = if n `geInt` 0 then n else negate n
signum n | n `ltInt` 0 = negate 1
| n `eqInt` 0 = 0
| otherwise = 1
{-# INLINE fromInteger #-} -- Just to be sure!
fromInteger i = I# (integerToInt i)
它们是GHC实现的某种内在运算符吗?
它们是GHC原始操作,即未在Haskell中实现但由运行时提供的原始操作。
这些特定的在GHC.Prim
中声明并实现未装箱的Int
s:Int#
是未装箱Int
s的类型,I#
是Int
(即data Int = I# Int#
)的构造函数,+#
是Int#
加法。