Haskell 在指定参数类型时抛出错误

Haskell throws an error while specifying the type of a parameter

我刚开始学习Haskell。我正在尝试实现一个函数,该函数以数字作为输入,并且 return -1、0 或 1 取决于它的值。输入可以是任何数字(整数或浮点数)。这是代码:

signum :: (Num a) => a -> Int
signum x
    | x > 0 = 1
    | x < 0 = -1
    | otherwise = 0

但是当我尝试将其加载到 ghci 时,它显示以下错误:

[1 of 1] Compiling Main             ( run_it.hs, interpreted )

run_it.hs:7:13:
Could not deduce (Ord a) arising from a use of `>'
from the context (Num a)
  bound by the type signature for Main.signum :: Num a => a -> Int
  at run_it.hs:5:11-29
Possible fix:
  add (Ord a) to the context of
    the type signature for Main.signum :: Num a => a -> Int
In the expression: x > 0
In a stmt of a pattern guard for
               an equation for `signum':
  x > 0
In an equation for `signum':
    signum x
      | x > 0 = 1
      | x < 0 = - 1
      | otherwise = 0
Failed, modules loaded: none.

这个错误是什么意思?

方法>定义在Ordclass中,因此您需要在类型签名中添加额外的约束:

signum :: (Ord a, Num a) => a -> Int

在GHCi中,可以查看class和:i <Class>的方法,例如:

*Main> :i Ord
class Eq a => Ord a where
  compare :: a -> a -> Ordering
  (<) :: a -> a -> Bool
  (<=) :: a -> a -> Bool
  (>) :: a -> a -> Bool
  (>=) :: a -> a -> Bool
  [...]

或者,您可以检查方法本身:

*Main> :i (>)
class Eq a => Ord a where
  ...
  (>) :: a -> a -> Bool
  ...
    -- Defined in ‘GHC.Classes’
infix 4 >

原来的signum之所以没有这个约束是因为它没有使用Ord中的方法。相反,它使用专门针对所讨论类型的函数(如 ltInt)或直接进行模式匹配(参见 Word 实例):https://hackage.haskell.org/package/base-4.8.0.0/docs/src/GHC-Num.html#signum