home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Power-Programmierung
/
CD1.mdf
/
apl
/
ratapl
/
ratapl.doc
< prev
next >
Wrap
Text File
|
1989-03-15
|
30KB
|
742 lines
Rationalized APL\PC Rel. T1.4
7 February 1989
Network Distribution
(c) Copyright 1988, 1989 by NGARG, Zurich
All rights reserved.
Author: A. Werder
Rationalized APL\PC is an experimental APL interpreter model that
implements the concept of Rationalized APL and its successor
described in "A Dictionary of APL" by K.E. Iverson (APL Quote Quad,
Volume 18, #1, ACM). Although it does not entirely implement all of
the Dictionary APL the interpreter provides the basic functionalities
of a future 3rd generation APL.
DISCLAIMER: This experimental software and documentation is
provided on a "as is" basis with no warranty
whatsoever.
This product is provided as freeware; its use
for commercial advantage is strictly prohibited.
1. Installing the Interpreter
The requirements for running the Rationalized APL\PC interpreter
are: an IBM/PC-XT or AT (or compatible) with 640kB main memory, a
floppy disk (for the installation, if source is obtained from disk)
and a hard disk. Any PC/DOS release above 2.1 is acceptable.
PC Files for Interpreter
There are 3 files required for running the interpreter:
RATAPL.EXE
RATAPL.SYS
PROLOG.ERR
The actual code which includes a minimal TurboProlog run time
system is contained in the file of type .EXE. The definitions for
the interpreter such as function and operator parts are contained
in the file RATAPL.SYS. This is in fact the prolog data base in
the form of an ASCII text file. The third file simply contains
error messages required for the TurboProlog run time system in
case of a failure.
It is suggested that these files are installed in a suitable
directory; for example, C:\RATAPL. This documentation assumes
that Rationalized APL\PC is installed in that directory.
To start the Rationalized APL\PC, simply type <CD \RATAPL> and
then <RATAPL>.
To see a short demonstration, start Rationalized APL\PC and
type <)load demo>.
2. Implementation Details
APL Keyboard and Display
The keyboard mapping for APL and ASCII characters is based on the
so called "union"-keyboard. The first alphabet is ASCII lowercase,
the second alphabet (normally APL underbarred characters) is upper
case. The APL characters can be generated by pressing ALT plus the
corresponding key. A number of APL characters and overstrikes can
be generated by pressing a key together with the CTRL-key.
The four cursor keys as well as the <HOME>, <END>, <INS> and <DEL>
key provide some simple editing facilities. In particular, cursor
<UP> recalls the previously entered line for re-editing.
TurboProlog resets the EGA character set to the default.
The supplied workspace "ega" may be used to load the APL character
set. Type <)load ega> to load the EGA APL character set.
APL Variables: Limitations on Type, Rank and Size
Only the 4 basic data types "character", "integer", "real" and
"enclosed" have been implemented. For the purpose of internal
rationalization the data types "string" and "token list" have been
added. The rules for typing have been relaxed so that
heterogeneous arrays are now allowed. Arrays may be heterogeneous
in class and type. It is possible for instance to mix numbers and
functions. Class and type of an array are given by the class and
type of the first element in the array. Note that still most of
the primitives require homogeneous arguments.
Boolean variables are represented as integers. Integers are 2
bytes long. Type coercion between integer and real normally occurs
if a scalar function is used. Some functions such as modulo or
binomial which use built-in TurboProlog predicates however do not
detect an integer overflow so that the result of such an operation
can lead to incorrect results. The absence of integer arithmetics
for real numbers in the underlying TurboProlog compiler
unfortunately also limits the domain of a number of functions (for
instance encode, decode etc.) to 2 byte-integers.
Complex numbers are not implemented and consequently they neither
are within the argument domain of scalar functions.
Infinity and negative infinity are represented by the symbol ² and
²² respectively. Internally they are handled as big reals. Not all
scalar functions can handle them correctly and care should be
taken that no overflow occurs.
In general the formatting facilities of the interpreter are very
limited. In particular the display of enclosed arrays is very
rudimentary. Also, in order to take advantage of the built-in
TurboProlog predicates, input and display of real numbers can
deviate from standard APL.
The maxmimum rank of arrays has been fixed to 63, exceeding it
will lead to a <limit error>. Since the data part of APL variables
is always represented in a list of data elements and those lists
are processed within the interpreter in recursive predicates the
amount of stack memory required is considerable. Variables with
more than 500 elements almost always lead to problems with the
stack and should be avoided.
Note: If a behaviour of the interpreter is said to be problematic
this means that in case of a failure the Rationalized APL\PC
interpreter will terminate with an error message (in the
lucky case) but it may also cause the PC to hang (in the
unlucky case). In such a situation the active workspace
is definitely lost. Frequent )save of the workspace can
save the user from loss of valuable data.
Some work has been done in order to make the interpreter safer. In
particular, stack overflow should now be reported as workspace full
error (due to some problems with the compiler's error trapping
feature errors other than ws full may also occur though). The use of
TurboProlog version 2.0 compiler has helped to sort out some
problems. Yet the interpreter is still not 100% reliable...
Active and Saved Workspaces
The user's workspace is kept in the interpreter's (Prolog-) data
base. The workspace which mainly consists of the symbol table is
read and written every time the user is prompted in immediate
execution mode. If a user defined function is called, a new local
symbol table is set up that also contains the global environment.
Upon completion of a function the local environment is dissolved
and the previous more global environment is re-established. Global
objects are not kept within the symbol table functor. They are
stored as separate objects in the data base.
This mechanism is very slow and limits considerably the use of
user defined (in particular recursive) functions. Defined
functions consisting only of one line are special cased and run a
bit faster.
Saved workspaces are unloaded versions of the relevant parts of
the data base. It is not guaranteed that saved workspaces are
compatible with future releases of the Rationalized APL\PC.
Workspaces saved under release T1.0 are not compatible with this
release.
Function and Operator Parts
The definition of functions and operators (the basic function and
operator parts) are also taken from the interpreter's data base.
This data base is initially set up at program start (this is done
with a <consult> of the file <RATAPL.SYS>).
Provided the experimentor is familiar with the internals of the
Rationalized APL\PC and has some knowledge of TurboProlog, it is
possible to change such parts in order to vary the behaviour of
the interpreter or some part of it. Such changes however should be
made with extreme caution. A set of files for maintaining the part
definitions is available separately from the author.
Parsing rules are not defined dynamically through the data base.
For reasons of speed they have been coded into the interpreter.
Modifying those syntax rules is therefore not possible.
3. Functionalities of the Rationalized APL\PC
The Rationalized APL Syntax
The Rationalized APL\PC interpreter is based on the Rationalized APL
syntax. The syntax rules have been been slightly modified however.
Functions derived from a monadic operator which evaluate to class
data (for instance from a functional enclose operator) do not
require an additional pair of parentheses (thus allowing the
expression f+$, -$ if $ is the functional enclose operator).
The current version implements most of the APL primitives listed in
"A Dictionary of APL". Note that the assignment of symbols to
primitives deviates in a few cases from the APL Dictionary.
Available Primitive Functions
+ identity plus 0 0 0
- negative minus 0 0 0
Æ sign times 0 0 0
÷ reciprocal divide 0 0 0
* exponential power 0 0 0
natural log log 0 0 0
ì ceiling maximum 0 0 0
Å floor minimum 0 0 0
■ magnitude absolute 0 0 0
! factorial binomial 0 0 0
? roll deal 0 0 0 not implemented
~ not difference 0 0 0 difference not implemented
^ and 0 0 lcm not implemented
· or 0 0 gcd not implemented
Ω nand 0 0
σ nor 0 0
= equal 0 0
å not equal 0 0
< enclose less ² 0 0
> disclose greater 0 0 0
≥ not less 0 0
≤ not greater 0 0
∙ Pi times circle 0 0 0 circle functions: αε1 2 3 ²3
Γ index gen. index of ² 1 ²
, ravel catenate ² ² ²
₧ ravel catenate ² ² ² functions along first axis
weak enclose link ² ² ²
pass dex ² ² ²
stop lev ² ² ²
√ shape reshape ² 1 ²
nub take ² 1 ²
raze drop ² 1 ²
{ cart.prod. from ² 0 ²
no complementary indexing
φ transpose transpose ² 1 ² dyadic transpose: α≡α
Φ reverse rotate 1 0 1
Θ reverse rotate ² ² ² reverse along first axis
ε member 0 ²
τ encode ² ² en- and decode: 2-Byte integers
µ tokenise decode 1 ² ² only; tokenise not implemented
grade grade ² 1 ² no secondary ordering
gradedown gradedown ² 1 ² no secondary ordering
ÿ mat.inverse mat.divide 2 ² 2 not implemented
≡ match ² ²
⌠ format format ² ² ² very limited implementation
⌡ execute execute 1 1 1 extended result range
I-beam ² ² system functions
Operators
f/ reduction derived function: monadic only
fδ reduction along 1st axis derived function: monadic only
f\ scan
fº scan along 1st axis
α/ compress
fδ compress along 1st axis
f\ expand not implemented
fº expand along 1st axis not implemented
f swap
f} select, merge monadic form not implemented
f[ yoke always
f] yoke conditionally experimental
α.f tie, outer product forces fπ0; α: ° treated as 0
f.g inner product derived function: dyadic only
f.α power not implemented
fπα set function rank
fπg on (function composition)
απf cut not implemented
αf glue
fα glue
fg with (function composition)
αΣf prefer ~α≡α
fΣα defer ~α≡α
fΣg upon (function composition)
fh[g yoke always
fh]g yoke conditionally (recursion operator)
α∞ direct definition
f∞g function application
α∞f class definition αε4 5
Derived Functions
Functions can be recursively derived and there is virtually no
limitation on the depth of recursion (except the interpreter's
stack area). Some examples might illustrate the power of such
recursions:
(,π2)Σ1 Ravel rank-2 cells after deferring axis
+.Æδ Reduction on inner product
+/Σ2 Reduction along axis 2
₧>/ The raze function
⌡Σ(0{) Execute after selection
Internally a number of derived functions are used to implement
functions of high complexitiy with combinations of functions of
less complexity. For instance, the cartesian product has been
implemented as the derived function
>Σ(0.(,>)>/Σ(<π0>))
Again, the complexity of the definition restricts the domain of the
arguments to relatively small arrays in such cases.
User defined functions can also be derived:
('+' ∞ 'α+')π0
With the exception functions derived from the del-operator the
execution of derived functions is deferred until they are actually
evaluated. The vector representation of derived functions for
displays corresponds to the constituting APL expression after
names have been replaced by their values.
Function Rank
The RATAPL.SYS file delivered with the interpreter defines some
default function ranks different from the APL Dictionary. For
instance an expression like Γ2 3 results in a rank error. Using
the function rank operator explicitly will produce a result rather
than an error message:
Γπ(0) 2 3
0 1 0
0 1 2
A modification of the RATAPL.SYS file can change the behaviour to
conform with the Dictionary.
Function Definition
Functions may be defined using the function definition operator
(del):
plus '+' ∞ 'α+'
fac ('(≤1){1 2' 'Ʊ -1' '1') ∞ °
The operator always requires both definitions, the monadic and the
dyadic. The arguments may be a character array of rank>2 or an
enclosed vector of character arrays of rank<2. Instead of an empty
character vector the jot (°) can be used to designate a non
existing case of a definition. Execution of an undefined case is
identical to the execution of the identity function.
The following rules apply within the function definition:
- The symbol α denotes the optional left, the right argument.
- The result of the last line executed is propagated as the
function's overall result.
- Branching is done with (the interpreter replaces the goto by
an assignment to òlc).
- If a branch is done with an vector of line numbers then those
lines are executed in the corresponding order.
- The function terminates upon exhaustion of òlc or if an
invalid line number is detected.
- Self reference of a function is achieved with the symbol ±.
- One line functions should not begin with a branch.
Resuming a suspended function is not possible. A naked branch
usually pops up one level and so allows to escape from it. Note
that a naked branch in immediate execution mode on the top level
causes the interpreter to terminate normally: the next level is
PC/DOS.
Defined functions functionally behave like primitive and derived
functions. In particular they may appear as arguments to operators
(for instance in inner products; likewise the argument rank of a
defined function may be set with the rank operator).
Defined functions build up a new level in the state indicator
stack and provide a new environment for local variables. This
process is unfortunately is extremely slow so that the execution
defined functions is quite limited.
Defined Operators
The del-hyper operator may be used for changing the object class.
With a left argument of 4 the operator turns a function
(primitive, derived or defined) into a monadic operator. A left
argument of 5 turns it into a dyadic operator.
4 ∞ f Change class of f from function to monadic operator
5 ∞ f Change class of f from function to dyadic operator
Examples:
fenc 4 ∞ < ª functional enclose
each 4 ∞ ('>'∞°) ª each operator
ª Hodgkinson's if-else construct (Hodgkinson, 1987)
if 5 ∞ (°∞'>(å0){(<∞),(<∞α)')
else 5 ∞ (°∞'>((<∞α)≡(<∞)){(<∞α),(<∞)')
+ if (n>0) else - ª conditionally select a function
The Yoke-Operators
The current release includes the definitions for two new monadic
operators: Yoke always and yoke conditionally. Yoke always conforms to
the definitions given in the Dictionary of APL. Both accept a (dyadic)
function argument. The derived form however is not of class function but
of class dyadic operator. The derived dyadic operators accept a left and
right operator argument of class function. Both yoke operators perform
function composition except that they combine three functions instead of
two like their known counterparts (for instance Σ). The definition is the
following:
kf h[ g yoke always:
k(x,y): h(f(x,y), g(x,y))
k(x): h(f(x), g(x))
kf h] g yoke conditionally:
k(x,y): f(x,y)å0: h(x, k(x, g(x,y)))
f(x,y)=0: h(x, g(x,y))
k(x): f(x)å0: h(x, k(g(x)))
f(x)=0: h(x, g(x))
As can be seen from the definiton, the conditional yoke applies the
derived function recursively until a condition expression (identified by
the left derived operator argument f) evaluates to 0. A final function
composition on h and g terminates the recursion.
The faculty function fac: Æfac -1: ≤1 : 1 could be written as
fac 1< Æ] (1ìΣ(-1))
whereas the more complex
scan4 ∞ ('1<Σ√(₧[(δ))](²1)' ∞ °)
provides an alternative definition of the monadic scan-first operator.
The use of the simpler variant yoke always is more obvious. This can
be shown by a few examples:
sort {[ ª apply from on the results of grade
and identity
ª an alternative form of the average function
sum+/ shape√ dividedBy÷
avg sum dividedBy[ shape
System Constants, Variables and Functions
The only system constant ° is an undisclosable scalar.
System variables which are a part of the workspace environment are
available but not fully implemented. With the exception of the
variables òapl and òdbg, the system variables (òio, òrl, òct, òpp,
òps, òpw) can be referenced but assigning a value to them has no
influence (note that for instance òio is always 0 and cannot be
changed). System variables can be localised on every new SI stack
level.
òapl APL dialect (initially 0); 0 forces the interpreter
to use Rationalized APL rules, 1 forces AIDA rules
(see Gfeller 86).
òav Atomic vector; this system variable is available
(Note: display of 0{òav causes subsequent
characters to be discarded).
òio Index origin (always 0). The index origin cannot
be varied.
òct Comparison tolerance (always 0). The concept of
comparison tolerance and fuzzy functions is not
implemented, however the variable is available.
òdbg Debugging mode (initially 0). This variable can be
used to inspect the behaviour of the interpreter
during syntax parsing and operator and function
execution. It acceptes the following values:
1 - Syntax parsing, display of tokens in left and
right stack (see APL Dictionary).
2 - Same as 1 but display of full internal
representation of tokens in right stack.
3 - Same as 2 plus additional display of operator
execution stack during evaluation phase.
òlc Line counter; the line counter can be set with the
branch (). Only the top level of the state
indicator is reported. The result is always an
integer vector of line numbers (or empty).
òpp, òps, System variables that normally govern the
òpw, òfc formatting during output. These system variables
are not used for this purpose, they are only
available for completeness.
òrl Random link; not implemented.
ò, ù Quad, quote-quad; quad is only defined for output,
quote-quad only for input.
System Functions
òai Result is a 3-element vector and which reports the
user identification and the elapsed time since the
program start (the 3rd element is unused).
òcr Canonical representation (monadic argument rank 1);
returns the canonical representation of user
defined functions. The result is a two element
enclosed array with the first element containing
the monadic and the second containing the dyadic
definition of .
òcmd DOS command interface. Executes the DOS command
passed in and restores APL environment upon
completion.
òedit Edit vector of enclosed character vectors using
TurboPrologs built in editor.
òex Expunge object (monadic argument rank: 1); removes
the most local value bound to name from the
symbol table.
ònc Name class (monadic argument rank: 1); reports
the class of a named object. The result is an
integer array of rank and shape depending on the
rank and shape of the argument. The classes are
assigned as follows:
0 - Invalid name
2 - Variable
3 - Function
4 - Monadic operator
5 - Dyadic operator
ònl Name list (monadic argument rank: 0); returns a
list of all objects of class in the form of a
character matrix. Note that ²1 denotes all
classes.
òsa Storage area; reports the available free stack,
heap and tail area in bytes.
òts System time stamp in the form of a 7-element
integer vector (year, month, day, hour, minute,
second, second/100).
òwa Work area; reports the available free heap area in
bytes (same as 1{òsa).
For the purpose of internal design rationalization the I-beam
primitive has been implemented to provide a further number of system
functions. The following functions are available:
0 Convert string or token list to character vector
6 Convert character vector to string scalar
8 Tokenise string; result is a token list
20 Edit string vector in current window
21 Switch or define new window on screen
√√ 0: switch to window (must be defined)
√ 7: define window [0] with attributes
[1 2] and an offset and size of 3
220 Resize the currently active window
23 Resize the screen (EGA/VGA cards only)
0{ rows, 1{ columns
24 Display a menu in current window and return
the selected row index (where is a string variable;
for matrices, use the expression: 246π><π1 )
To edit an array (an enclosed vector of character vectors for
instance) the composed edit function can be used (in fact the
system function òedit is defined the same way):
edit0>Σ(20Σ(6π>))
aedit 'try this''example''for editing'
Since the built-in TurboProlog editor is invoked APL characters can
not be typed in. They are displayed however and can be composed with
the proper ALT-number sequence.
System Commands
System commands are implemented in a very rudimentary form. Among
the available commands, load and save are probably the most useful
commands.
)clear Clear workspace; only to be executed on the root
level of the SI stack.
)load Load Rationalized APL\PC workspace
)off Leave Rationalized APL\PC (the same effect can be
achieved with a naked branch on the top immediate
execution level).
)save Save Rationalized APL\PC workspace
)si Display state indicator; note that an immediate
execution level is always reported separately and
that since defined functions and operators are always
unnamed no names are displayed.
)wsid Display or set name of the active workspace.
Workspaces are saved in the form of a TurboProlog data base. They
are ordinary ASCII text files containing only a few predicates
(workspace information, state indicator and global values).
Changing the content of such a file outside of Rationalized APL\PC
can cause a message "workspace incompatible" at the next load.
Assignment
Direct and indirect assignment is supported. Direct assignment can
be local or global.
If the assignment arrow is preceded by a space then the variable
is assigned a global scope. Otherwise the variable will have local
scope.
aΓ10 ª a local variable
fenc 4∞< ª a global operator
Note that within defined functions variables are always assumed to
be global until they are assigned a value locally. In the example
inc('+AA+1' ∞ °)
the first occurrence of A from right binds the value of the global
variable A and assigns it (after having it incremented) to a local
variable named A. The global variable A remains unchanged if the
function inc is executed.
Indirect assignment is achieved with an expression within
parentheses to the left of the assignment arrow. If the expression
evaluates to an enclosed array then multiple assignment is
provided. In that case the outer frames of the expressions to the
left and the right of the assignment arrow must correspond.
('jan''feb''mar') φqtr881 ª assign each column
ª individually
(<> 'plus''a') ⌡>'+''100' ª mixed class assigment
If the expression on the left of the assigment arrow contains
recursively enclosed elements then the corresponding cells are
disclosed prior to the assigment.
4. Miscellanous Functionalities
Non-data Arrays
The interpreter allows the construction of non-data arrays.
Currently only function arrays are implemented. The simplest
method the create a function arrays is to use the execute function
with an argument of rank grater than 1:
⌡π(0) 2 2 √'+-Æ÷' ª 2 by 2 function matrix
Refer to Werder 1988 for more details. Function arrays may
be applied to data arrays in a similar way as proposed by
Bernecky (Bernecky 84):
2 far 4 ª note the scalar extension
6 ²2
8 0.5
(⌡π0 'Φ') 2 3√Γ6
2 1 0
3 4 5
In the current implementation the shape of the outer frame
resulting from applying the function rank must conform with
the shape of the function array (unless the function array is
a scalar in which case scalar extension applies). The function
array's function rank is derived from the first function.
Uniformity in function ranks is not required for the array.
APL Language Dialects
In a clear workspace, the system variable òapl is set to 0. This
causes the interpreter to use normal Rationalized APL rules for
syntax parsing. If the variable òapl is set to 1 the interpreter is
forced to use the parsing rules of the APL incompatible dialect
AIDA (see Gfeller 1986 for details). Note that only the syntax is
changed, and no additional functionalities are provided.
òapl1
(/+) (Γ10)
45
5√+,-
+ - + - +
Since de-tokenisation is always based on òapl0 displaying
for instance the derived function (/+) results in the normal
order +/.
Note that no other value except 0 or 1 should ever be assigned to
òapl. A value different from 0 or 1 forces the interpreter to use
inexistant parsing rules - and therefore results always in an
undefined syntax.
5. References
Gfeller 86
Gfeller, Martin: Operators considered harmful, APL Quote
Quad, Vol. 17, No. 1, September 1986
Hodgkinson 87
Hodgkinson, Robert: Practical Uses of Operators in SHARP
APL/HP, APL 87 Conference Proceedings, Dallas, May 1987
Iverson 83
Iverson, K.E.: Rationalized APL, I.P. Sharp Research
Report #1, Toronto, April 1983
Iverson 87
Iverson, K.E.: A Dictionary of APL, I.P. Sharp Associates,
Toronto, March 1987
Werder 88
Werder, A.: Arrays of Objects in Rationalized APL,
APL 88 Conference Proceedings, Sydney, February 1988