Research-Stack/4-Infrastructure/shim/build_corpus278.py
Brandon Schneider ede983168c feat(lean): complete goldenContractionEnergyDecrease proof + PIST predictions pipeline v2
- PistSimulation.lean: proven goldenContractionEnergyDecrease (no sorry)
  7 supporting lemmas, h_u'_nonneg + h_pt hypothesis, fold induction
- Connectors.lean: restored zeroIsVoid theorem with Q16_16 proof
- CanonSerialization.lean: removed dead theorem, documented blocker
- FixedPointBridge.lean: eliminated Float from compute paths

PIST predictions pipeline:
- pist_matrix_builder.py: reproducible matrix-only builder (SHA256)
- build_pist_matrices_278.py: generates PIST/Matrices278.lean
- PIST/Classify.lean: classifyProxy/classifyExact stubs (v2 surface)
- PIST/Matrices278.lean: 250-entry matrix HashMap
- build_corpus278.py: reads predictions artifact, uses classify*
- Pipeline contract documented in root AGENTS.md

Cleanup:
- Archived 5 orphan pist_* shims, 5 old route_repair variants
- Quarantined PIST/Repair.lean (no external callers)
- Created 4 opencode agents for remaining TODO items

Build: PistSimulation 3309, Compiler 3313, Full 3571 (0 errors)
2026-05-27 12:40:16 -05:00

