GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/theory/quantifiers/quant_util.cpp Lines: 68 83 81.9 %
Date: 2021-05-22 Branches: 93 224 41.5 %

Line Exec Source
1
/******************************************************************************
2
 * Top contributors (to current version):
3
 *   Andrew Reynolds, Morgan Deters, Aina Niemetz
4
 *
5
 * This file is part of the cvc5 project.
6
 *
7
 * Copyright (c) 2009-2021 by the authors listed in the file AUTHORS
8
 * in the top-level source directory and their institutional affiliations.
9
 * All rights reserved.  See the file COPYING in the top-level source
10
 * directory for licensing information.
11
 * ****************************************************************************
12
 *
13
 * Implementation of quantifier utilities
14
 */
15
16
#include "theory/quantifiers/quant_util.h"
17
18
#include "theory/quantifiers/term_util.h"
19
20
using namespace cvc5::kind;
21
22
namespace cvc5 {
23
namespace theory {
24
25
99069
QuantPhaseReq::QuantPhaseReq( Node n, bool computeEq ){
26
99069
  initialize( n, computeEq );
27
99069
}
28
29
99069
void QuantPhaseReq::initialize( Node n, bool computeEq ){
30
198138
  std::map< Node, int > phaseReqs2;
31
99069
  computePhaseReqs( n, false, phaseReqs2 );
32
274368
  for( std::map< Node, int >::iterator it = phaseReqs2.begin(); it != phaseReqs2.end(); ++it ){
33
175299
    if( it->second==1 ){
34
83122
      d_phase_reqs[ it->first ] = true;
35
92177
    }else if( it->second==-1 ){
36
92110
      d_phase_reqs[ it->first ] = false;
37
    }
38
  }
39
99069
  Debug("inst-engine-phase-req") << "Phase requirements for " << n << ":" << std::endl;
40
  //now, compute if any patterns are equality required
41
99069
  if( computeEq ){
42
    for( std::map< Node, bool >::iterator it = d_phase_reqs.begin(); it != d_phase_reqs.end(); ++it ){
43
      Debug("inst-engine-phase-req") << "   " << it->first << " -> " << it->second << std::endl;
44
      if( it->first.getKind()==EQUAL ){
45
        if( quantifiers::TermUtil::hasInstConstAttr(it->first[0]) ){
46
          if( !quantifiers::TermUtil::hasInstConstAttr(it->first[1]) ){
47
            d_phase_reqs_equality_term[ it->first[0] ] = it->first[1];
48
            d_phase_reqs_equality[ it->first[0] ] = it->second;
49
            Debug("inst-engine-phase-req") << "      " << it->first[0] << ( it->second ? " == " : " != " ) << it->first[1] << std::endl;
50
          }
51
        }else if( quantifiers::TermUtil::hasInstConstAttr(it->first[1]) ){
52
          d_phase_reqs_equality_term[ it->first[1] ] = it->first[0];
53
          d_phase_reqs_equality[ it->first[1] ] = it->second;
54
          Debug("inst-engine-phase-req") << "      " << it->first[1] << ( it->second ? " == " : " != " ) << it->first[0] << std::endl;
55
        }
56
      }
57
    }
58
  }
59
99069
}
60
61
306783
void QuantPhaseReq::computePhaseReqs( Node n, bool polarity, std::map< Node, int >& phaseReqs ){
62
306783
  bool newReqPol = false;
63
  bool newPolarity;
64
306783
  if( n.getKind()==NOT ){
65
83191
    newReqPol = true;
66
83191
    newPolarity = !polarity;
67
223592
  }else if( n.getKind()==OR || n.getKind()==IMPLIES ){
68
40730
    if( !polarity ){
69
40730
      newReqPol = true;
70
40730
      newPolarity = false;
71
    }
72
182862
  }else if( n.getKind()==AND ){
73
7494
    if( polarity ){
74
      newReqPol = true;
75
      newPolarity = true;
76
    }
77
  }else{
78
175368
    int val = polarity ? 1 : -1;
79
175368
    if( phaseReqs.find( n )==phaseReqs.end() ){
80
175299
      phaseReqs[n] = val;
81
69
    }else if( val!=phaseReqs[n] ){
82
67
      phaseReqs[n] = 0;
83
    }
84
  }
85
306783
  if( newReqPol ){
86
331635
    for( int i=0; i<(int)n.getNumChildren(); i++ ){
87
207714
      if( n.getKind()==IMPLIES && i==0 ){
88
        computePhaseReqs( n[i], !newPolarity, phaseReqs );
89
      }else{
90
207714
        computePhaseReqs( n[i], newPolarity, phaseReqs );
91
      }
92
    }
93
  }
94
306783
}
95
96
1338508
void QuantPhaseReq::getPolarity( Node n, int child, bool hasPol, bool pol, bool& newHasPol, bool& newPol ) {
97
1338508
  if( n.getKind()==AND || n.getKind()==OR || n.getKind()==SEP_STAR ){
98
300388
    newHasPol = hasPol;
99
300388
    newPol = pol;
100
1038120
  }else if( n.getKind()==IMPLIES ){
101
8
    newHasPol = hasPol;
102
8
    newPol = child==0 ? !pol : pol;
103
1038112
  }else if( n.getKind()==NOT ){
104
158437
    newHasPol = hasPol;
105
158437
    newPol = !pol;
106
879675
  }else if( n.getKind()==ITE ){
107
12738
    newHasPol = (child!=0) && hasPol;
108
12738
    newPol = pol;
109
866937
  }else if( n.getKind()==FORALL ){
110
164
    newHasPol = (child==1) && hasPol;
111
164
    newPol = pol;
112
  }else{
113
866773
    newHasPol = false;
114
866773
    newPol = false;
115
  }
116
1338508
}
117
118
4245641
void QuantPhaseReq::getEntailPolarity( Node n, int child, bool hasPol, bool pol, bool& newHasPol, bool& newPol ) {
119
4245641
  if( n.getKind()==AND || n.getKind()==OR || n.getKind()==SEP_STAR ){
120
2180568
    newHasPol = hasPol && pol!=( n.getKind()==OR );
121
2180568
    newPol = pol;
122
2065073
  }else if( n.getKind()==IMPLIES ){
123
24784
    newHasPol = hasPol && !pol;
124
24784
    newPol = child==0 ? !pol : pol;
125
2040289
  }else if( n.getKind()==NOT ){
126
260975
    newHasPol = hasPol;
127
260975
    newPol = !pol;
128
  }else{
129
1779314
    newHasPol = false;
130
1779314
    newPol = false;
131
  }
132
4245641
}
133
134
}  // namespace theory
135
28194
}  // namespace cvc5