1
我無法弄清楚msg的含義。我想爲下面的數據類型定義函數距離...我不想使用任何GHC擴展..即使代碼很醜,也希望更好地理解錯誤,然後再繼續使用擴展。有人可以讓我知道這個錯誤意味着什麼,以及我如何擺脫這個。Haskell:GHCI中錯誤的含義
class Vector v where
distance :: v -> v -> Double
-- doesn't make sense, but WTH...
newtype OneD1 a = OD1 a
deriving (Show)
instance Vector (Maybe m) where
distance _ _ = 5.6
instance Vector (OneD1 m) where
distance (OD1 x1) (OD1 x2) = x2-x1
Prelude> :reload
[1 of 1] Compiling Main (VectorTypeClass.hs, interpreted)
VectorTypeClass.hs:33:33:
Couldn't match expected type `Double' with actual type `m'
`m' is a rigid type variable bound by
the instance declaration
at C:\Users\byamm\Documents\Programming\Haskell\Lab\Expts\VectorTypeClass\VectorTypeClass.hs:32:10
Relevant bindings include
x2 :: m
(bound at C:\Users\byamm\Documents\Programming\Haskell\Lab\Expts\VectorTypeClass\VectorTypeClass.hs:33:27)
x1 :: m
(bound at C:\Users\byamm\Documents\Programming\Haskell\Lab\Expts\VectorTypeClass\VectorTypeClass.hs:33:18)
distance :: OneD1 m -> OneD1 m -> Double
(bound at C:\Users\byamm\Documents\Programming\Haskell\Lab\Expts\VectorTypeClass\VectorTypeClass.hs:33:4)
In the first argument of `(-)', namely `x2'
In the expression: x2 - x1
Failed, modules loaded: none.
Prelude>
在您的類型簽名'你說'v distance'> - V - > Double'。在你的'OneD1 m'的實例聲明中,你有'm'類型的'x1'和'x2'。那是你在'OneD1 m'中提到的那個m。由於'v - > v - > Double'類型,Haskell希望構造一個'Double'類型的值。這意味着'm' ='Double'。但'm'不是'Double'。你可以使用類型族或者進一步推廣'距離'類型來解決問題。例如'distance :: v - > v - > v'和'instance Num m => Vector(OneD1 m)where'。 – bash0r