new-lang/checker/proof.py

58 lines
1.6 KiB
Python

"""
The proofs module contains various methods to construct a proof.
"""
from __future__ import annotations
import .props as p
from .props import Context, Prop
class ProofCheckerFailedException(Exception):
pass
class Proof:
"""
Base class to construct a proof.
"""
# -----------------------------------------------------------------------------
class Reflexivity(Proof):
"""
Reflectivity proof checker that solves t = t.
"""
def __init__(self, ctx : Context, goal : Prop) -> None:
"""
Create a new proof that uses reflexivity. This axiom defines that
variables equal oneselves.
:param ctx: The context with which one wants to prove this.
:type ctx: Context
:param goal: The equality that needs to be proven.
:type goal: Prop
:raises ProofCheckerFailedException: The proof checker cannot
verify the goal.
"""
match goal:
case p.Eq():
lhs = goal.x.normalize()
rhs = goal.y.normalize()
if lhs == rhs:
self.ctx = ctx
self.goal = goal
else:
l, r = lhs.reduce_on_equality(rhs)
raise ProofCheckerFailedException(
f"Could not prove that {lhs} = {rhs}. Got stuck on {l} = {r}."
)
case _:
raise ProofCheckerFailedException(
"Reflexivity can only prove that t = t. Found another prop type than `Eq`."
)