5.7 Exchange proofs with OpenTheory¶
Tutorial sequence · Previous: 5.6 Snapshot publication, replace, and auditing
You will do: write a theorem as an OpenTheory article, read and check it, import a statement, and preserve the checked article in a binary library. Use the Store setup from Lesson 5.4.
Pick the format for the job¶
The native binary library stores versioned declarations, dependencies, and proof rows for reuse by judgment. OpenTheory version 6 is a text command format for exchanging proofs in its supported higher-order logic fragment. An article may contain several theorems. It is not arbitrary Mojo or Python code, and not every judgment proof has an OpenTheory representation.
We will exchange True = True. Reflexivity provides a small proof with no additional assumptions, so we can see the complete round trip without loading a large profile.
Mojo
"""Export a reflexivity proof, check its article, and publish it for reuse."""
from std.tempfile import mkdtemp
from std.testing import assert_equal, assert_true
from judgment import Session
from judgment_store import Article, Library
from termbank import Budget
def main() raises:
var directory = mkdtemp()
var path = String(directory, "/reflexivity.art")
var budget = Budget(10_000_000)
var s = Session()
var proof = s.theorem("truth_refl", s.true_() == s.true_())
proof.rfl()
var theorem = proof.finish()
# verify=True: checked read-back before publication.
Article.write(path, [s.export(theorem, "basics.truth_refl")], budget)
var article = Article.open(path)
var entries = article.theorems()
assert_equal(len(entries), 1)
print("Checked article theorems:", len(entries))
var receiving = Session()
var imported = receiving.import_theorem(article, entries[0].name)
assert_true(Bool(imported.imported()))
print("Imported statement:", imported.statement().format())
print("Recorded as an import:", Bool(imported.imported()))
# Keep the checked article in a binary library for future processes.
var library_path = String(directory, "/library")
var library = Library.create(library_path, "none")
var change = library.change()
change.add_article(path)
_ = change.publish()
var reopened = Library.open(library_path)
var checked = Session.open(reopened.snapshot(budget))
var recovered = checked.import_fact(reopened.theorems()[0].name)
assert_equal(len(recovered.axioms()), 0)
assert_equal(len(recovered.admissions()), 0)
print("From checked binary snapshot:", recovered)
Run from the repository root pixi run mojo run -I src docs/examples/store/opentheory_roundtrip.mojo
Output
Checked article theorems: 1
Imported statement: True = True
Recorded as an import: True
From checked binary snapshot: ⊢ True = True
Python
"""Export a reflexivity proof, check its article, and publish it for reuse."""
from pathlib import Path
from tempfile import TemporaryDirectory
from judgment import Article, Budget, Library, Session
def main():
with TemporaryDirectory() as directory:
path = str(Path(directory) / "reflexivity.art")
budget = Budget(10_000_000)
s = Session()
proof = s.theorem("truth_refl", s.true_() == s.true_())
proof.rfl()
theorem = proof.finish()
exports = [s.export(theorem, "basics.truth_refl")]
Article.write(path, exports, budget) # verify=True: checked read-back before publication.
article = Article.open(path)
entries = article.theorems()
assert len(entries) == 1
print("Checked article theorems:", len(entries))
receiving = Session()
imported = receiving.import_theorem(article, entries[0].name)
assert imported.imported() is not None
print("Imported statement:", imported.statement().format())
print("Recorded as an import:", imported.imported() is not None)
# Keep the checked article in a binary library for future processes.
library_path = str(Path(directory) / "library")
library = Library.create(library_path, "none")
change = library.change()
change.add_article(path)
change.publish()
reopened = Library.open(library_path)
checked = Session.open(reopened.snapshot(budget))
recovered = checked.import_fact(reopened.theorems()[0].name)
assert len(recovered.axioms()) == 0
assert len(recovered.admissions()) == 0
print("From checked binary snapshot:", recovered)
if __name__ == "__main__":
main()
Run from the repository root PYTHONPATH=bindings/python:docs/examples/python/store pixi run python docs/examples/python/store/opentheory_roundtrip.py
Output
Checked article theorems: 1
Imported statement: True = True
Recorded as an import: True
From checked binary snapshot: ⊢ True = True
Export with checked read-back¶
s.export(...) collects the proof; Article.write(path, exports, budget) writes
the article. write_article(...) is the equivalent free function, also available
in both languages. The list can hold several theorem exports.
The writer first replays the exported proof rows. With verify=True, the
default used here, it reads its generated article back through the checking
converter before replacing the destination file. The result must provide the
expected theorem statement identities and exactly the expected imported
assumptions. A dropped proof step or an extra assumption fails that check.
Keep this verification enabled.
Exports that cite library declarations need the matching library path through
the library argument. Use the same profile when authoring, writing, and reading.
The none profile handles supported equational proofs; supported
natural-deduction proofs need classical. The writer refuses isabelle, sorted
type variables, and other constructs without a supported version-6 form. A
binary library can retain proofs that this exporter cannot express; changing
file extensions does not convert them. The OpenTheory export theory
lists the supported rules and refusals.
What happens when importing?¶
Article.open(path) reads the article as untrusted input. It runs the checking
converter into a private temporary binary library. The converter checks commands
and references, constructs typed terms and declarations, and establishes the
proof steps through the kernel. Invalid commands, ill-typed rules, missing
dependencies, or an unsupported format cause a refusal. The Article owns its
temporary files and removes them when released; scratch selects their parent
directory if you need a particular location.
An article can contain axioms. When an axiom matches an existing provider, the
converter resolves it to that provider. A new axiom needs explicit provenance in
the source argument, in the form PROVER|RELEASE|SESSION|THEORY; with the
default empty source it is refused. Provenance records where an assumption came
from. It does not prove that assumption or establish consistency. Review the
profile and the final theorem's dependencies as well as whether checking passed.
The example then calls receiving.import_theorem(article, name). At this point
the article has been checked by Article.open, but the session's import API
still imports its statement and dependency information. It records an
imported declaration; it does not replay the article's proof inside that session.
imported.imported() exposes that distinction. import_theorems(article, names)
provides the corresponding batch operation.
Plain OpenTheory articles do not preserve our friendly theorem aliases.
Enumerate article.theorems() and use its names or statement identities; do not
assume that basics.truth_refl survived the export. The example has exactly one
theorem. The catalog can also include axioms: inspect TheoremInfo.kind as well
as the statement identity. For a larger article, select the intended theorem
rather than depending on list order or treating every entry as a proved result.
Keep an article's checked proof for later¶
The last part uses change.add_article(path) and publish() to convert and
store the article in a persistent binary version. It then follows Lesson 5.4's
full snapshot → Session.open → import_fact path, checking the stored proof
rows before reuse. It needs no source here because the reflexivity proof adds
no axioms. A change holds articles or session exports, not a mixture.
If you prove a new theorem using a statement imported directly from an Article, publishing that new theorem needs the article's declarations in the destination library first. Publish the article in one change, reopen the resulting library, then add the new theorem in a later change. The new proof cites those declarations by identity, not by a temporary file path.
Try it¶
Replace the article's contents with 6, version, and refl, one per line,
then call Article.open. Does naming a valid proof rule make this a valid proof?
Explanation
No. refl needs a term on the article's stack. The input is malformed and is
refused. More generally, the checker validates a rule's inputs and conclusion;
it does not accept a claim merely because it is labeled “theorem.” Adding an
axiom also does not prove it: it changes the assumptions the theorem depends on.
Next: 5.8 Verify an exported proof in Isabelle/HOL, or consult importing articles and the Store API reference.