コード例 #1
0
 def test_apply_substitution_1(self):
     expression = Function.build('p(x, A)').get_child()
     substitutions = [
         Substitution(Variable.build('y'), Variable.build('x'))
     ]
     expected = [Variable.build('y'), Constant.build('A')]
     self.assertEqual(
         expected,
         MostGeneralUnifier.apply_substitution(expression, substitutions))
コード例 #2
0
    def test_composition_of_substitution_4(self):
        first_substitution = [
            Substitution(Function.build('f(x)'), Variable.build('u'))
        ]

        second_substitution = [
            Substitution(Function.build('k(f(x))'), Variable.build('y'))
        ]

        third_substitution = [
            Substitution(Function.build('k(f(x))'), Variable.build('z'))
        ]

        fourth_substitution = [
            Substitution(Function.build('h(w)'), Variable.build('x'))
        ]

        expected = [
            Substitution(Function.build('f(h(w))'), Variable.build('u')),
            Substitution(Function.build('k(f(h(w)))'), Variable.build('y')),
            Substitution(Function.build('k(f(h(w)))'), Variable.build('z')),
            Substitution(Function.build('h(w)'), Variable.build('x'))
        ]

        output = []
        output = MostGeneralUnifier.apply_composition_to_substitution(
            output, first_substitution)
        output = MostGeneralUnifier.apply_composition_to_substitution(
            output, second_substitution)
        output = MostGeneralUnifier.apply_composition_to_substitution(
            output, third_substitution)
        output = MostGeneralUnifier.apply_composition_to_substitution(
            output, fourth_substitution)
        self.assertEqual(expected, output)
コード例 #3
0
    def test_unification_1(self):
        expression1 = Function.build('p(f(x), y, g(y ,x))')
        expression2 = Function.build('p(u, k(u), g(z, h(w)))')

        _, unification_substitution = MostGeneralUnifier.unify(
            expression1, expression2)
        expected_substitutions = [
            Substitution(Function.build('f(h(w))'), Variable.build('u')),
            Substitution(Function.build('k(f(h(w)))'), Variable.build('y')),
            Substitution(Function.build('k(f(h(w)))'), Variable.build('z')),
            Substitution(Function.build('h(w)'), Variable.build('x'))
        ]

        self.assertEqual(expected_substitutions, unification_substitution)
コード例 #4
0
    def test_composition_of_substitution_1(self):
        empty_substitution = []
        first_substitution = [
            Substitution(Function.build('f(y)'), Variable.build('x')),
            Substitution(Variable.build('z'), Variable.build('y'))
        ]

        self.assertEqual(
            first_substitution,
            MostGeneralUnifier.apply_composition_to_substitution(
                empty_substitution, first_substitution))
        self.assertEqual(
            first_substitution,
            MostGeneralUnifier.apply_composition_to_substitution(
                first_substitution, empty_substitution))
コード例 #5
0
 def test_apply_substitution_2(self):
     expression1 = Function.build('p(f(x), y, g(y ,x))').get_child()
     expression2 = Function.build('p(u, k(u), g(z, h(w)))').get_child()
     substitutions = [
         Substitution(Function.build('f(h(w))'), Variable.build('u')),
         Substitution(Function.build('k(f(h(w)))'), Variable.build('y')),
         Substitution(Function.build('k(f(h(w)))'), Variable.build('z')),
         Substitution(Function.build('h(w)'), Variable.build('x'))
     ]
     expected = Function.build(
         'p(f(h(w)), k(f(h(w))), g(k(f(h(w))), h(w)))').get_child()
     self.assertEqual(
         expected,
         MostGeneralUnifier.apply_substitution(expression1, substitutions))
     self.assertEqual(
         expected,
         MostGeneralUnifier.apply_substitution(expression2, substitutions))
コード例 #6
0
    def test_unification_4(self):
        expression1 = Function.build('p(f(x), Y, g(y ,x))').get_child()
        expression2 = Variable.build('abc')

        result, unification_substitution = MostGeneralUnifier.unify(
            expression1, expression2)
        self.assertFalse(result)
        self.assertIsNone(unification_substitution)
コード例 #7
0
    def test_composition_of_substitution_3(self):
        first_substitution = [
            Substitution(Function.build('g(x, y)'), Variable.build('z'))
        ]

        second_substitution = [
            Substitution(Constant.build('A'), Variable.build('x')),
            Substitution(Constant.build('B'), Variable.build('y')),
            Substitution(Variable.build('w'), Variable.build('c')),
            Substitution(Constant.build('D'), Variable.build('z'))
        ]

        expected = [
            Substitution(Function.build('g(A, B)'), Variable.build('z')),
            Substitution(Constant.build('A'), Variable.build('x')),
            Substitution(Constant.build('B'), Variable.build('y')),
            Substitution(Variable.build('w'), Variable.build('c'))
        ]

        output = MostGeneralUnifier.apply_composition_to_substitution(
            first_substitution, second_substitution)
        self.assertEqual(expected, output)
コード例 #8
0
    def test_composition_of_substitution_2(self):
        first_substitution = [
            Substitution(Function.build('f(y)'), Variable.build('x')),
            Substitution(Variable.build('z'), Variable.build('y'))
        ]

        second_substitution = [
            Substitution(Variable.build('a'), Variable.build('x')),
            Substitution(Variable.build('b'), Variable.build('y')),
            Substitution(Variable.build('y'), Variable.build('z'))
        ]

        expected = [
            Substitution(Function.build('f(b)'), Variable.build('x')),
            Substitution(Variable.build('y'), Variable.build('z'))
        ]

        output = MostGeneralUnifier.apply_composition_to_substitution(
            first_substitution, second_substitution)
        self.assertEqual(expected, output)