Skip to main content

metamath-py

A python interface to Metamath

install

$ pip install --user metamath-py

basic usage

Loading a .mm file (may take a minute for large databases):

>>> import metamathpy.database as md
>>> import os
>>> db = md.parse(os.path.join(os.environ["HOME"], "metamath", "set.mm"))

Change the path depending on where you installed metamath or downloaded set.mm, which you can quickly do with

wget https://raw.githubusercontent.com/metamath/set.mm/refs/heads/develop/set.mm

Access any statement by its label, for example the major premise of the modus ponens axiom:

>>> db.statements['maj'] # uses a named tuple data structure
Statement(label='maj', tag='$e', tokens=['|-', '(', 'ph', '->', 'ps', ')'], proof=[])
>>> " ".join(db.statements['maj'].tokens) # more human readable
|- ( ph -> ps )

Access any inference rule by the label of its consequent:

>>> db.rules['ax-mp']
<metamathpy.database.Rule object at 0x7fe21f099370>
>>> print(db.rules['ax-mp']) # more human readable
ax-mp $a |- ps $.
disjoint variable sets: set()
  wph $f wff ph $.
  wps $f wff ps $.
  min $e |- ph $.
  maj $e |- ( ph -> ps ) $.

It has fields for the consequent and hypotheses

>>> db.rules['ax-mp'].consequent
Statement(label='ax-mp', tag='$a', tokens=['|-', 'ps'], proof=[])
>>> db.rules['ax-mp'].essentials
[Statement(label='min', tag='$e', tokens=['|-', 'ph'], proof=[]), Statement(label='maj', tag='$e', tokens=['|-', '(', 'ph', '->', 'ps', ')'], proof=[])]

Verify a proof to construct the proof tree in memory:

>>> import metamathpy.proof as mp
>>> root, steps = mp.verify_proof(db, db.rules['a1i'])

root is a ProofStep object representing the final step of the proof; it is the root of a proof tree. The nodes of that tree are other ProofStep objects and the values in the steps dictionary; the corresponding key is the step's conclusion. ProofSteps display like strings but have the following attributes:

>>> root
ax-mp |- ( ps -> ph )
>>> root.conclusion # the conclusion of this step of the proof
('|-', '(', 'ps', '->', 'ph', ')')
>>> root.rule # the rule used to justify this step
<metamathpy.database.Rule object at 0x7fe21f099370>
>>> root.rule.consequent # in this case it is ax-mp
Statement(label='ax-mp', tag='$a', tokens=['|-', 'ps'], proof=[])
>>> root.substitution # the variable substitution that unified ax-mp with this step
{'ph': ('ph',), 'ps': ('(', 'ps', '->', 'ph', ')')}
>>> root.dependencies # the other steps on which this one was justified
{'wph': ProofStep(conclusion=[wph] wff ph), 'wps': ProofStep(conclusion=[wi] wff ( ps -> ph )), 'min': ProofStep(conclusion=[a1i.1] |- ph), 'maj': ProofStep(conclusion=[ax-1] |- ( ph -> ( ps -> ph ) ))}

The dependencies are a dictionary where keys are the labels of the rule's hypotheses, and values are the other proof steps that satisfied those hypotheses. These can be thought of as the children of the root in the proof tree.

You can instantiate a gym-like environment that operates similarly to metamath solitaire. Initialize it with a database and reset to a blank proof state, optionally with a claim you want to prove:

>>> from metamathpy.environment import Environment
>>> env = Environment(db)
>>> env.reset(claim=db.rules['mpd'])
(<metamathpy.database.Rule object at 0x74ad291f37c0>, [], [])

It returns a state tuple (claim, proof, stack). Rule applications are treated like actions and their labels are saved in the proof list. The stack is updated according to the metamath proof verification algorithm. To apply a rule, provide it as an action at the current step:

>>> env.step("wph")
((<metamathpy.database.Rule object at 0x74ad291f37c0>, [], [ProofStep(conclusion=[wph] wff ph)]), '')
>>> env.step("wps")
((<metamathpy.database.Rule object at 0x74ad291f37c0>, [], [ProofStep(conclusion=[wph] wff ph), ProofStep(conclusion=[wps] wff ps)]), '')
>>> env.step("wi")
((<metamathpy.database.Rule object at 0x74ad291f37c0>, [], [ProofStep(conclusion=[wi] wff ( ph -> ps ))]), '')
>>> env.step("wi")
((<metamathpy.database.Rule object at 0x74ad291f37c0>, [], [None]), '2 hypotheses != 1 dependences')

The step returns a (state, msg) tuple. msg is empty if the rule was valid, otherwise it contains an error message and the top of stack is None.

Environments can be deep-copied if you want to use them in a branching tree search:

>>> env.reset()
(None, [], [])
>>> env.step("wph")
((None, ['wph'], [ProofStep(conclusion=[wph] wff ph)]), '')
>>> env2 = env.copy()
>>> env.step("wi")
((None, ['wph', 'wi'], [None]), '2 hypotheses != 1 dependences')
>>> env2.step("wps")
((None, ['wph', 'wps'], [ProofStep(conclusion=[wph] wff ph), ProofStep(conclusion=[wps] wff ps)]), '')

user interface (in-progress)

A TUI (requires blessed) that shows live proof searches in a Metamath database:

mmpy1024

Metadata

Release files for metamath-py 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for metamath-py 0.1.0
File Size Uploaded
metamath_py-0.1.0.tar.gz 39.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for metamath-py 0.1.0
File Interpreter ABI Platform
metamath_py-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 89.7 kB

Release files / metamath_py-0.1.0.tar.gz

Download URL metamath_py-0.1.0.tar.gz
Size 39.0 kB
Tags Source
SHA-256 checksum
How to use checksums
a2be36bc9d33d03fc5bd84ee02d16f6d8101afd3684f24bf0224c0faf2c105da
BLAKE2b-256 checksum
How to use checksums
5a36f1c87cb633824602278fb949da513ab0f7e21075c3f5b2bbfe02a38fedaf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.3

Release files / metamath_py-0.1.0-py3-none-any.whl

Download URL metamath_py-0.1.0-py3-none-any.whl
Size 50.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9809314cd2b4e539d3f91d4fc292fa82949453acdef1bbb4226b3fdbbc14da85
BLAKE2b-256 checksum
How to use checksums
af98cf58781508e69f0ed21073c3a13f6ba7f66f479ae4c9d68faa34784bdfed
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.3

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

0.0.9

2 release files

0.0.8

2 release files

0.0.7

2 release files

0.0.6

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page