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());
        }
        public Object visitNotSentence(NotSentence notSentence, Object arg)
        {
            // CNF requires NOT (~) to appear only in literals, so we 'move ~
            // inwards' by repeated application of the following equivalences:
            Sentence negated = notSentence.getNegated();

            // ~(~alpha) equivalent to alpha (double negation elimination)
            if (negated is NotSentence)
            {
                return(((NotSentence)negated).getNegated().accept(this, arg));
            }

            if (negated is ConnectedSentence)
            {
                ConnectedSentence negConnected = (ConnectedSentence)negated;
                Sentence          alpha        = negConnected.getFirst();
                Sentence          beta         = negConnected.getSecond();
                // ~(alpha ^ beta) equivalent to (~alpha V ~beta) (De Morgan)
                if (Connectors.isAND(negConnected.getConnector()))
                {
                    // I need to ensure the ~s are moved in deeper
                    Sentence notAlpha = (Sentence)(new NotSentence(alpha)).accept(
                        this, arg);
                    Sentence notBeta = (Sentence)(new NotSentence(beta)).accept(
                        this, arg);
                    return(new ConnectedSentence(Connectors.OR, notAlpha, notBeta));
                }

                // ~(alpha V beta) equivalent to (~alpha ^ ~beta) (De Morgan)
                if (Connectors.isOR(negConnected.getConnector()))
                {
                    // I need to ensure the ~s are moved in deeper
                    Sentence notAlpha = (Sentence)(new NotSentence(alpha)).accept(
                        this, arg);
                    Sentence notBeta = (Sentence)(new NotSentence(beta)).accept(
                        this, arg);
                    return(new ConnectedSentence(Connectors.AND, notAlpha, notBeta));
                }
            }

            // in addition, rules for negated quantifiers:
            if (negated is QuantifiedSentence)
            {
                QuantifiedSentence negQuantified = (QuantifiedSentence)negated;
                // I need to ensure the ~ is moved in deeper
                Sentence notP = (Sentence)(new NotSentence(negQuantified
                                                           .getQuantified())).accept(this, arg);

                // ~FORALL x p becomes EXISTS x ~p
                if (Quantifiers.isFORALL(negQuantified.getQuantifier()))
                {
                    return(new QuantifiedSentence(Quantifiers.EXISTS, negQuantified
                                                  .getVariables(), notP));
                }

                // ~EXISTS x p becomes FORALL x ~p
                if (Quantifiers.isEXISTS(negQuantified.getQuantifier()))
                {
                    return(new QuantifiedSentence(Quantifiers.FORALL, negQuantified
                                                  .getVariables(), notP));
                }
            }

            return(new NotSentence((Sentence)negated.accept(this, arg)));
        }