Jump to content

The Principia Machina/TPC-PY/gen yardstick corpus.py: Difference between revisions

From The Invariance Itself
TPC-PY source gen_yardstick_corpus.py (-), SHA-256 b51f0ec9a83216d6; conformed TPS 0.3.0; PROPOSED, not declared (CO-8)
 
m 1 revision imported
(No difference)

Revision as of 04:32, 28 September 2026

# -*- coding: utf-8 -*-
"""Generator for tpc/vectors/yardstick_corpus.json (TPC step 2, increment 5).
Expectations are HAND-DERIVED from the synthetic suite and written literally
below; the generator refuses to write the corpus if the module disagrees.
Run: cd tpc && python3 gen_yardstick_corpus.py (Windows: py ...).
Recovered 2026-09-27 (BASE-GEN; record 0.18.0 section 7) from the TIDY 3
archive, tidy3_2026-09-25/gen_yardstick_corpus.py, SHA-256
74edc22c5818f5c8f93f5004b76128e52dd857bbc95ea11289729e891f49eae1, where it
regenerates the archived corpus byte for byte. Changed in the tree: the
writer only (ensure_ascii=False, so a hidden character is written as
itself, as the published corpus carries it; RD-C8-2). No case changed."""
import copy, json, os, sys
HERE = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, HERE)
import tpc_strike as S
import tpc_yardstick as Y

# The synthetic record suite of increment 3, verbatim (sr9_corpus.json).
SUITE = json.load(open(os.path.join(HERE, "vectors", "sr9_corpus.json"), encoding="utf-8"))["cases"][0]["input"]["suite"]
# Hand facts about the suite: a(): R1=1 R2=2 R3=0 R4=0 R5=1 R6=1 R7=0.
# steps: R1=3 R2=1 R3=1 R4=1 R5=2 R6=2 R7=2. Only R4 fails SR1.
# DOMAIN: R5=LIM, R6=OTHER, the rest MAIN. L-M pairs: R1|R3, R2|R3, R7|R3.
assert [SUITE["records"][r]["steps"] for r in ["R1", "R2", "R3", "R4", "R5", "R6", "R7"]] == [3, 1, 1, 1, 2, 2, 2]

PARAMS = {"STEP_CONVENTION": "one inference rule is one step", "K": "2", "N_X": 2, "S_MAX": 10}
CONSTS = [{"NAME": "w_a", "VALUE": "1"}, {"NAME": "w_s", "VALUE": "0.5"},
          {"NAME": "w_big", "VALUE": "1e400"}, {"NAME": "w_huge", "VALUE": "1e308"}]


def tms(*yardsticks):
    return {"PARAMETERS": dict(PARAMS), "CONSTANTS": copy.deepcopy(CONSTS),
            "YARDSTICKS": [{"NAME": n, "KEY": k, "LIMITING": l} for n, k, l in yardsticks]}


T = tms(
    ("skel", ["SR1", "A", "S"], []),
    ("comb", ["SR1", "WSUM:w_a*A;w_s*S", "S"], []),
    ("lim", ["SR1", "A", "S"], [{"DOMAIN": "LIM", "KEY": ["SR1", "NEG:A", "S"]}]),
    ("lim_main", ["SR1", "A"], [{"DOMAIN": "MAIN", "KEY": ["SR1", "NEG:A"]}]),
    ("field", ["SR1", "FIELD:VALUE"], []),
    ("big", ["SR1", "WSUM:w_big*S"], []),
    ("huge", ["SR1", "WSUM:w_huge*A;w_huge*S"], []),
)
R = SUITE["records"]

cases = []
def case(cid, fn, inp, expect, **extra):
    c = {"id": cid, "fn": fn, "input": inp}
    c.update(extra)
    c["expect"] = expect
    cases.append(c)

# ---- keys ------------------------------------------------------------------
case("key/skel-R1", "yardstick_key", {"tms": T, "name": "skel", "rid": "R1", "records": R}, "[OK,[0,1,3]]")
case("key/skel-R4-fails-SR1", "yardstick_key", {"tms": T, "name": "skel", "rid": "R4", "records": R}, "[OK,[1,0,1]]")
case("key/comb-R1", "yardstick_key", {"tms": T, "name": "comb", "rid": "R1", "records": R}, "[OK,[0,2.5,3]]",
     note="a + 0.5 s = 1 + 1.5")
case("key/comb-R2", "yardstick_key", {"tms": T, "name": "comb", "rid": "R2", "records": R}, "[OK,[0,2.5,1]]",
     note="2 + 0.5; ties R1 on the WSUM, S breaks the tie")
