views:

312

answers:

9

Is it possible to construct a turing-complete language in which every string is a correct program?

Any examples? Even better, any real-world examples?

Precisions: by "correct" I mean "compiles", although "runs without error" and "runs without error, and finishes in finite time" would be interesting questions too :)

By string I mean any sequence of bytes, although a restriction to a set of characters will do.

+9  A: 

Yes (assuming by correct you mean compiles, not does something useful). Take brainfuck and map multiple letters to the eight commands.

Edit... oh and redefine an unmatched [ or ] to print "meh. nitpickers" to the screen.

One PhD please ;)

wefwfwefwe
Actually since a turing machine doesn't have the concept of an exception, basically every turing machine fullfills the requirements
Jens Schauder
Wouldn't this be a problem if you have an extra [ or ]?
strager
Actually, most brainfuck implementations will accept arbitrary input and ignore characters other than the important 8. It's only the picky ones that whine about it.
Chris Lutz
+2  A: 

No, because your definition of 'correct' would leave no room for 'incorrect' since 'correct' would include all computable numbers and non-halting programs. To answer in the affirmative would make the question meaningless as 'correct' loses it's definition.

Samuel Danielson
+1  A: 

If by "correct" you mean syntactically, then certainly, yes.

http://en.wikipedia.org/wiki/One%5Finstruction%5Fset%5Fcomputer

http://en.wikipedia.org/wiki/Whitespace%5F%28programming%5Flanguage)

etc

moonshadow
There are problems with Whitespace, similar to those in Brainfuck, where you can jump to an invalid location.
strager
@strager: well, it may crash when it runs, but that's not what he said "correct" meant. If it means "doesn't crash", then the answer is "no", and the proof takes a similar form to the halting problem.
moonshadow
+1  A: 

Turing-complete and "finishes in finite time" are not possible.

Excerpt from wikipedia: http://en.wikipedia.org/wiki/Turing%5Fcompleteness

"One important result from computability theory is that it is impossible in general to determine whether a program written in a Turing-complete language will continue executing forever or will stop within a finite period of time (see halting problem)."

César L
+4  A: 

Existence proof: perl.

soru
Ha-ha, that's a nice one!
Pavel Shved
+7  A: 

This is a compiler for a C-like language expressed in BNF as

<program> ::= <character> | <character> <program>

#!/bin/bash
# full_language.sh

gcc "$1"
if [ $? != 0 ]
then
    echo -e "#!/bin/bash\necho 'hi'" > a.out
    chmod +x a.out
fi
Adrian Panasiuk
This is a feature of the "compiler", not the language...
strager
What is a feature of the compiler?
Adrian Panasiuk
@strager: the point is that this language is a superset of C. Call it "APlang". If an APlang program is identical to a C-with-GNU-extensions program which compiles, then it does the same as that C program. Otherwise, the program prints 'hi' and quits. Clearly this language is Turing complete, as it's a superset of C. Clearly every input compiles (subject to resource limits), and hence is "correct" by the definition of the question. So APlang satisfies the requirements of the question.
Steve Jessop
+1 - this is definitely better than the tired Perl joke.
Chris Lutz
+1  A: 

What you are describing is essentially similar to a mapping from Godel number to original program. Briefly, the idea is that every program should be reducible to a unique integer, and you could use that to draw conclusions about the program, such as with some sort of oracle. One such mapping is the Jot language, which has only two operators, 1 and 0, and the first operator must be a 1.

TokenMacGuy
+2  A: 

Combinatory logic is very near to the requirement You pose. Every string (over the {K, S, @} alphabet) can be extended to a program. Thus, althogh Your requirement is not entirely fulfilled, but its straighforward weakening to prefix property is satisfied by combinatory logic.

Although these programs are syntactically correct, but they do not necessarily halt. That is not necessarily a problem: combinatory logic has originally been developed for investigating theoretical questions, not for a practical programming language (although can be used as such). Are non-halting combinatory logic "programs@ interesting? Do they have at least a theoretical relevance? Of course some of them do! For example, Omega is a non-halting combinatory logic term, but it is subject of articles, book chapters, it has theroetical interestingness, thus we can say, it is meaningful.

Summary: if we regard combinatory logic over alphabet {K, S, @}, the we can say, every possible strings over this alphabet can be extended (as a prefix) to a syntactically correct combinatory logic program. Some of these won't halt, but even those who don't halt can be theoretically interesting, thus "meaningful" (e.g. Omega).

The answer TokenMacGuy provided is better than mine, becasue it approaches the poblem from a broader view, and also because Jot is inspired combinatory logic, thus TokenMacGuy's answer supercedes mine.

physis
The precise statement is somewhat more complicated. Combinatory logic terms can be PARSED without failure. The parser will surely take a random string, but1) it may end parsing in the middle of the string, thus the remainder of the string must be handled as another new program2) it may continue parsing at the end of the string, waiting for more tokens thus the string must be regarded as a prefix of a possible correct program.Combinatory logic programs are self-delimiting, the end of the program can be deduced vithout resorting to an END-OF-PROGRAM symbol.
physis
By using syntactic sugars (adopting other notations for the usage of parantheses), the property (1) can be evaded. Thus it remains only property (2) that weakens Your original requirement. Property (1) can be very useful in situations when self-delimiting is required.
physis
+4  A: 

We can build this up out of any turing-complete language. Take C, for example. If input is a correct C program, than do what it intended to. Otherwise, print "Hello, world!". Or just do nothing.

That makes a turing-complete language where every string is a correct program.

Pavel Shved
Although you could argue that 'hello world' is now an error message?
wefwfwefwe
*OR*, we can argue that "syntax error" is *not* an error message ;-)
Pavel Shved
Would make my job easier...
wefwfwefwe