summaryrefslogtreecommitdiff
path: root/src/theory/quantifiers/single_inv_partition.cpp
blob: 6c7a06ebe210b7459cedbbf7a85291c912c55920 (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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
/*********************                                                        */
/*! \file single_inv_partition.cpp
 ** \verbatim
 ** Top contributors (to current version):
 **   Andrew Reynolds, Morgan Deters
 ** This file is part of the CVC4 project.
 ** Copyright (c) 2009-2019 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 utility for processing single invocation synthesis conjectures
 **
 **/
#include "theory/quantifiers/single_inv_partition.h"

#include "expr/node_algorithm.h"
#include "theory/quantifiers/term_util.h"

using namespace CVC4;
using namespace CVC4::kind;
using namespace std;

namespace CVC4 {
namespace theory {
namespace quantifiers {

bool SingleInvocationPartition::init(Node n)
{
  // first, get types of arguments for functions
  std::vector<TypeNode> typs;
  std::map<Node, bool> visited;
  std::vector<Node> funcs;
  if (inferArgTypes(n, typs, visited))
  {
    return init(funcs, typs, n, false);
  }
  else
  {
    Trace("si-prt") << "Could not infer argument types." << std::endl;
    return false;
  }
}

Node SingleInvocationPartition::getFirstOrderVariableForFunction(Node f) const
{
  std::map<Node, Node>::const_iterator it = d_func_fo_var.find(f);
  if (it != d_func_fo_var.end())
  {
    return it->second;
  }
  return Node::null();
}

Node SingleInvocationPartition::getFunctionForFirstOrderVariable(Node v) const
{
  std::map<Node, Node>::const_iterator it = d_fo_var_to_func.find(v);
  if (it != d_fo_var_to_func.end())
  {
    return it->second;
  }
  return Node::null();
}

Node SingleInvocationPartition::getFunctionInvocationFor(Node f) const
{
  std::map<Node, Node>::const_iterator it = d_func_inv.find(f);
  if (it != d_func_inv.end())
  {
    return it->second;
  }
  return Node::null();
}

void SingleInvocationPartition::getFunctionVariables(
    std::vector<Node>& fvars) const
{
  fvars.insert(fvars.end(), d_func_vars.begin(), d_func_vars.end());
}

void SingleInvocationPartition::getFunctions(std::vector<Node>& fs) const
{
  fs.insert(fs.end(), d_all_funcs.begin(), d_all_funcs.end());
}

void SingleInvocationPartition::getSingleInvocationVariables(
    std::vector<Node>& sivars) const
{
  sivars.insert(sivars.end(), d_si_vars.begin(), d_si_vars.end());
}

void SingleInvocationPartition::getAllVariables(std::vector<Node>& vars) const
{
  vars.insert(vars.end(), d_all_vars.begin(), d_all_vars.end());
}

// gets the argument type list for the first APPLY_UF we see
bool SingleInvocationPartition::inferArgTypes(Node n,
                                              std::vector<TypeNode>& typs,
                                              std::map<Node, bool>& visited)
{
  if (visited.find(n) == visited.end())
  {
    visited[n] = true;
    if (n.getKind() != FORALL)
    {
      if (n.getKind() == APPLY_UF)
      {
        for (unsigned i = 0; i < n.getNumChildren(); i++)
        {
          typs.push_back(n[i].getType());
        }
        return true;
      }
      else
      {
        for (unsigned i = 0; i < n.getNumChildren(); i++)
        {
          if (inferArgTypes(n[i], typs, visited))
          {
            return true;
          }
        }
      }
    }
  }
  return false;
}

bool SingleInvocationPartition::init(std::vector<Node>& funcs, Node n)
{
  Trace("si-prt") << "Initialize with " << funcs.size() << " input functions ("
                  << funcs << ")..." << std::endl;
  std::vector<TypeNode> typs;
  if (!funcs.empty())
  {
    TypeNode tn0 = funcs[0].getType();
    if (tn0.getNumChildren() > 0)
    {
      for (unsigned i = 0, nargs = tn0.getNumChildren() - 1; i < nargs; i++)
      {
        typs.push_back(tn0[i]);
      }
    }
    for (unsigned i = 1, size = funcs.size(); i < size; i++)
    {
      TypeNode tni = funcs[i].getType();
      if (tni.getNumChildren() != tn0.getNumChildren())
      {
        // can't anti-skolemize functions of different sort
        Trace("si-prt") << "...type mismatch" << std::endl;
        return false;
      }
      else if (tni.getNumChildren() > 0)
      {
        for (unsigned j = 0, nargs = tni.getNumChildren() - 1; j < nargs; j++)
        {
          if (tni[j] != typs[j])
          {
            Trace("si-prt") << "...argument type mismatch" << std::endl;
            return false;
          }
        }
      }
    }
  }
  Trace("si-prt") << "#types = " << typs.size() << std::endl;
  return init(funcs, typs, n, true);
}

bool SingleInvocationPartition::init(std::vector<Node>& funcs,
                                     std::vector<TypeNode>& typs,
                                     Node n,
                                     bool has_funcs)
{
  Assert(d_arg_types.empty());
  Assert(d_input_funcs.empty());
  Assert(d_si_vars.empty());
  NodeManager* nm = NodeManager::currentNM();
  d_has_input_funcs = has_funcs;
  d_arg_types.insert(d_arg_types.end(), typs.begin(), typs.end());
  d_input_funcs.insert(d_input_funcs.end(), funcs.begin(), funcs.end());
  Trace("si-prt") << "Initialize..." << std::endl;
  for (unsigned j = 0; j < d_arg_types.size(); j++)
  {
    std::stringstream ss;
    ss << "s_" << j;
    Node si_v = nm->mkBoundVar(ss.str(), d_arg_types[j]);
    d_si_vars.push_back(si_v);
  }
  Assert(d_si_vars.size() == d_arg_types.size());
  for (const Node& inf : d_input_funcs)
  {
    Node sk = nm->mkSkolem("_sik", inf.getType());
    d_input_func_sks.push_back(sk);
  }
  Trace("si-prt") << "SingleInvocationPartition::process " << n << std::endl;
  Trace("si-prt") << "Get conjuncts..." << std::endl;
  std::vector<Node> conj;
  if (collectConjuncts(n, true, conj))
  {
    Trace("si-prt") << "...success." << std::endl;
    for (unsigned i = 0; i < conj.size(); i++)
    {
      std::vector<Node> si_terms;
      std::vector<Node> si_subs;
      Trace("si-prt") << "Process conjunct : " << conj[i] << std::endl;
      // do DER on conjunct
      // Must avoid eliminating the first-order input functions in the
      // getQuantSimplify step below. We use a substitution to avoid this.
      // This makes it so that e.g. the synthesis conjecture:
      //   exists f. f!=0 ^ P
      // is not rewritten to exists f. (f=0 => false) ^ P and subsquently
      // rewritten to exists f. false ^ P by the elimination f -> 0.
      Node cr = conj[i].substitute(d_input_funcs.begin(),
                                   d_input_funcs.end(),
                                   d_input_func_sks.begin(),
                                   d_input_func_sks.end());
      cr = TermUtil::getQuantSimplify(cr);
      cr = cr.substitute(d_input_func_sks.begin(),
                         d_input_func_sks.end(),
                         d_input_funcs.begin(),
                         d_input_funcs.end());
      if (cr != conj[i])
      {
        Trace("si-prt-debug") << "...rewritten to " << cr << std::endl;
      }
      std::map<Node, bool> visited;
      // functions to arguments
      std::vector<Node> args;
      std::vector<Node> terms;
      std::vector<Node> subs;
      bool singleInvocation = true;
      bool ngroundSingleInvocation = false;
      if (processConjunct(cr, visited, args, terms, subs))
      {
        for (unsigned j = 0; j < terms.size(); j++)
        {
          si_terms.push_back(subs[j]);
          Node op = subs[j].hasOperator() ? subs[j].getOperator() : subs[j];
          Assert(d_func_fo_var.find(op) != d_func_fo_var.end());
          si_subs.push_back(d_func_fo_var[op]);
        }
        std::map<Node, Node> subs_map;
        std::map<Node, Node> subs_map_rev;
        std::vector<Node> funcs;
        // normalize the invocations
        if (!terms.empty())
        {
          Assert(terms.size() == subs.size());
          cr = cr.substitute(
              terms.begin(), terms.end(), subs.begin(), subs.end());
        }
        std::vector<Node> children;
        children.push_back(cr);
        terms.clear();
        subs.clear();
        Trace("si-prt") << "...single invocation, with arguments: "
                        << std::endl;
        for (unsigned j = 0; j < args.size(); j++)
        {
          Trace("si-prt") << args[j] << " ";
          if (args[j].getKind() == BOUND_VARIABLE
              && std::find(terms.begin(), terms.end(), args[j]) == terms.end())
          {
            terms.push_back(args[j]);
            subs.push_back(d_si_vars[j]);
          }
          else
          {
            children.push_back(d_si_vars[j].eqNode(args[j]).negate());
          }
        }
        Trace("si-prt") << std::endl;
        cr = children.size() == 1
                 ? children[0]
                 : NodeManager::currentNM()->mkNode(OR, children);
        Assert(terms.size() == subs.size());
        cr =
            cr.substitute(terms.begin(), terms.end(), subs.begin(), subs.end());
        Trace("si-prt-debug") << "...normalized invocations to " << cr
                              << std::endl;
        // now must check if it has other bound variables
        std::unordered_set<Node, NodeHashFunction> fvs;
        expr::getFreeVariables(cr, fvs);
        // bound variables must be contained in the single invocation variables
        for (const Node& bv : fvs)
        {
          if (std::find(d_si_vars.begin(), d_si_vars.end(), bv)
              == d_si_vars.end())
          {
            // getFreeVariables also collects functions in the rare case that
            // we are synthesizing a function with 0 arguments, take this into
            // account here.
            if (std::find(d_input_funcs.begin(), d_input_funcs.end(), bv)
                == d_input_funcs.end())
            {
              Trace("si-prt")
                  << "...not ground single invocation." << std::endl;
              ngroundSingleInvocation = true;
              singleInvocation = false;
            }
          }
        }
        if (singleInvocation)
        {
          Trace("si-prt") << "...ground single invocation" << std::endl;
        }
      }
      else
      {
        Trace("si-prt") << "...not single invocation." << std::endl;
        singleInvocation = false;
        // rename bound variables with maximal overlap with si_vars
        std::unordered_set<Node, NodeHashFunction> fvs;
        expr::getFreeVariables(cr, fvs);
        std::vector<Node> terms;
        std::vector<Node> subs;
        for (const Node& v : fvs)
        {
          TypeNode tn = v.getType();
          Trace("si-prt-debug")
              << "Fit bound var: " << v << " with si." << std::endl;
          for (unsigned k = 0; k < d_si_vars.size(); k++)
          {
            if (tn == d_arg_types[k])
            {
              if (std::find(subs.begin(), subs.end(), d_si_vars[k])
                  == subs.end())
              {
                terms.push_back(v);
                subs.push_back(d_si_vars[k]);
                Trace("si-prt-debug") << "  ...use " << d_si_vars[k]
                                      << std::endl;
                break;
              }
            }
          }
        }
        Assert(terms.size() == subs.size());
        cr =
            cr.substitute(terms.begin(), terms.end(), subs.begin(), subs.end());
      }
      cr = Rewriter::rewrite(cr);
      Trace("si-prt") << ".....got si=" << singleInvocation
                      << ", result : " << cr << std::endl;
      d_conjuncts[2].push_back(cr);
      std::unordered_set<Node, NodeHashFunction> fvs;
      expr::getFreeVariables(cr, fvs);
      d_all_vars.insert(d_all_vars.end(), fvs.begin(), fvs.end());
      if (singleInvocation)
      {
        // replace with single invocation formulation
        Assert(si_terms.size() == si_subs.size());
        cr = cr.substitute(
            si_terms.begin(), si_terms.end(), si_subs.begin(), si_subs.end());
        cr = Rewriter::rewrite(cr);
        Trace("si-prt") << ".....si version=" << cr << std::endl;
        d_conjuncts[0].push_back(cr);
      }
      else
      {
        d_conjuncts[1].push_back(cr);
        if (ngroundSingleInvocation)
        {
          d_conjuncts[3].push_back(cr);
        }
      }
    }
  }
  else
  {
    Trace("si-prt") << "...failed." << std::endl;
    return false;
  }
  return true;
}

bool SingleInvocationPartition::collectConjuncts(Node n,
                                                 bool pol,
                                                 std::vector<Node>& conj)
{
  if ((!pol && n.getKind() == OR) || (pol && n.getKind() == AND))
  {
    for (unsigned i = 0; i < n.getNumChildren(); i++)
    {
      if (!collectConjuncts(n[i], pol, conj))
      {
        return false;
      }
    }
  }
  else if (n.getKind() == NOT)
  {
    return collectConjuncts(n[0], !pol, conj);
  }
  else if (n.getKind() == FORALL)
  {
    return false;
  }
  else
  {
    if (!pol)
    {
      n = TermUtil::simpleNegate(n);
    }
    Trace("si-prt") << "Conjunct : " << n << std::endl;
    conj.push_back(n);
  }
  return true;
}

bool SingleInvocationPartition::processConjunct(Node n,
                                                std::map<Node, bool>& visited,
                                                std::vector<Node>& args,
                                                std::vector<Node>& terms,
                                                std::vector<Node>& subs)
{
  std::map<Node, bool>::iterator it = visited.find(n);
  if (it != visited.end())
  {
    return true;
  }
  else
  {
    bool ret = true;
    for (unsigned i = 0; i < n.getNumChildren(); i++)
    {
      if (!processConjunct(n[i], visited, args, terms, subs))
      {
        ret = false;
      }
    }
    if (ret)
    {
      Node f;
      bool success = false;
      if (d_has_input_funcs)
      {
        f = n.hasOperator() ? n.getOperator() : n;
        if (std::find(d_input_funcs.begin(), d_input_funcs.end(), f)
            != d_input_funcs.end())
        {
          success = true;
        }
      }
      else
      {
        if (n.getKind() == kind::APPLY_UF)
        {
          f = n.getOperator();
          success = true;
        }
      }
      if (success)
      {
        if (std::find(terms.begin(), terms.end(), n) == terms.end())
        {
          // check if it matches the type requirement
          if (isAntiSkolemizableType(f))
          {
            if (args.empty())
            {
              // record arguments
              for (unsigned i = 0; i < n.getNumChildren(); i++)
              {
                args.push_back(n[i]);
              }
            }
            else
            {
              // arguments must be the same as those already recorded
              for (unsigned i = 0; i < n.getNumChildren(); i++)
              {
                if (args[i] != n[i])
                {
                  Trace("si-prt-debug") << "...bad invocation : " << n
                                        << " at arg " << i << "." << std::endl;
                  ret = false;
                  break;
                }
              }
            }
            if (ret)
            {
              terms.push_back(n);
              subs.push_back(d_func_inv[f]);
            }
          }
          else
          {
            Trace("si-prt-debug") << "... " << f << " is a bad operator."
                                  << std::endl;
            ret = false;
          }
        }
      }
    }
    //}
    visited[n] = ret;
    return ret;
  }
}

bool SingleInvocationPartition::isAntiSkolemizableType(Node f)
{
  std::map<Node, bool>::iterator it = d_funcs.find(f);
  if (it != d_funcs.end())
  {
    return it->second;
  }
  else
  {
    TypeNode tn = f.getType();
    bool ret = false;
    if (((tn.isFunction() && tn.getNumChildren() == d_arg_types.size() + 1)
         || (d_arg_types.empty() && tn.getNumChildren() == 0)))
    {
      ret = true;
      std::vector<Node> children;
      children.push_back(f);
      // TODO: permutations of arguments
      for (unsigned i = 0; i < d_arg_types.size(); i++)
      {
        children.push_back(d_si_vars[i]);
        if (tn[i] != d_arg_types[i])
        {
          ret = false;
          break;
        }
      }
      if (ret)
      {
        Node t;
        if (children.size() > 1)
        {
          t = NodeManager::currentNM()->mkNode(kind::APPLY_UF, children);
        }
        else
        {
          t = children[0];
        }
        d_func_inv[f] = t;
        std::stringstream ss;
        ss << "F_" << f;
        TypeNode rt;
        if (d_arg_types.empty())
        {
          rt = tn;
        }
        else
        {
          rt = tn.getRangeType();
        }
        Node v = NodeManager::currentNM()->mkBoundVar(ss.str(), rt);
        d_func_fo_var[f] = v;
        d_fo_var_to_func[v] = f;
        d_func_vars.push_back(v);
        d_all_funcs.push_back(f);
      }
    }
    d_funcs[f] = ret;
    return ret;
  }
}

Node SingleInvocationPartition::getConjunct(int index)
{
  return d_conjuncts[index].empty() ? NodeManager::currentNM()->mkConst(true)
                                    : (d_conjuncts[index].size() == 1
                                           ? d_conjuncts[index][0]
                                           : NodeManager::currentNM()->mkNode(
                                                 AND, d_conjuncts[index]));
}

void SingleInvocationPartition::debugPrint(const char* c)
{
  Trace(c) << "Single invocation variables : ";
  for (unsigned i = 0; i < d_si_vars.size(); i++)
  {
    Trace(c) << d_si_vars[i] << " ";
  }
  Trace(c) << std::endl;
  Trace(c) << "Functions : " << std::endl;
  for (std::map<Node, bool>::iterator it = d_funcs.begin(); it != d_funcs.end();
       ++it)
  {
    Trace(c) << "  " << it->first << " : ";
    if (it->second)
    {
      Trace(c) << d_func_inv[it->first] << " " << d_func_fo_var[it->first]
               << std::endl;
    }
    else
    {
      Trace(c) << "not incorporated." << std::endl;
    }
  }
  for (unsigned i = 0; i < 4; i++)
  {
    Trace(c) << (i == 0 ? "Single invocation"
                        : (i == 1 ? "Non-single invocation"
                                  : (i == 2 ? "All"
                                            : "Non-ground single invocation")));
    Trace(c) << " conjuncts: " << std::endl;
    for (unsigned j = 0; j < d_conjuncts[i].size(); j++)
    {
      Trace(c) << "  " << (j + 1) << " : " << d_conjuncts[i][j] << std::endl;
    }
  }
  Trace(c) << std::endl;
}

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