Possible Duplicate:
Return specific type within HaskellHaskell刚性类型?
此代码:
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
?
你说得对。 DUP。 – qrest 2010-07-06 06:52:06
'(fromRational。toRational)::(小数a,实数b)=> a - > b'? – ony 2010-07-06 09:08:22
ony:你的类型是向后的; (fromRational。toRational)::(Real a,Fractional b)=> a - > b'。这也被称为'realToFrac'。 – BMeph 2010-07-06 20:56:04