addFunction() public méthode

public addFunction ( String function ) : void
function String
Résultat void
        public void testDefaultClauseSimplifier()
        {
            FOLDomain domain = new FOLDomain();
            domain.addConstant("ZERO");
            domain.addConstant("ONE");
            domain.addPredicate("P");
            domain.addFunction("Plus");
            domain.addFunction("Power");

            FOLParser parser = new FOLParser(domain);

            List<TermEquality> rewrites = new List<TermEquality>();
            rewrites.Add((TermEquality)parser.parse("Plus(x, ZERO) = x"));
            rewrites.Add((TermEquality)parser.parse("Plus(ZERO, x) = x"));
            rewrites.Add((TermEquality)parser.parse("Power(x, ONE) = x"));
            rewrites.Add((TermEquality)parser.parse("Power(x, ZERO) = ONE"));
            DefaultClauseSimplifier simplifier = new DefaultClauseSimplifier(
                    rewrites);

            Sentence s1 = parser
                    .parse("((P(Plus(y,ZERO),Plus(ZERO,y)) OR P(Power(y, ONE),Power(y,ZERO))) OR P(Power(y,ZERO),Plus(y,ZERO)))");

            CNFConverter cnfConverter = new CNFConverter(parser);

            CNF cnf = cnfConverter.convertToCNF(s1);

            Assert.AreEqual(1, cnf.getNumberOfClauses());

            Clause simplified = simplifier.simplify(cnf.getConjunctionOfClauses()
                    [0]);

            Assert.AreEqual("[P(y,y), P(y,ONE), P(ONE,y)]", simplified
                    .ToString());
        }
Exemple #2
0
 public void setUp()
 {
     FOLDomain domain = new FOLDomain();
     domain.addConstant("P");
     domain.addConstant("John");
     domain.addConstant("Saladin");
     domain.addFunction("LeftLeg");
     domain.addFunction("BrotherOf");
     domain.addFunction("EnemyOf");
     domain.addPredicate("HasColor");
     domain.addPredicate("King");
     lexer = new FOLLexer(domain);
 }
Exemple #3
0
        public static FOLDomain crusadesDomain()
        {
            FOLDomain domain = new FOLDomain();
            domain.addConstant("John");
            domain.addConstant("Richard");
            domain.addConstant("England");
            domain.addConstant("Saladin");
            domain.addConstant("Crown");

            domain.addFunction("LeftLegOf");
            domain.addFunction("BrotherOf");
            domain.addFunction("EnemyOf");
            domain.addFunction("LegsOf");

            domain.addPredicate("King");
            return domain;
        }
Exemple #4
0
        public static FOLDomain crusadesDomain()
        {
            FOLDomain domain = new FOLDomain();

            domain.addConstant("John");
            domain.addConstant("Richard");
            domain.addConstant("England");
            domain.addConstant("Saladin");
            domain.addConstant("Crown");

            domain.addFunction("LeftLegOf");
            domain.addFunction("BrotherOf");
            domain.addFunction("EnemyOf");
            domain.addFunction("LegsOf");

            domain.addPredicate("King");
            return(domain);
        }
Exemple #5
0
 public static FOLDomain knowsDomain()
 {
     FOLDomain domain = new FOLDomain();
     domain.addConstant("John");
     domain.addConstant("Jane");
     domain.addConstant("Bill");
     domain.addConstant("Elizabeth");
     domain.addFunction("Mother");
     domain.addPredicate("Knows");
     return domain;
 }
        public void testSimpleAtomicExamples()
        {
            FOLDomain domain = new FOLDomain();
            domain.addConstant("A");
            domain.addConstant("B");
            domain.addConstant("C");
            domain.addConstant("D");
            domain.addConstant("E");
            domain.addPredicate("P");
            domain.addFunction("F");
            domain.addFunction("G");
            domain.addFunction("H");
            domain.addFunction("J");

            FOLParser parser = new FOLParser(domain);

            Predicate expression = (Predicate)parser
                    .parse("P(A,F(B,G(A,H(B)),C),D)");
            TermEquality assertion = (TermEquality)parser.parse("B = E");

            Predicate altExpression = (Predicate)demodulation.apply(assertion,
                    expression);

            Assert.IsFalse(expression.Equals(altExpression));
            Assert
                    .AreEqual("P(A,F(E,G(A,H(B)),C),D)", altExpression
                            .ToString());

            altExpression = (Predicate)demodulation
                    .apply(assertion, altExpression);

            Assert
                    .AreEqual("P(A,F(E,G(A,H(E)),C),D)", altExpression
                            .ToString());

            assertion = (TermEquality)parser.parse("G(x,y) = J(x)");

            altExpression = (Predicate)demodulation.apply(assertion, expression);

            Assert.AreEqual("P(A,F(B,J(A),C),D)", altExpression.ToString());
        }
Exemple #7
0
        public static FOLDomain knowsDomain()
        {
            FOLDomain domain = new FOLDomain();

            domain.addConstant("John");
            domain.addConstant("Jane");
            domain.addConstant("Bill");
            domain.addConstant("Elizabeth");
            domain.addFunction("Mother");
            domain.addPredicate("Knows");
            return(domain);
        }
        public void testSimpleExample()
        {
            FOLDomain domain = new FOLDomain();
            domain.addConstant("A");
            domain.addConstant("B");
            domain.addPredicate("P");
            domain.addPredicate("Q");
            domain.addPredicate("R");
            domain.addFunction("F");

            FOLParser parser = new FOLParser(domain);

            List<Literal> lits = new List<Literal>();
            AtomicSentence a1 = (AtomicSentence)parser.parse("P(F(x,B),x)");
            AtomicSentence a2 = (AtomicSentence)parser.parse("Q(x)");
            lits.Add(new Literal(a1));
            lits.Add(new Literal(a2));

            Clause c1 = new Clause(lits);

            lits.Clear();
            a1 = (AtomicSentence)parser.parse("F(A,y) = y");
            a2 = (AtomicSentence)parser.parse("R(y)");
            lits.Add(new Literal(a1));
            lits.Add(new Literal(a2));

            Clause c2 = new Clause(lits);

            List<Clause> paras = paramodulation.apply(c1, c2);
            Assert.AreEqual(2, paras.Count);

            foreach (Clause c in paras)
            {
                Assert.AreEqual("[P(B,A), Q(A), R(B)]", c.ToString());
                Assert.AreEqual("[P(F(A,F(x,B)),x), Q(x), R(F(x,B))]", c
                        .ToString());
            }
        }
        public void testMultipleTermEqualitiesInBothClausesExample()
        {
            FOLDomain domain = new FOLDomain();
            domain.addConstant("A");
            domain.addConstant("B");
            domain.addConstant("C");
            domain.addConstant("D");
            domain.addPredicate("P");
            domain.addPredicate("Q");
            domain.addPredicate("R");
            domain.addFunction("F");

            FOLParser parser = new FOLParser(domain);

            List<Literal> lits = new List<Literal>();
            AtomicSentence a1 = (AtomicSentence)parser.parse("F(C,x) = D");
            AtomicSentence a2 = (AtomicSentence)parser.parse("A = D");
            AtomicSentence a3 = (AtomicSentence)parser.parse("P(F(x,B),x)");
            AtomicSentence a4 = (AtomicSentence)parser.parse("Q(x)");
            AtomicSentence a5 = (AtomicSentence)parser.parse("R(C)");
            lits.Add(new Literal(a1));
            lits.Add(new Literal(a2));
            lits.Add(new Literal(a3));
            lits.Add(new Literal(a4));
            lits.Add(new Literal(a5));

            Clause c1 = new Clause(lits);

            lits.Clear();
            a1 = (AtomicSentence)parser.parse("F(A,y) = y");
            a2 = (AtomicSentence)parser.parse("F(B,y) = C");
            a3 = (AtomicSentence)parser.parse("R(y)");
            a4 = (AtomicSentence)parser.parse("R(A)");
            lits.Add(new Literal(a1));
            lits.Add(new Literal(a2));
            lits.Add(new Literal(a3));
            lits.Add(new Literal(a4));

            Clause c2 = new Clause(lits);

            List<Clause> paras = paramodulation.apply(c1, c2);
            Assert.AreEqual(5, paras.Count);

            List<Clause>.Enumerator it = paras.GetEnumerator();
            it.MoveNext();
            Assert
                    .AreEqual(
                            "[F(B,B) = C, F(C,A) = D, A = D, P(B,A), Q(A), R(A), R(B), R(C)]",
                            it.Current.ToString());
            it.MoveNext();
            Assert
                    .AreEqual(
                            "[F(A,F(C,x)) = D, F(B,F(C,x)) = C, A = D, P(F(x,B),x), Q(x), R(F(C,x)), R(A), R(C)]",
                            it.Current.ToString());
            it.MoveNext();
            Assert
                    .AreEqual(
                            "[F(A,B) = B, F(C,B) = D, A = D, P(C,B), Q(B), R(A), R(B), R(C)]",
                            it.Current.ToString());
            it.MoveNext();
            Assert
                    .AreEqual(
                            "[F(F(B,y),x) = D, F(A,y) = y, A = D, P(F(x,B),x), Q(x), R(y), R(A), R(C)]",
                            it.Current.ToString());
            it.MoveNext();
            Assert
                    .AreEqual(
                            "[F(B,y) = C, F(C,x) = D, F(D,y) = y, P(F(x,B),x), Q(x), R(y), R(A), R(C)]",
                            it.Current.ToString());
        }
        public void testInductionAxiomSchema()
        {
            FOLDomain domain = new FOLDomain();
            domain.addPredicate("Equal");
            domain.addFunction("Plus");
            domain.addConstant("A");
            domain.addConstant("B");
            domain.addConstant("N");
            domain.addConstant("ONE");
            domain.addConstant("ZERO");

            FOLParser parser = new FOLParser(domain);
            CNFConverter cnfConv = new CNFConverter(parser);

            // Base Case:
            Sentence sent = parser
                    .parse("NOT(FORALL x (FORALL y (Equal(Plus(Plus(x,y),ZERO), Plus(x,Plus(y,ZERO))))))");
            CNF cnf = cnfConv.convertToCNF(sent);
            Assert.AreEqual(
                    "[~Equal(Plus(Plus(SC0,SC1),ZERO),Plus(SC0,Plus(SC1,ZERO)))]",
                    cnf.ToString());

            // Instance of Induction Axion Scmema
            sent = parser
                    .parse("(("
                            + "Equal(Plus(Plus(A,B),ZERO), Plus(A,Plus(B,ZERO)))"
                            + " AND "
                            + "(FORALL x (FORALL y (FORALL z("
                            + "Equal(Plus(Plus(x,y),z), Plus(x,Plus(y,z)))"
                            + " => "
                            + "Equal(Plus(Plus(x,y),Plus(z,ONE)), Plus(x,Plus(y,Plus(z,ONE))))"
                            + "))))" + ")" + " => "
                            + "FORALL x (FORALL y (FORALL z("
                            + "Equal(Plus(Plus(x,y),z), Plus(x,Plus(y,z)))"
                            + "))))");
            cnf = cnfConv.convertToCNF(sent);
            Assert
                    .AreEqual(
                            "[~Equal(Plus(Plus(A,B),ZERO),Plus(A,Plus(B,ZERO))), Equal(Plus(Plus(q0,q1),q2),Plus(q0,Plus(q1,q2))), Equal(Plus(Plus(SC2,SC3),SC4),Plus(SC2,Plus(SC3,SC4)))],[~Equal(Plus(Plus(A,B),ZERO),Plus(A,Plus(B,ZERO))), ~Equal(Plus(Plus(SC2,SC3),Plus(SC4,ONE)),Plus(SC2,Plus(SC3,Plus(SC4,ONE)))), Equal(Plus(Plus(q0,q1),q2),Plus(q0,Plus(q1,q2)))]",
                            cnf.ToString());

            // Goal
            sent = parser
                    .parse("NOT(FORALL x (FORALL y (FORALL z (Equal(Plus(Plus(x,y),z), Plus(x,Plus(y,z)))))))");
            cnf = cnfConv.convertToCNF(sent);
            Assert.AreEqual(
                    "[~Equal(Plus(Plus(SC5,SC6),SC7),Plus(SC5,Plus(SC6,SC7)))]",
                    cnf.ToString());
        }
        public void testSimpleAtomicNonExample()
        {
            FOLDomain domain = new FOLDomain();
            domain.addConstant("A");
            domain.addConstant("B");
            domain.addConstant("C");
            domain.addConstant("D");
            domain.addConstant("E");
            domain.addPredicate("P");
            domain.addFunction("F");
            domain.addFunction("G");
            domain.addFunction("H");
            domain.addFunction("J");

            FOLParser parser = new FOLParser(domain);

            Predicate expression = (Predicate)parser.parse("P(A,G(x,B),C)");
            TermEquality assertion = (TermEquality)parser.parse("G(A,y) = J(y)");

            Predicate altExpression = (Predicate)demodulation.apply(assertion,
                    expression);

            Assert.IsNull(altExpression);
        }
        public void testSimpleClauseExamples()
        {
            FOLDomain domain = new FOLDomain();
            domain.addConstant("A");
            domain.addConstant("B");
            domain.addConstant("C");
            domain.addConstant("D");
            domain.addConstant("E");
            domain.addPredicate("P");
            domain.addPredicate("Q");
            domain.addPredicate("W");
            domain.addFunction("F");
            domain.addFunction("G");
            domain.addFunction("H");
            domain.addFunction("J");

            FOLParser parser = new FOLParser(domain);

            List<Literal> lits = new List<Literal>();
            Predicate p1 = (Predicate)parser.parse("Q(z, G(D,B))");
            Predicate p2 = (Predicate)parser.parse("P(x, G(A,C))");
            Predicate p3 = (Predicate)parser.parse("W(z,x,u,w,y)");
            lits.Add(new Literal(p1));
            lits.Add(new Literal(p2));
            lits.Add(new Literal(p3));

            Clause clExpression = new Clause(lits);

            TermEquality assertion = (TermEquality)parser.parse("G(x,y) = x");

            Clause altClExpression = demodulation.apply(assertion, clExpression);

            Assert.AreEqual("[P(x,G(A,C)), Q(z,D), W(z,x,u,w,y)]",
                    altClExpression.ToString());

            altClExpression = demodulation.apply(assertion, altClExpression);

            Assert.AreEqual("[P(x,A), Q(z,D), W(z,x,u,w,y)]", altClExpression
                    .ToString());
        }
        public void testBypassReflexivityAxiom()
        {
            FOLDomain domain = new FOLDomain();
            domain.addConstant("A");
            domain.addConstant("B");
            domain.addConstant("C");
            domain.addPredicate("P");
            domain.addFunction("F");

            FOLParser parser = new FOLParser(domain);

            List<Literal> lits = new List<Literal>();
            Predicate p1 = (Predicate)parser.parse("P(y, F(A,y))");
            lits.Add(new Literal(p1));

            Clause clExpression = new Clause(lits);

            TermEquality assertion = (TermEquality)parser.parse("x = x");

            Clause altClExpression = demodulation.apply(assertion, clExpression);

            Assert.IsNull(altClExpression);
        }
