Help / Learning / Ap 1: Evaluating Expressions

From J Wiki
Jump to navigation Jump to search

>> << Pri JfC LJ Phr Dic Voc !: Rel NuVoc wd Help Learning J


Appendix 1: Evaluating Expressions

A1.1 Introduction

Here we look at the process of evaluating a J expression. Evaluating a complete expression proceeds by a sequence of basic steps, such as obtaining the value assigned to a name, or applying a function to its argument(s). For example, given

   x =: 3

then the expression

   4+5*x
19

is (in outline) evaluated by the steps:

  1. obtain the value assigned to x giving 3
  2. compute 5 * 3 giving 15
  3. compute 4 + 15 giving 19

The sequence in which the steps take place is governed by the grammatical (or "parsing") rules of the J language. The parsing rules have various consequences, or effects, which can be stated informally, for example:

  • verbs have long right scope. For example, in the expression 2 * 3 + 4 the right argument of * is 3 + 4 so that 2 * 3 + 4 means 2* (3 + 4). This we earlier called the "rightmost-first" rule.
  • verbs have short left scope. For example in 2 * 3 + 4 the left argument of + is 3.
  • adverbs and conjunctions get applied before verbs. For example + & 1 % 2 means (+ & 1 )% 2
  • adverbs and conjunctions have long left scope and short right scope

These effects describe how an expression is implicitly parenthesized. Of course, we can always produce desired effects by writing explicit parentheses, even though they may not be needed. Further effects are:

  • names denoting nouns are evaluated as soon as encountered
  • names denoting functions are not evaluated until the function is applied
  • names with no assigned values are assumed to denote verbs
  • long trains of verbs are resolved into trains of length 2 or 3

and we will look at how the parsing rules give rise to these effects. To illustrate the process, we can use a function which models, or simulates, the evaluation process step by step, showing it at work in slow motion. This function, an adverb called EVM,is based on the description of the parsing algorithm given in the J Dictionary, section IIE. It is defined in a File:Help-learning-91a.ijs.txt.

A1.2 First Example

Evaluation of an expression such as 2+3 can be modelled by offering the argument '2+3' (a string, notice) to the modelling adverb EVM.

  2+3   '2+3' EVM
5 5

We see that '2+3' EVM computes the same value as 2+3, but EVM also produces a step-by-step record, or history, of the evaluation process. This history is displayed by entering the expression hist

   hist ''
                                              
 Queue          Stack                  Rule   
                                              
 +----------+   +------+---+---+---+   +----+ 
 |mark 2 + 3|   |      |   |   |   |   |    | 
 +----------+   +------+---+---+---+   +----+ 
 |mark 2 +  |   | 3    |   |   |   |   |    | 
 +----------+   +------+---+---+---+   +----+ 
 |mark 2    |   | +    | 3 |   |   |   |    | 
 +----------+   +------+---+---+---+   +----+ 
 |mark      |   | 2    | + | 3 |   |   |    | 
 +----------+   +------+---+---+---+   +----+ 
 |          |   | mark | 2 | + | 3 |   |dyad| 
 +----------+   +------+---+---+---+   +----+ 
 |          |   | mark | 5 |   |   |   |    | 
 +----------+   +------+---+---+---+   +----+ 
                                              

We see successive stages of the process. In this example there are six stages. Each stage is defined by the values of two variables. Firstly there is a "queue", initially containing the expression being evaluated, divided into words and preceded by a symbol to mark the beginning. Secondly, there is a "stack", initially empty. The first stage shows queue and stack at the outset.

At each stage the stack is inspected to see if anything can be done, that is, whether the first few words in the stack form a pattern to which a rule applies. There are 9 of these rules, and each one is tried in turn. If no rule applies, then a word is transferred from the tail of the queue to the head of the stack, and we go to the next stage and try again. This process takes us from the first stage to the fifth stage.

At the fifth stage, we find that a rule is applicable. This rule is identified as dyad in the rightmost column. Informally, the dyad rule is:

if the first four items in the stack are something, noun, verb, noun, then apply verb to noun and noun to get new-noun, and replace the first four items in the stack by two, namely original-something followed by new-noun.

The sixth and last stage shows the results of applying the "dyad" rule recognized at the previous stage. The rules are tried again, with no result, and there are no more words in the queue, so we have finished. The final result is the second item of the stack. The history is maintained in 3 global variables, Qh Sh and Rh. The expression hist computes a formatted display from these variables.

A1.3 Parsing Rules

In this section an example is shown of each of the 9 parsing rules. Each rule looks for a pattern of items at the front of the stack, such as something verb noun verb.

Each item of the stack is classified as one of the following: verb, noun, adverb, conjunction, name, left-parenthesis, right-parenthesis, assignment-symbol (=. or =:) or beginning-mark.

To aid in a compact statement of the rules, larger classes of items can be formed. For example, an item is classified as an "EDGE" if it is a beginning-mark, an assignment-symbol or a left-parenthesis.

The rules are always tried in the same order, the order in which they are presented below, beginning with the 'monad rule' and ending with the 'parenthesis rule'.

A1.3.1 Monad Rule

