示例#1
0
IVector ode_solver::extract_invariants() {
    map<Enode*, pair<double, double>> inv_map;
    for (auto inv : m_invs) {
        Enode * p = inv->getCdr()->getCdr()->getCdr()->getCdr()->getCar();
        Enode * op = p->getCar();
        bool pos = true;

        // Handle Negation
        if (op->getId() == ENODE_ID_NOT) {
            p = p->getCdr()->getCar();
            op = p->getCar();
            pos = false;
        }
        switch (op->getId()) {
        case ENODE_ID_GEQ:
        case ENODE_ID_GT:
            // Handle >= & >
            pos = !pos;
        case ENODE_ID_LEQ:
        case ENODE_ID_LT: {
            // Handle <= & <
            Enode * lhs = pos ? p->getCdr()->getCar() : p->getCdr()->getCdr()->getCar();
            Enode * rhs = pos ? p->getCdr()->getCdr()->getCar() : p->getCdr()->getCar();
            if (lhs->isVar() && rhs->isConstant()) {
                if (inv_map.find(lhs) != inv_map.end()) {
                    inv_map[lhs].second = rhs->getValue();
                } else {
                    inv_map.emplace(lhs, make_pair(lhs->getLowerBound(), rhs->getValue()));
                }
            } else if (lhs->isConstant() && rhs->isVar()) {
                if (inv_map.find(rhs) != inv_map.end()) {
                    inv_map[rhs].first = lhs->getValue();
                } else {
                    inv_map.emplace(rhs, make_pair(lhs->getValue(), rhs->getUpperBound()));
                }
            } else {
                cerr << "ode_solver::extract_invariant: error:" << p << endl;
            }
        }
            break;
        default:
            cerr << "ode_solver::extract_invariant: error" << p << endl;
        }
    }
    IVector ret (m_t_vars.size());
    unsigned i = 0;
    for (auto const & m_t_var : m_t_vars) {
        if (inv_map.find(m_t_var) != inv_map.end()) {
            auto inv = interval(inv_map[m_t_var].first, inv_map[m_t_var].second);
            DREAL_LOG_INFO << "Invariant extracted from  " << m_t_var << " = " << inv;
            ret[i++] = inv;
        } else {
            auto inv = interval(m_t_var->getLowerBound(), m_t_var->getUpperBound());
            DREAL_LOG_INFO << "Default Invariant set for " << m_t_var << " = " << inv;
            ret[i++] = inv;
        }
    }
    return ret;
}
示例#2
0
文件: THandler.C 项目: clhuang/dreal3
void THandler::clearVar( Var v )
{
  assert( var_to_enode[ v ] != NULL );
  Enode * e = var_to_enode[ v ];
  assert( e->getId( ) < static_cast< int >( enode_id_to_var.size( ) ) );
  assert( enode_id_to_var[ e->getId( ) ] == v );
  var_to_enode[ v ] = NULL;
  enode_id_to_var[ e->getId( ) ] = var_Undef;
}
示例#3
0
//
// Merge collected arguments for nodes
//
Enode * Cnfizer::mergeEnodeArgs( Enode * e
                               , map< enodeid_t, Enode * > & cache
                               , map< enodeid_t, int > & enodeid_to_incoming_edges )
{
  assert( e->isAnd( ) || e->isOr( ) );

  Enode * e_symb = e->getCar( );
  vector< Enode * > new_args;

  for ( Enode * list = e->getCdr( ) ;
        !list->isEnil( ) ;
        list = list->getCdr( ) )
  {
    Enode * arg = list->getCar( );
    Enode * sub_arg = cache[ arg->getId( ) ];
    Enode * sym = arg->getCar( );

    if ( sym->getId( ) != e_symb->getId( ) )
    {
      new_args.push_back( sub_arg );
      continue;
    }

    assert( enodeid_to_incoming_edges.find( arg->getId( ) ) != enodeid_to_incoming_edges.end( ) );
    assert( enodeid_to_incoming_edges[ arg->getId( ) ] >= 1 );

    if ( enodeid_to_incoming_edges[ arg->getId( ) ] > 1 )
    {
      new_args.push_back( sub_arg );
      continue;
    }

    for ( Enode * sub_arg_list = sub_arg->getCdr( ) ;
          !sub_arg_list->isEnil( ) ;
          sub_arg_list = sub_arg_list->getCdr( ) )
      new_args.push_back( sub_arg_list->getCar( ) );
  }

  Enode * new_list = const_cast< Enode * >(egraph.enil);

  while ( !new_args.empty( ) )
  {
    new_list = egraph.cons( new_args.back( ), new_list );
    new_args.pop_back( );
  }

  return egraph.cons( e_symb, new_list );
}
示例#4
0
cgcolor_t CGraph::colorNodesRec( CNode * c, const uint64_t mask )
{
  // Already done
  if ( colored_nodes.find( c ) != colored_nodes.end( ) )
    return c->color;
  // Base case, color variables
  if ( c->e->getArity( ) == 0 )
  {
    cgcolor_t color = CG_UNDEF;
    // Belongs to B
    if ( (egraph.getIPartitions( c->e ) &  mask) != 0 )
      color |= CG_B;
    // Belongs to A
    if ( (egraph.getIPartitions( c->e ) & ~mask) != 0 )
      color |= CG_A;
    c->color = color;
  }
  else
  {
    // Function symbol: color depending on the arguments
    // Decide color of term as intersection
    cgcolor_t color = CG_AB;
    Enode * args = c->e->getCdr( );
    for ( args = c->e->getCdr( )
        ; !args->isEnil( )
        ; args = args->getCdr( ) )
    {
      Enode * arg = args->getCar( );
      // Not necessairly an argument is needed in the graph
      if ( cnodes_store.find( arg->getId( ) ) != cnodes_store.end( ) )
        color &= colorNodesRec( cnodes_store[ arg->getId( ) ], mask );
    }
    c->color = color;
  }
  assert( colored_nodes.find( c ) == colored_nodes.end( ) );
  colored_nodes.insert( c );
  return c->color;
}
示例#5
0
// a != b → R( a, i_{a,b} ) = R( b, i_{a,b} )
void Egraph::ExtAxiom( Enode * a, Enode * b )
{
  assert( isDynamic( a ) );
  assert( isDynamic( b ) );
  Enode * as = dynamicToStatic( a );
  Enode * bs = dynamicToStatic( b );
  assert( isStatic( as ) );
  assert( isStatic( bs ) );

  assert( as->isDTypeArray( ) );
  assert( bs->isDTypeArray( ) );

  // create fresh index i_a,b for pair a,b
  char def_name[ 48 ];
  sprintf( def_name, IND_STR, as->getId( ), bs->getId( ) );
  const unsigned type = DTYPE_ARRAY_INDEX;
  if ( lookupSymbol( def_name ) == NULL )
    newSymbol( def_name, type );
  // Create new variable
  Enode * i = mkVar( def_name );
  // Create two new selections
  Enode * select1  = mkSelect( as, i );
  Enode * select2  = mkSelect( bs, i );
  // Create new literals
  Enode * lit1     = mkEq( cons( as, cons( bs ) ) );
  Enode * lit2_pos = mkEq( cons( select1, cons( select2 ) ) );
  Enode * lit2     = mkNot( cons( lit2_pos ) );
#ifdef PRODUCE_PROOF
  if ( config.gconfig.print_inter > 0 )
  {
    const uint64_t shared = getIPartitions( as ) 
			  & getIPartitions( bs );
    // Mixed can't be one at this point
    assert( shared != 1 );
    // Set AB-mixed partition if no intersection
    if ( shared == 0 )
    {
      setIPartitions( i, 1 );
      setIPartitions( select1, 1 );
      setIPartitions( select2, 1 );
      setIPartitions( lit1, 1 );
      setIPartitions( lit2_pos, 1 );
      setIPartitions( lit2, 1 );
    }
    // Otherwise they share something
    else
    {
      setIPartitions( i, shared );
      setIPartitions( select1, shared );
      setIPartitions( select2, shared );
      setIPartitions( lit1, shared );
      setIPartitions( lit2_pos, shared );
      setIPartitions( lit2, shared );
    }
  }
#endif
  vector< Enode * > v;
  v.push_back( lit1 );
  v.push_back( lit2 );
#ifdef ARR_VERB
  cout << "Axiom Ext ->   " << "( or " << lit1 << " " << lit2 << " )" << endl;
#endif
  splitOnDemand( v, id );

  handleArrayAssertedAtomTerm( select1 );
  handleArrayAssertedAtomTerm( select2 );

  // New contexts to propagate info about new index
  // Given R(a,new) look for all store users W(a,i,e) 
  // and instantiate RoW over R(W(a,i,e),new)
  vector< Enode * > sela;
  vector< Enode * > stoa;
  vector< Enode * > selb;
  vector< Enode * > stob;
  Enode * select3 = NULL;

  // Act over a
  getUsers( a, sela, stoa );
  for( size_t j = 0 ; j < stoa.size( ) ; j++ )
  {
    assert( isDynamic( stoa[ j ] ) );
    Enode * ss = dynamicToStatic( stoa[ j ] );
    assert( isStatic( ss ) );
    // Creation new select for each store user of a
    select3 = mkSelect( ss, i );
    // RoW over new select
    handleArrayAssertedAtomTerm( select3 );
#ifdef PRODUCE_PROOF
    if ( config.gconfig.print_inter > 0 )
    {
      const uint64_t shared = getIPartitions( ss ) 
	                    & getIPartitions( i );
      // Mixed can't be one at this point
      assert( shared != 1 );
      // Set AB-mixed partition if no intersection
      if ( shared == 0 )
	setIPartitions( select3, 1 );
      // Otherwise they share something
      else
	setIPartitions( select3, shared );
    }
#endif
  }

  // Act over b
  getUsers( b, selb, stob );
  for ( size_t j = 0 ; j < stob.size( ) ; j++ )
  {
    assert( isDynamic( stoa[ j ] ) );
    Enode * ss = dynamicToStatic( stob[ j ] );
    assert( isStatic( ss ) );
    // Creation new select for each store user of b
    select3 = mkSelect( ss, i );
#ifdef PRODUCE_PROOF
    if ( config.gconfig.print_inter > 0 )
    {
      const uint64_t shared = getIPartitions( ss ) 
	                    & getIPartitions( i );
      // Mixed can't be one at this point
      assert( shared != 1 );
      // Set AB-mixed partition if no intersection
      if ( shared == 0 )
	setIPartitions( select3, 1 );
      // Otherwise they share something
      else
	setIPartitions( select3, shared );
    }
#endif
    // RoW over new select
    handleArrayAssertedAtomTerm( select3 );
  }
}
示例#6
0
Enode *
ExpandITEs::doit( Enode * formula )
{
  assert( formula );
  list< Enode * > new_clauses;
  vector< Enode * > unprocessed_enodes;
  egraph.initDupMap1( );

  unprocessed_enodes.push_back( formula );
  //
  // Visit the DAG of the formula from the leaves to the root
  //
  while( !unprocessed_enodes.empty( ) )
  {
    Enode * enode = unprocessed_enodes.back( );
    //
    // Skip if the node has already been processed before
    //
    if ( egraph.valDupMap1( enode ) != NULL )
    {
      unprocessed_enodes.pop_back( );
      continue;
    }

    bool unprocessed_children = false;
    Enode * arg_list;
    for ( arg_list = enode->getCdr( ) ;
          arg_list != egraph.enil ;
          arg_list = arg_list->getCdr( ) )
    {
      Enode * arg = arg_list->getCar( );

      assert( arg->isTerm( ) );
      //
      // Push only if it is unprocessed
      //
      if ( egraph.valDupMap1( arg ) == NULL )
      {
        unprocessed_enodes.push_back( arg );
        unprocessed_children = true;
      }
    }
    //
    // SKip if unprocessed_children
    //
    if ( unprocessed_children )
      continue;

    unprocessed_enodes.pop_back( );
    Enode * result = NULL;
    //
    // At this point, every child has been processed
    //
    char def_name[ 32 ];

    if ( enode->isIte( ) )
    {
      //
      // Retrieve arguments
      //
      Enode * i = egraph.valDupMap1( enode->get1st( ) );
      Enode * t = egraph.valDupMap1( enode->get2nd( ) );
      Enode * e = egraph.valDupMap1( enode->get3rd( ) );
      Enode * not_i = egraph.mkNot( egraph.cons( i ) );
      //
      // Generate variable symbol
      //
      sprintf( def_name, ITE_STR, enode->getId( ) );
      Snode * sort = enode->getLastSort( );
      egraph.newSymbol( def_name, sort );
      //
      // Generate placeholder
      //
      result = egraph.mkVar( def_name );
      //
      // Generate additional clauses
      //
      Enode * eq_then = egraph.mkEq( egraph.cons( result
                                   , egraph.cons( t ) ) );
      Enode * eq_else = egraph.mkEq( egraph.cons( result
                                   , egraph.cons( e ) ) );
      new_clauses.push_back( egraph.mkOr( egraph.cons( not_i
                                        , egraph.cons( eq_then ) ) ) );
      new_clauses.push_back( egraph.mkOr( egraph.cons( i
                                        , egraph.cons( eq_else ) ) ) );
    }
    else
    {
      result = egraph.copyEnodeEtypeTermWithCache( enode );
    }

    assert( result );
    assert( egraph.valDupMap1( enode ) == NULL );
    egraph.storeDupMap1( enode, result );
  }

  Enode * new_formula = egraph.valDupMap1( formula );
  assert( new_formula );
  egraph.doneDupMap1( );

  new_clauses.push_back( new_formula );

  return egraph.mkAnd( egraph.cons( new_clauses ) );
}
示例#7
0
//
// Performs the actual cnfization
//
bool Tseitin::cnfize( Enode * formula, map< enodeid_t, Enode * > & cnf_cache )
{
  (void)cnf_cache;
  assert( formula );
  assert( !formula->isAnd( ) );

  Enode * arg_def = egraph.valDupMap1( formula );
  if ( arg_def != NULL )
  {
    vector< Enode * > clause;
    clause.push_back( arg_def );
#ifdef PRODUCE_PROOF
    if ( config.produce_inter > 0 )
      return solver.addSMTClause( clause, egraph.getIPartitions( formula ) );
#endif
    return solver.addSMTClause( clause );
  }

  vector< Enode * > unprocessed_enodes;       // Stack for unprocessed enodes
  unprocessed_enodes.push_back( formula );    // formula needs to be processed
  //
  // Visit the DAG of the formula from the leaves to the root
  //
  while( !unprocessed_enodes.empty( ) )
  {
    Enode * enode = unprocessed_enodes.back( );
    //
    // Skip if the node has already been processed before
    //
    if ( egraph.valDupMap1( enode ) != NULL )
    {
      unprocessed_enodes.pop_back( );
      continue;
    }

    bool unprocessed_children = false;
    Enode * arg_list;
    for ( arg_list = enode->getCdr( ) ;
          arg_list != egraph.enil ;
          arg_list = arg_list->getCdr( ) )
    {
      Enode * arg = arg_list->getCar( );

      assert( arg->isTerm( ) );
      //
      // Push only if it is an unprocessed boolean operator
      //
      if ( enode->isBooleanOperator( )
        && egraph.valDupMap1( arg ) == NULL )
      {
        unprocessed_enodes.push_back( arg );
        unprocessed_children = true;
      }
      //
      // If it is an atom (either boolean or theory) just
      // store it in the cache
      //
      else if ( arg->isAtom( ) )
      {
        egraph.storeDupMap1( arg, arg );
      }
    }
    //
    // SKip if unprocessed_children
    //
    if ( unprocessed_children )
      continue;

    unprocessed_enodes.pop_back( );
    Enode * result = NULL;
    //
    // At this point, every child has been processed
    //
    //
    // Do the actual cnfization, according to the node type
    //
    char def_name[ 32 ];

    if ( enode->isLit( ) )
    {
      result = enode;
    }
    else if ( enode->isNot( ) )
    {
      Enode * arg_def = egraph.valDupMap1( enode->get1st( ) );
      assert( arg_def );
      result = egraph.mkNot( egraph.cons( arg_def ) ); // Toggle the literal
    }
    else
    {
      Enode * arg_def = NULL;
      Enode * new_arg_list = egraph.copyEnodeEtypeListWithCache( enode->getCdr( ) );
      //
      // If the enode is not top-level it needs a definition
      //
      if ( formula != enode )
      {
        sprintf( def_name, CNF_STR, formula->getId( ), enode->getId( ) );
        egraph.newSymbol( def_name, sstore.mkBool( ) );
        arg_def = egraph.mkVar( def_name );
#ifdef PRODUCE_PROOF
        if ( config.produce_inter > 0 )
        {
          // Tag Positive and negative literals
          egraph.tagIFormula( arg_def
                            , egraph.getIPartitions( enode ) );
          egraph.tagIFormula( egraph.mkNot( egraph.cons( arg_def ) )
                            , egraph.getIPartitions( enode ) );
        }
#endif
      }
#ifdef PRODUCE_PROOF
      uint64_t partitions = 0;
      if ( config.produce_inter > 0 )
      {
        partitions = egraph.getIPartitions( enode );
        assert( partitions != 0 );
      }
#endif
      //
      // Handle boolean operators
      //
      if ( enode->isAnd( ) )
        cnfizeAnd( new_arg_list, arg_def
#ifdef PRODUCE_PROOF
        , partitions
#endif
        );
      else if ( enode->isOr( ) )
        cnfizeOr( new_arg_list, arg_def
#ifdef PRODUCE_PROOF
        , partitions
#endif
        );
      else if ( enode->isIff( ) )
        cnfizeIff( new_arg_list, arg_def
#ifdef PRODUCE_PROOF
        , partitions
#endif
        );
      else if ( enode->isXor( ) )
        cnfizeXor( new_arg_list, arg_def
#ifdef PRODUCE_PROOF
        , partitions
#endif
        );
      else
      {
        opensmt_error2( "operator not handled ", enode->getCar( ) );
      }

      if ( arg_def != NULL )
        result = arg_def;
    }

    assert( egraph.valDupMap1( enode ) == NULL );
    egraph.storeDupMap1( enode, result );
  }

  if ( formula->isNot( ) )
  {
    // Retrieve definition of argument
    Enode * arg_def = egraph.valDupMap1( formula->get1st( ) );
    assert( arg_def );
    vector< Enode * > clause;
    clause.push_back( toggleLit( arg_def ) );
#ifdef PRODUCE_PROOF
    if ( config.produce_inter > 0 )
      return solver.addSMTClause( clause, egraph.getIPartitions( formula ) );
#endif
    return solver.addSMTClause( clause );
  }

  return true;
}
示例#8
0
//
// Rewrite formula with maximum arity for operators
//
Enode * Cnfizer::rewriteMaxArity( Enode * formula, map< enodeid_t, int > & enodeid_to_incoming_edges )
{
  assert( formula );

  vector< Enode * > unprocessed_enodes;       // Stack for unprocessed enodes
  unprocessed_enodes.push_back( formula );    // formula needs to be processed
  map< enodeid_t, Enode * > cache;            // Cache of processed nodes
  //
  // Visit the DAG of the formula from the leaves to the root
  //
  while( !unprocessed_enodes.empty( ) )
  {
    Enode * enode = unprocessed_enodes.back( );
    //
    // Skip if the node has already been processed before
    //
    if ( cache.find( enode->getId( ) ) != cache.end( ) )
    {
      unprocessed_enodes.pop_back( );
      continue;
    }

    bool unprocessed_children = false;
    Enode * arg_list;
    for ( arg_list = enode->getCdr( ) ;
          arg_list != egraph.enil ;
          arg_list = arg_list->getCdr( ) )
    {
      Enode * arg = arg_list->getCar( );

      assert( arg->isTerm( ) );
      //
      // Push only if it is an unprocessed boolean operator
      //
      if ( arg->isBooleanOperator( )
        && cache.find( arg->getId( ) ) == cache.end( ) )
      {
        unprocessed_enodes.push_back( arg );
        unprocessed_children = true;
      }
      //
      // If it is an atom (either boolean or theory) just
      // store it in the cache
      //
      else if ( arg->isAtom( ) )
      {
        cache.insert( make_pair( arg->getId( ), arg ) );
      }
    }
    //
    // SKip if unprocessed_children
    //
    if ( unprocessed_children )
      continue;

    unprocessed_enodes.pop_back( );
    Enode * result = NULL;
    //
    // At this point, every child has been processed
    //
    assert ( enode->isBooleanOperator( ) );

    if ( enode->isAnd( )
      || enode->isOr ( ) )
    {
      assert( enode->isAnd( ) || enode->isOr( ) );
      //
      // Construct the new lists for the operators
      //
      result = mergeEnodeArgs( enode, cache, enodeid_to_incoming_edges );
    }
    else
    {
      result = enode;
    }

    assert( result );
    assert( cache.find( enode->getId( ) ) == cache.end( ) );
    cache[ enode->getId( ) ] = result;
  }

  Enode * top_enode = cache[ formula->getId( ) ];
  return top_enode;
}
示例#9
0
//
// Compute the number of incoming edges for e and children
//
void Cnfizer::computeIncomingEdges( Enode * e
                                  , map< enodeid_t, int > & enodeid_to_incoming_edges )
{
  assert( e );

  vector< Enode * > unprocessed_enodes;       // Stack for unprocessed enodes
  unprocessed_enodes.push_back( e );    // formula needs to be processed
  //
  // Visit the DAG of the formula from the leaves to the root
  //
  while( !unprocessed_enodes.empty( ) )
  {
    Enode * enode = unprocessed_enodes.back( );
    //
    // Skip if the node has already been processed before
    //
    map< enodeid_t, int >::iterator it = enodeid_to_incoming_edges.find( enode->getId( ) );
    if ( it != enodeid_to_incoming_edges.end( ) )
    {
      it->second++;
      unprocessed_enodes.pop_back( );
      continue;
    }

    bool unprocessed_children = false;
    if ( enode->isBooleanOperator( ) )
    {
      for ( Enode * arg_list = enode->getCdr( )
          ; !arg_list->isEnil( )
          ; arg_list = arg_list->getCdr( ) )
      {
        Enode * arg = arg_list->getCar( );
        //
        // Push only if it is an unprocessed boolean operator
        //
        map< enodeid_t, int >::iterator it = enodeid_to_incoming_edges.find( arg->getId( ) );
        if ( it == enodeid_to_incoming_edges.end( ) )
        {
          unprocessed_enodes.push_back( arg );
          unprocessed_children = true;
        }
        else
        {
          it->second ++;
        }
      }
    }
    //
    // SKip if unprocessed_children
    //
    if ( unprocessed_children )
      continue;

    unprocessed_enodes.pop_back( );
    //
    // At this point, every child has been processed
    //
    assert ( enode->isBooleanOperator( ) || enode->isAtom( ) );
    assert ( enodeid_to_incoming_edges.find( enode->getId( ) ) == enodeid_to_incoming_edges.end( ) );
    enodeid_to_incoming_edges[ enode->getId( ) ] = 1;
  }
}