case("key/lim-R5-uses-limiting-key", "yardstick_key", {"tms": T, "name": "lim", "rid": "R5", "records": R}, "[OK,[0,-1,2]]")
case("key/lim-R1-uses-main-key", "yardstick_key", {"tms": T, "name": "lim", "rid": "R1", "records": R}, "[OK,[0,1,3]]")
case("key/lim_main-R3-no-negative-zero", "yardstick_key", {"tms": T, "name": "lim_main", "rid": "R3", "records": R},
     "[OK,[0,0]]", note="NEG of a(R3) = 0 is written 0, never -0")

# ---- the harness on declared yardsticks --------------------------------------
case("harness/skel", "yardstick_harness", {"tms": T, "name": "skel", "suite": SUITE}, "[OK,true,true,true,0]",
     note="the strike skeleton declared in the grammar reproduces increment 3's skeleton fixture")
case("harness/comb", "yardstick_harness", {"tms": T, "name": "comb", "suite": SUITE}, "[OK,true,true,true,0]")
case("checks/lim", "yardstick_checks", {"tms": T, "name": "lim", "suite": SUITE},
     "[OK,[true,[]],[true,[],[R5]],[true,[]]]",
     note="R5 (LIM) is keyed by -a: one more assumption lowers its key, exempt inside the declared DOMAIN")
case("checks/lim_main", "yardstick_checks", {"tms": T, "name": "lim_main", "suite": SUITE},
     "[OK,[true,[]],[true,[],[R1,R2,R3,R4,R7]],[true,[]]]",
     note="every MAIN record inverted and exempt; R5 (LIM) and R6 (OTHER) keep the main KEY and pass")

# ---- refusals ----------------------------------------------------------------
case("refused/naive-key-is-not-well-formed", "yardstick_harness",
     {"tms": tms(("naive", ["A", "S"], [])), "name": "naive", "suite": SUITE},
     "[REFUSED,TMS is not well-formed]", note="C1 fails by construction: KEY must begin with SR1")
case("refused/more-is-better-is-not-well-formed", "yardstick_harness",
     {"tms": tms(("more", ["SR1", "NEG:A", "S"], [])), "name": "more", "suite": SUITE},
     "[REFUSED,TMS is not well-formed]", note="static C2 refuses it at entry")
case("refused/no-such-yardstick", "yardstick_harness", {"tms": T, "name": "nope", "suite": SUITE},
     "[REFUSED,no yardstick named nope]")
case("refused/name-escaped", "yardstick_harness", {"tms": T, "name": "x\u202ey", "suite": SUITE},
     "[REFUSED,no yardstick named x<U+202E>y]")
case("refused/field-awaits-c16", "yardstick_harness", {"tms": T, "name": "field", "suite": SUITE},
     "[REFUSED,FIELD terms await the record field schema (C-16)]")
case("refused/weight-not-finite", "yardstick_harness", {"tms": T, "name": "big", "suite": SUITE},
     "[REFUSED,weight w_big is not a finite double]")
case("refused/value-not-finite", "yardstick_harness", {"tms": T, "name": "huge", "suite": SUITE},
     "[REFUSED,a yardstick value is not finite]", note="1e308*a + 1e308*s overflows to infinity")


def dispatch(fn, i):
    if fn == "yardstick_key":
        return Y.yardstick_key(i["tms"], i["name"], i["rid"], i["records"], i.get("tfk_assumptions", []))
    if fn == "yardstick_checks":
        return Y.yardstick_checks(i["tms"], i["name"], i["suite"])
    if fn == "yardstick_harness":
        return Y.yardstick_harness(i["tms"], i["name"], i["suite"])
    raise KeyError(fn)


bad = 0
for c in cases:
    got = S.canon(dispatch(c["fn"], c["input"]))
    if got != c["expect"]:
        bad += 1
        print("MISMATCH", c["id"], "\n  expect", c["expect"], "\n  got   ", got)
if bad:
    sys.exit("refusing to write: %d mismatches" % bad)

corpus = {
  "meta": {
    "name": "tpc_yardstick_corpus", "version": "0.1.0",
    "status": "VERIFIED (both residences)",
    "conforms_tps": "0.1.0 revision 4 (TPS 4.5: SR9 C1, C2, C4 on a declared, executable TMS entry; D100.1)",
    "module": "tpc_yardstick 0.1.0 (tpc_zone 0.2.0, tpc_sr9 0.1.0)",
    "suite": "sr9_synthetic_suite 0.1.0, verbatim",
    "note": ("Expectations hand-derived: a() R1=1 R2=2 R3=0 R4=0 R5=1 R6=1 R7=0; steps 3 1 1 1 2 2 2; "
             "only R4 fails SR1; R5 in LIM, R6 in OTHER. Both residences MUST reproduce every expect.")
  },
  "cases": cases
}
with open(os.path.join(HERE, "vectors", "yardstick_corpus.json"), "w", encoding="utf-8",
          newline="\n") as f:
    f.write(json.dumps(corpus, indent=1, ensure_ascii=False) + "\n")
print("wrote", len(cases), "yardstick cases; all match hand-derived expectations")