If the first 3 items of the stack are an "EDGE" followed by a verb followed by a noun, then the verb is applied (monadically) to the noun to give a result-value symbolized by Z say, and the value Z replaces the verb and noun in the stack. The scheme for transforming the items of the stack is:

          monad rule: EDGE VERB NOUN etc  =>   EDGE Z etc

where Z is the result computed by applying VERB to NOUN. For example:

  *: 4   '*: 4' EVM
16 16

   hist ''
                                           
 Queue         Stack               Rule    
                                           
 +---------+   +------+----+---+   +-----+ 
 |mark *: 4|   |      |    |   |   |     | 
 +---------+   +------+----+---+   +-----+ 
 |mark *:  |   | 4    |    |   |   |     | 
 +---------+   +------+----+---+   +-----+ 
 |mark     |   | *:   | 4  |   |   |     | 
 +---------+   +------+----+---+   +-----+ 
 |         |   | mark | *: | 4 |   |monad| 
 +---------+   +------+----+---+   +-----+ 
 |         |   | mark | 16 |   |   |     | 
 +---------+   +------+----+---+   +-----+ 
                                           

A1.3.2 Second Monad Rule

An item in the stack is classified as "EAVN" if it is an EDGE or an adverb or verb or noun. The scheme is:

        monad2 rule: EAVN VERB1 VERB2 NOUN etc => EAVN VERB1 Z  etc

where Z is VERB2 monadically applied to NOUN. For example:

  - *: 4   '- *: 4' EVM
_16 _16

   hist ''
                                                    
 Queue           Stack                     Rule     
                                                    
 +-----------+   +------+-----+----+---+   +------+ 
 |mark - *: 4|   |      |     |    |   |   |      | 
 +-----------+   +------+-----+----+---+   +------+ 
 |mark - *:  |   | 4    |     |    |   |   |      | 
 +-----------+   +------+-----+----+---+   +------+ 
 |mark -     |   | *:   | 4   |    |   |   |      | 
 +-----------+   +------+-----+----+---+   +------+ 
 |mark       |   | -    | *:  | 4  |   |   |      | 
 +-----------+   +------+-----+----+---+   +------+ 
 |           |   | mark | -   | *: | 4 |   |monad2| 
 +-----------+   +------+-----+----+---+   +------+ 
 |           |   | mark | -   | 16 |   |   |monad | 
 +-----------+   +------+-----+----+---+   +------+ 
 |           |   | mark | _16 |    |   |   |      | 
 +-----------+   +------+-----+----+---+   +------+ 
                                                    

A1.3.3 Dyad Rule

The scheme is

          dyad rule:  EAVN NOUN1 VERB NOUN2 etc => EAVN Z etc

where Z is VERB applied dyadically to NOUN1 and NOUN2. For example.

  3 * 4   '3 * 4' EVM
12 12

   hist ''
                                               
 Queue          Stack                   Rule   
                                               
 +----------+   +------+----+---+---+   +----+ 
 |mark 3 * 4|   |      |    |   |   |   |    | 
 +----------+   +------+----+---+---+   +----+ 
 |mark 3 *  |   | 4    |    |   |   |   |    | 
 +----------+   +------+----+---+---+   +----+ 
 |mark 3    |   | *    | 4  |   |   |   |    | 
 +----------+   +------+----+---+---+   +----+ 
 |mark      |   | 3    | *  | 4 |   |   |    | 
 +----------+   +------+----+---+---+   +----+ 
 |          |   | mark | 3  | * | 4 |   |dyad| 
 +----------+   +------+----+---+---+   +----+ 
 |          |   | mark | 12 |   |   |   |    | 
 +----------+   +------+----+---+---+   +----+ 
                                               

A1.3.4 Adverb Rule

An item which is a verb or a noun is classified as a "VN" The scheme is:

          adverb rule: EAVN VN ADVERB etc => EAVN Z etc

where Z is the result of applying ADVERB to VN. For example:

  + / 1 2 3   '+ / 1 2 3' EVM
6 6

   hist ''
                                                                
 Queue              Stack                               Rule    
                                                                
 +--------------+   +-------+-------+-------+-------+   +-----+ 
 |mark + / 1 2 3|   |       |       |       |       |   |     | 
 +--------------+   +-------+-------+-------+-------+   +-----+ 
 |mark + /      |   | 1 2 3 |       |       |       |   |     | 
 +--------------+   +-------+-------+-------+-------+   +-----+ 
 |mark +        |   | /     | 1 2 3 |       |       |   |     | 
 +--------------+   +-------+-------+-------+-------+   +-----+ 
 |mark          |   | +     | /     | 1 2 3 |       |   |     | 
 +--------------+   +-------+-------+-------+-------+   +-----+ 
 |              |   | mark  | +     | /     | 1 2 3 |   |adv  | 
 +--------------+   +-------+-------+-------+-------+   +-----+ 
 |              |   | mark  | +/    | 1 2 3 |       |   |monad| 
 +--------------+   +-------+-------+-------+-------+   +-----+ 
 |              |   | mark  | 6     |       |       |   |     | 
 +--------------+   +-------+-------+-------+-------+   +-----+ 
                                                                

A1.3.5 Conjunction Rule