Exemple #14
0
        public void testNOTSentence()
        {
            FOLDomain domain = new FOLDomain();
            domain.addConstant("A");
            domain.addConstant("B");
            domain.addConstant("C");
            domain.addFunction("Plus");
            domain.addPredicate("P");

            FOLParser parser = new FOLParser(domain);

            Sentence s1 = parser.parse("NOT(P(A))");
            Sentence s2 = parser.parse("NOT(P(A))");

            Dictionary<Variable, Term> result = unifier.unify(s1, s2);

            Assert.IsNotNull(result);
            Assert.AreEqual(0, result.Count);

            s1 = parser.parse("NOT(P(A))");
            s2 = parser.parse("NOT(P(B))");

            result = unifier.unify(s1, s2);

            Assert.IsNull(result);

            s1 = parser.parse("NOT(P(A))");
            s2 = parser.parse("NOT(P(x))");

            result = unifier.unify(s1, s2);

            Assert.IsNotNull(result);
            Assert.AreEqual(1, result.Count);
            Assert.AreEqual(new Constant("A"), result[new Variable("x")]);
        }
Exemple #15
0
        public void testQuantifiedSentence()
        {
            FOLDomain domain = new FOLDomain();
            domain.addConstant("A");
            domain.addConstant("B");
            domain.addConstant("C");
            domain.addFunction("Plus");
            domain.addPredicate("P");

            FOLParser parser = new FOLParser(domain);

            Sentence s1 = parser
                    .parse("FORALL x,y ((P(x) AND P(A)) OR (P(A) => P(y)))");
            Sentence s2 = parser
                    .parse("FORALL x,y ((P(x) AND P(A)) OR (P(A) => P(y)))");

            Dictionary<Variable, Term> result = unifier.unify(s1, s2);

            Assert.IsNotNull(result);
            Assert.AreEqual(0, result.Count);

            s1 = parser.parse("FORALL x,y ((P(x) AND P(A)) OR (P(A) => P(y)))");
            s2 = parser.parse("FORALL x   ((P(x) AND P(A)) OR (P(A) => P(y)))");

            result = unifier.unify(s1, s2);

            Assert.IsNull(result);

            s1 = parser.parse("FORALL x,y ((P(x) AND P(A)) OR (P(A) => P(y)))");
            s2 = parser.parse("FORALL x,y ((P(x) AND P(A)) OR (P(B) => P(y)))");

            result = unifier.unify(s1, s2);

            Assert.IsNull(result);

            s1 = parser.parse("FORALL x,y ((P(x) AND P(A)) OR (P(A) => P(y)))");
            s2 = parser.parse("FORALL x,y ((P(A) AND P(A)) OR (P(A) => P(y)))");

            result = unifier.unify(s1, s2);

            Assert.IsNotNull(result);
            Assert.AreEqual(1, result.Count);
            Assert.AreEqual(new Constant("A"), result[new Variable("x")]);

            //
            s1 = parser.parse("EXISTS x,y ((P(x) AND P(A)) OR (P(A) => P(y)))");
            s2 = parser.parse("EXISTS x,y ((P(x) AND P(A)) OR (P(A) => P(y)))");

            result = unifier.unify(s1, s2);

            Assert.IsNotNull(result);
            Assert.AreEqual(0, result.Count);

            s1 = parser.parse("EXISTS x,y ((P(x) AND P(A)) OR (P(A) => P(y)))");
            s2 = parser.parse("EXISTS x   ((P(x) AND P(A)) OR (P(A) => P(y)))");

            result = unifier.unify(s1, s2);

            Assert.IsNull(result);

            s1 = parser.parse("EXISTS x,y ((P(x) AND P(A)) OR (P(A) => P(y)))");
            s2 = parser.parse("EXISTS x,y ((P(x) AND P(A)) OR (P(B) => P(y)))");

            result = unifier.unify(s1, s2);

            Assert.IsNull(result);

            s1 = parser.parse("EXISTS x,y ((P(x) AND P(A)) OR (P(A) => P(y)))");
            s2 = parser.parse("EXISTS x,y ((P(A) AND P(A)) OR (P(A) => P(y)))");

            result = unifier.unify(s1, s2);

            Assert.IsNotNull(result);
            Assert.AreEqual(1, result.Count);
            Assert.AreEqual(new Constant("A"), result[new Variable("x")]);
        }
