Functional Programming in Scala

March 12, 2016

These are my notes from Functional Programming in Scala.

Chapter 7: Purely functional parallelism

The algebra of an API

The law of mapping

map(unit(x))(f) == unit(f(x))

It is important to note that f here has no given implementation. No information is available to assume anything about it, particularly its type signature.

Scala gives us certain escape hatches to get around this (e.g. downcasting) or inspect f at runtime (e.g. isInstanceOf, Java's reflection API), so we must informally assert that these are disallowed by the above law.

We can start from a simplified version:

map(y)(id) == y

And work backwards to the earlier, more complex version:

map(unit(x))(f) == unit(f(x))

This is because map shall not behave differently for different types of functions f.

We get this second law for free7.2, so we call it a free theorem.