Add derive2

This commit is contained in:
Emi Simpson 2023-03-06 08:16:20 -05:00
parent b046152a4c
commit f0d7ee2d5e
Signed by: Emi
GPG key ID: A12F2C2FFDC3D847

View file

@ -95,6 +95,29 @@ def derive(clauses: Sequence[Clause]) -> Sequence[Clause]:
for clause in merge_clauses(c1, c2)
]
def derive2(kb1: Sequence[Clause], kb2: Sequence[Clause]) -> Sequence[Clause]:
"""
All clauses which derive in one step from the combination of two knowledge bases
Each resulting clause is the combination of one clause from the left knowledge base
with exactly one clause from the right knowledge base. Clauses from the same
knowledge base will not be combined.
>>> derive2([
... [IRNeg(IRProp('animal', [IRProp('Kim')]))],
... [IRNeg(IRProp('cat', [IRVar('x1')])), IRProp('animal', [IRVar('x1')])],
... ], [
... [IRNeg(IRProp('dog', [IRVar('x0')])), IRProp('animal', [IRVar('x0')])],
... [IRProp('dog', [IRProp('Kim')])],
... ])
[[¬dog(Kim())]]
"""
return [
clause
for (c1, c2) in product(kb1, kb2)
for clause in merge_clauses(c1, c2)
]
if __name__ == '__main__':
import doctest
doctest.testmod()