def test_parse_list_simple(self): assert parse_list("i:1") == conslist([integer(1)]) assert parse_list("i:1,i:1") == conslist([integer(1), integer(1)]) assert parse_list("p:10") == conslist([peano_num(10)]) assert parse_list("p:10,p:20") == conslist([peano_num(10), peano_num(20)])
def test_fail_lambda(self): w_int1 = integer(1) w_int2 = integer(2) l = lamb(([w_int1], w_int2)) with py.test.raises(NoMatch) as e: res = interpret_expression(mu(l, [w_int2]))
def test_fail_lambda(self): w_int1 = integer(1) w_int2 = integer(2) l = lamb(([w_int1], w_int2)) with py.test.raises(NoMatch) as e: l.call([w_int2])
def test_format_list_nest(self): l = conslist([integer(42), conslist([integer(42)])]) assert format_list(l) == ["42", "(42)"] assert format(l) == "(42,(42))" l = conslist([peano_num(42), conslist([peano_num(42)])]) assert format_list(l) == ["42", "(42)"] assert format(l) == "(42,(42))"
def test_catch_all(self): w_int = integer(1) rule = Rule([], expression(w_int)) assert rule.arity() == 0 expr = rule.match_all([integer(2)], []) assert expr.evaluate_with_binding([]) is w_int
def test_complex(self): var1 = Variable("x") var1.binding_index = 0 var2 = Variable("y") var2.binding_index = 1 var3 = Variable("z") var3.binding_index = 2 var4 = Variable("a") var4.binding_index = 3 var5 = Variable("b") var5.binding_index = 4 var6 = Variable("c") var6.binding_index = 5 w_int1 = integer(1) w_int2 = integer(2) w_int3 = integer(3) w_cons1 = cons("zork") w_cons2 = cons("barf", w_int1, w_int2) w_cons3 = cons("moep", w_cons1) expr1 = expression(cons("universe", var1, var2)) expr2 = expression(cons("moep", var3)) expr3 = expression(cons("universe", cons("barf", var4, var5), var6)) binding = [w_cons2, w_cons3, w_cons1, w_cons2, w_cons3, w_cons1] w_res = expr1.evaluate_with_binding(binding) assert w_res.get_tag() is tag("universe", 2) w_child0 = w_res.get_child(0) assert w_child0.get_tag() is tag("barf", 2) assert w_child0.get_child(0) is w_int1 assert w_child0.get_child(1) is w_int2 w_child1 = w_res.get_child(1) assert w_child1.get_tag() is tag("moep", 1) assert w_child1.get_child(0).get_tag() is tag("zork", 0) w_res = expr2.evaluate_with_binding(binding) assert w_res.get_tag() is tag("moep", 1) w_child0 = w_res.get_child(0) assert w_child0.get_tag() is tag("zork", 0) w_res = expr3.evaluate_with_binding(binding) assert w_res.get_tag() is tag("universe", 2) w_child0 = w_res.get_child(0) assert w_child0.get_tag() is tag("barf", 2) w_child00 = w_child0.get_child(0) assert w_child00.get_tag() is tag("barf", 2) assert w_child00.get_child(0) is w_int1 assert w_child00.get_child(1) is w_int2 w_child01 = w_child0.get_child(1) assert w_child01.get_tag() is tag("moep", 1) assert w_child01.get_child(0).get_tag() is tag("zork", 0) w_child1 = w_res.get_child(1) assert w_child1.get_tag() is tag("zork", 0)
def test_simple_append_processing(self): fun = all_functions["append"] args = ["1;i:1", "1;i:1"] ops = [fun.parse_arg(i, a) for i, a in enumerate(args)] assert ops == [conslist([integer(1)]), conslist([integer(1)])] ret = run(fun.lamb, ops) assert plist(ret) == [integer(1), integer(1)] assert fun.format_ret(ret) == "(1,1)"
def test_simple_rule(self): w_int = integer(1) expr = expression(w_int) rule = Rule([pattern(w_int)], expr) assert rule.arity() == 1 res = rule.match_all([w_int], []) assert res.evaluate_with_binding([]) is w_int with py.test.raises(NoMatch) as e: rule.match_all([integer(2)], [])
def test_constructor_with_int(self): w_cons = cons("zork", integer(1)) pat = pattern(w_cons) w_obj = cons("zork", integer(1)) binding = [] pat.match(w_obj, binding) assert binding == [] w_obj = cons("zork", integer(2)) with py.test.raises(NoMatch) as e: pat.match(w_obj, binding)
def test_int_pattern(self): w_int = integer(1) pat = pattern(w_int) w_obj = integer(1) binding = [] pat.match(w_obj, binding) assert True # should not raise. w_obj = integer(2) with py.test.raises(NoMatch) as e: pat.match(w_obj, binding)
def test_nary_constructors(self): for i in range(12): w_children = [integer(n) for n in range(i)] w_res = cons("zork", *w_children) assert isinstance(w_res, W_Constructor) assert w_res.get_tag() is tag("zork", len(w_children)) assert w_res.get_number_of_children() is i if i > 0: assert w_res.get_child(i - 1) == integer(i - 1) with py.test.raises(IndexError) as e: w_res.get_child(i)
def test_parse_list_len(self): assert parse_list("1;i:1") == conslist([integer(1)]) assert parse_list("2;i:1,i:1") == conslist([integer(1), integer(1)]) assert parse_list("1;p:10") == conslist([peano_num(10)]) assert parse_list("2;p:10,p:20") == conslist([peano_num(10), peano_num(20)]) l = [peano_num(10), peano_num(10), peano_num(10)] assert parse_list("3;p:10") == conslist(l) l = [peano_num(2), peano_num(10), peano_num(10)] assert parse_list("3;p:2,p:10") == conslist(l)
def test_multi_rule(self): w_int0 = integer(0) w_int1 = integer(1) w_int2 = integer(2) expr = expression(w_int0) rule = Rule([pattern(w_int1), pattern(w_int2)], expr) assert rule.arity() == 2 res = rule.match_all([w_int1, w_int2], []) assert res.evaluate_with_binding([]) is w_int0 with py.test.raises(NoMatch) as e: rule.match_all([w_int2, w_int1], [])
def test_format_list_simple(self): l = conslist([integer(42)]) assert format_list(l) == ["42"] assert format(l) == "(42)" l = conslist([peano_num(42)]) assert format_list(l) == ["42"] assert format(l) == "(42)" l = conslist([integer(42), integer(43)]) assert format_list(l) == ["42", "43"] assert format(l) == "(42,43)" l = conslist([peano_num(42), peano_num(43)]) assert format_list(l) == ["42", "43"] assert format(l) == "(42,43)"
def test_recursive_predefined_shape(self): w_1 = integer(1) barf_1 = clean_tag("barf", 1) shape_1 = CompoundShape(barf_1, [in_storage_shape]) c_1 = W_Constructor.construct(shape_1, [w_1]) assert c_1.get_number_of_children() == 1 assert c_1.get_child(0) == w_1 zork_2 = clean_tag("zork", 2) shape_2 = CompoundShape(zork_2, [shape_1, shape_1]) c_1_1 = W_Constructor.construct(shape_1, [w_1]) c_2 = W_Constructor.construct(shape_2, [w_1, w_1]) assert c_2.get_number_of_children() == 2 assert c_2.get_child(0) == c_1 assert c_2.get_child(0).get_child(0) == w_1 assert c_2.get_child(1).get_child(0) == w_1 foo_2 = clean_tag("foo", 2) shape_3 = CompoundShape(foo_2, [shape_2, shape_2]) c_1_3 = W_Constructor.construct(shape_1, [w_1]) c_1_4 = W_Constructor.construct(shape_1, [w_1]) c_2_1 = W_Constructor.construct(shape_2, [c_1_3, c_1_4]) # foo(zork(barf(1),barf(1)),zork(barf(1),barf(1))) c_3 = W_Constructor.construct(shape_3, [w_1, w_1, w_1, w_1]) assert c_3.get_number_of_children() == 2 assert c_3.get_child(0) == c_2 assert c_3.get_child(0).get_child(0) == c_1 assert c_3.get_child(0).get_child(0).get_child(0) == w_1 assert c_3.get_child(0).get_child(1) == c_1 assert c_3.get_child(0).get_child(1).get_child(0) == w_1 assert c_3.get_child(1).get_child(0).get_child(0) == w_1 assert c_3.get_child(1).get_child(1).get_child(0) == w_1
def test_compound_shape_merge_1(self): """ (zork (barf 1) (barf 1)) """ w_1 = integer(1) barf_1 = clean_tag("barf", 1) shape_1 = CompoundShape(barf_1, [in_storage_shape]) c_1 = W_Constructor.construct(shape_1, [w_1]) c_1_1 = W_Constructor.construct(shape_1, [w_1]) zork_2 = clean_tag("zork", 2) shape_2 = CompoundShape(zork_2, [in_storage_shape, in_storage_shape]) shape_2_1 = CompoundShape(zork_2, [shape_1, in_storage_shape]) shape_2_2 = CompoundShape(zork_2, [in_storage_shape, shape_1]) shape_2_3 = CompoundShape(zork_2, [shape_1, shape_1]) shape_2.transformation_rules[(0, shape_1)] = shape_2_1 shape_2.transformation_rules[(1, shape_1)] = shape_2_2 shape_2_1.transformation_rules[(1, shape_1)] = shape_2_3 shape_2_2.transformation_rules[(0, shape_1)] = shape_2_3 storage = [c_1, c_1_1] (new_shape, new_storage) = shape_2.fusion(storage) assert new_shape == CompoundShape(zork_2, [shape_1, shape_1]) assert new_storage == [w_1, w_1]
def test_bounded_deep_structures(self, no_cover): w_1 = integer(1) c = clean_tag("cons", 2) def _cons(*ch): children = list(ch) pre_shape = c.default_shape shape, storage = pre_shape.fusion(children) constr = W_Constructor.construct(shape, storage) return constr def _conslist(p_list): result = nil() for element in reversed(p_list): result = _cons(element, result) return result with SConf(substitution_threshold=17): def check_width(c, width): if isinstance(c, W_Constructor) and not is_nil(c): assert c.get_storage_width() < width # We deliberately use a n-ary Constructor, hence, # know that _structure is there for child in c._storage: check_width(child, width) sys.setrecursionlimit(100000) for num in [50, 100, 1000, 10000, 50000]: l = _cons(w_1, nil()) for i in range(num): l = _cons(w_1, l) check_width(l, 25)
def test_cons_list(self): w_1 = integer(1) cons_ = clean_tag("cons", 2) nil_ = clean_tag("nil", 0) nil_shape = CompoundShape(nil_, []) w_nil_ = W_Constructor.construct(nil_shape, []) list_default_shape = CompoundShape( cons_, [in_storage_shape, in_storage_shape]) list_1_shape = CompoundShape(cons_, [in_storage_shape, nil_shape]) list_2_shape = CompoundShape(cons_, [in_storage_shape, list_1_shape]) list_default_shape.transformation_rules[(1, nil_shape)] = list_1_shape list_default_shape.transformation_rules[(1, list_1_shape)] = list_2_shape w_list_0 = w_nil_ (shape, storage) = list_default_shape.fusion([w_1, w_nil_]) w_list_1 = W_Constructor.construct(shape, storage) list_1_shape.transformation_rules[(1, list_1_shape)] = list_2_shape (shape, storage) = list_default_shape.fusion([w_1, w_list_1]) w_list_2 = W_Constructor.construct(shape, storage) assert w_list_2._storage == [w_1, w_1]
def test_simple_expression(self): w_int = integer(1) expr = expression(w_int) binding = [] w_res = expr.evaluate_with_binding(binding) assert w_res is w_int
def test_simple_shape_non_merge(self): w_1 = integer(1) barf_0 = clean_tag("barf", 0) shape_0 = CompoundShape(barf_0, []) storage = [] (new_shape, new_storage) = shape_0.fusion(storage) assert new_shape == shape_0 assert new_storage == storage w_1 = integer(1) barf_1 = clean_tag("barf", 1) shape_1 = CompoundShape(barf_1, [in_storage_shape]) storage = [w_1] (new_shape, new_storage) = shape_1.fusion(storage) assert new_shape == shape_1 assert new_storage == storage
def prim_minus(w_args): from theseus.model import W_Integer assert len(w_args) == 2 w_arg0 = w_args[0] assert isinstance(w_arg0, W_Integer) w_arg1 = w_args[1] assert isinstance(w_arg1, W_Integer) return integer(w_arg0.value() - w_arg1.value())
def test_post_recursive_structures(self): c = clean_tag("cons", 2) def _cons(car, cdr): children = [car, cdr] pre_shape = c.default_shape shape, storage = pre_shape.fusion(children) constr = W_Constructor.construct(shape, storage) return constr # Be near immediate with SConf(substitution_threshold=2): assert len(c.default_shape.transformation_rules) == 0 assert len(c.default_shape._hist) == 0 cell = _cons(integer(1), nil()) assert len(c.default_shape.transformation_rules) == 0 assert len(c.default_shape._hist) == 1 cell2 = _cons(integer(1), cell) assert len(c.default_shape.transformation_rules) == 0 assert len(c.default_shape._hist) == 2 cell3 = _cons(integer(1), cell2) assert len(c.default_shape.transformation_rules) == 1 assert len(c.default_shape._hist) == 2 condition, result_shape = \ c.default_shape.transformation_rules.items()[0] assert condition[0] == 1 # pos assert condition[1] is c.default_shape # shape assert result_shape._tag is c # same tag assert len(result_shape._structure) == 2 assert result_shape._structure[0] is in_storage_shape assert result_shape._structure[1] is c.default_shape assert cell3._shape is result_shape
def test_constructor_with_var(self): var = Variable("x") var.binding_index = 0 w_cons = cons("zork", var) w_int = integer(1) expr = expression(w_cons) binding = [w_int] w_res = expr.evaluate_with_binding(binding) assert w_res.get_child(0) == w_int
def test_constructor_with_int(self): for num in range(0, 12): w_int = integer(1) w_children = [w_int] * num w_cons = cons("zork", *w_children) expr = expression(w_cons) binding = [] w_res = expr.evaluate_with_binding(binding) assert w_res == w_cons
def test_var_rule(self): w_int = integer(1) var = Variable("x") expr = expression(var) rule = Rule([pattern(var)], expr) binding = [None] * rule.maximal_number_of_variables res = rule.match_all([w_int], binding) result = res.evaluate_with_binding(binding) assert result is w_int
def test_constructor_with_var(self): var = Variable("x") pat = pattern(cons("zork", var)) w_int = integer(1) w_obj = cons("zork", w_int) binding = [None] var.binding_index = 0 pat.match(w_obj, binding) assert binding[var.binding_index] == w_int
def test_complex(self): var1 = Variable("x") var1.binding_index = 0 var2 = Variable("y") var2.binding_index = 1 var3 = Variable("z") var3.binding_index = 2 var4 = Variable("a") var4.binding_index = 3 var5 = Variable("b") var5.binding_index = 4 var6 = Variable("c") var6.binding_index = 5 w_int1 = integer(1) w_int2 = integer(2) w_int3 = integer(3) w_cons1 = cons("zork") w_cons2 = cons("barf", w_int1, w_int2) w_cons3 = cons("moep", w_cons1) w_cons4 = cons("universe", w_cons2, w_cons3) pat1 = pattern(cons("universe", var1, var2)) pat2 = pattern(cons("moep", var3)) pat3 = pattern(cons("universe", cons("barf", var4, var5), var6)) binding = [None] * 6 pat1.match(w_cons4, binding) assert binding[var1.binding_index] == w_cons2 assert binding[var2.binding_index] == w_cons3 binding = [None] * 6 pat2.match(w_cons3, binding) assert binding[var3.binding_index] == w_cons1 binding = [None] * 6 pat3.match(w_cons4, binding) assert binding[var4.binding_index] == w_int1 assert binding[var5.binding_index] == w_int2 assert binding[var6.binding_index] == w_cons3
def test_parse_list_fun_prim(self): l = [integer(10), integer(11), integer(12)] assert parse_list("3;i:10,f:+") == conslist(l) l = [integer(2), integer(10), integer(11)] assert parse_list("3;i:2,i:10,f:+") == conslist(l)
def test_default_shape(self): w_1 = integer(1) barf = clean_tag("barf", 3) w_barf = w_constructor(barf, [w_1, w_1, w_1]) assert w_barf._shape == CompoundShape( barf, [in_storage_shape, in_storage_shape, in_storage_shape]) w_barf_1 = w_constructor(barf, [w_1, w_1, w_1]) assert w_barf_1._shape is w_barf._shape
def test_variable_expression(self): w_int = integer(42) var = Variable("x") var.binding_index = 0 expr = expression(var) binding = [w_int] w_res = expr.evaluate_with_binding(binding) assert w_res is w_int with py.test.raises(VariableUnbound) as e: expr.evaluate_with_binding([None])