4 ms·
> Consider indefinite loops, which require someone to reason about potentially infinite executions of a block of code. What if we didn't have them? I've been t
by jtolmar 9y ago
> Consider indefinite loops, which require someone to reason about potentially infinite executions of a block of code.
What if we didn't have them? I've been thinking about making a language that's primitive recursive, and not actually Turing complete. This doesn't even have to look particularly alien: all the looping constructs are for-range and for-each style, recursion (cycles in the function call graph) is a compiler error, and otherwise it can be a typical programming language. The majority of code can be ported to this world just fine.
Graph searches don't naturally look like for-each loops, but they do iterate over each element in a domain at most once. We can add a for-search loop which iterates over a domain in an order that's determined mid-loop (looks like a for-each loop plus a queue command, the compiler generates the visited set and queue/stack/priority queue). That can be used (a little awkwardly) to do binary search as well, which is the main place people actually use recursion. (I think this is worth stealing, btw.)
Some programs like games and web servers are built around an intentional infinite loop. Having more than one infinite loop on the same thread sounds like an error, so let's say there's an actual infinite loop construct, and only the main method is allowed to use it. This plus generator functions allow dealing with input streams (though somewhat awkwardly). (This makes the language as a whole Turing complete, but most of the code is still not.)