The scheme is:

          conjunction  EAVN VN1 CONJ VN1 etc => EAVN Z etc

where Z is the result of applying conjunction CONJ to arguments VN1 and VN2. For example:

  1 & + 2   '1 & + 2' EVM
3 3

   hist ''
                                                       
 Queue            Stack                        Rule    
                                                       
 +------------+   +------+-----+---+---+---+   +-----+ 
 |mark 1 & + 2|   |      |     |   |   |   |   |     | 
 +------------+   +------+-----+---+---+---+   +-----+ 
 |mark 1 & +  |   | 2    |     |   |   |   |   |     | 
 +------------+   +------+-----+---+---+---+   +-----+ 
 |mark 1 &    |   | +    | 2   |   |   |   |   |     | 
 +------------+   +------+-----+---+---+---+   +-----+ 
 |mark 1      |   | &    | +   | 2 |   |   |   |     | 
 +------------+   +------+-----+---+---+---+   +-----+ 
 |mark        |   | 1    | &   | + | 2 |   |   |     | 
 +------------+   +------+-----+---+---+---+   +-----+ 
 |            |   | mark | 1   | & | + | 2 |   |conj | 
 +------------+   +------+-----+---+---+---+   +-----+ 
 |            |   | mark | 1&+ | 2 |   |   |   |monad| 
 +------------+   +------+-----+---+---+---+   +-----+ 
 |            |   | mark | 3   |   |   |   |   |     | 
 +------------+   +------+-----+---+---+---+   +-----+ 
                                                       

A1.3.6 Trident Rule

The scheme is:

          trident rule: EAVN VN1 VERB2 VERB3 etc => EAVN Z etc

and there are two cases: VN1 may be a verb or a noun.

If VN1 is the verb VERB1 then Z is the single verb defined as the fork VERB1 VERB2 VERB3.

If VN1 is the noun NOUN1 then Z is the single verb defined as the abbreviation for a fork NOUN1 VERB2 VERB3. Forks and abbreviations for forks are described in Chapter 09.

Here is an example: 1 + *: is an abbreviation for the fork 1: + *:

  (1: + *:) 2 3   (1 + *:)2 3 '(1 + *:) 2 3' EVM
5 10 5 10 5 10

   hist ''
                                                                           
 Queue                   Stack                                   Rule      
                                                                           
 +-------------------+   +-------+-------+----+----+----+----+   +-------+ 
 |mark ( 1 + *: ) 2 3|   |       |       |    |    |    |    |   |       | 
 +-------------------+   +-------+-------+----+----+----+----+   +-------+ 
 |mark ( 1 + *: )    |   | 2 3   |       |    |    |    |    |   |       | 
 +-------------------+   +-------+-------+----+----+----+----+   +-------+ 
 |mark ( 1 + *:      |   | )     | 2 3   |    |    |    |    |   |       | 
 +-------------------+   +-------+-------+----+----+----+----+   +-------+ 
 |mark ( 1 +         |   | *:    | )     | 2 3|    |    |    |   |       | 
 +-------------------+   +-------+-------+----+----+----+----+   +-------+ 
 |mark ( 1           |   | +     | *:    | )  | 2 3|    |    |   |       | 
 +-------------------+   +-------+-------+----+----+----+----+   +-------+ 
 |mark (             |   | 1     | +     | *: | )  | 2 3|    |   |       | 
 +-------------------+   +-------+-------+----+----+----+----+   +-------+ 
 |mark               |   | (     | 1     | +  | *: | )  | 2 3|   |trident| 
 +-------------------+   +-------+-------+----+----+----+----+   +-------+ 
 |mark               |   | (     | 1 + *:| )  | 2 3|    |    |   |paren  | 
 +-------------------+   +-------+-------+----+----+----+----+   +-------+ 
 |mark               |   | 1 + *:| 2 3   |    |    |    |    |   |       | 
 +-------------------+   +-------+-------+----+----+----+----+   +-------+ 
 |                   |   | mark  | 1 + *:| 2 3|    |    |    |   |monad  | 
 +-------------------+   +-------+-------+----+----+----+----+   +-------+ 
 |                   |   | mark  | 5 10  |    |    |    |    |   |       | 
 +-------------------+   +-------+-------+----+----+----+----+   +-------+ 
                                                                           
   

A1.3.7 Bident Rule

The scheme is:

          bident rule: EDGE CAVN1 CAVN2 etc => EDGE Z etc

and there are altogether these 6 cases for the bident rule:

CAVN1 CAVN2 Z
verb verb verb (a hook)
adverb adverb adverb
conjunction verb adverb
conjunction noun adverb
noun conjunction adverb
verb conjunction adverb

The first case (the hook) is described in Chapter 03 and the remaining cases in the schemes for bidents in Chapter 15.

In the following example the expression (1 &) is a bident of the form noun conjunction. Therefore it is an adverb.

  + (1 &) 2   '+ (1 &) 2' EVM
