2026-08-28·作者 Sijie Wang#idea#math

minsky-examples

Minsky machine — small programs

Parent: minsky-machine

Everything is built from INC and test/DEC. (Rj = register j.)

Addition: R1 := R1 + R2 (consumes R2)

L0: if R2 > 0 then { DEC R2; goto L1 } else goto END
L1: INC R1; goto L0
END: halt

"Pour R2 into R1, one unit at a time."

Trace on R1=2, R2=3:

stepinstrR1R2
start23
1DEC R2, INC R132
2DEC R2, INC R141
3DEC R2, INC R150
4R2=0 → halt50

Copy: R2 := R1 (non-destructive, via temp R3)

A: if R1>0 then { DEC R1; INC R2; INC R3; goto A } else goto B   # drain R1 into R2 and R3
B: if R3>0 then { DEC R3; INC R1; goto B } else halt             # refill R1 from R3

Leaves R2 = R1 with R1 restored — you can't read without destroying, so you copy through a spare and put it back.

Multiply: R3 := R1 * R2

Add R1 to R3 exactly R2 times (using the copy trick so R1 survives each round). Repeated addition is all you have — and it's enough.

A simulator (Haskell — program is data)

import qualified Data.Map as M
data Instr = Inc Int Int          -- Inc r goto
           | Dec Int Int Int      -- Dec r ifPositive ifZero
           | Halt
type Prog = M.Map Int Instr       -- label -> instruction
type Regs = M.Map Int Int

step :: Prog -> (Int, Regs) -> Maybe (Int, Regs)
step p (pc, rs) = case M.lookup pc p of
  Just (Inc r l)     -> Just (l, M.insertWith (+) r 1 rs)
  Just (Dec r pos z) -> case M.findWithDefault 0 r rs of
                          0 -> Just (z,   rs)
                          n -> Just (pos, M.insert r (n-1) rs)
  _                  -> Nothing            -- Halt (or fell off) -> stop

run :: Prog -> (Int, Regs) -> (Int, Regs)
run p st = maybe st (run p) (step p st)

Same shape as the TM simulator: a tiny driver + the program as data — which is exactly what makes a universal machine possible.

关于这一条

sijie 的一条 wiki 条目。本站的 AI 以同一套语料库为依据、以 sijie 的口吻作答,并附有指向此类条目的引用——回答会花 sijie 的钱,所以它在访问码后等待: 输入访问码 →

minsky-examples — Sijie Wang