コード例 #1
0
    def test_unify_pair(self):
        x = V.make_variable('x')
        y = V.make_variable('y')

        w = []

        def do():
            x.pair(y).go(C.make_const(1).make_const(1), lambda: w.append(None))

        x.go(y, do)
        self.assertEqual(w, [None])
コード例 #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_unify_two_vars(self):
        w = 0
        x = V.make_variable("x")
        y = V.make_variable("y")

        def check():
            nonlocal w
            w += 1
            assert x.value() == 2
            assert y.value() == 1

        x.make_const(1).go(C.make_const(2).pair(y), check)
        self.assertEqual(w, 1)
コード例 #4
0
    def test_two_answers(self):
        x = V.make_variable("x")
        y = V.make_variable("y")

        p = Prolog()
        p.fact(x)
        p.fact(y)

        w = []

        p.go(C.make_const(1), lambda: w.append(None))

        self.assertEqual(w, [None, None])
コード例 #5
0
    def test_unify_repeated(self):
        w = 0
        x = V.make_variable('x')
        y = V.make_variable('y')

        def check():
            nonlocal w
            w += 1
            self.assertEqual(x.value(), 1)
            self.assertEqual(y.value(), 1)

        x.make_const(1).go(y.pair(y), check)
        self.assertEqual(w, 1)
コード例 #6
0
    def test_head_body_conjunction_warmup_warmup(self):
        x = V.make_variable("x")
        y = V.make_variable("y")
        p = Prolog()
        p.fact(C.make_const(1).make_const('a'))
        p.fact(C.make_const(2).make_const('b'))
        w = 0

        def check():
            nonlocal w
            w += 1
            self.assertEqual(y.value(), 2)

        p.go(y.make_const('b'), check)
        self.assertEqual(w, 1)
コード例 #7
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])
コード例 #8
0
    def test_head_body_conjunction_warmup_warmup_y_unassigned(self):
        x = V.make_variable("x")
        y = V.make_variable("y")
        p = Prolog()
        p.fact(C.make_const(1).make_const('a'))
        p.fact(C.make_const(2).make_const('b'))
        w = 0

        def check():
            nonlocal w
            w += 1
            self.assertEqual(x.value(), 1)
            with self.assertRaises(Exception):
                y.value()

        p.go(x.make_const('a'), check)
        self.assertEqual(w, 1)
コード例 #9
0
 def test(self):
     x = V.make_variable('x')
     t = [1, 2, 3]
     p = Prolog()
     for e in t:
         p.fact(C.make_const(e))
     w = []
     p.go(x, lambda: w.append(x.value()))
     self.assertEqual(w, t)
コード例 #10
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])
コード例 #11
0
    def test_unify_with_pair(self):
        w = 0
        x = V.make_variable('x')

        def check():
            nonlocal w
            w += 1
            self.assertEqual(x.value()[0], 1)
            self.assertEqual(x.cdr().value(), 2)

        C.make_const(1).make_const(2).go(x, check)
        self.assertEqual(w, 1)
コード例 #12
0
    def test_instantiation(self):
        x = V.make_variable("x")

        p = Prolog()
        p.fact(x)
        w = 0

        def do():
            nonlocal w
            w += 1

        p.go(C.make_const(1) & C.make_const(2), do)
        self.assertEqual(w, 1)
コード例 #13
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])
コード例 #14
0
    def test_unify_nested(self):
        w = 0
        x = V.make_variable('x')

        def check1():
            nonlocal w
            w = w + 1
            x.go(C.make_const('b'), lambda: self.assertFalse(True))

        x.go(C.make_const('a'), check1)

        def check2():
            nonlocal w
            w = w + 1

        x.go(C.make_const('b'), check2)
        self.assertEqual(w, 2)
コード例 #15
0
    def test_conjunction(self):
        x = V.make_variable('x')
        p = Prolog()
        p.fact(C.make_const(1).make_const("single"))
        p.fact(C.make_const(2).make_const("single"))
        p.head_body(x.pair(x).make_const("double"), x.make_const("single"))

        w = []

        def check():
            w.append(x.value())
            if check.counter >= 10:
                assert False
            check.counter += 1

        check.counter = 0

        p.go(x.pair(x).make_const("double"), check)
        self.assertEqual(w, [1, 2])
コード例 #16
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'], []])
コード例 #17
0
 def test_occur_check(self):
     x = V.make_variable('x')
     x.go(L.pair(x), lambda: self.assertTrue(False))