3 3

   hist ''
                                                             
 Queue                Stack                         Rule     
                                                             
 +----------------+   +------+-----+----+---+---+   +------+ 
 |mark + ( 1 & ) 2|   |      |     |    |   |   |   |      | 
 +----------------+   +------+-----+----+---+---+   +------+ 
 |mark + ( 1 & )  |   | 2    |     |    |   |   |   |      | 
 +----------------+   +------+-----+----+---+---+   +------+ 
 |mark + ( 1 &    |   | )    | 2   |    |   |   |   |      | 
 +----------------+   +------+-----+----+---+---+   +------+ 
 |mark + ( 1      |   | &    | )   | 2  |   |   |   |      | 
 +----------------+   +------+-----+----+---+---+   +------+ 
 |mark + (        |   | 1    | &   | )  | 2 |   |   |      | 
 +----------------+   +------+-----+----+---+---+   +------+ 
 |mark +          |   | (    | 1   | &  | ) | 2 |   |bident| 
 +----------------+   +------+-----+----+---+---+   +------+ 
 |mark +          |   | (    | 1&  | )  | 2 |   |   |paren | 
 +----------------+   +------+-----+----+---+---+   +------+ 
 |mark +          |   | 1&   | 2   |    |   |   |   |      | 
 +----------------+   +------+-----+----+---+---+   +------+ 
 |mark            |   | +    | 1&  | 2  |   |   |   |      | 
 +----------------+   +------+-----+----+---+---+   +------+ 
 |                |   | mark | +   | 1& | 2 |   |   |adv   | 
 +----------------+   +------+-----+----+---+---+   +------+ 
 |                |   | mark | 1&+ | 2  |   |   |   |monad | 
 +----------------+   +------+-----+----+---+---+   +------+ 
 |                |   | mark | 3   |    |   |   |   |      | 
 +----------------+   +------+-----+----+---+---+   +------+ 
                                                             
   

A1.3.8 Assignment Rule

We write NN to denote a noun or a name. and Asgn for the assignment symbol =: or =.. The scheme is:

          assign rule: NN Asgn CAVN etc => Z etc

where Z is the value of CAVN.

  1 + x =: 6   '1 + x =: 6' EVM
7 7

   hist ''
                                                      
 Queue               Stack                   Rule     
                                                      
 +---------------+   +------+----+---+---+   +------+ 
 |mark 1 + x =: 6|   |      |    |   |   |   |      | 
 +---------------+   +------+----+---+---+   +------+ 
 |mark 1 + x =:  |   | 6    |    |   |   |   |      | 
 +---------------+   +------+----+---+---+   +------+ 
 |mark 1 + x     |   | =:   | 6  |   |   |   |      | 
 +---------------+   +------+----+---+---+   +------+ 
 |mark 1 +       |   | x    | =: | 6 |   |   |assign| 
 +---------------+   +------+----+---+---+   +------+ 
 |mark 1 +       |   | 6    |    |   |   |   |      | 
 +---------------+   +------+----+---+---+   +------+ 
 |mark 1         |   | +    | 6  |   |   |   |      | 
 +---------------+   +------+----+---+---+   +------+ 
 |mark           |   | 1    | +  | 6 |   |   |      | 
 +---------------+   +------+----+---+---+   +------+ 
 |               |   | mark | 1  | + | 6 |   |dyad  | 
 +---------------+   +------+----+---+---+   +------+ 
 |               |   | mark | 7  |   |   |   |      | 
 +---------------+   +------+----+---+---+   +------+ 
                                                      

A1.3.9 Parenthesis Rule

The scheme is:

         paren rule: ( CAVN ) etc => Z etc

where Z is the value of CAVN. For example:

  (1+2)*3   '(1+2)*3' EVM
9 9

   hist ''
                                                                   
 Queue                  Stack                              Rule    
                                                                   
 +------------------+   +------+---+---+---+---+---+---+   +-----+ 
 |mark ( 1 + 2 ) * 3|   |      |   |   |   |   |   |   |   |     | 
 +------------------+   +------+---+---+---+---+---+---+   +-----+ 
 |mark ( 1 + 2 ) *  |   | 3    |   |   |   |   |   |   |   |     | 
 +------------------+   +------+---+---+---+---+---+---+   +-----+ 
 |mark ( 1 + 2 )    |   | *    | 3 |   |   |   |   |   |   |     | 
 +------------------+   +------+---+---+---+---+---+---+   +-----+ 
 |mark ( 1 + 2      |   | )    | * | 3 |   |   |   |   |   |     | 
 +------------------+   +------+---+---+---+---+---+---+   +-----+ 
 |mark ( 1 +        |   | 2    | ) | * | 3 |   |   |   |   |     | 
 +------------------+   +------+---+---+---+---+---+---+   +-----+ 
 |mark ( 1          |   | +    | 2 | ) | * | 3 |   |   |   |     | 
 +------------------+   +------+---+---+---+---+---+---+   +-----+ 
 |mark (            |   | 1    | + | 2 | ) | * | 3 |   |   |     | 
 +------------------+   +------+---+---+---+---+---+---+   +-----+ 
 |mark              |   | (    | 1 | + | 2 | ) | * | 3 |   |dyad | 
 +------------------+   +------+---+---+---+---+---+---+   +-----+ 
 |mark              |   | (    | 3 | ) | * | 3 |   |   |   |paren| 
 +------------------+   +------+---+---+---+---+---+---+   +-----+ 
 |mark              |   | 3    | * | 3 |   |   |   |   |   |     | 
 +------------------+   +------+---+---+---+---+---+---+   +-----+ 
 |                  |   | mark | 3 | * | 3 |   |   |   |   |dyad | 
 +------------------+   +------+---+---+---+---+---+---+   +-----+ 
 |                  |   | mark | 9 |   |   |   |   |   |   |     | 
 +------------------+   +------+---+---+---+---+---+---+   +-----+ 
                                                                   
   

