Re: Scope of implied-do index
- From: Brooks Moses <bmoses-nospam@xxxxxxxxxxxxxxxxxx>
- Date: Wed, 30 Aug 2006 21:28:42 -0700
Steven G. Kargl wrote:
ISTR reading in the F95 standard that the scope of the index
of an implied-do is limited to the implied-do. I can't locate
where I would have gotten that impression, so a pointer to
the F95 text would be much appreciated.
In particular, I interested in what the standard says about
integer i
real x(10)
do i = 1, 10
x = (/ (i, i=1, 10) /)
end do
I spent about 15 minutes tracking down why that was illegal, and then came across section 14.1.3, which caused me to change my opinion and consider it to be legal:
"The name of a variable that appears as the DO variable of an implied-DO in a DATA statement or an array constructor has the scope of the implied-DO list. It is a scalar variable that has the type and type parameters that it would have if it were the name of a variable in the scoping unit that includes the DATA statement or array constructor, and this type shall be integer type."
It goes on to discuss the names of variables that are indices in FORALL statements in much the same way, and for those explicitly notes that they effectively mask the corresponding variable from the outer scoping unit rather than accessing it -- i.e. FORALL(i=1:10) x(i)=i would be legal there.
From this I conclude that not only is your code legal, but the following (where i is an array in the outer scope, but a scalar within the construct) is also legal:
integer i(2,0:3,5)
real x(10)
x = (/ (i, i=1, 10) /)
Note, though, that this does _not_ apply to implied-do loops in input/output statements! Those appear to be treated differently, and are not limited in scope. Aside from the fact that they're not included in the above quote, consider section 14.7.5 (Events that cause variables to become defined), which includes "Beginning of execution of the action specified by an implied-DO list in an input/output statement causes the implied-DO variable to become defined." -- input/output implied-DO loops are the only ones mentioned here. Also, section 9.4.2 requires that the variable for an i/o implied-do be a scalar variable; this restriction is not there for array-constructor or data implied-do loops.
Similarly, the F2003 standard's section 16.5.7 (things you can't do with variables you aren't allowed to define or undefine, like INTENT(IN) arguments) specifically mentions "A do-variable in a do-stmt or io-implied-do", but not in implied-do constructs in array constructors or DATA statements.
An additional complication, though, is that the text for array-constructor implied-do loops (section 4.5) states that nested implied-DO loops in an array constructor shall not use the same variable (by which I presume it means "the same variable name".) So they can't completely ignore _all_ outer context.
The text for input/output implied-do loops is similar, but includes the additional requirement that the variables in nested input/output implied-do loops shall not be associated -- which distinction makes sense with the fact that their scope is beyond that of the implied-do loop; the association would be irrelevant if they're not really the same variable as the one in the outer context.
Thus, I conclude that your code sample and mine are legal, but were you to replace the array constructor in either of those with a write statement containing an implied-do, they would then be illegal.
I further conclude that this distinction between types of implied-do loops and different rules for them is an extremely fine hair for the standard to be splitting, and I wish to register my sense of "Eeew!" at it!
- Brooks
--
The "bmoses-nospam" address is valid; no unmunging needed.
.
- Follow-Ups:
- Re: Scope of implied-do index
- From: Steven G. Kargl
- Re: Scope of implied-do index
- From: Richard E Maine
- Re: Scope of implied-do index
- From: glen herrmannsfeldt
- Re: Scope of implied-do index
- From: Brooks Moses
- Re: Scope of implied-do index
- References:
- Scope of implied-do index
- From: Steven G. Kargl
- Scope of implied-do index
- Prev by Date: Re: Scope of implied-do index
- Next by Date: Re: Scope of implied-do index
- Previous by thread: Re: Scope of implied-do index
- Next by thread: Re: Scope of implied-do index
- Index(es):
Relevant Pages
|