Beispiel #1
0
    def s_retrieve(self, trace=False):
        """
        Carry out S-Retrieval of binding operators in store. If hack=True,
        serialize the bindop and core as strings and reparse. Ugh.

        Each permutation of the store (i.e. list of binding operators) is
        taken to be a possible scoping of quantifiers. We iterate through the
        binding operators in each permutation, and successively apply them to
        the current term, starting with the core semantic representation,
        working from the inside out.

        Binding operators are of the form::

             bo(\P.all x.(man(x) -> P(x)),z1)
        """
        for perm, store_perm in enumerate(self._permute(self.store)):
            if trace:
                print("Permutation %s" % (perm + 1))
            term = self.core
            for bindop in store_perm:
                # we just want the arguments that are wrapped by the 'bo' predicate
                quant, varex = tuple(bindop.args)
                # use var to make an abstraction over the current term and then
                # apply the quantifier to it
                term = ApplicationExpression(
                    quant, LambdaExpression(varex.variable, term))
                if trace:
                    print("  ", term)
                term = term.simplify()
            self.readings.append(term)
    def s_retrieve(self, trace=False):
        """
        Carry out S-Retrieval of binding operators in store. If hack=True,
        serialize the bindop and core as strings and reparse. Ugh.

        Each permutation of the store (i.e. list of binding operators) is
        taken to be a possible scoping of quantifiers. We iterate through the
        binding operators in each permutation, and successively apply them to
        the current term, starting with the core semantic representation,
        working from the inside out.

        Binding operators are of the form::

             bo(\P.all x.(man(x) -> P(x)),z1)
        """
        for perm, store_perm in enumerate(self._permute(self.store)):
            if trace:
                print("Permutation %s" % (perm+1))
            term = self.core
            for bindop in store_perm:
                # we just want the arguments that are wrapped by the 'bo' predicate
                quant, varex = tuple(bindop.args)
                # use var to make an abstraction over the current term and then
                # apply the quantifier to it
                term = ApplicationExpression(quant, LambdaExpression(varex.variable, term))
                if trace:
                    print("  ", term)
                term = term.simplify()
            self.readings.append(term)
Beispiel #3
0
def demo():
    from nltk_contrib.drt import DRT

    DRT.testTp_equals()
    print '\n'
    
    lp = LogicParser()
    a = lp.parse(r'some x.((man x) and (walks x))')
    b = lp.parse(r'some x.((walks x) and (man x))')
    bicond = ApplicationExpression(ApplicationExpression(Operator('iff'), a), b)
    print "Trying to prove:\n '%s <-> %s'" % (a.infixify(), b.infixify())
    print 'tableau: %s' % get_prover(bicond, prover_name='tableau').prove()
    print 'Prover9: %s' % get_prover(bicond, prover_name='Prover9').prove()
    print '\n'
    
    demo_drt_glue_remove_duplicates()

    lp = LogicParser()
    a = lp.parse(r'all x.((man x) implies (mortal x))')
    b = lp.parse(r'(man socrates)')
    c1 = lp.parse(r'(mortal socrates)')
    c2 = lp.parse(r'(not (mortal socrates))')

    print get_prover(c1, [a,b], 'prover9').prove()
    print get_prover(c2, [a,b], 'prover9').prove()
    print get_model_builder(c1, [a,b], 'mace').build_model()
    print get_model_builder(c2, [a,b], 'mace').build_model()
Beispiel #4
0
def replace_function_names(expr, resolution_guide, active=None):
    if active is None:
        active = {}
    else:
        active = dict(active)
    if expr in resolution_guide:
        for prev_pred, new_pred in resolution_guide[expr]:
            active[prev_pred] = new_pred
    if isinstance(expr, ConstantExpression) or \
       isinstance(expr, AbstractVariableExpression) or \
       isinstance(expr, Variable):
        return expr
    elif isinstance(expr, NegatedExpression):
        expr.term = replace_function_names(expr.term, resolution_guide, active)
        return expr
    elif isinstance(expr, BinaryExpression):
        child_exprs = [expr.first, expr.second]
        exprs = [
            replace_function_names(e, resolution_guide, active)
            for e in child_exprs
        ]
        expr.first = exprs[0]
        expr.second = exprs[1]
    elif isinstance(expr, ApplicationExpression):
        func, args = expr.uncurry()
        if str(func) in active:
            func = type(func)(Variable(active[str(func)]))
        args_exprs = [
            replace_function_names(e, resolution_guide, active) for e in args
        ]
        exprs = [func] + args_exprs
        expr = functools.reduce(lambda f, a: ApplicationExpression(f, a),
                                exprs)
    elif isinstance(expr, VariableBinderExpression):
        child_exprs = [expr.variable, expr.term]
        exprs = [
            replace_function_names(e, resolution_guide, active)
            for e in child_exprs
        ]
        expr.variable = exprs[0]
        expr.term = exprs[1]
    else:
        raise NotImplementedError(
            'Expression not recognized: {0}, type: {1}'.format(
                expr, type(expr)))
    return expr
Beispiel #5
0
 def fol(self):
     return ApplicationExpression(self.function.fol(), self.argument.fol())