GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/theory/quantifiers/sygus/sygus_qe_preproc.cpp Lines: 82 83 98.8 %
Date: 2021-09-29 Branches: 178 374 47.6 %

Line Exec Source
1
/******************************************************************************
2
 * Top contributors (to current version):
3
 *   Andrew Reynolds, Mathias Preiner, Morgan Deters
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
 * Sygus quantifier elimination preprocessor.
14
 */
15
16
#include "theory/quantifiers/sygus/sygus_qe_preproc.h"
17
18
#include "expr/node_algorithm.h"
19
#include "expr/skolem_manager.h"
20
#include "theory/quantifiers/single_inv_partition.h"
21
#include "theory/smt_engine_subsolver.h"
22
23
using namespace cvc5::kind;
24
25
namespace cvc5 {
26
namespace theory {
27
namespace quantifiers {
28
29
1193
SygusQePreproc::SygusQePreproc(Env& env) : EnvObj(env) {}
30
31
5
Node SygusQePreproc::preprocess(Node q)
32
{
33
10
  Node body = q[1];
34
5
  if (body.getKind() == NOT && body[0].getKind() == FORALL)
35
  {
36
4
    body = body[0][1];
37
  }
38
5
  NodeManager* nm = NodeManager::currentNM();
39
5
  SkolemManager* sm = nm->getSkolemManager();
40
10
  Trace("cegqi-qep") << "Compute single invocation for " << q << "..."
41
5
                     << std::endl;
42
10
  quantifiers::SingleInvocationPartition sip;
43
10
  std::vector<Node> funcs0;
44
5
  funcs0.insert(funcs0.end(), q[0].begin(), q[0].end());
45
5
  sip.init(funcs0, body);
46
5
  Trace("cegqi-qep") << "...finished, got:" << std::endl;
47
5
  sip.debugPrint("cegqi-qep");
48
49
5
  if (sip.isPurelySingleInvocation() || !sip.isNonGroundSingleInvocation())
50
  {
51
3
    return Node::null();
52
  }
53
  // create new smt engine to do quantifier elimination
54
4
  std::unique_ptr<SmtEngine> smt_qe;
55
2
  initializeSubsolver(smt_qe, d_env);
56
4
  Trace("cegqi-qep") << "Property is non-ground single invocation, run "
57
2
                        "QE to obtain single invocation."
58
2
                     << std::endl;
59
  // partition variables
60
4
  std::vector<Node> all_vars;
61
2
  sip.getAllVariables(all_vars);
62
4
  std::vector<Node> si_vars;
63
2
  sip.getSingleInvocationVariables(si_vars);
64
4
  std::vector<Node> qe_vars;
65
4
  std::vector<Node> nqe_vars;
66
70
  for (unsigned i = 0, size = all_vars.size(); i < size; i++)
67
  {
68
136
    Node v = all_vars[i];
69
68
    if (std::find(funcs0.begin(), funcs0.end(), v) != funcs0.end())
70
    {
71
63
      Trace("cegqi-qep") << "- fun var: " << v << std::endl;
72
    }
73
5
    else if (std::find(si_vars.begin(), si_vars.end(), v) == si_vars.end())
74
    {
75
2
      qe_vars.push_back(v);
76
2
      Trace("cegqi-qep") << "- qe var: " << v << std::endl;
77
    }
78
    else
79
    {
80
3
      nqe_vars.push_back(v);
81
3
      Trace("cegqi-qep") << "- non qe var: " << v << std::endl;
82
    }
83
  }
84
4
  std::vector<Node> orig;
85
4
  std::vector<Node> subs;
86
  // skolemize non-qe variables
87
5
  for (unsigned i = 0, size = nqe_vars.size(); i < size; i++)
88
  {
89
    Node k = sm->mkDummySkolem(
90
6
        "k", nqe_vars[i].getType(), "qe for non-ground single invocation");
91
3
    orig.push_back(nqe_vars[i]);
92
3
    subs.push_back(k);
93
6
    Trace("cegqi-qep") << "  subs : " << nqe_vars[i] << " -> " << k
94
3
                       << std::endl;
95
  }
96
4
  std::vector<Node> funcs1;
97
2
  sip.getFunctions(funcs1);
98
65
  for (unsigned i = 0, size = funcs1.size(); i < size; i++)
99
  {
100
126
    Node f = funcs1[i];
101
126
    Node fi = sip.getFunctionInvocationFor(f);
102
126
    Node fv = sip.getFirstOrderVariableForFunction(f);
103
63
    Assert(!fi.isNull());
104
63
    orig.push_back(fi);
105
    Node k = sm->mkDummySkolem(
106
126
        "k", fv.getType(), "qe for function in non-ground single invocation");
107
63
    subs.push_back(k);
108
63
    Trace("cegqi-qep") << "  subs : " << fi << " -> " << k << std::endl;
109
  }
110
4
  Node conj_se_ngsi = sip.getFullSpecification();
111
2
  Trace("cegqi-qep") << "Full specification is " << conj_se_ngsi << std::endl;
112
  Node conj_se_ngsi_subs = conj_se_ngsi.substitute(
113
4
      orig.begin(), orig.end(), subs.begin(), subs.end());
114
2
  Assert(!qe_vars.empty());
115
6
  conj_se_ngsi_subs = nm->mkNode(
116
8
      EXISTS, nm->mkNode(BOUND_VAR_LIST, qe_vars), conj_se_ngsi_subs.negate());
117
118
4
  Trace("cegqi-qep") << "Run quantifier elimination on " << conj_se_ngsi_subs
119
2
                     << std::endl;
120
4
  Node qeRes = smt_qe->getQuantifierElimination(conj_se_ngsi_subs, true, false);
121
2
  Trace("cegqi-qep") << "Result : " << qeRes << std::endl;
122
123
  // create single invocation conjecture, if QE was successful
124
2
  if (!expr::hasBoundVar(qeRes))
125
  {
126
2
    qeRes =
127
4
        qeRes.substitute(subs.begin(), subs.end(), orig.begin(), orig.end());
128
2
    if (!nqe_vars.empty())
129
    {
130
1
      qeRes = nm->mkNode(EXISTS, nm->mkNode(BOUND_VAR_LIST, nqe_vars), qeRes);
131
    }
132
2
    Assert(q.getNumChildren() == 3);
133
2
    qeRes = nm->mkNode(FORALL, q[0], qeRes, q[2]);
134
4
    Trace("cegqi-qep") << "Converted conjecture after QE : " << qeRes
135
2
                       << std::endl;
136
2
    qeRes = rewrite(qeRes);
137
4
    Node nq = qeRes;
138
    // must assert it is equivalent to the original
139
4
    Node lem = q.eqNode(nq);
140
4
    Trace("cegqi-lemma") << "Cegqi::Lemma : qe-preprocess : " << lem
141
2
                         << std::endl;
142
2
    return lem;
143
  }
144
  return Node::null();
145
}
146
147
}  // namespace quantifiers
148
}  // namespace theory
149
22746
}  // namespace cvc5