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*x19

is (in outline) evaluated by the steps:

obtain the value assigned to x giving 3

compute 5 * 3 giving 15

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.

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
downloadable J script.

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 ''

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, QhSh 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:

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: