concept fork in category functional programming

appears as: fork
Functional Programming with Kotlin

This is an excerpt from Manning's book Functional Programming with Kotlin.

fun sum(ints: List<Int>): Par<Int> =
    if (ints.size <= 1)
        unit { ints.firstOption().getOrElse { 0 } }
    else {
        val (l, r) = ints.splitAt(ints.size / 2)
        map2(
            fork { sum(l) },
            fork { sum(r) }
        ) { lx: Int, rx: Int -> lx + rx }
    }

With fork, we can now make map2 strict, leaving it up to the programmer to wrap arguments if they wish. A function like fork solves the problem of instantiating our parallel computations too strictly, but more fundamentally it puts the parallelism explicitly under programmer control. We’re addressing two concerns here. The first is that we need some way to indicate that the results of the two parallel tasks should be combined. Apart from this, we have the choice of whether a particular task should be performed asynchronously. By keeping these concerns separate, we avoid having any sort of global policy for parallelism attached to map2 and other combinators we write, which would mean making tough and ultimately arbitrary choices about what global policy is best.

Let’s now return to the question of whether unit should be strict or lazy. With fork, we can now make unit strict without any loss of expressiveness. A non-strict version of it, let’s call it lazyUnit, can be implemented using unit and fork.

Listing 7.7. A strict unit and fork can be combined to form a lazy variant of unit.
fun <A> unit(a: A): Par<A> = Par(a)

fun <A> lazyUnit(a: () -> A): Par<A> =
    fork { unit(a()) }

The function lazyUnit is a simple example of a derived combinator, as opposed to a primitive combinator like unit. We were able to define lazyUnit in terms of other operations only. Later, when we pick a representation for Par, lazyUnit won’t need to know anything about this representation—its only knowledge of Par is through the operations fork and unit that are defined on Par.

We know we want fork to signal that its argument gets evaluated in a separate logical thread. But we still have the question of whether it should begin doing so immediately upon being called, or else hold on to its argument to be evaluated in a logical thread later when the computation is forced using something like get. In other words, should evaluation be the responsibility of fork or of get? Should evaluation be eager or lazy? When you’re unsure about a meaning to assign to some function in your API, you can always continue with the design process—at some point later the trade-offs of different choices of meaning may become clear. Here we make use of a helpful trick—we’ll think about what sort of information is required to implement fork and get with various meanings.

If fork begins evaluating its argument immediately in parallel, the implementation must clearly know something, either directly or indirectly, about how to create threads or submit tasks to some sort of thread pool. This implies that the thread pool or whatever resource we use to implement the parallelism must be globally accessible and properly initialized wherever we want to call fork. This means we lose the ability to control the parallelism strategy used for different parts of our program. And though there’s nothing wrong with having a global resource for executing parallel tasks, we can imagine how it would be useful to have more fine-grained control over what implementations are used and in what context. For instance, we might like each subsystem of a large application to get its own thread pool with different parameters. As we consider this, it seems much more appropriate to give get, the responsibility of creating threads and submitting execution tasks.

Note that coming to these conclusions didn’t require knowing exactly how fork and get would be implemented, nor even what the representation of Par would be. We just reasoned informally about the sort of information required to actually spawn a parallel task, then examined the consequences of having Par values know about this information.

If fork simply holds on to its unevaluated argument until later, it requires no access to the mechanism for implementing parallelism. It just takes an unevaluated Par and “marks” it for concurrent evaluation. With this model, Par itself doesn’t need to know how to actually implement the parallelism. It’s more a description of a parallel computation that gets interpreted at a later time by something like the get function. This is a shift from before, where we were considering Par to be a container of a value that we could simply get when it becomes available. Now it’s more of a first-class program that we can run. Keeping this new discovery in mind, let’s rename our get function to run, and dictate that this is where the parallelism actually gets implemented.

Next up is the map2 operator. It doesn’t evaluate the call to f in a separate logical thread, in accordance with our design choice of having fork be the sole function in the API that controls parallelism. We could always wrap map2 with a call to fork if we wanted the evaluation of f to occur in a separate thread.