summaryrefslogtreecommitdiff
path: root/src/theory/uf/theory_uf.cpp
blob: 840c7b51e437203fe255afa1285175dbeb197cf0 (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

#include "theory/uf/theory_uf.h"
#include "theory/uf/ecdata.h"
#include "expr/kind.h"

using namespace CVC4;
using namespace theory;
using namespace context;


/* Temporaries to facilitate compiling. */
enum TmpEnum {EC};
void setAttribute(Node n, TmpEnum te, ECData * ec){}
ECData* getAttribute(Node n, TmpEnum te) { return NULL; }
Node getOperator(Node x) { return Node::null(); }


void TheoryUF::setup(const Node& n){
  ECData * ecN = new (true) ECData(d_context, n);

  //TODO Make sure attribute manager owns the pointer
  setAttribute(n, EC, ecN);

  if(n.getKind() == APPLY){
    for(Node::iterator cIter = n.begin(); cIter != n.end(); ++cIter){
      Node child = *cIter;
      
      ECData * ecChild = getAttribute(child, EC);
      ecChild = ccFind(ecChild);

      ecChild->addPredecessor(n, d_context);
    }
  }
}

bool TheoryUF::equiv(Node x, Node y){
  if(x.getNumChildren() != y.getNumChildren())
    return false;

  if(getOperator(x) != getOperator(y))
    return false;

  Node::iterator xIter = x.begin();
  Node::iterator yIter = y.begin();

  while(xIter != x.end()){
    
    if(ccFind(getAttribute(*xIter, EC)) !=
       ccFind(getAttribute(*yIter, EC)))
      return false;

    ++xIter;
    ++yIter;
  }
  return true;
}

ECData* TheoryUF::ccFind(ECData * x){
  if( x->getFind() == x)
    return x;
  else{
    return ccFind(x->getFind());
  }
}

void TheoryUF::ccUnion(ECData* ecX, ECData* ecY){
  ECData* nslave;
  ECData* nmaster;

  if(ecX->getWatchListSize() <= ecY->getWatchListSize()){
    nslave = ecX;
    nmaster = ecY;
  }else{
    nslave = ecY;
    nmaster = ecX;
  }

  nslave->setFind(nmaster);

  for(Link* Px = nmaster->getFirst(); Px != NULL; Px = Px->next ){
    for(Link* Py = nslave->getFirst(); Py != NULL; Py = Py->next ){
      if(equiv(Px->data,Py->data)){
        d_pending.push_back((Px->data).eqExpr(Py->data));
      }
    }
  }

  ECData::takeOverDescendantWatchList(nslave, nmaster);
}

//TODO make parameters soft references
void TheoryUF::merge(){
  do{
    Node assertion = d_pending[d_currentPendingIdx];
    d_currentPendingIdx = d_currentPendingIdx + 1;
    
    Node x = assertion[0];
    Node y = assertion[1];
    
    ECData* ecX = ccFind(getAttribute(x, EC));
    ECData* ecY = ccFind(getAttribute(y, EC));
    
    if(ecX == ecY)
      continue;
    
    ccUnion(ecX, ecY);
  }while( d_currentPendingIdx < d_pending.size() );
}

void TheoryUF::check(OutputChannel& out, Effort level){
  while(!done()){
    Node assertion = get();
    

    switch(assertion.getKind()){
    case EQUAL:
      d_pending.push_back(assertion);
      merge();
      break;
    case NOT:
      d_disequality.push_back(assertion[0]);
      break;
    default:
      Unreachable();
    }
  }
  if(fullEffort(level)){
    for(CDList<Node>::const_iterator diseqIter = d_disequality.begin();
        diseqIter != d_disequality.end();
        ++diseqIter){
      
      if(ccFind(getAttribute((*diseqIter)[0], EC)) ==
         ccFind(getAttribute((*diseqIter)[1], EC))){
        out.conflict(*diseqIter, true);
      }
    }
  }
}
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback