summaryrefslogtreecommitdiff
path: root/src/theory/quantifiers/local_theory_ext.cpp
blob: 375754b26ccecb943aac74ad320931d375f9aa4b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/*********************                                                        */
/*! \file local_theory_ext.cpp
 ** \verbatim
 ** Top contributors (to current version):
 **   Andrew Reynolds, Paul Meng
 ** This file is part of the CVC4 project.
 ** Copyright (c) 2009-2017 by the authors listed in the file AUTHORS
 ** in the top-level source directory) and their institutional affiliations.
 ** All rights reserved.  See the file COPYING in the top-level source
 ** directory for licensing information.\endverbatim
 **
 ** \brief Implementation of local theory ext utilities
 **/

#include "theory/quantifiers/local_theory_ext.h"
#include "theory/quantifiers/term_database.h"
#include "theory/quantifiers/term_util.h"
#include "theory/quantifiers_engine.h"
#include "theory/quantifiers/first_order_model.h"

using namespace std;
using namespace CVC4;
using namespace CVC4::kind;
using namespace CVC4::context;
using namespace CVC4::theory;
using namespace CVC4::theory::quantifiers;


LtePartialInst::LtePartialInst( QuantifiersEngine * qe, context::Context* c ) : 
QuantifiersModule( qe ), d_wasInvoked( false ), d_needsCheck( false ){

}

/** add quantifier */
void LtePartialInst::preRegisterQuantifier( Node q ) {
  if( !q.getAttribute(LtePartialInstAttribute()) ){
    if( d_do_inst.find( q )!=d_do_inst.end() ){
      if( d_do_inst[q] ){
        d_lte_asserts.push_back( q );
        d_quantEngine->setOwner( q, this );
      }
    }else{
      d_vars[q].clear();
      d_pat_var_order[q].clear();
      //check if this quantified formula is eligible for partial instantiation
      std::map< Node, bool > vars;
      for( unsigned i=0; i<q[0].getNumChildren(); i++ ){
        vars[q[0][i]] = false;
      }
      getEligibleInstVars( q[1], vars );

      //instantiate only if we would force ground instances
      std::map< Node, int > var_order;
      bool doInst = true;
      for( unsigned i=0; i<q[0].getNumChildren(); i++ ){
        if( vars[q[0][i]] ){
          d_vars[q].push_back( q[0][i] );
          var_order[q[0][i]] = i;
        }else{
          Trace("lte-partial-inst-debug") << "...do not consider, variable " << q[0][i] << " was not found in correct position in body." << std::endl;
          doInst = false;
          break;
        }
      }
      if( doInst ){
        //also needs patterns
        if( q.getNumChildren()==3 && q[2].getNumChildren()==1 ){
          for( unsigned i=0; i<q[2][0].getNumChildren(); i++ ){
            Node pat = q[2][0][i];
            if( pat.getKind()==APPLY_UF ){
              for( unsigned j=0; j<pat.getNumChildren(); j++ ){
                if( !addVariableToPatternList( pat[j], d_pat_var_order[q], var_order ) ){
                  doInst = false;
                }
              }
            }else if( !addVariableToPatternList( pat, d_pat_var_order[q], var_order ) ){
              doInst = false;
            }
            if( !doInst ){
              Trace("lte-partial-inst-debug") << "...do not consider, cannot resolve pattern : " << pat << std::endl;
              break;
            }
          }
        }else{
          Trace("lte-partial-inst-debug") << "...do not consider (must have exactly one pattern)." << std::endl;
        }
      }
      
      
      Trace("lte-partial-inst") << "LTE: ...will " << ( doInst ? "" : "not ") << "instantiate " << q << std::endl;
      d_do_inst[q] = doInst;
      if( doInst ){
        d_lte_asserts.push_back( q );
        d_needsCheck = true;
        d_quantEngine->setOwner( q, this );
      }
    }
  }
}

bool LtePartialInst::addVariableToPatternList( Node v, std::vector< int >& pat_var_order, std::map< Node, int >& var_order ) {
  std::map< Node, int >::iterator it = var_order.find( v );
  if( it==var_order.end() ){
    return false;
  }else if( std::find( pat_var_order.begin(), pat_var_order.end(), it->second )!=pat_var_order.end() ){
    return false;
  }else{
    pat_var_order.push_back( it->second );
    return true;
  }
}

void LtePartialInst::getEligibleInstVars( Node n, std::map< Node, bool >& vars ) {
  if( n.getKind()==APPLY_UF && !n.getType().isBoolean() ){
    for( unsigned i=0; i<n.getNumChildren(); i++ ){
      if( vars.find( n[i] )!=vars.end() ){
        vars[n[i]] = true;
      }
    }
  }
  for( unsigned i=0; i<n.getNumChildren(); i++ ){
    getEligibleInstVars( n[i], vars );
  }
}

/* whether this module needs to check this round */
bool LtePartialInst::needsCheck( Theory::Effort e ) {
  return e>=Theory::EFFORT_FULL && d_needsCheck;
}
/* Call during quantifier engine's check */
void LtePartialInst::check(Theory::Effort e, QEffort quant_e)
{
  //flush lemmas ASAP (they are a reduction)
  if (quant_e == QEFFORT_CONFLICT && d_needsCheck)
  {
    std::vector< Node > lemmas;
    getInstantiations( lemmas );
    //add lemmas to quantifiers engine
    for( unsigned i=0; i<lemmas.size(); i++ ){
      d_quantEngine->addLemma( lemmas[i], false );
    }
    d_needsCheck = false;
  }
}


