From: jhmeyers@miu.edu (John H Meyers)
Subject: Re: How to ORDER with/without HACK
Date: 21 Nov 1997 00:00:00 GMT
Message-ID: <653iac$tme$1@news.iastate.edu>
References: <650usj$b8d$2@news.iastate.edu>,<01bcf5c6$7fd55b70$1d22249e@Q270>
Organization: MIU Computer Services,  Fairfield, IA 52557.  Not Approved.
Reply-To: jhmeyers@miu.edu
Newsgroups: comp.sys.hp48


In article <01bcf5c6$7fd55b70$1d22249e@Q270>,
Christian Meland <christian.meland@pfi.no> writes:

: I may have missed a point here, but wouldn't
: << IFERR ORD THEN ORDER END >> do all right
: if you compile it when HACK is installed?
: When HACK is not installed, you will just see
: << IFERR XLIB 1219 16 THEN ORDER END >>

"Everything should be as simple as possible -- but no simpler"
                                - Albert Einstein

I confess that I went through several tries in this direction;
the one I thought had solved everything was sitting in my calc
for several months before I threw the argument { 5 } at my
latest variable hider (HIDEVARS), which sent me right back to
the drawing board to fix up my just-broken ORDER command,
which did not correctly emulate the proper error behavior.

Here are all the goals I had in mind:

o No modifications needed to any other User-RPL program; rather,
  the ORDER command just becomes "smarter"

o All new ascii downloads of User-RPL programs automatically
  use the smarter ORDER; existing programs do so whenever they
  are re-edited (even if no changes are made).

o Correct reactions to any and all possible error conditions
  (this criterion was failed by my non-disclosed earlier version :)

o The 'ORDER' program itself must be "bulletproof," not depending
  on whether HACK is or isn't present when it is first downloaded
  or compiled, nor "breaking" if it should later be re-compiled.

So, if you meant to add the above logic into other programs, that
would violate my first goal; let's therefore assume that you suggest
the above program as what will be stored into a variable named 'ORDER'

If it is first compiled when the hack library isn't present, then 'ORD'
would become a Global Name, its evaluation would just put that name
on the stack, rather than execute anything, and we'd never get
proper execution in the first place.  If it was first compiled
when the hack library was present, but later re-compiled when
it wasn't, the word XLIB would appear and itself be compiled
(or error) -- another chance to wreck the original program.

If ORD was correctly compiled (the first time) as a library call,
and the library is later missing, we get an "Undefined XLIB Name"
error, as you expect.  Now, what do you suppose happens to whatever
is on the stack when that error occurs?  Well, we might presume
that whatever was on the stack will be left there; however,
the command whose arguments *might* be returned to the stack
(*if* flag -55 is clear) is actually the last *prior* command which
actually saved its Last Arguments, and before even those arguments
are returned to the stack, everything else is first *deleted* back
to the last "stack mark" [SysRPL stuff] -- what a bummer, eh?

And, as long as flag -55 had to rear its ugly head, we should note
that a User-RPL program attempting to recover from errors using
IFERR must either make definite the state of that flag before
the error occurs, or else test that flag in the error-handling
branch of the structure; otherwise, should an error occur,
the program can not know for sure what will be left on the stack
following the error.

Even HP is guilty of neglecting this: just look at the APLY program
(produced by the G/GX TEACH command, and also listed in the AUR);
you will note it contains an IFERR structure, but do you see any
mention of flag -55?  Well, just set flag -55 and then use
APLY to create a symbolic result -- bingo, there's a bug!

Moving on, if ORD (compiled as a library call) does error,
do we presume that the cause must have been "Undefined XLIB Name" ?
Well, couldn't the cause instead have been an "Undefined [Global] Name"
or a "Bad Argument Type" or "Insufficient Memory" etc. ?
Do we want to plow right ahead and do a real ORDER (on an argument which
may or may not still be there) if any of these things has occurred?

BTW, have you ever seen the ORD command end up in different libraries,
or to change its "function number" between different versions of the
same library?  This reveals another pitfall to everyone newly making
libraries for themselves: if you change the order of or insert
any new library commands between versions of the same library,
any pre-compiled programs (still referring to the old numbers) can
suddenly metamorphose into having completely different function calls!

This horror is more or less why most system entry points were "frozen"
and not moved between various HP48 ROM versions; otherwise, any
programs transferred in binary (pre-compiled) form would have been
blown away by the movement of what their address pointers referenced,
and every program would have needed re-compilation for each version
of the internal ROM libraries.  It was said that HP really hoped
to let all programs be transferred only in ascii mode and thus be
re-compiled between OS versions; think of what this would have done
to the possibilities of ever developing the HP48 software base now
in existence; BTW, anybody got a Stat Pak for a Rev M to trade for
the same app on a Rev R?  Thank you, HP, for the heroic effort
involved in revising the OS so many times while moving hardly anything!

The moral here is: If you add new library commands after using your
own library for a while, append the new commands to the *end* of the
library; if you wish to *delete* commands -- don't! (just nullify
them by storing "empty" programs to replace the original commands).

Lastly -- but not leastly [what??] -- using the ORDER command in a
user program stored in a variable named 'ORDER' is a neat trap to
spring on the unsuspecting person who next reviews that program
in the command line, and then presses ENTER afterwards:
Oops! -- an infinite recursion loop!

It would have been easy to do this task in SysRPL, but as we have seen,
many can be the pitfalls of a simple little one-liner in User-RPL :)

After falling into just about every one of the traps myself at one time
or another, I finally posted what I believe to be a sure bet:

%%HP: T(3); @ ORDER - Use ORD (XLIB) if available, else xORDER
\<< @ Outer program, stores inner program in variable named 'ORDER'
  \<< "{ORD}" OBJ\-> 1 GET @ Inner program == ORDER command replacement
  DUP TYPE 14 == { EVAL } { DROP #20FD9h SYSEVAL } IFTE \>>
"ORDER" #5B15h SYSEVAL STO \>> @ Checksum: #4FF1h (please verify it!)

In the above program, we compile the word ORD right now; we use the
result *only* if it turns into a library call; otherwise we use
*only* the built-in ORDER *command*, and we make sure to avoid the
"infinite recursion" pitfall by using the SYSEVAL address for ORDER,
and not the ascii word ORDER.  No matter whether the hack lib is or
isn't present when we first compile or download this, nor whether we
view and re-compile this many times more thereafter, nor what kinds
of error conditions are produced, the result seems to me to be
indistinguishable from directly using the internal ORDER command,
or have I missed something?  Perhaps I've missed an easier way
to guarantee all of the above goals (just watch - Christian will answer
that I can drop the closing "}" from the string and save one byte :)

I hope this long excursion into the ramifications of one line
of User RPL offers someone a few Insights and Deep Thoughts
(while waiting for a new ML Chess to arrive :)

"I sat next the Duchess at tea;
 It was just as I feared it would be:
    Her rumblings internal
    Were simply infernal --
 And everyone thought it was me!"

-----------------------------------------------------------
With best wishes from:   John H Meyers   <jhmeyers@mum.edu>
