summaryrefslogtreecommitdiff
path: root/src/theory/rewriterules/efficient_e_matching.h
blob: b02d465fc925fd5d277b5a8bd8922230e3717e46 (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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
/*********************                                                        */
/*! \file efficient_e_matching.h
 ** \verbatim
 ** Original author: Andrew Reynolds
 ** Major contributors: Morgan Deters
 ** Minor contributors (to current version): none
 ** This file is part of the CVC4 project.
 ** Copyright (c) 2009-2013  New York University and The University of Iowa
 ** See the file COPYING in the top-level source directory for licensing
 ** information.\endverbatim
 **
 ** \brief efficient e-matching
 **/

#include "cvc4_private.h"

#ifndef __CVC4__EFFICIENT_E_MATCHING_H
#define __CVC4__EFFICIENT_E_MATCHING_H

#include "expr/node.h"
#include "context/context.h"
#include "context/context_mm.h"
#include "context/cdchunk_list.h"

#include "util/statistics_registry.h"
#include "util/ntuple.h"
#include "context/cdqueue.h"
#include "context/cdo.h"

#include "theory/uf/equality_engine.h"

namespace CVC4 {
namespace theory {

class QuantifiersEngine;

namespace quantifiers{
  class TermDb;
}

class EfficientEMatcher;
class HandlerPcDispatcher;
class HandlerPpDispatcher;

typedef std::set<Node> SetNode;

template<class T>
class CleanUpPointer{
public:
  inline void operator()(T** e){
    delete(*e);
  };
};

class EfficientHandler{
public:
  typedef std::pair< Node, size_t > MonoCandidate;
  typedef std::pair< MonoCandidate, MonoCandidate > MultiCandidate;
  typedef std::pair< SetNode, size_t > MonoCandidates;
  typedef std::pair< MonoCandidates, MonoCandidates > MultiCandidates;
private:
  /* Queue of candidates */
  typedef context::CDQueue< MonoCandidates *, CleanUpPointer<MonoCandidates> > MonoCandidatesQueue;
  typedef context::CDQueue< MultiCandidates *, CleanUpPointer<MultiCandidates> > MultiCandidatesQueue;
  MonoCandidatesQueue d_monoCandidates;
  typedef SetNode::iterator SetNodeIter;
  context::CDO<SetNodeIter> d_si;
  context::CDO<bool> d_mono_not_first;

  MonoCandidatesQueue d_monoCandidatesNewTerm;
  context::CDO<SetNodeIter> d_si_new_term;
  context::CDO<bool> d_mono_not_first_new_term;


  MultiCandidatesQueue d_multiCandidates;
  context::CDO<SetNodeIter> d_si1;
  context::CDO<SetNodeIter> d_si2;
  context::CDO<bool> d_multi_not_first;


  friend class EfficientEMatcher;
  friend class HandlerPcDispatcher;
  friend class HandlerPpDispatcher;
  friend class HandlerNewTermDispatcher;
protected:
  void addMonoCandidate(SetNode & s, size_t index){
    Assert(!s.empty());
    d_monoCandidates.push(new MonoCandidates(s,index));
  }
  void addMonoCandidateNewTerm(SetNode & s, size_t index){
    Assert(!s.empty());
    d_monoCandidatesNewTerm.push(new MonoCandidates(s,index));
  }
  void addMultiCandidate(SetNode & s1, size_t index1, SetNode & s2, size_t index2){
    Assert(!s1.empty() && !s2.empty());
    d_multiCandidates.push(new MultiCandidates(MonoCandidates(s1,index1),
                                               MonoCandidates(s2,index2)));
  }
public:
  EfficientHandler(context::Context * c):
    //false for d_mono_not_first beacause its the default constructor
    d_monoCandidates(c), d_si(c), d_mono_not_first(c,false),
    d_monoCandidatesNewTerm(c), d_si_new_term(c),
    d_mono_not_first_new_term(c,false),
    d_multiCandidates(c) , d_si1(c), d_si2(c), d_multi_not_first(c,false) {};

  bool getNextMonoCandidate(MonoCandidate & candidate){
    if(d_monoCandidates.empty()) return false;
    const MonoCandidates * front = d_monoCandidates.front();
    SetNodeIter si_tmp;
    if(!d_mono_not_first){
      Assert(front->first.begin() != front->first.end());
      d_mono_not_first = true;
      si_tmp=front->first.begin();
    }else{
      si_tmp = d_si;
      ++si_tmp;
    };
    if(si_tmp != front->first.end()){
      candidate.first = (*si_tmp);
      candidate.second = front->second;
      d_si = si_tmp;
      Debug("efficienthandler") << "Mono produces " << candidate.first << " for " << candidate.second << std::endl;
      return true;
    };
    d_monoCandidates.pop();
    d_mono_not_first = false;
    return getNextMonoCandidate(candidate);
  };

  bool getNextMonoCandidateNewTerm(MonoCandidate & candidate){
    if(d_monoCandidatesNewTerm.empty()) return false;
    const MonoCandidates * front = d_monoCandidatesNewTerm.front();
    SetNodeIter si_tmp;
    if(!d_mono_not_first_new_term){
      Assert(front->first.begin() != front->first.end());
      d_mono_not_first_new_term = true;
      si_tmp=front->first.begin();
    }else{
      si_tmp = d_si_new_term;
      ++si_tmp;
    };
    if(si_tmp != front->first.end()){
      candidate.first = (*si_tmp);
      candidate.second = front->second;
      d_si_new_term = si_tmp;
      Debug("efficienthandler") << "Mono produces " << candidate.first << " for " << candidate.second << std::endl;
      return true;
    };
    d_monoCandidatesNewTerm.pop();
    d_mono_not_first_new_term = false;
    return getNextMonoCandidateNewTerm(candidate);
  };

  bool getNextMultiCandidate(MultiCandidate & candidate){
    if(d_multiCandidates.empty()) return false;
    const MultiCandidates* front = d_multiCandidates.front();
    SetNodeIter si1_tmp;
    SetNodeIter si2_tmp;
    if(!d_multi_not_first){
      Assert(front->first.first.begin() != front->first.first.end());
      Assert(front->second.first.begin() != front->second.first.end());
      si1_tmp = front->first.first.begin();
      si2_tmp = front->second.first.begin();
    }else{
      si1_tmp = d_si1;
      si2_tmp = d_si2;
      ++si2_tmp;
    };
    if(si2_tmp != front->second.first.end()){
      candidate.first.first = *si1_tmp;
      candidate.first.second = front->first.second;
      candidate.second.first = *si2_tmp;
      candidate.second.second = front->second.second;
      if(!d_multi_not_first){d_si1 = si1_tmp; d_multi_not_first = true; };
      d_si2 = si2_tmp;
      Debug("efficienthandler") << "Multi1 produces "
                                << candidate.first.first << " for "
                                << candidate.first.second << " and "
                                << candidate.second.first << " for "
                                << candidate.second.second << " and "
                                << std::endl;
      return true;
    }; // end of the second set
    si2_tmp = front->second.first.begin();
    ++si1_tmp;
    if(si1_tmp != front->first.first.end()){
      candidate.first.first = *si1_tmp;
      candidate.first.second = front->first.second;
      candidate.second.first = *si2_tmp;
      candidate.second.second = front->second.second;
      d_si1 = si1_tmp;
      d_si2 = si2_tmp;
      Debug("efficienthandler") << "Multi2 produces "
                                << candidate.first.first << " for "
                                << candidate.first.second << " and "
                                << candidate.second.first << " for "
                                << candidate.second.second << " and "
                                << std::endl;
      return true;
    }; // end of the first set
    d_multiCandidates.pop();
    d_multi_not_first = false;
    return getNextMultiCandidate(candidate);
  }
};

class PcDispatcher{
public:
  virtual ~PcDispatcher(){};
  /* Send the node to the dispatcher */
  virtual void send(SetNode & s) = 0;
};


class HandlerPcDispatcher: public PcDispatcher{
  EfficientHandler* d_handler;
  size_t d_index;
public:
  HandlerPcDispatcher(EfficientHandler* handler, size_t index):
    d_handler(handler), d_index(index) {};
  void send(SetNode & s){
    d_handler->addMonoCandidate(s,d_index);
  }
};


/** All the dispatcher that correspond to this node */
class NodePcDispatcher: public PcDispatcher{
#ifdef CVC4_DEBUG
public:
  Node pat;
#endif/* CVC4_DEBUG*/
private:
  std::vector<HandlerPcDispatcher> d_dis;
public:
  void send(SetNode & s){
    Assert(!s.empty());
    for(std::vector<HandlerPcDispatcher>::iterator i = d_dis.begin(), end = d_dis.end();
        i != end; ++i){
      (*i).send(s);
    }
  }
  void addPcDispatcher(EfficientHandler* handler, size_t index){
    d_dis.push_back(HandlerPcDispatcher(handler,index));
  }
};


class HandlerNewTermDispatcher: public PcDispatcher{
  EfficientHandler* d_handler;
  size_t d_index;
public:
  HandlerNewTermDispatcher(EfficientHandler* handler, size_t index):
    d_handler(handler), d_index(index) {};
  void send(SetNode & s){
    d_handler->addMonoCandidateNewTerm(s,d_index);
  }
};

/** All the dispatcher that correspond to this node */
class NodeNewTermDispatcher: public PcDispatcher{
#ifdef CVC4_DEBUG
public:
  Node pat;
#endif/* CVC4_DEBUG*/
private:
  std::vector<HandlerNewTermDispatcher> d_dis;
public:
  void send(SetNode & s){
    Assert(!s.empty());
    for(std::vector<HandlerNewTermDispatcher>::iterator i = d_dis.begin(), end = d_dis.end();
        i != end; ++i){
      (*i).send(s);
    }
  }
  void addNewTermDispatcher(EfficientHandler* handler, size_t index){
    d_dis.push_back(HandlerNewTermDispatcher(handler,index));
  }
};

class PpDispatcher{
public:
  virtual ~PpDispatcher(){};
  /* Send the node to the dispatcher */
  virtual void send(SetNode & s1, SetNode & s2, SetNode & sinter) = 0;
};


class HandlerPpDispatcher: public PpDispatcher{
  EfficientHandler* d_handler;
  size_t d_index1;
  size_t d_index2;
public:
  HandlerPpDispatcher(EfficientHandler* handler, size_t index1, size_t index2):
    d_handler(handler), d_index1(index1), d_index2(index2) {};
  void send(SetNode & s1, SetNode & s2, SetNode & sinter){
    if(d_index1 == d_index2){
      if(!sinter.empty())
        d_handler->addMonoCandidate(sinter,d_index1);
    }else{
      d_handler->addMultiCandidate(s1,d_index1,s2,d_index2);
    }
  }
};


/** All the dispatcher that correspond to this node */
class NodePpDispatcher: public PpDispatcher{
#ifdef CVC4_DEBUG
public:
  Node pat1;
  Node pat2;
#endif/* CVC4_DEBUG */
private:
  std::vector<HandlerPpDispatcher> d_dis;
  void send(SetNode & s1, SetNode & s2, SetNode & inter){
    for(std::vector<HandlerPpDispatcher>::iterator i = d_dis.begin(), end = d_dis.end();
        i != end; ++i){
      (*i).send(s1,s2,inter);
    }
  }
public:
  void send(SetNode & s1, SetNode & s2){
    // can be done in HandlerPpDispatcher lazily
    Assert(!s1.empty() && !s2.empty());
    SetNode inter;
    std::set_intersection( s1.begin(), s1.end(), s2.begin(), s2.end(),
                           std::inserter( inter, inter.begin() ) );
    send(s1,s2,inter);
  }
  void addPpDispatcher(EfficientHandler* handler, size_t index1, size_t index2){
    d_dis.push_back(HandlerPpDispatcher(handler,index1,index2));
  }
};

//equivalence class info
class EqClassInfo
{
public:
  typedef context::CDHashMap<Node, bool, NodeHashFunction> BoolMap;
  typedef context::CDChunkList<Node> NodeList;
public:
  //a list of operators that occur as top symbols in this equivalence class
  //  Efficient E-Matching for SMT Solvers: "funs"
  BoolMap d_funs;
  //a list of operators f for which a term of the form f( ... t ... ) exists
  //  Efficient E-Matching for SMT Solvers: "pfuns"
  BoolMap d_pfuns;
  //a list of equivalence classes that are disequal
  BoolMap d_disequal;
public:
  EqClassInfo( context::Context* c );
  ~EqClassInfo(){}
  //set member
  void setMember( Node n, quantifiers::TermDb* db );
  //has function "funs"
  bool hasFunction( Node op );
  //has parent "pfuns"
  bool hasParent( Node op );
  //merge with another eq class info
  void merge( EqClassInfo* eci );
};

class EfficientEMatcher{
protected:
  /** reference to the quantifiers engine */
  QuantifiersEngine* d_quantEngine;
public:
  EfficientEMatcher(CVC4::theory::QuantifiersEngine* qe);
  ~EfficientEMatcher() {
    for(std::map< Node, std::pair<NodePcDispatcher*, NodePpDispatcher*> >::iterator
          i = d_pat_cand_gens.begin(), end = d_pat_cand_gens.end();
        i != end; i++){
      delete(i->second.first);
      delete(i->second.second);
    }
  }
  /** get equality engine we are using */
  eq::EqualityEngine* getEqualityEngine();
private:
  //information for each equivalence class
  std::map< Node, EqClassInfo* > d_eqc_ops;
public:
  /** new node */
  void newEqClass( TNode n );
  /** merge */
  void merge( TNode a, TNode b );
  /** assert terms are disequal */
  void assertDisequal( TNode a, TNode b, TNode reason );
  /** get equivalence class info */
  EqClassInfo* getEquivalenceClassInfo( Node n );
  EqClassInfo* getOrCreateEquivalenceClassInfo( Node n );
  typedef std::vector< std::pair< Node, int > > Ips;
  typedef std::map< Node, std::vector< std::pair< Node, Ips > > > PpIpsMap;
  typedef std::map< Node, std::vector< triple< size_t, Node, Ips > > > MultiPpIpsMap;
private:
  /** Parent/Child Pairs (for efficient E-matching)
      So, for example, if we have the pattern f( g( x ) ), then d_pc_pairs[g][f][f( g( x ) )] = { f.0 }.
  */
  std::map< Node, std::map< Node, std::vector< std::pair< NodePcDispatcher*, Ips > > > > d_pc_pairs;
  /** Parent/Parent Pairs (for efficient E-matching) */
  std::map< Node, std::map< Node, std::vector< triple< NodePpDispatcher*, Ips, Ips > > > > d_pp_pairs;
  /** Constants/Child Pairs
      So, for example, if we have the pattern f( x ) = c, then d_pc_pairs[f][c] = ..., pcdispatcher, ...
  */
  //TODO constant in pattern can use the same thing just add an Ips
  std::map< Node, std::map< Node, NodePcDispatcher* > > d_cc_pairs;
  /** list of all candidate generators for each operator */
  std::map< Node, NodeNewTermDispatcher > d_cand_gens;
  /** list of all candidate generators for each type */
  std::map< TypeNode, NodeNewTermDispatcher > d_cand_gen_types;
  /** map from patterns to candidate generators */
  std::map< Node, std::pair<NodePcDispatcher*, NodePpDispatcher*> > d_pat_cand_gens;
  /** helper functions */
  void registerPatternElementPairs2( Node pat, Ips& ips,
                                     PpIpsMap & pp_ips_map, NodePcDispatcher* npc);
  void registerPatternElementPairs( Node pat, PpIpsMap & pp_ips_map,
                                    NodePcDispatcher* npc, NodePpDispatcher* npp);
  /** find the pp-pair between pattern inside multi-pattern*/
  void combineMultiPpIpsMap(PpIpsMap & pp_ips_map, MultiPpIpsMap & multi_pp_ips_map,
                            EfficientHandler& eh, size_t index2,
                            const std::vector<Node> & pats); //pats for debug
  /** compute candidates for pc pairs */
  void computeCandidatesPcPairs( Node a, EqClassInfo*, Node b, EqClassInfo* );
  /** compute candidates for pp pairs */
  void computeCandidatesPpPairs( Node a, EqClassInfo*, Node b, EqClassInfo* );
  /** compute candidates for cc pairs */
  void computeCandidatesConstants( Node a, EqClassInfo*, Node b, EqClassInfo* );
  /** collect terms based on inverted path string */
  void collectTermsIps( Ips& ips, SetNode& terms, int index);
  bool collectParentsTermsIps( Node n, Node f, int arg, SetNode& terms, bool addRep, bool modEq = true );
public:
  void collectTermsIps( Ips& ips, SetNode& terms);
public:
  void registerEfficientHandler( EfficientHandler& eh, const std::vector<Node> & pat );
public:
  void newTerms(SetNode& s);
public:
  /** output eq class */
  void outputEqClass( const char* c, Node n );
  /** output inverted path string */
  void outputIps( const char* c, Ips& ips );
};/* class EfficientEMatcher */


}/* CVC4::theory namespace */
}/* CVC4 namespace */

#endif /* __CVC4__EFFICIENT_E_MATCHING_H */
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback