Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikipediaThe Free Encyclopedia
Search

Counter machine

From Wikipedia, the free encyclopedia
Abstract machine used in a formal logic and theoretical computer science

Acounter machine orcounter automaton is anabstract machine used in aformal logic andtheoretical computer science tomodel computation. It is the most primitive of the four types ofregister machines. A counter machine comprises a set of one or more unboundedregisters, each of which can hold a single non-negative integer, and a list of (usually sequential) arithmetic and control instructions for the machine to follow. The counter machine is typically used in the process of designing parallel algorithms in relation to the mutual exclusion principle. When used in this manner, the counter machine is used to model the discrete time-steps of a computational system in relation to memory accesses. By modeling computations in relation to the memory accesses for each respective computational step, parallel algorithms may be designed in such a matter to avoid interlocking, the simultaneous writing operation by two (or more) threads to the samememory address.

Counter machines with three counters can compute anypartial recursive function of a single variable.Counter machines with two counters areTuring complete: they can simulate any appropriately-encoded Turing machine. Counter machines with only a single counter can recognize a proper superset of theregular languages and a subset of thedeterministic context free languages.[1]

Basic features

[edit]

For a given counter machine model the instruction set is tiny—from just one to six or seven instructions. Most models contain a few arithmetic operations and at least one conditional operation (ifcondition is true, then jump). Threebase models, each using three instructions, are drawn from the following collection. (The abbreviations are arbitrary.)

  • CLR (r): CLeaR registerr. (Setr to zero.)
  • INC (r): INCrement the contents of registerr.
  • DEC (r): DECrement the contents of registerr.
  • CPY (rj, rk): CoPY the contents of registerrj to registerrk leaving the contents ofrj intact.
  • JZ (r, z): IF registerr contains Zero THEN Jump to instructionz ELSE continue in sequence.
  • JE (rj, rk, z): IF the contents of registerrj Equals the contents of registerrk THEN Jump to instructionz ELSE continue in sequence.

In addition, a machine usually has a HALT instruction, which stops the machine (normally after the result has been computed).

Using the instructions mentioned above, various authors have discussed certain counter machines:

  • set 1: { INC (r), DEC (r), JZ (r, z) }, (Minsky (1961, 1967), Lambek (1961))
  • set 2: { CLR (r), INC (r), JE (rj, rk, z) }, (Ershov (1958), Peter (1958) as interpreted by Shepherdson–Sturgis (1964); Minsky (1967); Schönhage (1980))
  • set 3: { INC (r), CPY (rj, rk), JE (rj, rk, z) }, (Elgot–Robinson (1964), Minsky (1967))

The three counter machine base models have the same computational power since the instructions of one model can be derived from those of another. All are equivalent to the computational power ofTuring machines. Due to their unary processing style, counter machines are typically exponentially slower than comparable Turing machines.

Alternative names, alternative models

[edit]
Main article:Counter-machine model

