To make a type an instance of a class, we can either provide our implementation or use the default implementation by a mechanism called deriving.

 
 
data Color Red | Blue | Green deriving (Show)
 
 

The default implementaion will be exactly what you expected.

 
 
let a = Red
a
Red
 
 

If you don't want the default implementation, you can write you own

 
 
instance Show Color where
  show Red = "This is Red"
  show Blue = "This is Blue"
  show Green = "This is Green"
 
 

Some classes are trivial to implement, Haskell provide a default implementation for them. Here Show is one of them, others include: Eq for equality, Ord for ordering, and Read which is opposite of Show, Enum, Bounded. That means Haskell will automatically generate necessary code for you, its built into the compiler. For new comers to Haskell, it feels like magical.

You can acquire a bunch of functionalities simply by deriving from existing typeclasses for free.

What is typeclass in Haskell?

Simply look at the name typeclass, we can read as a class of types. From another point of view, a type class is a collection of functions, those functions have different implementations for underlying types but same signature. It sounds like a interface in Java, but the instance that implements these interfaces are types in Haskell, in Java it will be classes.

Haskell Typeclasses vs Java interfaces

Haskell typeclasses looks like interfaces in Java. They both capture the idea of abstraction, they extract out some common patterns from a set of different things, for typeclassses, its the set of types, for interfaces its a set of classes and objects.

They both implement some kind of polymorphism. You can apply the abstraction(the extracted common pattern) to different object as if they are the same kind of things. For example, all the instances of Eq typeclasses can compare with each other with the same operator == and /=. In Java, suppose two class Dog and Cat implemented the Pet interface which has a method Bark, now we know both Dog and Cat has the behavior of bark, we can let them bark with the same method without knowing the concrete types of the object.