Jump to content

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

From The Invariance Itself
TPC-PY source gen_settlement_corpus.py (0.1.0), SHA-256 5ca16004f1d172a4; conformed TPS 0.4.0; PROPOSED, not declared (TPC-PY 0.2.0)
 
m 1 revision imported
 
(No difference)

Latest revision as of 02:11, 30 September 2026

# -*- coding: utf-8 -*-
"""gen_settlement_corpus 0.1.0 (agent TQ, increment 12): writes
vectors/settlement_corpus.json, the vectors of tpc_eom 0.2.0's
settlement_check (tpm_record_functions 0.2.0 section 12; D198.1 (b),
D199.1 (b)), answered through tpc_record's run_case (tpc_eom imports
nothing of tpc_record).

VECTORS FIRST: every expectation is a literal of this generator in TPM's
canonical JSON, written before settlement_check existed. What the
structure test itself settles is the existing tpc_eom eom_register_test's
answer (increment 11, unchanged in that respect), taken here over literal
data and pinned; a refusal of the test is quoted from it likewise. The TFK
is the tree's example corner_no_eom_x.json, whose structure check finds v
UNDER. Standard library only; run from tpc/.
"""
import json
import os
import sys

HERE = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, HERE)
import tpc_eom as E      # noqa: E402  existing function (eom_register_test), read only

CORPUS_VERSION = "0.1.0"
CASES = []


def cj(v):
    if v is True:
        return "true"
    if v is False:
        return "false"
    if v is None:
        return "null"
    if type(v) is int:
        return str(v)
    if type(v) is str:
        out = ['"']
        for ch in v:
            if ch == '"':
                out.append('\\"')
            elif ch == "\\":
                out.append("\\\\")
            elif ord(ch) < 0x20:
                out.append("\\u%04x" % ord(ch))
            else:
                out.append(ch)
        out.append('"')
        return "".join(out)
    if type(v) is list:
        return "[" + ",".join(cj(x) for x in v) + "]"
    if type(v) is dict:
        return "{" + ",".join(cj(k) + ":" + cj(v[k]) for k in sorted(v)) + "}"
    raise TypeError(type(v))


def refused(*p):
    return cj(["REFUSED", [list(x) for x in sorted(set(tuple(x) for x in p))]])


def okd(by_test, by_naming, open_, used):
    return cj(["OK", {"BY_TEST": by_test, "BY_NAMING": by_naming, "OPEN": open_, "USED": used}])


with open(os.path.join(HERE, "examples", "corner_no_eom_x.json"), encoding="utf-8") as _f:
    TFK = json.load(_f)
_t = E.eom_register_test(TFK, [{"ID": "RR-10", "STATUS": "PROVISIONAL", "ROLE": "EOM(v)",
                                "EQUATION": "m*D(v,t) = -k_s*x"}])
assert _t[3:] == [["USED", ["RR-10"]], ["SETTLED", ["v"]], ["OPEN", []]], _t
assert E.eom_register_test(TFK, [])[5] == ["OPEN", ["v"]]

GAP = {"ID": "RR-2", "CLASS": "NAMED_GAP", "GRADE": "NOT GRADED", "SOURCE": "UNDER", "AGAINST": ["v"],
       "TRIGGER": "T", "STATEMENT": "The unknown DOF v is determined by no form (UNDER).", "STATE": "OPEN"}
IC2 = dict(GAP, ID="RR-3", SOURCE="IC2", AGAINST=["x"])
R10 = {"ID": "RR-10", "CLASS": "RESULT", "GRADE": "PROVISIONAL", "REGIME": "L", "ROLE": "EOM(v)",
       "EQUATION": "m*D(v,t) = -k_s*x", "REFS": ["k_s", "m", "t", "v", "x"]}
R11 = {"ID": "RR-11", "CLASS": "RESULT", "GRADE": "PROVISIONAL", "REGIME": "L",
       "STATEMENT": "m dv/dt = -k_s x, derived in prose"}
R12 = dict(R10, ID="RR-12", ROLE="INVARIANT", EQUATION="m*v = m*v", REFS=["m", "v"])
R13 = dict(R10, ID="RR-13", EQUATION="m*D(v,t) = -k_q*x")
A14 = {"ID": "RR-14", "CLASS": "ASSUMPTION", "GRADE": "PROVISIONAL", "ROLE": "EOM(v)",
       "EQUATION": "m*D(v,t) = -k_s*x", "REFS": ["k_s", "m", "t", "v", "x"], "PROPOSITION": "p"}
W = {"RR-10": "PROVISIONAL", "RR-11": "PROVISIONAL", "RR-12": "PROVISIONAL", "RR-13": "PROVISIONAL",
     "RR-14": "PROVISIONAL", "RR-2": "NOT GRADED", "RR-3": "NOT GRADED"}


