Selective Applicative Functors
Selective applicative functors, first proposed in 2019 by Andrey Mokhov, Georgy Lukyanov, Simon Marlow, and Jeremie Dimino, are a relatively new concept that aims to provide a better description of what can and cannot be allowed in terms of control flow within a programming language. However, despite their potential, the theoretical analysis of select, the key component of selective applicative functors, has been hindered by the lack of a suitable framework to describe its behavior.
The main goal of selective applicative functors is to encode exclusive and exhaustive determined choice, allowing for the selection between a finite number of predetermined case branches based on previous results during evaluation. They represent the shape of an abstract syntax tree (AST) for a programming language, making them a natural fit for programming language design. Selective applicative functors can be related to the Alternative typeclass, which provides the | operator for nondeterministic choice.
To understand selective applicative functors, it is helpful to consider monads. Monads encode dynamic control flow by allowing binding any function as a continuation, which can construct an arbitrary action to execute next based on the result of the previous action. This enforces a clear direction to evaluation, as left effects have to happen before the right effects.
The static analysis of monads is facilitated by monoid accumulation, but monads themselves do not have a Monad instance due to compatibility issues with applicative functors.
Selective applicative functors sit in a sweet spot between monads and applicative functors, expressing finite control flow while still allowing dynamic effects. They restrict the applicative actions to finite-case functions, enabling exclusive and determined choice. To achieve this, selective applicative functors must be defined using arrows (composable profunctors) rather than plain functors, as the former allows for coproducts (either x → r and y → r) to be considered alongside products (tuple (i → x) and (i → y)).
The key insight of selective applicative functors is that they should encode finite-case functions to express exclusive and determined choice. This can be achieved by restricting applicative actions to finite-case functions using a CaseTree data type. Focusing on arrows instead of functors allows for a better understanding of selective applicative functors and their structure. The concept of "finite-case functions" is somewhat tricky to define but is crucial for the proper formulation of selective applicative functors.
Once we focus on arrows, the concept of "finite-case functions" becomes much clearer, and the structure of selective applicative functors becomes more apparent. They can be interpreted into a constant functor, enabling static analysis using near-semirings. This is a significant advantage as it allows for the analysis of selective applicative functors' behavior even without executing the dynamic code.
In summary, selective applicative functors provide a powerful tool for expressing finite control flow in programming languages. By considering arrows instead of plain functors, we can capture the essence of selective applicative functors and their ability to encode exclusive and exhaustive determined choice. This new framework enables static analysis using near-semirings, making selective applicative functors a valuable addition to the toolbox of programming language designers.
Written by urgent.news from Lobsters's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.