基本的一阶逻辑推理失败了对称二进制谓词



超基本问题。我试图在两个二元谓词(父母和子女(之间表达对称关系。但是,通过以下陈述,我的解决方案供者允许我证明任何东西。转换后的CNF形式对我来说是有意义的,而解决方案也是如此,但这应该是虚假的一个明显案例。我想念什么?

forall x,y (is-parent-of(x,y) <-> is-child-of(y,x)) 

我使用的是NLTK Python库和分辨率供供供体。这是NLTK代码:

from nltk.sem import Expression as exp
from nltk.inference import ResolutionProver as prover
s = exp.fromstring('all x.(all y.(parentof(y, x) <-> childof(x, y)))')
q = exp.fromstring('foo(Bar)')
print prover().prove(q, [s], verbose=True)

输出:

[1] {-foo(Bar)}                             A 
[2] {-parentof(z9,z10), childof(z10,z9)}    A 
[3] {parentof(z11,z12), -childof(z12,z11)}  A 
[4] {}                                      (2, 3) 
True

这是解决方案备件的快速修复。

导致谚语不符合的问题是,当有多个互补的文字文字时,它不能正确实施解决方案规则。例如。鉴于条款{A B C}{-A -B D}二进制分辨率将产生{A -A C D}{B -B C D}子句。两者都将被丢弃为重言式。当前的NLTK实现将产生{C D}

这可能是因为子句在NLTK中表示为列表,因此在子句中可能不止一次地发生。当应用于子句{A A}{-A -A}时,此规则确实会正确地产生一个空句,但是通常此规则不正确。

看来,如果我们将子句摆脱相同的文字的重复,我们可以通过一些更改重新获得健全性。

首先定义一个删除相同文字的函数。

这是这种函数的幼稚实现

import nltk.inference.resolution as res
def _simplify(clause):
    """
    Remove duplicate literals from a clause
    """
    duplicates=[]
    for i,c in enumerate(clause):
       if i in duplicates:
          continue
       for j,d in enumerate(clause[i+1:],start=i+1):
          if j in duplicates:
             continue
          if c == d:
               duplicates.append(j)
    result=[]
    for i,c in enumerate(clause):
        if not i in duplicates:
           result.append(clause[i])
    return res.Clause(result)

现在,我们可以将此功能插入nltk.inference.resolution模块的某些功能。

def _iterate_first_fix(first, second, bindings, used, skipped, finalize_method, debug):
    """
    This method facilitates movement through the terms of 'self'
    """
    debug.line('unify(%s,%s) %s'%(first, second, bindings))
    if not len(first) or not len(second): #if no more recursions can be performed
        return finalize_method(first, second, bindings, used, skipped, debug)
    else:
        #explore this 'self' atom
        result = res._iterate_second(first, second, bindings, used, skipped, finalize_method, debug+1)
        #skip this possible 'self' atom
        newskipped = (skipped[0]+[first[0]], skipped[1])
        result += res._iterate_first(first[1:], second, bindings, used, newskipped, finalize_method, debug+1)
        try:
            newbindings, newused, unused = res._unify_terms(first[0], second[0], bindings, used)
            #Unification found, so progress with this line of unification
            #put skipped and unused terms back into play for later unification.
            newfirst = first[1:] + skipped[0] + unused[0]
            newsecond = second[1:] + skipped[1] + unused[1]
            # We return immediately when `_unify_term()` is successful
            result += _simplify(finalize_method(newfirst,newsecond,newbindings,newused,([],[]),debug))
        except res.BindingException:
            pass
    return result
res._iterate_first=_iterate_first_fix

类似地更新res._iterate_second

def _iterate_second_fix(first, second, bindings, used, skipped, finalize_method, debug):
    """
    This method facilitates movement through the terms of 'other'
    """
    debug.line('unify(%s,%s) %s'%(first, second, bindings))
    if not len(first) or not len(second): #if no more recursions can be performed
        return finalize_method(first, second, bindings, used, skipped, debug)
    else:
        #skip this possible pairing and move to the next
        newskipped = (skipped[0], skipped[1]+[second[0]])
        result = res._iterate_second(first, second[1:], bindings, used, newskipped, finalize_method, debug+1)
        try:
            newbindings, newused, unused = res._unify_terms(first[0], second[0], bindings, used)
            #Unification found, so progress with this line of unification
            #put skipped and unused terms back into play for later unification.
            newfirst = first[1:] + skipped[0] + unused[0]
            newsecond = second[1:] + skipped[1] + unused[1]
            # We return immediately when `_unify_term()` is successful
            result += _simplify(finalize_method(newfirst,newsecond,newbindings,newused,([],[]),debug))
        except res.BindingException:
            #the atoms could not be unified,
            pass
    return result
res._iterate_second=_iterate_second_fix

最后,将我们的功能插入clausify()中,以确保输入不含重复。

def clausify_simplify(expression):
    """
    Skolemize, clausify, and standardize the variables apart.
    """
    clause_list = []
    for clause in res._clausify(res.skolemize(expression)):
        for free in clause.free():
            if res.is_indvar(free.name):
                newvar = res.VariableExpression(res.unique_variable())
                clause = clause.replace(free, newvar)
        clause_list.append(_simplify(clause))
    return clause_list
res.clausify=clausify_simplify

应用了这些更改后,供供者应运行标准测试并正确处理parentof/childof关系。

print res.ResolutionProver().prove(q, [s], verbose=True)

输出:

[1] {-foo(Bar)}                                  A 
[2] {-parentof(z144,z143), childof(z143,z144)}   A 
[3] {parentof(z146,z145), -childof(z145,z146)}   A 
[4] {childof(z145,z146), -childof(z145,z146)}    (2, 3) Tautology
[5] {-parentof(z146,z145), parentof(z146,z145)}  (2, 3) Tautology
[6] {childof(z145,z146), -childof(z145,z146)}    (2, 3) Tautology
False

更新:实现正确性不是故事的终结。一个更有效的解决方案是将用于在Clause类中存储文字的容器用基于内置的基于Python Hash的集合的容器,但这似乎需要更彻底地重新进行供体实现的重新工作,并引入一些性能测试基础架构。

相关内容

  • 没有找到相关文章

最新更新