GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/theory/quantifiers/quant_util.cpp Lines: 69 84 82.1 %
Date: 2021-11-05 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
76470
QuantifiersUtil::QuantifiersUtil(Env& env) : EnvObj(env) {}
26
27
117764
QuantPhaseReq::QuantPhaseReq( Node n, bool computeEq ){
28
117764
  initialize( n, computeEq );
29
117764
}
30
31
117764
void QuantPhaseReq::initialize( Node n, bool computeEq ){
32
235528
  std::map< Node, int > phaseReqs2;
33
117764
  computePhaseReqs( n, false, phaseReqs2 );
34
322794
  for( std::map< Node, int >::iterator it = phaseReqs2.begin(); it != phaseReqs2.end(); ++it ){
35
205030
    if( it->second==1 ){
36
92664
      d_phase_reqs[ it->first ] = true;
37
112366
    }else if( it->second==-1 ){
38
112277
      d_phase_reqs[ it->first ] = false;
39
    }
40
  }
41
117764
  Debug("inst-engine-phase-req") << "Phase requirements for " << n << ":" << std::endl;
42
  //now, compute if any patterns are equality required
43
117764
  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
117764
}
62
63
355597
void QuantPhaseReq::computePhaseReqs( Node n, bool polarity, std::map< Node, int >& phaseReqs ){
64
355597
  bool newReqPol = false;
65
  bool newPolarity;
66
355597
  if( n.getKind()==NOT ){
67
92755
    newReqPol = true;
68
92755
    newPolarity = !polarity;
69
262842
  }else if( n.getKind()==OR || n.getKind()==IMPLIES ){
70
47219
    if( !polarity ){
71
47219
      newReqPol = true;
72
47219
      newPolarity = false;
73
    }
74
215623
  }else if( n.getKind()==AND ){
75
10502
    if( polarity ){
76
      newReqPol = true;
77
      newPolarity = true;
78
    }
79
  }else{
80
205121
    int val = polarity ? 1 : -1;
81
205121
    if( phaseReqs.find( n )==phaseReqs.end() ){
82
205030
      phaseReqs[n] = val;
83
91
    }else if( val!=phaseReqs[n] ){
84
89
      phaseReqs[n] = 0;
85
    }
86
  }
87
355597
  if( newReqPol ){
88
377807
    for( int i=0; i<(int)n.getNumChildren(); i++ ){
89
237833
      if( n.getKind()==IMPLIES && i==0 ){
90
        computePhaseReqs( n[i], !newPolarity, phaseReqs );
91
      }else{
92
237833
        computePhaseReqs( n[i], newPolarity, phaseReqs );
93
      }
94
    }
95
  }
96
355597
}
97
98
1596028
void QuantPhaseReq::getPolarity(
99
    Node n, size_t child, bool hasPol, bool pol, bool& newHasPol, bool& newPol)
100
{
101
1596028
  if( n.getKind()==AND || n.getKind()==OR || n.getKind()==SEP_STAR ){
102
372450
    newHasPol = hasPol;
103
372450
    newPol = pol;
104
1223578
  }else if( n.getKind()==IMPLIES ){
105
8
    newHasPol = hasPol;
106
8
    newPol = child==0 ? !pol : pol;
107
1223570
  }else if( n.getKind()==NOT ){
108
185939
    newHasPol = hasPol;
109
185939
    newPol = !pol;
110
1037631
  }else if( n.getKind()==ITE ){
111
17706
    newHasPol = (child!=0) && hasPol;
112
17706
    newPol = pol;
113
1019925
  }else if( n.getKind()==FORALL ){
114
152
    newHasPol = (child==1) && hasPol;
115
152
    newPol = pol;
116
  }else{
117
1019773
    newHasPol = false;
118
1019773
    newPol = false;
119
  }
120
1596028
}
121
122
3269474
void QuantPhaseReq::getEntailPolarity(
123
    Node n, size_t child, bool hasPol, bool pol, bool& newHasPol, bool& newPol)
124
{
125
3269474
  if( n.getKind()==AND || n.getKind()==OR || n.getKind()==SEP_STAR ){
126
1143840
    newHasPol = hasPol && pol!=( n.getKind()==OR );
127
1143840
    newPol = pol;
128
2125634
  }else if( n.getKind()==IMPLIES ){
129
23036
    newHasPol = hasPol && !pol;
130
23036
    newPol = child==0 ? !pol : pol;
131
2102598
  }else if( n.getKind()==NOT ){
132
240290
    newHasPol = hasPol;
133
240290
    newPol = !pol;
134
  }else{
135
1862308
    newHasPol = false;
136
1862308
    newPol = false;
137
  }
138
3269474
}
139
140
}  // namespace theory
141
31125
}  // namespace cvc5