Take the 2-minute tour ×

Here is a Haskell code which compiles:

class Category categ where
  method1 :: categ a a
  method2 :: categ a b -> categ b c -> categ a c

But I don't understand its meaning:

  1. what's categ? how can it be defined: through data or class? maybe this is a function?
  2. what are a and b and c? since they are not specified as

    class Category categ a b c where
        method1 :: categ a a

    this code shouldn't compile, should it?

share|improve this question

1 Answer 1

up vote 6 down vote accepted
class Category categ where

This is a type class declaration,

It declares a type class called Category. The categ is the variable used to refer to the a type implementing Category in the associated functions. Later you will fill it when you say

instance Category Foo where

Then wherever categ is used in the type class methods, you substitute Foo and define the methods.

Read it out loud as "A type categ is a Category if it has the following methods"

Now for the methods:

method1 :: categ a a

Declares 2 functions that any type implementing Category must implement. The first one is a no argument function which returns, for any type a, the type categ a a. method2 takes in two pieces of data, categ a b and categ b c, and returns categ a c.

Here a b c are type variables, they are placeholders that are filled with arbitrary concrete types when the function is called. If you're familiar with C++ or Java,

template<typename A>
categ<A, A> method1();

is pretty close to what you have in Haskell. In Haskell, we just use type variables without declaring them. They are function local, and equivalent to saying something like, For any type a, the following code works.

This is pretty fundamental to Haskell, I'd recommend reading Learn You A Haskell. It's free and will cover all aspects of the code you just posted.

share|improve this answer
I'm asking instance Category Foo where -- how Foo could be defined: through class or data or something else? Could it be a function? –  Marius Kavansky Sep 4 '13 at 5:54
@MariusKavansky Can you explain what you mean by "how can it be a function"? –  jozefg Sep 4 '13 at 7:25
@MariusKavansky Foo would have to be a concrete type (defined as data or newtype) of kind * -> * -> *, i.e. it has to take two type variables. And it could be the type of functions (->), if you meant that. –  phg Sep 4 '13 at 9:01
@phg, this is exactly what I mean. Could it be class also? –  Marius Kavansky Sep 4 '13 at 10:26
No, not directly, as classes are not like types. What you can do is making all instances of one class instances of another class, like instance Monad m => Category (Kleisli m) where ... –  phg Sep 4 '13 at 10:40

Your Answer


