© Jørgen Steensgaard-Madsen, Copenhagen, 2006
Description of operators                                                  
 
   

The syntax for using an operation is the ususal prefix notation, where the name of the operation appears first. An operator is similar to an operation, but the syntax for its use is the infix notation, where the name appears between two operands.
Operators may be overloaded, i.e. different meanings may be associated with an operator symbol, and one of these may be determined by the types of the operands. Overloading is different from polymorphism, which indicates some type independence of an operation, e.g. the length of a list.
An expression is best described as an alternating sequence of operands and operators. Each operator in principle requires a left and a right operand, but either one of them may be left out. Similarly, an operator symbol may not appear visibly in the sequence, if a notion of an optional operator has been defined for the operands. An operand is an application of an operation, i.e. written in prefix. Operators and operations have names composed of characters from disjoint sets.
An operator symbol has properties that are common to all meanings associated with it, e.g. the precedence which describe how tightly the operator is bound to operands in comparison with others, e.g.
== 3 both eq

This introduces the symbol as an operator with precedence 3, associating to the left because the precedence is odd, tells that both operands will be coerced when possible, and finally that the semantics routines will have derived names like
For each meaning associated with the symbol a description is needed:
[ ==(int,int):int ]
[ ==(real,real):int ]

The spine of interpreters will analyse commands, find the types of operands, and select the appropriate meaning for the operator according to the operand types.
The meaning of an operator may be polymorphic. One example is the meaning of the meaning of operator for lists. That meaning is defined in terms of a meaning of the operator for the type of elements of the list. The dependency can be resolved automatically due to the type analysis of expressions. The support for this complicates the description of operators, but not their use.
Testing for equality is expressed with the operator. The meanings of the operator for lists and pairs require meanings for the components. Examples:
demo> pairs(loop(INTERPRET));
demo> [pair(2,pair([1,3],4))]==[pair(2,pair([1,3],4))];
1
demo> [pair(2,pair([0,3],4))]==[pair(2,pair([1,3],4))];
0
demo> [pair(2,pair(3,4))]==[pair(2,pair("abc",4))];
 >> Type conflict: I expected int but I see string
 >> Type conflict: I expected int but I see string
demo> [pair(2,pair("xx",4))]==[pair(2,pair("abc",4))];
 ?? I cannot find any meaning for == in
 ...

The required additional meanings are automatically supplied as extra, hidden operands when needed. The last (abbreviated) error message indicates that the actual interpreter has no equality defined for strings.
For pairs the abstract syntax of equality is substantially more complex:
[ == OF A, B {A==A,B==B}(A B Pair, A B Pair):int ]

The names A and B denote types, so the operator expects both left and right operands of type A B Pair. In addition two meanings of the operator are required, one with both operands of type A and one with both of type B. As illustrated the use of the operator is straightforward, since the required meanings are supplied automatically by completion.
For comparisons it is sometimes more convenient to use a three valued operator. A possible description that conforms to the compairison of strings in Unix might be
[ ??(string,string):int ]
so that x ?? y returns a negative value if x is less than y , zero if the values are equal, and positive otherwise.
It can be convenient to define the ?? operator for composite types, e.g. List types. It might depend on the same operator on component types, just like the equality operator described above. The main advantage is that the ?? operator allows generic definitions of common relational operators, e.g. 
[ <= OF T {T??T}(T,T):int ]
The semantics of this operator can easily be defined so that the less-than-or-equal operator can be used with any operands for which the ?? operator is defined.
A special operator symbol, ?, is used in descriptions to denote an optional operator in commands. Its description follows the general pattern, e.g.
[ ?(string,string):string ]
with concatenation as its probable semantics. One has to be careful with optional operators, since some syntactical ambiguity arises which is resolved by giving `applications' priority.
Unary operators may be defined by using the typename NONE for a missing operand. There is an alternative to this that will be explained along with examples of defining the semantics of operators in GNU C.

Contents

Demo language
·Implementation tool
Copyrights


Introduction
·Principles
Interpreter construction


Deep- and surface structures
Design issues
Abstract type operatorss
·Description of operator
Name introduction
Data abstraction and control



File translated from TEX by TTH, version 3.33.
On 18 Oct 2006, 16:47.
SourceForge.net Logo