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)
def test_dummy_times_between(self): _, Y = DummyVar('_'), Var('Y') eq_(eval(begin(parse_text(times_between(char(_), 2, 3, _, Y), '234'), Y)), ['2','3', '4']) eq_(eval(begin(parse_text(times_between(char(_), 2, 3, _, Y), '23'), Y)), ['2','3']) eq_(eval(begin(parse_text(times_between(char(_), 2, 3, _, Y), '2345'), Y)), ['2','3', '4']) assert_raises(NoSolution, eval, begin(parse_text(times_between(char(_), 2, 3, _, Y), '2'), Y)) assert_raises(NoSolution, eval, begin(parse_text((and_p, times_between(char(_), 2, 3, _, Y), eoi), '2345'), Y))
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)
def test_dummy_any(self): _, Y = DummyVar('_'), Var('Y') eq_(eval(begin(parse_text(any(char(_), _, Y), '222'), Y)), ['2', '2', '2']) eq_(eval(begin(parse_text(any(char(_), _, Y), '234'), Y)), ['2', '3', '4']) eq_(eval(begin(parse_text(any(char(_), _, Y), ''), Y)), [])
def test_some(self): X, Y = Var('X'), Var('Y') eq_(eval(begin(parse_text(some(char(X), X, Y), '222'), Y)), ['2', '2', '2']) eq_(eval(begin(parse_text(some(char(X), X, Y), '234'), Y)), ['2']) assert_raises(NoSolution, eval, begin(parse_text(some(char(X), X, Y), ''), Y))
def test_times_a2(self): X, Y, S = Var('X'), Var('Y'), Var('S') function1 = function(((Y, ), times(char('a'), 2, 'a', Y))) eq_(eval(begin(parse_text(function1(X), 'aa'), X)), ['a', 'a']) assert_raises(NoSolution, eval, begin(parse_text(function1(X), 'a'), X)) assert_raises(NoSolution, eval, begin(parse_text(and_p(function1(X), eoi), 'aaa'), X))
def test_dumy_some(self): _, Y = DummyVar('_'), Var('Y') eq_(eval(begin(parse_text(some(char(_), _, Y), '222'), Y)), ['2', '2', '2']) eq_(eval(begin(parse_text(some(char(_), _, Y), '234'), Y)), ['2', '3', '4']) assert_raises(NoSolution, eval, begin(parse_text(some(char(_), _, Y), ''), Y))
def test_dummy_times_more(self): _, Y = DummyVar('_'), Var('Y') eq_(eval(begin(parse_text((times_more, char(_), 3, _, Y), '234'), Y)), ['2', '3', '4']) eq_(eval(begin(parse_text((times_more, char(_), 3, _, Y), '2345'), Y)), ['2', '3', '4', '5']) assert_raises(NoSolution, eval, begin(parse_text(times_more(char(_), 3, _, Y), '23'), Y))
def test_dummy_seplist(self): _, Y = DummyVar('_'), Var('Y') eq_( eval( begin(parse_text(seplist(char(_), char(','), _, Y), '2,2,2'), Y)), ['2', '2', '2']) eq_( eval( begin(parse_text(seplist(char(_), char(','), _, Y), '2,3,4'), Y)), ['2', '3', '4']) eq_(eval(begin(parse_text(seplist(char(_), char(','), _, Y), '2'), Y)), ['2'])
def test_seplist(self): X, Y = Var('X'), Var('Y') eq_( eval( begin(parse_text(seplist(char(X), char(','), X, Y), '2,2,2'), Y)), ['2', '2', '2']) eq_( eval( begin(parse_text(seplist(char(X), char(','), X, Y), '2,3,4'), Y)), ['2']) eq_(eval(begin(parse_text(seplist(char(X), char(','), X, Y), '2'), Y)), ['2']) eq_(eval(begin(parse_text(seplist(char(X), char(','), X, Y), ''), Y)), [])
def test_times4(self): _ = DummyVar('_') x = LogicVar('x') eq_( eval( begin(set_text('aaa'), times(char(_), 3, _, x), eoi, getvalue(x))), ['a', 'a', 'a'])
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')
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)
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)))
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'])
def test_digit_string0(self): x = LogicVar('x') eq_(eval(parse_text(digits0, '')), '') eq_(eval(parse_text(digits0, 'a')), '') eq_(eval(parse_text(digits0, '123')), '123') eq_(eval(parse_text(is_(x, digits0), '123a')), '123') eq_(eval(begin(parse_text(eval_unify(x, digits0), '123 '), getvalue(x))), '123')
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'])
def test_some5(self): _ = DummyVar('_') y = LogicVar('y') eq_( eval( begin(set_text('abc'), some(char(_), _, y), char(_), eoi, getvalue(y))), ['a', 'b'])
def testblock3(self): a = Var('a') eq_( eval( block(a, 1, if_(0, continue_block(a), begin(exit_block(a, 2), 3)))), 2)
def test_greedy_some5(self): _ = DummyVar('_') y = LogicVar('y') assert_raises( NoSolution, eval, begin(set_text('aaa'), greedy_some(char(_), _, y), char(_), eoi, y))
def test_any5(self): _ = DummyVar('_') y = LogicVar('y') eq_( eval( begin(set_text('aaa'), any(char(_), _, y), char(_), eoi, getvalue(y))), ['a', 'a'])
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)
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))), '_')
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'])
def test_any_some(self): X, Y = Var('X'), Var('Y') eq_( eval( begin( parse_text( char(X) + any(~char('b') + some(char(X))) + eoi, 'abaaaa'), X)), 'a')
def compile(exp): compiler = Compiler() exp = il.element(exp).cps(compiler, done()) env = Environment() exp = exp.optimize(env, compiler) exps, has_statement = exp.pythonize(Environment(), compiler) compiler = Compiler() return begin(*exps).to_code(compiler)
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')
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 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_dummy_times_less_lazy(self): _, Y = DummyVar('_'), Var('Y') eq_( eval( preparse( begin( parse_text( times_less(char(_), 3, _, Y, lazy) + char('4'), '234'), Y))), ['2', '3'])
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 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 test_dummy_times_less(self): _, Y = DummyVar('_'), Var('Y') eq_( eval( preparse( begin( parse_text( times_less(char(_), 3, _, Y) + char('4'), '234'), Y))), ['2', '3']) eq_(eval(begin(parse_text(times_less(char(_), 3, _, Y), '234'), Y)), ['2', '3', '4']) eq_(eval(begin(parse_text(times_less(char(_), 3, _, Y), '23'), Y)), ['2', '3']) assert_raises( NoSolution, eval, preparse( begin(parse_text(times_less(char(_), 3, _, Y) + eoi, '2345'), Y)))
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 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 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_getvalue_default(self): x = LogicVar('x') eq_(eval(getvalue_default(x)), None) eq_(eval(getvalue_default(x, 1)), 1) eq_(eval(begin(unify(x, 2), getvalue(x))), 2)
def test_getvalue(self): x = LogicVar('x') x1 = DaoLogicVar('x') eq_(eval(getvalue(x)), x1) eq_(eval(begin(unify(x, 1), getvalue(x))), 1)
def test_findall_string_concat2(self): eq_(eval(begin(findall(concat(x, y, "abc"), L(x, y), z), z)), [L("a", "bc"), L("ab", "c")])
def test_sub_string2(self): eq_(eval(begin(findall(subsequence('ab', x, y, z, k), k, z), z)), ['a', 'ab', 'b'])
def test_sub_string(self): eq_(eval(begin(findall(subsequence('ab', 0, y, 2, "ab"), y, z), z)), [2])
def test_string_concat2(self): eq_(eval(begin(concat(x, y, "abcdef"), y)), "bcdef")
def test_string_concat(self): eq_(eval(concat("abc", "def", "abcdef")), True) eq_(eval(begin(concat("abc", "def", x), x)), "abcdef") eq_(eval(begin(concat(y, "def", "abcdef"), y)), "abc")
def test_cut_or4(self): x = LogicVar('x') eq_(eval(or_(or_(begin(unify(x, 1), cut_or, unify(x, 2)), unify(x, 2)), unify(x, 2))), True)
def test_cut_or2(self): assert_raises(NoSolution, eval, or_(begin(prin(1), cut_or, fail), prin(2)))
def test_getvalue(self): from dao.solvebase import conslist eq_(eval(begin(getvalue(L("abc", 1)))), conslist("abc", 1))
def test5(self): x = Var('x') Lx = LogicVar('x') assert_raises(NoSolution, eval, begin(unify(Lx, 1), unify(Lx,2)))
def test4(self): x = Var('x') Lx = LogicVar('x') eq_(eval(begin(unify(Lx, 1), unify(Lx,1))), True)
def test_cut_or(self): eq_(eval(or_(begin(prin(1), fail), prin(2))), None)
def test_findall_template_or(self): x, y, z = LogicVar('x'), LogicVar('y'), LogicVar('z') f = Var('f') eq_(eval(begin(findall(or_(is_(x, 1), is_(x, 2)), x, y), getvalue(y))), [1, 2])
def test_cut_or3(self): x = LogicVar('x') assert_raises(NoSolution, eval, or_(begin(unify(x, 1), cut_or, unify(x, 2)), unify(x, 2)))
def test_string_length2(self): eq_(eval(begin(length("abc", x), x)), 3)
def test_begin(self): eq_(alpha(begin(1,2)), begin(1,2))
def test_char_in(self): eq_(eval(begin(contain('abc', x), x)), 'a')