def test_letrec_odd_even(self): from util import n, odd, even eq_( eval( letrec([(odd, lamda([n], if_(eq(n, 0), 0, even(sub(n, 1))))), (even, lamda([n], if_(eq(n, 0), 1, odd(sub(n, 1)))))], odd(3))), 1)
def test_letrec_fac(self): from util import m, n, fac eq_( eval( letrec([ (fac, lamda([n], if_(eq(n, 1), 1, mul(n, fac(sub(n, 1)))))) ], fac(3))), 6)
def testembedvar2(self): e, e2, f, g, h = Var('e'), Var('e2'), Var('f'), Var('g'), Var('h') eq_( eval( letrec([(f, macro([[cons(1, e2)], g(e2)])), (g, function([[e], h(e)])), (h, function([[1], True]))], f(e), e)), cons(1, 1))
def test_letrec(self): f, k, function = il.Var('f'), il.Var('k'), il.Var('function') result = cps(letrec([(f, lamda((), f()))], f())) expect = il.Clamda(v, il.Assign(f, v), v)(il.Lamda( (k, ), il.Clamda(function, function(k))(f))) eq_(result, expect)
def test_letrec(self): f, k, function = il.Var('f'), il.Var('k'), il.Var('function') result = cps(letrec([(f, lamda((), f()))], f())) expect = il.Clamda(v, il.Assign(f, v), v)( il.Lamda((k,), il.Clamda(function, function(k))(f))) eq_(result, expect)
def test_letrec_fac(self): from util import m, n fac = MacroVar('fac') eq_( eval( letrec([ (fac, macro([n], if_(eq(n, 1), 1, mul(n, fac(sub(n, 1)))))) ], fac(3))), 6)
def test_letrec_odd_even(self): from util import n odd, even = MacroVar('odd'), MacroVar('even') eq_( eval( letrec([(odd, macro([n], if_(eq(n, 0), 0, even(sub(n, 1))))), (even, macro([n], if_(eq(n, 0), 1, odd(sub(n, 1)))))], odd(3))), 1)
def testCut1(self): a, b, c, x = Var('a'), Var('b'), Var('c'), Var('x') Lx = LogicVar('x') eq_( eval( letrec([(a, rules([[x], begin(b(x), cut, c(x))])), (b, rules([[1], True], [[2], True], [[3], True])), (c, rules([[1], True]))], a(Lx), getvalue(Lx))), 1)
def test_cut2(self): a, b, c, x = Var('a'), Var('b'), Var('c'), Var('x') Lx = LogicVar('x') assert_raises( NoSolution, eval, letrec([(a, rules([[x], begin(b(x), cut, c(x))])), (b, rules([[1], True], [[2], True], [[3], True])), (c, rules([[2], True]))], a(Lx), getvalue(Lx)))
def test_kleene2(self): f, kleene = MacroVar('f'), MacroVar('kleene') _ = DummyVar('_') item = Var('item') fun = macro((item, ), letrec([(f, macrorules(((), item, f()), ((), nullword)))], f())) eq_(eval(let([(kleene, fun)], set_text('ab'), kleene(char(_)))), True)
def testKleene1(self): #occurs_check x, s, kleene = Var('x'), Var('s'), Var('kleene') x1 = Var('x') result = LogicVar('result') ruleList = [(kleene, rules( ((cons('a', x),), begin(char('a'), kleene(x))), ((nil,), nullword)))] eq_(eval(letrec(ruleList, parse_text(kleene(result), 'aa'), getvalue(result))), L('a', 'a'))
def test_cut2_no_Cut3_begin(self): a, b, c, d, x = Var('a'), Var('b'), Var('c'), Var('d'), Var('x') Lx = LogicVar('x') eq_( eval( letrec([(a, rules([[x], begin(b(x), c(x))], [[x], d(x)])), (b, rules([[1], 'b1'], [[4], 'b4'])), (c, rules([[4], 'c4'])), (d, rules([[3], 'd3']))], a(Lx), getvalue(Lx))), 4)
def test_kleene1(self): f, kleene = MacroVar('f'), MacroVar('kleene') item = Var('item') fun = macro((item,), letrec([(f, macrorules( ((), item, f()), ((), nullword)))], f())) eq_(eval(let([(kleene, fun)], set_text('aa'), kleene(char('a')))), True)
def testCut4(self): a, b, c, d, x = Var('a'), Var('b'), Var('c'), Var('d'), Var('x'), Lx = LogicVar('x') assert_raises( NoSolution, eval, letrec([(a, rules([[x], begin(b(x), cut, c(x))], [[x], d(x)])), (b, rules([[1], 'b1'], [[4], 'b4'])), (c, rules([[4], 'c4'])), (d, rules([[3], 'd3']))], a(Lx), getvalue(Lx)))
def test_cut2(self): a, b, c, x = Var('a'), Var('b'), Var('c'), Var('x') Lx = LogicVar('x') assert_raises(NoSolution, eval, letrec([(a, rules([[x], begin(b(x), cut, c(x))])), (b, rules([[1], True], [[2], True], [[3], True])), (c, rules([[2], True]))], a(Lx), getvalue(Lx)))
def testCut1(self): a, b, c, x = Var('a'), Var('b'), Var('c'), Var('x') Lx = LogicVar('x') eq_(eval(letrec([(a, rules([[x], begin(b(x), cut, c(x))])), (b, rules([[1], True], [[2], True], [[3], True])), (c, rules([[1], True]))], a(Lx), getvalue(Lx))), 1)
def test_macro6(self): n, act, ntimes = Var('n'), Var('act'), MacroVar('ntimes') eq_( eval( letrec([(ntimes, macro((n, act), if_(eq(n, 0), 1, begin(act, ntimes(sub(n, 1), act)))))], ntimes(3, println(1)))), 1)
def testCut4(self): a, b, c, d, x = Var('a'), Var('b'), Var('c'), Var('d'), Var('x'), Lx = LogicVar('x') assert_raises(NoSolution, eval, letrec([(a, rules([[x], begin(b(x), cut, c(x))], [[x], d(x)])), (b, rules([[1], 'b1'], [[4], 'b4'])), (c, rules([[4], 'c4'])), (d, rules([[3], 'd3']))], a(Lx), getvalue(Lx)))
def test_cut2_no_Cut3_begin(self): a, b, c, d, x = Var('a'), Var('b'), Var('c'), Var('d'), Var('x') Lx = LogicVar('x') eq_(eval(letrec([(a, rules([[x], begin(b(x),c(x))], [[x], d(x)])), (b, rules([[1], 'b1'], [[4], 'b4'])), (c, rules([[4], 'c4'])), (d, rules([[3], 'd3']))], a(Lx), getvalue(Lx))), 4)
def testKleene1(self): #occurs_check x, s, kleene = Var('x'), Var('s'), Var('kleene') x1 = Var('x') result = LogicVar('result') ruleList = [(kleene, rules(((cons('a', x), ), begin(char('a'), kleene(x))), ((nil, ), nullword)))] eq_( eval( letrec(ruleList, parse_text(kleene(result), 'aa'), getvalue(result))), L('a', 'a'))
def testCut5(self): start, a, b, c, d, x = Var('start'), Var('a'), Var('b'), Var('c'), Var( 'd'), Var('x'), Lx = LogicVar('x') eq_( eval( letrec([(start, rules([[x], a(x)], [[x], d(x)])), (a, rules([[x], begin(b(x), c(x))], [[x], d(x)])), (b, rules([[1], 'b1'], [[4], 'b4'])), (c, rules([[4], 'c4'])), (d, rules([[3], 'd3']))], start(Lx), getvalue(Lx))), 4)
def test_cut2_no_Cut2_and_(self): # test_cut2_no_Cut_and_p work correct. # but this test and test_cut2_no_Cut3_begin work wrong because the bug below: # bug in Var.getvalue/Var.setvalue: # dont't restore the longer chain of bindings after shorten it. a, b, c, d, x = Var('a'), Var('b'), Var('c'), Var('d'), Var('x') Lx = LogicVar('x') eq_( eval( letrec([(a, rules([[x], begin(b(x), c(x))], [[x], d(x)])), (b, rules([[1], True], [[4], True])), (c, rules([[4], True])), (d, rules([[3], True]))], a(Lx), getvalue(Lx))), 4)
def testCut6(self): start, a, b, c, d, x = Var('start'), Var('a'), Var('b'), Var('c'), Var('d'), Var('x'), Lx = LogicVar('x') eq_(eval(letrec([ (start, rules([[x], a(x)], [[x], d(x)])), (a, rules([[x], begin(b(x), cut, c(x))], [[x], d(x)])), (b, rules([[1], 'b1'], [[4], 'b4'])), (c, rules([[4], 'c4'])), (d, rules([[3], 'd3']))], start(Lx), getvalue(Lx))), 3)
def test_cut2_no_Cut2_and_(self): # test_cut2_no_Cut_and_p work correct. # but this test and test_cut2_no_Cut3_begin work wrong because the bug below: # bug in Var.getvalue/Var.setvalue: # dont't restore the longer chain of bindings after shorten it. a, b, c, d, x = Var('a'), Var('b'), Var('c'), Var('d'), Var('x') Lx = LogicVar('x') eq_(eval(letrec([(a, rules([[x], begin(b(x),c(x))], [[x], d(x)])), (b, rules([[1], True], [[4], True])), (c, rules([[4], True])), (d, rules([[3], True]))], a(Lx), getvalue(Lx))), 4)
def test_letrec_fac(self): from util import m, n, fac eq_(eval(letrec([(fac, lamda([n], if_(eq(n,1), 1, mul(n, fac(sub(n, 1))))))], fac(3))), 6)
def test_letrec_odd_even(self): from util import n, odd, even eq_(eval(letrec([(odd, lamda([n], if_(eq(n,0), 0, even(sub(n,1))))), (even, lamda([n], if_(eq(n,0), 1, odd(sub(n, 1)))))], odd(3))), 1)
def test_macro6(self): n, act, ntimes = Var('n'), Var('act'), MacroVar('ntimes') eq_(eval(letrec([(ntimes, macro((n, act), if_(eq(n, 0), 1, begin(act, ntimes(sub(n, 1), act)))))], ntimes(3, println(1)))), 1)
def test_letrec_rules(self): f = Var('f') x = Var('x') eq_(eval(letrec([(f, rules([[1], 1], [[x], f(sub(x, 1))]))], f(1))), 1) # passed eq_(eval(letrec([(f, rules([[1], 1], [[x], f(sub(x, 1))]))], f(2))), 1)
def test_let_rec_embed_var1(self): e, f = LogicVar('e'), Var('f') eq_(eval(letrec([(f, rules([[1], 1]))], f(e))), 1)
def xtest_letrec2(self): x, f = Var('x'), Var('f') eq_(eval(letrec([(f, lamda((x,), f(1)))], f(2))), 1)
def test_letrec3(self): x, f = Var('x'), MacroVar('f') eq_( eval( letrec([(f, macro((x, ), if_(eq(x, 1), 1, f(sub(x, 1)))))], f(2))), 1)
def test_letrec_assign2(self): x, f = Const('x'), Const('f') assert_raises(MultiAssignToConstError, eval, letrec([(f, macro((x, ), f(1), assign(f, 1)))], f(2)))
def testembedvar2(self): e, e2, f, g, h = Var('e'), Var('e2'), Var('f'), Var('g'), Var('h') eq_(eval(letrec([(f, macro([[cons(1, e2)], g(e2)])), (g, function([[e], h(e)])), (h, function([[1], True]))], f(e), e)), cons(1, 1))
def test_letrec3(self): x, f = Var('x'), MacroVar('f') eq_(eval(letrec([(f, macro((x,), if_(eq(x,1), 1, f(sub(x,1)))))], f(2))), 1)
def test_letrec_fac(self): from util import m, n fac = MacroVar('fac') eq_(eval(letrec([(fac, macro([n], if_(eq(n,1), 1, mul(n, fac(sub(n, 1))))))], fac(3))), 6)
def test_letrec_assign2(self): x, f = Const('x'), Const('f') assert_raises(MultiAssignToConstError, eval, letrec([(f, macro((x,), f(1), assign(f, 1)))], f(2)))
def xtest_letrec2(self): x, f = Var('x'), MacroVar('f') eq_(eval(letrec([(f, macro((x,), f(1)))], f(2))), 1)
def xtest_letrec2(self): x, f = Var('x'), MacroVar('f') eq_(eval(letrec([(f, macro((x, ), f(1)))], f(2))), 1)
def test_letrec_odd_even(self): from util import n odd, even = MacroVar('odd'), MacroVar('even') eq_(eval(letrec([(odd, macro([n], if_(eq(n,0), 0, even(sub(n,1))))), (even, macro([n], if_(eq(n,0), 1, odd(sub(n, 1)))))], odd(3))), 1)
def test_letrec(self): x, y = Const('x'), Const('y') eq_(eval(letrec([(x, 1), (y, x)], y)), 1) eq_(eval(letrec([(x, 1), (y, add(x, 1))], y)), 2)
def test_letrec_rules(self): f = Var('f') x = Var('x') eq_(eval(letrec([(f, rules([[1], 1],[[x],f(sub(x,1))]))], f(1))), 1) # passed eq_(eval(letrec([(f, rules([[1], 1],[[x],f(sub(x,1))]))], f(2))), 1)
def xtest_letrec2(self): x, f = Var('x'), Var('f') eq_(eval(letrec([(f, lamda((x, ), f(1)))], f(2))), 1)