Exemple #16
0
        public void testAdditionalVariableMixtures()
        {
            FOLDomain domain = new FOLDomain();
            domain.addConstant("A");
            domain.addConstant("B");
            domain.addFunction("F");
            domain.addFunction("G");
            domain.addFunction("H");
            domain.addPredicate("P");

            FOLParser parser = new FOLParser(domain);

            // Test Cascade Substitutions handled correctly
            Sentence s1 = parser.parse("P(z, x)");
            Sentence s2 = parser.parse("P(x, a)");
            Dictionary<Variable, Term> result = unifier.unify(s1, s2);

            Assert.AreEqual("{z=a, x=a}", result.ToString());

            s1 = parser.parse("P(x, z)");
            s2 = parser.parse("P(a, x)");
            result = unifier.unify(s1, s2);

            Assert.AreEqual("{x=a, z=a}", result.ToString());

            s1 = parser.parse("P(w, w, w)");
            s2 = parser.parse("P(x, y, z)");
            result = unifier.unify(s1, s2);

            Assert.AreEqual("{w=z, x=z, y=z}", result.ToString());

            s1 = parser.parse("P(x, y, z)");
            s2 = parser.parse("P(w, w, w)");
            result = unifier.unify(s1, s2);

            Assert.AreEqual("{x=w, y=w, z=w}", result.ToString());

            s1 = parser.parse("P(x, B, F(y))");
            s2 = parser.parse("P(A, y, F(z))");
            result = unifier.unify(s1, s2);

            Assert.AreEqual("{x=A, y=B, z=B}", result.ToString());

            s1 = parser.parse("P(F(x,B), G(y),         F(z,A))");
            s2 = parser.parse("P(y,      G(F(G(w),w)), F(w,z))");
            result = unifier.unify(s1, s2);

            Assert.IsNull(result);

            s1 = parser.parse("P(F(G(A)), x,    F(H(z,z)), H(y,    G(w)))");
            s2 = parser.parse("P(y,       G(z), F(v     ), H(F(w), x   ))");
            result = unifier.unify(s1, s2);

            Assert.AreEqual(
                    "{y=F(G(A)), x=G(G(A)), v=H(G(A),G(A)), w=G(A), z=G(A)}",
                    result.ToString());
        }
