84 points · by ingve · 6y ago · 101 comments
def min(foo: List[int]) -> Optional[int]:
min = None
for val in foo:
if min is None or val < min:
min = val
return min
I argue this is much easier to read than the recursive version without assignments: def min(foo: List[int]) -> Optional[int]:
if not foo:
return None
else if len(foo) == 1:
return foo[0]
else:
rest_min = min(foo[1:])
return rest_min if rest_min < foo[0] else foo[0]
We're optimizing for readability, and sometimes readability means mutable variables. This is to says nothing about performance which is often much better with mutable variables in languages like C++.Edit: people have posted better version of my code in the comments. Take a look for more pythonic examples.
My view of a program is that of a dependence graph, ideally directed acyclic. Ideally we want that graph to be as sparse as possible: good old decoupling. Mutable state (not just global variables) tend to introduce hidden dependencies, making that graph denser than what it initially appears. That can be a killer.
Surprisingly though, as years pass, I am less and less certain what the "correct" structure of a program should be. I sure want to keep it small, simple, easy to maintain… but the method to achieve this still eludes me.
(Although game programmers seem to have necessarily built their mental models on global state, and think about it differently. Their programs generally don't get maintained for decades though.)
But "functional in the large" is basically the same thing as dependency injection in OOP languages. State and I/O are explicit parameters rather than globals.
Here is a comment I wrote about that a long time ago, on Disadvantages of Purely Functional Programming:
https://news.ycombinator.com/item?id=11841893
To me there's no real advantage to expressing something like "split a string by a delimiter" in a purely functional style. Either way, you have a trivial referentially transparent function you can reuse without causing complexity in your program. You might as well do the obvious imperative thing.
...
So both "functions and data" are effectively and usefully implemented as classes. The key is to make some classes like functions, and some classes like data. And have more of a bipartite dependency graph, where functions depend on data, and data depends on functions.
-----
I also explicitly structure the program as a graph, which I try to make acyclic, but again the problems and solutions are domain-specific. (Programming is very diverse and programmers tend to generalize from their own few examples.)
For example, Compilers and interpreters are inherently mutually recursive, which gives you cyclic dependencies. As far as I can tell, this is the essential reason that Fabrice Bellard's QuickJS has one file that's 70K lines of C. And you can see the same thing if you look at the Zig compiler.
In Oil I "modularize" the cyclic dependencies so you can plug them in and out at runtime (which is actually used in the program), but it has a performance cost.
I think of it as having to deal with the fact that humans only have so much mental space/short term memory.
The problem with stateful everything is that you can't build a complete understanding of it; there's just too much. However, for short bits of code that don't leak their imperativeness to the outside world, it doesn't matter. You can read the whole function and understand it.
That's the basis of what I call "faux-mutability", wherein you have an immutable type system, but all updates are expressed as mutations.
In the current docs for a language I'm working on, I give a brief example[1], but in a nutshell, you can transform an expression like so:
let x.a.b := 5
let x := set_rec(x, #a, set_rec(x.a, #b, 5))
It does prohibit[2] making arbitrary graphs, but for the use case I'm targeting this is fine.(Also, as a prototype for how this can work, I wrote a python module[3] that implements it with the pyrsistent module.)
[1]: https://tenet-lang.org/#how-does-it-work
[2]: Or you'd need another mechanism to do so.
> I sure want to keep it small, simple, easy to maintain… but the method to achieve this still eludes me.
It eludes all of us. At best, we know some methods that have worked moderately well for some people in some circumstances.
let list_min l =
reduce min l
I find this crushingly simple to write and to reason about, much more than the convoluted and inefficient imperative code.EDIT: for completeness, here's the definition of `reduce` and `min`:
let reduce op l =
match l with
| [] -> invalid_arg "reduce: empty list"
| [x] -> x
| hd::tl -> op (hd) (reduce op tl)
let min a b =
if a < b then a else b list_min = (l) -> reduce min, l
With the definitions being: min = (a, b) -> if a < b then a else b
reduce = (op, l) => switch
when l.length is 0 then throw "reduce: empty list"
when l.length is 1 then l[0]
else reduce op, [(op l[0], l[1]), l[2..]].flat() from functools import reduce
def min2(a, b):
if a < b:
return a
return b
def min(list):
return reduce(min2, list, None)Oh, Python…
Secondly, I think there's something to be said for familiarity. I think looping is second nature to most of us not because it's superior or more intuitive, but because traditionally this is one of the first things we're all taught. If functional programming were the norm and people were taught recursion early on instead I think your notion of simplicity would differ.
I've been teaching a little bit of programming to a beginner lately, and they've often found functional approaches to problems easier to grasp than their imperative equivalents because they're more mathematical. It's like an expression of something they already know versus looping, mutability, et al which are new concepts entirely.
Recursion is much more of a mathematical concept that we learn about long after preschool, while my 3 year old already gets loops.
> On the other hand, recursion is quite fundamental. It is not inherently a high-level concept, suitable only for grownups but not for children. Most people actually grew up with an implicit understanding of rudimentary recursion long before they understood iteration—that is, before they understood how to carry out a loop of instructions. Given a list of tasks to do, the simplest mode of operation is a recursive approach that charges blindly ahead: […]
> It's only after we've gained experience with such an almost mindless method that we acquire a more global view of the process:
> Do Jobs = Do each job on the list. (2)
> […]
> Do a_k for 1 ≤ k ≤ n. (5)
> All computer programmers have in fact progressed from stage (1) to these later stages rather early in our lives, because of an inner desire to “see through” the entire chain of consequences that result from primitive actions.
> Furthermore, once we reached these later stages, we entirely forgot that they were actually elaborations of the recursive procedure in (1). There was no point in narrowing our perspective to the simple-minded form. The vast majority of recursive situations that faced us as children fell into very simple patterns that could readily be “seen through” and understood as a unit. Thus we came to understand iteration as an elementary idea.
> It was only later, when faced with difficult problems whose recursive structure cannot be visualized in straightforward terms such as operations on arrays of elements, that we re-learned the importance of recursion. Therefore we now tend to regard recursion as a high-level concept, although it is really fundamental.
Possibly these views of his are influenced by his early background in programming in assembly/machine language, where it takes some sophistication to translate recursive ideas into code (maintaining a stack etc), but it's an interesting point of view worth some consideration. This is from his draft of TAOCP Chapter 8 ("Recursion"), and is preceded by the following paragraph:
> High-level languages for computing—which themselves are de ned recursively, because algebraic formulas are built up from algebraic formulas—allow programmers to pretend that recursion is real, that a computer is somehow manufactured from recursive components. But programs that invoke themselves are actually implemented by means of a stack structure behind the scenes. A good programmer is able to understand such algorithms by viewing them simultaneously at a high level and at the machine level, and at many levels in between, thereby perceiving what a real computer can actually do well.
On the flipside, I have a lot of mathematical training, so maybe this is an uncommon perspective.
Recursive has the advantage that when you use the tools for it, recursive is easier to read. If I see e.g. a map, I know we're converting a collection of things to some other things without restriction. With a look, I just have to read through it to see what it's doing.
Anthropomorphism is the bane of our craft. Our untrained intuitions don't really matter.
> Recursion is much more of a mathematical concept that we learn about long after preschool
Programming is applied mathematics. Different from the kind of maths you learn at school of course, and often even more rigorous.
I personally have no qualms about requiring some mathematical proficiency in programming. Makes you much more capable at a number of applications later on.
It may be instructive to look at how readable a very naive implementation of a basic function like "min" is, but it probably doesn't actually matter that much in practice, because you'll be using what the standard library offers (and its implementation is probably much less straightforward than you might think).
If you are attempting to debug logic, it often helps to have all the context. To do that in recursion, you have to travel up and down the stack, examining local contexts one by one.
Here are two recursive ways to write a min function:
Idiomatic and structured, using (Maybe . Min) monoid:
min = foldMap (Just . Min)
Incidentally, the above works on any data structure where this operation makes sense, not just lists, which is nice.Unstructured, ad-hoc
minimum [] = Nothing
minimum (x:xs) = case minimum xs of Nothing -> Just x; Just y -> Just (min x y)
This latter doesn’t even rely on anything unique to functional programming ecosystems (like folds or principled algebraic classes). #!/usr/bin/env python3
from functools import reduce
from typing import List, Optional
def min_pair(min: Optional[int], val: Optional[int]) -> Optional[int]:
if min is None or val < min:
return val
else
return min
def min(foo: List[int]) -> Optional[int]:
return reduce(min_pair, foo) def min(foo: List[int], default=None) -> Optional[int]:
if not foo:
return default
cur_min = foo[0] if default is None or foo[0] < default else default
return min(foo[1:], cur_min) def min(foo):
it = iter(foo)
try:
m = next(it)
except StopIteration:
return None
for val in it:
if val < m:
m = val
return mIn Scala the idiomatic approach would be to favor the immutable/recursive version.
def min(foos: List[Int]): Option[Int] = foos match {
case Nil => None
case x :: Nil => Some(x)
case x :: xs =>
min(xs).filter(_ < x) orElse Some(x)
}In pseudo-Haskell notation:
min1 : Int -> List Int -> Int
min1 def Nil = def
min1 def (Cons x xs) = min1 (def if def <= x else x) xs
min : List Int -> Maybe Int
min Nil = None
min (Cons x xs) = min1 x xsWhat's the best way to have your expression-oriented programming and use cache-friendly data structures? How do Haskell et al handle this?
This allows mutation to remain the performance optimisation it is, without allowing it to seep into the design of systems, which is where it causes badness.
As far as I know, Haskell is one of the few languages (if not the only one) that supports local mutation that is guaranteed to not be visible in any way to the caller.
To take full advantage of the functional paradigm most settle for logarithmic access with tries.
“Sometimes, you just can't avoid the assignment statement or other side effects. Maybe you need so much efficiency that you have to mutate state to optimise your program”.
This class of programs includes all high performance numerical code. These programs compute by declaring large multidimensional arrays (usually distributed among multiple compute nodes), reading values from array elements, doing arithmetic on those values, and writing the results into array elements. These elements have fixed memory addresses, so the computation consists in changing the values stored in fixed memory locations. Efficiency comes from minimizing the movement of data and parallelizing as much as possible. It’s what Rich Hickey would call computing with places, not with values.
Using separate variables instead of reusing the same variable will sometimes actually increase the optimizations that compilers can do. Why? Because the version where you overwrite the variable is introducing additional constraints (e.g. keeping X1 and X2 in the very same stack slot called X) that have costs, but whose semantics you aren’t necessarily (or even usually!) relying upon.
For example: you know all that spare register spill space in the x64 calling convention that you’re allocating either way, and which deallocates as a block on function exit? Can’t make efficient use of that if you’re telling the compiler it has to keep both of your pointers successively in the same memory address!
On the other hand, the opposite is also true: sometimes overwriting a variable will free the compiler from a constraint it was under. This mostly comes about as the result of X1 and X2 both holding a shared or overlapping pointer, introducing aliasing (as often happens in the pure-functional data structures OP advocates for.) By overwriting X1 with X2 (or, equivalently, by freeing X1 as soon as you’re done with it!) you can allow the compiler to be more confident that nothing will access the shared pointer through X1.Y any more, only through X2.Y—which may allow e.g. smart pointer classes to elide a synchronization point.
(Note that this assumes a language with a model where a variable directly relates to memory, such that assigning to the same variable implies assigning to the same memory. This is true in most low-level languages, but not in functional languages; in e.g. Elixir, a second assignment to the same variable is actually creating a second variable with a mangled name, and leaving it to the compiler’s lifetime analysis to figure out that the first one is dead after that point.)
LLVM optimizations, for instance, often need the IR to be in SSA form (static single Assignment).
int a = 5
int b = 2 * a
// don't use a again
and> int a = 5 > a = 2 * a
?
From there, you just have to know whether the compiler you're using supports those optimizations. If it does, and your code should have them, but it doesn't end up having them—well, that's a bug in your compiler, and you should report it as such!
If there were a clearly-more-efficient query plan possible to generate for a query of a particular data model in an RDBMS, and your RDBMS failed to come up with said query plan, you'd consider that a bug in your RDBMS, no? Well, same thing here.
Hard to see the upside there. By adding another variable (y in the example), you've added another degree of freedom to your program state, arguably for no good reason. Now there's something else to go wrong that wasn't there before.
It's still very much an issue for a certain compiler vendor without full support for C99.
I believe I didn't want to scare readers away. But more importantly, functional programming is more than just removing the assignment statement. If you take a language and just remove the assignment statement, the language you get isn't functional, it's just crippled.
Functional programming involves other techniques to compensate. Higher order functions are critical, and often used to hide recursion under the rug (see map & reduce). I have a more complete guide to functional programming there: http://loup-vaillant.fr/tutorials/from-imperative-to-functio...
Something interesting that's happened since then: I've taken a job at a Python company, and I've noticed that, compared to JavaScript, assigning intermediate results into local variables seems to be much more idiomatic in Python for some reason. This is doubly surprising since JavaScript at least has the const keyword. I'm not sure yet how I feel about it.
One other anecdote: I personally relax my usage of assignment a lot more when using Rust, because a) it has rich, deep immutability at a type level, and b) its expressions tend to be longer than those in other languages due to all the monads and such.
I did like the article and how the author writes so simply. To the author's credit, right below, they do have the section on "When you can't help it", which if you do need insertion like that and performance matters, probably best to use a mutable data structure.
Correct me if I’m wrong, but isn’t this plain wrong? You are increasing the memory required for your program to run, and if the data saved to those variables are sufficiently large you can run into memory issues this way.