def testoptionalcut(self): x, s = Var('x'), Var('s') ruleList = [(s, function(((x, ), and_p(-char('a'), cut, char(x)))))] eq_(eval(let(ruleList, parse_text(s(x), 'aa'), x)), 'a') eq_(eval(let(ruleList, parse_text(s(x), 'b'), x)), 'b') assert_raises(NoSolutionFound, eval, let(ruleList, parse_text(s(x), 'a'), x))
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_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_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(NoSolutionFound, eval, begin(parse_text(times_between(char(_), 2, 3, _, Y), '2'), Y)) assert_raises(NoSolutionFound, 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_some2(self): X = Var('X') eq_(eval(parse_text(and_p(some(char(X)), char('4')), '224')), '4') eq_(eval(parse_text(and_p(some(char(X)), cut, char('4')), '224')), '4') assert_raises( NoSolutionFound, eval, parse_text(and_p(some(char(X)), char('3'), char('5')), '2234'))
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 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(NoSolutionFound, eval, begin(parse_text(some(char(X), X, Y), ''), Y))
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(NoSolutionFound, eval, begin(parse_text(some(char(_), _, Y), ''), Y))
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_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(NoSolutionFound, eval, begin(parse_text(times_more(char(_), 3, _, Y), '23'), Y))
def test_unify_right_recursive(self): x, p = Var('x'), Var('p') function1 = [(p,function( ((x,), and_p(char(x), p(x))), ((x,),char(x))))] eq_(eval(letr(function1, parse_text(p(x), 'aa'))), 'a') eq_(eval(letr(function1, parse_text(p(x), 'a'))), 'a') assert_raises(NoSolutionFound, eval, letr(function1, parse_text(and_p(p(x), eoi), 'xy'))) assert_raises(NoSolutionFound, eval, letr(function1, parse_text(p(x), '')))
def testIndentUndent(self): _, n, s, line = DummyVar('_'), Var('n'), Var('s'), Var('line') space = char(' ') ruleList = [(s,function( ((n,), some(line(n)),s(add(n,1))), ((n,), some(line(n))))), (line,function( ((n,), times(space, n),some(letter(_)),any(space),char('\n')))) ] eq_(eval(letr(ruleList, parse_text(s(0), 'a\n b\n c\n'))), True) eq_(eval(letr(ruleList, parse_text(s(0), 'asd\n bdf\n cdfh\n'))), True)
def testDirectLeftRecursive(self): #assert 0, 'temporary mask' E = Var('E') ruleList = [(E,function( ((), E()+char('a')), ((), char('b')), ))] eq_(eval(letr(ruleList, parse_text(E()+eoi, 'b'))), True) eq_(eval(letr(ruleList, parse_text(E()+eoi, 'ba'))), True) eq_(eval(letr(ruleList, parse_text(E()+eoi, 'baa'))), True)
def testKleene1(self): #occurs_check x, s, kleene = Var('x'), Var('s'), Var('kleene') ruleList = [(s,function( ((x,), kleene(x)))), (kleene,function( ((Cons('a', x),), and_p(char('a'), kleene(x))), ((nil,), nullword)))] eq_(eval(letr(ruleList, parse_text(s(x), 'aa'), x)), L('a', 'a')) eq_(eval(letr(ruleList, parse_text(s(x), ''), x)), nil) assert_raises(NoSolutionFound, eval, letr(ruleList, parse_text(and_p(s(x), eoi), '6'), x)) assert_raises(NoSolutionFound, eval, letr(ruleList, parse_text(and_p(s(x), eoi), '41'), x))
def test_unify_right_recursive(self): x, p = Var('x'), Var('p') function1 = [(p, function(((x, ), and_p(char(x), p(x))), ((x, ), char(x))))] eq_(eval(letr(function1, parse_text(p(x), 'aa'))), 'a') eq_(eval(letr(function1, parse_text(p(x), 'a'))), 'a') assert_raises(NoSolution, eval, letr(function1, parse_text(and_p(p(x), eoi), 'xy'))) assert_raises(NoSolution, eval, letr(function1, parse_text(p(x), '')))
def testOr(self): x, s, one, two = Var('x'), Var('s'), Var('one'), Var('two') ruleList = [(s, function( ((x,), or_p(one(x), two(x))))), (one, function( (('1',),char('1')))), (two, function( (('2',),char('2'))))] eq_(eval(letr(ruleList, parse_text(s(x), '1'))), '1') eq_(eval(letr(ruleList, parse_text(s(y), '2'))), '2') assert_raises(NoSolutionFound, eval, letr(ruleList, parse_text(s(x), '3'))) assert_raises(NoSolutionFound, eval, letr(ruleList, parse_text(and_p(s(x), eoi), '12'))) assert_raises(NoSolutionFound, eval, letr(ruleList, parse_text(s(x), '')))
def testOr(self): x, s, one, two = Var('x'), Var('s'), Var('one'), Var('two') ruleList = [(s, function(((x, ), or_p(one(x), two(x))))), (one, function((('1', ), char('1')))), (two, function((('2', ), char('2'))))] eq_(eval(letr(ruleList, parse_text(s(x), '1'))), '1') eq_(eval(letr(ruleList, parse_text(s(y), '2'))), '2') assert_raises(NoSolution, eval, letr(ruleList, parse_text(s(x), '3'))) assert_raises(NoSolution, eval, letr(ruleList, parse_text(and_p(s(x), eoi), '12'))) assert_raises(NoSolution, eval, letr(ruleList, parse_text(s(x), '')))
def testIndirectLeftRecursive(self): #assert 0, 'temporary mask' A, B, C = vars('A, B, C') ruleList = [(A, function(((), B()))), (B, function( ((), A()+char('a')), ((), char('b')), ))] eq_(eval(letr(ruleList, parse_text(A()+eoi, 'b'))), True) eq_(eval(letr(ruleList, parse_text(A()+eoi, 'ba'))), True) eq_(eval(letr(ruleList, parse_text(A()+eoi, 'baa'))), True)
def testIndirectLeftRecursive(self): #assert 0, 'temporary mask' A, B, C = vars('A, B, C') ruleList = [(A, function(((), B()))), (B, function( ((), A() + char('a')), ((), char('b')), ))] eq_(eval(letr(ruleList, parse_text(A() + eoi, 'b'))), True) eq_(eval(letr(ruleList, parse_text(A() + eoi, 'ba'))), True) eq_(eval(letr(ruleList, parse_text(A() + eoi, 'baa'))), True)
def testIndentUndent(self): _, n, s, line = DummyVar('_'), Var('n'), Var('s'), Var('line') space = char(' ') ruleList = [(s, function(((n, ), some(line(n)), s(add(n, 1))), ((n, ), some(line(n))))), (line, function(((n, ), times(space, n), some(letter(_)), any(space), char('\n'))))] eq_(eval(letr(ruleList, parse_text(s(0), 'a\n b\n c\n'))), True) eq_(eval(letr(ruleList, parse_text(s(0), 'asd\n bdf\n cdfh\n'))), True)
def testKleene1(self): #occurs_check x, s, kleene = Var('x'), Var('s'), Var('kleene') ruleList = [(s, function(((x, ), kleene(x)))), (kleene, function(((Cons('a', x), ), and_p(char('a'), kleene(x))), ((nil, ), nullword)))] eq_(eval(letr(ruleList, parse_text(s(x), 'aa'), x)), L('a', 'a')) eq_(eval(letr(ruleList, parse_text(s(x), ''), x)), nil) assert_raises(NoSolutionFound, eval, letr(ruleList, parse_text(and_p(s(x), eoi), '6'), x)) assert_raises(NoSolutionFound, eval, letr(ruleList, parse_text(and_p(s(x), eoi), '41'), x))
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 testKleene2(self): x, c, s, kleene = Var('x'), Var('c'), Var('s'), Var('kleene') ruleList = [(s,function( ((x,), kleene(c, x)))), (kleene,function( ((c, Cons(c, x)), and_p(char(c), kleene(c, x))), ((c, nil), nullword)))] eq_(eval(letr(ruleList, parse_text(s(x), 'aa'), x)), L('a', 'a')) eq_(eval(letr(ruleList, parse_text(s(x), 'aaa'), x)), L('a', 'a', 'a')) eq_(eval(letr(ruleList, parse_text(s(x), 'bbb'), x)), L('b', 'b', 'b')) eq_(eval(letr(ruleList, parse_text(s(x), ''), x)), nil) assert_raises(NoSolutionFound, eval, letr(ruleList, parse_text(and_p(s(x), eoi), 'aab'), x)) assert_raises(NoSolutionFound, eval, letr(ruleList, parse_text(and_p(s(x), eoi), 'abc'), x)) assert_raises(NoSolutionFound, eval, letr(ruleList, parse_text(and_p(s(x), eoi), '41'), x))
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 testExpressionByRightRecursiveList(self): E, e, e1, e2 = Var('E'), Var('e'), Var('e1'), Var('e2') ruleList = [(E,function( (((e1, '/', e2), 1), E(e1,2), char('/'), E(e2, 1)), ((1, 2), char('1')), ((e, 1), E(e, 2))))] eq_(eval(letr(ruleList, parse_text(E(e, 1), '1/1/1'), e)), (1, '/', (1, '/', 1))) eq_(eval(letr(ruleList, parse_text(E(e, 1), '1/1'), e)), (1, '/', 1)) eq_(eval(letr(ruleList, parse_text(E(e, 1), '1'), e)), 1) assert_raises(NoSolutionFound, eval, letr(ruleList, parse_text(and_p(E(e, 1), eoi), '1+1/1'), e)) assert_raises(NoSolutionFound, eval, letr(ruleList, parse_text(and_p(E(e, 1), eoi), '2'), e)) assert_raises(NoSolutionFound, eval, letr(ruleList, parse_text(and_p(E(e, 1), eoi), '1/'), e)) assert_raises(NoSolutionFound, eval, letr(ruleList, parse_text(and_p(E(e, 1), eoi), '/'), e)) assert_raises(NoSolutionFound, eval, letr(ruleList, parse_text(and_p(E(e, 1), eoi), ''), e))
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)
def testLoopWhen(self): eq_( eval( tag_loop_label( let([(i, 3)], LoopWhenForm((set(i, sub(i, 1)), prin(i)), gt(i, 0))))), None)
def test_kleene1(self): f, item, kleene = Var('f'), Var('item'), Var('kleene') fun = macro(((item, ), letr([(f, macro(((), eval_(item), f()), ((), nullword)))], f()))) eq_(eval(let([(kleene, fun)], set_text('aa'), kleene(char('a')))), True)
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 testLoopUntil(self): eq_( eval( tag_loop_label( let([(i, 3)], LoopUntilForm((set(i, sub(i, 1)), prin(i)), eq(i, 0))))), None)
def testRecursiveReturnValue3(self): E, e, e1, e2 = Var('E'), Var('e'), Var('e1'), Var('e2') ruleList = [(E, function(((e, 1), E(e, 2)), ((e, 2), char(e)), (((e1, e2), 1), E(e1, 2), E(e2, 1))))] eq_(eval(letr(ruleList, parse_text(E(e, 1) + eoi, '12'), e)), ('1', '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 testKleene2(self): x, c, s, kleene = Var('x'), Var('c'), Var('s'), Var('kleene') ruleList = [(s, function(((x, ), kleene(c, x)))), (kleene, function(((c, Cons(c, x)), and_p(char(c), kleene(c, x))), ((c, nil), nullword)))] eq_(eval(letr(ruleList, parse_text(s(x), 'aa'), x)), L('a', 'a')) eq_(eval(letr(ruleList, parse_text(s(x), 'aaa'), x)), L('a', 'a', 'a')) eq_(eval(letr(ruleList, parse_text(s(x), 'bbb'), x)), L('b', 'b', 'b')) eq_(eval(letr(ruleList, parse_text(s(x), ''), x)), nil) assert_raises(NoSolutionFound, eval, letr(ruleList, parse_text(and_p(s(x), eoi), 'aab'), x)) assert_raises(NoSolutionFound, eval, letr(ruleList, parse_text(and_p(s(x), eoi), 'abc'), x)) assert_raises(NoSolutionFound, eval, letr(ruleList, parse_text(and_p(s(x), eoi), '41'), x))
def test_findall_or_p(self): x, y = Var('x'), Var('y') eq_( eval( findall( macrorules([[x, y], x], [[x, y], y])(prin(1), prin(2)))), None)
def test_unwind_protect_loop(self): from util import a, i eq_(eval(let([(i,3)], block(a, assign(i, sub(i, 1)), if_(eq(i, 0), exit_block(a, 1), unwind_protect(continue_block(a), prin(i)))), i)), 0)
def test_kleene1(self): f, item, kleene = Var('f'), Var('item'), Var('kleene') fun = macro(((item,), letr([(f,macro(((), eval_(item), f()), ((), nullword)))], f()))) eq_(eval(let([(kleene,fun)], set_text('aa'), kleene(char('a')))), True)
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_chars(self): x, cs, chars = Var('x'), Var('cs'), Var('chars') eq_( eval( let([(chars, function( ((x, cs), and_p(char(x), contain(cs, x)))))], parse_text(chars(x, 'a'), 'a'))), True)
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 testblock3(self): a = Var('a') eq_( eval( block(a, 1, if_(0, continue_block(a), begin(exit_block(a, 2), 3)))), 2)
def test_kleene2(self): f, pred, kleene = Var('f'), Var('pred'), Var('kleene') fun = macro(((pred,), letr([(f,macro( ((x,), pred(x), f(x)), ((x,), nullword)))], f(x)))) eq_(eval(let([(kleene,fun)], set_text('ab'), kleene(char))), True)
def testLoopTimes(self): eq_( eval( tag_loop_label( let([(i, 3)], LoopTimesForm(3, (set(i, sub(i, 1)), prin(i)))))), None)
def test_right_recursive2(self): x, p = Var('x'), Var('p') function1 = [(p,function( ((), and_p(char(x), p())), ((),char(x))))] eq_(eval(letr(function1, parse_text(p(),'a'), parse_text(p(),'ab'), parse_text(p(),'abc'))), 'c') assert_raises(NoSolutionFound, eval, letr(function1, parse_text(p(), '')))
def test_right_recursive1(self): function1 = function( ((), and_p(char('a'), f())), ((),char('b'))) eq_(eval(letr([(f,function1)], parse_text(f(),'b'), parse_text(f(),'ab'), parse_text(f(),'aab'))), 'b') assert_raises(NoSolutionFound, eval, letr([(f,function1)], parse_text(f(), 'a')))
def test_any5(self): _ = DummyVar('_') y = LogicVar('y') eq_( eval( begin(set_text('aaa'), any(char(_), _, y), char(_), eoi, getvalue(y))), ['a', 'a'])
def testCut1(self): a, b, c, x = Var('a'), Var('b'), Var('c'), Var('x'), eq_(eval(letr([(a, function([[x], b(x)&cut&c(x)])), #[cut] = cut = cut() (b, function([[1], True], [[2], True], [[3], True])), (c, function([[1], True]))], a(x), x)), (1))
def test_if_p(self): from dao.builtins.control import if_p eq_(eval(if_p(succeed, succeed)), True) assert_raises(NoSolutionFound, eval, if_p(succeed, fail)) # This below unusual semantics is part of the ISO and all de-facto Prolog standards. # see SWI-Prolog help. assert_raises(NoSolutionFound, eval, if_p(fail, succeed)) assert_raises(NoSolutionFound, eval, if_p(fail, fail))
def testABCD(self): A, B, C, D = vars('A, B, C, D') ruleList = [(A,function(((), B()|C()))), (B,function(((), D()+char('b')))), (C,function(((), D()+char('c')))), (D,function(((), char('d')))), ] eq_(eval(letr(ruleList, parse_text(A()+eoi, 'dc'))), True)
def testRecursiveReturnValue3(self): E, e, e1, e2 = Var('E'), Var('e'), Var('e1'), Var('e2') ruleList = [(E,function( ((e, 1), E(e, 2)), ((e, 2), char(e)), (((e1, e2), 1), E(e1,2), E(e2, 1)) ))] eq_(eval(letr(ruleList, parse_text(E(e, 1)+eoi, '12'), e)), ('1', '2'))
def testloop(self): from util import a, i eq_(eval(let([(i,3)], block(a, assign(i, sub(i, 1)), if_(eq(i, 0), exit_block(a, 1), continue_block(a))), i)), 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'))
def testCut4(self): a, b, c, d, x = Var('a'), Var('b'), Var('c'), Var('d'), Var('x'), eq_(eval(letr([(a, function([[x], b(x)&cut&c(x)], [[x], d(x)])), (b, function([[1], 'b1'], [[4], 'b4'])), (c, function([[4], 'c4'])), (d, function([[3], 'd3']))], a(x), x)), 3)
def test_cut2_no_Cut3_begin(self): a, b, c, d, x = Var('a'), Var('b'), Var('c'), Var('d'), Var('x') eq_(eval(letr([(a, function([[x], begin(b(x),c(x))], [[x], d(x)])), (b, function([[1], 'b1'], [[4], 'b4'])), (c, function([[4], 'c4'])), (d, function([[3], 'd3']))], a(x), x)), 4)
def test_open_read2(self): file1 = Var('file1') x = Var('x') eq_(eval(let([(file1, open_file('test.txt'))], readline(file1), assign(x, readlines(file1)), close_file(file1), x)), ['hello'])
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_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 testparallelRule(self): x, s, gt, lt = Var('x'), Var('s'), Var('>'), Var('<') ruleList = [(s,function( ((x,), parallel(gt(x, 3), lt(x, 5))))), (gt,function( ((4, 3),char('4')))), (lt,function( ((4, 5),char('4'))))] eq_(eval(letr(ruleList, parse_text(s(x), '4'), x)), 4) assert_raises(NoSolutionFound, eval, letr(ruleList, parse_text(s(x), '6'), x)) assert_raises(NoSolutionFound, eval, letr(ruleList, parse_text(and_p(s(x), eoi), '41'), x)) assert_raises(NoSolutionFound, eval, letr(ruleList, parse_text(s(x), ''), x))