コード例 #1
0
    def test_member_hard(self):
        x = V.make_variable("x")
        y = V.make_variable("y")
        z = V.make_variable("z")

        p = Prolog()
        p.fact(x.pair(y.pair(x)).make_const("member"))
        p.head_body(
            x.pair(y.pair(z)).make_const("member"),
            x.pair(y).make_const("member"))

        w = []
        m = L.make_const(1).make_const(2).make_const(3).make_const(4)
        n = L.make_const(0).make_const(2).make_const(4).make_const(6)
        p.go(
            x.pair(m).make_const("member") & x.pair(n).make_const("member"),
            lambda: w.append(x.value()))
        self.assertEqual(w, [4, 2])
コード例 #2
0
    def test_unification(self):
        x = V.make_variable("x")
        y = V.make_variable("y")

        pattern = x.pair(y.pair(x))
        value = C.make_const(3).pair(L.pair(C.make_const(2)))

        def check():
            assert False

        pattern.go(value, check)
コード例 #3
0
    def test_last(self):
        a = V.make_variable('a')
        b = V.make_variable('b')
        c = V.make_variable('c')

        x = V.make_variable('x')
        y = V.make_variable('y')
        z = V.make_variable('z')

        p = Prolog()
        p.fact(x.pair(y.pair(x)).make_const("member"))
        p.head_body(
            x.pair(y.pair(z)).make_const("member"),
            x.pair(y).make_const("member"))

        p.fact(L.pair(x).pair(x).make_const("append"))
        p.head_body(
            a.pair(x).pair(b).pair(c.pair(x)).make_const("append"),
            a.pair(b).pair(c).make_const("append"))

        def do():
            for q in [x, y]:
                s = []
                p.go(
                    a.pair(q).make_const("member"),
                    lambda: s.append(a.value()))
                w.append(s)

        w = []
        p.go(
            x.pair(y).pair(L.make_const('c').make_const('b').make_const(
                'a')).make_const('append'), do)

        self.assertEqual(w,
                         [[], ['a', 'b', 'c'], ['a'], ['b', 'c'], ['a', 'b'],
                          ['c'], ['a', 'b', 'c'], []])
コード例 #4
0
    def test_member(self):
        x = V.make_variable("x")
        y = V.make_variable("y")
        z = V.make_variable("z")

        p = Prolog()
        p.fact(x.pair(y.pair(x)).make_const("member"))
        p.head_body(
            x.pair(y.pair(z)).make_const("member"),
            x.pair(y).make_const("member"))

        w = []
        p.go(
            x.pair(L.make_const(1).make_const(2).make_const(3)).make_const(
                "member"), lambda: w.append(x.value()))
        self.assertEqual(w, [3, 2, 1])
コード例 #5
0
    def test(self):
        t = L.make_const(1).pair(V.make_variable(2).make_const(3))
        assert t.car().car().value() is None
        assert t.car().cdr().value() == 1
        self.assertIsNone(t.car().value()[0])
        self.assertEqual(t.car().value()[1], 1)
        self.assertEqual(t.cdr().cdr().value(), 3)

        a = V.make_variable(1)
        b = V.make_variable(2)
        c = V.make_variable(1)
        self.assertNotEqual(a, b)
        self.assertEqual(a, c)

        t = C.make_const(1).make_variable("z")
        self.assertEqual(t.car().value(), 1)
        with self.assertRaises(Exception):
            t.value()

        C.make_const(1).go(C.make_const(2), lambda: self.assertFalse(True))

        w = []
        C.make_const(1).go(C.make_const(1), lambda: w.append(1))
        self.assertEqual(w, [1])
コード例 #6
0
 def test_const_none(self):
     self.assertIsNone(L.value())
コード例 #7
0
 def test_var_pair(self):
     assert L.make_variable("blah").car().value() is None
コード例 #8
0
 def test_const_pair(self):
     assert L.make_const(3).cdr().value() == 3
     assert L.make_const(3).car().value() is None
コード例 #9
0
 def test_pair(self):
     assert L.pair(L).car().value() is None
     assert L.pair(L).cdr().value() is None
コード例 #10
0
 def test_occur_check(self):
     x = V.make_variable('x')
     x.go(L.pair(x), lambda: self.assertTrue(False))