|
Chapter 31: Evaluating Expressions31.1 IntroductionIn this chapter 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:
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:
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 script which can be viewed as a web page and is also available as a downloadable J script. I should say here that the EVM adverb is only my interpretation of the Dictionary (as indeed is the whole of this book). For the examples below, EVM appears to compute the same results as J itself, but it has not been exhaustively tested. 31.2 First ExampleEvaluation of an expression such as 2+3 can be modelled by offering the argument '2+3' (a string, notice) to the modelling adverb EVM.
We see that '2+3' EVM computes the same value as 2+3, but EVM also produces a trace, or history, of the evaluation process. The history of 2+3 looks like this: show ''
We see successive stages of the process. 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. Stage 0 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 10 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 stage 0 to stage 4. At stage 4, 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. Stage 5 shows the results of applying the "dyad". in stage 4. 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 represented by 3 global variables, Qh Sh and Rh. The history can be displayed directly in the execution window by entering the expression Qh,.Sh,.Rh. Qh ,. Sh ,. Rh +----+-+-+-+----+-+-+-+----+ |mark|2|+|3| | | | | | +----+-+-+-+----+-+-+-+----+ |mark|2|+| |3 | | | | | +----+-+-+-+----+-+-+-+----+ |mark|2| | |+ |3| | | | +----+-+-+-+----+-+-+-+----+ |mark| | | |2 |+|3| | | +----+-+-+-+----+-+-+-+----+ | | | | |mark|2|+|3|dyad| +----+-+-+-+----+-+-+-+----+ | | | | |mark|5| | | | +----+-+-+-+----+-+-+-+----+ However, a more readable display is produced by the show function which computes, from Qh Sh and Rh, a fragment of HTML. This HTML is not for viewing in the execution window but rather for pasting into a web page such as this one. Corresponding to Qh Sh and Rh as above we would see: show ''
31.3 Parsing RulesIn this section an example is shown of each of the 10 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, adjective, 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'.
31.3.1 Monad RuleIf 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:
show ''
31.3.2 Second Monad RuleAn 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:
show ''
31.3.3 Dyad RuleThe scheme is dyad rule: EAVN NOUN1 VERB NOUN2 etc => EAVN Z etc where Z is VERB applied dyadically to NOUN1 and NOUN2. For example.
show ''
31.3.4 Adverb RuleAn 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:
show ''
31.3.5 Conjunction RuleThe 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:
show ''
31.3.6 Fork RuleThe scheme is: fork rule: EAVN VERB1 VERB2 VERB3 etc => EAVN Z etc where Z is a single verb defined as the fork (VERB1 VERB2 VERB3). For example: f=: +/ g=: % h=: #
show ''
31.3.7 Trident RuleWe can write "CAVN" to denote an item which is a conjunction or adverb or verb or noun. The scheme is: trident rule: EDGE CAVN1 CAVN2 CAVN3 etc => EDGE Z etc where Z is a single item (itself a CAVN) defined by one of the schemes for tridents in Chapter 13. In the following example the expression (g,@) is of the form verb, verb conjunction, and so is an instance of trident VVC. Therefore it is a conjunction.
show ''
31.3.8 Bident RuleThe scheme is: bident rule: EDGE CAVN1 CAVN2 etc => EDGE Z etc where Z is a single item (itself a CAVN) defined by one of the schemes for bidents in Chapter 13. In the following example the expression (1 &) is of the form noun conjunction, and so is an instance of bident NC. Therefore it is an adverb.
show ''
31.3.9 Assignment RuleWe 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.
show ''
31.3.10 Parenthesis RuleThe scheme is: paren rule: ( CAVN ) etc => Z etc where Z is the value of CAVN. For example:
show ''
31.3.11 Examples of TransferThe 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.
show ''
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.
show ''
31.3.12 Review of Parsing Rules
31.4 Effects of Parsing RulesNow 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. 31.4.1 Dyad Has Long Right ScopeConsider the expression 4+3-2, which means 4+(3-2).
show ''
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". 31.4.2 Operators Before VerbsAdverbs and conjunctions get applied first, and then the resulting verbs:
'* & 1 % 2' EVM 0.5 show ''
31.4.3 Operators Have Long Left ScopeAn adverb or a conjunction takes as its left argument as much as possible. In the following, look at the structure of the resulting verbs: evidently the / adverb and the @ conjunction take everything to their left:
show ''
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". 31.4.4 Train on the LeftThe 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 / ' EVM f g (h/) show ''
Similarly for a conjunction (with a right argument)
show ''
However, for a conjunction with no right argument, the left scope does extend through a train:
show ''
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"
show ''
31.4.5 Presumption of VerbA name with no value assigned is presumed to be a verb. For example, in the following the three names make a fork:
show ''
This is the end of Chapter 31 |
Copyright © Roger Stokes 2001. This material may be freely reproduced, provided that this copyright notice is also reproduced.
last updated 12 Sep 01