views:

179

answers:

5

Years ago, I have heard that someone was about to demonstrate that every computer program could be solved with just three instructions:

  • Assignment
  • Conditional
  • Loop

Please I would like to hear your opinion. I mean representing any algorithm as a computer program. Do you agree with this?

+1  A: 

You're talking about Turing equivalence. Do some googling and be enlightened.

David M.
+6  A: 

No need. The minimal theoretical computer needs just one instruction. They are called One Instruction Set Computers (OISC for short, kinda like the ultimate RISC).

There are two types. The first is a theoretically "pure" one instruction machine in which the instruction really works like a regular instruction in normal CPUs. The instruction is usually:

subtract and branch if less than zero

or variations thereof. The wikipedia article have examples of how this single instruction can be used to write code that emulates other instructions.

The second type is not theoretically pure. It is the transfer triggered architecture (wikipedia again, sorry). This family of architectures are also known as move machines and I have designed and built some myself.

Some consider move machines cheating since the machine actually have all the regular instructions only that they are memory mapped instead of being part of the opcode. But move machines are not merely theoretical, they are practical (like I said, I've built some myself). There is even a commercially available family of CPUs built by Maxim: the MAXQ. If you look at the MAXQ instruction set (they call it transfer set since there is really only one instruction, I usually call it register set) you will see that MAXQ assembly looks rather like a standard accumulator based architecture.

slebetman
Although it is one instruction, semantically and algorithmically it is still 3 operations: an assignment, a branching operation, and a conditional.
Vivin Paliath
@Vivin I disagree. It is a single instruction. Similarly, addition is a single operation even though it has multiple parts (sum matching bits, do carries, repeat until done). Many operations can be thought of as being composed of multiple smaller operations, but that is irrelevant here. It's not like there is a subtract operation and a separate branch operation. They must be performed together.
Strilanc
Vivian and Strilanc, I think that this is a little like the story of the egg and the hen. I understand both of you, thanks for your comments.
Ramon Araujo
I think the OP was talking about this in the context of algorithms in general. In which case it would be 3 separate instructions (semantically). You could combine a bunch of instructions into just one instruction, and so at the assembly-code level yes, it is just one instruction. But if you were looking at the algorithm as a whole IMO it would be 3 instructions
Vivin Paliath
+3  A: 

The minimal set is a single command, but you have to choose a fitting one, for example - One instruction set computer

When I studied, we used such a "computer" to calculate factorial, using just a single instruction:

SBN - Subtract and Branch if Negative:
SBN A, B, C

Meaning:

if((Memory[A] -= Memory[B]) < 0) goto C  
// (Wikipedia has a slightly different definition)
Kobi
Although it is one instruction, semantically and algorithmically it is still 3 operations: an assignment, a branching operation, and a conditional.
Vivin Paliath
At least two people (including me) based solutions to [Code Golf: Shortest Turing-complete interpreter.](http://stackoverflow.com/questions/1053931/) on these things.
dmckee
+3  A: 

This is a consequence of Turing Completeness, which is something that was established many decades ago.

Alan Turing, the famous computer scientist, proved that any computable function could be computed using a Turing Machine. A Turing machine is a very simple theoretical device which can do only a few things. It can read and write to a tape (i.e. memory), maintain an internal state which is altered by the contents read from memory, and use the internal state and the last read memory cell to determine which direction to move the tape before reading the next memory cell.

The operations of assignment, conditional, and loop are sufficient to simulate a Turing Machine. Reading and writing memory and maintaining state requires assignment. Changing the direction of the tape based on state and memory contents require conditionals and loops. "Loops" in fact are a bit more high-level than what is actually required. All that is really required is that program flow can jump backwards somehow. This implies that you can create loops if you want to, but the language does not need to have an explicit loop construct.

Since these three operations allow simulation of a Turing Machine, and a Turing Machine has been proven to be able to compute any computable function, it follows that any language which provides these operations is also able to compute any computable function.

Edit: And, as other answerers pointed out, these operations do not need to be discrete. You can craft a single instruction which does all three of these things (assign, compare, and branch) in such a way that it can simulate a Turing machine all by itself.

Tyler McHenry
A: 

Take a look at this list of bizarre programming languages.

Some may actually be Turing complete languages. But if it doesn't answer the question, you will at least have fun reading it.

Peladao
Some? To the best of my knowledge, they all are. It'd barely be worth calling a programming language if it wasn't.
Nick Johnson