Exemplo n.º 1
0
        private bool IsBetaRule(Proposition proposition)
        {
            if (proposition.GetType() == typeof(Negation))
            {
                Negation n = (Negation)proposition;

                return(n.LeftSuccessor.GetType() == typeof(Conjunction) ||
                       n.LeftSuccessor.GetType() == typeof(BiImplication));
            }

            if (proposition.GetType() == typeof(Nand))
            {
                return(true);
            }

            if (proposition.GetType() == typeof(Disjunction))
            {
                return(true);
            }

            if (proposition.GetType() == typeof(Implication))
            {
                return(true);
            }

            if (proposition.GetType() == typeof(BiImplication))
            {
                return(true);
            }

            return(false);
        }
Exemplo n.º 2
0
        // Both, but equals still needs to be overriden for predicates
        protected bool IsContradiction(Proposition proposition1, Proposition proposition2)
        {
            Negation    negatedProposition = null;
            Proposition targetLiteral      = null;

            if (proposition1 is Negation)
            {
                negatedProposition = (Negation)proposition1;
                targetLiteral      = proposition2;
            }

            if (proposition2 is Negation)
            {
                negatedProposition = (Negation)proposition2;
                targetLiteral      = proposition1;
            }

            if (negatedProposition != null)
            {
                Proposition negatedSuccessor = negatedProposition.LeftSuccessor;
                return(negatedSuccessor.Equals(targetLiteral));
            }

            return(false);
        }
Exemplo n.º 3
0
        private void ApplyDoubleNegationRule(Proposition proposition)
        {
            HashSet <Proposition> propositions = new HashSet <Proposition>();

            foreach (Proposition p in Propositions)
            {
                if (!p.Equals(proposition))
                {
                    propositions.Add(p);
                }
            }

            Proposition doubleNegationRemoved = null;

            if (proposition.GetType() == typeof(Negation))
            {
                Negation    negation           = (Negation)proposition;
                Proposition negatedProposition = negation.LeftSuccessor;

                if (negatedProposition.GetType() == typeof(Negation))
                {
                    Negation negatedNegation = (Negation)negatedProposition;

                    doubleNegationRemoved = negatedNegation.LeftSuccessor;
                    propositions.Add(doubleNegationRemoved);
                }
            }

            LeftChild = new SemanticTableauxElement(propositions, new HashSet <char>(ReplacementVariables));
        }
Exemplo n.º 4
0
        private static void NumberOperands(Proposition expressionRoot)
        {
            List <Proposition> queue         = new List <Proposition>();
            Proposition        currentSymbol = expressionRoot;

            queue.Add(currentSymbol);

            int nodeCounter = 1;

            while (currentSymbol != null)
            {
                currentSymbol.NodeNumber = nodeCounter;

                if (currentSymbol is BinaryConnective)
                {
                    queue.Add(((BinaryConnective)currentSymbol).LeftSuccessor);
                    queue.Add(((BinaryConnective)currentSymbol).RightSuccessor);
                }
                else if (currentSymbol is UnaryConnective)
                {
                    queue.Add(((UnaryConnective)currentSymbol).LeftSuccessor);
                }

                queue.RemoveAt(0);
                if (queue.Count > 0)
                {
                    currentSymbol = queue[0];
                }
                else
                {
                    currentSymbol = null;
                }
                nodeCounter++;
            }
        }
Exemplo n.º 5
0
        private Proposition GenerateExpressionRecursively(Proposition result, int level)
        {
            if (!(result is UnaryConnective))
            {
                return(result);
            }

            int newLevel = level + 1;

            if (level >= 5)
            {
                newLevel = level + 4;
            }

            if (result is UnaryConnective)
            {
                // Generate a random left
                UnaryConnective connective = (UnaryConnective)result;
                connective.LeftSuccessor = GenerateProposition(level);

                GenerateExpressionRecursively(connective.LeftSuccessor, newLevel);
            }

            if (result is BinaryConnective)
            {
                // Generate a random right
                BinaryConnective connective = (BinaryConnective)result;
                connective.RightSuccessor = GenerateProposition(level);

                GenerateExpressionRecursively(connective.RightSuccessor, newLevel);
            }

            return(result);
        }
