无法用 Haskell 推导 (a ~ Double)
Could not deduce (a ~ Double) with Haskell
我在使用 Haskell 函数时遇到了打字问题。
我实现了这个(简化的)功能:
function (xa,ya,za) (xb,yb,zb) (Size tai) = function (xa,ya,za) (xb,yb,zb) (Ndiv ndiv)
where
ndiv = ceiling (leng / tai)
leng = sqrt((xb-xa)**2+(yb-ya)**2+(zb-za)**2)
data Method = Ndiv Int
| Size Double
如果我给 function
这个签名,它会很好用:
function :: (Double,Double,Double) -> (Double,Double,Double) -> Method -> [(Double,Double,Double)]
现在,我想将我的功能扩展到整个 Num
class。我强制类型为:
function :: Num a => (a,a,a) -> (a,a,a) -> Method -> [(a,a,a)]
并且在编译时,GHC 给我以下错误:
Could not deduce (a ~ Double)
from the context (Num a)
bound by the type signature for
function :: Num a =>
(a, a, a) -> (a, a, a) -> Method -> [(a, a, a)]
at Type.hs:7:13-62
`a' is a rigid type variable bound by
the type signature for
function :: Num a =>
(a, a, a) -> (a, a, a) -> Method -> [(a, a, a)]
at Type.hs:7:13
In the second argument of `(/)', namely `tai'
In the first argument of `ceiling', namely `(leng / tai)'
In the expression: ceiling (leng / tai)
我从来没有遇到过这样的错误,我有点失望。
我怀疑 class conflict/mismatch 但我不知道如何解决它?
你知道我的功能有什么问题以及如何让它工作吗?
由于您的数据类型,tai
参数固定为 Double
。将其更改为
data Method a = Ndiv Int | Size a
例如,你的函数应该进行类型检查,尽管你需要比 Num
更强的约束,因为 cieling
需要 RealFrac
。类型将为
function :: RealFrac a => (a,a,a) -> (a,a,a) -> Method a -> [(a,a,a)]
我在使用 Haskell 函数时遇到了打字问题。 我实现了这个(简化的)功能:
function (xa,ya,za) (xb,yb,zb) (Size tai) = function (xa,ya,za) (xb,yb,zb) (Ndiv ndiv)
where
ndiv = ceiling (leng / tai)
leng = sqrt((xb-xa)**2+(yb-ya)**2+(zb-za)**2)
data Method = Ndiv Int
| Size Double
如果我给 function
这个签名,它会很好用:
function :: (Double,Double,Double) -> (Double,Double,Double) -> Method -> [(Double,Double,Double)]
现在,我想将我的功能扩展到整个 Num
class。我强制类型为:
function :: Num a => (a,a,a) -> (a,a,a) -> Method -> [(a,a,a)]
并且在编译时,GHC 给我以下错误:
Could not deduce (a ~ Double)
from the context (Num a)
bound by the type signature for
function :: Num a =>
(a, a, a) -> (a, a, a) -> Method -> [(a, a, a)]
at Type.hs:7:13-62
`a' is a rigid type variable bound by
the type signature for
function :: Num a =>
(a, a, a) -> (a, a, a) -> Method -> [(a, a, a)]
at Type.hs:7:13
In the second argument of `(/)', namely `tai'
In the first argument of `ceiling', namely `(leng / tai)'
In the expression: ceiling (leng / tai)
我从来没有遇到过这样的错误,我有点失望。
我怀疑 class conflict/mismatch 但我不知道如何解决它?
你知道我的功能有什么问题以及如何让它工作吗?
由于您的数据类型,tai
参数固定为 Double
。将其更改为
data Method a = Ndiv Int | Size a
例如,你的函数应该进行类型检查,尽管你需要比 Num
更强的约束,因为 cieling
需要 RealFrac
。类型将为
function :: RealFrac a => (a,a,a) -> (a,a,a) -> Method a -> [(a,a,a)]