1 min read

Writing code to be spoken

A personal favorite design element that APL allows me to explore is the "lyricality" of a given piece of code. I ask myself how much the natural language description of what a piece of code does would map directly to the code. This is a sort of discussion that simply doesn't come up in other general purpose programming languages very often. Here's an example from my compiler.

Pass Overview: Count the Rank of Indexing Expressions

Description #1: Store the count of the rank of indexing expressions in the n field of each indexing expression.

Description #2: Store in the n field of each indexing expression the number of children it has.

Description #3: For each child whose parent is an indexing expression increment the parent's n field by 1.

Description #4: For nodes whose parent is an expression of index kind increment the parent's n field by 1.

Description #5: Increment the n field of the parent for nodes whose parent is of type 2 and kind 3 for each node by 1.

Each of these descriptions becomes more an more precise until it is precisely describing what needs to be done. Now, let's look at a version of the code

n[p⌿⍨(t[p]=2)∧k[p]=3]+←1

Now let's see how that code maps to the description:

n[                    p⌿⍨                           (t[p]=2)
Increment the n field of the parents for nodes whose parents are type 2

∧   k[p]=3 ]+←           1
and kind 3 for each node by 1