Thoughts on
Vector 7:4, page 119
The text of this article is not yet online. No complete scan of this issue has been found. The only copy found, on vector.org.uk, is damaged: its page images are lost, but the text machine-read from them survives, shown below (help find a scan).
Unedited OCR text, machine-read from a scan now lost: it contains errors, and its APL is wrong. Shown for searching.
VECTOR Vol.7 No.4
Thoughts on +r g A
by Maurice Jordan
Introduction These notes arise from ideas first stimulated by Iverson's presentation at APL88 on yoke (which allows among other things infinite sets to be handled simply in APL), and function assignment e.g.
sumet/
Yoke has been superseded by the phrasal forms hook and fork (APL89) and these are now implemented in J (APL90). These phrasal forms assign meaning to phrases such as
Ief gh
where f g and are functions. Powerful though the concepts are, are they the best use of this phrasal form?.
Experiments using Dyalog APL Function assignment has been available in Dyalog APL since their introduction of defined operators in 1986. When used with operators it becomes possible to build quite complex functions in a simple sequence of assigned functions.
As soon as I got my hands on a Beta-release version of Dyalog/386, I began experimenting with fork and hook. Hook is simply expressed as an operator:
VY Z+{A}(£ hook g)B a Elidable left args must be enclosed in {} av Iverson/McDonnell's hook phrasal form. na 2++A £ g B (dyadic); Bf g B (monadic} +(O#0QNC 'A')4E1 0 A+B Li:a*A £ g B v
But the trouble with fork is that it needs a mechanism for deriving an operator or a way of passing more than two functions to an operator. I quickly found that I
119
VECTOR Vol.7 No.4
could use Bob Smith’s NARS composition operator as implemented in Dyalog (analogous to & in J) to bind 3 functions so that they looked like one
fegeoh
and thus fool the interpreter. Once inside the operator, the intemal representation can be examined:
fght+/erep ADISP OCR 'fgh' a ADISP is a utility to display nested
EEE
and in simple cases (where f g and h are primitive) the original f g and h can be reconstructed easily:
VY Z2+{A} (fins fork)B;f;9;A;FNS a Iverson/McDonnell's phrasal forms idea. A fnse+fogeh; Z*({A} £B) g ({A} fh B) a8 £g h are primitives here, FNS<«QCR'fns' f+s2>FNS © g+2322FNS © A+ 3aFNS a Monadic > is + in APL2 +(OSQNC'A')}+Monad o 2+{A £ B)g AA Bo +0 Monad:Z+(f B)g h B v
This allows
mean++/erep fork mean i234 5
I was able to experiment enough with these operators to realise that their combination with function assignment leads to a powerful tool.
120
VECTOR Vol.7 No.4
A Better Way
This representation of fork depends on a fudge. The fudge is clumsy and difficult to generalise so that it deals with defined, derived-from-defined, and assigned functions. Instead of the composition primitive, relying on undocumented internals (therefore liable to change), why not regain control and use a defined operator?
v Z+(f J_ g)B aV A Join operator to stack functions together for operator a calis and unstack them within the operator. +(B=0)4hi 9 Z+f o +0 L1:a+g v
This enables two (or more) functions to be joined together for syntactic binding in operator calls, and provides an easy means of unscrambling them again within the operator. This is used in the next generation of the fork operator:
Vv Z+{A)(fgh J_)B;fgsfigih av Performs J's fork construct when fgh is of form f£j_g jl 2(0=0NC 'A')/'Atright! fg+fgh 0 a fgh must be of form Lj_g fA ferg 0 grfg 1 hefgh 1 Z+(A f£ B)g(A h BY} az:a MHJ 075ep90 v
meanet+/ j_ + Joe IL mean i234 5 3
right is simply the right identity from Dictionary APL:
VY B+(A}right B aV Right identity (+ in Dictionary APL) v
as this is not a primitive in Dyalog APL. The idea of assigning an identity function for elided left arguments in ambivalent functions and operators comes from Phil Last. What a pity we can’t write
121
VECTOR Vol.7 No.4
¥ Ze{Ae+}(fgh J_) B
to supply a default for an elided left argument and avoid clumsy conditional executes or computed gotos.
Even Simpler
A simpler construction, requiring only one call of j_ results from
V Ze{A} (fg Fork h)B;f3g av A simpler form of fork depending on the s t a c k operator Je 2(O=0NC 'A')/'Atright! f+fg 0 a be of form f j_ g g+ig 1 Z(A £ BYg AB az:a MHS 07Sep90 v
mean++/ j_ + Fork p mean it234 5 a
Other Forms of Fork
Other forms of fork can be derived from
¥ 2+{A}(f£ strand g)B £(OS0NC'A')/'Aeright! Z+(A £ B)(A g B) v
meantae(+/)e(+/ strand p) a rather unlovely
or
meantze(+/)e(+/ j_ p fnarray)
where fnarray is an operator to simulate Benkard’s function array ideas but lacks the elegance to be shown here.
122
VECTOR Vol.7 No.4
Back to the Yoke
Although forks may be simulated in these ways their usage lacks the elegance achieved by simply, but tediously, building a series of defined operators to cover the g functions inthe £ g A fork (as in the original yoke idea) e.g.
VY Z«{A}(L and g)B avY AND operator for proposition functions f and g #(O=QNCtA')/'Acright'! Z+(A £ B)sAA Gg B v
Vv 2+{A}(f£ or g)B av OR operator for proposition functions f and g 2(O=DNC'A')/'Atright | Z+(A £ BVA Gg B v
50 2.8.
integer_matrix+integer and matrix
This approach has the advantage that with very little added complication an early exit and/or error handling may be provided, allowing DOMAIN ERROR to be avoided in functions such as
probability+(se1) and (2°0) and numeric
Alternative Meaning for f g 2
The experimentation with fork, hook and function assignment thus made possible confirm the expressive power of these constructs. However, much is due to the composition operator, this operator very soon starts to get in the way with simple functions such as matrix:
matrix*>e2e=ep9p
which can be applied to any array:
any_array+ieleft
123
VECTOR Vol.7 No.4
APL allows me to assign from the right simple expression:
Zelgha
fer A
When assigning functions, why can’t I write expressions such as
Z+(fgh+L (gheg h))A
In other words, why shouldn’t APL allow me to write
matrix+>2=pp any_array+¢i1a
In this context dyadic functions with left arguments a r e regarded as deriving monadic functions. Commute can be used for the rarer cases where we want a monadic function derived from a dyadic function and right argument e.g.
spete= a 'spe'+$'eps'
For example, rewriting some J assignments from Vector 7.1 p 72 with APL equivalents:
cos=. 20, A cos+20 Gr. #8180 A g+t5180 A or g+180+~ rld=. 0.9 a rideog cosd=. cos rfd A cosd+cos rfd Sind=, 10.0.(%8180) 9 sind+100180+=
This last expression was originally
Sind=. 180,.8(0.8(48180))} a sind+1ceo0(oe{+0180)) J
I have found that insertion of composition (and parentheses) into phrases for function assignment is often non-trivial. Yet, with the exception of cases like the definition of g above where an alternative is available through the commute operator, the expressions can simply if composition and parentheses are ignored. Why not provide the facility to write them without composition? Unfortunately this is in conflict with fork and hook. My experience so far is that
124
VECTOR Vol.7 No.4
fork and hook are a (not insignificant) minority of useful function assignments. For my purpose, it is more useful to use the yoke concept for fork, even if it means writing a family of operators which could have been derived from yoke.
Conclusion Although composition (¢ in Dyalog and NARS, or & and other operators in J) is the basis for a lot of useful assigned functions and other constructions, it quickly ends up in the way. It should be possible to reduce it to an elided operator with a suitable definition of APL syntax. My impression is that there is a language of exceptional beauty simplicity and power struggling to emerge. Which constraint on our current thinking must be broken to release it?
References and Further Reading [1] Iverson A Commentary on APL development Presentation at APL88 published in QuoteQuad 19.1
[2] Iverson, McDonnell Phrasal Forms APL89 Proceedings
[3] Hui,tverson,McDonnell, Whitney APL\? APL90 Proceedings [4] Iverson ] Vector 7.1 [5] Cherlin APL Trivia APL90 Proceedings
[6] Benkard Nonce Functions APL90 Proceedings
[7] Benkard Structural Experiments with Arrays of Functions APL85 Proceedings
Maurice Jordan 91 Coldershaw Road, West Ealing, London W13 9DU
125