Exemplo n.º 1
0
 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)
Exemplo n.º 2
0
 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)
Exemplo n.º 3
0
 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))
Exemplo n.º 4
0
 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)
Exemplo n.º 5
0
 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)
Exemplo n.º 6
0
 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)
Exemplo n.º 7
0
 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)
Exemplo n.º 8
0
 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)
Exemplo n.º 9
0
 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)))
Exemplo n.º 10
0
 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)
Exemplo n.º 11
0
 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'))
Exemplo n.º 12
0
 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)
Exemplo n.º 13
0
 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)
Exemplo n.º 14
0
 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)))
Exemplo n.º 15
0
 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)))
Exemplo n.º 16
0
 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) 
Exemplo n.º 17
0
 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)
Exemplo n.º 18
0
 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)))
Exemplo n.º 19
0
 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) 
Exemplo n.º 20
0
 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'))
Exemplo n.º 21
0
 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)
Exemplo n.º 22
0
 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)
Exemplo n.º 23
0
 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)
Exemplo n.º 24
0
 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) 
Exemplo n.º 25
0
 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)
Exemplo n.º 26
0
 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)
Exemplo n.º 27
0
 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)
Exemplo n.º 28
0
 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)
Exemplo n.º 29
0
 def test_let_rec_embed_var1(self):
     e, f = LogicVar('e'), Var('f')
     eq_(eval(letrec([(f, rules([[1], 1]))], f(e))), 1)
Exemplo n.º 30
0
 def xtest_letrec2(self):
   x, f = Var('x'), Var('f')
   eq_(eval(letrec([(f, lamda((x,), f(1)))], f(2))), 1)
Exemplo n.º 31
0
 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)
Exemplo n.º 32
0
 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)))
Exemplo n.º 33
0
 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))
Exemplo n.º 34
0
 def test_let_rec_embed_var1(self):
   e, f = LogicVar('e'), Var('f')
   eq_(eval(letrec([(f, rules([[1], 1]))], f(e))), 1)
Exemplo n.º 35
0
 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)
Exemplo n.º 36
0
 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)
Exemplo n.º 37
0
 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)))
Exemplo n.º 38
0
 def xtest_letrec2(self):
   x, f = Var('x'), MacroVar('f')
   eq_(eval(letrec([(f, macro((x,), f(1)))], f(2))), 1)
Exemplo n.º 39
0
 def xtest_letrec2(self):
     x, f = Var('x'), MacroVar('f')
     eq_(eval(letrec([(f, macro((x, ), f(1)))], f(2))), 1)
Exemplo n.º 40
0
 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)
Exemplo n.º 41
0
 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)
Exemplo n.º 42
0
 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)
Exemplo n.º 43
0
 def xtest_letrec2(self):
     x, f = Var('x'), Var('f')
     eq_(eval(letrec([(f, lamda((x, ), f(1)))], f(2))), 1)
Exemplo n.º 44
0
 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)