```
package cats
package laws
import cats.syntax.functor._
/**
* Laws that must be obeyed by any `Functor`.
*/
trait FunctorLaws[F[_]] extends InvariantLaws[F] {
implicit override def F: Functor[F]
def covariantIdentity[A](fa: F[A]): IsEq[F[A]] =
fa.map(identity) <-> fa
def covariantComposition[A, B, C](fa: F[A], f: A => B, g: B => C): IsEq[F[C]] =
fa.map(f).map(g) <-> fa.map(f andThen g)
}
object FunctorLaws {
def apply[F[_]](implicit ev: Functor[F]): FunctorLaws[F] =
new FunctorLaws[F] { def F: Functor[F] = ev }
}
```

Mutable structures cannot be functors because they fail the identity law.

`Set`

does not obey the second law:

```
case class Bad(a: Int) { override def hashCode = 1; override def equals(a:Any) = true }
val f = (n:Int) => Bad(n)
val g = (b:Bad) => b.a
Set(1,2,3).map(f andThen g) == Set(1,2,3).map(f).map(g) // false
```

## 0 comments:

Post a Comment