예제 #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
 // See:
 // http://logic.stanford.edu/classes/cs157/2008/miscellaneous/faq.html#jump165
 // for need for this.
 private IMap <Variable, Term> cascadeSubstitution(IMap <Variable, Term> theta, Variable var, Term x)
 {
     theta.Put(var, x);
     foreach (Variable v in theta.GetKeys())
     {
         theta.Put(v, _substVisitor.subst(theta, theta.Get(v)));
     }
     // Ensure Function Terms are correctly updates by passing over them
     // again. Fix for testBadCascadeSubstitution_LCL418_1()
     foreach (Variable v in theta.GetKeys())
     {
         Term t = theta.Get(v);
         if (t is Function)
         {
             theta.Put(v, _substVisitor.subst(theta, t));
         }
     }
     return(theta);
 }
예제 #3
0
        // Note: see page 327.
        public StandardizeApartResult standardizeApart(Sentence sentence, StandardizeApartIndexical standardizeApartIndexical)
        {
            ISet <Variable>       toRename            = variableCollector.collectAllVariables(sentence);
            IMap <Variable, Term> renameSubstitution  = CollectionFactory.CreateInsertionOrderedMap <Variable, Term>();
            IMap <Variable, Term> reverseSubstitution = CollectionFactory.CreateInsertionOrderedMap <Variable, Term>();

            foreach (Variable var in toRename)
            {
                Variable v = null;
                do
                {
                    v = new Variable(standardizeApartIndexical.getPrefix() + standardizeApartIndexical.getNextIndex());
                    // Ensure the new variable name is not already
                    // accidentally used in the sentence
                } while (toRename.Contains(v));

                renameSubstitution.Put(var, v);
                reverseSubstitution.Put(v, var);
            }

            Sentence standardized = substVisitor.subst(renameSubstitution, sentence);

            return(new StandardizeApartResult(sentence, standardized, renameSubstitution, reverseSubstitution));
        }
예제 #4
0
        public object visitQuantifiedSentence(QuantifiedSentence sentence, object arg)
        {
            ISet <Variable> seenSoFar = (Set <Variable>)arg;

            // Keep track of what I have to subst locally and
            // what my renamed variables will be.
            IMap <Variable, Term>  localSubst    = CollectionFactory.CreateInsertionOrderedMap <Variable, Term>();
            ICollection <Variable> replVariables = CollectionFactory.CreateQueue <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.Put(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.AddAll(replVariables);

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

            return(new QuantifiedSentence(sentence.getQuantifier(), replVariables, sQuantified));
        }