Beispiel #1
0
        public object visitQuantifiedSentence(QuantifiedSentence sentence, object arg)
        {
            Sentence        quantified     = sentence.getQuantified();
            ISet <Variable> universalScope = (Set <Variable>)arg;

            // Skolemize: Skolemization is the process of removing existential
            // quantifiers by elimination. This is done by introducing Skolem
            // functions. The general rule is that the arguments of the Skolem
            // function are all the universally quantified variables in whose
            // scope the existential quantifier appears.
            if (Quantifiers.isEXISTS(sentence.getQuantifier()))
            {
                IMap <Variable, Term> skolemSubst = CollectionFactory.CreateInsertionOrderedMap <Variable, Term>();
                foreach (Variable eVar in sentence.getVariables())
                {
                    if (universalScope.Size() > 0)
                    {
                        // Replace with a Skolem Function
                        string skolemFunctionName = parser.getFOLDomain().addSkolemFunction();

                        ICollection <Term> terms = CollectionFactory.CreateQueue <Term>();
                        foreach (Variable v in universalScope)
                        {
                            terms.Add(v);
                        }
                        skolemSubst.Put(eVar, new Function(skolemFunctionName, terms));
                    }
                    else
                    {
                        // Replace with a Skolem Constant
                        string skolemConstantName = parser.getFOLDomain().addSkolemConstant();
                        skolemSubst.Put(eVar, new Constant(skolemConstantName));
                    }
                }

                Sentence skolemized = substVisitor.subst(skolemSubst, quantified);
                return(skolemized.accept(this, arg));
            }

            // Drop universal quantifiers.
            if (Quantifiers.isFORALL(sentence.getQuantifier()))
            {
                // Add to the universal scope so that
                // existential skolemization may be done correctly
                universalScope.AddAll(sentence.getVariables());

                Sentence droppedUniversal = (Sentence)quantified.accept(this, arg);

                // Enusre my scope is removed before moving back up
                // the call stack when returning
                universalScope.RemoveAll(sentence.getVariables());

                return(droppedUniversal);
            }

            // Should not reach here as have already
            // handled the two quantifiers.
            throw new IllegalStateException("Unhandled Quantifier:" + sentence.getQuantifier());
        }
Beispiel #2
0
        public object visitNotSentence(NotSentence notSentence, object arg)
        {
            // CNF requires NOT (~) to appear only in literals, so we 'move ~
            // inwards' by repeated application of the following equivalences:
            Sentence negated = notSentence.getNegated();

            // ~(~alpha) equivalent to alpha (double negation elimination)
            if (negated is NotSentence)
            {
                return(((NotSentence)negated).getNegated().accept(this, arg));
            }

            if (negated is ConnectedSentence)
            {
                ConnectedSentence negConnected = (ConnectedSentence)negated;
                Sentence          alpha        = negConnected.getFirst();
                Sentence          beta         = negConnected.getSecond();
                // ~(alpha ^ beta) equivalent to (~alpha V ~beta) (De Morgan)
                if (Connectors.isAND(negConnected.getConnector()))
                {
                    // I need to ensure the ~s are moved in deeper
                    Sentence notAlpha = (Sentence)(new NotSentence(alpha)).accept(this, arg);
                    Sentence notBeta  = (Sentence)(new NotSentence(beta)).accept(this, arg);
                    return(new ConnectedSentence(Connectors.OR, notAlpha, notBeta));
                }

                // ~(alpha V beta) equivalent to (~alpha ^ ~beta) (De Morgan)
                if (Connectors.isOR(negConnected.getConnector()))
                {
                    // I need to ensure the ~s are moved in deeper
                    Sentence notAlpha = (Sentence)(new NotSentence(alpha)).accept(this, arg);
                    Sentence notBeta  = (Sentence)(new NotSentence(beta)).accept(this, arg);
                    return(new ConnectedSentence(Connectors.AND, notAlpha, notBeta));
                }
            }

            // in addition, rules for negated quantifiers:
            if (negated is QuantifiedSentence)
            {
                QuantifiedSentence negQuantified = (QuantifiedSentence)negated;
                // I need to ensure the ~ is moved in deeper
                Sentence notP = (Sentence)(new NotSentence(negQuantified.getQuantified())).accept(this, arg);

                // ~FORALL x p becomes EXISTS x ~p
                if (Quantifiers.isFORALL(negQuantified.getQuantifier()))
                {
                    return(new QuantifiedSentence(Quantifiers.EXISTS, negQuantified.getVariables(), notP));
                }

                // ~EXISTS x p becomes FORALL x ~p
                if (Quantifiers.isEXISTS(negQuantified.getQuantifier()))
                {
                    return(new QuantifiedSentence(Quantifiers.FORALL, negQuantified.getVariables(), notP));
                }
            }

            return(new NotSentence((Sentence)negated.accept(this, arg)));
        }