/********************* */ /*! \file macros.cpp ** \verbatim ** Original author: Andrew Reynolds ** Major contributors: Morgan Deters ** Minor contributors (to current version): Kshitij Bansal ** This file is part of the CVC4 project. ** Copyright (c) 2009-2014 New York University and The University of Iowa ** See the file COPYING in the top-level source directory for licensing ** information.\endverbatim ** ** \brief Sort inference module ** ** This class implements quantifiers macro definitions. **/ #include #include "theory/quantifiers/macros.h" #include "theory/rewriter.h" using namespace CVC4; using namespace std; using namespace CVC4::theory; using namespace CVC4::theory::quantifiers; using namespace CVC4::kind; using namespace CVC4::context; bool QuantifierMacros::simplify( std::vector< Node >& assertions, bool doRewrite ){ Trace("macros") << "Find macros..." << std::endl; //first, collect macro definitions for( size_t i=0; i args; for( size_t j=0; j& candidates ){ if( n.getKind()==APPLY_UF ){ if( isBoundVarApplyUf( n ) ){ candidates.push_back( n ); } }else if( n.getKind()==PLUS ){ for( size_t i=0; i plus_children; //find monomial with n for( size_t j=0; jmkConst( Rational(1) ); }else if( lit[1][j].getKind()==MULT && lit[1][j][1]==n ){ Assert( coeff.isNull() ); Assert( lit[1][j][0].isConst() ); coeff = lit[1][j][0]; }else{ plus_children.push_back( lit[1][j] ); } } if( !coeff.isNull() ){ term = NodeManager::currentNM()->mkNode( PLUS, plus_children ); term = NodeManager::currentNM()->mkNode( MINUS, lit[0], term ); } } if( !coeff.isNull() ){ coeff = NodeManager::currentNM()->mkConst( Rational(1) / coeff.getConst() ); term = NodeManager::currentNM()->mkNode( MULT, coeff, term ); term = Rewriter::rewrite( term ); return term; } } } Trace("macros-debug") << "Cannot find for " << lit << " " << n << std::endl; return Node::null(); } bool QuantifierMacros::getFreeVariables( Node n, std::vector< Node >& v_quant, std::vector< Node >& vars, bool retOnly ){ if( std::find( v_quant.begin(), v_quant.end(), n )!=v_quant.end() ){ if( std::find( vars.begin(), vars.end(), n )==vars.end() ){ if( retOnly ){ return true; }else{ vars.push_back( n ); } } } for( size_t i=0; i& v_quant, std::map< Node, Node >& solved, std::vector< Node >& vars, std::vector< Node >& subs, bool reqComplete ){ bool success = true; for( size_t a=0; a& args, Node f ){ if( n.getKind()==NOT ){ process( n[0], !pol, args, f ); }else if( n.getKind()==AND || n.getKind()==OR ){ //bool favorPol = (n.getKind()==AND)==pol; //conditional? }else if( n.getKind()==ITE ){ //can not do anything }else if( n.getKind()==APPLY_UF ){ //predicate case if( isBoundVarApplyUf( n ) ){ Node n_def = NodeManager::currentNM()->mkConst( pol ); Trace("macros-quant") << "Macro found for " << f << std::endl; Trace("macros") << "* " << n_def << " is a macro for " << n.getOperator() << std::endl; d_macro_defs[ n.getOperator() ] = n_def; } }else{ //literal case if( isMacroLiteral( n, pol ) ){ std::vector< Node > candidates; for( size_t i=0; i fvs; getFreeVariables( m, args, fvs, false ); //get definition and condition Node n_def = solveInEquality( m, n ); //definition for the macro //definition must exist and not contain any free variables apart from fvs if( !n_def.isNull() && !getFreeVariables( n_def, args, fvs, true ) && !containsBadOp( n_def, op ) ){ Node n_cond; //condition when this definition holds //conditional must not contain any free variables apart from fvs if( n_cond.isNull() || !getFreeVariables( n_cond, args, fvs, true ) ){ Trace("macros-debug") << m << " is possible macro in " << f << std::endl; //now we must rewrite candidates[i] to a term of form g( x1, ..., xn ) where // x1 ... xn are distinct variables if( d_macro_basis[op].empty() ){ for( size_t a=0; amkSkolem( ss.str(), m[a].getType(), "created during macro definition recognition" ); d_macro_basis[op].push_back( v ); } } std::map< Node, Node > solved; for( size_t a=0; a vars; std::vector< Node > subs; if( getSubstitution( fvs, solved, vars, subs, true ) ){ n_def = n_def.substitute( vars.begin(), vars.end(), subs.begin(), subs.end() ); Trace("macros-quant") << "Macro found for " << f << std::endl; Trace("macros") << "* " << n_def << " is a macro for " << op << std::endl; d_macro_defs[op] = n_def; return; } } } } } } } } Node QuantifierMacros::simplify( Node n ){ Trace("macros-debug") << "simplify " << n << std::endl; std::vector< Node > children; bool childChanged = false; for( size_t i=0; i >::iterator it = d_macro_basis.find( op ); Node ret = d_macro_defs[op]; if( it!=d_macro_basis.end() ){ ret = ret.substitute( it->second.begin(), it->second.end(), children.begin(), children.end() ); } return ret; } } if( childChanged ){ if( n.getMetaKind() == kind::metakind::PARAMETERIZED ){ children.insert( children.begin(), n.getOperator() ); } return NodeManager::currentNM()->mkNode( n.getKind(), children ); }else{ return n; } }