Reputation: 1592
I am new to Haskell and I am trying to understand the syntax. I have data type Vec
and it implements Show
, Floating
and Foldable
. But the syntax is different for Foldable
, why?
data Vec a = Vec [a]
instance Show a => Show (Vec a) where
show (Vec x) = show x
instance Floating a => Floating (Vec a) where
pi = Vec (repeat pi)
exp (Vec x) = Vec (map exp x)
log (Vec x) = Vec (map log x)
sin (Vec x) = Vec (map sin x)
cos (Vec x) = Vec (map cos x)
instance Foldable Vec where
foldr f x (Vec y) = foldr f x y
Upvotes: 2
Views: 72
Reputation: 262794
Your instance for Foldable
is "self-contained", it does not depend on the availability of other instances (of this or a different type class).
But for your instance of Show (Vec a)
you also need an instance of Show a
first (so that you can in your implementation call show x
).
That =>
syntax establishes this requirement/dependency.
Upvotes: 6