A1.3.10 Examples of Transfer

The following example shows that when a name is transferred from queue to stack, if the name denotes a value which is a noun, then the value, not the name, moves to the queue.

  a =: 6   (a=:7) , a
6 7 6
 
  a=: 6   '(a =: 7) , a' EVM
6 7 6

   hist ''
                                                                   
 Queue                   Stack                            Rule     
                                                                   
 +-------------------+   +------+-----+---+---+---+---+   +------+ 
 |mark ( a =: 7 ) , a|   |      |     |   |   |   |   |   |      | 
 +-------------------+   +------+-----+---+---+---+---+   +------+ 
 |mark ( a =: 7 ) ,  |   | 6    |     |   |   |   |   |   |      | 
 +-------------------+   +------+-----+---+---+---+---+   +------+ 
 |mark ( a =: 7 )    |   | ,    | 6   |   |   |   |   |   |      | 
 +-------------------+   +------+-----+---+---+---+---+   +------+ 
 |mark ( a =: 7      |   | )    | ,   | 6 |   |   |   |   |      | 
 +-------------------+   +------+-----+---+---+---+---+   +------+ 
 |mark ( a =:        |   | 7    | )   | , | 6 |   |   |   |      | 
 +-------------------+   +------+-----+---+---+---+---+   +------+ 
 |mark ( a           |   | =:   | 7   | ) | , | 6 |   |   |      | 
 +-------------------+   +------+-----+---+---+---+---+   +------+ 
 |mark (             |   | a    | =:  | 7 | ) | , | 6 |   |assign| 
 +-------------------+   +------+-----+---+---+---+---+   +------+ 
 |mark (             |   | 7    | )   | , | 6 |   |   |   |      | 
 +-------------------+   +------+-----+---+---+---+---+   +------+ 
 |mark               |   | (    | 7   | ) | , | 6 |   |   |paren | 
 +-------------------+   +------+-----+---+---+---+---+   +------+ 
 |mark               |   | 7    | ,   | 6 |   |   |   |   |      | 
 +-------------------+   +------+-----+---+---+---+---+   +------+ 
 |                   |   | mark | 7   | , | 6 |   |   |   |dyad  | 
 +-------------------+   +------+-----+---+---+---+---+   +------+ 
 |                   |   | mark | 7 6 |   |   |   |   |   |      | 
 +-------------------+   +------+-----+---+---+---+---+   +------+ 
                                                                   

By contrast, if the name is that of a verb, then the name is transferred into the stack without evaluating it. Hence a subsequent assignment changes the verb applied.

  f=: +   ((f=:-) , f) 4
+ _4 _4
 
  f =: +   '((f =: -),f) 4' EVM
+ _4 _4

   hist ''
                                                                             
 Queue                         Stack                               Rule      
                                                                             
 +-------------------------+   +------+------+--+--+--+--+--+--+   +-------+ 
 |mark ( ( f =: - ) , f ) 4|   |      |      |  |  |  |  |  |  |   |       | 
 +-------------------------+   +------+------+--+--+--+--+--+--+   +-------+ 
 |mark ( ( f =: - ) , f )  |   | 4    |      |  |  |  |  |  |  |   |       | 
 +-------------------------+   +------+------+--+--+--+--+--+--+   +-------+ 
 |mark ( ( f =: - ) , f    |   | )    | 4    |  |  |  |  |  |  |   |       | 
 +-------------------------+   +------+------+--+--+--+--+--+--+   +-------+ 
 |mark ( ( f =: - ) ,      |   | f    | )    | 4|  |  |  |  |  |   |       | 
 +-------------------------+   +------+------+--+--+--+--+--+--+   +-------+ 
 |mark ( ( f =: - )        |   | ,    | f    | )| 4|  |  |  |  |   |       | 
 +-------------------------+   +------+------+--+--+--+--+--+--+   +-------+ 
 |mark ( ( f =: -          |   | )    | ,    | f| )| 4|  |  |  |   |       | 
 +-------------------------+   +------+------+--+--+--+--+--+--+   +-------+ 
 |mark ( ( f =:            |   | -    | )    | ,| f| )| 4|  |  |   |       | 
 +-------------------------+   +------+------+--+--+--+--+--+--+   +-------+ 
 |mark ( ( f               |   | =:   | -    | )| ,| f| )| 4|  |   |       | 
 +-------------------------+   +------+------+--+--+--+--+--+--+   +-------+ 
 |mark ( (                 |   | f    | =:   | -| )| ,| f| )| 4|   |assign | 
 +-------------------------+   +------+------+--+--+--+--+--+--+   +-------+ 
 |mark ( (                 |   | -    | )    | ,| f| )| 4|  |  |   |       | 
 +-------------------------+   +------+------+--+--+--+--+--+--+   +-------+ 
 |mark (                   |   | (    | -    | )| ,| f| )| 4|  |   |paren  | 
 +-------------------------+   +------+------+--+--+--+--+--+--+   +-------+ 
 |mark (                   |   | -    | ,    | f| )| 4|  |  |  |   |       | 
 +-------------------------+   +------+------+--+--+--+--+--+--+   +-------+ 
 |mark                     |   | (    | -    | ,| f| )| 4|  |  |   |trident| 
 +-------------------------+   +------+------+--+--+--+--+--+--+   +-------+ 
 |mark                     |   | (    | - , f| )| 4|  |  |  |  |   |paren  | 
 +-------------------------+   +------+------+--+--+--+--+--+--+   +-------+ 
 |mark                     |   | - , f| 4    |  |  |  |  |  |  |   |       | 
 +-------------------------+   +------+------+--+--+--+--+--+--+   +-------+ 
 |                         |   | mark | - , f| 4|  |  |  |  |  |   |monad  | 
 +-------------------------+   +------+------+--+--+--+--+--+--+   +-------+ 
 |                         |   | mark | _4 _4|  |  |  |  |  |  |   |       | 
 +-------------------------+   +------+------+--+--+--+--+--+--+   +-------+ 
                                                                             

