2015-06-06 33 views
1

我有一個Haskell功能的輸入問題。如果我給我的function此簽名無法推斷(一〜雙人間)哈斯克爾

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 :: (Double,Double,Double) -> (Double,Double,Double) -> Method -> [(Double,Double,Double)] 

,我想我的功能擴展到整個Num類 我實現了這個(簡化)功能。我強迫型與:

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) 

我從未有過這樣一個錯誤,我有點失望。
我懷疑類衝突/不匹配,但我不明白如何解決它?

你知道什麼是錯我的功能,以及如何使其工作?

+6

由於您的數據類型,「tai」參數固定爲「Double」。將其更改爲'data Method a = Ndiv Int |例如,大小爲a,那麼你的函數應該檢查,雖然你需要一個比'Num'更強的約束,'cieling'需要'RealFrac'。類型將是'RealFrac一個=>(A,A,A) - >(A,A,A) - >方法a - > [(A,A,A)]' – user2407038

+2

@ user2407038你應該把此成回答;) – Carsten

回答

3

由於數據類型的原因,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)]