这个问题已经在这里有了答案:




已关闭10年。






这段代码:

class Alpha a where
    half :: Real f => a -> f

instance Alpha Double where
    half a = a/2.0

产生此错误:
rigid.hs:6:13:
    Couldn't match expected type `f' against inferred type `Double'
      `f' is a rigid type variable bound by
          the type signature for `half' at rigid.hs:3:17
    In the expression: a / 2.0
    In the definition of `half': half a = a / 2.0
    In the instance declaration for `Alpha Double'
Failed, modules loaded: none.

为什么?

编辑:确定刚性类型的业务,我认为我已经提出了一个更集中的问题。如何将Double转换为满足Ord和Num的值?就是说,一个使我成为(Ord a, Num a) => Double -> a的函数?

最佳答案

您声称half可以将a类型转换为任何Real类型。但是您的half只能将a(Double)转换为Double

10-08 02:16