def test_parse_list_fun_fun(self): l = [peano_num(10), peano_num(11), peano_num(12)] assert parse_list("3;p:10,f:succ") == conslist(l) l = [peano_num(2), peano_num(10), peano_num(11)] assert parse_list("3;p:2,p:10,f:succ") == conslist(l)
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_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_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_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 parse_list(arg): """ [num;]fmt:elem,fmt:elem[,f:func] """ num = -1 num_elem = arg.split(";", 1) elements = None if len(num_elem) > 1: num = int(num_elem[0]) elements = num_elem[1].split(",", -1) else: elements = num_elem[0].split(",", -1) elements_given = len(elements) max_index = num if num > -1 else elements_given fun = None l = [None] * max_index for index in range(max_index): if index >= elements_given: if fun is None: l[index] = l[index - 1] else: l[index] = fun.apply_to(l[index - 1]) else: fmt, e = elements[index].split(":", 1) if fmt == "f": fun = list_fun(e) l[index] = fun.apply_to(l[index - 1]) else: l[index] = parse(fmt, e) assert None not in l return conslist(l)
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_append(self): x1 = Variable("x") x2 = Variable("x") h = Variable("head") t = Variable("tail") l = lamb() l._rules = ziprules( ([nil(), x1], x1), ([cons("cons", h, t), x2], cons("cons", h, mu(l, [t, x2])))) list1_w = [integer(1), integer(2), integer(3)] list2_w = [integer(4), integer(5), integer(6)] assert plist(l.call([conslist(list1_w), conslist(list2_w)])) == list1_w + list2_w
def test_mapping(self): fun = all_functions["map"] args = ["succ", "5;p:1,f:succ"] l = 5 ops = [fun.parse_arg(i, a) for i, a in enumerate(args)] assert ops[0] == all_functions["succ"].lamb assert ops[1] == conslist([peano_num(i + 1) for i in range(l)]) ret = run(fun.lamb, ops) assert plist(ret) == [peano_num(i + 2) for i in range(l)]
def test_append(self): x1 = Variable("x") x2 = Variable("x") h = Variable("head") t = Variable("tail") l = lamb() l._name = "append" l._rules = ziprules( ([nil(), x1], x1), ([cons("cons", h, t), x2], cons("cons", h, mu(l, [t, x2])))) list1_w = [integer(1), integer(2), integer(3)] list2_w = [integer(4), integer(5), integer(6)] res = interpret_expression( mu(l, [conslist(list1_w), conslist(list2_w)])) assert plist(res) == list1_w + list2_w
def test_mapping_cliissue(self): from theseus.shape import CompoundShape from theseus.tests.test_shape import SConf with SConf(substitution_threshold=1): fun = all_functions["map"] args = ["succ", "10;p:1,f:succ"] l = 10 ops = [fun.parse_arg(i, a) for i, a in enumerate(args)] assert ops[0] == all_functions["succ"].lamb assert ops[1] == conslist([peano_num(i + 1) for i in range(l)]) ret = run(fun.lamb, ops) assert plist(ret) == [peano_num(i + 2) for i in range(l)]
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_map(self): """ in scheme (define (map proc lis) (cond ((null? lis) '()) ((pair? lis) (cons (proc (car lis)) (map proc (cdr lis)))))) nil ≔ (nil) map ≔ λ: F, (cons X, Y) ↦ (cons μ(F, X), μ(map, F, Y)) _, nil ↦ nil """ from mu.peano import startup_peano, _succ startup_peano() f = Variable("F") x = Variable("X") y = Variable("Y") _ = Variable("_") _2 = Variable("_") m = lamb() m._name = "map" m._rules = ziprules( ([f, cons("cons", x, y)], cons("cons", mu(f, [x]), mu(m, [f, y]))), ([_, nil()], nil())) x1 = Variable("x") list_w = [peano_num(1), peano_num(2), peano_num(3)] #list_w = [peano_num(1)] res = interpret_expression(mu(m, [_succ(), conslist(list_w)])) assert plist(res) == [peano_num(2), peano_num(3), peano_num(4)]
def test_parse_list(self): l = [integer(2), integer(10), integer(11)] assert parse("l", "3;i:2,i:10,f:+") == conslist(l)
def convert_arguments(arguments): from theseus.util.construction_helper import conslist from theseus import model return conslist([model.w_string(arg) for arg in arguments])
def explode(s): l = [model.w_string(s[i]) for i in range(len(s))] return conslist(l)