Exemplo n.º 6
0
 public TruthTableRow(Proposition propositionRoot, List <Proposition> uniqueVariables, int numberOfVariables)
 {
     this.propositionRoot = propositionRoot;
     this.uniqueVariables = uniqueVariables;
     Cells        = new char[numberOfVariables];
     IsSimplified = false;
 }
Exemplo n.º 7
0
        public SemanticTableaux(Proposition proposition)
        {
            Proposition = proposition;

            if (proposition == null)
            {
                throw new NullReferenceException("A proposition is required to make use of the semantic tableaux");
            }

            Negation negation = new Negation();

            negation.LeftSuccessor = proposition;

            HashSet <Proposition> propositions = new HashSet <Proposition>()
            {
                negation
            };

            // Then the type of semantic tableaux element could be
            // set by the semantic tableaux by perhaps setting
            // an enum on the semantic tableaux element
            // which calls a create method
            SemanticTableauxElement.ResetReplacementVariables();
            Head = new SemanticTableauxElement(propositions);
        }
Exemplo n.º 8
0
        private void CreateConnective(char connective)
        {
            Proposition result = null;

            // 'GetCorrespondingConnective'
            switch (connective)
            {
            case Negation.SYMBOL:
                result = new Negation();
                break;

            case Implication.SYMBOL:
                result = new Implication();
                break;

            case BiImplication.SYMBOL:
                result = new BiImplication();
                break;

            case Conjunction.SYMBOL:
                result = new Conjunction();
                break;

            case Disjunction.SYMBOL:
                result = new Disjunction();
                break;

            case Nand.SYMBOL:
                result = new Nand();
                break;

            case UniversalQuantifier.SYMBOL:
                result = new UniversalQuantifier(PopBoundVariable());
                break;

            case ExistentialQuantifier.SYMBOL:
                result = new ExistentialQuantifier(PopBoundVariable());
                break;
            }

            // 'CreateExpression'
            // The symbols[symbols.Count - 1] can always be done. Placement is dependent on Unary or Binary connective.
            if (result is BinaryConnective)
            {
                ((BinaryConnective)result).LeftSuccessor  = symbols[symbols.Count - 2];
                ((BinaryConnective)result).RightSuccessor = symbols[symbols.Count - 1];
                symbols.RemoveAt(symbols.Count - 1);
                symbols.RemoveAt(symbols.Count - 1);
            }
            else
            {
                ((UnaryConnective)result).LeftSuccessor = symbols[symbols.Count - 1];
                symbols.RemoveAt(symbols.Count - 1);
            }

            // Now the expression is a symbol in case a nested connective needs to use it
            // as right or left successor.
            symbols.Add(result);
        }
Exemplo n.º 9
0
        private void ApplyAlphaRule(Proposition proposition)
        {
            HashSet <Proposition> childPropositions = new HashSet <Proposition>();

            foreach (Proposition p in Propositions)
            {
                if (!p.Equals(proposition))
                {
                    childPropositions.Add(p);
                }
            }

            if (proposition.GetType() == typeof(Conjunction))
            {
                BinaryConnective connective = (BinaryConnective)proposition;

                childPropositions.Add(connective.LeftSuccessor);
                childPropositions.Add(connective.RightSuccessor);
            }

            if (proposition.GetType() == typeof(Negation))
            {
                Negation         negation         = (Negation)proposition;
                BinaryConnective nestedConnective = (BinaryConnective)negation.LeftSuccessor;

                if (nestedConnective.GetType() != typeof(Nand))
                {
                    // Both cases have a negation of the right successor.
                    Negation negatedRight = new Negation();
                    negatedRight.LeftSuccessor = nestedConnective.RightSuccessor;

                    // Only disjunction results in a left side negated as well.
                    if (nestedConnective.GetType() == typeof(Disjunction))
                    {
                        Negation negatedLeft = new Negation();
                        negatedLeft.LeftSuccessor = nestedConnective.LeftSuccessor;

                        childPropositions.Add(negatedLeft);
                    }

                    // Should not forget to add the implication's left successor
                    if (nestedConnective.GetType() == typeof(Implication))
                    {
                        childPropositions.Add(nestedConnective.LeftSuccessor);
                    }

                    childPropositions.Add(negatedRight);
                }
                else
                {
                    // It's a negated Nand and we can add left and right to the set
                    childPropositions.Add(nestedConnective.LeftSuccessor);
                    childPropositions.Add(nestedConnective.RightSuccessor);
                }
            }

            LeftChild = new SemanticTableauxElement(childPropositions, new HashSet <char>(ReplacementVariables));
        }
