O'Reilly logo

Haskell Cookbook by Yogesh Sajanikar

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

How it works...

The preceding example is rather simple, but it cleverly uses the type family. Look at the definition of VectorSpace type-class. We know that we can scale a vector by a scalar. The type class VectorSpace allows us to define our data structure as a vector. however, we should also be able to customize which scalar we would like to associate with this type.

The VectorSpace type class is defined as follows:

    class AdditiveGroup v => VectorSpace v where
      type Scalar v :: *
 

In the preceding class definition, the type Scalar is defined as type Scalar v :: *. The type Scalar v means that, for a data type v, which is an instance of VectorSpace, there is a scalar associated with it. And this type Scalar can be defined as we define the ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required