GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/theory/quantifiers/quant_util.cpp Lines: 68 83 81.9 %
Date: 2021-08-20 Branches: 93 222 41.9 %

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
112516
QuantPhaseReq::QuantPhaseReq( Node n, bool computeEq ){
26
112516
  initialize( n, computeEq );
27
112516
}
28
29
112516
void QuantPhaseReq::initialize( Node n, bool computeEq ){
30
225032
  std::map< Node, int > phaseReqs2;
31
112516
  computePhaseReqs( n, false, phaseReqs2 );
32
308485
  for( std::map< Node, int >::iterator it = phaseReqs2.begin(); it != phaseReqs2.end(); ++it ){
33
195969
    if( it->second==1 ){
34
88607
      d_phase_reqs[ it->first ] = true;
35
107362
    }else if( it->second==-1 ){
36
107295
      d_phase_reqs[ it->first ] = false;
37
    }
38
  }
39
112516
  Debug("inst-engine-phase-req") << "Phase requirements for " << n << ":" << std::endl;
40
  //now, compute if any patterns are equality required
41
112516
  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
112516
}
60
61
339352
void QuantPhaseReq::computePhaseReqs( Node n, bool polarity, std::map< Node, int >& phaseReqs ){
62
339352
  bool newReqPol = false;
63
  bool newPolarity;
64
339352
  if( n.getKind()==NOT ){
65
88676
    newReqPol = true;
66
88676
    newPolarity = !polarity;
67
250676
  }else if( n.getKind()==OR || n.getKind()==IMPLIES ){
68
45360
    if( !polarity ){
69
45360
      newReqPol = true;
70
45360
      newPolarity = false;
71
    }
72
205316
  }else if( n.getKind()==AND ){
73
9278
    if( polarity ){
74
      newReqPol = true;
75
      newPolarity = true;
76
    }
77
  }else{
78
196038
    int val = polarity ? 1 : -1;
79
196038
    if( phaseReqs.find( n )==phaseReqs.end() ){
80
195969
      phaseReqs[n] = val;
81
69
    }else if( val!=phaseReqs[n] ){
82
67
      phaseReqs[n] = 0;
83
    }
84
  }
85
339352
  if( newReqPol ){
86
360872
    for( int i=0; i<(int)n.getNumChildren(); i++ ){
87
226836
      if( n.getKind()==IMPLIES && i==0 ){
88
        computePhaseReqs( n[i], !newPolarity, phaseReqs );
89
      }else{
90
226836
        computePhaseReqs( n[i], newPolarity, phaseReqs );
91
      }
92
    }
93
  }
94
339352
}
95
96
1450259
void QuantPhaseReq::getPolarity( Node n, int child, bool hasPol, bool pol, bool& newHasPol, bool& newPol ) {
97
1450259
  if( n.getKind()==AND || n.getKind()==OR || n.getKind()==SEP_STAR ){
98
329268
    newHasPol = hasPol;
99
329268
    newPol = pol;
100
1120991
  }else if( n.getKind()==IMPLIES ){
101
8
    newHasPol = hasPol;
102
8
    newPol = child==0 ? !pol : pol;
103
1120983
  }else if( n.getKind()==NOT ){
104
168670
    newHasPol = hasPol;
105
168670
    newPol = !pol;
106
952313
  }else if( n.getKind()==ITE ){
107
14160
    newHasPol = (child!=0) && hasPol;
108
14160
    newPol = pol;
109
938153
  }else if( n.getKind()==FORALL ){
110
164
    newHasPol = (child==1) && hasPol;
111
164
    newPol = pol;
112
  }else{
113
937989
    newHasPol = false;
114
937989
    newPol = false;
115
  }
116
1450259
}
117
118
4180996
void QuantPhaseReq::getEntailPolarity( Node n, int child, bool hasPol, bool pol, bool& newHasPol, bool& newPol ) {
119
4180996
  if( n.getKind()==AND || n.getKind()==OR || n.getKind()==SEP_STAR ){
120
2136210
    newHasPol = hasPol && pol!=( n.getKind()==OR );
121
2136210
    newPol = pol;
122
2044786
  }else if( n.getKind()==IMPLIES ){
123
22948
    newHasPol = hasPol && !pol;
124
22948
    newPol = child==0 ? !pol : pol;
125
2021838
  }else if( n.getKind()==NOT ){
126
259456
    newHasPol = hasPol;
127
259456
    newPol = !pol;
128
  }else{
129
1762382
    newHasPol = false;
130
1762382
    newPol = false;
131
  }
132
4180996
}
133
134
}  // namespace theory
135
29358
}  // namespace cvc5