The BEAM needs an APL-y language

Gordon Guthrie
6 min readApr 9, 2018

--

APL — yikes, scary innit?

One like, one unpopular opinion is one of them top Twitter games, innit?

Well this is mine — the BEAM, which already has Prolog-y (Erlang), Algol-y (Elixir) and Lisp-y (LFE) languages needs an APL-y language wut looks like the pic at the top of this post.

APL — which stands for Another Programming Language, imagine the brass neck of that, eh? — is an old 60s language designed to be expressive.

Why in the name of God and all the Saints?

Good question — lets start with why before we tuck in. The only reason to write a new programming language is to be able to do things better/faster.

Way back in the day I was hired at Basho and had to do their coding challenge. As the company is bankrupt now, lets see it:

A version vector is a concise representation of events for tracking changes to a data item in a distributed system. You can read more
about them here (http://en.wikipedia.org/wiki/Version_vector), and here (http://haslab.wordpress.com/2011/07/08/version-vectors-are-not-vector-clocks/).
Attached is a file. On each line is a two-tuple containing two version vectors {VC1, VC2}. The version vector is modeled as a list of two-tuples, VC=[{ActorId, CountInteger}]. The first entry is an actor in the system, in this case an 8 byte Erlang binary <<A,B,C,D,E,F,G,H>>. The second entry is a positive integer that denotes the latest event by that actor that the clock summarizes.Version vector A descends version vector B if every event summarized in B is also in A. Version Vector A and B are equal if A descends B and B descends A, A and B are concurrent if A summarizes events not seen by B and B summarizes events not seen by A.For the provided file, write a program that outputs a row in a table for each pair of vectors such that each column outputs true or false, where A is the first clock in the tuple, and B is the second.A descends B | B descends A | A equals B | A is concurrent with Bfor example:{[{<<199,214,110,13,247,118,16,223>>,1}],[{<<0,88,3,113,66,198,7,52>>,1},{<<199,214,110,13,247,118,16,223>>,1}]}generatesfalse | true | false | falseas the first version vector is an ancestor of the second version vector.Program in the language of your choice. You may use external libraries of your choice. Please provide us with the program, instructions on how to run it, and the resulting output.Please email us if you need further clarification. Good luck!

My solution is online now — basically 500 raw lines of code in total, including tests. So what would it look like in APL? Something like this (forgive me the gods of APL, for I is a noob):

Vector1 ← 3 2 ⍴ 'a' 41 'b' 42 'c' 43
Vector2 ← 3 2 ⍴ 'a' 12 'c' 43 'd' 99
IsSameShape ← {^/((⍴⍺) = (⍴⍵))}
... (need an If here)IsEqual ← {+/ (⍺[;1] = ⍵[;1]) ^ (⍺[;2] = ⍵[;2]) }
Actors1 ← Vector1[;1]
Actors2 ← Vector2[;1]
Shared1 ← Vector1[(Actors1 ∊ Actors2) / ⍳⍴Actors1;2]
Shared2 ← Vector2[(Actors2 ∊ Actors1) / ⍳⍴Actors2;2]
IsUnshared ← ^/(Actors1 ∊ Actors2)
... (need an If here)DoesDescend ← ^/ ((Shared1 ⌈ Shared2) = Shared1)

(you can’t do flow control in the in-browser REPL I wrote this in, sooooo…. This version is also probably a bit too long and real APLer could do it shorter.)

What’s yer takeaway son? What does it all mean?

The APL programme is/would be much smaller than the Erlang.

APL is designed to be so expressive that whole programmes can be written on a single page and their structure grasped by the programmer. This prezzo makes the case for APL ably and without it I wouldn’t have written this blog post. Do read it — it blew my mind.

The coding example is a key part of CRDTs (Convergent Replicated Data Types) and a swatch at one of those CS papers might give a clue as to why it is so short in APL, it all set-based maths:

Extract from a CRDT paper https://hal.inria.fr/inria-00555588/document

APL is designed to be expressive — and exposes many set operations to the developer — it is quick to write CRDT-like things because it is designed that way. Once you understand the seemingly bizarre syntax it is actually a very simple language.

Bear in mind my APL experience is reading the manual for 4 hours on a flight, and programming for another 4 so: 1 days experience tops.

By comparison when I did it in Erlang I had 11 years full time under my belt.

So are there other use cases?

Riak has a number of other gnarly problems which have elegant statements of requirement but whose implementations have considerable accidental complexity when written in Erlang.

Some promising places a BEAM APL-y language could help might be:

  • the claim algorithm — which handles placement of vnodes on the cluster
  • preflists — where to do to to read data from the cluster, particularly for stuff like rack-awareness and guaranteed writes to multiple data centres and stuff
  • ring resizing — how to restructure the data distribution on a cluster dynamically

What was APL used for when it was used in programming?

A core technique in APL is taking two sets of information and generating an boolean matrix with them, and then apply a set of rules to reduce that boolean matrix to a single boolean — a decision, yes/no. Essentially it is a business rules language.

My dear friend Mary Kane informs me that when she left school to join Irish Life Insurance she was trained as an APL programmer — taking sets of actuarial tables, apply an individuals data to them and reducing the resulting boolean matrices to business decisions, buy/sell, pricing, calculate returns, etc, etc.

This pattern will be familiar to spreadsheet users. Spreadsheets as a business function are largely large sets of static data which are mapped to boolean matrices in the presence of scenario data and then reduced to decisions.

The famous paper on what functions spreadsheet users actually use makes this very clear. They dumped every spreadsheet at a large company and a government department and then finger-printed function use in them. The results are stark: sum, vlookup and if for the win:

https://arxiv.org/abs/1111.6866

So basically turning spreadsheets into business programmes then. (A subject that some of you will know has long been close to my heart.)

Gonnae write an APL language for the BEAM then?

Jeezo, naw, Ah’m run aff ma tits.

Sometimes you are just pregnant with a whole programme — you can see its implementation suddenly, as from a blue sky, and you just need to vom it out through your fingers — so it is with this APL dialect.

But I really have too much on my plate with a new job and all to do such a thing. If anyone fancies it I could write up how I would do it ;-)

If you like this share it on social media and follow me on Twitter

--

--

Gordon Guthrie

Former SNP Parliamentary Candidate — Quondam Computer Boffin