Exemplo n.º 10
0
        private void randomPropositionBtn_Click(object sender, EventArgs e)
        {
            ResetAllControls();

            propositionTbx.Text  = "";
            generatedProposition = propositionGenerator.GenerateExpression();

            infixTbx.Text = generatedProposition.ToString();
        }
Exemplo n.º 11
0
        public static void CreateGraphOfProposition(Proposition propositionRoot, string fileName)
        {
            if (propositionRoot == null)
            {
                throw new ArgumentNullException("Proposition root cannot be null!");
            }

            NumberOperands(propositionRoot);
            CreateGraph(fileName, propositionRoot.NodeLabel());
        }
Exemplo n.º 12
0
        protected override void ExecuteParsingActivities()
        {
            if (Root.IsAbstractProposition())
            {
                hashCodes = new List <string>();

                // 1
                TruthTable = new TruthTable(Root);
                hashCodeCalculator.GenerateHashCode(TruthTable.GetConvertedResultColumn());
                hashCodes.Add(hashCodeCalculator.HashCode);

                // 2
                SimplifiedTruthTable = TruthTable.Simplify();

                if (NonConstantExpressionWasParsed())
                {
                    // Convert the table to DNF such that we have a proposition to work with.
                    Proposition simplified   = SimplifiedTruthTable.CreateDisjunctiveNormalForm();
                    TruthTable  simplifiedTt = new TruthTable(simplified);
                    // Check the hash code for the proposition.
                    hashCodeCalculator.GenerateHashCode(simplifiedTt.GetConvertedResultColumn());
                    hashCodes.Add(hashCodeCalculator.HashCode);

                    // 3
                    Nandified = Root.Nandify();
                    TruthTable nandifiedTruthTable = new TruthTable(Nandified);
                    TruthTable nandifiedSimplified = nandifiedTruthTable.Simplify();

                    // Same as for simplified truth table.
                    Proposition nandSimpleDnf   = nandifiedSimplified.CreateDisjunctiveNormalForm();
                    TruthTable  nandSimpleDnfTt = new TruthTable(nandSimpleDnf);

                    hashCodeCalculator.GenerateHashCode(nandSimpleDnfTt.GetConvertedResultColumn());
                    hashCodes.Add(hashCodeCalculator.HashCode);

                    // 4
                    DisjunctiveNormalForm = TruthTable.CreateDisjunctiveNormalForm();
                    TruthTable disjunctiveTruthTable = new TruthTable(DisjunctiveNormalForm);
                    hashCodeCalculator.GenerateHashCode(disjunctiveTruthTable.GetConvertedResultColumn());
                    hashCodes.Add(hashCodeCalculator.HashCode);

                    // 5
                    Proposition simplifiedDisjunctiveNormal           = SimplifiedTruthTable.CreateDisjunctiveNormalForm();
                    TruthTable  simplifiedDisjunctiveNormalTruthTable = new TruthTable(simplifiedDisjunctiveNormal);
                    hashCodeCalculator.GenerateHashCode(simplifiedDisjunctiveNormalTruthTable.GetConvertedResultColumn());
                    hashCodes.Add(hashCodeCalculator.HashCode);

                    // 6
                    Proposition nandifiedSimplifiedDisjunctiveNormal           = simplifiedDisjunctiveNormal.Nandify();
                    TruthTable  nandifiedSimplifiedDisjunctiveNormalTruthTable = new TruthTable(nandifiedSimplifiedDisjunctiveNormal);
                    hashCodeCalculator.GenerateHashCode(nandifiedSimplifiedDisjunctiveNormalTruthTable.GetConvertedResultColumn());
                    hashCodes.Add(hashCodeCalculator.HashCode);
                }
            }
        }
