我试图理解为什么会出现以下错误,
add (Erf prob) to the context of the instance declaration
我有一个使用erffrom 的简单实例Data.Number.Erf
instance (Floating prob) => CDF (Normal prob) where
cdf dist dp = ( 0.5 * ( 1 + erf ( (x - mu)/ (sqrt $ sigma2 *2) )))
Run Code Online (Sandbox Code Playgroud)
但是ghc-7.6.3会抱怨上面的内容,可能的解决办法是改变instance (Floating prob),instance(Floating prob, Erf prob)任何人都可以解释为什么需要这样做?`
如果你查看Data.Number.Erf的文档,你就会看到它
class Floating a => Erf a where
erf :: a -> a
Run Code Online (Sandbox Code Playgroud)
这意味着该erf函数是Erf该类的一部分.它的类型是erf :: Erf a => a -> a.这意味着您需要添加Erf a在=>其上使用它的任何函数类型的上下文(位前)a,以确保erf为类型的值定义a.
在这种情况下,您已经调用了该类型prob,因此我们需要Erf prob.
实际上,因为Floating是一个超类Erf,所以任何实例Erf必须已经是一个实例Floating,所以你不需要明确指定它.这意味着你可以写
instance (Erf prob) => CDF (Normal prob) where
...
Run Code Online (Sandbox Code Playgroud)