public Sentence GetNegation()
 {
     Sentence negated = new Sentence();
     for (int i = 0; i < Clauses.Count; i++)
     {
         Clause currentClause = Clauses[i];
         if (Clauses.Count > 1)
         {
             for (int j = i + 1; j < Clauses.Count; j++)
             {
                 Clause nextClause = Clauses[j];
                 foreach (Literal lit1 in currentClause.Literals)
                 {
                     foreach (Literal lit2 in nextClause.Literals)
                     {
                         Clause clause = new Clause();
                         clause.AddLiteral(lit1.GetNegation());
                         clause.AddLiteral(lit2.GetNegation());
                         negated.AddClause(clause);
                     }
                 }
             }
         }
         else
         {
             negated = currentClause.GetNegation();
         }
     }
     return negated;
 }
 public static bool PlResolution(Sentence premises, Sentence conclusion)
 {
     List<Clause> clauses = premises.Clauses.Union(conclusion.GetNegation().Clauses).ToList();
     List<Clause> newc = new List<Clause>();
     while (true)
     {
         foreach (Clause ci in clauses)
         {
             List<Clause> restClauses = new List<Clause>(clauses);
             restClauses.Remove(ci);
             foreach (Clause cj in restClauses)
             {
                 List<Clause> resolvents = PlResolve(ci, cj);
                 if (resolvents.All(x => x.IsEmpty()))
                 {
                     return true;
                 }
                 newc.U(resolvents);
             }
         }
         if (newc.IsSubsetOf(clauses))
             return false;
         clauses.U(newc);
     }
 }
 public void SentenceNegationTest()
 {
     Clause a = new Clause();
     a.AddLiteral(new Literal("~", "A"));
     a.AddLiteral(new Literal("", "B"));
     a.AddLiteral(new Literal("~", "C"));
     Clause b = new Clause();
     b.AddLiteral(new Literal("", "C"));
     b.AddLiteral(new Literal("", "D"));
     b.AddLiteral(new Literal("", "E"));
     Sentence s = new Sentence();
     s.AddClause(a);
     s.AddClause(b);
     /*
         s is now ( ~A v B v ~C ) ^ ( C v D v E )
         NEGATE IT        ~ ( ( ~A v B v ~C ) ^ ( C v D v E ) )
         DeMorgan's gives us   ~ ( ~A v B v ~C ) v ~ ( C v D v E )
         DeMorgan's again      ( A ^ ~B ^ C ) v (~C ^ ~D ^ ~E )
         Expected result is thus:
         ( A v ~C ) ^ ( A v ~D ) ^ ( A v ~E ) ^ ( ~B v ~C ) ^ ( ~B v ~D ) ^ ( ~B v ~E ) ^ ( C v ~C ) ^ ( C v ~D ) ^ ( C v ~E )
     */
     string expected =
         "( A v ~C ) ^ ( A v ~D ) ^ ( A v ~E ) ^ ( ~B v ~C ) ^ ( ~B v ~D ) ^ ( ~B v ~E ) ^ ( C v ~C ) ^ ( C v ~D ) ^ ( C v ~E )";
     Assert.AreEqual(expected, s.GetNegation().ToString(), "Negation result is incorrect");
 }
 public Sentence GetNegation()
 {
     Sentence negation = new Sentence();
     foreach (Literal literal in Literals)
     {
         Clause negClause = new Clause();
         negClause.AddLiteral(literal.GetNegation());
         negation.AddClause(negClause);
     }
     return negation;
 }
        public void PlResolutionTest2()
        {
            Sentence premises = new Sentence();
            Clause tempClause = new Clause();
            tempClause.AddLiteral(new Literal("","A"));
            premises.AddClause(tempClause);

            Sentence conclusion = new Sentence();
            conclusion.AddClause(tempClause);

            bool canResolve = TheoremProver.PlResolution(premises, conclusion);
            Assert.AreEqual(true, canResolve, "Resolution should be possible");
        }
 public void SentenceListTest()
 {
     Sentence s = new Sentence();
     Clause a = new Clause();
     a.AddLiteral(new Literal("~", "A"));
     a.AddLiteral(new Literal("", "B"));
     Clause b = new Clause();
     b.AddLiteral(new Literal("","C"));
     b.AddLiteral(new Literal("","D"));
     s.AddClause(a);
     s.AddClause(b);
     Assert.AreEqual(s.Clauses[0].ToString(), "( ~A v B )", "First Clause incorrect");
     Assert.AreEqual(s.Clauses[1].ToString(), "( C v D )", "Second Clause incorrect");
 }
 public static Sentence ExtractSentence(string input)
 {
     Sentence result = new Sentence();
     foreach (Clause clause in ExtractClauses(input))
     {
         result.AddClause(clause);
     }
     return result;
 }
        public static void resolutionTest2()
        {
            Console.WriteLine("******************** start resolutionTest2 ********************");

            Sentence premises = new Sentence();

            Clause tempClause = new Clause();

            Literal tempLiteral = new Literal("", "A");
            tempClause.AddLiteral(tempLiteral);
            tempLiteral = new Literal("~", "B");
            tempClause.AddLiteral(tempLiteral);

            premises.AddClause(tempClause);

            tempClause = new Clause();
            tempLiteral = new Literal("", "B");
            tempClause.AddLiteral(tempLiteral);
            tempLiteral = new Literal("~", "D");
            tempClause.AddLiteral(tempLiteral);

            premises.AddClause(tempClause);

            tempClause = new Clause();
            tempLiteral = new Literal("", "E");
            tempClause.AddLiteral(tempLiteral);
            tempLiteral = new Literal("~", "F");
            tempClause.AddLiteral(tempLiteral);
            tempLiteral = new Literal("~", "A");
            tempClause.AddLiteral(tempLiteral);

            premises.AddClause(tempClause);

            //Console.WriteLine("premises\n"+premises);

            ///////////////
            ////////////
            /////////////////

            Sentence conclusion = new Sentence();
            tempClause = new Clause();
            tempLiteral = new Literal("", "C");
            tempClause.AddLiteral(tempLiteral);

            conclusion.AddClause(tempClause);

            tempClause = new Clause();
            tempLiteral = new Literal("~", "X");
            tempClause.AddLiteral(tempLiteral);

            conclusion.AddClause(tempClause);

            Console.WriteLine("**** premises " + premises);
            Console.WriteLine("***** conclusion " + conclusion);

            bool canResolve = TheoremProver.PlResolution(premises, conclusion);

            Console.WriteLine("canResolve " + canResolve);

            Console.WriteLine("******************** end resolutionTest2 ********************");
        }
        public static void basicTests()
        {
            Console.WriteLine("******************** start basicTests ********************");
            Sentence theSentence = new Sentence();

            Clause tempClause = new Clause();

            Literal tempLiteral = new Literal("", "A");
            Console.WriteLine("tempLiteral (should be 'A')\n" + tempLiteral);
            tempClause.AddLiteral(tempLiteral);
            tempLiteral = new Literal("~", "B");
            Console.WriteLine("tempLiteral (should be '~B')\n" + tempLiteral);
            tempClause.AddLiteral(tempLiteral);
            Console.WriteLine("tempClause (should be '( A v ~B )')\n" + tempClause);

            theSentence.AddClause(tempClause);
            Console.WriteLine("theSentence (should be '( A v ~B )')\n" + theSentence);

            tempClause = new Clause();
            tempLiteral = new Literal("", "B");
            tempClause.AddLiteral(tempLiteral);
            tempLiteral = new Literal("~", "D");
            tempClause.AddLiteral(tempLiteral);

            theSentence.AddClause(tempClause);
            Console.WriteLine("theSentence (should be '( A v ~B ) ^ ( B v ~D )'\n" + theSentence);

            tempClause = new Clause();
            tempLiteral = new Literal("", "E");
            tempClause.AddLiteral(tempLiteral);
            tempLiteral = new Literal("~", "F");
            tempClause.AddLiteral(tempLiteral);
            tempLiteral = new Literal("~", "A");
            tempClause.AddLiteral(tempLiteral);

            theSentence.AddClause(tempClause);

            Console.WriteLine("theSentence (should be '( A v ~B ) ^ ( B v ~D ) ^ ( E v ~F v ~A )'\n" + theSentence);

            tempLiteral = new Literal("", "A");
            Console.WriteLine("tempLiteral (should be 'A')\n" + tempLiteral);
            tempLiteral = tempLiteral.GetNegation();
            Console.WriteLine("negation of tempLiteral (should be '~A')\n" + tempLiteral);
            tempLiteral = tempLiteral.GetNegation();
            Console.WriteLine("negation of negation of tempLiteral (should be 'A')\n" + tempLiteral);

            tempClause = new Clause();
            tempClause.AddLiteral(tempLiteral);
            tempLiteral = new Literal("", "B");
            tempClause.AddLiteral(tempLiteral);

            Console.WriteLine("tempClause   (should be '( A v B )'\n" + tempClause);

            Sentence negatedTempClause = tempClause.GetNegation();
            Console.WriteLine("negatedTempClause   (should be '( ~A ) ^ ( ~B )'\n" + negatedTempClause);

            Sentence negatedSentence = negatedTempClause.GetNegation();

            Console.WriteLine("negatedSentence   (should be '( A v B )'\n" + tempClause);

            Console.WriteLine("=======");
            Console.WriteLine("=======");

            Sentence testerSentence = new Sentence();

            tempClause = new Clause();
            tempLiteral = new Literal("~", "A");
            tempClause.AddLiteral(tempLiteral);
            tempLiteral = new Literal("", "B");
            tempClause.AddLiteral(tempLiteral);
            tempLiteral = new Literal("~", "C");
            tempClause.AddLiteral(tempLiteral);

            Console.WriteLine("tempClause   (should be '( ~A v B v ~C )'\n" + tempClause);

            testerSentence.AddClause(tempClause);
            Console.WriteLine("testerSentence   (should be '( ~A v B v ~C )'\n" + testerSentence);

            tempClause = new Clause();
            tempLiteral = new Literal("", "C");
            tempClause.AddLiteral(tempLiteral);
            tempLiteral = new Literal("", "D");
            tempClause.AddLiteral(tempLiteral);
            tempLiteral = new Literal("", "E");
            tempClause.AddLiteral(tempLiteral);

            Console.WriteLine("tempClause   (should be '( C v D v E )'\n" + tempClause);

            testerSentence.AddClause(tempClause);
            Console.WriteLine("testerSentence   (should be '( ~A v B v ~C ) ^ ( C v D v E )'\n" + testerSentence);

            //WE START WITH     ( ~A v B v ~C ) ^ ( C v D v E )

            //NEGATE IT        ~ ( ( ~A v B v ~C ) ^ ( C v D v E ) )
            //DeMorgan's gives us   ~ ( ~A v B v ~C ) v ~ ( C v D v E )
            //DeMorgan's again      ( A ^ ~B ^ C ) v (~C ^ ~D ^ ~E )

            //now do distribution to arrive at CNF

            ///  ( A v ~C) ^ ( A v ~D ) ^ ( A v ~E ) ^ ( ~B v ~C ) ^ ( ~B v ~D ) ^ ( ~B v ~E ) ^ ( C v ~C ) ^ ( C v ~D ) ^ ( C v ~E )

            Sentence negatedTesterSentence = testerSentence.GetNegation();

            Console.WriteLine("negatedTesterSentence   (should be '( A v ~C ) ^ ( A v ~D ) ^ ( A v ~E ) ^ ( ~B v ~C ) ^ ( ~B v ~D ) ^ ( ~B v ~E ) ^ ( C v ~C ) ^ ( C v ~D ) ^ ( C v ~E ) \n" +
            "        '\n" + negatedTesterSentence);

            Console.WriteLine("******************** end basicTests ********************");
        }