Newsgroups: comp.lang.apl
Path: watmath!watserv1!utgpu!cs.utexas.edu!qt.cs.utexas.edu!zaphod.mps.ohio-state.edu!uunet.ca!geac!itcyyz!yrloc!hui
From: hui@yrloc.ipsa.reuter.COM (Roger Hui)
Subject: Re: Meaning of Curry in J
Message-ID: <1992Feb8.045229.23356@yrloc.ipsa.reuter.COM>
Organization: Iverson Software Inc.
References: <920204044743_72740.1026_EHE37-1@CompuServe.COM> <ROCKWELL.92Feb4081353@socrates.umd.edu>
Date: Sat, 8 Feb 92 04:52:29 GMT

Bob Hendricks writes:
 
> I do not understand what is meant by "curry".  In the APL issue of the IBM
> Systems Journal (Vol 30, No 4, 1991) there is a paper K. E. Iverson called "A
> personal view of APL".  The last example in the paper is:
>
>    table=. / ([`by`]`over`) \
>    2 3 4 *table 1 2 3 4 5
> +-+------------+
> | |1 2  3  4  5|
> |-+------------|
> |2|2 4  6  8 10|
> |3|3 6  9 12 15|
> |4|4 8 12 16 20|
> +-+------------+
>
> (In the paper there is typography error making the second and fourth accent
> marks look like quote marks.)
>    ...
> But what happens to make it so?  Can anyone explain curry in the context of
> this example, perhaps by showing how the stack is processed?
 
There are indeed typographical errors in "table" as it appeared in the
Systems Journal.  The text was recast with insufficient care _after_ a
final reading by Iverson.  (The version above is correct -- all grave
accents, no quotes.)
 
Raul Rockwell's reduction of "table" is useful and perhaps even easier
to understand, but the parser does something else.
 
Recall that a sentence to be parsed is placed on a queue, and as parsing
proceeds from right to left words are moved from the queue onto a stack.
An eligible portion of the stack is executed and replaced by the result.
Eligibility for execution is define by the 14-by-4 table on page 5
of the dictionary, and is completely determined by the _classes_
(noun, verb, adverb, etc.) of the first four words on the stack.
 
Assuming that "over" and "by" are previously defined as verbs,
then the sentence   table=./([`by`]`over`)\   is parsed as follows.
Each line represents a step in the parse.  Successive columns show
the index of the line, the queue, the stack, the applicable rule
and action, and comments.  ("M" is the left marker.)
 
                                         Rule/
 I     Queue                 Stack      Action       Comments
 
 0  M table=./([`by`]`over`)\
 1  M table=./([`by`]`over`)       \   13 Move
 2  M table=./([`by`]`over`       )\   13 Move
 3  M table=./([`by`]`over       `)\   13 Move
 4  M table=./([`by`]`       over`)\   13 Move
 5  M table=./([`by`]       `over`)\   13 Move
 6  M table=./([`by`       ]`over`)\   13 Move
 7  M table=./([`by       `]`over`)\   13 Move
 8  M table=./([`       by`]`over`)\   13 Move
 9  M table=./([       `by`]`over`)\   13 Move
10  M table=./(       [`by`]`over`)\   13 Move
11  M table=./       ([`by`]`over`)\   13 Move
12  M table=./         (n0`]`over`)\    4 Conj      n0=.[`by
13  M table=./           (n1`over`)\    4 Conj      n1=.n0`]
14  M table=./                (n2`)\    4 Conj      n2=.n1`over
15  M table=./                 (a0)\   10 Curry     a0=.n2`
16  M table=./                   a0\   12 Punct
17  M table=.                   /a0\   13 Move
18  M table                   =./a0\   13 Move
19  M table                     =.a1    7 Forkc     a1=./a0\
20  M                      table=.a1   13 Move
21  M                             a1   11 Is        table=.a1
22                              M a1   13 Move
 
The "Curry" action on line 15 applies because the first four words
of the stack (on the previous line) are "( n2 ` )", satisfying
the pattern for the rule.  The phrase n2` -- juxtaposing a conjunction
with an argument -- derives an adverb according to section II F
subsection b on page 5 of the dictionary.  This form of "Curry" has
many other useful instances.  For example:
 
Inverse   =. ^:_1      OnAtoms =. "0         Files    =. 1!:
Limit     =. ^:_       OnList  =. "1         WS       =. 2!:
Each      =. &.>       OnLeft  =. @[         Internal =. 3!:
Capsulate =. <@        OnRight =. @]         Names    =. 4!:
 
The above shows how "table" is _defined_; the following is how "table"
is _applied_.  Note that the definition of "table" is not re-parsed
(i.e. it is compiled).
 
                                         Rule/
 I     Queue                 Stack      Action       Comments
 
 0  M 3 4 *table 4 5 6
 1  M 3 4 *table               4 5 6   13 Move
 2  M 3 4 *              table 4 5 6   13 Move
 3  M 3 4               *table 4 5 6   13 Move
 4  M                3 4*table 4 5 6   13 Move
 5  M                   3 4 v0 4 5 6    3 Adverb   v0=.*table
 6                    M 3 4 v0 4 5 6   13 Move
 7                              M n0    2 Dyad     n0 =. 3 4 v0 4 5 6
 
Finally, the verbs "over" and "by" used in "table" are defined as follows.
They have been revised and polished many times; your version may be
slightly different.
 
   rowhead =. ' '&; @ ,.
   colhead =. {. ; }.
   over    =. colhead @ ": @ ,
   by      =. rowhead@[ ,. ]

------------------------------------
Roger Hui, Iverson Software Inc., 33 Major Street, Toronto, Ontario  M5S 2K9
Phone: (416) 925 6096;  Fax: (416) 488 7559