Exemple #17
0
        public void testTermEquality()
        {
            FOLDomain domain = new FOLDomain();
            domain.addConstant("A");
            domain.addConstant("B");
            domain.addFunction("Plus");

            FOLParser parser = new FOLParser(domain);

            TermEquality te1 = (TermEquality)parser.parse("x = x");
            TermEquality te2 = (TermEquality)parser.parse("x = x");

            // Both term equalities the same,
            // should unify but no substitutions.
            Dictionary<Variable, Term> result = unifier.unify(te1, te2);

            Assert.IsNotNull(result);
            Assert.AreEqual(0, result.Count);

            // Different variable names but should unify.
            te1 = (TermEquality)parser.parse("x1 = x1");
            te2 = (TermEquality)parser.parse("x2 = x2");

            result = unifier.unify(te1, te2);

            Assert.IsNotNull(result);
            Assert.AreEqual(1, result.Count);
            Assert.AreEqual("{x1=x2}", result.ToString());

            // Test simple unification with reflexivity axiom
            te1 = (TermEquality)parser.parse("x1 = x1");
            te2 = (TermEquality)parser.parse("Plus(A,B) = Plus(A,B)");

            result = unifier.unify(te1, te2);

            Assert.IsNotNull(result);

            Assert.AreEqual(1, result.Count);
            Assert.AreEqual("{x1=Plus(A,B)}", result.ToString());

            // Test more complex unification with reflexivity axiom
            te1 = (TermEquality)parser.parse("x1 = x1");
            te2 = (TermEquality)parser.parse("Plus(A,B) = Plus(A,z1)");

            result = unifier.unify(te1, te2);

            Assert.IsNotNull(result);

            Assert.AreEqual(2, result.Count);
            Assert.AreEqual("{x1=Plus(A,B), z1=B}", result.ToString());

            // Test reverse of previous unification with reflexivity axiom
            // Should still be the same.
            te1 = (TermEquality)parser.parse("x1 = x1");
            te2 = (TermEquality)parser.parse("Plus(A,z1) = Plus(A,B)");

            result = unifier.unify(te1, te2);

            Assert.IsNotNull(result);

            Assert.AreEqual(2, result.Count);
            Assert.AreEqual("{x1=Plus(A,B), z1=B}", result.ToString());

            // Test with nested terms
            te1 = (TermEquality)parser
                    .parse("Plus(Plus(Plus(A,B),B, A)) = Plus(Plus(Plus(A,B),B, A))");
            te2 = (TermEquality)parser
                    .parse("Plus(Plus(Plus(A,B),B, A)) = Plus(Plus(Plus(A,B),B, A))");

            result = unifier.unify(te1, te2);

            Assert.IsNotNull(result);

            Assert.AreEqual(0, result.Count);

            // Simple term equality unification fails
            te1 = (TermEquality)parser.parse("Plus(A,B) = Plus(B,A)");
            te2 = (TermEquality)parser.parse("Plus(A,B) = Plus(A,B)");

            result = unifier.unify(te1, te2);

            Assert.IsNull(result);
        }
        public void testTermEquality()
        {
            FOLDomain domain = new FOLDomain();
            domain.addPredicate("P");
            domain.addPredicate("Q");
            domain.addPredicate("R");
            domain.addConstant("A");
            domain.addConstant("B");
            domain.addConstant("C");
            domain.addConstant("D");
            domain.addFunction("Plus");
            domain.addConstant("ONE");
            domain.addConstant("ZERO");

            FOLParser parser = new FOLParser(domain);
            CNFConverter cnfConv = new CNFConverter(parser);

            // x=y
            Sentence sent = parser.parse("x = y");
            CNF cnf = cnfConv.convertToCNF(sent);

            Assert.AreEqual("[x = y]", cnf.ToString());

            // x!=y
            sent = parser.parse("NOT(x = y)");
            cnf = cnfConv.convertToCNF(sent);

            Assert.AreEqual("[~x = y]", cnf.ToString());

            // A=B
            sent = parser.parse("A = B");
            cnf = cnfConv.convertToCNF(sent);

            Assert.AreEqual("[A = B]", cnf.ToString());

            // A!=B
            sent = parser.parse("NOT(A = B)");
            cnf = cnfConv.convertToCNF(sent);

            Assert.AreEqual("[~A = B]", cnf.ToString());

            // ~(((~A=B or ~D=C) => ~(A=B or D=C)) => A=D)
            sent = parser
                    .parse("NOT(((((NOT(A = B) OR NOT(D = C))) => NOT((A = B OR D = C))) => A = D))");
            cnf = cnfConv.convertToCNF(sent);

            Assert
                    .AreEqual(
                            "[~A = B, A = B],[~A = B, D = C],[~D = C, A = B],[~D = C, D = C],[~A = D]",
                            cnf.ToString());

            //
            // Induction Axiom Schema using Term Equality

            // Base Case:
            sent = parser
                    .parse("NOT(FORALL x (FORALL y (Plus(Plus(x,y),ZERO) = Plus(x,Plus(y,ZERO)))))");
            cnf = cnfConv.convertToCNF(sent);
            Assert.AreEqual(
                    "[~Plus(Plus(SC0,SC1),ZERO) = Plus(SC0,Plus(SC1,ZERO))]", cnf
                            .ToString());

            // Instance of Induction Axion Scmema
            sent = parser.parse("(("
                    + "Plus(Plus(A,B),ZERO) = Plus(A,Plus(B,ZERO))" + " AND "
                    + "(FORALL x (FORALL y (FORALL z("
                    + "Plus(Plus(x,y),z) = Plus(x,Plus(y,z))" + " => "
                    + "Plus(Plus(x,y),Plus(z,ONE)) = Plus(x,Plus(y,Plus(z,ONE)))"
                    + "))))" + ")" + " => " + "FORALL x (FORALL y (FORALL z("
                    + "Plus(Plus(x,y),z) = Plus(x,Plus(y,z))" + "))))");
            cnf = cnfConv.convertToCNF(sent);
            Assert
                    .AreEqual(
                            "[~Plus(Plus(A,B),ZERO) = Plus(A,Plus(B,ZERO)), Plus(Plus(q0,q1),q2) = Plus(q0,Plus(q1,q2)), Plus(Plus(SC2,SC3),SC4) = Plus(SC2,Plus(SC3,SC4))],[~Plus(Plus(A,B),ZERO) = Plus(A,Plus(B,ZERO)), ~Plus(Plus(SC2,SC3),Plus(SC4,ONE)) = Plus(SC2,Plus(SC3,Plus(SC4,ONE))), Plus(Plus(q0,q1),q2) = Plus(q0,Plus(q1,q2))]",
                            cnf.ToString());

            // Goal
            sent = parser
                    .parse("NOT(FORALL x (FORALL y (FORALL z (Plus(Plus(x,y),z) = Plus(x,Plus(y,z))))))");
            cnf = cnfConv.convertToCNF(sent);
            Assert.AreEqual(
                    "[~Plus(Plus(SC5,SC6),SC7) = Plus(SC5,Plus(SC6,SC7))]", cnf
                            .ToString());
        }
        // Note: see -
        // http://logic.stanford.edu/classes/cs157/2008/lectures/lecture15.pdf
        // slide 16,17, and 18 for where this test example was taken from.
        public static FOLKnowledgeBase createABCDEqualityAndSubstitutionKnowledgeBase(
                InferenceProcedure infp, bool includeEqualityAxioms)
        {
            FOLDomain domain = new FOLDomain();
            domain.addConstant("A");
            domain.addConstant("B");
            domain.addConstant("C");
            domain.addConstant("D");
            domain.addPredicate("P");
            domain.addFunction("F");

            FOLKnowledgeBase kb = new FOLKnowledgeBase(domain, infp);

            kb.tell("F(A) = B");
            kb.tell("F(B) = A");
            kb.tell("C = D");
            kb.tell("P(A)");
            kb.tell("P(C)");

            if (includeEqualityAxioms)
            {
                // Reflexivity Axiom
                kb.tell("x = x");
                // Symmetry Axiom
                kb.tell("(x = y => y = x)");
                // Transitivity Axiom
                kb.tell("((x = y AND y = z) => x = z)");
                // Function F Substitution Axiom
                kb.tell("((x = y AND F(y) = z) => F(x) = z)");
                // Predicate P Substitution Axiom
                kb.tell("((x = y AND P(y)) => P(x))");
            }

            return kb;
        }
