def test_unfiy_tricky(): v1 = qvar() v2 = qvar() g1 = qfun(v1, v2) g2 = qfun(v2, qobj()) assert_equal(unify([g1, g2]), qfun(qobj(), qobj()))
def test_str(): assert str(qobj()) == 'obj' assert str(qbool()) == 'bool' assert str(qfun(qobj(), qbool())) == '(obj->bool)' assert str(qfun(qobj(), qbool())) == '(obj->bool)' assert (str(qfun(qfun(qobj(), qobj()), qbool())) == '((obj->obj)->bool)')
def test_free_variables(self): assert_equal(Combination(Variable('a', qfun(type_a(), type_a())), Constant('b', type_a())).free_variables(), set([Variable('a', qfun(type_a(), type_a()))])) assert_equal(Combination(Constant('a', qfun(type_a(), type_a())), Variable('b', type_a())).free_variables(), set([Variable('b', type_a())])) assert_equal(Combination(Variable('a', qfun(type_a(), type_a())), Variable('b', type_a())).free_variables(), set([Variable('a', qfun(type_a(), type_a())), Variable('b', type_a())]))
def test_parse_function(self): t = qfun(qobj(), qobj()) f = qterm.Variable('f', t) g = qterm.Variable('g', t) h = qterm.Variable('h', qfun(qobj(), t)) x = qterm.Variable('x', qobj()) y = qterm.Variable('y', qobj()) assert_equal(self.parser.parse('f:obj->obj(x)'), term_builder.build_combination(f, x)) assert (self.parser.parse('f:obj->obj(g:obj->obj(x))') == term_builder.build_combination(f, term_builder.build_combination(g, x))) assert (self.parser.parse('h:obj->obj->obj(x, y)') == term_builder.build_binary_op(h, x, y))
def test_parse_typed_variable(self): var = self.parser.parse('var:bool') assert var.name == 'var' assert var.qtype == qbool() var = self.parser.parse('var:bool->obj') assert var.name == 'var' assert var.qtype == qfun(qbool(), qobj())
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
def test_constructor(self): # operator is not a function assert_raises(TypeError, Combination, Constant('a', type_a()), Constant('b', type_b())) # operand is wrong type assert_raises(TypeError, Combination, Constant('a', qfun(type_a(), type_b())), Constant('d', type_b()))
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)
def test_parse_constant_type(self): matches = [ ('bool', qbool()), ('(bool)', qbool()), ('obj', qobj()), ('obj->obj', qfun(qobj(), qobj())), ('obj->obj->obj', qfun(qobj(), qfun(qobj(), qobj()))), ('(obj->obj)->obj', qfun(qfun(qobj(), qobj()), qobj())), ('obj->bool', qfun(qobj(), qbool())), ] for string, obj in matches: assert self.parser.parse_type(string) == obj
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)
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)
def qtype(self): return qtype.qfun(self.bound.qtype, self.body.qtype)
def make_fun(self, name, arg_type, result_type): return Constant(name, qfun(arg_type, result_type))
def p_function_type(self, p): "type : type ARROW type" p[0] = qtype.qfun(p[1], p[3])
def test_parse_abstraction(self): f = qterm.Variable('f', qfun(qbool(), qbool())) x = qterm.Variable('x', qbool()) y = qterm.Variable('y', qbool())