Exemplo n.º 1
0
        private static Matrix ParseQuantification(
            string aQuantifier,
            string[] aBody,
            CollectedItems aCollectedItems,
            VariableDictionary aBoundVariables)
        {
            char     lSymbolForVariable = GetSymbolForVariable(aQuantifier);
            Variable lVariable          = Factory.Variable(lSymbolForVariable);
            Matrix   lBody = Parse(
                aBody,
                aCollectedItems,
                aBoundVariables.CreateNewSetThatRebinds(lSymbolForVariable, lVariable));

            if (aQuantifier.Matches(ForAll))
            {
                return(Factory.ForAll(lVariable, lBody));
            }

            if (aQuantifier.Matches(ThereExists))
            {
                return(Factory.ThereExists(lVariable, lBody));
            }

            if (aQuantifier.Matches(The))
            {
                return(Factory.The(lVariable, lBody));
            }

            // The program should never reach this point.
            throw new Exception();
        }
Exemplo n.º 2
0
        private static Matrix ParseUnaryOperatorOnTail(
            string aUnaryOperator,
            string[] aTail,
            CollectedItems aCollectedItems,
            VariableDictionary aBoundVariables)
        {
            Matrix lTail = Parse(aTail, aCollectedItems, aBoundVariables);

            if (aUnaryOperator.Matches(Not))
            {
                return(Factory.Not(lTail));
            }

            if (aUnaryOperator.Matches(Possibly))
            {
                return(Factory.Possibly(lTail));
            }

            if (aUnaryOperator.Matches(Necessarily))
            {
                return(Factory.Necessarily(lTail));
            }

            // The program should never reach this point.
            throw new Exception();
        }
Exemplo n.º 3
0
        private static Matrix ParseTwoTermProposition(string aExpression, CollectedItems aCollectedItems)
        {
            RegexMatch lMatch = TwoTermProposition.Exec(aExpression);

            Term lSubject = Factory.AllAndOnly(
                aCollectedItems.AddUnaryPredicate(lMatch[2][0]),
                lMatch[1].Length > 0);
            Term lPredicate = Factory.AllAndOnly(
                aCollectedItems.AddUnaryPredicate(lMatch[5][0]),
                lMatch[4].Length > 0);
            bool lModernInterpretation = lMatch[6].Length > 0;

            return(FormTwoTermProposition(lSubject, lPredicate, lMatch[3][0], lModernInterpretation));
        }
Exemplo n.º 4
0
 private static Variable FindVariable(
     char aSymbol,
     VariableDictionary aBoundVariables,
     CollectedItems aCollectedItems)
 {
     if (aBoundVariables.ContainsVariableForSymbol(aSymbol))
     {
         return(aBoundVariables.Retrieve(aSymbol));
     }
     else
     {
         return(aCollectedItems.AddUnboundVariable(aSymbol));
     }
 }
Exemplo n.º 5
0
        private static Matrix Parse(
            string[] aExpressions,
            CollectedItems aCollectedItems,
            VariableDictionary aBoundVariables)
        {
            string lFirst = aExpressions[0];

            if (aExpressions.Length == 1)
            {
                return(ParsePropositionalSubexpression(lFirst, aCollectedItems, aBoundVariables));
            }

            string[] lTail = aExpressions.Tail <string>();

            if (AreUnariesFollowedByAQuantifier(aExpressions))
            {
                return(ParseUnaryOperatorOnTail(lFirst, lTail, aCollectedItems, aBoundVariables));
            }

            if (lFirst.IsQuantifier())
            {
                return(ParseQuantification(lFirst, lTail, aCollectedItems, aBoundVariables));
            }

            if (aExpressions.TakeWhile(fExpression => !fExpression.IsQuantifier()).Any(fToken => fToken.IsBinaryOperator()))
            {
                return(ParseBinaryOperator(aExpressions, aCollectedItems, aBoundVariables));
            }

            if (IsUnaryOperator(lFirst))
            {
                return(ParseUnaryOperatorOnTail(lFirst, lTail, aCollectedItems, aBoundVariables));
            }

            throw new ParseError("Could not parse \"{0}\"", string.Join("", aExpressions));
        }
