[comp.lang.modula3] Modula-3 syntax and semantics for constants

buschman@tubsibr.uucp (Andreas Buschmann) (12/17/90)

Hello!
About constant declarations and constant expressions:
How are constant expressions to be handled, what can be seen in a
constant declaration.
As an example let's have the following program

	MODULE Main;
	IMPORT Stdio, Wr, Fmt;
	CONST i = j;
	CONST j = 10;
	BEGIN
	    Wr.PutText (Stdio.stdout, Fmt.Int (i) & "\n");
	END Main.

Compiled with SRC Modula3 the program prints 10.
In the report (21.10.89) it is said: "Expressions whose values can be
determined statically are called constant expressions; ..." (p.2).
But what is really allowed in constant expressions? In the report at
least procedural operators are used in examples. What else would be
ok?

	VAR	a : INTEGER := 10;
	CONST	b : INTEGER = a;

is rejected by the compiler, but the value of b could be determined
statically to be 10.
Also

	PROCEDURE p (x : INTEGER) : INTEGER =
	  BEGIN  RETURN x + 1;  END p;
	CONST	b : INTEGER = p (1);

is rejected.
A related question:
what can be used in a constant expression?
I need some enlightment.

						Tschuess
							Andreas

p.s. I don't need to know how one or more compiler may handle this
     stuff, but how it is intended to be.

moss%ibis@cs.umass.edu (Eliot Moss) (12/20/90)

>>>>> On Sun, 16 Dec 90 17:17:34 +0100, buschman@tubsibr said:

buschman> In the report (21.10.89) it is said: "Expressions whose values can be
buschman> determined statically are called constant expressions; ..." (p.2).
buschman> But what is really allowed in constant expressions? In the report at
buschman> least procedural operators are used in examples. What else would be
buschman> ok?

I don't know what you mean by "procedural operators", but certain built-in
functions and infix/prefix operators are allowed if their values can be
determined at compile time. User defined procedures are not.

buschman> 	VAR	a : INTEGER := 10;
buschman> 	CONST	b : INTEGER = a;

buschman> is rejected by the compiler, but the value of b could be determined
buschman> statically to be 10.

It is always hard to be precise, but perhaps it would help to say "Expressions
whose values can be determined statically without applying optimization
techniques (such as data flow analysis or inline expansion) are called
constant expressions." You are quite right that with advancing compiler
technology it is harder ot make these definitions precise, though I suspect
most language implementors would interpret the report the same way. It's
roughly: it's a constant expression if we can determine its value statically
by constant folding, i.e., it is an expression built from literals, constant
definitions, and applications of certain functions and operators. (Some
operators give a constant expression even when the expression they're applied
to is not constant; again, that could all be handled.) It could be, and has
been, done; it is just tedious.

Hope this helps; I would tend to agree that Report wording should be
reconsidered if it is not clear .....

		J. Eliot B. Moss, Assistant Professor
		Department of Computer and Information Science
		Lederle Graduate Research Center
		University of Massachusetts
		Amherst, MA  01003
		(413) 545-4206; Moss@cs.umass.edu