from pathlib import Path
import hashlib
import json
import subprocess

exports = {
    "export.ndjson.zst": {
        "OAI.LinearAlgebra.SpectralBooks.square_exponent",
        "OAI.LinearAlgebra.SpectralBooks.dual_exponent",
        "OAI.LinearAlgebra.SpectralBooks.rectangular_exponent",
        "OAI.LinearAlgebra.SpectralBooks.formalOmega_square_eq_omega",
        "OAI.LinearAlgebra.MatrixExponent.omega_eq",
    },
    "phase4-export.ndjson.zst": {
        "PerN.gatesPerN_holds", "PerN.gatesPerNAll_holds",
        "PerN.asymptoticRank_holds", "PerN.borderRankLower_holds",
    }
}
for filename, targets in exports.items():
    names={0:""}
    present={}
    axioms=set()
    digest=hashlib.sha256()
    with subprocess.Popen(["zstd","-dc",str(Path("certificates")/filename)],stdout=subprocess.PIPE) as process:
        for line in process.stdout:
            digest.update(line)
            if line.startswith(b'{"in":'):
                record=json.loads(line)
                item=record.get("str",record.get("num"))
                prefix=names[item["pre"]]
                atom=item.get("str",item.get("i"))
                names[record["in"]]=f"{prefix}.{atom}" if prefix else str(atom)
            elif line.startswith(b'{"axiom":'):
                axioms.add(names[json.loads(line)["axiom"]["name"]])
            elif line.startswith(b'{"thm":'):
                declaration=json.loads(line)["thm"]
                name=names[declaration["name"]]
                if name in targets:
                    present[name]=declaration["type"]
        assert process.wait()==0
    assert present.keys()==targets, (filename, targets-present.keys())
    assert axioms=={"Classical.choice","Quot.sound","propext"}, (filename,axioms)
    print(filename,flush=True)
    print("uncompressed SHA-256",digest.hexdigest(),flush=True)
    for goal in sorted(present):
        print("theorem",goal,"type expression",present[goal],flush=True)
    print("all exported axioms",", ".join(sorted(axioms)),flush=True)
