Add a derive method

This commit is contained in:
Emi Simpson 2023-03-05 22:33:00 -05:00
parent 122b810b73
commit b046152a4c
Signed by: Emi
GPG key ID: A12F2C2FFDC3D847

View file

@ -1,6 +1,6 @@
from emis_funky_funktions import * from emis_funky_funktions import *
from itertools import product from itertools import combinations, product
from ir import Clause, IRNeg, IRProp, IRTerm, IRVar, Substitutions, sub_all, unify, unify_clauses from ir import Clause, IRNeg, IRProp, IRTerm, IRVar, Substitutions, sub_all, unify, unify_clauses
@ -75,6 +75,26 @@ def merge_clauses(c1: Clause, c2: Clause) -> Sequence[Clause]:
for (subs, i1, i2) in valid_substitutions for (subs, i1, i2) in valid_substitutions
] ]
def derive(clauses: Sequence[Clause]) -> Sequence[Clause]:
"""
All possible clauses which derive in one step of resolution from a knowledge base
Attempts to merge every possible combination of clauses, in the knowledge base.
>>> derive([
... [IRNeg(IRProp('animal', [IRProp('Kim')]))],
... [IRNeg(IRProp('dog', [IRVar('x0')])), IRProp('animal', [IRVar('x0')])],
... [IRNeg(IRProp('cat', [IRVar('x1')])), IRProp('animal', [IRVar('x1')])],
... [IRProp('dog', [IRProp('Kim')])],
... ])
[[¬dog(Kim())], [¬cat(Kim())], [animal(Kim())]]
"""
return [
clause
for (c1, c2) in combinations(clauses, 2)
for clause in merge_clauses(c1, c2)
]
if __name__ == '__main__': if __name__ == '__main__':
import doctest import doctest
doctest.testmod() doctest.testmod()