[Haskell-cafe] resolving missing class instances @ compile time

Greg Buchholz haskell at sleepingsquirrel.org
Wed May 4 20:18:34 EDT 2005


    Here's a little quirk I ran into recently.  While making a little
vector data type in class Num (code below), I didn't implement an
instance of "fromInteger" (thinking I didn't need it).  Well as you can
probably guess, it turns out I did need it, and subsequently got a run
time exception.  Which surprised me a little, since it seems like it
could have been caught at compile time.  (Although I did ignore a
warning).  This has probably been answered a thousand times, but could
someone point me in the direction of documentation explaining why it
compiled?

Thanks,

Greg Buchholz


--uncomment out "fromInteger" to get a working program

instance Num Vec where
    (V a b c) + (V x y z) = (V (a+x) (b+y) (c+z))
    (V a b c) - (V x y z) = (V (a-x) (b-y) (c-z))
    --fromInteger 0 = V 0.0 0.0 0.0 
instance Eq Vec where 
    (V a b c) == (V x y z) = (a==x) && (b==y) && (c==z)

data Vec = V !Double !Double !Double deriving Show

main = print $ sum [(V 1.0 2.0 3.0), (V 4.0 5.0 6.0)]
 


More information about the Haskell-Cafe mailing list