Exemplo n.º 13
0
        public void Parse(Proposition expression)
        {
            if (expression == null)
            {
                throw new ArgumentException("Proposition may not be null");
            }

            Root = expression;

            ExecuteParsingActivities();
        }
Exemplo n.º 14
0
        private bool TryToCreateDoubleNegation(Proposition proposition)
        {
            if (IsDoubleNegationRule(proposition))
            {
                ApplyDoubleNegationRule(proposition);

                return(true);
            }

            return(false);
        }
Exemplo n.º 15
0
        private bool TryToCreateBetaRule(Proposition proposition)
        {
            if (IsBetaRule(proposition))
            {
                ApplyBetaRule(proposition);

                return(true);
            }

            return(false);
        }
Exemplo n.º 16
0
        public Proposition CreateDisjunctiveNormalForm()
        {
            List <Proposition> propositionList = GenerateDisjunctiveNormalFormEquivalentPropositions();

            // Check for missing variables.
            HashSet <Proposition> missingVariables = GetMissingVariables(propositionList);

            if (missingVariables.Count == propositionVariablesSet.Count)
            {
                if (propositionList.Count > 0)
                {
                    Proposition p = propositionList[0];
                    propositionList.Remove(p);

                    if (p.GetType() == typeof(True))
                    {
                        // Create tautologies.
                        foreach (Proposition variable in missingVariables)
                        {
                            propositionList.Add(PropositionGenerator.CreateTautologyFromProposition(variable));
                        }
                    }
                }
                else
                {
                    // Create contradictions.
                    foreach (Proposition variable in missingVariables)
                    {
                        propositionList.Add(PropositionGenerator.CreateContradictionFromProposition(variable));
                    }
                }
            }

            // If we miss one or more variables but not all, this indicates that those variables
            // had no meaning in the original expression. Thus by logical equivalence we can say that
            // the expression would be equivalent to for ex: A & (B | ~(B)), the truth value of B
            // does not matter thus we can restate this as A | 0 which means we are totally reliant
            // on the truth value of A which can be obtained by creating a contradiction of
            // the variable that does not matter (B) in this case to keep the result column of equal
            // length, this would result in A | (B & ~(B)) which IS in DNF
            if (missingVariables.Count > 0 && missingVariables.Count < propositionVariablesSet.Count)
            {
                foreach (Proposition missingVariable in missingVariables)
                {
                    propositionList.Add(PropositionGenerator.CreateContradictionFromProposition(missingVariable));
                }
            }

            propositionList = CreateCorrespondingDisjuncts(propositionList);

            return(propositionList[0]);
        }
