Beispiel #1
0
def test_unfiy_tricky():
    v1 = qvar()
    v2 = qvar()
    g1 = qfun(v1, v2)
    g2 = qfun(v2, qobj())

    assert_equal(unify([g1, g2]), qfun(qobj(), qobj()))
Beispiel #2
0
    def test_unify_types2(self):
        x1 = qterm.Variable('x', qvar())
        x2 = qterm.Variable('x', qvar())
        assert x1 != x2

        x1u, x2u = unify_types([x1, x2])
        assert x1u == x2u
Beispiel #3
0
 def print_separation(self, term):
     superset, predicate = match_separation(term)
     bound = term_builder.build_variable('x', qtype.qvar())
     pretty_predicate = substitution.beta_reduce(term_builder.unary_op(predicate, bound))
     return '{ %s in %s | %s }' % (self.print_atom(bound),
                                   self.print_term(superset),
                                   self.print_combination(pretty_predicate))
Beispiel #4
0
 def setup_class(cls):
     cls.for_all = Constant('for_all', qfun(qfun(qobj(), qbool()), qbool()))
     cls.exists = Constant('exists',  qfun(qfun(qobj(), qbool()), qbool()))
     cls.not_ = Constant('not', qfun(qbool(), qbool()))
     eqvar = qvar()
     cls.equals = Constant('=', qfun(eqvar, qfun(eqvar, qbool())))
     
     extensions = [
         Type(qbool, 'bool'),
         Type(qobj, 'obj'),
         Binder(cls.for_all),
         Binder(cls.exists),
         Operator(cls.not_, 1, 'right', 100),
         Operator(cls.equals, 2, 'left', 200),
         ]
         
     cls.parser = parser.Parser(Syntax(extensions), term_builder)
Beispiel #5
0
    def build_combination(self, operator, operand):
        if qtype.is_variable(operator.qtype):
            operator = operator.substitute_type(qtype.qfun(operand.qtype, qtype.qvar()), operator.qtype)

        if operator.qtype.name != "fun":
            raise TypeError("operators must be functions")

        unifier = qtype_unifier.TypeUnifier()
        unifier.unify(operator.qtype.args[0], operand.qtype)
        for key, value in unifier.get_substitutions().iteritems():
            operator = operator.substitute_type(value, key)
            operand = operand.substitute_type(value, key)

        operator, operand = unify_types([operator, operand])

        if operator.qtype.args[0] != operand.qtype:
            raise TypeError("operand type must match operator argument type")

        return qterm.Combination(operator, operand)
Beispiel #6
0
def test_unify():
    assert_raises(UnificationError, unify, [qobj(), qbool()])
    assert_equal(unify([qobj(), qobj()]), qobj())
    assert_equal(unify([qvar(), qobj()]), qobj())
    assert_equal(unify([qobj(), qvar()]), qobj())

    v1 = qvar()
    v2 = qvar()
    uni = unify([v1, v2])
    assert_true(uni == v1 or uni == v2)

    f1 = qfun(qobj(), qbool())
    f2 = qfun(qobj(), qobj())
    f3 = qfun(qvar(), qobj())
    f4 = qfun(qvar(), qvar())

    assert_raises(UnificationError, unify, [f1, f2])
    assert_equal(unify([f2, f3]), f2)
    assert_equal(unify([f2, f4]), f2)
    assert_equal(unify([f3, f4]), f3)
Beispiel #7
0
    def test_unify_types(self):
        f1 = qterm.Variable('f', qfun(qvar(), qvar()))
        f2 = qterm.Variable('f', qfun(qvar(), qvar()))
        assert f1 != f2

        f1u, f2u = unify_types([f1, f2])
        assert f1u == f2u

        # this is a particularly tricky situation
        v1 = qvar()
        v2 = qvar()
        g1 = qterm.Variable('g', qfun(v1, v2))
        g2 = qterm.Variable('g', qfun(v2, qobj()))
        assert g1 != g2

        g1u, g2u = unify_types([g1, g2])
        assert g1u == g2u
Beispiel #8
0
 def p_atomic_variable_type(self, p):
     "atomic_type : QMARK IDENT"
     if p[2] not in self.type_context:
         self.type_context[p[2]] = qtype.qvar()
     p[0] = self.type_context[p[2]]
Beispiel #9
0
 def p_atom(self, p):
     "atom : IDENT"
     p[0] = self.term_builder.build_variable(p[1], qtype.qvar())