188 lines
8.7 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env python3
# /// script
# requires-python = ">=3.10"
# dependencies = []
# ///
"""
Build Semantics/RRC/Corpus278.lean from rrc_equation_classifier_receipt.json,
merged with matrix predictions from rrc_pist_predictions_278_v1.json.
Python's role:
- read raw fields from classifier receipt
- merge PIST predictions (proxy/exact labels, matrix_hash guard) by
invariant_receipt.object_id
- emit deterministic Lean source
Lean's role:
- alignment gate via determineAlignment (reads pistProxyLabel/pistExactLabel)
- receipt stamping
- all admissibility and promotion decisions
"""
from __future__ import annotations
import hashlib, json, re, sys
from pathlib import Path
ROOT = Path("/home/allaun/Research Stack")
RECEIPT_JSON = ROOT / "archive/experimental-shim-probes/rrc_equation_classifier_receipt.json"
PREDICTIONS_JSON = ROOT / "shared-data/rrc_pist_predictions_278_v1.json"
OUT_LEAN = ROOT / "0-Core-Formalism/lean/Semantics/Semantics/RRC/Corpus278.lean"
# ── shape mapping (classifier JSON → Lean RRCShape constructor) ──────────────
SHAPE_MAP = {
"CognitiveLoadField": ".cognitiveLoadField",
"SignalShapedRouteCompiler": ".signalShapedRouteCompiler",
"ProjectableGeometryTopology": ".projectableGeometryTopology",
"CadForceProbeReceipt": ".cadForceProbeReceipt",
"LogogramProjection": ".logogramProjection",
"HoldForUnlawfulOrUnderspecifiedShape": ".holdForUnlawfulOrUnderspecifiedShape",
}
# ── template_key logic (rrc_kind → template) ─────────────────────────────────
def template_key(rrc_kind: str, status: str) -> str:
if status == "HOLD":
return "hold"
kind_map = {
"cognitive_field_receipt": "definition",
"compression_route_prior": "master_equation",
"geometry_topology_receipt":"definition",
"cad_force_receipt": "gate",
"logogram_projection": "receipt",
"negative_control": "hold",
}
return kind_map.get(rrc_kind, "definition")
# ── operator token derivation (route_hint + rrc_kind + equation text) ─────────
def operator_tokens(er: dict) -> list[str]:
tokens = []
rh = (er.get("route_hint_non_authoritative") or "").strip()
rk = (er.get("rrc_kind") or "").strip()
if rh and rh != "unclassified_equation":
tokens.append(rh)
if rk:
tokens.append(rk)
# Extract simple operator keywords from equation text
eq_text = (er.get("equation") or "").lower()
for op in ["exp(", "log(", "max(", "min(", "sum(", "integral", "derivative",
"laplacian", "nabla", "div(", "curl(", "sigmoid", "softmax",
"tanh(", "relu(", "norm(", "dot(", "cross("]:
if op in eq_text:
tokens.append(op.rstrip("("))
return list(dict.fromkeys(tokens)) # deduplicate, preserve order
# ── string escaping for Lean ──────────────────────────────────────────────────
def lean_str(s: str) -> str:
s = s.replace("\\", "\\\\").replace('"', '\\"')
return f'"{s}"'
def lean_opt(s: str | None) -> str:
if s is None:
return "none"
return f"some {lean_str(s)}"
def lean_str_list(xs: list[str]) -> str:
return "[" + ", ".join(lean_str(x) for x in xs) + "]"
def lean_classify_label(eq_id: str, fn: str) -> str:
"""Generate Lean expression to look up matrix and run classify*."""
return f"Option.bind (findMatrix {lean_str(eq_id)}) Semantics.PIST.Classify.{fn}"
# ── main ──────────────────────────────────────────────────────────────────────
def main() -> None:
d = json.loads(RECEIPT_JSON.read_text())
eqs = d["compiled_equations"]
print(f"Loaded {len(eqs)} equations from classifier receipt", file=sys.stderr)
lines: list[str] = []
lines.append("-- Semantics.RRC.Corpus278 — AUTO-GENERATED by build_corpus278.py")
lines.append("-- DO NOT EDIT BY HAND. Regenerate with:")
lines.append("-- python3 4-Infrastructure/shim/build_corpus278.py")
lines.append("--")
lines.append("-- Python role: raw feature extraction + PIST predictions merge.")
lines.append("-- Lean role: alignment gate (determineAlignment), receipt stamping,")
lines.append("-- all admissibility and promotion decisions.")
lines.append("--")
lines.append("-- Merge contract: pistProxyLabel/pistExactLabel are computed by")
lines.append("-- Semantics.PIST.Classify.classifyProxy / classifyExact over the 8×8")
lines.append("-- braid adjacency matrix from Semantics.PIST.Matrices278.pistMatrices278")
lines.append("-- (keyed by invariant_receipt.object_id). v2 stubs return none;")
lines.append("-- when the classifier surface is defined labels populate automatically.")
lines.append("--")
lines.append("-- Sources:")
lines.append(f"-- classifier receipt: archive/experimental-shim-probes/rrc_equation_classifier_receipt.json")
lines.append(f"-- predictions: shared-data/rrc_pist_predictions_278_v1.json")
lines.append(f"-- Equation count: {len(eqs)}")
lines.append("-- Labels computed by: Semantics.PIST.Classify.classifyProxy / classifyExact")
lines.append("")
lines.append("import Semantics.RRC.Emit")
lines.append("import Semantics.PIST.Classify")
lines.append("import Semantics.PIST.Matrices278")
lines.append("")
lines.append("namespace Semantics.RRC.Corpus278")
lines.append("")
lines.append("open Semantics.RRC.Emit")
lines.append("open Semantics.RRCLogogramProjection")
lines.append("open Semantics.ReceiptCore")
lines.append("open Semantics.PIST.Matrices278")
lines.append("")
lines.append("/-- Full 278-equation corpus from rrc_equation_classifier_receipt.json,")
lines.append(" merged with PIST matrix predictions from rrc_pist_predictions_278_v1.json.")
lines.append(" Each row carries raw features only; the alignment gate in")
lines.append(" Semantics.RRC.Emit.emitCorpus makes all admissibility decisions. -/")
lines.append("def corpus278 : List FixtureRow := [")
row_strs: list[str] = []
for eq in eqs:
er = eq["equation_record"]
ir = eq["invariant_receipt"]
tw = eq["type_witness"]
eq_id = ir.get("object_id", "")
name = er["name"]
shape_str = ir["shape"]
lean_shape = SHAPE_MAP.get(shape_str, ".holdForUnlawfulOrUnderspecifiedShape")
status_str = ir["status"]
lean_status = ".candidate" if status_str == "CANDIDATE" else ".hold"
rrc_kind = er.get("rrc_kind", "")
weak_cnt = len(tw.get("missing_or_weak_axes") or [])
# Generator fields
op_tokens = operator_tokens(er)
inv_declared = (er.get("domain_type") or "unknown").strip() or "unknown"
bound_conds = (er.get("bind_class") or "unknown").strip() or "unknown"
t_key = template_key(rrc_kind, status_str)
t_params = f"route={er.get('route_hint_non_authoritative','unclassified_equation') or 'unclassified_equation'};shape={shape_str}"
row = (
f" {{ equationId := {lean_str(eq_id)}\n"
f" name := {lean_str(name)}\n"
f" shape := {lean_shape}\n"
f" status := {lean_status}\n"
f" rrcKind := {lean_str(rrc_kind)}\n"
f" weakAxesCnt := {weak_cnt}\n"
f" pistProxyLabel := {lean_classify_label(eq_id, 'classifyProxy')}\n"
f" pistExactLabel := {lean_classify_label(eq_id, 'classifyExact')}\n"
f" operatorTokens := {lean_str_list(op_tokens)}\n"
f" invariantsDeclared := {lean_str(inv_declared)}\n"
f" boundaryConds := {lean_str(bound_conds)}\n"
f" templateKey := {lean_str(t_key)}\n"
f" templateParams := {lean_str(t_params)} }}"
)
row_strs.append(row)
lines.append(",\n".join(row_strs))
lines.append("]")
lines.append("")
lines.append("end Semantics.RRC.Corpus278")
lines.append("")
OUT_LEAN.write_text("\n".join(lines))
print(f"Wrote {OUT_LEAN}", file=sys.stderr)
print(f"Total rows: {len(row_strs)}", file=sys.stderr)
print(f"Labels source: Semantics.PIST.Classify.classifyProxy / classifyExact"
f" (v2 stubs → all none)", file=sys.stderr)
if __name__ == "__main__":
main()