Exemplo n.º 1
0
def eresolution(clause) -> list:
    res = []
    for l in range(len(clause)):
        lit = clause.getLiteral(l)
        if lit.atom[0] == '=' and lit.isNegative():
            left = lit.atom[1]
            right = lit.atom[2]

            unifier = mgu(left, right)
            if unifier is not None:
                others = []

                for ll in range(len(clause)):
                    lit2 = clause.getLiteral(ll)
                    if ll != l:
                        if unifier is None:
                            others.append(lit2)
                        else:
                            others.append(lit2.instantiate(unifier))

                new_clause = Clause(others)

                new_clause.setDerivation(
                    flatDerivation("eresolution", [clause, clause]))

                res.append(new_clause)
    return res
Exemplo n.º 2
0
def termFind(term1, term2):
    positions = []
    if termIsVar(term1):
        return []
    subst = mgu(term1, term2)
    if isinstance(term1, list):
        for i in range(len(term1)):
            positions2 = termFind(subterm(term1, [i]), term2)
            for p in positions2:
                p.add_first(i)
                positions.append(p)
    if subst is not None:
        positions.append(Position(subst))

    return positions
Exemplo n.º 3
0
def factor(clause, lit1, lit2):
    """
    Check if it is possible to form a factor between lit1 and lit2. If
    yes, return it, otherwise return None.
    """
    l1 = clause.getLiteral(lit1)
    l2 = clause.getLiteral(lit2)
    if l1.isNegative() != l2.isNegative():
        return None
    sigma = mgu(l1.atom, l2.atom)
    if sigma == None:
        return None
    lits = [l.instantiate(sigma) for l in clause.literals if l != l2]
    res = clauses.Clause(lits)
    res.removeDupLits()
    res.setDerivation(flatDerivation("factor", [clause]))
    return res
Exemplo n.º 4
0
def resolution(clause1, lit1, clause2, lit2):
    """
    Implementation of the Resolution rule. lit1 and lit2 are indices
    of literals in clause1 and clause2, respectively, so clause1|lit1
    and clause2|lit2 are literals.

    Try to resolve clause1|lit1 against clause2|lit2. If this is
    possible, return the resolvent. Otherwise, return None.
    """
    l1 = clause1.getLiteral(lit1)
    l2 = clause2.getLiteral(lit2)
    if l1.isNegative() == l2.isNegative():
        return None
    sigma = mgu(l1.atom, l2.atom)
    if sigma is None:
        return None
    lits1 = [l.instantiate(sigma) for l in clause1.literals if l!=l1]
    lits2 = [l.instantiate(sigma) for l in clause2.literals if l!=l2]
    lits1.extend(lits2)
    res = clauses.Clause(lits1)
    res.removeDupLits()
    res.setDerivation(flatDerivation("resolution", [clause1, clause2]))
    return res