public static IEnumerable<Clause> ExtractClauses(string input)
 {
     foreach (string clauseString in ExtractClauseStrings(input))
     {
         if (clauseString.Contains("<-->"))
         {
             foreach (Clause clause in EliminateEquivalency(clauseString))
             {
                 yield return clause;
             }
         }
         else if (clauseString.Contains("-->"))
         {
             yield return EliminateImplication(clauseString);
         }
         else
         {
             Clause clause = new Clause();
             foreach (Literal literal in ExtractLiterals(clauseString))
             {
                 clause.AddLiteral(literal);
             }
             yield return clause;
         }
     }
 }
 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 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 void ClauseStringTest()
 {
     Clause clause = new Clause();
     Literal a = new Literal("","A");
     Literal b = new Literal("~", "B");
     clause.AddLiteral(a);
     clause.AddLiteral(b);
     Assert.AreEqual(clause.ToString(), "( A v ~B )", "Clause.ToString result incorrect");
 }
 public void ClauseListTest()
 {
     Clause clause = new Clause();
     Literal a = new Literal("", "A");
     Literal b = new Literal("~", "B");
     clause.AddLiteral(a);
     clause.AddLiteral(b);
     Assert.AreEqual(clause.Literals[0], a, "First element incorrect");
     Assert.AreEqual(clause.Literals[1], b, "Second element incorrect");
 }
 public void ClauseNegationTest()
 {
     Clause clause = new Clause();
     Literal a = new Literal("","A");
     Literal b = new Literal("~", "B");
     clause.AddLiteral(a);
     clause.AddLiteral(b);
     Sentence s = clause.GetNegation();
     Assert.AreEqual(s.ToString(), "( ~A ) ^ ( B )", "Negated clause incorrect");
 }
        public void EliminateImplicationTest()
        {
            Clause result = TextParser.EliminateImplication("A --> B");

            Clause expected = new Clause();
            expected.AddLiteral(new Literal("~", "A"));
            expected.AddLiteral(new Literal("", "B"));

            Assert.AreEqual(expected,result, "Clauses don't match");
        }
 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 ResolveTest()
        {
            Clause c1 = new Clause();
            c1.AddLiteral(new Literal("", "A"));
            c1.AddLiteral(new Literal("~", "B"));

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

            List<Clause> resolution = TheoremProver.PlResolve(c1, c2);
            Assert.AreEqual("( A )", resolution[0].ToString(), "First Clause wrong");
            Assert.AreEqual(true, resolution[1].IsEmpty(), "Second Clause not empty");
        }
 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 void EliminateEquivalencyTest()
        {
            List<Clause> result = TextParser.EliminateEquivalency("A <--> B");

            Clause firstExpected = new Clause();
            firstExpected.AddLiteral(new Literal("~","A"));
            firstExpected.AddLiteral(new Literal("","B"));
            Clause secondExpected = new Clause();
            secondExpected.AddLiteral(new Literal("~", "B"));
            secondExpected.AddLiteral(new Literal("", "A"));

            Assert.AreEqual(firstExpected, result[0], "Clauses not equal");
            Assert.AreEqual(secondExpected, result[1], "Clauses not equal");
        }
        public static Clause EliminateImplication(string input)
        {
            string[] halves = input.Split(new[] {"-->"}, StringSplitOptions.None);
            List<Literal> firstHalf = ExtractLiterals(halves[0]).ToList();
            List<Literal> secondHalf = ExtractLiterals(halves[1]).ToList();

            Clause result = new Clause();
            foreach (Literal literal in firstHalf)
            {
                result.AddLiteral(literal.GetNegation());
            }
            foreach (Literal literal in secondHalf)
            {
                result.AddLiteral(literal);
            }
            return result;
        }
        public static List<Clause> PlResolve(Clause ci, Clause cj)
        {
            List<Clause> result = new List<Clause>();
            List<int> iRemovals = new List<int>();
            List<int> jRemovals = new List<int>();
            foreach (Literal ciLiteral in ci.Literals)
            {
                foreach (Literal cjLiteral in cj.Literals)
                {
                    if (ciLiteral.Variable == cjLiteral.Variable && ciLiteral.Negation != cjLiteral.Negation)
                    {
                        iRemovals.Add(ci.Literals.IndexOf(ciLiteral));
                        jRemovals.Add(cj.Literals.IndexOf(cjLiteral));
                    }
                }

            }

            //This indexing method is nice, because direct removal breaks iteration/enumeration
            //If we just mark them as we go, however, it's easy to construct the new Clauses

            Clause newCi = new Clause();
            for (int i = 0; i < ci.Literals.Count; i++)
            {
                if(!iRemovals.Contains(i))
                    newCi.AddLiteral(ci.Literals[i]);
            }
            result.Add(newCi);

            Clause newCj = new Clause();
            for (int i = 0; i < cj.Literals.Count; i++)
            {
                if (!jRemovals.Contains(i))
                    newCj.AddLiteral(cj.Literals[i]);
            }
            result.Add(newCj);

            return result;
        }
 public void AddClause(Clause clause)
 {
     Clauses.Add(clause);
 }
        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 ********************");
        }
        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 ********************");
        }