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()); }
public Literal createAnswerLiteral(Sentence forQuery) { String alName = parser.getFOLDomain().addAnswerLiteral(); List <Term> terms = new List <Term>(); List <Variable> vars = variableCollector.collectAllVariables(forQuery); foreach (Variable v in vars) { // Ensure copies of the variables are used. terms.Add((Term)v.copy()); } return(new Literal(new Predicate(alName, terms))); }
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()); }