From b046152a4cad2c514d1375c274381eb11b8143f5 Mon Sep 17 00:00:00 2001 From: Emi Simpson Date: Sun, 5 Mar 2023 22:33:00 -0500 Subject: [PATCH] Add a derive method --- resolution.py | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/resolution.py b/resolution.py index a755c17..c2f8dd6 100644 --- a/resolution.py +++ b/resolution.py @@ -1,6 +1,6 @@ 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 @@ -75,6 +75,26 @@ def merge_clauses(c1: Clause, c2: Clause) -> Sequence[Clause]: 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__': import doctest doctest.testmod() \ No newline at end of file