Example #1
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);
        }
Example #2
0
        public Literal CreateAnswerLiteral(ISentence forQuery)
        {
            string        alName = parser.GetFOLDomain().AddAnswerLiteral();
            IList <ITerm> terms  = new List <ITerm>();

            ISet <Variable> vars = variableCollector.CollectAllVariables(forQuery);

            foreach (Variable v in vars)
            {
                // Ensure copies of the variables are used.
                terms.Add((ITerm)v.Copy());
            }

            return(new Literal(new Predicate(alName, terms)));
        }