A1.3.11 Review of Parsing Rules

rule   stack before   stack after   where Z is ...
monad   EDGE Verb Noun etc   EDGE Z etc   Verb applied to Noun
monad2   EAVN Verb1 Verb2 Noun   EAVN Verb1 Z   Verb2 applied to Noun
dyad   EAVN Noun1 Verb Noun2   EAVN Z etc   Verb applied to Noun1 and Noun2
adverb   EAVN VN Adv etc   EAVN Z etc   Adv applied to VN
conj   EAVN VN1 Conj VN2   EAVN Z etc   Conj applied to VN1 and VN2
trident   EAVN VN1 Verb2 Verb3   EAVN Z etc   fork (VN1 Verb2 Verb3)
bident   EDGE CAVN1 CAVN2 etc   EDGE Z etc   bident (CAVN1 CAVN2)
assign   NN Asgn CAVN etc   Z etc etc   CAVN
paren   ( CAVN ) etc   Z etc etc   CAVN

A1.4 Effects of Parsing Rules

Now we look at some of the effects, of the parsing rules. In what follows, notice how the parsing rules in effect give rise to implicit parentheses.

A1.4.1 Dyad Has Long Right Scope

Consider the expression 4+3-2, which means 4+(3-2).

  4 + 3 - 2   4 + (3-2)   '4+3-2' EVM
5 5 5

   hist ''
                                                  
 Queue              Stack                  Rule   
                                                  
 +--------------+   +------+---+---+---+   +----+ 
 |mark 4 + 3 - 2|   |      |   |   |   |   |    | 
 +--------------+   +------+---+---+---+   +----+ 
 |mark 4 + 3 -  |   | 2    |   |   |   |   |    | 
 +--------------+   +------+---+---+---+   +----+ 
 |mark 4 + 3    |   | -    | 2 |   |   |   |    | 
 +--------------+   +------+---+---+---+   +----+ 
 |mark 4 +      |   | 3    | - | 2 |   |   |    | 
 +--------------+   +------+---+---+---+   +----+ 
 |mark 4        |   | +    | 3 | - | 2 |   |dyad| 
 +--------------+   +------+---+---+---+   +----+ 
 |mark 4        |   | +    | 1 |   |   |   |    | 
 +--------------+   +------+---+---+---+   +----+ 
 |mark          |   | 4    | + | 1 |   |   |    | 
 +--------------+   +------+---+---+---+   +----+ 
 |              |   | mark | 4 | + | 1 |   |dyad| 
 +--------------+   +------+---+---+---+   +----+ 
 |              |   | mark | 5 |   |   |   |    | 
 +--------------+   +------+---+---+---+   +----+ 
                                                  

Here we have an example of a general rule: a dyadic verb takes as its right argument as much as possible, so in this example + is applied to 3-2, not just 3.

Further, a dyadic verb takes as left argument as little as possible. In this example the left argument of - is just 3, not 4+3. Hence a dyadic verb is said to have a "long right scope" and a "short left scope".

A1.4.2 Operators Before Verbs

Adverbs and conjunctions get applied first, and then the resulting verbs:

  * & 1 % 2   (*&1) % 2   '* & 1 % 2' EVM
