示例#1
0
 def test_findall_template_func(self):
     x, y, z = LogicVar('x'), LogicVar('y'), LogicVar('z')
     f = Var('f')
     eq_(
         eval(
             let([(f, rules(((), 2), ((), 3)))], findall(is_(x, f()), x, y),
                 getvalue(y))), [2, 3])
示例#2
0
 def test_unify2(self):
     x = LogicVar('x')
     result = compile_optimize(unify(x, 2))
     x1 = il.LogicVar('x')
     x2 = il.ConstLocalVar('x')
     expect = il.begin(
         il.Assign(x2, il.Deref(LogicVar(x1))),
         il.If(
             il.IsLogicVar(x2),
             il.begin(il.SetBinding(x2, il.Integer(2)), il.TRUE),
             il.If(il.Eq(x2, il.Integer(2)), il.TRUE,
                   il.failcont(il.TRUE))))
     eq_(result, expect)
示例#3
0
 def test_digit_string1(self):
     x = LogicVar('x')
     eq_(eval(parse_text(digits1, '123')), '123')
     eq_(eval(parse_text(is_(x, digits1), '123a')), '123')
     eq_(
         eval(begin(parse_text(eval_unify(x, digits1), '123 '),
                    getvalue(x))), '123')
示例#4
0
 def test_times4(self):
     _ = DummyVar('_')
     x = LogicVar('x')
     eq_(
         eval(
             begin(set_text('aaa'), times(char(_), 3, _, x), eoi,
                   getvalue(x))), ['a', 'a', 'a'])
示例#5
0
 def test_greedy_seplist5(self):
     _ = DummyVar('_')
     y = LogicVar('y')
     assert_raises(
         NoSolution, eval,
         begin(set_text('a,a,a'), greedy_seplist(char(_), char(','), _, y),
               char(_), eoi, getvalue(y)))
示例#6
0
 def test_seplist5(self):
     _ = DummyVar('_')
     y = LogicVar('y')
     eq_(
         eval(
             begin(set_text('a,b,cd'), seplist(char(_), char(','), _, y),
                   char(_), eoi, getvalue(y))), ['a', 'b', 'c'])
示例#7
0
 def test_greedy_some5(self):
     _ = DummyVar('_')
     y = LogicVar('y')
     assert_raises(
         NoSolution, eval,
         begin(set_text('aaa'), greedy_some(char(_), _, y), char(_), eoi,
               y))
示例#8
0
 def test_greedy_some4(self):
     _ = DummyVar('_')
     y = LogicVar('y')
     eq_(
         eval(
             begin(set_text('aaa'), greedy_some(char(_), _, y), eoi,
                   getvalue(y))), ['a', 'a', 'a'])
示例#9
0
 def test_some5(self):
     _ = DummyVar('_')
     y = LogicVar('y')
     eq_(
         eval(
             begin(set_text('abc'), some(char(_), _, y), char(_), eoi,
                   getvalue(y))), ['a', 'b'])
示例#10
0
 def test_any5(self):
     _ = DummyVar('_')
     y = LogicVar('y')
     eq_(
         eval(
             begin(set_text('aaa'), any(char(_), _, y), char(_), eoi,
                   getvalue(y))), ['a', 'a'])
示例#11
0
 def test2(self):
     x = Var('x')
     Lx = LogicVar('x')
     eq_(eval(unify(1, 1)), True)
     eq_(eval(begin(unify(1, 1), unify(2, 2))), True)
     eq_(eval(begin(unify(Lx, 1), unify(Lx, 1))), True)
     eq_(eval(let([(x, 1)], unify(x, 1))), True)
示例#12
0
 def test_lazy_seplist5(self):
     _ = DummyVar('_')
     y = LogicVar('y')
     eq_(
         eval(
             begin(set_text('a,a,aa'), lazy_seplist(char(_), char(','), _,
                                                    y), char(_), eoi,
                   getvalue(y))), ['a', 'a', 'a'])
示例#13
0
 def test_underline_letter_digit(self):
     x = LogicVar('x')
     eq_(eval(parse_text(underline_letter_digit, '1')), '1')
     eq_(eval(parse_text(is_(x, underline_letter_digit), 'a')), 'a')
     eq_(
         eval(
             begin(parse_text(eval_unify(x, underline_letter_digit), '_'),
                   getvalue(x))), '_')
示例#14
0
 def test_chars(self):
     x, cs, chars = LogicVar('x'), Var('cs'), MacroVar('chars')
     x1 = Var('x')
     eq_(
         eval(
             let([(chars, lamda(
                 (x1, cs), begin(char(x1), contain(cs, x1))))],
                 parse_text(chars(x, 'a'), 'a'), getvalue(x))), 'a')
示例#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)))
示例#16
0
 def test10(self):
     x = LogicVar('x')
     x1 = Const('x')
     y1 = Const('y')
     eq_(
         eval(
             begin(assign(x1, L(L(1, x))), assign(y1, L(L(1, x))),
                   unify(x1, y1))), True)
示例#17
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)
示例#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)))
示例#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)
示例#20
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)
示例#21
0
 def test_type(self):
     x = LogicVar('x')
     eq_(eval(isinteger(x)), False)
     eq_(eval(isinteger(1)), True)
     eq_(eval(isfloat(1)), False)
     eq_(eval(isfloat(1.0)), True)
     eq_(eval(isnumber(1.0)), True)
     eq_(eval(isnumber(1)), True)
     eq_(eval(istuple(())), True)
     eq_(eval(islist([])), True)
     eq_(eval(isdict({})), True)
示例#22
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'))
示例#23
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)
示例#24
0
 def test_quote2(self):
     x = LogicVar('x')
     eq_(eval(quote(x)), LogicVar('x'))
示例#25
0
 def test_let_embed_var2(self):
     e, f = LogicVar('e'), Var('f')
     eq_(eval(let([(f, rules([[1], 1]))], f(e), getvalue(e))), 1)
示例#26
0
 def test_let_rec_embed_var1(self):
     e, f = LogicVar('e'), Var('f')
     eq_(eval(letrec([(f, rules([[1], 1]))], f(e))), 1)
示例#27
0
 def test_quote(self):
     lo_x = LogicVar('x')
     eq_(eval(quote(lo_x)), LogicVar('x'))
示例#28
0
 def test2(self):
     lx = LogicVar('x')
     eq_(eval(rules([[1], 1], [[2], 2])(lx)), 1)
示例#29
0
 def test1(self):
     x = Var('x')
     lx = LogicVar('x')
     eq_(eval(rules([[x], x])(2)), 2)
     eq_(eval(rules([[1], 1], [[x], x])(2)), 2)
示例#30
0
 def test_unify_parse_text(self):
     x = LogicVar('x')
     eq_(eval(begin(set_text('abcde'), unify_parse_text(x), getvalue(x))),
         'abcde')
     eq_(eval(begin(set_text('abcde'), unify_parse_text('abcde'))), True)