Пример #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());
        }
Пример #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)));
        }
Пример #3
0
 public Object visitQuantifiedSentence(QuantifiedSentence sentence,
                                       Object arg)
 {
     return(new QuantifiedSentence(sentence.getQuantifier(), sentence
                                   .getVariables(), (Sentence)sentence.getQuantified().accept(
                                       this, arg)));
 }
Пример #4
0
        public object visitQuantifiedSentence(QuantifiedSentence sentence, object arg)
        {
            // Ensure I collect quantified variables too
            ISet <Variable> variables = (ISet <Variable>)arg;

            variables.AddAll(sentence.getVariables());

            sentence.getQuantified().accept(this, arg);

            return(sentence);
        }
Пример #5
0
        public virtual object visitQuantifiedSentence(QuantifiedSentence sentence, object arg)
        {
            ICollection <Variable> variables = CollectionFactory.CreateQueue <Variable>();

            foreach (Variable var in sentence.getVariables())
            {
                variables.Add((Variable)var.accept(this, arg));
            }

            return(new QuantifiedSentence(sentence.getQuantifier(), variables, (Sentence)sentence.getQuantified().accept(this, arg)));
        }
Пример #6
0
        public Object visitQuantifiedSentence(QuantifiedSentence sentence,
                                              Object arg)
        {
            // Ensure I collect quantified variables too
            List <Variable> variables = (List <Variable>)arg;

            variables.AddRange(sentence.getVariables());

            sentence.getQuantified().accept(this, arg);

            return(sentence);
        }
Пример #7
0
        public virtual Object visitQuantifiedSentence(QuantifiedSentence sentence,
                                                      Object arg)
        {
            List <Variable> variables = new List <Variable>();

            foreach (Variable var in sentence.getVariables())
            {
                variables.Add((Variable)var.accept(this, arg));
            }

            return(new QuantifiedSentence(sentence.getQuantifier(), variables,
                                          (Sentence)sentence.getQuantified().accept(this, arg)));
        }
Пример #8
0
        public Object visitQuantifiedSentence(QuantifiedSentence sentence,
                                              Object arg)
        {
            List <Variable> seenSoFar = (List <Variable>)arg;

            // Keep track of what I have to subst locally and
            // what my renamed variables will be.
            Dictionary <Variable, Term> localSubst    = new Dictionary <Variable, Term>();
            List <Variable>             replVariables = new List <Variable>();

            foreach (Variable v in sentence.getVariables())
            {
                // If local variable has be renamed already
                // then I need to come up with own name
                if (seenSoFar.Contains(v))
                {
                    Variable sV = new Variable(quantifiedIndexical.getPrefix()
                                               + quantifiedIndexical.getNextIndex());
                    localSubst.Add(v, sV);
                    // Replacement variables should contain new name for variable
                    replVariables.Add(sV);
                }
                else
                {
                    // Not already replaced, this name is good
                    replVariables.Add(v);
                }
            }

            // Apply the local subst
            Sentence subst = substVisitor.subst(localSubst, sentence
                                                .getQuantified());

            // Ensure all my existing and replaced variable
            // names are tracked
            seenSoFar.AddRange(replVariables);

            Sentence sQuantified = (Sentence)subst.accept(this, arg);

            return(new QuantifiedSentence(sentence.getQuantifier(), replVariables,
                                          sQuantified));
        }
        public override Object visitQuantifiedSentence(QuantifiedSentence sentence,
                                                       Object arg)
        {
            Dictionary <Variable, Term> substitution = (Dictionary <Variable, Term>)arg;

            Sentence quantified          = sentence.getQuantified();
            Sentence quantifiedAfterSubs = (Sentence)quantified.accept(this, arg);

            List <Variable> variables = new List <Variable>();

            foreach (Variable v in sentence.getVariables())
            {
                if (substitution.ContainsKey(v))
                {
                    Term st = substitution[v];
                    if (st is Variable)
                    {
                        // Only if it is a variable to I replace it, otherwise
                        // I drop it.
                        variables.Add((Variable)st.copy());
                    }
                }
                else
                {
                    // No substitution for the quantified variable, so
                    // keep it.
                    variables.Add((Variable)v.copy());
                }
            }

            // If not variables remaining on the quantifier, then drop it
            if (variables.Count == 0)
            {
                return(quantifiedAfterSubs);
            }

            return(new QuantifiedSentence(sentence.getQuantifier(), variables,
                                          quantifiedAfterSubs));
        }
Пример #10
0
        public override object visitQuantifiedSentence(QuantifiedSentence sentence, object arg)
        {
            IMap <Variable, Term> substitution = (IMap <Variable, Term>)arg;

            Sentence quantified          = sentence.getQuantified();
            Sentence quantifiedAfterSubs = (Sentence)quantified.accept(this, arg);

            ICollection <Variable> variables = CollectionFactory.CreateQueue <Variable>();

            foreach (Variable v in sentence.getVariables())
            {
                Term st = substitution.Get(v);
                if (null != st)
                {
                    if (st is Variable)
                    {
                        // Only if it is a variable to I replace it, otherwise
                        // I drop it.
                        variables.Add((Variable)st.copy());
                    }
                }
                else
                {
                    // No substitution for the quantified variable, so
                    // keep it.
                    variables.Add(v.copy());
                }
            }

            // If not variables remaining on the quantifier, then drop it
            if (variables.Size() == 0)
            {
                return(quantifiedAfterSubs);
            }

            return(new QuantifiedSentence(sentence.getQuantifier(), variables, quantifiedAfterSubs));
        }
Пример #11
0
        public Object visitQuantifiedSentence(QuantifiedSentence sentence,
                                              Object arg)
        {
            Sentence        quantified     = sentence.getQuantified();
            List <Variable> universalScope = (List <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()))
            {
                Dictionary <Variable, Term> skolemSubst = new Dictionary <Variable, Term>();
                foreach (Variable eVar in sentence.getVariables())
                {
                    if (universalScope.Count > 0)
                    {
                        // Replace with a Skolem Function
                        String skolemFunctionName = parser.getFOLDomain()
                                                    .addSkolemFunction();
                        skolemSubst.Add(eVar, new Function(skolemFunctionName,
                                                           new List <Term>(universalScope)));
                    }
                    else
                    {
                        // Replace with a Skolem Constant
                        String skolemConstantName = parser.getFOLDomain()
                                                    .addSkolemConstant();
                        skolemSubst.Add(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.AddRange(sentence.getVariables());

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

                // Enusre my scope is removed before moving back up
                // the call stack when returning
                foreach (Variable s in sentence.getVariables())
                {
                    universalScope.Remove(s);
                }

                return(droppedUniversal);
            }

            // Should not reach here as have already
            // handled the two quantifiers.
            throw new ApplicationException("Unhandled Quantifier:"
                                           + sentence.getQuantifier());
        }