summaryrefslogtreecommitdiff
path: root/src/theory/quantifiers/quant_epr.cpp
blob: 187466aaa1758820de4e283628b57942268ba08d (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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*********************                                                        */
/*! \file quant_epr.cpp
 ** \verbatim
 ** Top contributors (to current version):
 **   Andrew Reynolds, Morgan Deters, Tim King
 ** This file is part of the CVC4 project.
 ** Copyright (c) 2009-2017 by the authors listed in the file AUTHORS
 ** in the top-level source directory) and their institutional affiliations.
 ** All rights reserved.  See the file COPYING in the top-level source
 ** directory for licensing information.\endverbatim
 **
 ** \brief Implementation of quantifier EPR.
 **/

#include "theory/quantifiers/quant_epr.h"

#include "theory/quantifiers/quant_util.h"

using namespace CVC4::kind;

namespace CVC4 {
namespace theory {
namespace quantifiers {

void QuantEPR::registerNode(Node n,
                            std::map<int, std::map<Node, bool> >& visited,
                            bool beneathQuant,
                            bool hasPol,
                            bool pol)
{
  int vindex = hasPol ? (pol ? 1 : -1) : 0;
  if (visited[vindex].find(n) == visited[vindex].end())
  {
    visited[vindex][n] = true;
    Trace("quant-epr-debug") << "QuantEPR::registerNode " << n << std::endl;
    if (n.getKind() == FORALL)
    {
      if (beneathQuant || !hasPol || !pol)
      {
        for (unsigned i = 0; i < n[0].getNumChildren(); i++)
        {
          TypeNode tn = n[0][i].getType();
          if (d_non_epr.find(tn) == d_non_epr.end())
          {
            Trace("quant-epr") << "Sort " << tn
                               << " is non-EPR because of nested or possible "
                                  "existential quantification."
                               << std::endl;
            d_non_epr[tn] = true;
          }
        }
      }
      else
      {
        beneathQuant = true;
      }
    }
    TypeNode tn = n.getType();

    if (n.getNumChildren() > 0)
    {
      if (tn.isSort())
      {
        if (d_non_epr.find(tn) == d_non_epr.end())
        {
          Trace("quant-epr") << "Sort " << tn << " is non-EPR because of " << n
                             << std::endl;
          d_non_epr[tn] = true;
        }
      }
      for (unsigned i = 0; i < n.getNumChildren(); i++)
      {
        bool newHasPol;
        bool newPol;
        QuantPhaseReq::getPolarity(n, i, hasPol, pol, newHasPol, newPol);
        registerNode(n[i], visited, beneathQuant, newHasPol, newPol);
      }
    }
    else if (tn.isSort())
    {
      if (n.getKind() == BOUND_VARIABLE)
      {
        if (d_consts.find(tn) == d_consts.end())
        {
          // mark that at least one constant must occur
          d_consts[tn].clear();
        }
      }
      else if (std::find(d_consts[tn].begin(), d_consts[tn].end(), n)
               == d_consts[tn].end())
      {
        d_consts[tn].push_back(n);
        Trace("quant-epr") << "...constant of type " << tn << " : " << n
                           << std::endl;
      }
    }
  }
}

void QuantEPR::registerAssertion(Node assertion)
{
  std::map<int, std::map<Node, bool> > visited;
  registerNode(assertion, visited, false, true, true);
}

void QuantEPR::finishInit()
{
  Trace("quant-epr-debug") << "QuantEPR::finishInit" << std::endl;
  for (std::map<TypeNode, std::vector<Node> >::iterator it = d_consts.begin();
       it != d_consts.end();
       ++it)
  {
    Assert(d_epr_axiom.find(it->first) == d_epr_axiom.end());
    Trace("quant-epr-debug") << "process " << it->first << std::endl;
    if (d_non_epr.find(it->first) != d_non_epr.end())
    {
      Trace("quant-epr-debug") << "...non-epr" << std::endl;
      it->second.clear();
    }
    else
    {
      Trace("quant-epr-debug") << "...epr, #consts = " << it->second.size()
                               << std::endl;
      if (it->second.empty())
      {
        it->second.push_back(NodeManager::currentNM()->mkSkolem(
            "e", it->first, "EPR base constant"));
      }
      if (Trace.isOn("quant-epr"))
      {
        Trace("quant-epr") << "Type " << it->first
                           << " is EPR, with constants : " << std::endl;
        for (unsigned i = 0; i < it->second.size(); i++)
        {
          Trace("quant-epr") << "  " << it->second[i] << std::endl;
        }
      }
    }
  }
}

bool QuantEPR::isEPRConstant(TypeNode tn, Node k)
{
  return std::find(d_consts[tn].begin(), d_consts[tn].end(), k)
         != d_consts[tn].end();
}

void QuantEPR::addEPRConstant(TypeNode tn, Node k)
{
  Assert(isEPR(tn));
  Assert(d_epr_axiom.find(tn) == d_epr_axiom.end());
  if (!isEPRConstant(tn, k))
  {
    d_consts[tn].push_back(k);
  }
}

Node QuantEPR::mkEPRAxiom(TypeNode tn)
{
  Assert(isEPR(tn));
  std::map<TypeNode, Node>::iterator ita = d_epr_axiom.find(tn);
  if (ita == d_epr_axiom.end())
  {
    std::vector<Node> disj;
    Node x = NodeManager::currentNM()->mkBoundVar(tn);
    for (unsigned i = 0; i < d_consts[tn].size(); i++)
    {
      disj.push_back(
          NodeManager::currentNM()->mkNode(EQUAL, x, d_consts[tn][i]));
    }
    Assert(!disj.empty());
    d_epr_axiom[tn] = NodeManager::currentNM()->mkNode(
        FORALL,
        NodeManager::currentNM()->mkNode(BOUND_VAR_LIST, x),
        disj.size() == 1 ? disj[0]
                         : NodeManager::currentNM()->mkNode(OR, disj));
    return d_epr_axiom[tn];
  }
  else
  {
    return ita->second;
  }
}

} /* CVC4::theory::quantifiers namespace */
} /* CVC4::theory namespace */
} /* CVC4 namespace */
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback