我有一个简单的类型类,用于表示一个实例的连续概率分布。我想尽可能地写出类型类和公式,以便例如可以在Float或Double之间进行适当选择。
无法编译:
class ContinuousDistribution d where
ppf :: Floating a => d -> a -> a
data Uniform = Uniform
{ lowerBound :: Float,upperBound :: Float
} deriving (Show)
instance ContinuousDistribution Uniform where
ppf d q = (1.0 - q) * lowerBound d + q * upperBound d
生产
stats.hs:10:27: error:
• Couldn't match expected type ‘a’ with actual type ‘Float’
‘a’ is a rigid type variable bound by
the type signature for:
ppf :: forall a. Floating a => Uniform -> a -> a
at stats.hs:10:5
• In the second argument of ‘(*)’,namely ‘lowerBound d’
In the first argument of ‘(+)’,namely ‘(1.0 - q) * lowerBound d’
In the expression: (1.0 - q) * lowerBound d + q * upperBound d
• Relevant bindings include
q :: a (bound at stats.hs:10:11)
ppf :: Uniform -> a -> a (bound at stats.hs:10:5)
Failed,modules loaded: none.
在类型类声明中指定类型可以编译,但仅限于Float。
class ContinuousDistribution d where
ppf :: d -> Float -> Float
data Uniform = Uniform
{ lowerBound :: Float,upperBound :: Float
} deriving (Show)
instance ContinuousDistribution Uniform where
ppf d q = (1.0 - q) * lowerBound d + q * upperBound d
修改类型类的规范方法是什么,以便我可以根据需要使用Float或Double?