concept polymorphism in category haskell

appears as: polymorphism
Get Programming with Haskell

This is an excerpt from Manning's book Get Programming with Haskell.

Listing 14.5. Demonstrating the need for polymorphism defining show for TwoSidedDie
data TwoSidedDie = One | Two show :: TwoSidedDie -> String show One = "one" show Two = "two"

The problem is that by default you’d like to have more than one behavior for show, depending on the type you’re using. What you’re looking for here is called polymorphism. Polymorphism means that the same function behaves differently depending on the type of data it’s working with. Polymorphism is important in object-oriented programming and equally so in Haskell. The OOP equivalent to show would be a toString method, one that’s common among any classes that can be turned into a string. Type classes are the way you use polymorphism in Haskell, as shown in figure 14.1.

Figure 14.1. Visualizing polymorphism for read