【发布时间】:2011-03-12 04:05:26
【问题描述】:
这段代码:
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的函数?
【问题讨论】:
-
(fromRational . toRational) :: (Fractional a, Real b) => a -> b怎么样? -
ony: 你的类型倒退了;
(fromRational . toRational) :: (Real a, Fractional b) => a -> b。这也称为realToFrac。