Exemple #20
0
        public void testComplexEquals()
        {
            FOLDomain domain = new FOLDomain();
            domain.addConstant("A");
            domain.addConstant("B");
            domain.addConstant("C");
            domain.addConstant("D");
            domain.addPredicate("P");
            domain.addPredicate("Animal");
            domain.addPredicate("Kills");
            domain.addFunction("F");
            domain.addFunction("SF0");

            FOLParser parser = new FOLParser(domain);

            CNFConverter cnfConverter = new CNFConverter(parser);
            Sentence s1 = parser.parse("((x1 = y1 AND y1 = z1) => x1 = z1)");
            Sentence s2 = parser.parse("((x2 = y2 AND F(y2) = z2) => F(x2) = z2)");
            CNF cnf1 = cnfConverter.convertToCNF(s1);
            CNF cnf2 = cnfConverter.convertToCNF(s2);

            Clause c1 = cnf1.getConjunctionOfClauses()[0];
            Clause c2 = cnf2.getConjunctionOfClauses()[0];

            Assert.IsFalse(c1.Equals(c2));

            s1 = parser.parse("((x1 = y1 AND y1 = z1) => x1 = z1)");
            s2 = parser.parse("((x2 = y2 AND y2 = z2) => x2 = z2)");
            cnf1 = cnfConverter.convertToCNF(s1);
            cnf2 = cnfConverter.convertToCNF(s2);

            c1 = cnf1.getConjunctionOfClauses()[0];
            c2 = cnf2.getConjunctionOfClauses()[0];

            Assert.IsTrue(c1.Equals(c2));

            s1 = parser.parse("((x1 = y1 AND y1 = z1) => x1 = z1)");
            s2 = parser.parse("((y2 = z2 AND x2 = y2) => x2 = z2)");
            cnf1 = cnfConverter.convertToCNF(s1);
            cnf2 = cnfConverter.convertToCNF(s2);

            c1 = cnf1.getConjunctionOfClauses()[0];
            c2 = cnf2.getConjunctionOfClauses()[0];

            Assert.IsTrue(c1.Equals(c2));

            s1 = parser.parse("(((x1 = y1 AND y1 = z1) AND z1 = r1) => x1 = r1)");
            s2 = parser.parse("(((x2 = y2 AND y2 = z2) AND z2 = r2) => x2 = r2)");
            cnf1 = cnfConverter.convertToCNF(s1);
            cnf2 = cnfConverter.convertToCNF(s2);

            c1 = cnf1.getConjunctionOfClauses()[0];
            c2 = cnf2.getConjunctionOfClauses()[0];

            Assert.IsTrue(c1.Equals(c2));

            s1 = parser.parse("(((x1 = y1 AND y1 = z1) AND z1 = r1) => x1 = r1)");
            s2 = parser.parse("(((z2 = r2 AND y2 = z2) AND x2 = y2) => x2 = r2)");
            cnf1 = cnfConverter.convertToCNF(s1);
            cnf2 = cnfConverter.convertToCNF(s2);

            c1 = cnf1.getConjunctionOfClauses()[0];
            c2 = cnf2.getConjunctionOfClauses()[0];

            Assert.IsTrue(c1.Equals(c2));

            s1 = parser.parse("(((x1 = y1 AND y1 = z1) AND z1 = r1) => x1 = r1)");
            s2 = parser.parse("(((x2 = y2 AND y2 = z2) AND z2 = y2) => x2 = r2)");
            cnf1 = cnfConverter.convertToCNF(s1);
            cnf2 = cnfConverter.convertToCNF(s2);

            c1 = cnf1.getConjunctionOfClauses()[0];
            c2 = cnf2.getConjunctionOfClauses()[0];

            Assert.IsFalse(c1.Equals(c2));

            s1 = parser
                    .parse("(((((x1 = y1 AND y1 = z1) AND z1 = r1) AND r1 = q1) AND q1 = s1) => x1 = r1)");
            s2 = parser
                    .parse("(((((x2 = y2 AND y2 = z2) AND z2 = r2) AND r2 = q2) AND q2 = s2) => x2 = r2)");
            cnf1 = cnfConverter.convertToCNF(s1);
            cnf2 = cnfConverter.convertToCNF(s2);

            c1 = cnf1.getConjunctionOfClauses()[0];
            c2 = cnf2.getConjunctionOfClauses()[0];

            Assert.IsTrue(c1.Equals(c2));

            s1 = parser
                    .parse("((((NOT(Animal(c1920)) OR NOT(Animal(c1921))) OR NOT(Kills(c1922,c1920))) OR NOT(Kills(c1919,c1921))) OR NOT(Kills(SF0(c1922),SF0(c1919))))");
            s2 = parser
                    .parse("((((NOT(Animal(c1929)) OR NOT(Animal(c1928))) OR NOT(Kills(c1927,c1929))) OR NOT(Kills(c1930,c1928))) OR NOT(Kills(SF0(c1930),SF0(c1927))))");
            cnf1 = cnfConverter.convertToCNF(s1);
            cnf2 = cnfConverter.convertToCNF(s2);

            c1 = cnf1.getConjunctionOfClauses()[0];
            c2 = cnf2.getConjunctionOfClauses()[0];

            Assert.IsTrue(c1.Equals(c2));
        }
