GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/theory/quantifiers/quant_util.cpp Lines: 69 84 82.1 %
Date: 2021-11-06 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
76475
QuantifiersUtil::QuantifiersUtil(Env& env) : EnvObj(env) {}
26
27
117774
QuantPhaseReq::QuantPhaseReq( Node n, bool computeEq ){
28
117774
  initialize( n, computeEq );
29
117774
}
30
31
117774
void QuantPhaseReq::initialize( Node n, bool computeEq ){
32
235548
  std::map< Node, int > phaseReqs2;
33
117774
  computePhaseReqs( n, false, phaseReqs2 );
34
322814
  for( std::map< Node, int >::iterator it = phaseReqs2.begin(); it != phaseReqs2.end(); ++it ){
35
205040
    if( it->second==1 ){
36
92664
      d_phase_reqs[ it->first ] = true;
37
112376
    }else if( it->second==-1 ){
38
112287
      d_phase_reqs[ it->first ] = false;
39
    }
40
  }
41
117774
  Debug("inst-engine-phase-req") << "Phase requirements for " << n << ":" << std::endl;
42
  //now, compute if any patterns are equality required
43
117774
  if( computeEq ){
44
    for( std::map< Node, bool >::iterator it = d_phase_reqs.begin(); it != d_phase_reqs.end(); ++it ){
45
      Debug("inst-engine-phase-req") << "   " << it->first << " -> " << it->second << std::endl;
46
      if( it->first.getKind()==EQUAL ){
47
        if( quantifiers::TermUtil::hasInstConstAttr(it->first[0]) ){
48
          if( !quantifiers::TermUtil::hasInstConstAttr(it->first[1]) ){
49
            d_phase_reqs_equality_term[ it->first[0] ] = it->first[1];
50
            d_phase_reqs_equality[ it->first[0] ] = it->second;
51
            Debug("inst-engine-phase-req") << "      " << it->first[0] << ( it->second ? " == " : " != " ) << it->first[1] << std::endl;
52
          }
53
        }else if( quantifiers::TermUtil::hasInstConstAttr(it->first[1]) ){
54
          d_phase_reqs_equality_term[ it->first[1] ] = it->first[0];
55
          d_phase_reqs_equality[ it->first[1] ] = it->second;
56
          Debug("inst-engine-phase-req") << "      " << it->first[1] << ( it->second ? " == " : " != " ) << it->first[0] << std::endl;
57
        }
58
      }
59
    }
60
  }
61
117774
}
62
63
355611
void QuantPhaseReq::computePhaseReqs( Node n, bool polarity, std::map< Node, int >& phaseReqs ){
64
355611
  bool newReqPol = false;
65
  bool newPolarity;
66
355611
  if( n.getKind()==NOT ){
67
92755
    newReqPol = true;
68
92755
    newPolarity = !polarity;
69
262856
  }else if( n.getKind()==OR || n.getKind()==IMPLIES ){
70
47223
    if( !polarity ){
71
47223
      newReqPol = true;
72
47223
      newPolarity = false;
73
    }
74
215633
  }else if( n.getKind()==AND ){
75
10502
    if( polarity ){
76
      newReqPol = true;
77
      newPolarity = true;
78
    }
79
  }else{
80
205131
    int val = polarity ? 1 : -1;
81
205131
    if( phaseReqs.find( n )==phaseReqs.end() ){
82
205040
      phaseReqs[n] = val;
83
91
    }else if( val!=phaseReqs[n] ){
84
89
      phaseReqs[n] = 0;
85
    }
86
  }
87
355611
  if( newReqPol ){
88
377815
    for( int i=0; i<(int)n.getNumChildren(); i++ ){
89
237837
      if( n.getKind()==IMPLIES && i==0 ){
90
        computePhaseReqs( n[i], !newPolarity, phaseReqs );
91
      }else{
92
237837
        computePhaseReqs( n[i], newPolarity, phaseReqs );
93
      }
94
    }
95
  }
96
355611
}
97
98
1596361
void QuantPhaseReq::getPolarity(
99
    Node n, size_t child, bool hasPol, bool pol, bool& newHasPol, bool& newPol)
100
{
101
1596361
  if( n.getKind()==AND || n.getKind()==OR || n.getKind()==SEP_STAR ){
102
372560
    newHasPol = hasPol;
103
372560
    newPol = pol;
104
1223801
  }else if( n.getKind()==IMPLIES ){
105
8
    newHasPol = hasPol;
106
8
    newPol = child==0 ? !pol : pol;
107
1223793
  }else if( n.getKind()==NOT ){
108
185986
    newHasPol = hasPol;
109
185986
    newPol = !pol;
110
1037807
  }else if( n.getKind()==ITE ){
111
17706
    newHasPol = (child!=0) && hasPol;
112
17706
    newPol = pol;
113
1020101
  }else if( n.getKind()==FORALL ){
114
152
    newHasPol = (child==1) && hasPol;
115
152
    newPol = pol;
116
  }else{
117
1019949
    newHasPol = false;
118
1019949
    newPol = false;
119
  }
120
1596361
}
121
122
3269207
void QuantPhaseReq::getEntailPolarity(
123
    Node n, size_t child, bool hasPol, bool pol, bool& newHasPol, bool& newPol)
124
{
125
3269207
  if( n.getKind()==AND || n.getKind()==OR || n.getKind()==SEP_STAR ){
126
1143770
    newHasPol = hasPol && pol!=( n.getKind()==OR );
127
1143770
    newPol = pol;
128
2125437
  }else if( n.getKind()==IMPLIES ){
129
23036
    newHasPol = hasPol && !pol;
130
23036
    newPol = child==0 ? !pol : pol;
131
2102401
  }else if( n.getKind()==NOT ){
132
240224
    newHasPol = hasPol;
133
240224
    newPol = !pol;
134
  }else{
135
1862177
    newHasPol = false;
136
1862177
    newPol = false;
137
  }
138
3269207
}
139
140
}  // namespace theory
141
31137
}  // namespace cvc5