From 548c239a85b56557f28a1831835458f95cb34c86 Mon Sep 17 00:00:00 2001 From: dsh Date: Fri, 6 Nov 2026 09:12:15 +0800 Subject: [PATCH] Tape-length search beats the layer search on the full adder Five layers cost 95 characters; six layers cost 90. Both are in examples/ and both pass the eight-combination check on the ROT-8 machine in tests/synth_test.cc (which now takes a file argument). sacrifice B, FLIP {S,T} sacrifice A, FLIP {B} sacrifice B, FLIP {A,Ci,S} sacrifice Ci, FLIP {B,Co,T} sacrifice S, SET1 {Ci,Co} sacrifice T, FLIP {B,Ci,S,Co} Cost 75 in the model, 90 on the tape once the inter-block hops and the two output lines are counted. The goal was popped at 75 only after everything below 75 had been expanded, so 75 is the minimum of this cost model. Trading a layer for a shorter tape is not a contradiction: the layer search cannot see that a small, cheap block can be worth more than one wide, expensive one. --- README.md | 22 ++++++++++ examples/full_add_cost.r8asm | 85 ++++++++++++++++++++++++++++++++++++ tests/synth_test.cc | 7 +-- 3 files changed, 111 insertions(+), 3 deletions(-) create mode 100644 examples/full_add_cost.r8asm diff --git a/README.md b/README.md index 0d49a24..0100d8b 100644 --- a/README.md +++ b/README.md @@ -88,6 +88,28 @@ full_add(`r8veritas<6>`):**5 层,2038 万状态,7.7 秒,穷尽搜索 ## 按纸带长度搜(`solve_cost`) +**结论先说**:full_add 上 5 层那版是 95 字符,代价搜索找到的 6 块版是 **90 字符**。两个都在 +`examples/` 里,都跑过那台 ROT-8 虚拟机,8 种输入组合全对: + +| 版本 | 层数 | 模型代价 | 真实纸带 | +|---|---|---|---| +| 层数最优(`full_add.r8asm`) | 5 | 82 | 95 字符 | +| 代价最优(`full_add_cost.r8asm`) | 6 | 75 | **90 字符** | + +代价最优解: + +``` +sacrifice B, FLIP {S,T} +sacrifice A, FLIP {B} +sacrifice B, FLIP {A,Ci,S} +sacrifice Ci, FLIP {B,Co,T} +sacrifice S, SET1 {Ci,Co} +sacrifice T, FLIP {B,Ci,S,Co} +``` + +层数最优不等于纸带最优:多一层反而少 5 个字符。Dijkstra 在弹出代价 75 之前已经把 < 75 的 +区域穷尽,所以 75 是模型内的最小值。 + 层数最少不等于纸带最短。`solve_cost` 用同一张状态图跑 Dijkstra,边权就是这个块汇编出来 多少字符: diff --git a/examples/full_add_cost.r8asm b/examples/full_add_cost.r8asm new file mode 100644 index 0000000..cffc8a9 --- /dev/null +++ b/examples/full_add_cost.r8asm @@ -0,0 +1,85 @@ +%include "asm/lib.r8asm" + +; 按纸带长度搜索得到的最优解:6 个块,模型代价 75(5 层那版是 82)。 +; 这个文件不走 %synth,直接手写,用来实测真实长度并跑虚拟机。 +.var A 0 +.var B 1 +.var Ci 2 +.var S 3 +.var Co 4 +.var T 5 + +; sacrifice B, FLIP {S,T} +.tp B +biz + .tp S + flb + .tp T + flb + .tp B + flb +rnz + +; sacrifice A, FLIP {B} +.tp A +biz + .tp B + flb + .tp A + flb +rnz + +; sacrifice B, FLIP {A,Ci,S} +.tp B +biz + .tp A + flb + .tp Ci + flb + .tp S + flb + .tp B + flb +rnz + +; sacrifice Ci, FLIP {B,Co,T} +.tp Ci +biz + .tp B + flb + .tp Co + flb + .tp T + flb + .tp Ci + flb +rnz + +; sacrifice S, SET1 {Ci,Co} +.tp S +biz + .tp Ci + set_1 + .tp Co + set_1 + .tp S + flb +rnz + +; sacrifice T, FLIP {B,Ci,S,Co} +.tp T +biz + .tp B + flb + .tp Ci + flb + .tp S + flb + .tp Co + flb + .tp T + flb +rnz + +output S +output Co diff --git a/tests/synth_test.cc b/tests/synth_test.cc index 3817370..250999c 100644 --- a/tests/synth_test.cc +++ b/tests/synth_test.cc @@ -71,10 +71,11 @@ struct machine { } // namespace -int main() { +int main(int argc, char **argv) { + const char *file = argc > 1 ? argv[1] : "examples/full_add.r8asm"; r8asm assembler; - if (!assembler.import_file("examples/full_add.r8asm", 8)) { - std::cerr << "could not assemble examples/full_add.r8asm\n"; + if (!assembler.import_file(file, 8)) { + std::cerr << "could not assemble " << file << "\n"; return 1; } const std::string tape = assembler.export_rot8();