void LtePartialInst::reset() {
  d_reps.clear();
  eq::EqualityEngine* ee = d_quantEngine->getActiveEqualityEngine();
  eq::EqClassesIterator eqcs_i = eq::EqClassesIterator( ee );
  while( !eqcs_i.isFinished() ){
    TNode r = (*eqcs_i);
    TypeNode tn = r.getType();
    d_reps[tn].push_back( r );
    ++eqcs_i;
  }
}


/** get instantiations */
void LtePartialInst::getInstantiations( std::vector< Node >& lemmas ) {
  Trace("lte-partial-inst") << "LTE : get instantiations, # quant = " << d_lte_asserts.size() << std::endl;
  reset();
  for( unsigned i=0; i<d_lte_asserts.size(); i++ ){
    Node q = d_lte_asserts[i];
    Assert( d_do_inst.find( q )!=d_do_inst.end() && d_do_inst[q] );
    if( d_inst.find( q )==d_inst.end() ){
      Trace("lte-partial-inst") << "LTE : Get partial instantiations for " << q << "..." << std::endl;
      d_inst[q] = true;
      Assert( !d_vars[q].empty() );
      //make bound list
      Node bvl;
      std::vector< Node > bvs;
      for( unsigned j=0; j<q[0].getNumChildren(); j++ ){
        if( std::find( d_vars[q].begin(), d_vars[q].end(), q[0][j] )==d_vars[q].end() ){
          bvs.push_back( q[0][j] );
        }
      }
      if( !bvs.empty() ){
        bvl = NodeManager::currentNM()->mkNode( BOUND_VAR_LIST, bvs );
      }
      std::vector< Node > conj;
      std::vector< Node > terms;
      std::vector< TypeNode > types;
      for( unsigned j=0; j<d_vars[q].size(); j++ ){
        types.push_back( d_vars[q][j].getType() );
        terms.push_back( Node::null() );
      }

      getPartialInstantiations( conj, q, bvl, d_vars[q], terms, types, NULL, 0, 0, 0 );
      Assert( !conj.empty() );
      lemmas.push_back( NodeManager::currentNM()->mkNode( OR, q.negate(), conj.size()==1 ? conj[0] : NodeManager::currentNM()->mkNode( AND, conj ) ) );
      d_wasInvoked = true;
    }
  }
}

void LtePartialInst::getPartialInstantiations( std::vector< Node >& conj, Node q, Node bvl,
                                               std::vector< Node >& vars, std::vector< Node >& terms, std::vector< TypeNode >& types, TermArgTrie * curr,
                                               unsigned pindex, unsigned paindex, unsigned iindex ){
  if( iindex==vars.size() ){
    Node body = q[1].substitute( vars.begin(), vars.end(), terms.begin(), terms.end() );
    if( bvl.isNull() ){
      conj.push_back( body );
      Trace("lte-partial-inst") << " - ground conjunct : " << body << std::endl;
    }else{
      Node nq;
      if( q.getNumChildren()==3 ){
        Node ipl = q[2].substitute( vars.begin(), vars.end(), terms.begin(), terms.end() );
        nq = NodeManager::currentNM()->mkNode( FORALL, bvl, body, ipl );
      }else{
        nq = NodeManager::currentNM()->mkNode( FORALL, bvl, body );
      }
      Trace("lte-partial-inst") << " - quantified conjunct : " << nq << std::endl;
      LtePartialInstAttribute ltpia;
      nq.setAttribute(ltpia,true);
      conj.push_back( nq );
    }
  }else{
    Assert( pindex<q[2][0].getNumChildren() );
    Node pat = q[2][0][pindex];
    Assert( pat.getNumChildren()==0 || paindex<=pat.getNumChildren() );
    if( pat.getKind()==APPLY_UF ){
      Assert( paindex<=pat.getNumChildren() );
      if( paindex==pat.getNumChildren() ){
        getPartialInstantiations( conj, q, bvl, vars, terms, types, NULL, pindex+1, 0, iindex );
      }else{
        if( !curr ){
          Assert( paindex==0 );
          //start traversing term index for the operator
          curr = d_quantEngine->getTermDatabase()->getTermArgTrie( pat.getOperator() );
        }
        for( std::map< TNode, TermArgTrie >::iterator it = curr->d_data.begin(); it != curr->d_data.end(); ++it ){
          terms[d_pat_var_order[q][iindex]] = it->first;
          getPartialInstantiations( conj, q, bvl, vars, terms, types, &it->second, pindex, paindex+1, iindex+1 );
        }
      }
    }else{
      std::map< TypeNode, std::vector< Node > >::iterator it = d_reps.find( types[iindex] );
      if( it!=d_reps.end() ){
        Trace("lte-partial-inst-debug") << it->second.size() << " reps of type " << types[iindex] << std::endl;
        for( unsigned i=0; i<it->second.size(); i++ ){
          terms[d_pat_var_order[q][iindex]] = it->second[i];
          getPartialInstantiations( conj, q, bvl, vars, terms, types, NULL, pindex+1, 0, iindex+1 );
        }
      }else{
        Trace("lte-partial-inst-debug") << "No reps found of type " << types[iindex] << std::endl;
      }
    }
  }
}
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback