1 |
|
/****************************************************************************** |
2 |
|
* Top contributors (to current version): |
3 |
|
* Andrew Reynolds, 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 first order model for full model check. |
14 |
|
*/ |
15 |
|
|
16 |
|
#include "theory/quantifiers/fmf/first_order_model_fmc.h" |
17 |
|
|
18 |
|
#include "expr/attribute.h" |
19 |
|
#include "expr/skolem_manager.h" |
20 |
|
#include "theory/quantifiers/fmf/full_model_check.h" |
21 |
|
#include "theory/rewriter.h" |
22 |
|
|
23 |
|
using namespace cvc5::kind; |
24 |
|
|
25 |
|
namespace cvc5 { |
26 |
|
namespace theory { |
27 |
|
namespace quantifiers { |
28 |
|
namespace fmcheck { |
29 |
|
|
30 |
|
/** |
31 |
|
* Marks that a term represents the entire domain of quantified formula for |
32 |
|
* the finite model finding fmc algorithm. |
33 |
|
*/ |
34 |
|
struct IsStarAttributeId |
35 |
|
{ |
36 |
|
}; |
37 |
|
using IsStarAttribute = expr::Attribute<IsStarAttributeId, bool>; |
38 |
|
|
39 |
1444 |
FirstOrderModelFmc::FirstOrderModelFmc(QuantifiersState& qs, |
40 |
|
QuantifiersRegistry& qr, |
41 |
1444 |
TermRegistry& tr) |
42 |
1444 |
: FirstOrderModel(qs, qr, tr) |
43 |
|
{ |
44 |
1444 |
} |
45 |
|
|
46 |
4332 |
FirstOrderModelFmc::~FirstOrderModelFmc() |
47 |
|
{ |
48 |
2489 |
for (std::pair<const Node, Def*>& d : d_models) |
49 |
|
{ |
50 |
1045 |
delete d.second; |
51 |
|
} |
52 |
2888 |
} |
53 |
|
|
54 |
3958 |
void FirstOrderModelFmc::processInitialize(bool ispre) |
55 |
|
{ |
56 |
3958 |
if (!ispre) |
57 |
|
{ |
58 |
1979 |
return; |
59 |
|
} |
60 |
8063 |
for (std::pair<const Node, Def*>& d : d_models) |
61 |
|
{ |
62 |
6084 |
d.second->reset(); |
63 |
|
} |
64 |
|
} |
65 |
|
|
66 |
152971 |
void FirstOrderModelFmc::processInitializeModelForTerm(Node n) |
67 |
|
{ |
68 |
152971 |
if (n.getKind() == APPLY_UF) |
69 |
|
{ |
70 |
|
// cannot be a bound variable |
71 |
54538 |
Node op = n.getOperator(); |
72 |
27269 |
if (op.getKind() != BOUND_VARIABLE) |
73 |
|
{ |
74 |
26900 |
if (d_models.find(op) == d_models.end()) |
75 |
|
{ |
76 |
1045 |
d_models[op] = new Def; |
77 |
|
} |
78 |
|
} |
79 |
|
} |
80 |
152971 |
} |
81 |
|
|
82 |
2079584 |
bool FirstOrderModelFmc::isStar(Node n) |
83 |
|
{ |
84 |
2079584 |
return n.getAttribute(IsStarAttribute()); |
85 |
|
} |
86 |
|
|
87 |
828667 |
Node FirstOrderModelFmc::getStar(TypeNode tn) |
88 |
|
{ |
89 |
828667 |
std::map<TypeNode, Node>::iterator it = d_type_star.find(tn); |
90 |
828667 |
if (it != d_type_star.end()) |
91 |
|
{ |
92 |
827879 |
return it->second; |
93 |
|
} |
94 |
788 |
SkolemManager* sm = NodeManager::currentNM()->getSkolemManager(); |
95 |
|
Node st = |
96 |
1576 |
sm->mkDummySkolem("star", tn, "skolem created for full-model checking"); |
97 |
788 |
d_type_star[tn] = st; |
98 |
788 |
st.setAttribute(IsStarAttribute(), true); |
99 |
788 |
return st; |
100 |
|
} |
101 |
|
|
102 |
7129 |
Node FirstOrderModelFmc::getFunctionValue(Node op, const char* argPrefix) |
103 |
|
{ |
104 |
7129 |
Trace("fmc-model") << "Get function value for " << op << std::endl; |
105 |
7129 |
NodeManager* nm = NodeManager::currentNM(); |
106 |
14258 |
TypeNode type = op.getType(); |
107 |
14258 |
std::vector<Node> vars; |
108 |
19061 |
for (size_t i = 0, nargs = type.getNumChildren() - 1; i < nargs; i++) |
109 |
|
{ |
110 |
23864 |
std::stringstream ss; |
111 |
11932 |
ss << argPrefix << (i + 1); |
112 |
23864 |
Node b = nm->mkBoundVar(ss.str(), type[i]); |
113 |
11932 |
vars.push_back(b); |
114 |
|
} |
115 |
14258 |
Node boundVarList = nm->mkNode(BOUND_VAR_LIST, vars); |
116 |
14258 |
Node curr; |
117 |
7129 |
Def* odef = d_models[op]; |
118 |
23878 |
for (size_t i = 0, ncond = odef->d_cond.size(); i < ncond; i++) |
119 |
|
{ |
120 |
16749 |
size_t ii = (ncond - 1) - i; |
121 |
33498 |
Node v = odef->d_value[ii]; |
122 |
16749 |
Trace("fmc-model-func") << "Value is : " << v << std::endl; |
123 |
16749 |
Assert(v.isConst()); |
124 |
16749 |
if (curr.isNull()) |
125 |
|
{ |
126 |
7129 |
Trace("fmc-model-func") << "base : " << v << std::endl; |
127 |
7129 |
curr = v; |
128 |
|
} |
129 |
|
else |
130 |
|
{ |
131 |
|
// make the condition |
132 |
19240 |
Node cond = odef->d_cond[ii]; |
133 |
9620 |
Trace("fmc-model-func") << "...cond : " << cond << std::endl; |
134 |
19240 |
std::vector<Node> children; |
135 |
29093 |
for (size_t j = 0, nchild = cond.getNumChildren(); j < nchild; j++) |
136 |
|
{ |
137 |
38946 |
TypeNode tn = vars[j].getType(); |
138 |
19473 |
if (!isStar(cond[j])) |
139 |
|
{ |
140 |
38946 |
Node c = getRepresentative(cond[j]); |
141 |
19473 |
c = getRepresentative(c); |
142 |
19473 |
children.push_back(nm->mkNode(EQUAL, vars[j], c)); |
143 |
|
} |
144 |
|
} |
145 |
9620 |
Assert(!children.empty()); |
146 |
19240 |
Node cc = nm->mkAnd(children); |
147 |
|
|
148 |
19240 |
Trace("fmc-model-func") |
149 |
9620 |
<< "condition : " << cc << ", value : " << v << std::endl; |
150 |
9620 |
curr = nm->mkNode(ITE, cc, v, curr); |
151 |
|
} |
152 |
|
} |
153 |
7129 |
Trace("fmc-model") << "Made " << curr << " for " << op << std::endl; |
154 |
7129 |
curr = Rewriter::rewrite(curr); |
155 |
14258 |
return nm->mkNode(LAMBDA, boundVarList, curr); |
156 |
|
} |
157 |
|
|
158 |
|
} // namespace fmcheck |
159 |
|
} // namespace quantifiers |
160 |
|
} // namespace theory |
161 |
29340 |
} // namespace cvc5 |