Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Should loop {} really loop indefinitely?

by moritz (Cardinal)
on Oct 07, 2013 at 16:24 UTC ( [id://1057278]=note: print w/replies, xml ) Need Help??


in reply to Should loop {} really loop 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.

The doubts about infinite repetition of no-ops look artificially constructed to me, and make a fine topic for a meditation, as long as you don't actually propose to change the language specification.

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. And that's because not all operations that work on finite things work the same on infinite things, which is a well-known theme in mathematics. (For example proof by induction only works over finite numbers).

Replies are listed 'Best First'.
Re^2: Should loop {} really loop indefinitely?
by grondilu (Friar) on Oct 07, 2013 at 18:45 UTC

    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.

    Edit:

    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?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1057278]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (7)
As of 2024-03-28 13:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found