TITLE: Doing Concatenative Programming in a Spreadsheet
AUTHOR: Eugene Wallingford
DATE: June 17, 2020 3:53 PM
DESC:
-----
BODY:
It's been a long time since I was excited by a new piece of software
the way I was excited by
Loglo,
Avi Bryant's new creation. Loglo is "LOGO for the Glowforge", an
experimental programming environment for creating SVG images. That's
not a problem I need to solve, but the way Loglo works drew me in
immediately. It consists of a stack programming language and a set of
primitives for describing vector graphics, integrated into a spreadsheet
interface. It's the use of a stack language to program a spreadsheet
that excites me so much.
Actually, it's the reverse relationship that really excites me: using
a spreadsheet to build and visualize a stack-based program. Long-time
readers know that I am interested in this style of programming (see
Summer of Joy
for a post from last year) and sometimes introduce it in my programming
languages course. Students understand small examples easily enough,
but they usually find it hard to grok larger programs and to fully
appreciate how typing in such a language can work. How might Loglo
help?
In Loglo, a cell can refer to the values produced by other cells in the
familiar spreadsheet way, with an absolute address such as "a1"
or "f2". But Loglo cells have two other ways to refer to other
cell's values. First, any cell can access the value produced by the cell
to its left implicitly, because Loglo leaves the result of a cell's
computation sitting on top of the stack. Second, a cell can access the
value produced by the cell above it by using the special variable
"^". These last two features strike me as a useful way for
programmers to see their computations grow over time, which can
be an even more powerful mode of interaction for beginners who are
learning this programming style.
Stack-oriented programming
of this sort is
concatenative:
programs are created by juxtaposing other programs, with a stack of
values implicitly available to every operator. Loglo uses the stack
as leverage to enable programmers to build images incrementally, cell
by cell and row by row, referring to values on the stack as well as to
predecessor cells. The programmer can see in a cell the value produced
by a cumulative bit of code that includes new code in the cell itself.
Reading Bryant's description of programming in Loglo, it's easy to see
how this can be helpful when building images. I think my students
might find it helpful when learning how to write concatenative programs
or learning how types and programs work in a concatenative language.
For example, here is a concatenative program that works in Loglo as
well as other stack-based languages such as Forth and Joy:
2 3 + 5 * 2 + 6 / 3 /
Loglo tells us that it computes the value 1.5:
This program consists of eleven tokens, each of which is a program in
its own right. More interestingly, we can partition this program into
smaller units by taking any subsequences of the program:
2 3 + 5 * 2 + 6 / 3 /
--------- ------- ---
These are the programs in cells A1, B1, and C1 of our spreadsheet. The
first computes 25, the second uses that value to compute 4.5, and the
third uses the 4.5 to compute 1.5. Notice that the programs in cells
B1 and C1 require an extra value to do their jobs. They are like
functions of one argument. Rather than pass an argument to the function,
Loglo allows it to read a value from the stack, produced by the cell to
its left.
By making the intermediate results visible to the programmer, this
interface might help programmers better see how pieces of a concatenative
program work and learn what the type of a program fragment such as
2 + 6 / (in cell B1 above) or 3 / is.
Allowing locally-relative references on a new row will, as Avi points out,
enable an incremental programming style in which the programmer uses a
transformation computed in one cell as the source for a parameterized
version of the transformation in the cell below. This can give the novice
concatenative programmer an interactive experience more supportive than
the usual REPL. And Loglo is a spreadsheet, so changes in one cell
percolate throughout the sheet on each update!
Am I the only one who thinks this could be a really cool environment
for programmers to learn and practice this style of programming?
Teaching concatenative programming isn't a primary task in my courses,
so I've never taken the time to focus on a pedagogical environment for
the style. I'm grateful to Avi for demonstrating a spreadsheet model
for stack programs and stimulating me to think more about it.
For now, I'll play with Loglo as much as time permits and think more
about its use, or use of a tool like it, in my courses. There are
couple of features I'll have to get used to. For one, it seems that a
cell can access only one item left on the stack by its left neighbor,
which limits the kind of partial functions we can write into cells.
Another is that named functions such as rotate push themselves
onto the stack by default and thus require a ! to apply them,
whereas operators such as + evaluate by default and thus
require quotation in a {} block to defer execution. (I have
an academic's fondness for overarching simplicity.) Fortunately,
these are the sorts of features one gets used to whenever learning a
new language. They are part of the fun.
Thinking beyond Loglo, I can imagine implementing an IDE like this for
my students that provides features that Loglo's use cases don't require.
For example, it would be cool to enable the programmer to ctrl-click on
a cell to see the type of the program it contains, as well as an option
to see the cumulative type along the row or built on a cell referenced
from above. There is much fun to be had here.
To me, one sign of a really interesting project is how many tangential
ideas flow out of it. For me, Loglo is teeming with ideas, and I'm not
even in its target demographic. So, kudos to Avi!
Now, back to administrivia and
that database course...
-----