I know that, in imperative programming languages, a while-do loop is sufficient as a control flow construct to make the language Turing-complete (as far as control flow goes - of course we also need unbounded memory and certain operators...). The gist of my question is: does a do-while loop have the same computational power as a while-do loop? In other words, can a language be Turing-complete if it's impossible to skip instructions entirely.
I realise that some of the semantics here could be a bit ambiguous, so let me phrase the actual question with a specific example:
Brainfuck (BF) is a Turing tarpit where the only control flow is a while-do loop, denoted as [...]
(there is a complete language spec at the bottom of the question, in case you're not familiar with Brainfuck). Let's define a new language BF*, where ,.+-<>
have the same semantics as in BF, but instead of []
we have {}
which denotes a do-while loop. That is, the only difference to BF is that every loop is executed at least once before further iterations can be skipped.
Is BF* Turing-complete? If it is, I'd be interested in how I could translate BF to BF*. If it isn't, how do I prove that?
Some observations of my own:
- Not every BF program can be translated into BF*. For instance, it's impossible to write a program in BF* which may or may not read or print a value - if the program potentially prints one or more values, it will always print at least one. However, there might be a Turing-complete subset of BF which can be translated to BF*.
- We cannot simply translate
[f]
(wheref
is some arbitrary, Brainfuck program consisting only of+-[]<>
) tof-1{f}
(in an attempt to cancel the effect of the first iteration), because a) not every computable function has a computable inverse and b) even if it did,f-1
wouldn't necessarily have fewer loops thanf
so applying this step recursively isn't guaranteed to terminate in the first place.
Here is a quick overview over the Brainfuck language. Brainfuck operates on an infinite tape where each cell contains a byte values, initially zero. Overflows wrap around, so incrementing 255 gives 0 and vice versa. The language consists of 8 instructions:
+ Increment the current cell.
- Decrement the current cell.
> Move tape head to the right.
< Move tape head to the left.
, Input a character from STDIN into the current cell.
. Output the current cell as a character to STDOUT.
[ If the current cell is zero, jump past the matching ].
] If the current cell is non-zero, jump back to just behind the matching [.
[]
is not exactly defining a "while do" loop in BF. as in your table the left and right brackets evaluate the current cell zero/ nonzero. so what is the exact description of the corresponding{}
braces evaluation logic? suggest further dialog/ discussion in Computer Science Chat. also your "observations" are more like "postulates" or "propositions" without proof. $\endgroup${}
would be to make{
do nothing at all and}
the same as]
. I won't have much time over the next few days, but I'll join you in chat when I do find some time. $\endgroup${}
and taking away[]
, is BF* Turing complete. with the understanding that BF[]
is a construct only something somewhat like/ analogous to a while-do loop in Turing complete languages. $\endgroup$