Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Out of curiosity is there any point to a loop that doesn’t produce some effect in its body? Obviously I mean traditional imperative looping constructs and not map functions that return a value.


One example is to cause a fairly predictable delay in embedded microcontrollers for bit-banging[1] digital signals. If we know the clock frequency and the cycle cost of a certain instruction (say, add or nop), we can specify how many times to execute the instruction to get the desired delay time. Very inefficient, but very low level processors like PICs have limited capabilities for interrupt based timing, and their low-power requirements make it viable to waste cycles.

    inline void delay (int desired_delay) {
        int i = 0;
        while (i < desired_delay)
            i++;
    }
While "delay" is obviously an effect, it's not an effect in the PLT meaning of the word, since it causes no state changes to the program, and effect systems don't concern themselves with compute cost. The signal emission happens outside of the delay loop - usually inside some enclosing loop which obviously does have side-effects.

[1]:https://en.wikipedia.org/wiki/Bit_banging


Your loop has the effect of mutating the counter. It's not an effect that leaks out of the function, but it is an effect of the loop, which was what your parent was asking about, I think.


No. In fact for example the C++ standard says such a loop can be optimized away by the compiler, even if syntax suggests it is an infinite loop.


> No.

This isn't quite correct, as demonstrated by the example of C++, because the act of turning a function that never halts (i.e. a function that is guaranteed to diverge) into a function that might halt is an observable effect. Rust actually relies on the fact that infinite loops are infinite, and they had to submit patches to LLVM to make it possible for languages to opt out of C++-style semantics: https://github.com/rust-lang/rust/issues/28728


C++ is pretty unique in considering infinite loops without side effects undefined behavior. It's allowed in C[1] as well as pretty much all mainstream programming languages from the low level to the high level (for good reason). Furthermore, there's an active proposal to match C by removing this undefined behavior in the cases C does[2].

Even in C++, it's uncommon for compilers to exploit it (especially in the trivial case) probably because, even if it's undefined, generally such behavior are pretty surprising and unexpected (and programmers do write infinite loops on purpose!).

Having non-effect producing infinite loops is useful (and sometimes is the only safe way of performing an operation), especially in some low level code. Another commenter pointed out the idea of delays in microcontrollers[3] but I want to provide an example from operating systems.

Take for example a panic function. At the end of it, you usually have an infinite loop of some sort, partly because you can't really safely do too much and panics generally represent a completely unrecoverable state from the operating system's point of view. A trivial

    for (;;) {}
...is useful.

[1]: For constant expressions. [2]: https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2023/p28... [3]: https://news.ycombinator.com/item?id=40542860




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: