While trying to understand instances in Haskell I made this example. The Integer part works well but it does not work for the Float instance. I think it is better to make a single instance of the type Num (so that square works on all Num). I suppose I have to add Num as a constraint to my class declaration but I couldn't figure out what the instance would look like. As I understand it, the constraint on the class forces any instances to be of that type (according to the constraint).
class Square a where
area :: a -> a
instance Square Integer where
area a = a*a
instance Square Float where
area a = a*a
Square Float
instance should work just fine. What problem a you experiencing with it? – leftaroundabout