Exemplo n.º 17
0
        private void ApplyDeltaRule(Proposition proposition)
        {
            // COULD BE: GetAllDifferingChildPropositions(proposition)
            // REFACTOR WORTHY: otherwise needs to be tested for every rule which is all the same
            HashSet <Proposition> childPropositions = new HashSet <Proposition>();

            foreach (Proposition p in Propositions)
            {
                if (!p.Equals(proposition))
                {
                    childPropositions.Add(p);
                }
            }
            // REFACTOR WORTHY

            // Current proposition we want to break up again into new pieces.
            Quantifier  quantifier = null;
            Proposition predicate  = null;

            if (proposition.GetType() == typeof(Negation))
            {
                Negation negatedUniversalQuantifier = (Negation)proposition;
                quantifier = (Quantifier)negatedUniversalQuantifier.LeftSuccessor;

                predicate = quantifier.LeftSuccessor;
                Negation negatedPredicate = new Negation();
                negatedPredicate.LeftSuccessor = predicate;

                predicate = negatedPredicate;
            }
            else
            {
                quantifier = (Quantifier)proposition;
                predicate  = quantifier.LeftSuccessor;
            }

            char boundVariable        = quantifier.GetBoundVariable();
            char replacementCharacter = GenerateReplacementVariable();

            Proposition predicateCopy = predicate.Copy();

            predicateCopy.Replace(boundVariable, replacementCharacter);

            // Get current set locally, add new variable and pass them down.
            HashSet <char> childReplacementVariables = new HashSet <char>(ReplacementVariables);

            childReplacementVariables.Add(replacementCharacter);

            childPropositions.Add(predicateCopy);

            LeftChild = new SemanticTableauxElement(childPropositions, childReplacementVariables);
        }
Exemplo n.º 18
0
        public void Parse(string propositionExpression)
        {
            if (propositionExpression == null || propositionExpression == string.Empty)
            {
                throw new ArgumentException("Proposition expression may not be null or empty string");
            }

            Root = parser.Parse(propositionExpression);

            ExecuteParsingActivities();

            parsedExpression = propositionExpression;
        }
Exemplo n.º 19
0
        public static Proposition CreateContradictionFromProposition(Proposition variable)
        {
            if (variable == null)
            {
                throw new NullReferenceException("A proposition is required to create a contradiction from it!");
            }

            Negation negation = new Negation();

            negation.LeftSuccessor = CreateTautologyFromProposition(variable);

            return(negation);
        }
Exemplo n.º 20
0
        private bool IsDoubleNegationRule(Proposition proposition)
        {
            if (proposition.GetType() == typeof(Negation))
            {
                Negation    negation           = (Negation)proposition;
                Proposition negatedProposition = negation.LeftSuccessor;

                if (negatedProposition.GetType() == typeof(Negation))
                {
                    return(true);
                }
            }

            return(false);
        }
Exemplo n.º 21
0
        // Specific to abstract propositions
        private bool ConstantContradiction(Proposition proposition)
        {
            if (proposition.GetType() == typeof(False))
            {
                return(true);
            }

            if (proposition.GetType() == typeof(Negation))
            {
                Negation negation = (Negation)proposition;
                return(negation.LeftSuccessor.GetType() == typeof(True));
            }

            return(false);
        }
Exemplo n.º 22
0
        public override Proposition Nandify()
        {
            // ~(A) == ~(A & A) == A % A
            Nand        nand          = new Nand();
            Proposition nandifiedLeft = LeftSuccessor;

            if (LeftSuccessor.GetType() != typeof(Proposition))
            {
                nandifiedLeft = LeftSuccessor.Nandify();
            }

            nand.LeftSuccessor  = nandifiedLeft;
            nand.RightSuccessor = nandifiedLeft;

            return(nand);
        }
Exemplo n.º 23
0
        private void Parse()
        {
            string proposition = propositionTbx.Text;

            if (proposition != string.Empty)
            {
                generatedProposition = null;
                selectedApp.Parse(proposition);
            }

            if (generatedProposition != null)
            {
                propositionTbx.Text = "";
                selectedApp.Parse(generatedProposition);
            }
        }
Exemplo n.º 24
0
        private bool IsGammaRule(Proposition proposition)
        {
            if (proposition.GetType() == typeof(UniversalQuantifier))
            {
                return(true);
            }

            if (proposition.GetType() == typeof(Negation))
            {
                Proposition quantifier = ((Negation)proposition).LeftSuccessor;

                return(quantifier.GetType() == typeof(ExistentialQuantifier));
            }

            return(false);
        }
Exemplo n.º 25
0
        public static Proposition CreateTautologyFromProposition(Proposition variable)
        {
            if (variable == null)
            {
                throw new NullReferenceException("A proposition is required to create a tautology from it!");
            }

            Disjunction tautology = new Disjunction();

            tautology.LeftSuccessor = variable; // A
            Negation negatedVariable = new Negation();

            negatedVariable.LeftSuccessor = variable;
            tautology.RightSuccessor      = negatedVariable; // A | ~(A) == 1

            return(tautology);
        }