Exemple #21
0
        public void testIsTautology()
        {
            FOLDomain domain = new FOLDomain();
            domain.addConstant("A");
            domain.addPredicate("P");
            domain.addPredicate("Q");
            domain.addPredicate("R");
            domain.addFunction("F");

            FOLParser parser = new FOLParser(domain);

            // {p(f(a)),~p(f(a))}
            Clause c = new Clause();
            c.addPositiveLiteral((Predicate)parser.parse("P(F(A))"));
            Assert.IsFalse(c.isTautology());
            c.addNegativeLiteral((Predicate)parser.parse("P(F(A))"));
            Assert.IsTrue(c.isTautology());

            // {p(x),q(y),~q(y),r(z)}
            c = new Clause();
            c.addPositiveLiteral((Predicate)parser.parse("P(x)"));
            Assert.IsFalse(c.isTautology());
            c.addPositiveLiteral((Predicate)parser.parse("Q(y)"));
            Assert.IsFalse(c.isTautology());
            c.addNegativeLiteral((Predicate)parser.parse("Q(y)"));
            Assert.IsTrue(c.isTautology());
            c.addPositiveLiteral((Predicate)parser.parse("R(z)"));
            Assert.IsTrue(c.isTautology());

            // {~p(a),p(x)}
            c = new Clause();
            c.addNegativeLiteral((Predicate)parser.parse("P(A)"));
            Assert.IsFalse(c.isTautology());
            c.addPositiveLiteral((Predicate)parser.parse("P(x)"));
            Assert.IsFalse(c.isTautology());
        }
