Ejemplo n.º 1
0
        // Note: see page 327.
        public StandardizeApartResult standardizeApart(Sentence sentence,
                                                       StandardizeApartIndexical standardizeApartIndexical)
        {
            List <Variable>             toRename            = variableCollector.collectAllVariables(sentence);
            Dictionary <Variable, Term> renameSubstitution  = new Dictionary <Variable, Term>();
            Dictionary <Variable, Term> reverseSubstitution = new Dictionary <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.Add(var, v);
                reverseSubstitution.Add(v, var);
            }
            Sentence standardized = substVisitor.subst(renameSubstitution,
                                                       sentence);

            return(new StandardizeApartResult(sentence, standardized,
                                              renameSubstitution, reverseSubstitution));
        }
Ejemplo n.º 2
0
        // See:
        // http://logic.stanford.edu/classes/cs157/2008/miscellaneous/faq.html#jump165
        // for need for this.
        private void cascadeSubstitution(Dictionary <Variable, Term> theta, Variable var,
                                         Term x)
        {
            theta.Add(var, x);
            List <Variable> thetaKeys = theta.Keys.ToList <Variable>();

            foreach (Variable v in thetaKeys)
            {
                Term t = theta[v];
                if (theta.ContainsKey(v))
                {
                    theta[v] = _substVisitor.subst(theta, t);
                }
                else
                {
                    theta.Add(v, _substVisitor.subst(theta, t));
                }
            }
        }
        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 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());
        }