Type compatibility in Oberon

Karl Landström 2017-10-11

The type compatibility rules in the Oberon report (2016) are not completely specified. To fill in the missing pieces, appendix A from The Programming Language Oberon-2 (1993) by Hanspeter Mössenböck and Niklaus Wirth is used here as a template, with adjustments for the 2016 edition of Oberon.

Note that Oberon uses non-strict name equivalence. This means that for a type identifier Ta, the type declarations Tb = Ta and Tc = Ta make Tb and Tc equivalent.

Same types

Two variables a and b with types Ta and Tb are of the same type if

  1. Ta and Tb are both denoted by the same type identifier, or
  2. Ta is declared to equal Tb in a type declaration of the form Ta = Tb, or
  3. a and b appear in the same identifier list in a variable, record field, or formal parameter declaration and are not open arrays.
Equal types

Two types Ta and Tb are equal if

  1. Ta and Tb are the same type, or
  2. Ta and Tb are open array types with equal element types, or
  3. Ta and Tb are procedure types whose formal parameter lists match.
Assignment compatible

An expression e of type Te is assignment compatible with a variable v of type Tv if one of the following conditions hold:

  1. Te and Tv are the same type;
  2. Tv is CHAR and e is a single-character string constant;
  3. Te is INTEGER and Tv is BYTE or vice versa;
  4. Tv is ARRAY n OF CHAR, e is a string constant with m characters, and m < n;
  5. Te is an open array, Tv is a non-open array and their element types are the same;
  6. Te and Tv are record types and Te is an extension of Tv and the dynamic type of v is Tv;
  7. Te and Tv are pointer types and Te is an extension of Tv;
  8. Tv is a pointer or a procedure type and e is NIL;
  9. Tv is a procedure type and e is the name of a procedure whose formal parameters match those of Tv.
Array compatible

An actual parameter a of type Ta is array compatible with a formal parameter f of type Tf if

  1. Tf and Ta are the same type, or
  2. Tf is an open array, Ta is any array, and their element types are array compatible, or
  3. Tf is ARRAY OF CHAR and a is a string.
Expression compatible

For a given operator, the types of its operands are expression compatible if they conform to the following table (which shows also the result type of the expression). Character arrays that are to be compared must contain 0X as a terminator. Type T1 must be an extension of type T0:

Operator First operand Second operand Result type
+ − * INTEGER, BYTE INTEGER, BYTE INTEGER
+ − * / REAL REAL REAL
SET SET SET
DIV MOD INTEGER, BYTE INTEGER, BYTE INTEGER
OR & ~ BOOLEAN BOOLEAN BOOLEAN
= # < <= > >= CHAR, one-char string CHAR, one-char string BOOLEAN
INTEGER, BYTE INTEGER, BYTE BOOLEAN
REAL REAL BOOLEAN
character array, string character array, string BOOLEAN
= # BOOLEAN BOOLEAN BOOLEAN
SET SET BOOLEAN
pointer type T0 or T1, or NIL pointer type T0 or T1, or NIL BOOLEAN
procedure type T or NIL procedure type T or NIL BOOLEAN
IN INTEGER, BYTE SET BOOLEAN
IS type T0 type T1 BOOLEAN
Matching formal parameter lists

Two formal parameter lists match if

  1. they have the same number of parameters, and
  2. they have either the same function result type or none, and
  3. parameters at corresponding positions have equal types, and
  4. parameters at corresponding positions are both either value or variable parameters.