Exemplo n.º 6
0
        private static Matrix ParsePropositionalSubexpression(
            string aExpression,
            CollectedItems aCollectedItems,
            VariableDictionary aBoundVariables)
        {
            if (aExpression.Matches(TwoTermProposition))
            {
                return(ParseTwoTermProposition(aExpression, aCollectedItems));
            }

            if (aExpression.Matches(Is))
            {
                return(aCollectedItems.AddUnaryPredication(
                           aCollectedItems.AddUnaryPredicate(aExpression[0]),
                           FindVariable(aExpression[1], aBoundVariables, aCollectedItems)));
            }

            if (aExpression.Matches(Are))
            {
                return(aCollectedItems.AddBinaryPredication(
                           aCollectedItems.AddBinaryPredicate(aExpression[1]),
                           FindVariable(aExpression[0], aBoundVariables, aCollectedItems),
                           FindVariable(aExpression[2], aBoundVariables, aCollectedItems)));
            }

            if (aExpression.Matches(TrueThat))
            {
                return(aCollectedItems.AddNullPredicate(aExpression[0]));
            }

            if (aExpression.Matches(IsTheSameAs))
            {
                return(aCollectedItems.AddIdentification(
                           FindVariable(aExpression[0], aBoundVariables, aCollectedItems),
                           FindVariable(aExpression[2], aBoundVariables, aCollectedItems)));
            }

            if (aExpression.Matches(ThereAreThisManyOfThese))
            {
                return(Factory.ThereAreThisManyOfThese(
                           UInt32.Parse(aExpression.Substring(0, aExpression.Length - 1)),
                           aCollectedItems.AddUnaryPredicate(aExpression[aExpression.Length - 1])));
            }

            if (aExpression.Matches(ThisManyOfTheseAreTrue))
            {
                List <Matrix> lPredicates = new List <Matrix>();
                RegexMatch    lMatch      = ThisManyOfTheseAreTrue.Exec(aExpression);
                for (int i = 0; i < lMatch[2].Length; i++)
                {
                    lPredicates.Add(aCollectedItems.AddNullPredicate(lMatch[2][i]));
                }
                return(Factory.ThisManyOfTheseAreTrue(UInt32.Parse(lMatch[1]), lPredicates));
            }

            if (aExpression.Matches(ParenthesizedExpression))
            {
                return(Parse(
                           aExpression.Substring(1, aExpression.Length - 2).GetSubexpressions(),
                           aCollectedItems,
                           aBoundVariables));
            }

            throw new ParseError("Expected predication or parenthesized expression, found \"{0}\"", aExpression);
        }
Exemplo n.º 7
0
        private static Matrix ParseBinaryOperator(
            string[] aTokens,
            CollectedItems aCollectedItems,
            VariableDictionary aBoundVariables)
        {
            int lNumberOfTokensBeforeBinaryOperator = -1;
            int lHighestPrecedenceFound             = -1;

            for (int i = 0; i < aTokens.Length; i++)
            {
                if (aTokens[i].IsBinaryOperator())
                {
                    int lPrecedence = aTokens[i].PrecedenceOfBinaryOperator();
                    if (lPrecedence >= lHighestPrecedenceFound)
                    {
                        lHighestPrecedenceFound             = lPrecedence;
                        lNumberOfTokensBeforeBinaryOperator = i;
                    }
                }

                if (aTokens[i].IsQuantifier())
                {
                    break;
                }
            }

            string[] lTokensBeforeLastBinaryOperator = aTokens.Subarray(0, lNumberOfTokensBeforeBinaryOperator);
            string[] lTokensAfterLastBinaryOperator  = aTokens.Subarray(
                lNumberOfTokensBeforeBinaryOperator + 1,
                aTokens.Length - lNumberOfTokensBeforeBinaryOperator - 1);
            string lLastBinaryOperator = aTokens[lNumberOfTokensBeforeBinaryOperator];

            if (lTokensBeforeLastBinaryOperator.Length == 0 || lTokensAfterLastBinaryOperator.Length == 0)
            {
                throw new ParseError("Could not parse \"{0}\"", string.Join("", aTokens));
            }

            Matrix lLeft  = Parse(lTokensBeforeLastBinaryOperator, aCollectedItems, aBoundVariables);
            Matrix lRight = Parse(lTokensAfterLastBinaryOperator, aCollectedItems, aBoundVariables);

            if (lLastBinaryOperator.Matches(And))
            {
                return(Factory.And(lLeft, lRight));
            }

            if (lLastBinaryOperator.Matches(Or))
            {
                return(Factory.Or(lLeft, lRight));
            }

            if (lLastBinaryOperator.Matches(OnlyIf))
            {
                return(Factory.OnlyIf(lLeft, lRight));
            }

            if (lLastBinaryOperator.Matches(IfAndOnlyIf))
            {
                return(Factory.IfAndOnlyIf(lLeft, lRight));
            }

            if (lLastBinaryOperator.Matches(Nor))
            {
                return(Factory.Nor(lLeft, lRight));
            }

            if (lLastBinaryOperator.Matches(Xor))
            {
                return(Factory.Xor(lLeft, lRight));
            }

            if (lLastBinaryOperator.Matches(NecessarilyOnlyIf))
            {
                return(Factory.NecessarilyOnlyIf(lLeft, lRight));
            }

            // The program should never reach this point.
            throw new Exception();
        }