Next: , Previous: Data-terms, Up: Syntax   [Contents]


2.16 Variable scoping

There are three sorts of variables in Mercury: ordinary variables, type variables, and inst variables.

Variables occurring in types are called type variables. Variables occurring in insts or modes are called inst variables. Variables that occur in data-terms, and that are not inst variables or type variables, are called ordinary variables.

(Type variables can occur in data-terms in the right-hand [Type] operand of an explicit type qualification. Inst variables can occur in data-terms in the right-hand [Mode] operand of an explicit mode qualification. Apart from that, all other variables in data-terms are ordinary variables.)

The three different variable sorts occupy different namespaces: there is no semantic relationship between two variables of different sorts (e.g. a type variable and an ordinary variable) even if they happen to share the same name. However, as a matter of programming style, it is generally a bad idea to use the same name for variables of different sorts in the same clause.

The scope of ordinary variables is the clause or declaration in which they occur, unless they are quantified, either explicitly (see Goals) or implicitly (see Implicit quantification).

The scope of type variables in a predicate or function’s type declaration extends over any explicit type qualifications (see Explicit type qualification) in the clauses for that predicate or function, and over ‘pragma type_spec’ (see Type specialization) declarations for that predicate or function, so that explicit type qualifications and ‘pragma type_spec’ declarations can refer to those type variables. The scope of any type variables in an explicit type qualification which do not occur in the predicate or function’s type declaration is the clause in which they occur.

The scope of inst variables is the clause or declaration in which they occur.


Next: , Previous: Data-terms, Up: Syntax   [Contents]