An Implementation of A
Vector 18:2, page 14
Transcribed from the printed issue; not yet reviewed. Read it in the PDF of the issue, page 14
A is best described as a reduced instruction set APL. It was designed and developed by Arthur Whitney around 10 years ago as a super-fast engine for manipulating timeseries data. It was subsequently developed as A+ by Morgan Stanley and has now been placed into the public domain under the GNU public licence. This full version includes X-window support and runs under various flavours of Unix.
The A Project at the British APL Association HTTP://www.vector.org.uk/aflat/¶
In spring of 2001 it was agreed to allocate funds to investigate an implementation of the core A interpreter under Windows 9x and 2000. This phase of the investigation is now complete, and the interpreter is available for download from these pages. Please feel free to try it out, but be aware that the environment is totally primitive and that you will have to be a little persistent to achieve more than ‘2+2’ in console mode. You might like to start with some of the simple scripts on the Getting Started pages of the website, which include a translation into A of some functions from Norman Thomson’s excellent APL for the Mathematics Classroom. Any components with Unix dependencies (such as the .sys context) are not available in this version.
The site reproduces a subset of Morgan Stanley’s original A+ documentation (available in full at www.aplusdev.org), with acknowledgement of copyright where appropriate. Some sections (relevant only in a Unix environment) have been pruned. You will need the KAPL TrueType font installed to read the sample code in your browser. This font and the entire site content are available as ZIP files from the downloads page if you would like to read it offline.
Current Status of the A Project¶
Where it all began¶
The A engine was placed into the public domain late in 2000, and caused considerable interest in the APL community. In February this year, I contacted Morgan Stanley to sound them out on attempting a Windows version of the interpreter. On Monday 12th Feb 2001 I received the following reply …
"Morgan Stanley is very supportive of your plan to sponsor such a project. The only ‘restriction’ we have (as part of the GPL) is that your source code should be made available for some period. Building a wrapper around A+, and giving it away is a great idea."
The BAA committee was sounded out and approved a budget to cover the programming time to investigate the feasibility of porting the interpreter. Causeway agreed to do the work – giving us 5 days to read, understand and compile Arthur’s code. This proposal was reported to the AGM in May and again raised a good deal of interest among the membership.
The painful part¶
Reading other people’s code is never easy, and reading Arthur’s C code is almost impossible. When Causeway finally made a week of clear time to attack this one, we decided to leave the core A code well alone and concentrate simply on stripping off anything that failed to compile until it produced an EXE that could run under Windows. This was easier said than done – it actually took the best part of 2 days to get it compiling under BSD Unix, then it was mostly a case of:
WHILE finished = false DO
if compile_link = true
finished = true
else
comment out failing code using #ifdef's
endif
endwhile
The end result being a console-mode interpreter with a small supporting DLL (also freely distributable) to cover the fact that the console thinks it is running in a Unix shell!
What we have now¶
As far as we can tell, we have a functional A engine, including basic system facilities, for example you can run script files. Some system functions (such as _fmt) are missing and will be tracked down eventually. The .sys context is not included as it is strongly dependent on the Unix emulation layer, which we intend to shake off as soon as possible. A already supports ‘ASCII’ mode input so you can fire up the interpreter and type:
What you cannot do in the console is enter (or see) APL symbols. Also you cannot use the ‘up arrow’ to modify or re-execute prior input lines. If you want to do anything interesting, you must write it as a script!
As part of the download set, you will find a very basic editor to allow you to write scripts with proper APL symbols, and type the APL characters using the familiar unified keyboard. Of course you can also use Notepad and write your scripts in ASCII mode if you prefer. Some simple scripts are included, for example you can make prime numbers with the sieve of Eratosthenes by running:
Which compares the creation of primes up to 500,000 by a recursive or old-style looping method. The script looks like this
⍝ Experiments with primes to give us a rough speed check
⍝ This is a direct rendering from 'D' as per Dyalog dynamic fns
L eratos R : {
⍝ Primes by the Sieve of Erastosthenes (see Scholes, Vector 13.2 p88)
⍝ This one uses recursion and seems very fast!
next←1↑R;
mask←×next|R;
if (∧/1↓mask) ←L,R; ⍝ Return result
(L,next) & mask/R ⍝ Call self for the next step
}
⍝ Now let's try the same algorithm 'old style' with a while loop
Eratos n : {
⍝ Primes up to n (the boring way)
primes←2↓⍳n+1;
max←n*÷2;
div←2;
divs←div;
while (div<max) {
mask←×div|primes;
primes←mask/primes;
div←1↑primes;
divs←divs,div
};
(¯1↓divs),primes
}
It is interesting to compare A with Dyalog APL dynamic functions – both have lexical scoping and automatic localisation. In fact the only major difference seems to be that in A you have to name your arguments! There is also a strong affinity with the I-APL ‘direct definition’ approach to functions – it is remarkably easy to translate the functions from Norman Thomson’s book APL Programs for the Mathematics Classroom into A.
Where next¶
Having proved that A can be made to run under Windows, the next stage is to decide what should be done with it. The initial idea was to make something which could be used in schools and colleges as an experimental environment for science and mathematics, in fact very much the same idea as the I-APL project which provided a free interpreter on the BBC Microcomputer for schools. For this to make any impact on schools in the UK, it has to support the National Curriculum and come accompanied by appropriate teaching material. Norman’s book was written with I-APL in mind and he has agreed to consider a ‘translation’ to A which would make it an excellent starting point. However, I feel we need one more step before we can properly research this possibility.
The interpreter itself should be buried as a DLL (hence losing the last vestiges of its Unix heritage and that dreadful console) and so a suitable ‘experimenting environment’ will be needed to call it. In the first instance this can just be a ‘session’ with proper keyboard support and an APL font. Remember that this is emphatically not a Windows application development tool – what is needed is somewhere you can write, save, and run simple functions to manipulate and display numbers. Creation of suitable output files (probably in CSV format) or simply clipboard support, will be sufficient to permit transfer of results to Excel for graphing and further analysis.
Over to you¶
Please download the interpreter, play with some scripts and give us feedback. This is an extremely powerful APL engine, and Morgan Stanley have done the world APL community a huge service in making it publicly available. The challenge now is to make it useful in a way which does not threaten the commercial APL vendors, and which will be attractive to both hobby APLers and the educational world. To a great extent, the blossoming of APL in the 1980s was due to IBM’s far-sighted approach in giving away VS APL licences to universities - with A we at last have the opportunity to do the same thing with a surprisingly modern-looking APL engine.