Thecounter machine models go by a number of different names that may help to distinguish them by their peculiarities. In the following the instruction "JZDEC ( r )" is a compound instruction that tests to see if a register r is empty; if so then jump to instruction Iz, else if not then DECrement the contents of r:

  • Minsky machine, becauseMarvin Minsky (1961) formalized the model.Abacus machine, the name Lambek (1961) gave to his simplification of the Melzak (1961) model, and what Boolos–Burgess–Jeffrey (1974) call it.Lambek machine, an alternative name Boolos–Burgess–Jeffrey (1974) gave to the abacus machine. Usually uses instruction set (1), but instruction-execution is not default-sequential so the additional parameter 'z' appears to specify the next instruction after INC and as the alternative in JZDEC:
    { INC ( r, z ), JZDEC ( r, ztrue, zfalse) }
  • Program machine,program computer, the names Minsky (1967) gave the model because, like acomputer its instructions proceed sequentially unless a conditional jump is successful. Uses (usually) instruction set (1) but may be augmented similar to the Shepherson–Sturgis model. JZDEC is often split apart:
    { INC ( r ), DEC ( r ), JZ ( r, ztrue )}
  • Successor machine, because it uses the 'successor operation' of, and closely resembles, thePeano axioms. Used as a base for thesuccessor RAM model. Uses instruction set (2) by e.g. Schönhage as a base for his RAM0 and RAM1 models that lead to hispointer machine SMM model,[2][3] also discussed briefly by Van Emde Boas:[4][5]
    { CLR ( r ), INC ( r ), JE ( rj, rk, z ) }
  • Elgot–Robinson model, used to define their RASP model (1964). This model requires one empty register at the start (e.g. [r0] =0). (They augmented the same model with indirect addressing by use of an additional register to be used as an "index" register.)
    { INC (r), CPY ( rs, rd ), JE ( rj, rk, z ) }
  • Shepherdson–Sturgis machine, because these authors formally stated their model in an easily accessible exposition (1963). Uses instruction set (1) augmented with additional convenience instructions (JNZ is "Jump if Not Zero, used in place of JZ):
    { INC ( r ), DEC ( r ), CLR ( r ), CPY ( rj, rk ), JNZ ( r, z ), J ( z ) }
  • Other counter machines: Minsky (1967) demonstrates how to build the three base models (program/Minsky/Lambek-abacus, successor, and Elgot–Robinson) from the superset of available instructions described in the lead paragraph of this article. The Melzak (1961) model is quite different from the above because it includes 'add' and 'subtract' rather than 'increment' and 'decrement'. The proofs of Minsky (1961, 1967) that a single register will suffice for Turing equivalence requires the two instructions { MULtiply k, and DIV k } to encode and decode theGödel number in the register that represents the computation. Minsky shows that if two or more registers are available then the simpler INC, DEC etc. are adequate (but theGödel number is still required to demonstrateTuring equivalence; also demonstrated in Elgot–Robinson 1964).

Formal definition

[edit]

A counter machine consists of:

  1. Labeled unbounded integer-valued registers: a finite (or infinite in some models) set of registersr0 ... rn each of which can hold any single non-negative integer (0, 1, 2, ... - i.e. unbounded). The registers do their own arithmetic; there may or may not be one or more special registers e.g. "accumulator" (SeeRandom-access machine for more on this).
  2. Astate register that stores/identifies the current instruction to be executed. This register is finite and separate from the registers above; thus the counter machine model is an example of theHarvard architecture
  3. List of labelled, sequential instructions: A finite list of instructionsI0 ... Im. The program store (instructions of thefinite-state machine) is not in the same physical "space" as the registers. Usually, but not always, likecomputer programs the instructions are listed in sequential order; unless a jump is successful, the default sequence continues in numerical order. Each of the instructions in the list is from a (very) small set, but this set does not include indirection. Historically most models drew their instructions from this set:
{ Increment (r), Decrement (r), Clear (r); Copy (rj,rk), conditional Jump if contents of r=0, conditional Jump if rj=rk, unconditional Jump, HALT }
Some models have either further atomized some of the above into no-parameter instructions, or combined them into a single instruction such as "Decrement" preceded by conditional jump-if-zero "JZ ( r, z )" . The atomization of instructions or the inclusion of convenience instructions causes no change in conceptual power, as any program in one variant can be straightforwardly translated to the other.
Alternative instruction-sets are discussed in the supplementRegister-machine models.

Example: COPY the count from register #2 to #3

[edit]

This example shows how to create three more useful instructions:clear,unconditional jump, andcopy.

Afterwardrs will contain its original count (unlike MOVE which empties the source register, i.e., clears it to zero).

The basic set (1) is used as defined here:

InstructionEffect on register "j"Effect on Instruction Counter Register ICRSummary
INC ( j )[j] +1 → j[IC] +1 → ICIncrement contents of register j; next instruction
DEC ( j )[j] -1 → j[IC] +1 → ICDecrement contents of register j; next instruction
JZ ( j, z)IF [j] = 0 THEN Iz → IC ELSE [IC] +1 → ICIf contents of register j=0 then instruction z else next instruction
HALT

Initial conditions

[edit]

Initially, register #2 contains "2". Registers #0, #1 and #3 are empty (contain "0"). Register #0 remains unchanged throughout calculations because it is used for the unconditional jump. Register #1 is a scratch pad. The program begins with instruction 1.

Final conditions

[edit]

The program HALTs with the contents of register #2 at its original count and the contents of register #3 equal to the original contents of register #2, i.e.,

[2] = [3].

Program high-level description

[edit]

The program COPY ( #2, #3) has two parts. In the first part the programmoves the contents of source register #2 to both scratch-pad #1 and to destination register #3; thus #1 and #3 will becopies of one another and of the original count in #2, but #2 is cleared in the process of decrementing it to zero. Unconditional jumps J (z) are done by tests of register #0, which always contains the number 0:

[#2] →#3; [#2] →#1; 0 →#2

In the second the part the programmoves (returns, restores) the contents of scratch-pad #1 back to #2, clearing the scratch-pad #1 in the process:

[#1] →#2; 0 →#1

Program

[edit]

The program, highlighted in yellow, is shown written left-to-right in the upper right.

A "run" of the program is shown below. Time runs down the page. The instructions are in yellow, the registers in blue. The program is flipped 90 degrees, with the instruction numbers (addresses) along the top, the instruction mnemonics under the addresses, and the instruction parameters under the mnemonics (one per cell):

12345678910← Instruction number (address)
JZDECINCINCJZJZDECINCJZH← Instruction
223101120← Register number
61106← Jump-to instruction number
stepICInst #reg #J-addrreg0reg1reg2reg3reg4IC
start002001move [#2] to #1 and #3:
11JZ26002001→2JZJump fails: register #2 contains 2
22DEC20002→1002→3DECDecrement register #2 from 2 to 1
33INC300010→103→4INCIncrement register #3 from 0 to 1
44INC1000→11104→5INCIncrement register #1 from 0 to 1
55JZ01011105→1JZU-Jump: register #0 is empty
61JZ26011101→2JZJump fails: register #2 contains 1
72DEC20011→0102→3DECDecrement register #2 from 1 to 0
83INC300101→203→4INCIncrement register #3 from 1 to 2
94INC1001→20204→5INCIncrement register #1 from 1 to 2
105JZ01020205→1JZU-Jump: register #0 is empty
111JZ26020201→6JZJump !: register #2 is empty
move [1] to 2:
126JZ110020206→7JZJump fails: register #1 contains 2
137DEC1002→10207→8DECDecrement register #1 from 2 to 1
148INC20010→1208→9INCIncrement register #2 from 0 to 1
159JZ06011209→6JZU-Jump: register #0 is empty
166JZ110011206→7JZJump fails: register #1 contains 1
177DEC1001→01207→8DECDecrement register #1 from 1 to 0
188INC20001→2208→9INCIncrement register #2 from 1 to 2
199JZ06002209→6JZU-Jump: register #0 is empty
206JZ110002206→10JZJump !: register #1 is empty
2110H000022010→10HHALT

The partial recursive functions: building "convenience instructions" using recursion

[edit]

The example above demonstrates how the first basic instructions { INC, DEC, JZ } can create three more instructions—unconditional jump J, CLR, CPY. In a sense CPY used both CLR and J plus the base set. If register #3 had had contents initially, thesum of contents of #2 and #3 would have ended up in #3. So to be fully accurate CPY program should have preceded its moves with CLR (1) and CLR (3).

However, we do see that ADD would have been possible, easily. And in fact the following is summary of how theprimitive recursive functions such as ADD, MULtiply and EXPonent can come about.[6]

  • Beginning instruction set: { DEC, INC, JZ, H }
  • Define unconditional "Jump J (z)" in terms of JZ ( r0, z ) given that r0 contains 0.
{ J, DEC, INC, JZ, H }
  • Define "CLeaR ( r ) in terms of the above:
{ CLR, J, DEC, INC, JZ, H }
  • Define "CoPY ( rj, rk )" while preserving contents of rj in terms of the above:
{ CPY, CLR, J, DEC, INC, JZ, H }
The above is the instruction set of Shepherdson–Sturgis (1963).
  • Define "ADD ( rj, rk, ri )", (perhaps preserving the contents of rj, and rk ), by use of the above:
{ ADD, CPY, CLR, J, DEC, INC, JZ, H }
  • Define " MULtiply ( rj, rk, ri )" (MUL) (perhaps preserving the contents of rj, rk), in terms of the above:
{ MUL, ADD, CPY, CLR, J, DEC, INC, JZ, H }
  • Define "EXPonential ( rj, rk, ri )" (EXP) (perhaps preserving the contents of rj, rk ) in terms of the above,
{ EXP, MUL, ADD, CPY, CLR, J, DEC, INC, JZ, H }

In general, we can buildany partial- or total-primitive recursive function that we wish, by using the same methods. Indeed, Minsky (1967), Shepherdson–Sturgis (1963) and Boolos–Burgess–Jeffrey (1974) give demonstrations of how to form the fiveprimitive recursive function "operators" (1-5 below) from the base set (1).

But what about fullTuring equivalence? We need to add the sixth operator—theμ operator—to obtain the full equivalence, capable of creating the total- and partial-recursive functions:

  1. Zero function (or constant function)
  2. Successor function
  3. Identity function
  4. Composition function
  5. Primitive recursion (induction)
  6. μ operator (unbounded search operator)

The authors show that this is done easily within any of the available base sets (1, 2, or 3) (an example can be found atμ operator). This means that any mu recursive function can be implemented as a counter machine,[7] despite the finite instruction set and program size of the counter machine design. However, the required construction may be counter-intuitive, even for functions that are relatively easy to define in more complex register machines such as therandom-access machine. This is because the μ operator can iterate an unbounded number of times, but any given counter machine cannot address an unbounded number of distinct registers due to the finite size of its instruction list.

For instance, the above hierarchy of primitive recursive operators can be further extended past exponentiation into higher-ordered arrow operations inKnuth's up-arrow notation. For any fixedk{\displaystyle k}, the functionQ(x,y)=xky{\displaystyle Q(x,y)=x\uparrow ^{k}y} is primitive recursive, and can be implemented as a counter machine in a straightforward way. But the functionR(n,x,y)=xny{\displaystyle R(n,x,y)=x\uparrow ^{n}y} is not primitive recursive. One may be tempted to implement the up-arrow operatorR{\displaystyle R} using a construction similar to the successor, addition, multiplication, and exponentiation instructions above, by implementing acall stack so that the function can be applied recursively on smaller values ofn{\displaystyle n}. This idea is similar to how one may implement the function in practice in many programming languages. However, the counter machine cannot use an unbounded number of registers in its computation, which would be necessary to implement a call stack that can grow arbitrarily large. The up-arrow operation can still be implemented as a counter machine since it is mu recursive, however the function would be implemented by encoding an unbounded amount of information inside a finite number of registers, such as by usingGödel numbering.

Problems with the counter machine model

[edit]
The problems are discussed in detail in the articleRandom-access machine. The problems fall into two major classes and a third "inconvenience" class:

(1)Unboundedcapacities of registers versus bounded capacities of state-machine instructions: How will the machine create constants larger than the capacity of its finite-state machine?

(2)Unboundednumbers of registers versus bounded numbers of state-machine instructions: How will the machine access registers with address-numbers beyond the reach/capability of its finite-state machine?

(3)The fully reduced models are cumbersome:

Shepherdson and Sturgis (1963) are unapologetic about their 6-instruction set. They have made their choice based on "ease of programming... rather than economy" (p. 219 footnote 1).

Shepherdson and Sturgis' instructions ( [r] indicates "contents of register r"):

    • INCREMENT ( r ) ; [r] +1 → r
    • DECREMENT ( r ) ; [r] -1 → r
    • CLEAR ( r ) ; 0 → r
    • COPY ( rs to rd ) ; [rs] → rd
    • JUMP-UNCONDITIONAL to instruction Iz
    • JUMP IF [r] =0 to instruction Iz

Minsky (1967) expanded his 2-instruction set { INC (z), JZDEC (r, Iz) } to { CLR (r), INC (r), JZDEC (r, Iz), J (Iz) } before his proof that a "Universal Program Machine" can be built with only two registers (p. 255ff).

Two-counter machines are Turing equivalent (with a caveat)

[edit]

For everyTuring machine, there is a 2CM that simulates it, given that the 2CM's input and output are properly encoded. This is proved in Minsky's book (Computation, 1967, p. 255-258), and an alternative proof is sketched below in three steps. First, a Turing machine can be simulated by a finite-state machine (FSM) equipped with two stacks. Then, two stacks can be simulated by four counters. Finally, four counters can be simulated by two counters.The two counters machine use the set of instruction { INC ( r, z ), JZDEC ( r, ztrue, zfalse) }.

Step 1: A Turing machine can be simulated by two stacks.

[edit]

A Turing machine consists of an FSM and an infinite tape, initially filled with zeros, upon which the machine can write ones and zeros. At any time, the read/write head of the machine points to one cell on the tape. This tape can be conceptually cut in half at that point. Each half of the tape can be treated as astack, where the top is the cell nearest the read/write head, and the bottom is some distance away from the head, with all zeros on the tape beyond the bottom. Accordingly, a Turing machine can be simulated by an FSM plus two stacks. Moving the head left or right is equivalent to popping a bit from one stack and pushing it onto the other. Writing is equivalent to changing the bit before pushing it.

Step 2: A stack can be simulated by two counters.

[edit]

A stack containing zeros and ones can be simulated by two counters when the bits on the stack are thought of as representing abinary number (the topmost bit on the stack being the least significant bit). Pushing a zero onto the stack is equivalent to doubling the number. Pushing a one is equivalent to doubling and adding 1. Popping is equivalent to dividing by 2, where theremainder is the bit that was popped. Two counters can simulate this stack, in which one of the counters holds a number whose binary representation represents the bits on the stack, and the other counter is used as a scratchpad. To double the number in the first counter, the FSM can initialize the second counter to zero, then repeatedly decrement the first counter once and increment the second counter twice. This continues until the first counter reaches zero. At that point, the second counter will hold the doubled number. Halving is performed by decrementing one counter twice and increment the other once, and repeating until the first counter reaches zero. The remainder can be determined by whether it reached zero after an even or anodd number of steps, where the parity of the number of steps is encoded in the state of the FSM.

Step 3: Four counters can be simulated by two counters.

[edit]

As before, one of the counters is used as scratchpad. The other holds aninteger whoseprimefactorization is 2a3b5c7d. The exponentsa,b,c, andd can be thought of as four virtual counters that are being packed (viaGödel numbering) into a single real counter. If the real counter is set to zero then incremented once, that is equivalent to setting all the virtual counters to zero. If the real counter is doubled, that is equivalent to incrementinga, and if it's halved, that's equivalent to decrementinga. By a similar procedure, it can be multiplied or divided by 3, which is equivalent to incrementing or decrementingb. Similarly,c andd can be incremented or decremented. To check if a virtual counter such asc is equal to zero, just divide the real counter by 5, see what the remainder is, then multiply by 5 and add back the remainder. That leaves the real counter unchanged. The remainder will have been nonzero if and only ifc was zero.

As a result, an FSM with two counters can simulate four counters, which are in turn simulating two stacks, which are simulating a Turing machine. Therefore, an FSM plus two counters is at least as powerful as a Turing machine. A Turing machine can easily simulate an FSM with two counters, therefore the two machines have equivalent power.

The caveat: *If* its counters are initialised toN and 0, then a 2CM cannot calculate 2N

[edit]

This result, together with a list of other functions ofN that are not calculable by a two-counter machine —when initialised withN in one counter and 0 in the other — such asN2, sqrt(N), log2(N), etc., appears in a paper by Schroeppel (1972). The result is not surprising, because the two-counter machine model was proved (by Minsky) to be universal only when the argumentN is appropriately encoded (by Gödelization) to simulate a Turing machine whose initial tape containsN encoded in unary; moreover, the output of the two-counter machine will be similarly encoded. This phenomenon is typical of very small bases of computation whose universality is proved only by simulation (e.g., manyTuring tarpits, the smallest-knownuniversal Turing machines, etc.).

The proof is preceded by some interesting theorems:

  • "Theorem: A three-counter machine can simulate a Turing machine" (p. 2, also cf Minsky 1967:170-174)
  • "Theorem: A 3CM [three-counter machine] can compute any partial recursive function of one variable. It starts with the argument [i.e.N] in a counter, and (if it halts) leaves the answer [i.e. F(N)] in a counter." (p. 3)
  • "Theorem: A counter machine can be simulated by a 2CM [two-counter machine], provided an obscure coding is accepted for the input and output" [p. 3; the "obscure coding" is: 2W3X5Y7Z where the simulated counters are W, X, Y, Z]
  • "Theorem: Any counter machine can be simulated by a 2CM, provided an obscure coding is accepted for the input and output." (p. 3)
    • "Corollary: thehalting problem for 2CMs is unsolvable.
    • "Corollary: A 2CM can compute any partial recursive function of one argument, provided the input is coded as 2N and the output (if the machine halts) is coded as 2answer." (p. 3)
  • "Theorem: There is no two counter machine that calculates 2N [if one counter is initialised toN]." (p. 11)

With regard to the second theorem that "A 3CM can compute any partial recursive function" the author challenges the reader with a "Hard Problem: Multiply two numbers using only three counters" (p. 2). The main proof involves the notion that two-counter machines cannot compute arithmetic sequences with non-linear growth rates (p. 15) i.e. "the function 2X grows more rapidly than anyarithmetic progression." (p. 11).

A practical example of calculation by counting

[edit]

TheFriden EC-130 calculator had no adder logic as such. Its logic was highly serial, doing arithmetic by counting. Internally, decimal digits were radix-1 — for instance, a six was represented by six consecutive pulses within the time slot allocated for that digit. Each time slot carried one digit, least significant first. Carries set a flip-flop which caused one count to be added to the digit in the next time slot.[citation needed]

Adding was done by an up-counter, while subtracting was done by a down-counter, with a similar scheme for dealing with borrows.[citation needed]

The time slot scheme defined six registers of 13 decimal digits, each with asign bit.Multiplication and division were done basically by repeated addition and subtraction. Thesquare root version, the EC-132, effectively subtracted consecutive odd integers, each decrement requiring two consecutive subtractions. After the first, the minuend was incremented by one before the second subtraction.[citation needed]

See also

[edit]

References

[edit]
  1. ^Hopcroft, Motwani & Ullman 2003, p. 352.
  2. ^Schönhage 1973.
  3. ^Schönhage 1980.
  4. ^van Emde Boas 1989, pp. 34–37.
  5. ^van Emde Boas 1990, pp. 32–35.
  6. ^SeeBoolos, Burgess & Jeffrey (2007, pp. 45–51)
  7. ^Boolos, Burgess & Jeffrey 2007.

Bibliography

[edit]

Further reading

[edit]

External links

[edit]
Retrieved from "https://en.wikipedia.org/w/index.php?title=Counter_machine&oldid=1320654158"
Category:
Hidden categories:

[8]ページ先頭

©2009-2025 Movatter.jp