Generalised variables with value types
I’ve not posted in a while, but I’ve been designing (for fun) a pedagogical programming language with the characteristics I talked about in The Little Compiler’s Predicament. One of the problems I’ve since identified as a source of possible pitfalls is the implicit reference type: in most object-oriented languages objects are linked into composites by reference, which means you get the following behaviour (here in Python),
xs = [[]] * 3 # [[],[],[]] xs[0] += [1] # [[1],[1],[1]]
This is because each [] is actually the same list, thrice. The result doesn’t give you an opportunity to learn what’s wrong, the program is just clearly broken. I even get bitten by this fairly regularly in dynamic languages; you have to start worrying about shallow copying, deep copying, API object ownership, and many other incredibly complicated problems that are a property of the language and not the concepts of pure computer science.
The solution is to make reference types explicit, defaulting to value types that embed directly into the composite. With these semantics we would get a result like the following (in pseudo-Python),
xs = [[]] * 3 # [[],[],[]] xs[0] += [1] # [[1],[],[]]
Which is clearly what was intended. (The first result could be achieved with explicit references.)
Though the confusion starts here, the drawbacks to implicit referencing extend far beyond, affecting the entire flavour of the language. Suppose we have a list of (references to) objects and we want to find one that matches a given predicate and replace it with a new object reference. In Python we’d say (assuming we had a higher-order find_index function),
xs[find_index(xs, pred)] = 5
Here the index acts as a kind of reference to the matching element, qualified relative to the list, so having acquired this reference we can replace the element with a new value reference. Note that if we only wanted to get the element, not to replace it, we would use another function find as we would not need its index. This is true for any method of retrieval from a composite: we need both ‘getter’ and ‘setter’ methods.
Common Lisp attempted to solve this problem with generalised variables. The macro setf would allow you to invert a ‘getter’ function and use it as a ‘setter’:
(find xs #'pred) ; get (setf (find xs #'pred) 5) ; set
This works by requiring that the programmer providing an inversion function for each ‘getter’ with the defsetf macro, thus converting find into what is called a place. But because setf is but a macro places only exist within its borders; the following would not work as intended.
(let ((x (find xs #'pred))) (setf x 5))
It is really quite a hack. But consider that with a fully value typed language, where objects are embedded directly into a composite, each object exists in only one place. Given any object from any source we know where we would need to replace it because it can only possibly exist in that one place. This means that with absolutely any function that returns an object it is implicitly invertible, even without a defsetf equivalent. In the language I am designing there is a sharp distinction between variable definition and value assignment; one defines a variable and can then assign values through that variable so as to modify the underlying composite.
let x = xs.find(pred) x := 5
Or equivalently,
xs.find(pred) := 5
These semantics are trivial once you rid the language of implicit reference types, and they result in much simpler code without any of the hassle of juggling list indices. This also works just as you would expect when you add explicit reference types. More power, less confusion.