Exemplo n.º 26
0
 public TruthTable(Proposition propositionRoot)
 {
     this.propositionRoot = propositionRoot;
     // Transparent caching
     if (propositionRoot.UniqueVariableSet == null)
     {
         propositionVariablesSet = propositionRoot.GetVariables();
     }
     else
     {
         propositionVariablesSet = propositionRoot.UniqueVariableSet;
     }
     // Place them in alphabetic order
     propositionVariablesSet.Sort();
     Rows = new List <ITruthTableRow>();
     CreateTruthTableRows();
 }
Exemplo n.º 27
0
        private bool IsDeltaRule(Proposition proposition)
        {
            if (proposition.GetType() == typeof(ExistentialQuantifier))
            {
                return(true);
            }

            // Negated Universal Quantifier
            if (proposition.GetType() == typeof(Negation))
            {
                Proposition left = ((Negation)proposition).LeftSuccessor;

                return(left.GetType() == typeof(UniversalQuantifier));
            }

            return(false);
        }
Exemplo n.º 28
0
        public Proposition GetDisjunctiveNormalFormEquivalent()
        {
            List <Proposition> propositionList = new List <Proposition>();

            for (int i = 0; i < Cells.Length; i++)
            {
                // return value is null if cell held *
                // Add the obtained variable to the list such that we can process it.
                Proposition resultVariable = GetDisjunctiveNormalFormVariable(Cells[i], uniqueVariables[i]);
                if (resultVariable != null)
                {
                    propositionList.Add(resultVariable);
                }
            }
            // Create conjuncts and insert them at the first position
            // until there is 1 proposition object left. (the root)
            while (propositionList.Count > 1)
            {
                // take vriable at pos 0 and 1 and create a conjunct between them.
                // Insert them back in the first position of the list.
                Conjunction conjunct = new Conjunction();
                // We do not need to create copies of variables since we did that beforehand.
                conjunct.LeftSuccessor  = propositionList[0];
                conjunct.RightSuccessor = propositionList[1];
                // Remove the individual variables from the list.
                propositionList.RemoveAt(1);
                propositionList.RemoveAt(0);
                // Insert the conjunct into the list.
                propositionList.Add(conjunct);
            }

            if (propositionList.Count > 0)
            {
                return(propositionList[0]);
            }

            if (Result == true)
            {
                return(new True());
            }

            return(new False());
        }
Exemplo n.º 29
0
        public Proposition GenerateExpression()
        {
            if (rng == null)
            {
                rng = new Random();
            }

            generatedPropositionVariables = new HashSet <Proposition>();
            int numberOfVariables = rng.Next(1, MAX_UNIQUE_VARIABLES + 1);

            while (generatedPropositionVariables.Count < numberOfVariables)
            {
                generatedPropositionVariables.Add(GetRandomProposition());
            }

            int         startLevel = 0;
            Proposition root       = GenerateProposition(startLevel);

            return(GenerateExpressionRecursively(root, startLevel + 1));
        }
Exemplo n.º 30
0
        private Proposition GetDisjunctiveNormalFormVariable(char truthValue, Proposition variable)
        {
            Proposition disjunctiveNormalFormVariable = null;

            // Can omit any actions if the cell holds a don't care variable
            // only the case for simplified truth tables.
            if (truthValue != '*')
            {
                if (truthValue == '0')
                {
                    // Negate the original variable as this will result into true in the conjunction.
                    Negation negation = new Negation();
                    negation.LeftSuccessor        = variable;
                    disjunctiveNormalFormVariable = negation;
                }
                else if (truthValue == '1')
                {
                    // Return the original variable.
                    disjunctiveNormalFormVariable = variable;
                }
            }
            return(disjunctiveNormalFormVariable);
        }