Add a cancelation checker

This commit is contained in:
Emi Simpson 2023-03-05 21:35:14 -05:00
parent ad4ed220b6
commit db2f9710dd
Signed by: Emi
GPG key ID: A12F2C2FFDC3D847

32
resolution.py Normal file
View file

@ -0,0 +1,32 @@
from emis_funky_funktions import *
from ir import IRNeg, IRProp, IRTerm, IRVar, Substitutions, unify, unify_clauses
def terms_cancel(t1: IRTerm, t2: IRTerm) -> Option[Substitutions]:
"""
Determine if two terms could cancel each other out, given the right substitutions
That is, is there some set of substitutions such that t1 = ¬t2 or ¬t1 = t2?
If negation is possible and a substitution exists, that substitution is returned.
Otherwise, `None` is returned.
>>> terms_cancel(IRNeg(IRVar('x1')), IRProp('Terezi'))
Some((Terezi()/x1,))
>>> terms_cancel(IRProp('Nepeta'), IRVar('x1'))
Some((¬Nepeta()/x1,))
>>> terms_cancel(IRProp('ancestor', [IRVar('x1')]), IRVar('x1')) is None
True
"""
match (t1, t2):
case (IRNeg(a), b) | (b, IRNeg(a)): #type: ignore
return hush(unify(a, b))
case (IRVar(_) as x, a) | (a, IRVar(_) as x): #type: ignore
return hush(unify(x, IRNeg(a)))
return None
if __name__ == '__main__':
import doctest
doctest.testmod()