RPL.DOC by Joe Horn People keep asking, "What Exactly *Is* RPL, Anyway?" RPL is not an "interpreted" language like early implementations of BASIC (the slowest way of doing things), nor a "tokenized" language like HP BASIC (speeds up the interpreter), nor a "p-code" language like PASCAL (a little better than an interpreter) nor a "compiled" language like FORTRAN or C (the best a high-level language can get). It's instead a "threaded" language like LISP and FORTH. What does that mean? When you type a program, the command names that you typed are not stored; nor are "tokens" that correspond to them in some sort of lookup table. Instead, only the *memory addresses* of each command are stored in your program. Running RPL programs is therefore very efficient; the HP 48 just does a "gosub" to each address. No time is wasted looking up command names or token addresses. Of course, each of these addresses may in fact be just another program full of jumps to other addresses, which in turn may be programs... and so on. A return stack keeps track of everything, and unlike the HP-41 and all earlier HP programmable calculators, the return stack is not a fixed size, but can grow as needed. Obviously, however, this "thread" must bottom out at some sort of primitive, non-threaded programs, and these are either written in machine language, or are data objects which merely place themselves on the stack (like pi). A good question is: If RPL programs only contain memory addresses, how can they be edited and printed? When you edit or print a program, the HP 48 looks at the memory immediately preceeding each address to find out the command's name, and displays/prints that. Of course, this assumes that all commands have names. Now, what would happen if HP included a command in the operating system, but forgot to give it a name? Then it would work okay inside an RPL program, but you couldn't edit that program, print it, or type that command on the command line. Guess what: the HP 48 has well over two thousand commands that have no names. A distinction must now be made between two kinds of RPL: User RPL and System RPL. User RPL is the language that the Owner's Manual tells you about, the language you see on the keyboard and in the menus, and which Joe Average uses; it's "the << >> language". But this is only a small subset of a much richer language with a vocabulary thousands of words larger, called System RPL. Unfortunately, all the System RPL words are "nameless objects", which means that they cannot be edited or listed. Any attempt to edit or list a System RPL program usually results in "External" being displayed (the default name for a nameless object), but sometimes results in other displays, or even system malfunction. If it's such a pain, why write in System RPL? Because it runs way faster and it's more powerful than User RPL. Most of the nifty features of the HP 48 operating system itself are written in System RPL. But how can you write System RPL if the objects have no names? Well, they have no names inside the HP 48 itself, but they do have names; the design team of the HP 48 has "in-house" names for all of them. (It's sorta like the parts of your body for which you don't know the name; they do have names, even if the names don't happen to be stored in your brain.) For example the System RPL program at address #30794h, which pushes the version string of your HP 48 onto the stack, is called VERSTRING by HP, but that name is not stored in the HP 48 anywhere. The name is stored in external software. For example, this User RPL program (which yields your ROM revision letter): << #30794h SYSEVAL 8 8 SUB >> (33 bytes; .020 seconds to run) can be written in System RPL this way: :: VERSTRING EIGHT LAST$ ; (12.5 bytes; .011 seconds to run) The HP 48 development team used computers to write HP 48 "programs" using these in-house names. The computers then used huge lookup tables to translate them and create real HP 48 downloadable programs. The entire operating system (256K of code!), the HP Solve Equation Library Card, and several third-party commercial software packages were developed entirely on computers this way. And you can do it too! HP has made a System RPL Development Tools package available. Get TOOLS.EXE from your favorite ftp site, or order Goodies Disk #4 for $4.95 from EduCALC. Everything you need is there, including well-written documentation for it all. But the really exciting news is that you don't even need a computer to write System RPL any more (and you don't have to be a genius hacker with a prodigious memory and infinite patience, like Alonzo Gariepy or Rick Grevelle, either!). Detlef Mueller and Brian Maguire have independently developed libraries that effectively replace the built-in User RPL command names with HP's in-house System RPL names! So instead of writing IF < THEN SWAP DROP ELSE ROT DROP END in slow User RPL, you can write the equivalent %< ITE SWAPDROP ROTDROP in fast System RPL, right from the HP 48 keyboard! The DETLEF subdirectory on this disk contain's Detlef Mueller's complete System RPL development system and library creator. It almost obsoletes Goodies Disk #4! Check it out. Here's a sample of useful System RPL which I just finished today, developed entirely on the HP 48 keyboard. It sorts a list of real numbers into ascending order, and it's *fast*, over 400 times faster (!) than HP's SORT program in the Owner's Manual (in User RPL, of course) on a list of 100 numbers: :: CK1&Dispatch # 5h :: INNERCOMP 1LAMBIND 1GETLAM #1+_ONE_DO 1GETLAM ROLL # 0h INDEX@ BEGIN 2DUP SWAP#- #2/ DUP#0<> WHILE :: OVERSWAP #-DUP #4+PICK 5PICK %> ITE ROTDROPSWAP SWAPDROP ; REPEAT ROT2DROP UNROLL LOOP 1GETABND {}N ; ; It sorts 100 random numbers in 4.5 seconds! (HP's takes half an hour!) And it's *shorter* than HP's SORT program, too, at only 90 bytes! Golly. Unfair comparison, I know; it's the worst algorithm in the most readable User RPL versus a better algorithm in almost-unreadable System RPL. But *still*, 400 times faster??? Geez! [The sorter above is on this disk, called "SORTLN", in the PROGRAMR subdirectory. -jkh-] But the benefits of System RPL are more than just saving time and memory. It also allows you to do things that User RPL simply cannot do, like work with Library Data objects, do math to 15-digit accuracy, use arrays containing nonnumeric objects, treat programs as if they were lists (PUT, GET, explode onto the stack, and reassemble from the stack), create HP-41-style prefix operators (e.g. TONE 8), peek and poke directly into memory, and more. If you know User RPL, learning System RPL won't take long or be difficult. It's just a new vocabulary, not a new way of thinking. It's *real* RPL. User RPL System RPL 1. To use a command, you just The commands have no names and can type its name. The HP 48 only be used with SYSEVAL or by recognizes the name because directly embedding the "External" it's in the built-in list of code in a program using various command names. System RPL programming tools. 2. The commands are safe to use The commands are dangerous to use because they have built-in because they assume the stack has argument type checking. the correct arguments on it, and may wipeout if not. 3. Slower (because of #2) Faster (because of #2) 4. You can view, print, and edit The only way to view, print, or edit programs directly on the HP 48 programs is with System RPL without any extra software developer's software in the HP 48 or needed. on a computer. 5. You can only handle a few of You can handle all 29 object types the HP 48's object types. directly. 6. You can only do "supported" You have total control of the HP 48 things in the Manual. and can do "impossible" things. 7. Impresses the natives. Impresses hackers. -jkh-