Re: Help need "Cannot use type synonym in instance head"

Please keep this on the list, so everyone can benefit.
Benny
Hello Jon
Thanks for the help. But it still didn't work after I changed the codes from:
type Poly = [Float] addPoly::Poly->Poly->Poly addPoly x y = zipWith (+) x y instance (Float a) => Num (Poly a) where (+) = addPoly
to your suggestion:
type Poly = [Float]
addPoly::Poly->Poly->Poly addPoly x y = zipWith (+) x y
instance Num [Float] where (+) = addPoly
I received this erreor message "Syntax error in instance head (variable expected)".
I sent this suggestion without actually trying it; unfortunately, as you discovered, it violates the rules of the Haskell 98 language. Please disregard this suggestion.
Then I tried your second suggestion, so I changed to codes to:
type Poly = [Float]
newtype Poly = Poly [Float]
addPoly::Poly->Poly->Poly addPoly (Poly x) (Poly y) = Poly (zipWith (+) x y)
instance (Float a) => Num (Poly a) where (+) = addPoly
Then I received this error message " Repeated definition of type constructor "Poly"".
What I meant was the following:
newtype Poly = Poly [Float]
addPoly::Poly->Poly->Poly addPoly (Poly x) (Poly y) = Poly (zipWith (+) x y)
instance Num Poly where (+) = addPoly
I.e., delete the type declaration as well as the extraneous information from the instance. <snip> Btw., you'll also want instances of Eq and Show to get Num to go through.
Thank you very much
You're welcome
Cheers
Benny
Jon Cast
participants (1)
-
Jon Cast