// Note: see page 327.
        public StandardizeApartResult GetStandardizeApartResult(ISentence aSentence,
                                                                IStandardizeApartIndexical standardizeApartIndexical)
        {
            var toRename = variableCollector.CollectAllVariables(aSentence);
            IDictionary <Variable, ITerm> renameSubstitution  = new Dictionary <Variable, ITerm>();
            IDictionary <Variable, ITerm> reverseSubstitution = new Dictionary <Variable, ITerm>();

            foreach (var 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[var] = v;
                reverseSubstitution[v]  = var;
            }

            var standardized = substVisitor.Subst(renameSubstitution, aSentence);

            return(new StandardizeApartResult(aSentence, standardized,
                                              renameSubstitution, reverseSubstitution));
        }
Beispiel #2
0
        public object VisitQuantifiedSentence(QuantifiedSentence sentence,
                                              object arg)
        {
            ISentence quantified     = sentence.Quantified;
            var       universalScope = (ISet <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.Quantifier))
            {
                IDictionary <Variable, ITerm> skolemSubst = new Dictionary <Variable, ITerm>();
                foreach (Variable eVar in sentence.Variables)
                {
                    if (universalScope.Count > 0)
                    {
                        // Replace with a Skolem Function
                        var skolemFunctionName = parser.GetFOLDomain()
                                                 .AddSkolemFunction();
                        skolemSubst[eVar] = new Function(skolemFunctionName, new List <ITerm>((IEnumerable <ITerm>)universalScope));
                    }
                    else
                    {
                        // Replace with a Skolem Constant
                        String skolemConstantName = parser.GetFOLDomain()
                                                    .AddSkolemConstant();
                        skolemSubst[eVar] = new Constant(skolemConstantName);
                    }
                }

                ISentence skolemized = substVisitor.Subst(skolemSubst, quantified);
                return(skolemized.Accept(this, arg));
            }

            // Drop universal quantifiers.
            if (Quantifiers.IsForall(sentence.Quantifier))
            {
                // Add to the universal scope so that
                // existential skolemization may be done correctly
                universalScope.UnionWith(sentence.Variables);

                ISentence droppedUniversal = (ISentence)quantified.Accept(this, arg);

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

                return(droppedUniversal);
            }

            // Should not reach here as have already
            // handled the two quantifiers.
            throw new InvalidOperationException("Unhandled Quantifier:" + sentence.Quantifier);
        }
Beispiel #3
0
        // See:
        // http://logic.stanford.edu/classes/cs157/2008/miscellaneous/faq.html#jump165
        // for need for this.
        private void CascadeSubstitution(IDictionary <Variable, ITerm> theta, Variable var, ITerm x)
        {
            theta[var] = x;

            var keys = theta.Keys.ToArray();

            foreach (Variable v in keys)
            {
                ITerm t = theta[v];
                theta[v] = _substVisitor.Subst(theta, t);
            }
        }
Beispiel #4
0
        public object VisitQuantifiedSentence(QuantifiedSentence sentence, object arg)
        {
            ISet <Variable> seenSoFar = (ISet <Variable>)arg;

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

            foreach (Variable v in sentence.Variables)
            {
                // If local variable has be renamed already
                // then I need to come up with own name
                if (seenSoFar.Contains(v))
                {
                    var sV = new Variable(quantifiedIndexical.GetPrefix()
                                          + quantifiedIndexical.GetNextIndex());
                    localSubst[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
            ISentence subst = substVisitor.Subst(localSubst, sentence.Quantified);

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

            var sQuantified = (ISentence)subst.Accept(this, arg);

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