示例#1
0
void CNFMgr::cleanup(const ASTNode& varphi)
{
    delete info[varphi]->clausespos;
    CNFInfo* toDelete = info[varphi]; // get the thing to delete.
    info.erase(varphi);               // remove it from the hashtable
    delete toDelete;


    ASTNodeToCNFInfoMap::const_iterator it1 = info.begin();
    for (; it1 != info.end(); it1++)
    {
        CNFInfo* x = it1->second;
        if (x->clausespos != NULL)
        {
            DELETE(x->clausespos);
        }
        if (x->clausesneg != NULL)
        {
            if (!isTerm(*x))
            {
                DELETE(x->clausesneg);
            }
        }
        delete x;
    }

    info.clear();
} //End of cleanup()
示例#2
0
static int
get_create_dict_ex(term_t t, term_t dt ARG_LD)
{ Word p = valTermRef(t);

  deRef(p);
  if ( isTerm(*p) )
  { Functor f = valueTerm(*p);
    FunctorDef fd = valueFunctor(f->definition);

    if ( fd->name == ATOM_dict &&
	 fd->arity%2 == 1 )		/* does *not* validate ordering */
    { *valTermRef(dt) = *p;
      return TRUE;
    }
  }

  if ( PL_get_dict_ex(t, 0, dt, DICT_GET_ALL) )
  { assert(isTerm(*valTermRef(dt)));
    return TRUE;
  }

  return PL_type_error("dict", t);
}
示例#3
0
void
MPTreeMgr::readNodeList( ifstream & input )
{
	string temp;
	Token token;

	token.reserve( 8 );
	while ( getline( input , temp ) ) {
		if ( !token.empty() ) token.clear();
		Nz_ParseToken( temp , token );
		if ( token.empty() ) continue;
		if ( isNode( token ) ) readOneNode( token );
		if ( isTerm( token ) ) readOneTerm( token );
	}
}
示例#4
0
static void
unshare_attvar(Word p ARG_LD)
{ for(;;)
  { deRef(p);

    if ( isTerm(*p) )
    { Functor t = valueTerm(*p);
      word fd = (t->definition & ~BOTH_MASK);

      if ( fd == FUNCTOR_att3 )
      {	t->definition = fd | MARK_MASK;
	p = &t->arguments[2];
      }
    } else
    { break;
    }
  }
}
示例#5
0
static int
get_dict_ex(term_t t, Word dp, int ex ARG_LD)
{ Word p = valTermRef(t);

  deRef(p);
  if ( isTerm(*p) )
  { Functor f = valueTerm(*p);
    FunctorDef fd = valueFunctor(f->definition);

    if ( fd->name == ATOM_dict &&
	 fd->arity%2 == 1 )		/* does *not* validate ordering */
    { *dp = *p;
      return TRUE;
    }
  }

  if ( !ex )
    return FALSE;

  PL_type_error("dict", t);
  return FALSE;
}
示例#6
0
文件: semantic.cpp 项目: ombt/ombt
// remove existential quantifier by using skolem functions. all skolem
// functions must be unique. remember that the skolem function is dependent
// on any universal variables that are in scope.
//
int
Semantic::skolemize(List<Symbol> &localscope)
{
	// check if expression or predicate
	if (isExpression())
	{
		// get expression
		Expression *pe = (Expression *)prep;

		// what type is it
		if (pe->type == Expression::Universal)
		{
			// store universal variable
			localscope.insertAtEnd(Symbol(pe->name, 
				Symbol::UniversalVariable));

			// follow right leg, left leg is null
			MustBeTrue(pe->left == NULL && pe->right != NULL);
			if (pe->right->skolemize(localscope) != OK)
			{
				ERROR("skolemize failed.", EINVAL);
				return(NOTOK);
			}

			// remove universal variable
			Symbol tmp;
			localscope.removeAtEnd(tmp);
		}
		else if (pe->type == Expression::Existential)
		{
			// store existential variable
			String uname = uniqueName(String("_SK"));
			localscope.insertAtEnd(Symbol(pe->name, uname,
				Symbol::ExistentialVariable));
			if (updateVariableNames(pe->name, uname) != OK)
			{
				ERROR("update variable names failed.", 
					EINVAL);
				return(NOTOK);
			}

			// follow right leg, left leg is null
			MustBeTrue(pe->left == NULL && pe->right != NULL);
			if (pe->right->skolemize(localscope) != OK)
			{
				ERROR("skolemize failed.", EINVAL);
				return(NOTOK);
			}

			// remove existential variable
			Symbol tmp;
			localscope.removeAtEnd(tmp);
		}
		else
		{
			// follow down other expression operators
			if (pe->left != NULL && 
			    pe->left->skolemize(localscope) != OK)
			{
				ERROR("skolemize failed.", EINVAL);
				return(NOTOK);
			}
			if (pe->right != NULL && 
			    pe->right->skolemize(localscope) != OK)
			{
				ERROR("skolemize failed.", EINVAL);
				return(NOTOK);
			}
		}
		
	}
	else if (isPredicate())
	{
		// get predicate
		Predicate *pp = (Predicate *)prep;

		// check for functions
		if ((pp->type == Predicate::Function) ||
		    (pp->type == Predicate::Equal))
		{
			// cycle thru arguments
			ListIterator<Semantic * > pargsIter(*pp->pargs);
			for ( ; !pargsIter.done(); pargsIter++)
			{
				Semantic *parg = pargsIter();
				if (parg != NULL && 
				    parg->skolemize(localscope) != OK)
				{
					ERROR("skolemize failed.", EINVAL);
					return(NOTOK);
				}
			}
		}
	}
	else if (isTerm())
	{
		// check type of argument
		Term *pa = (Term *)prep;
		switch (pa->type)
		{
		case Term::Variable:
		{
			// check if an existential variable
			Symbol qvarsym(pa->name);
			if(localscope.retrieve(qvarsym) != OK)
				break;
			if (qvarsym.getType() != Symbol::ExistentialVariable)
				break;

			// we have an existential variable
			String skolemName(qvarsym.getUniqueName());

			// need to replace this variable with a 
			// skolem function which is dependent on all
			// universal variables in scope at this time.
			//
			List<Semantic * > *pargs = new List<Semantic * >;
			MustBeTrue(pargs != NULL);
			ListIterator<Symbol> scopeIter(localscope);
			int nargs;
			for (nargs = 0; !scopeIter.done(); scopeIter++)
			{
				// get symbol
				Symbol uvar = scopeIter();

				// check if we found the current 
				// symbol. this marks the end of 
				// dependent variables for the 
				// skolem function. all other
				// existential variables are skipped.
				//
				if (uvar.getType() == 
					Symbol::ExistentialVariable)
				{
					if (uvar == Symbol(pa->name))
						break;
					else
						continue;
				}

				// we have a universal variable in
				// scope
				//
				Semantic *parg = new Semantic(
					Term::Variable, uvar.getName());
				MustBeTrue(parg != NULL);
				pargs->insertAtEnd(parg);
				nargs++;
			}
			if (nargs == 0)
			{
				// skolem constant
				pa->type = Term::Constant;
				pa->name = skolemName;
				pa->pargs = NULL;
				pa->argnum = 0;

				// delete unused argument list
				delete pargs;
			}
			else
			{
				// skolem function
				pa->type = Term::Function;
				pa->name = skolemName;
				pa->pargs = pargs;
				pa->argnum = nargs;
			}
			break;
		}

		case Term::Function:
		{
			// we have a function, scan its arguments
			ListIterator<Semantic *> pargsIter(*pa->pargs);
			for ( ; !pargsIter.done(); pargsIter++)
			{
				Semantic *parg = pargsIter();
				if (parg != NULL && 
				    parg->skolemize(localscope) != OK)
				{
					ERROR("skolemize failed.", EINVAL);
					return(NOTOK);
				}
			}
			break;
		}
		}
	}
	else
	{
		MustBeTrue(0);
	}

	// all done
	return(OK);
}
示例#7
0
文件: semantic.cpp 项目: ombt/ombt
// rename all variable names to unique names.
int
Semantic::renameVariables(List<Symbol> &localscope)
{
	// check of predicate or expression
	if (isExpression())
	{
		// get expression record
		Expression *pe = (Expression *)prep;

		// check if we have quantifier
		int popscope = 0;
		if (pe->type == Expression::Universal)
		{
			// we have a quantifier, rename variable
			popscope = 1;
			String uname = uniqueName(String("_V"));
			localscope.insertAtFront(
				Symbol(pe->name, uname, 
				Symbol::UniversalVariable));

			// change name in semantic record
			if (updateVariableNames(pe->name, uname) != OK)
			{
				ERROR("update variable names failed.", EINVAL);
				return(NOTOK);
			}
			pe->name = uname;
		}
		else if (pe->type == Expression::Existential)
		{
			// we have a quantifier, rename variable
			popscope = 1;
			String uname = uniqueName(String("_V"));
			localscope.insertAtFront(
				Symbol(pe->name, uname, 
				Symbol::ExistentialVariable));

			// change name in semantic record
			if (updateVariableNames(pe->name, uname) != OK)
			{
				ERROR("update variable names failed.", EINVAL);
				return(NOTOK);
			}
			pe->name = uname;
		}

		// follow left and right branches
		if (pe->left != NULL && 
		    pe->left->renameVariables(localscope) != OK) 
		{
			ERROR("renameVariables failed.", EINVAL);
			return(NOTOK);
		}
		if (pe->right != NULL && 
		    pe->right->renameVariables(localscope) != OK)
		{
			ERROR("renameVariables failed.", EINVAL);
			return(NOTOK);
		}

		// pop scope variable if required
		if (popscope)
		{
			Symbol tmp;
			MustBeTrue(localscope.removeAtFront(tmp) == OK);
		}
	}
	else if (isPredicate())
	{
		// get predicate record
		Predicate *pp = (Predicate *)prep;

		// check if a function
		if ((pp->type == Predicate::Function) ||
		    (pp->type == Predicate::Equal))
		{
			// we have a function, scan argument list
			ListIterator<Semantic *> pargsIter(*pp->pargs);
			for ( ; !pargsIter.done(); pargsIter++)
			{
				Semantic *parg = pargsIter();
				if (parg != NULL && 
				    parg->renameVariables(localscope) != OK)
				{
					ERROR("renameVariables failed.", 
						EINVAL);
					return(NOTOK);
				}
			}
		}
	}
	else if (isTerm())
	{
		// check if a variable, function or anything else
		Term *pa = (Term *)prep;
		if (pa->type == Term::Variable)
		{
			// find variable in scope
			Symbol usym(pa->name);
			if (localscope.retrieve(usym) == OK)
			{
				pa->name = usym.getUniqueName();
				MustBeTrue(pa->name != String(""));
			}
		}
		else if (pa->type == Term::Function)
		{
			// we have a function, scan argument list
			ListIterator<Semantic *> pargsIter(*pa->pargs);
			for ( ; !pargsIter.done(); pargsIter++)
			{
				Semantic *parg = pargsIter();
				if (parg != NULL && 
				    parg->renameVariables(localscope) != OK)
				{
					ERROR("renameVariables failed.", 
						EINVAL);
					return(NOTOK);
				}
			}
		}
	}
	else
	{
		MustBeTrue(0);
	}

	// all done
	return(OK);
}
示例#8
0
// Logic:
// - If it is a number then just add it to the actions vector (but adding two
// times a number is not allowed as "3 4 6 =" is not a correct syntax. "3 + 6 =" would be correct.)
// - If it is an expression-operation (like "3+4-") then do the operation with the operation before that
// ("3+4" in the example) and add it to "left expression" (m_leftExpression).
// - If it is a term-operation (like 3+4x) add the left number (3) to m_leftExpression and
// the right handside number (4) of the expression operation to m_leftTerm.
// - "=" and "None" as an operation means that a totally new calculation started from there,
// meaning that the first number after them are just assigned to "left expression" (m_leftExpression).
// After "=" and "None" the first temporary results (untill the next expression operator)
// go to m_leftTerm and m_leftExpression remains zero.
// return value: Returns true if input was valid otherwise false
bool Calculator::addInput(const Action& input)
{
    const Calculator::Action lastInput = getLastInput();

    if (input.actionType == ActionType::Number)
    {
        // adding a number after a number would be an error -> that entry is ignored
        if (lastInput.actionType != ActionType::Number)
            m_actions.push_back(input);
    }
    else if (isOperation(input.actionType))
    {
        if (lastInput.actionType == ActionType::Number)
        {
            ActionType lastOperation = getLastOperation();
            switch (lastOperation)
            {
            case ActionType::Plus:
                if (isExpression(input.actionType) || input.actionType == ActionType::Equals)
                {
                    // "3 + 4 -", "3 + 4 ="
                    m_leftExpression.add(lastInput.value);
                    m_leftTerm.reset();
                }
                else if (isTerm(input.actionType))
                {
                    // "3 + 4 x",
                    m_leftTerm.set(lastInput.value);
                }
                break;
            case ActionType::Minus:
                if (isExpression(input.actionType) || input.actionType == ActionType::Equals)
                {
                    // "3 - 4 -", "3 - 4 ="
                    m_leftExpression.add(-lastInput.value);
                    m_leftTerm.reset();
                }
                else if (isTerm(input.actionType))
                {
                    // "3 - 4 x",
                    m_leftTerm.set(-lastInput.value);
                }
                break;
            case ActionType::Multiply:
                if (isExpression(input.actionType) || input.actionType == ActionType::Equals)
                {
                    // "3 x 4 +", "3 x 4 ="
                    m_leftExpression.add(m_leftTerm.getValue() * lastInput.value);
                    m_leftTerm.reset();
                }
                else if (isTerm(input.actionType)) // "3 x 4 x"
                    m_leftTerm.multiplyBy(lastInput.value);
                break;
            case ActionType::Divide:
                if (isExpression(input.actionType) || input.actionType == ActionType::Equals)
                {
                    if (lastInput.value == 0.0)
                    {
                        CalculatorException divByZeroException("Error: Cannot Divide By Zero",
                                                               CalculatorException::ExceptionType::DividedByZero);
                        throw divByZeroException;
                    }
                    else
                    {
                        // "3 / 4 +", "3 / 4 ="
                        m_leftExpression.add(m_leftTerm.getValue() / lastInput.value);
                        m_leftTerm.reset();
                    }
                }
                else if (isTerm(input.actionType)) // "3 / 4 x"
                    m_leftTerm.multiplyBy(1.0 / lastInput.value);
                break;
            case ActionType::Equals: // "=" is the start of a new beginnning, see (h: *)
                if (isTerm(input.actionType))
                {
                    // "= 3 x "
                    m_leftExpression.reset();
                    m_leftTerm.set(lastInput.value);
                }
                else if (isExpression(input.actionType))
                {
                    // "= 3 + "
                    m_leftExpression.set(lastInput.value);
                    m_leftTerm.reset();
                }
                break;
            case ActionType::None: // "None" is the start of a new beginnning, see (h: *)
                if (isTerm(input.actionType))
                {
                    // "3 x "
                    m_leftExpression.reset();
                    m_leftTerm.set(lastInput.value);
                }
                else if (isExpression(input.actionType))
                {
                    // "3 + "
                    m_leftExpression.set(lastInput.value);
                    m_leftTerm.reset();
                }
                break;
            }
            m_actions.push_back(input);
            return true;
        }
    }
    return false;
}
示例#9
0
void CNFMgr::convertFormulaToCNF(const ASTNode& varphi, ClauseList* defs)
{
    CNFInfo* x = info[varphi];

    //########################################
    // divert to special case if term (word-level cnf)

    if (isTerm(*x))
    {
        convertTermForCNF(varphi, defs);
        setWasVisited(*x);
        return;
    }

    //########################################
    // do work

    if (sharesPos(*x) > 0 && !wasVisited(*x))
    {
        convertFormulaToCNFPosCases(varphi, defs);
    }

#if defined CRYPTOMINISAT__2
    if ((x->clausespos != NULL
            && (x->clausespos->size() > 1
                || (renameAllSiblings
                    && !(x->clausespos->size() == 1 && x->clausespos[0].size() ==1)
                    && !wasRenamedPos(*x)))
            || (doRenamePos(*x)
                && !wasVisited(*x))))
    {
        if (doSibRenamingPos(*x)
                || sharesPos(*x) > 1
                || doRenamePos(*x)
                || renameAllSiblings)
        {
            doRenamingPos(varphi, defs);
        }
    }
#else


    if (x->clausespos != NULL
            && (x->clausespos->size() > 1
                || (renameAllSiblings
                    && !(x->clausespos->size() == 1 && x->clausespos[0].size() ==1)
                    && !wasRenamedNeg(*x))))
    {
        if (doSibRenamingPos(*x)
                || sharesPos(*x) > 1
                || renameAllSiblings)
        {
            doRenamingPos(varphi, defs);
        }
    }
#endif

    if (sharesNeg(*x) > 0 && !wasVisited(*x))
    {
        convertFormulaToCNFNegCases(varphi, defs);
    }

    if (x->clausesneg != NULL
            && (x->clausesneg->size() > 1
                || (renameAllSiblings
                    && !(x->clausesneg->size() == 1 && x->clausesneg[0].size() ==1)
                    && !wasRenamedNeg(*x))))
    {
        if (doSibRenamingNeg(*x)
                || sharesNeg(*x) > 1
                || renameAllSiblings)
        {
            doRenamingNeg(varphi, defs);
        }
    }

    //########################################
    //mark that we've already done the hard work

    if(renameAllSiblings)
    {
        assert(info[varphi]->clausesneg == NULL
               || info[varphi]->clausesneg->size() ==1);

        assert(info[varphi]->clausespos == NULL
               || info[varphi]->clausespos->size() ==1);
    }

    setWasVisited(*x);
} //End of convertFormulaToCNF()