Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Re^2: Should loop {} really loop indefinitely?

by grondilu (Friar)
on Oct 07, 2013 at 18:45 UTC ( #1057300=note: print w/replies, xml ) Need Help??

in reply to Re: Should loop {} really loop indefinitely?
in thread Should loop {} really loop indefinitely?

Consider this program:

for ^1_000 {}

Would you accept that an optimizer just replaces it with a single no-op? Or that it just ignores it entirely, like a comment? To me it would make sense, but I understand it's far from obvious and could be argued (not all for (...) {} constructs should be ignored, as there could be side-effects in the ...).

Though if you do accept to ignore such a construct, then consider the state of 'loop {}' (with the current semantic, when loop {} is looping doing nothing) during the 1_000th loop. At this point, the program has been doing something (one thousand consecutive no-ops) that it would not have done in an other context (i.e. with for ^1000 {}). It's as if, as soon as you decide your loop will not end, you made your program forget the fact that consecutive no-ops could be ignored. Does not seem right.

In other words, at any point during the execution of loop {}, assuming it didn't die as I suggest, your program is supposed to have been 'looping' for a duration $T, during which it is supposed to have executed $n no-ops, with $n being any positive natural integer. Whatever the value of $n is, it will mean that your program will have had executed $n no-ops in $T seconds, although normally $n no-ops can be executed in exactly zero seconds (since they are normally ignored). So the behavior is not consistent.

That being said, as you mentioned this is just a meditation. It probably does not matter much anyway, and you Perl 6 guys do such a great job that I don't want to upset you with this or anything. I'm totally fine with 'loop {}' keeping doing what it is currently doing. It's more a philosophical issue than anything, I guess.

To try to answer on a more technical level: an optimizer is allowed to unroll loops as long as no behavior changes, but if the optimizer unrolls infinitely many iterations for noops into one, it makes an error.

Indeed, because that would mean that Inf * 0 == 0, which is not true. But it is also not true that Inf * 0 == Inf, which would mean that the program must run indefinitely.


Since loop { } could either mean "do nothing" or "loop infinitely", and one of these alternatives is clearly useless, it's pretty obvious to me what the construct does.

My point is that loop {} actually means neither "do nothing", nor "loop infinitely".

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1057300]
and the shadows deepen...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (3)
As of 2018-01-21 01:14 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (227 votes). Check out past polls.