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_parse_operator(self): assert (self.parser.parse('not a') == term_builder.build_combination(self.not_, qterm.Variable('a', qbool()))) assert (self.parser.parse('a:obj = b:obj') == term_builder.build_binary_op(self.equals, qterm.Variable('a', qobj()), qterm.Variable('b', qobj())))
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 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 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 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_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_binder(self): assert (self.parser.parse('for_all x phi') == term_builder.build_binder(self.for_all, qterm.Variable('x', qobj()), qterm.Variable('phi', qbool())))