Xref: utzoo comp.lang.misc:1462 comp.lang.pascal:816 Path: utzoo!mnetor!uunet!husc6!cmcl2!rutgers!ucla-cs!lanai!gast From: gast@lanai.cs.ucla.edu (David Gast) Newsgroups: comp.lang.misc,comp.lang.pascal Subject: Re: Threatening Pascal Loops Message-ID: <11369@shemp.CS.UCLA.EDU> Date: 18 Apr 88 22:52:52 GMT References: <2827@enea.se> <1557@pasteur.Berkeley.Edu> <2773@mmintl.UUCP> <294@tmsoft.UUCP> <11047@shemp.CS.UCLA.EDU> <3364@omepd> Sender: news@CS.UCLA.EDU Reply-To: gast@lanai.UUCP (David Gast) Organization: UCLA Computer Science Department Lines: 75 In article <3364@omepd> bobdi@omepd.UUCP (Bob Dietrich) writes: >In article <11047@shemp.CS.UCLA.EDU> gast@lanai.UUCP (David Gast) writes: >Sorry, but the existing ANSI/IEEE and ISO Pascal standards do not allow such >modification. [of the loop control variable]. Yes, I realize that. That was exactly my point. The program name was illegal. One might have thought the name was somewhat self-explanatory. The main comment went on to say: >>{This program is not legal pascal, but the compiler does not detect the >> error. If the scope of the index variable were local to the for loop >> (ala Algol 68), instead of global, then this error would be detected >> at compiler time. } >There is a concept called "threatening" explained under the >for-statement (section 6.8.3.9). Basically, if a variable has been >"threatened", it cannot be used as a for control variable. Dietrich goes on to explain what threatened means. Are there any compilers that detect every possible instance of a threatened variable? Presumably there must be some other verbage to the extent that an index variable cannot be accessed after the loop ends. The following program is also illegal, but again, I suspect that most compilers do not detect the error. The standard Berkeley 4.3 compiler does not. program ILLEGAL (output); var i : integer; procedure p ; begin writeln(i); /* Variable I is undefined at this point */ end; begin for i := 1 to 10 do writeln(i); p; end. Essentially, slightly over stated, one will have to guarantee that a loop control variable is never used outside the scope of the for loop or do extensive run-time checking. Of course, you can put extra checks into the compiler to try and detect these errors. These checks take time and make the compiler bigger. As long as you end up (de facto) not allowing the loop control variable to be used outside the scope of the for loop, why not do the sensible thing in the first place? Why not decide *in the language definition* that the loop control variable is local to the loop? That is the decision Algol 68 made and it works. It is impossible to assign to a loop control variable in Algol 68. And no concept like "threatening" is needed so it is easier to learn. One could have the following syntax: for FOR-CONT-VAR : S-TYPE := LB to UB do STATEMENT The one argument against this change is that such a change would make previously valid pascal programs illegal. But the use threatening also makes previously, valid pascal programs illegal. That is, a loop control variable can be threatened without actually being assigned to. Such a program would not have been illegal under the old standard, but it is, as I understand it, under the new standard. The old standard Pascal had many type insecurities. Perhaps the new standard eliminates all of these; probably it doesn't. As I no longer have to use Pascal, I do not care to investigate myself. If the new Pascal, however, does not have any type insecurities, then it is a far different language. The defining document and the compilers and run time systems are also undoubtedly much bigger as well.