Exemple #22
0
        public void testNonTrivialFactors()
        {
            FOLDomain domain = new FOLDomain();
            domain.addConstant("A");
            domain.addConstant("B");
            domain.addFunction("F");
            domain.addFunction("G");
            domain.addFunction("H");
            domain.addPredicate("P");
            domain.addPredicate("Q");

            FOLParser parser = new FOLParser(domain);

            // p(x,y), q(a,b), p(b,a), q(y,x)
            Clause c = new Clause();
            c.addPositiveLiteral((Predicate)parser.parse("P(x,y)"));
            c.addPositiveLiteral((Predicate)parser.parse("Q(A,B)"));
            c.addNegativeLiteral((Predicate)parser.parse("P(B,A)"));
            c.addPositiveLiteral((Predicate)parser.parse("Q(y,x)"));

            Assert.AreEqual("[[~P(B,A), P(B,A), Q(A,B)]]", c
                    .getNonTrivialFactors().ToString());

            // p(x,y), q(a,b), p(b,a), q(y,x)
            c = new Clause();
            c.addPositiveLiteral((Predicate)parser.parse("P(x,y)"));
            c.addPositiveLiteral((Predicate)parser.parse("Q(A,B)"));
            c.addNegativeLiteral((Predicate)parser.parse("P(B,A)"));
            c.addNegativeLiteral((Predicate)parser.parse("Q(y,x)"));

            Assert.AreEqual("[]", c.getNonTrivialFactors().ToString());

            // p(x,f(y)), p(g(u),x), p(f(y),u)
            c = new Clause();
            c.addPositiveLiteral((Predicate)parser.parse("P(x,F(y))"));
            c.addPositiveLiteral((Predicate)parser.parse("P(G(u),x)"));
            c.addPositiveLiteral((Predicate)parser.parse("P(F(y),u)"));

            // Should be: [{P(F(c#),F(c#)),P(G(F(c#)),F(c#))}]
            c = c.getNonTrivialFactors().First();
            Literal p = c.getPositiveLiterals()[0];
            Assert.AreEqual("P", p.getAtomicSentence().getSymbolicName());
            Function f = (Function)p.getAtomicSentence().getArgs()[0];
            Assert.AreEqual("F", f.getFunctionName());
            Variable v = (Variable)f.getTerms()[0];
            f = (Function)p.getAtomicSentence().getArgs()[1];
            Assert.AreEqual("F", f.getFunctionName());
            Assert.AreEqual(v, f.getTerms()[0]);

            //
            p = c.getPositiveLiterals()[1];
            f = (Function)p.getAtomicSentence().getArgs()[0];
            Assert.AreEqual("G", f.getFunctionName());
            f = (Function)f.getTerms()[0];
            Assert.AreEqual("F", f.getFunctionName());
            Assert.AreEqual(v, f.getTerms()[0]);
            f = (Function)p.getAtomicSentence().getArgs()[1];
            Assert.AreEqual("F", f.getFunctionName());
            Assert.AreEqual(v, f.getTerms()[0]);

            // p(g(x)), q(x), p(f(a)), p(x), p(g(f(x))), q(f(a))
            c = new Clause();
            c.addPositiveLiteral((Predicate)parser.parse("P(G(x))"));
            c.addPositiveLiteral((Predicate)parser.parse("Q(x)"));
            c.addPositiveLiteral((Predicate)parser.parse("P(F(A))"));
            c.addPositiveLiteral((Predicate)parser.parse("P(x)"));
            c.addPositiveLiteral((Predicate)parser.parse("P(G(F(x)))"));
            c.addPositiveLiteral((Predicate)parser.parse("Q(F(A))"));

            Assert.AreEqual("[[P(F(A)), P(G(F(F(A)))), P(G(F(A))), Q(F(A))]]",
                    c.getNonTrivialFactors().ToString());
        }
Exemple #23
0
        public void testCascadedOccursCheck()
        {
            FOLDomain domain = new FOLDomain();
            domain.addPredicate("P");
            domain.addFunction("F");
            domain.addFunction("SF0");
            domain.addFunction("SF1");
            FOLParser parser = new FOLParser(domain);

            Sentence s1 = parser.parse("P(SF1(v2),v2)");
            Sentence s2 = parser.parse("P(v3,SF0(v3))");
            Dictionary<Variable, Term> result = unifier.unify(s1, s2);

            Assert.IsNull(result);

            s1 = parser.parse("P(v1,SF0(v1),SF0(v1),SF0(v1),SF0(v1))");
            s2 = parser.parse("P(v2,SF0(v2),v2,     v3,     v2)");
            result = unifier.unify(s1, s2);

            Assert.IsNull(result);

            s1 = parser
                    .parse("P(v1,   F(v2),F(v2),F(v2),v1,      F(F(v1)),F(F(F(v1))),v2)");
            s2 = parser
                    .parse("P(F(v3),v4,   v5,   v6,   F(F(v5)),v4,      F(v3),      F(F(v5)))");
            result = unifier.unify(s1, s2);

            Assert.IsNull(result);
        }
        public void testNegativeTermEquality()
        {
            FOLDomain domain = new FOLDomain();
            domain.addConstant("A");
            domain.addConstant("B");
            domain.addConstant("C");
            domain.addPredicate("P");
            domain.addFunction("F");

            FOLParser parser = new FOLParser(domain);

            List<Literal> lits = new List<Literal>();
            AtomicSentence a1 = (AtomicSentence)parser.parse("P(y, F(A,y))");
            lits.Add(new Literal(a1));

            Clause c1 = new Clause(lits);

            lits.Clear();
            a1 = (AtomicSentence)parser.parse("F(x,B) = x");
            lits.Add(new Literal(a1, true));

            Clause c2 = new Clause(lits);

            List<Clause> paras = paramodulation.apply(c1, c2);
            Assert.AreEqual(0, paras.Count);
        }