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_mult(self): from mu.peano import startup_peano, _mult startup_peano() a_w = peano_num(2) b_w = peano_num(3) res = interpret_expression(mu(_mult(), [a_w, b_w])) assert python_num(res) == 6
def test_plus_acc(self): from mu.peano import startup_peano, _plus_acc startup_peano() a_w = peano_num(4) b_w = peano_num(5) res = interpret_expression(mu(_plus_acc(), [a_w, b_w])) assert python_num(res) == 9
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_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_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_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_plus(self): from mu.peano import peano_num, python_num, startup_peano, _plus startup_peano() n = 10 # n = 100 arg1 = peano_num(n) assert python_num(arg1) == n arg2 = peano_num(n) assert python_num(arg2) == n exp = mu(_plus(), [arg1, arg2]) assert python_num(arg2) == n assert python_num(arg1) == n res = interpret_expression(exp) assert python_num(arg2) == n assert python_num(arg1) == n assert python_num(res) == n + n
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_mult(self): from mu.peano import peano_num, python_num, startup_peano, _mult startup_peano() n = 4 # n = 100 arg1 = peano_num(n) assert python_num(arg1) == n arg2 = peano_num(n) assert python_num(arg2) == n exp = mu(_mult(), [arg1, arg2]) assert python_num(arg2) == n assert python_num(arg1) == n print "\n" * 10 res = interpret_expression(exp) assert python_num(arg2) == n assert python_num(arg1) == n assert python_num(res) == n * n
def parse(fmt, arg): from mu.peano import peano_num """ fmt mapping i W_Integer p peano-number (from int) l cons-list f function name ... """ if False: pass elif "i" == fmt: return integer(int(arg)) elif "p" == fmt: return peano_num(int(arg)) elif "l" == fmt: return parse_list(arg) elif "f" == fmt: try: return all_functions[arg].lamb except KeyError: raise UnknownFunction(arg) else: raise ArgfmtError(fmt)
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_format_peano_num(self): assert format(peano_num(42)) == "42"
def test_parse_peano(self): assert parse("p", "10") == peano_num(10)