[comp.lang.ada] FOR Iteration Scheme

pattis@june.cs.washington.edu (Richard Pattis) (07/19/88)

  In the process of writing a CS-1 book that uses Ada as its langauge of
discourse, I am trying to describe formally the semantics of the FOR iteration
scheme using other parts of Ada (whose semantics I explain operationally). Is
the following the most accurate statement I can make?  CAUTION: I am in the
process of learning Ada (trying to compose such explanations helps me), so
this may be a naive question.

FOR I IN E1..E2 LOOP
  <body>
END LOOP;

is equivalent to the following code fragment, where the values of E1 and E2
share subtype <Type-Mark> and <ub> and <is> are secret objects generated by
the Ada compiler.

DECLARE
  <is> : <Type-Mark> := E1;
  <ub> : CONSTANT <Type-Mark> := E2;
BEGIN
  IF <is> <= <ub>
    THEN
      LOOP
        DECLARE
          I : <Type-Mark> CONSTANT := <is>;
        BEGIN
          <body>
        END;
        EXIT WHEN <is> = <ub>;
        <is>:= <Type-Mark>'SUCC(<is>);
      END LOOP;
  END IF;
END;

John.Goodenough@SEI.CMU.EDU (07/19/88)

Such code fragments never seem to be completely equivalent.  Your structure is
not equivalent for expanded names, e.g., PROC.LOOP_NAME.I, won't be possible
because you've introduced an extra level of declare block.

In addition, language commentary AI-00006 says that the subtype of the loop
parameter is determined by the discrete range of the loop.  "In particular, if
the discrete range is static, the loop parameter has a static subtype".  This
affects the legality of case statements:

	for J in 1..2 loop
	    case J is
		when 1 => ... ;
	    	when 2 => ...;
	    end case;

No other choices are allowed or required.

Also, your equivalence should emphasize that the loop parameter is defined by
a base type:

	type T is range 0..255;
	
	for I in T'BASE'FIRST..T'PRED(T'FIRST) loop
	    -- no exception and no positive values!

So, your equivalence should be:

    DECLARE
      <lb> : CONSTANT <Base-Type-Mark> := E1;
      <is> : <Base-Type-Mark> := <lb>;
      <ub> : CONSTANT <Base-Type-Mark> := E2;
    BEGIN
      IF <is> <= <ub>
	THEN
	  LOOP
	    DECLARE
	      I : CONSTANT <Base-Type-Mark> RANGE <lb> .. <ub> := <is>;
	    BEGIN
	      <body>
	    END;
	    EXIT WHEN <is> = <ub>;
	    <is>:= <Type-Mark>'SUCC(<is>);
	  END LOOP;
      END IF;
    END;

The initialization of I with <is> ensures that I cannot be used in a static
expression, since it is not a static constant.

Having had bad luck with trying to define such equivalences, it wouldn't
surprise me if there is something else wrong with the above equivalence (apart
from the expanded name problem).  Any such problem will almost certainly have
to do with interactions with other rules in the language, and not the semantic
behavior of the loop.

John B. Goodenough (Goodenough@sei.cmu.edu)
Software Engineering Institute
Pittsburgh, PA  15213
412-268-6391

dee@linus.UUCP (David E. Emery) (07/20/88)

>From: pattis@june.cs.washington.edu (Richard Pattis)
>  In the process of writing a CS-1 book that uses Ada as its langauge of
>discourse, I am trying to describe formally the semantics of the FOR iteration
>scheme using other parts of Ada (whose semantics I explain operationally). Is
>the following the most accurate statement I can make?  CAUTION: I am in the
>process of learning Ada (trying to compose such explanations helps me), so
>this may be a naive question.

This might also be a naive question:  Why are you writing a book about
a language you do not know?  

Didn't the Boehm-Jacobini proof demonstrate the semantics of FOR loops
using iteration and selection?

				dave emery
				emery@mitre-bedford.arpa