0.5 0.5 0.5

   hist ''
                                                                
 Queue              Stack                              Rule     
                                                                
 +--------------+   +------+-----+-----+---+---+---+   +------+ 
 |mark * & 1 % 2|   |      |     |     |   |   |   |   |      | 
 +--------------+   +------+-----+-----+---+---+---+   +------+ 
 |mark * & 1 %  |   | 2    |     |     |   |   |   |   |      | 
 +--------------+   +------+-----+-----+---+---+---+   +------+ 
 |mark * & 1    |   | %    | 2   |     |   |   |   |   |      | 
 +--------------+   +------+-----+-----+---+---+---+   +------+ 
 |mark * &      |   | 1    | %   | 2   |   |   |   |   |      | 
 +--------------+   +------+-----+-----+---+---+---+   +------+ 
 |mark *        |   | &    | 1   | %   | 2 |   |   |   |      | 
 +--------------+   +------+-----+-----+---+---+---+   +------+ 
 |mark          |   | *    | &   | 1   | % | 2 |   |   |      | 
 +--------------+   +------+-----+-----+---+---+---+   +------+ 
 |              |   | mark | *   | &   | 1 | % | 2 |   |conj  | 
 +--------------+   +------+-----+-----+---+---+---+   +------+ 
 |              |   | mark | *&1 | %   | 2 |   |   |   |monad2| 
 +--------------+   +------+-----+-----+---+---+---+   +------+ 
 |              |   | mark | *&1 | 0.5 |   |   |   |   |monad | 
 +--------------+   +------+-----+-----+---+---+---+   +------+ 
 |              |   | mark | 0.5 |     |   |   |   |   |      | 
 +--------------+   +------+-----+-----+---+---+---+   +------+ 
                                                                

A1.4.3 Operators Have Long Left Scope

In the following examples, note that values of verbs are shown in the "parenthesized representation" (see Chapter 27) to show their structure.

An adverb or a conjunction takes as its left argument as much as possible. Look at the structure of these verbs: evidently the / adverb and the @ conjunction take everything to their left:

  f @ g /   f & g @ h   'f&g@h' EVM
(f@g)/ (f&g)@h (f&g)@h

   hist ''
                                                                
 Queue              Stack                                Rule   
                                                                
 +--------------+   +------+---------+---+---+---+---+   +----+ 
 |mark f & g @ h|   |      |         |   |   |   |   |   |    | 
 +--------------+   +------+---------+---+---+---+---+   +----+ 
 |mark f & g @  |   | h    |         |   |   |   |   |   |    | 
 +--------------+   +------+---------+---+---+---+---+   +----+ 
 |mark f & g    |   | @    | h       |   |   |   |   |   |    | 
 +--------------+   +------+---------+---+---+---+---+   +----+ 
 |mark f &      |   | g    | @       | h |   |   |   |   |    | 
 +--------------+   +------+---------+---+---+---+---+   +----+ 
 |mark f        |   | &    | g       | @ | h |   |   |   |    | 
 +--------------+   +------+---------+---+---+---+---+   +----+ 
 |mark          |   | f    | &       | g | @ | h |   |   |    | 
 +--------------+   +------+---------+---+---+---+---+   +----+ 
 |              |   | mark | f       | & | g | @ | h |   |conj| 
 +--------------+   +------+---------+---+---+---+---+   +----+ 
 |              |   | mark | f&g     | @ | h |   |   |   |conj| 
 +--------------+   +------+---------+---+---+---+---+   +----+ 
 |              |   | mark | (f&g)@h |   |   |   |   |   |    | 
 +--------------+   +------+---------+---+---+---+---+   +----+ 
                                                                

Thus operators are said to have a "long left scope". In the example of f&g@h we see that the right argument of & is just g, not g@h . Thus conjunctions have "short right scope".

A1.4.4 Train on the Left

The long left scope of an adverb does not extend through a train: parentheses may be needed to get the desired effect. Suppose f g h is intended as a train, then compare the following:

  (f g h) /   f g h /   'f g h / ' EVM
(f g h)/ f g (h/) f g (h/)

   hist ''
                                                            
 Queue            Stack                           Rule      
                                                            
 +------------+   +------+----------+----+----+   +-------+ 
 |mark f g h /|   |      |          |    |    |   |       | 
 +------------+   +------+----------+----+----+   +-------+ 
 |mark f g h  |   | /    |          |    |    |   |       | 
 +------------+   +------+----------+----+----+   +-------+ 
 |mark f g    |   | h    | /        |    |    |   |       | 
 +------------+   +------+----------+----+----+   +-------+ 
 |mark f      |   | g    | h        | /  |    |   |adv    | 
 +------------+   +------+----------+----+----+   +-------+ 
 |mark f      |   | g    | h/       |    |    |   |       | 
 +------------+   +------+----------+----+----+   +-------+ 
 |mark        |   | f    | g        | h/ |    |   |       | 
 +------------+   +------+----------+----+----+   +-------+ 
 |            |   | mark | f        | g  | h/ |   |trident| 
 +------------+   +------+----------+----+----+   +-------+ 
 |            |   | mark | f g (h/) |    |    |   |       | 
 +------------+   +------+----------+----+----+   +-------+ 
                                                            
   

Similarly for a conjunction (with a right argument)

  f g h @ +   'f g h @ +' EVM