def st(name, records, words, naming, expected):
    CASES.append({"name": name, "fn": "settlement_check",
                  "args": {"tfk": TFK, "records": records, "words": words, "naming": naming},
                  "expected": expected})


st("ST01 settled by the form", [GAP, IC2, R10], W, [], okd(["v"], [], [], ["RR-10"]))
st("ST02 settled by an ASSUMPTION's form", [GAP, A14], W, [], okd(["v"], [], [], ["RR-14"]))
st("ST03 settled by the lead's naming of a prose record", [GAP, R11], W, [["v", "RR-11"]],
   okd([], [["v", "RR-11"]], [], []))
st("ST04 REC-88 a naming of a formed record", [GAP, R12], W, [["v", "RR-12"]],
   refused(["REC-88", "-", "RECORD_ID RR-12 carries a form; it settles by the test alone"]))
st("ST05 REC-86 an unknown that is not an OPEN UNDER gap's DOF", [GAP, IC2, R11], W, [["x", "RR-11"]],
   refused(["REC-86", "-", "UNKNOWN x is not an OPEN UNDER gap's DOF left open by the test"]))
st("ST06 REC-86 an unknown the test settled", [GAP, R10, R11], W, [["v", "RR-11"]],
   refused(["REC-86", "-", "UNKNOWN v is not an OPEN UNDER gap's DOF left open by the test"]))
st("ST07 a HELD settling record: not standing, not settled", [GAP, R10], dict(W, **{"RR-10": "HELD"}), [],
   okd([], [], ["v"], []))
st("ST08 a WITHDRAWN settling record", [GAP, R10], dict(W, **{"RR-10": "WITHDRAWN"}), [],
   okd([], [], ["v"], []))
st("ST09 an empty naming, nothing settles", [GAP, R11], W, [], okd([], [], ["v"], []))
st("ST10 REC-84 a naming out of order", [GAP, R11], W, [["x", "RR-11"], ["v", "RR-11"]],
   refused(["REC-84", "-", "naming is not a sorted list of [UNKNOWN, RECORD_ID] pairs"]))
st("ST11 REC-84 a naming not of pairs", [GAP, R11], W, [["v"]],
   refused(["REC-84", "-", "naming is not a sorted list of [UNKNOWN, RECORD_ID] pairs"]))
st("ST12 REC-87 a naming of a record that does not stand", [GAP, R11], dict(W, **{"RR-11": "HELD"}),
   [["v", "RR-11"]], refused(["REC-87", "-", "RECORD_ID RR-11 is not a standing record of the register"]))
st("ST13 REC-87 a naming of a record not in the register", [GAP], W, [["v", "RR-77"]],
   refused(["REC-87", "-", "RECORD_ID RR-77 is not a standing record of the register"]))
st("ST14 the gap already SETTLED: nothing is OPEN", [dict(GAP, STATE="SETTLED 2026-09-28 BY RR-10"), R10], W,
   [], okd([], [], [], ["RR-10"]))
_ref = E.eom_register_test(TFK, [{"ID": "RR-13", "STATUS": "PROVISIONAL", "ROLE": "EOM(v)",
                                  "EQUATION": "m*D(v,t) = -k_q*x"}])
assert _ref[0] == "REFUSED", _ref
st("ST15 REC-85 the structure test refuses", [GAP, R13], W, [],
   refused(["REC-85", "-", "the structure test refused: " + _ref[1]]))
R15 = dict(R10, ID="RR-15")
del R15["ROLE"]
st("ST16 an EQUATION without ROLE enters no matching", [GAP, R15], dict(W, **{"RR-15": "PROVISIONAL"}), [],
   okd([], [], ["v"], []))
st("ST17 two naming failures reported together", [GAP, R12], W, [["v", "RR-12"], ["w", "RR-12"]],
   refused(["REC-86", "-", "UNKNOWN w is not an OPEN UNDER gap's DOF left open by the test"],
           ["REC-88", "-", "RECORD_ID RR-12 carries a form; it settles by the test alone"]))


def main():
    names = [c["name"] for c in CASES]
    assert len(set(names)) == len(names)
    out = {"corpus": "settlement", "version": CORPUS_VERSION,
           "spec": "tpm_record_functions 0.2.0 section 12 (tpc_eom 0.2.0)",
           "note": ("vectors first: every expectation a literal of the generator; the structure test's "
                    "answers pinned from the existing eom_register_test"),
           "cases": CASES}
    with open(os.path.join(HERE, "vectors", "settlement_corpus.json"), "w", encoding="utf-8",
              newline="\n") as f:
        json.dump(out, f, indent=1, sort_keys=True, ensure_ascii=True)
        f.write("\n")
    print("wrote %d settlement cases" % len(CASES))


if __name__ == "__main__":
    main()
# END OF ARTIFACT --- gen_settlement_corpus.py 0.1.0
# If this line is absent, the payload arrived truncated.