concept type class in category haskell

appears as: type classes, type class, type classes, The type classes, The type class, The type classes
Haskell in Depth

This is an excerpt from Manning's book Haskell in Depth.

I recommend reading the Typeclassopedia by Brent Yorgey as a brilliant introduction to type classes for abstracting computations for those who are mathematically inclined.

Keep in mind that almost every Haskell type class gives us some abstraction across particular types. In the previous sections, we saw many examples where the same methods were used with values of different types, in the same way, thanks to type classes. We’ll continue doing the same in this section. The only new thing is that we’ll abstract computations over values of various types. The type classes I’m going to discuss here are presented in Figure 2.4, where solid arrows represent extending type classes.

Figure 2.4. Type classes for abstracting computations
abs computations

As seen in the figure, there are two most useful groups of type classes:

The type class Traversable is both simpler (it has only four methods, and two of them repeat the other two in a richer context, existing only for historical reasons) and more complex due to combining the effects of Functor, Applicative, Monad, and Foldable. This is the definition of Traversable:

For example, let's define a type class with a function, which gives a name of a unit:
Get Programming with Haskell

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

If you look at the type classes that each belongs to, you get a good sense of your answer.

In this lesson, you’re going to look an important abstraction in Haskell’s type system: type classes. Type classes allow you to group types based on shared behavior. At first glance, type classes are similar to interfaces in most object-oriented programming languages. A type class states which functions a type must support in the same way that an interface specifies which methods a class must support. But type classes play a much more important role in Haskell than interfaces do in languages such as Java and C#. The major difference is that as you dive deeper into Haskell, you’ll see that type classes typically require you to think in increasingly more powerful forms of abstraction. In many ways, type classes are the heart of Haskell programming.

What you’ve encountered here is your first type class! Type classes in Haskell are a way of describing groups of types that all behave in the same way. If you’re familiar with Java or C#, type classes may remind you of interfaces. When you see Num a, the best way to understand that statement is to say that there’s some type a of class Num. But what does it mean to be part of type class Num? Num is a type class generalizing the idea of a number. All things of class Num must have a function (+) defined on them. There are other functions in the type class as well. One of the most valuable GHCi tools is :info, which provides information about types and type classes. If you use :info on Num, you get the following (partial) output.

Listing 13.1. Num type class definition
GHCi> :info Num class Num a where (+) :: a -> a -> a (-) :: a -> a -> a (*) :: a -> a -> a negate :: a -> a abs :: a -> a signum :: a -> a

What :info is showing is the definition of the type class. The definition is a list of functions that all members of the class must implement, along with the type signatures of those functions. The family of functions that describe a number is +, -, *, negate, abs, and signum (gives the sign of a number). Each type signature shows the same type variable a for all arguments and the output. None of these functions can return a different type than it takes as an argument. For example, you can’t add two Ints and get a Double.

13.4. Defining a type class

The output you got from GHCi for Num is the literal definition of the type class. Type class definitions have the structure illustrated in figure 13.1.

Figure 13.1. Structure of a type class definition

In the definition of Num, you see plenty of type variables. Nearly all functions required in any type class definition will be expressed in terms of type variables, because by definition you’re describing an entire class of types. When you define a type class, you’re doing so precisely because you don’t want your functions to be tied to a single type. One way of thinking of type classes is as a constraint on the categories of types that a type variable can represent.

13.7. Deriving type classes

For your Icecream type class, it’s a bit annoying that you have to implement Show. After all, Icecream is just like Bool, so why can’t you have Haskell be smart about it and do what it does with Bool? In Bool, all that happens is that the data constructors are printed out. It just so happens that Haskell is rather smart! When you define a type, Haskell can do its best to automatically derive a type class. Here’s the syntax for defining your Icecream type but deriving Show.