concept fork in category functional programming

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 makemap2strict, leaving it up to the programmer to wrap arguments if they wish. A function likeforksolves 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 tomap2and 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
unitshould be strict or lazy. Withfork, we can now makeunitstrict without any loss of expressiveness. A non-strict version of it, let’s call itlazyUnit, can be implemented usingunitandfork.Listing 7.7. A strict
unitandforkcan be combined to form a lazy variant ofunit.fun <A> unit(a: A): Par<A> = Par(a) fun <A> lazyUnit(a: () -> A): Par<A> = fork { unit(a()) }The function
lazyUnitis a simple example of a derived combinator, as opposed to a primitive combinator likeunit. We were able to definelazyUnitin terms of other operations only. Later, when we pick a representation forPar,lazyUnitwon’t need to know anything about this representation—its only knowledge ofParis through the operationsforkandunitthat are defined onPar.We know we want
forkto 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 likeget. In other words, should evaluation be the responsibility offorkor ofget? 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 implementforkandgetwith various meanings.If
forkbegins 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 callfork. 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 giveget, the responsibility of creating threads and submitting execution tasks.Note that coming to these conclusions didn’t require knowing exactly how
forkandgetwould be implemented, nor even what the representation ofParwould be. We just reasoned informally about the sort of information required to actually spawn a parallel task, then examined the consequences of havingParvalues know about this information.If
forksimply holds on to its unevaluated argument until later, it requires no access to the mechanism for implementing parallelism. It just takes an unevaluatedParand “marks” it for concurrent evaluation. With this model,Paritself 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 thegetfunction. This is a shift from before, where we were consideringParto 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 ourgetfunction torun, and dictate that this is where the parallelism actually gets implemented.
Next up is the
map2operator. It doesn’t evaluate the call tofin a separate logical thread, in accordance with our design choice of havingforkbe the sole function in the API that controls parallelism. We could always wrapmap2with a call toforkif we wanted the evaluation offto occur in a separate thread.