|Column Tag:||APL Vectors
APL Returns to the Mac
By S. Allyn Weaks, Contributing Editor, Seattle, WA
STSC APL*PLUS, Other APLs, and Some Utilities
Nearly two years of being distracted is a record even for me, but Ive started in on APL again, and with some luck maybe I can keep it up for a while this time. A lot has happened in the last few months - there are now four different APLs for the Mac, three commercial and one shareware. PortaAPL has become STSC APL*PLUS, APL.68000 has finally been released, MacAPL is available for those on a budget, and a shareware APL from France, APL 90+ has turned up. This month Ill give brief descriptions of each them, with detailed coverage of STSC APL, which Ive used the most.
APL.68000 ($295, Spencer Organization, Inc., 366 Kinderkamack Road, P.O. Box 248, Westwood, NJ 07675, (201) 666-6011 ) is competing directly with STSC as a full powered language with at least some access to the Mac toolbox. Unfortunately I havent been able to see it, and since it has a $295 list price I cant afford to buy it just for fun. But the rumors on the net are that its fast since its written in assembly, and solid. It follows the ISO standard except for the file functions- they use some made up symbols instead of the usual quad functions ( FTIE, FCREATE), which not only makes programs harder to read, but complicates porting programs in both directions. They get into more of the Mac toolbox than APL*PLUS does, real windows and dialogs for example. Ill try to get a good look at it soon so I can give you more information.
MacAPL ($125, Leptonic Systems Co., 405 Tarrytown Rd. #145, White Plains, NY 10607 ) is a relative newcomer, written by Michael C. OConner, author of Layout Editor. It doesnt yet support the entire language, and it doesnt access the toolbox, but it has some very nice features. It provides a run-time system, so you can distribute stand-alone applications, and it has the nicest user interface of the lot. You can open more than one workspace at a time and the editor is done properly. There is a function to put text and pictures into the clipboard. A demo version that can do everything but save a workspace is available on Compuserve, Delphi, and in many public domain libraries. I was surprised at how fast it was - it did my harmonic benchmark (+/÷´10000) in 23 seconds. Its well worth getting hold of a copy of the demo just to get a little of the flavor of APL.
APL 90 ($85 shareware, SYNC, 12 Place Hotel de Ville, 42000 Saint-Etienne France) is a straight APL from France. It is ported from a mainframe APL written in C at the Ecole des Mines de Saint-Etienne by Jean-Jacques Girardot, François Mireaux and Sega Sako, and is full APL, conforming to the ISO standard. It has a few Mac features, including a row of buttons along the bottom of the screen to change keyboard (ASCII vs. APL), debugging (NONE, TRACE, Macsbug) and to show the time and cursor position. Unfortunately there isnt a full-screen editor; you have to use the standard clumsy APL del-editor. If youre interested in doing number crunching and dont care much about immediate access to the toolbox, this is a good one to look at. Girardot plans to add APL2 features (including nested arrays, ambivalent functions, machine language functions, as well as some object-oriented extensions) by the end of the year. Since APL 90 comes from France, it has a few keyboard problems - they very kindly put in a UK keyboard option, but not a US option. I havent bothered to patch the keyboard resources for my copy yet, since I dont use it regularly, but it shouldnt be hard to do. One warning - the 1.0e version that I have is incompatible with System 4.1.
STSC APL*PLUS ($395, STSC Inc., 2115 East Jefferson St., Rockville, MD 20852) is the new name for PortaAPL. PortaAPL still lives as a company, but has licensed its Mac APL to STSC. PortaAPL still does all of the programming, and is bringing the interpreter into line with STSCs other APLs for IBM-PCs and mainframes, while STSC handles documentation, marketing, support, and utility libraries.
The package you get contains a 200 page Users Guide, 300 page Reference Manual, an APL tutorial (APL is Easy), key stickers to help you learn the keyboard layout, and two disks, one with the interpreter and the other with several utility workspaces. Price is $395 list, with educational discounts. The license agreement is the standard We have all of the rights, you have all of the responsibilities variety. Registration entitles you to free telephone support (but its not a toll-free phone number), and a quarterly newsletter. The phone support has been mostly helpful. They are very good at answering APL questions, but have more trouble with Mac specific problems. The people Ive talked to have all been IBM-PC types.
The documentation is basically OK. The books are well laid out and easy to read. The Reference Manual has an excellent APL language summary that I wish Id had available when I was first starting, and the Users Guide can easily be read through in a couple of sittings to get an overview of the system. The organization isnt great though. They went to some effort to not duplicate any information, and the indices arent complete. You have to go from one book to another a lot to find out enough about some things to use them. And some topics are just plain hard to find; for example, the SFOPEN and SFSAVE functions for invoking the Standard File package arent anywhere under files, but are listed under dialogs - an obvious confusion on their part about what dialogs actually are. All of the appendices in the Users Guide (the atomic vector, error messages, system limits) really should have been in the Reference Manual. The level of explanation is somewhat uneven as well. There is gory detail about what a file is, but youre expected to already know about forks and resources. Even the name of the program is confusing at times - they refer to the STSC APL*PLUS System file often, and they mean the interpreter, not the Mac System file.
The tutorial book APL is Easy isnt very useful. I found the tone of it to be patronizing, and almost every example is business related. (My brain clicks off whenever I see words like amortization and sales.) It is written for the IBM-PC version of APL*PLUS, with a page or so of corrections for the Mac, and the highlighting of text that you should type in is distracting. Buy a copy of Gilman and Rose instead (APL: An Interactive Approach, Leonard Gilman and Allen J. Rose, John Wiley & Sons, ISBN 0-471-09304-1). They have business examples too, but they throw in enough math and computer science to keep it interesting.
The Mac interface is a little better than before, as is the editor. The full screen editor now has a horizontal scroll bar for long lines, and you can cut and paste more than one line at a time. There is no search function, so changing a variable name can be tedious. Cursor placement is tricky; if you click once between characters, the character to the left is highlighted, and I mangled a lot of text by typing too fast until I learned to click on the letter to the right. Double-clicking doesnt select a word as it should, either. The scroll bars are present in the main interpreter window even though they arent active, and you cant make them go away. They reduce the useful area of the screen, and look untidy when doing graphics. You can still only cut and paste one line in the main window.
The font is better (it no longer conflicts with San Francisco), but isnt perfect. There are three sets of APL fonts (narrow, wide, and italic) and a downloadable Laser font, but since each family has the same ID numbers, you cant have them all installed at once. You have to use the Config workspace functions to change the default family and size, and the change doesnt take effect until the next time you load the interpreter. The font size you choose affects allowable values for the WINDOW function, which causes programming problems. Worse, if you install the font in your system file so that you can edit stuff in MacWrite, etc. the keys you need to get the APL characters are wildly different from what you use in APL. Why they couldnt arrange things to be consistent is beyond me. Id fix it with a font editor, but that would complicate transferring files to friends.
Toolbox access hasnt changed appreciably from the older PortaAPL version. Quickdraw, menus, mouse functions and some resource functions are still there as utility workspaces. What STSC refers to as a window is more appropriate to an IBM computer - just a restricted region of the screen.
The keyboard layout is very nice. You can choose between a standard APL layout, and the unified arrangement, in which the APL characters are typed in with the option key. Lowercase is supported for object names, which is a great improvement over the standard underscored characters.
Mainframe versions of APL*PLUS have many of the APL2 extensions (newer editions of Gilman and Rose cover APL 2 and other common extensions of APL), and I had been particularly hoping for nested arrays. But nested arrays are memory hogs, and STSC is trying to keep the microcomputer versions as compatible as possible. Intel processors just couldnt handle the load until recently with the 386, so well have to wait for awhile. Nested array are on the list though, so with luck we may have them before too long. They do provide dyadic up- and down-grade, which makes alphabetizing in any order you chose extremely easy, and a few other APL2 functions.
The stability of APL*PLUS is pretty good. If you ever use desk accessories, you should set the HEAP_SPACE value with the Config program to at least 100k. I had some crashes until I figured out that APL allocates as much memory as possible into one big non-relocatable block in order to handle large arrays. HEAP_SPACE tells APL how much memory to leave free for the system, printer divers, and desk accessories. The default value is set to only 40k, which is barely enough for the laser printer driver or small DAs. On a Mac Plus, 100k is a good value; on a 512k machine, 70k is a reasonable compromise that still gives you a 180k workspace.
Of the three bugs Ive found, one is minor; the SOUND function timing is from 20% to 80% too long, and the variation is random. This makes music programming impossible, but the control SOUND allows (pitch, volume and duration, and only one voice) is too limited for real music anyway. The other two are more serious. One is a SFSAVE problem. If you try to create a file that already exists, the Do you want to replace? dialog comes up normally, but NCREATE will refuse to create the file, giving an filename error. This can be worked around by trapping the error, deleting the old file, then continuing on, as I do in the utility functions below. The third bug is the most serious: NTYPE, which gives a file type and creator to a Mac file, crashes the system randomly. It crashes much less often (about half the time instead of 90% of the time) if TRACE is turned on. STSC knows about the bug, and it should be fixed in the next version. Meanwhile, there is no workaround; you must use a utility such as Disktop, or FileInfo to manually set the type and creator.
Overall, I like APL*PLUS. The Mac interface needs a lot of work, and I have a list of quibbles, but its useful and fun, and mostly works as advertised. The support is good, and I think that new versions will fix the problems and add more goodies (nested arrays, please!!) [Note: APL*PLUS does not work on a Mac II, although it is rumored a beta Mac II version is in the works. STSC readily admits all their effort and interest right now is on the IBM version, which is up to version 7.0, as opposed to the Mac product, which has never left 1.0!-Ed]
On to a few utilities that I wrote to help with getting information into and out of APL*PLUS. These are useful for including APL functions in a text file (like this article, oddly enough) without having to retype everything. Its possible to do this via the clipboard, but I dont care for the extra time it takes. Also, by reading and writing directly to a file, you can handle larger objects without running out of heap space. A note on one of my conventions - since lower case is now allowed in identifier names, I have used lower case exclusively for any global function name or variable. This makes it much easier to pick out my stuff from the utilities and routines that STSC provide, and that you have to leave lying around in the workspace to get anything done. And since everything except system functions are case sensitive, I dont need to worry about accidentally using one of their names.
The first, lister, is based on the APL*PLUS LISTER program which dumps a workspace to the screen. Ive changed it to dump to a file instead. It ignores all locked functions completely, and the format for the listing of variables is different. All of the local variables in the function end with a double underscore. Since local variables and functions are ignored by IDLIST (at least before you actually use a local variable), I wanted variable names that wouldnt shadow global variables. (Note that since lister itself is declared local, it will not dump itself to the file.) Im unlikely to use the double underscores under other circumstances, so lister should see everything in the workspace. lister shows up one of the big problems Ive found with APL. Redirecting I/O can be a real pain compared to Fortran. I havent yet found an easy way to say send stuff to a file instead of the screen until I say otherwise. If you write a numeric variable to a file, its sent as binary unless you explicitly tell it to be text by using the format () function.
Another complication is that since APL*PLUS is written in C, it uses newlines (line-feeds, TCNL) instead of the usual carriage returns ( TCCR) at the end of lines. This is assuming that there are any end of line markers at all - character vectors often have them, character matrices usually dont. My solution is to first find all newlines and change them to returns, then if the variable is a matrix, I append returns to the end of each row. I have a separate function replace_char to change any character into another that makes the substitution easy.
lister first finds an unused file tie number, then calls SFSAVE to get a file name. If cancel is selected, filename__ will be an empty vector, and lister will exit. If an existing file name is chosen, you have to try to create the file, trap the resulting error, then delete the old file yourself. After you have a valid file name, just call NCREATE again and continue on.
To list the functions, I use VR, the visual representation. This produces a character vector with line feeds, and gives the line numbers. If you dont want line numbers, you can use CR, the canonical representation instead; its a character matrix, and youll need to append carriage returns to each row. Variables are listed with the data type, then the variable name, and if it isnt a scalar, the shape. The data itself comes on the following lines, one row per line for dimensions higher than one.
replace_char is a general character replacement filter, and is a nice example of the membership (©) and compress (/) functions, and indexing. The right argument is a character vector whose first element is the character to be replaced by the second element. Anything else in the vector is ignored. Line 9 creates a boolean vector (loc) with ones at every occurrence of cv. ´Þ/shape creates a vector of 1 2 ... N, where N is the number of elements in the object. Multiplying by loc selects out the values that contain cv, and loc / compresses out the zeroes. loc can now be used as the array index for text. There is one problem with this algorithm: it uses a lot of memory. Iota uses 4 times as much memory as the text its working on. On a Mac+ this isnt too bad - in an empty workspace I can easily work with a 100k character vector.
The next two functions let you transport character data into and out of APL without having to use the clipboard. text_file takes a character vector or matrix, converts newlines to returns, then writes it to a file. file_text reads a text file into a character vector, converting returns into newlines. This makes it convenient to write up help screens in Mockwrite, or any other editor instead of the marginal built in editor. Character vectors take up less space than matrices as they dont have to pad out to the end of each line, but character matrices are often easier to manipulate. Note the check for available memory before actually reading the file - since file_text calls replace_char, you need to allow for the additional memory it needs.
Fig. 1 PlotDemo in APL
As an end note, some news about a BBS for APL users. BBS\APL (1-301-340-6296) is free (except for phone bills!). Now its oriented mostly towards IBM-PC users, but Im sure we can change that. The BBS is run by Murray Spencer in Gaithersburg MD, and is a rats nest to navigate around, but there are plenty of special interest groups, including specific APL systems, Amiga, and academic, so it shouldnt be hard to get them to support Macs. Give them a call and let them know that the IBM-PC is on the way out!
text file_text ;tie ;name ;size
 imports a text file to a character vector, converts CR to LF
 uses replace_char
 tie ( ® / 0, NNUMS ) - 1 get
unused tie number
 name SFOPEN 'TEXT' get filename
 ( name Â '' ) / 0 exit if empty filename
 name NTIE tie open file
 size NSIZE tie find out how big it is
 ( size WA ÷ 6 ) / MEMORY if too big, go to MEMORY
 text NREAD tie, 82, size, 0 read in the whole file
 text text replace_char TCCR, TCNL convert CR to LF
 NUNTIE tie close file
 EXIT: 0 exit
 'Not enough memory to read this file' print message to
 NUNTIE tie close file
lister ;fns__ ;var__ ;text__ ;x__ ;lister ;tie__ ;name__ ;type__
 list all function and variable names and definitions to a file
 uses replace_char
 ELX ' ERR ' trap errors
 var__ IDLIST 2 get list of variables
 fns__ IDLIST 1 get list of functions
 name__ 'Save as ...' SFSAVE 'Listing' get file name
 ( name__ Â '' ) / EXIT exit if empty filename (Cancel)
 tie__ ( ® / 0, NNUMS ) - 1 get an unused tie number
 name__ NCREATE tie__ create file
 'QED1TEXT' NTYPE tie__ assign file creator and type
 (commented out until NTYPE is
 (0 © µ fns__ ) / L4 If no FNS, skip
 L1: (0 = µ text__ VR fns__[ IO;]) / L3 If locked fn,
 text__ text__ replace_char TCNL, TCCR replace LF with
 L2: ( text__, TCCR ) NAPPEND tie__ output function
 L3: (~ 0 © µ fns__ 1 0 fns__) / L1 drop first name
 L4: (0 © µ var__) / L7 If no variables,
 L5: text__ (text__ ' ') / text__ var__[ IO;] remove
 x__ text__ get data for
 make ID line of type: [INT] variable_name 2 4 34
 type__ '[',(,(11 82 323 645 = DR x__) 4 3 µ'BITCHRINTFLT'),']
 text__ type__,text__
 (text__,' ',((0 µµ x__) / (µx__),'µ'), TCCR) NAPPEND
 (( x__) , TCCR) NAPPEND tie__ write object
 L6: (~0 © µ var__ 1 0 var__) / L5 see if theres
more to do
 L7: NUNTIE tie__ close file and
 EXIT: 0
 ERR: check for file name error
 (FILE NAME ERROR 15 ª DM) / OTHER_ERR
 name__ NTIE tie__ open file
 name__ NERASE tie__ delete file
 OTHER_ERR: displays error message and quits for any other
t text replace_char cv ;loc ;shape
 a filter to replace all occurrences of one character with another
 text can be either a vector or a matrix of any shape
 cv character to replace cv
 t returned with the same shape as the original text
 Warning: uses a lot of memory! (4 times as much as text takes
 shape µ text remember the original shape
 text ,text ravel text to a vector
 loc text © cv find all occurrences of cv
 loc loc / loc Þ ´ Þ/shape create list of positions of
 text[loc] cv do the replacement
 t shape µ text return value reshaped to original
text_file text ;name__ ;tie__ ; ELX
 writes a character vector or matrix to a file, converting LF
 a character matrix gets a CR added to the end of each row
 text can be any shape
 uses replace_char
 ELX ' ERR ' error handler
 tie__ ( ® / 0, NNUMS ) - 1 get unused file tie number
 name__ 'Save text as ... SFSAVE 'Text' get filename
 ( name__ Â '' ) / EXIT exit if empty filename
 name__ NCREATE tie__ create file
 'QED1TEXT' NTYPE tie__ give it a creator and
 text text replace_char TCNL, TCCR convert LF to CR
 ( (µµ text) ¾ 1 ) / L1 if it a vector, skip next
 text text , (µ text) µ TCCR append a CR to every row
 (text, TCCR) NAPPEND tie__ write text to file
 NUNTIE tie__ close file
 EXIT: 0 exit
 ERR: handle error to replace old file
 ( 'FILE NAME ERROR' 15 ª DM ) / OTHER_ERR
 name__ NTIE tie__
 name__ NERASE tie__
Fig. 2 Draw demo in APL