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