I am a newbie in Haskell and study type classes now. I created a simple type class but unfortunately I am not able to fix
Couldn't match expected type
-- So a can be Int, Float etc
data MyShape a = Circle a -- etc like | Rectangle a a
deriving(Show)
class Geo a where
inflate :: Num b => a -> b -> a
area :: Num b => a -> b
instance Num a => Geo (MyShape a) where
inflate (Circle r) x = Circle (r * x)
area (Circle r) = 3 * r * r
It does not like both inflate and area functions.
Error:
• Couldn't match expected type ‘a’ with actual type ‘b’
‘b’ is a rigid type variable bound by
the type signature for:
inflate :: forall b. Num b => MyShape a -> b -> MyShape a
at test2.hs:122:3-9
‘a’ is a rigid type variable bound by
the instance declaration
at test2.hs:121:10-33
• In the second argument of ‘(*)’, namely ‘x’
In the first argument of ‘Circle’, namely ‘(r * x)’
In the expression: Circle (r * x)
• Relevant bindings include
x :: b (bound at test2.hs:122:22)
r :: a (bound at test2.hs:122:19)
inflate :: MyShape a -> b -> MyShape a (bound at test2.hs:122:3)
|
122 | inflate (Circle r) x = Circle (r * x)
| ^