Node.JS
Node.JS

Reputation: 1592

Haskell trying to understand typeclass syntax

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

Answers (1)

Thilo
Thilo

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

Related Questions