I have a three-dimensional vector data type defined as 3 floats. I understand that if I provide a Num instance for my class and define normal math operators, I can use them in my class.
data Vec3 = Vec3 { x :: Float , y :: Float , z :: Float } deriving (Show, Eq) instance Num Vec3 where (+) v1 v2 = Vec3 (x v1 + x v2) (y v1 + y v2) (z v1 + z v2)
When I upload my file to ghci, I get warnings because I did not define all the functions in Num , which makes sense.
Prelude> :l temp.hs [1 of 1] Compiling Main ( temp.hs, interpreted ) temp.hs:6:10: Warning: No explicit method or default declaration for `*' In the instance declaration for `Num Vec3' temp.hs:6:10: Warning: No explicit method or default declaration for `abs' In the instance declaration for `Num Vec3' temp.hs:6:10: Warning: No explicit method or default declaration for `signum' In the instance declaration for `Num Vec3' temp.hs:6:10: Warning: No explicit method or default declaration for `fromInteger' In the instance declaration for `Num Vec3' Ok, modules loaded: Main.
However, I can still use the ones I defined.
*Main> let a = Vec3 1.0 2.0 3.0 *Main> let b = Vec3 2.0 4.0 5.0 *Main> a + b Vec3 {x = 3.0, y = 6.0, z = 8.0}
My confusion is due to the following error that I get when trying to use the sum function
*Main> sum [a,b] Vec3 {x = *** Exception: temp.hs:6:10-17: No instance nor default method for class operation GHC.Num.fromInteger
Why does the amount need to determine fromInteger for my Vec3 data Vec3 ? First, I would suggest that the sum uses only the + function, and for another, my data type does not use Integer .
source share