f g (h@+) f g (h@+)

   hist ''
                                                                 
 Queue              Stack                              Rule      
                                                                 
 +--------------+   +------+-----------+-----+-----+   +-------+ 
 |mark f g h @ +|   |      |           |     |     |   |       | 
 +--------------+   +------+-----------+-----+-----+   +-------+ 
 |mark f g h @  |   | +    |           |     |     |   |       | 
 +--------------+   +------+-----------+-----+-----+   +-------+ 
 |mark f g h    |   | @    | +         |     |     |   |       | 
 +--------------+   +------+-----------+-----+-----+   +-------+ 
 |mark f g      |   | h    | @         | +   |     |   |       | 
 +--------------+   +------+-----------+-----+-----+   +-------+ 
 |mark f        |   | g    | h         | @   | +   |   |conj   | 
 +--------------+   +------+-----------+-----+-----+   +-------+ 
 |mark f        |   | g    | h@+       |     |     |   |       | 
 +--------------+   +------+-----------+-----+-----+   +-------+ 
 |mark          |   | f    | g         | h@+ |     |   |       | 
 +--------------+   +------+-----------+-----+-----+   +-------+ 
 |              |   | mark | f         | g   | h@+ |   |trident| 
 +--------------+   +------+-----------+-----+-----+   +-------+ 
 |              |   | mark | f g (h@+) |     |     |   |       | 
 +--------------+   +------+-----------+-----+-----+   +-------+ 
                                                                 

However, for a conjunction with no right argument, the left scope does extend through a train:

  f g h @   'f g h @' EVM
(f g h)@ (f g h)@

   hist ''
                                                              
 Queue            Stack                             Rule      
                                                              
 +------------+   +------+----------+---+---+---+   +-------+ 
 |mark f g h @|   |      |          |   |   |   |   |       | 
 +------------+   +------+----------+---+---+---+   +-------+ 
 |mark f g h  |   | @    |          |   |   |   |   |       | 
 +------------+   +------+----------+---+---+---+   +-------+ 
 |mark f g    |   | h    | @        |   |   |   |   |       | 
 +------------+   +------+----------+---+---+---+   +-------+ 
 |mark f      |   | g    | h        | @ |   |   |   |       | 
 +------------+   +------+----------+---+---+---+   +-------+ 
 |mark        |   | f    | g        | h | @ |   |   |       | 
 +------------+   +------+----------+---+---+---+   +-------+ 
 |            |   | mark | f        | g | h | @ |   |trident| 
 +------------+   +------+----------+---+---+---+   +-------+ 
 |            |   | mark | f g h    | @ |   |   |   |bident | 
 +------------+   +------+----------+---+---+---+   +-------+ 
 |            |   | mark | (f g h)@ |   |   |   |   |       | 
 +------------+   +------+----------+---+---+---+   +-------+ 
                                                              

By contrast, in the case of of f @ g /, notice how the "conj" rule is applied before there is a chance to apply the "adverb" rule"

  f @ g /   'f @ g / ' EVM
(f@g)/ (f@g)/

   hist ''
                                                         
 Queue            Stack                           Rule   
                                                         
 +------------+   +------+--------+---+---+---+   +----+ 
 |mark f @ g /|   |      |        |   |   |   |   |    | 
 +------------+   +------+--------+---+---+---+   +----+ 
 |mark f @ g  |   | /    |        |   |   |   |   |    | 
 +------------+   +------+--------+---+---+---+   +----+ 
 |mark f @    |   | g    | /      |   |   |   |   |    | 
 +------------+   +------+--------+---+---+---+   +----+ 
 |mark f      |   | @    | g      | / |   |   |   |    | 
 +------------+   +------+--------+---+---+---+   +----+ 
 |mark        |   | f    | @      | g | / |   |   |    | 
 +------------+   +------+--------+---+---+---+   +----+ 
 |            |   | mark | f      | @ | g | / |   |conj| 
 +------------+   +------+--------+---+---+---+   +----+ 
 |            |   | mark | f@g    | / |   |   |   |adv | 
 +------------+   +------+--------+---+---+---+   +----+ 
 |            |   | mark | (f@g)/ |   |   |   |   |    | 
 +------------+   +------+--------+---+---+---+   +----+ 
                                                         

A1.4.5 Presumption of Verb

A name with no value assigned is presumed to be a verb. For example, in the following the two names make a hook:

  Blue Skies   'Blue Skies' EVM
Blue Skies Blue Skies

   hist ''
                                                               
 Queue               Stack                            Rule     
                                                               
 +---------------+   +-------+------------+-------+   +------+ 
 |mark Blue Skies|   |       |            |       |   |      | 
 +---------------+   +-------+------------+-------+   +------+ 
 |mark Blue      |   | Skies |            |       |   |      | 
 +---------------+   +-------+------------+-------+   +------+ 
 |mark           |   | Blue  | Skies      |       |   |      | 
 +---------------+   +-------+------------+-------+   +------+ 
 |               |   | mark  | Blue       | Skies |   |bident| 
 +---------------+   +-------+------------+-------+   +------+ 
 |               |   | mark  | Blue Skies |       |   |      | 
 +---------------+   +-------+------------+-------+   +------+ 
                                                               
   

This is the end of Appendix 1.


NEXT
Table of Contents
Index


The examples in this chapter were executed using J version 701. This chapter last updated 29 Jul 2012
Copyright © Roger Stokes 2012. This material may be freely reproduced, provided that this copyright notice is also reproduced.


>> << Pri JfC LJ Phr Dic Voc !: Rel NuVoc wd Help Learning J