**Istanze**

We have already seen how to declare instances of some simple classes; allow us to consider some more advanced classes here. There is a ** Functor** class defined in the

**module.**

`Functor`

**Note**: The name “functor”, like “monad” comes from category theory. There, a functor is like a function, but instead of mapping elements to elements, it maps structures to structures.

The definition of the functor class is:

**class Functor f where
fmap :: (a -> b) -> f a -> f b**

The type definition for ** fmap** (not to mention its name) is very similar to the function

**over lists. In fact,**

`map`

**is essentially a generalization of**

`fmap`

**to arbitrary structures (and, of course, lists are already instances of**

`map`

**). However, we can also define other structures to be instances of functors. Consider the following datatype for binary trees:**

`Functor`

**data BinTree a = Leaf a
| Branch (BinTree a) (BinTree a)**

We can immediately identify that the ** BinTree** type essentially “raises” a type

**into trees of that type. There is a naturally associated functor which goes along with this raising. We can write the instance:**

`a`

**instance Functor BinTree where
fmap f (Leaf a) = Leaf (f a)
fmap f (Branch left right) =
Branch (fmap f left) (fmap f right)**

Now, we’ve seen how to make something like ** BinTree** an instance of

**by using the**

`Eq`

**keyword, but here we will do it by hand. We want to make**

`deriving`

**as instances of**

`BinTree`

**but obviously we cannot do this unless**

`Eq`

**is itself an instance of**

`a`

**. We can specify this dependence in the instance declaration:**

`Eq`

**instance Eq a => Eq (BinTree a) where
Leaf a == Leaf b = a == b
Branch l r == Branch l' r' = l == l' && r == r'
_ == _ = False**

The first line of this can be read “if ** a** is an instance of

**, then**

`Eq`

**a is also an instance of**

`BinTree`

**“. We then provide the definitions. If we did not include the**

`Eq`

**part, the compiler would complain because we’re trying to use the**

`Eq a =>`

**function on as in the second line.**

`==`

The “** Eq a =>**” part of the definition is called the “context.” We should note that there are some restrictions on what can appear in the context and what can appear in the declaration. For instance, we’re not allowed to have instance declarations that don’t contain type constructors on the right hand side. To see why, consider the following declarations:

**class MyEq a where
myeq :: a -> a -> Bool
instance Eq a => MyEq a where
myeq = (==)**

As it stands, there doesn’t seem to be anything wrong with this definition. However, if elsewhere in a program we had the definition:

**instance MyEq a => Eq a where
(==) = myeq**

In this case, if we’re trying to establish if some type is an instance of ** Eq**, we could reduce it to trying to find out if that type is an instance of

**, which we could in turn reduce to trying to find out if that type is an instance of**

`MyEq`

**, and so on. The compiler protects itself against this by refusing the first instance declaration.**

`Eq`

This is commonly known as the closed-world assumption. That is, we’re assuming, when we write a definition like the first one, that there won’t be any declarations like the second. However, this assumption is invalid because there’s nothing to prevent the second declaration (or some equally evil declaration). The closed world assumption can also bite you in cases like:

**class OnlyInts a where
foo :: a -> a -> Bool
instance OnlyInts Int where
foo = (==)
bar :: OnlyInts a => a -> Bool
bar = foo 5**

We’ve again made the closed-world assumption: we’ve assumed that the only instance of ** OnlyInts** is

**, but there’s no reason another instance couldn’t be defined elsewhere, ruining our definition of**

`Int`

**.**

`bar`

đ˝