Introduction
This article aims to provide a critique of the use of functional programming paradigms in specific fields of low-level development without regard to specific implementations or any resulting complications. Such criticism already exists (see Going Further) and has been discussed about extensively, but I have never encountered any that utilizes points that will be discussed in this article.
Development
First, let’s define what functional programming is and what its promises are. Functional programming is a programming paradigm heavily inspired by lambda calculus whose peculiarity lies in functions being merely trees of expressions (in themselves composed by functions) as opposed to sequence of statements. In its tree-like form, it is akin to how mathematicians model formal proof systems; this helps functional programmers attain rigorous logic and semantics in their code. Functional programming aims to reduce side effects and pseudo-nondeterministic aspects of programming by taking a radical approach to how behavior is defined in code. Of course, middle ground exists, but for the purpose of this article we will only be considering pure functional programming which tries to be as faithful to lambda calculus as possible, and thus tries to employ as formal and rigorous logic as possible.
Expression trees as axiomatic combinations of axiomatic applications
In mathematics, we construct proofs using basic knowledge which we assume to be true, these are what we usually call “axioms”. All applications of logic models, functional programming included, base themselves off of axioms and are, by extension, axiomatic. But we also assume that, what we call in the context of formal logic “rules of inference”, are coherent and that they can be applied to the objects being studied. Since we cannot pragmatically know all consequences and interpretations in which a proposition A holds, we cannot know for certain that all inferences, with regards to A, are applicable. In a sense, these rules of inference are also axiomatic. Functional programming does not emancipate itself from these constraints as lambda calculus has roots in proof theory. This implies that, with all its (extensive) rigour, pure functional programming is still axiomatic in its very essence. As such, functional programming follows the steps of formal proofs in that it requires two sorts of premises to reach any conclusion. I will now properly articulate these two sorts.
Axiomatic applications
First, any code written following the functional paradigm still utilizes functions which are written in non-functional ways and which are assumed to be true. For instance, we assume that the integer addition function is sound even though it always uses, at some point in its expression tree*, code which is not completely pure or proven (memory write, memory read, etc…). This type of function is therefore formally axiomatic and will be referred to as an “axiomatic application”. It is important to note that the axiomatic component of these functions is inherited with applications of said functions. For instance, using the integer addition function to express integer multiplication leads to the axiomatic nature of the addition to be inherited by the multiplication ; if the addition fails for any reason, the multiplication does too.
Axiomatic combinations
The second assumption is that we preserve logical validity when combining functions in a pure way. Of course, this is only in regards to the behaviour described and not the behaviour intended by the programmer which may be mistaken. But as is the case with proofs, we have no certainty that our model may work as intended when “applied”. In other words, we have no formal proof that our logic system “makes sense”. Therefore pure combination of functions may behave in an unexpected way. I will call these “axiomatic combinations”, referring to how functions are combined to create new functions.
Of course, these axioms exist in all programming paradigms and therefore do not constitute a critique to the functional paradigm by themselves. However, functional programming prides itself in its logical validity and rigorousness and therefore compromises a lot to achieve its intended goal. Functional programming is often poorly optimized and idiosyncratic as both speed and accessibility are not important to its purpose. Instead, functional programming tries to provide soundness* for developers and, I think, only succeeds to provide validity*.
How should we handle axioms ?
Now let’s evaluate to what extent we should worry about these axioms. Specifically in the case of functional programming, axioms are quite important I think. The code structure provided by the paradigm allows for a reduction of developer created bugs and therefore shifts our focus towards other sources of bugs. One of these potential sources being false or compromised assumptions.
But I don’t think both of the previously defined axioms types are equal in this manner.
Should we take into account the axiomatic nature of combinations ?
Computer science bases itself off of formal logic and mathematics, therefore I think it is reasonable to assume that, in the context of computers, most commonly used logic systems are valid. Not necessarily because they have been proven or are directly applicable, but because the logic system as described in code is so fundamental to the very idea of a program, that accounting for its potential failure means accounting for the failure of the whole environment on which the program runs, and would therefore equate to negating all efforts to define behaviour with code. For instance, we trust that if a condition defined inside an “if” block evaluates to true, the code specified will execute, not because we have empirical evidence that it definitely will (although we do), but because if it does indeed fail, we either have intentionally stopped the program or because an error occured such that we cannot use our computer anyway.
In essence, we should not worry about the validity of what I call axiomatic combinations while programming because doubting those foundations undermines the very framework within which programs have meaning and purpose. What we instead should worry about instead is axiomatic applications.
Why we should worry about axiomatic applications
Because, assuming both combinatory soundness, and proper behaviour specifications, the only possible source of bugs would be the functions that do not fit our neatly defined model. Moreover, these axiomatic applications can and often are critical and non-deterministic. We cannot know for certain that a packet sent using our operating system or programming language’s network API will reach its destination, therefore the function from which we invoke that process is axiomatic. We also cannot know for certain that memory allocation on the stack will work and that variable assignment will yield expected results, therefore memory allocation is also axiomatic and its usage leads to nondeterministic behaviour. Now of course, you will be hard pressed to find a valuable program which does not allocate anything, which leads me to my point ; all programs need these axiomatic applications because they encapsulate interactions with all nondeterministic systems that computers have to deal with. Their logical unsoundness is, in a sense, the very reason for their high usage frequency. All software exists specifically because it is running on arguably nondeterministic hardware and therefore these unsound axiomatic applications will always be ubiquitous. Moreover, the criteria on which we would evaluate the validity of said functions would in themselves abide by the rules of some sort of logical model which abstracts away real world nuance. But let’s not get too epistemological as that would be too pedantic for the purpose of this article. Let’s instead just say that these axiomatic applications are incredibly common, and are currently impossible to “prove”.
How to handle axiomatic applications
However, I think it is fair to expect such functions to be used thoughtfully. Which is why we should think about what and how many axioms we want to accept. Obviously this heavily depends on our use case. If we are working on a high level or performance focused system, then we probably should accept all the axioms we need as it allows us to both ship code faster and potentially allows us to use probabilistic optimizations. For instance, when working on neural networks we probably don’t want to systemically worry about whether our graphics card is working properly in our code as doing so would probably entail slowdowns in both development time and runtime. But when dealing with low level environments or critical systems, we want to eliminate as many non-deterministic dependencies as possible. We should indeed worry about whether our graphics card is working as intended when working on a graphics card driver. To illustrate my point, I will be using guidelines to build robust and safe C code which have been established by NASA. For this article it’s not important to discuss all these guidelines but one ; which is the one that consists of using as little heap allocation as possible. In this case, malloc adjacent functions are axiomatic applications and NASA has explicitly restricted its use to enhance the safety of their code by removing dependencies on what they deem to be an unacceptable axiom. But obviously not every company is NASA and it is clear that axioms depend on use cases, and that as I have established, low level development usually should strip away the use of axiomatic applications as much as possible (as is the case with most formal proof systems).
As a consequence of this, we can conclude that if we want to conceptualize a framework which would allow for proper handling of axioms, we should create a system that explicitly prohibits or allows the use of specific axiomatic applications. Furthermore, axioms are inherited, meaning the explicitness and properties of axioms should be inherited to users too. For instance, if we define a new high level function which reads from a file. We can assume it would inherit the axiom associated with the axiomatic application provided by the kernel that allows us to read from a file. Which means that if we don’t accept axiomatic file reading, we don’t accept our high level interface. This would allow us to formally evaluate some aspects of the soundness of our code. Additionally, we usually want to accept as few axioms as possible, which means that axioms shouldn’t be accepted by default.
I am currently not aware of any established system similar to the one I am describing. The closest I can think of is Rust’s safeness feature, which forces explicitness when using “unsafe” functions. But Rust does not make any effort to properly disclose what makes a function unsafe. As previously stated, safeness is relative to our use case, which means that we should not be categorizing functions as outright unsafe but basing our analysis of its safeness on the axioms we have chosen to accept. Of course, Rust’s safeness system’s purpose is not to evaluate systemic safeness based on axioms, but to evaluate memory and type safeness based on who is responsible for it (developer or language), and so it does very well, I may add. But I think that a proper system for handling axioms is very feasible and that such a system is one of the things that functional programming needs to actually be useful in robust low level development.
Practical example
Now let us look at a practical example to both illustrate my point and understand how such a system would help us work around axioms in our code using a lisp like pseudo-language.
Imagine that we are trying to implement a device to device protocol. The receiving end of this protocol simply waits for a positive signal to call a callback function, then should wait for a negative signal which indicates that the device should go back to the beginning of the protocol sequence (wait for a positive signal). In C, a possible implementation of this part of this protocol could look like this:
|
|
In this example, read_address is an axiomatic application as it reads directly from IO which can be affected by noise. We therefore have no guarantee that the value that our code will read is what the other end meant to send. In other words, the behaviour of read_address is not properly definable as it relies on non-deterministic systems.
Now let’s implement this in our functional pseudocode language. A few things to note about this language:
callsimply calls its first argument passing its other arguments.ifis a functionimportimports a function into the program scope
|
|
Although this looks very different to the C implementation, the logic is
roughly the same.
Notice how functional programming did not help us point out the axiomatic
nature of the read_address function.
In fact, I would argue that the confidence in the described logic that
functional programming provides blinds us of the potential failings of our code.
Also note how much more complex the code looks and how harder it is for us
humans to parse it.
Now let us look at how we could systemically highlight the axioms.
Here the only functions we directly call that we have not defined ourselves
are if and read_address.
Therefore we could label these as unsafe.
Now let us imagine a compiler which implements the system I am describing.
Such a compiler, upon compilation of the previously defined code,
would throw these warnings:
|
|
To mitigate these warnings we could:
- either annotate our function calls as explicitly unsafe
- or accept the axioms associated with the unsafe function directly.
Obviously we would like to accept if as it is a builtin defined by
the language itself, and would like to label read_address because we want
the unsafe nature of read_address to be explicit.
Doing this would result in the following code:
|
|
Now we don’t get any warning from the compiler and have explicit information
about what functions may be unsafe in the code.
This allows both the developers and the LSP to be more precise and intentional.
Note that in this case both of the new functions are labelled as unsafe since
they both use unsafe functions (with regards to the currently accepted axioms).
A function that only calls if would automatically be labelled as accepted
(if and only if if has already been accepted).
This system is only a very basic proposition to illustrate my point and is very much perfectible.
Conclusion
Succinctly, functional programming aims to improve the soundness of code in a very elegant way. But it fails to consider the implications of its roots, that is, that its logic is very much reliant on axioms. In a sense, none of the logic exists in a vacuum and its soundness is only relevant in its own self described model. When creating robust and safe systems, as we ought to do in low level and embedded programming, we should be explicitly thoughtful about these axioms as they are both ubiquitous, and nondeterministic. Therefore, robust low level systems should not contempt themselves with the functional paradigm as a source of logical soundness but implement systems to help developers understand what logical dependencies their systems are based upon. Such rigorous systems don’t currently exist in functional programming despite functional programming’s rigorous intent and its already existing idiosyncrasies.
Annex
Definitions
- Valid: Composed of logic that is properly and formally defined within a system but that does not guarantee truth of axioms.
- Sound: Valid and whose premises have been proven.
Resources
- https://en.wikipedia.org/wiki/Purely_functional_programming
- https://en.wikipedia.org/wiki/Proof_theory
- https://doc.rust-lang.org/nomicon/meet-safe-and-unsafe.html
Going Further
- Interacting with Functional Languages - Duncan Cameron Sinclair - Section 2.6
- https://dl.acm.org/doi/10.1145/3674648
- https://atom-haskell.github.io/