GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/theory/sep/theory_sep.cpp Lines: 923 1089 84.8 %
Date: 2021-08-17 Branches: 2283 5462 41.8 %

Line Exec Source
1
/******************************************************************************
2
 * Top contributors (to current version):
3
 *   Andrew Reynolds, Haniel Barbosa, Tim King
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 the theory of separation logic.
14
 */
15
16
#include "theory/sep/theory_sep.h"
17
18
#include <map>
19
20
#include "base/map_util.h"
21
#include "expr/emptyset.h"
22
#include "expr/kind.h"
23
#include "expr/skolem_manager.h"
24
#include "options/quantifiers_options.h"
25
#include "options/sep_options.h"
26
#include "options/smt_options.h"
27
#include "smt/logic_exception.h"
28
#include "theory/decision_manager.h"
29
#include "theory/quantifiers/term_database.h"
30
#include "theory/quantifiers/term_util.h"
31
#include "theory/quantifiers_engine.h"
32
#include "theory/rewriter.h"
33
#include "theory/sep/theory_sep_rewriter.h"
34
#include "theory/theory_model.h"
35
#include "theory/valuation.h"
36
#include "util/cardinality.h"
37
38
using namespace std;
39
using namespace cvc5::kind;
40
41
namespace cvc5 {
42
namespace theory {
43
namespace sep {
44
45
9850
TheorySep::TheorySep(Env& env, OutputChannel& out, Valuation valuation)
46
    : Theory(THEORY_SEP, env, out, valuation),
47
9850
      d_lemmas_produced_c(getUserContext()),
48
      d_bounds_init(false),
49
      d_state(env, valuation),
50
      d_im(*this, d_state, d_pnm, "theory::sep::"),
51
      d_notify(*this),
52
9850
      d_reduce(getUserContext()),
53
29550
      d_spatial_assertions(getSatContext())
54
{
55
9850
  d_true = NodeManager::currentNM()->mkConst<bool>(true);
56
9850
  d_false = NodeManager::currentNM()->mkConst<bool>(false);
57
58
  // indicate we are using the default theory state object
59
9850
  d_theoryState = &d_state;
60
9850
  d_inferManager = &d_im;
61
9850
}
62
63
29550
TheorySep::~TheorySep() {
64
10143
  for( std::map< Node, HeapAssertInfo * >::iterator it = d_eqc_info.begin(); it != d_eqc_info.end(); ++it ){
65
293
    delete it->second;
66
  }
67
19700
}
68
69
121
void TheorySep::declareSepHeap(TypeNode locT, TypeNode dataT)
70
{
71
121
  if (!d_type_ref.isNull())
72
  {
73
4
    TypeNode te1 = d_loc_to_data_type.begin()->first;
74
4
    std::stringstream ss;
75
2
    ss << "ERROR: cannot declare heap types for separation logic more than "
76
          "once.  We are declaring heap of type ";
77
2
    ss << locT << " -> " << dataT << ", but we already have ";
78
2
    ss << d_type_ref << " -> " << d_type_data;
79
2
    throw LogicException(ss.str());
80
  }
81
238
  Node nullAtom;
82
119
  registerRefDataTypes(locT, dataT, nullAtom);
83
119
}
84
85
9850
TheoryRewriter* TheorySep::getTheoryRewriter() { return &d_rewriter; }
86
87
3766
ProofRuleChecker* TheorySep::getProofChecker() { return nullptr; }
88
89
9850
bool TheorySep::needsEqualityEngine(EeSetupInfo& esi)
90
{
91
9850
  esi.d_notify = &d_notify;
92
9850
  esi.d_name = "theory::sep::ee";
93
9850
  esi.d_notifyMerge = true;
94
9850
  return true;
95
}
96
97
9850
void TheorySep::finishInit()
98
{
99
9850
  Assert(d_equalityEngine != nullptr);
100
  // The kinds we are treating as function application in congruence
101
9850
  d_equalityEngine->addFunctionKind(kind::SEP_PTO);
102
  // we could but don't do congruence on SEP_STAR here.
103
9850
}
104
105
1876
void TheorySep::preRegisterTerm(TNode n)
106
{
107
1876
  Kind k = n.getKind();
108
1876
  if (k == SEP_PTO || k == SEP_EMP || k == SEP_STAR || k == SEP_WAND)
109
  {
110
282
    registerRefDataTypesAtom(n);
111
  }
112
1875
}
113
114
Node TheorySep::mkAnd( std::vector< TNode >& assumptions ) {
115
  if( assumptions.empty() ){
116
    return d_true;
117
  }else if( assumptions.size()==1 ){
118
    return assumptions[0];
119
  }else{
120
    return NodeManager::currentNM()->mkNode( kind::AND, assumptions );
121
  }
122
}
123
124
125
/////////////////////////////////////////////////////////////////////////////
126
// T-PROPAGATION / REGISTRATION
127
/////////////////////////////////////////////////////////////////////////////
128
129
3923
bool TheorySep::propagateLit(TNode literal)
130
{
131
3923
  return d_im.propagateLit(literal);
132
}
133
134
TrustNode TheorySep::explain(TNode literal)
135
{
136
  Debug("sep") << "TheorySep::explain(" << literal << ")" << std::endl;
137
  return d_im.explainLit(literal);
138
}
139
140
141
/////////////////////////////////////////////////////////////////////////////
142
// SHARING
143
/////////////////////////////////////////////////////////////////////////////
144
145
6544
void TheorySep::computeCareGraph() {
146
6544
  Debug("sharing") << "Theory::computeCareGraph<" << getId() << ">()" << endl;
147
7369
  for (unsigned i = 0; i < d_sharedTerms.size(); ++ i) {
148
1650
    TNode a = d_sharedTerms[i];
149
1650
    TypeNode aType = a.getType();
150
2429
    for (unsigned j = i + 1; j < d_sharedTerms.size(); ++ j) {
151
2545
      TNode b = d_sharedTerms[j];
152
1604
      if (b.getType() != aType) {
153
        // We don't care about the terms of different types
154
663
        continue;
155
      }
156
941
      switch (d_valuation.getEqualityStatus(a, b)) {
157
624
      case EQUALITY_TRUE_AND_PROPAGATED:
158
      case EQUALITY_FALSE_AND_PROPAGATED:
159
        // If we know about it, we should have propagated it, so we can skip
160
624
        break;
161
317
      default:
162
        // Let's split on it
163
317
        addCarePair(a, b);
164
317
        break;
165
      }
166
    }
167
  }
168
6544
}
169
170
171
/////////////////////////////////////////////////////////////////////////////
172
// MODEL GENERATION
173
/////////////////////////////////////////////////////////////////////////////
174
175
610
void TheorySep::postProcessModel( TheoryModel* m ){
176
610
  Trace("sep-model") << "Printing model for TheorySep..." << std::endl;
177
178
1220
  std::vector< Node > sep_children;
179
1220
  Node m_neq;
180
1220
  Node m_heap;
181
617
  for( std::map< TypeNode, Node >::iterator it = d_base_label.begin(); it != d_base_label.end(); ++it ){
182
    //should only be constructing for one heap type
183
7
    Assert(m_heap.isNull());
184
7
    Assert(d_loc_to_data_type.find(it->first) != d_loc_to_data_type.end());
185
7
    Trace("sep-model") << "Model for heap, type = " << it->first << " with data type " << d_loc_to_data_type[it->first] << " : " << std::endl;
186
14
    TypeNode data_type = d_loc_to_data_type[it->first];
187
7
    computeLabelModel( it->second );
188
7
    if( d_label_model[it->second].d_heap_locs_model.empty() ){
189
1
      Trace("sep-model") << "  [empty]" << std::endl;
190
    }else{
191
15
      for( unsigned j=0; j<d_label_model[it->second].d_heap_locs_model.size(); j++ ){
192
9
        Assert(d_label_model[it->second].d_heap_locs_model[j].getKind()
193
               == kind::SINGLETON);
194
18
        std::vector< Node > pto_children;
195
18
        Node l = d_label_model[it->second].d_heap_locs_model[j][0];
196
9
        Assert(l.isConst());
197
9
        pto_children.push_back( l );
198
9
        Trace("sep-model") << " " << l << " -> ";
199
9
        if( d_pto_model[l].isNull() ){
200
          Trace("sep-model") << "_";
201
          //m->d_comment_str << "_";
202
          TypeEnumerator te_range( data_type );
203
          if (d_state.isFiniteType(data_type))
204
          {
205
            pto_children.push_back( *te_range );
206
          }else{
207
            //must enumerate until we find one that is not explicitly pointed to
208
            bool success = false;
209
            Node cv;
210
            do{
211
              cv = *te_range;
212
              if( std::find( d_heap_locs_nptos[l].begin(), d_heap_locs_nptos[l].end(), cv )==d_heap_locs_nptos[l].end() ){
213
                success = true;
214
              }else{
215
                ++te_range;
216
              }
217
            }while( !success );
218
            pto_children.push_back( cv );
219
          }
220
        }else{
221
9
          Trace("sep-model") << d_pto_model[l];
222
18
          Node vpto = d_valuation.getModel()->getRepresentative( d_pto_model[l] );
223
9
          Assert(vpto.isConst());
224
9
          pto_children.push_back( vpto );
225
        }
226
9
        Trace("sep-model") << std::endl;
227
9
        sep_children.push_back( NodeManager::currentNM()->mkNode( kind::SEP_PTO, pto_children ) );
228
      }
229
    }
230
14
    Node nil = getNilRef( it->first );
231
14
    Node vnil = d_valuation.getModel()->getRepresentative( nil );
232
7
    m_neq = NodeManager::currentNM()->mkNode( kind::EQUAL, nil, vnil );
233
7
    Trace("sep-model") << "sep.nil = " << vnil << std::endl;
234
7
    Trace("sep-model") << std::endl;
235
7
    if( sep_children.empty() ){
236
2
      TypeEnumerator te_domain( it->first );
237
2
      TypeEnumerator te_range( d_loc_to_data_type[it->first] );
238
1
      m_heap = NodeManager::currentNM()->mkNode( kind::SEP_EMP, *te_domain, *te_range );
239
6
    }else if( sep_children.size()==1 ){
240
4
      m_heap = sep_children[0];
241
    }else{
242
2
      m_heap = NodeManager::currentNM()->mkNode( kind::SEP_STAR, sep_children );
243
    }
244
7
    m->setHeapModel( m_heap, m_neq );
245
  }
246
610
  Trace("sep-model") << "Finished printing model for TheorySep." << std::endl;
247
610
}
248
249
/////////////////////////////////////////////////////////////////////////////
250
// NOTIFICATIONS
251
/////////////////////////////////////////////////////////////////////////////
252
253
254
15201
void TheorySep::presolve() {
255
15201
  Trace("sep-pp") << "Presolving" << std::endl;
256
15201
}
257
258
259
/////////////////////////////////////////////////////////////////////////////
260
// MAIN SOLVER
261
/////////////////////////////////////////////////////////////////////////////
262
263
7044
bool TheorySep::preNotifyFact(
264
    TNode atom, bool polarity, TNode fact, bool isPrereg, bool isInternal)
265
{
266
14088
  TNode satom = atom.getKind() == SEP_LABEL ? atom[0] : atom;
267
14088
  TNode slbl = atom.getKind() == SEP_LABEL ? atom[1] : TNode::null();
268
7044
  bool isSpatial = isSpatialKind(satom.getKind());
269
7044
  if (isSpatial)
270
  {
271
3008
    reduceFact(atom, polarity, fact);
272
3008
    if (!slbl.isNull())
273
    {
274
2127
      d_spatial_assertions.push_back(fact);
275
    }
276
  }
277
  // assert to equality if non-spatial or a labelled pto
278
7044
  if (!isSpatial || (!slbl.isNull() && satom.getKind() == SEP_PTO))
279
  {
280
5103
    return false;
281
  }
282
  // otherwise, maybe propagate
283
1941
  doPending();
284
1941
  return true;
285
}
286
287
5103
void TheorySep::notifyFact(TNode atom,
288
                           bool polarity,
289
                           TNode fact,
290
                           bool isInternal)
291
{
292
10206
  TNode satom = atom.getKind() == SEP_LABEL ? atom[0] : atom;
293
5103
  if (atom.getKind() == SEP_LABEL && atom[0].getKind() == SEP_PTO)
294
  {
295
    // associate the equivalence class of the lhs with this pto
296
2134
    Node r = getRepresentative(atom[1]);
297
1067
    HeapAssertInfo* ei = getOrMakeEqcInfo(r, true);
298
1067
    addPto(ei, r, atom, polarity);
299
  }
300
  // maybe propagate
301
5103
  doPending();
302
5103
}
303
304
3008
void TheorySep::reduceFact(TNode atom, bool polarity, TNode fact)
305
{
306
3008
  if (d_reduce.find(fact) != d_reduce.end())
307
  {
308
    // already reduced
309
5011
    return;
310
  }
311
810
  d_reduce.insert(fact);
312
1005
  TNode satom = atom.getKind() == SEP_LABEL ? atom[0] : atom;
313
1005
  TNode slbl = atom.getKind() == SEP_LABEL ? atom[1] : TNode::null();
314
810
  NodeManager* nm = NodeManager::currentNM();
315
810
  SkolemManager* sm = nm->getSkolemManager();
316
810
  if (slbl.isNull())
317
  {
318
570
    Trace("sep-lemma-debug")
319
285
        << "Reducing unlabelled assertion " << atom << std::endl;
320
    // introduce top-level label, add iff
321
570
    TypeNode refType = getReferenceType(satom);
322
570
    Trace("sep-lemma-debug")
323
285
        << "...reference type is : " << refType << std::endl;
324
570
    Node b_lbl = getBaseLabel(refType);
325
570
    Node satom_new = nm->mkNode(SEP_LABEL, satom, b_lbl);
326
570
    Node lem;
327
285
    Trace("sep-lemma-debug") << "...polarity is " << polarity << std::endl;
328
285
    if (polarity)
329
    {
330
226
      lem = nm->mkNode(OR, satom.negate(), satom_new);
331
    }
332
    else
333
    {
334
59
      lem = nm->mkNode(OR, satom, satom_new.negate());
335
    }
336
570
    Trace("sep-lemma-debug")
337
285
        << "Sep::Lemma : base reduction : " << lem << std::endl;
338
285
    d_im.lemma(lem, InferenceId::SEP_LABEL_INTRO);
339
285
    return;
340
  }
341
525
  Trace("sep-lemma-debug") << "Reducing assertion " << fact << std::endl;
342
720
  Node conc;
343
525
  if (Node* in_map = FindOrNull(d_red_conc[slbl], satom))
344
  {
345
54
    conc = *in_map;
346
  }
347
  else
348
  {
349
    // make conclusion based on type of assertion
350
471
    if (satom.getKind() == SEP_STAR || satom.getKind() == SEP_WAND)
351
    {
352
326
      std::vector<Node> children;
353
326
      std::vector<Node> c_lems;
354
326
      TypeNode tn = getReferenceType(satom);
355
163
      if (d_reference_bound_max.find(tn) != d_reference_bound_max.end())
356
      {
357
161
        c_lems.push_back(nm->mkNode(SUBSET, slbl, d_reference_bound_max[tn]));
358
      }
359
326
      std::vector<Node> labels;
360
163
      getLabelChildren(satom, slbl, children, labels);
361
326
      Node empSet = nm->mkConst(EmptySet(slbl.getType()));
362
163
      Assert(children.size() > 1);
363
163
      if (satom.getKind() == SEP_STAR)
364
      {
365
        // reduction for heap : union, pairwise disjoint
366
292
        Node ulem = nm->mkNode(UNION, labels[0], labels[1]);
367
146
        size_t lsize = labels.size();
368
201
        for (size_t i = 2; i < lsize; i++)
369
        {
370
55
          ulem = nm->mkNode(UNION, ulem, labels[i]);
371
        }
372
146
        ulem = slbl.eqNode(ulem);
373
292
        Trace("sep-lemma-debug")
374
146
            << "Sep::Lemma : star reduction, union : " << ulem << std::endl;
375
146
        c_lems.push_back(ulem);
376
493
        for (size_t i = 0; i < lsize; i++)
377
        {
378
642
          for (size_t j = (i + 1); j < lsize; j++)
379
          {
380
590
            Node s = nm->mkNode(INTERSECTION, labels[i], labels[j]);
381
590
            Node ilem = s.eqNode(empSet);
382
590
            Trace("sep-lemma-debug")
383
295
                << "Sep::Lemma : star reduction, disjoint : " << ilem
384
295
                << std::endl;
385
295
            c_lems.push_back(ilem);
386
          }
387
        }
388
      }
389
      else
390
      {
391
34
        Node ulem = nm->mkNode(UNION, slbl, labels[0]);
392
17
        ulem = ulem.eqNode(labels[1]);
393
34
        Trace("sep-lemma-debug")
394
17
            << "Sep::Lemma : wand reduction, union : " << ulem << std::endl;
395
17
        c_lems.push_back(ulem);
396
34
        Node s = nm->mkNode(INTERSECTION, slbl, labels[0]);
397
34
        Node ilem = s.eqNode(empSet);
398
34
        Trace("sep-lemma-debug")
399
17
            << "Sep::Lemma : wand reduction, disjoint : " << ilem << std::endl;
400
17
        c_lems.push_back(ilem);
401
        // nil does not occur in labels[0]
402
34
        Node nr = getNilRef(tn);
403
34
        Node nrlem = nm->mkNode(MEMBER, nr, labels[0]).negate();
404
34
        Trace("sep-lemma")
405
17
            << "Sep::Lemma: sep.nil not in wand antecedant heap : " << nrlem
406
17
            << std::endl;
407
17
        d_im.lemma(nrlem, InferenceId::SEP_NIL_NOT_IN_HEAP);
408
      }
409
      // send out definitional lemmas for introduced sets
410
799
      for (const Node& clem : c_lems)
411
      {
412
636
        Trace("sep-lemma") << "Sep::Lemma : definition : " << clem << std::endl;
413
636
        d_im.lemma(clem, InferenceId::SEP_LABEL_DEF);
414
      }
415
163
      conc = nm->mkNode(AND, children);
416
    }
417
308
    else if (satom.getKind() == SEP_PTO)
418
    {
419
      // TODO(project##230): Find a safe type for the singleton operator
420
616
      Node ss = nm->mkSingleton(satom[0].getType(), satom[0]);
421
308
      if (slbl != ss)
422
      {
423
        conc = slbl.eqNode(ss);
424
      }
425
      // note semantics of sep.nil is enforced globally
426
    }
427
    else if (satom.getKind() == SEP_EMP)
428
    {
429
      Node lem;
430
      Node emp_s = nm->mkConst(EmptySet(slbl.getType()));
431
      if (polarity)
432
      {
433
        lem = nm->mkNode(OR, fact.negate(), slbl.eqNode(emp_s));
434
      }
435
      else
436
      {
437
        Node kl = sm->mkDummySkolem("loc", getReferenceType(satom));
438
        Node kd = sm->mkDummySkolem("data", getDataType(satom));
439
        Node econc = nm->mkNode(
440
            SEP_LABEL,
441
            nm->mkNode(SEP_STAR, nm->mkNode(SEP_PTO, kl, kd), d_true),
442
            slbl);
443
        // Node econc = nm->mkNode( AND, slbl.eqNode( emp_s ).negate(),
444
        lem = nm->mkNode(OR, fact.negate(), econc);
445
      }
446
      Trace("sep-lemma") << "Sep::Lemma : emp : " << lem << std::endl;
447
      d_im.lemma(lem, InferenceId::SEP_EMP);
448
    }
449
    else
450
    {
451
      // labeled emp should be rewritten
452
      Unreachable();
453
    }
454
471
    d_red_conc[slbl][satom] = conc;
455
  }
456
525
  if (conc.isNull())
457
  {
458
660
    Trace("sep-lemma-debug")
459
330
        << "Trivial conclusion, do not add lemma." << std::endl;
460
330
    return;
461
  }
462
195
  bool use_polarity = satom.getKind() == SEP_WAND ? !polarity : polarity;
463
195
  if (!use_polarity)
464
  {
465
    // introduce guard, assert positive version
466
160
    Trace("sep-lemma-debug")
467
80
        << "Negated spatial constraint asserted to sep theory: " << fact
468
80
        << std::endl;
469
160
    Node g = sm->mkDummySkolem("G", nm->booleanType());
470
240
    d_neg_guard_strategy[g].reset(new DecisionStrategySingleton(
471
160
        "sep_neg_guard", g, getSatContext(), getValuation()));
472
80
    DecisionStrategySingleton* ds = d_neg_guard_strategy[g].get();
473
80
    d_im.getDecisionManager()->registerStrategy(
474
        DecisionManager::STRAT_SEP_NEG_GUARD, ds);
475
160
    Node lit = ds->getLiteral(0);
476
80
    d_neg_guard[slbl][satom] = lit;
477
160
    Trace("sep-lemma-debug")
478
80
        << "Neg guard : " << slbl << " " << satom << " " << lit << std::endl;
479
80
    AlwaysAssert(!lit.isNull());
480
80
    d_neg_guards.push_back(lit);
481
80
    d_guard_to_assertion[lit] = satom;
482
    // Node lem = nm->mkNode( EQUAL, lit, conc );
483
160
    Node lem = nm->mkNode(OR, lit.negate(), conc);
484
80
    Trace("sep-lemma") << "Sep::Lemma : (neg) reduction : " << lem << std::endl;
485
80
    d_im.lemma(lem, InferenceId::SEP_NEG_REDUCTION);
486
  }
487
  else
488
  {
489
    // reduce based on implication
490
230
    Node lem = nm->mkNode(OR, fact.negate(), conc);
491
115
    Trace("sep-lemma") << "Sep::Lemma : reduction : " << lem << std::endl;
492
115
    d_im.lemma(lem, InferenceId::SEP_POS_REDUCTION);
493
  }
494
}
495
496
7044
bool TheorySep::isSpatialKind(Kind k) const
497
{
498
7044
  return k == SEP_STAR || k == SEP_WAND || k == SEP_PTO || k == SEP_EMP;
499
}
500
501
27047
void TheorySep::postCheck(Effort level)
502
{
503
54192
  if (level != EFFORT_LAST_CALL || d_state.isInConflict()
504
27145
      || d_valuation.needCheck())
505
  {
506
53959
    return;
507
  }
508
98
  NodeManager* nm = NodeManager::currentNM();
509
98
  SkolemManager* sm = nm->getSkolemManager();
510
98
  Trace("sep-process") << "Checking heap at full effort..." << std::endl;
511
98
  d_label_model.clear();
512
98
  d_tmodel.clear();
513
98
  d_pto_model.clear();
514
98
  Trace("sep-process") << "---Locations---" << std::endl;
515
135
  std::map<Node, int> min_id;
516
98
  for (std::map<TypeNode, std::vector<Node> >::iterator itt =
517
98
           d_type_references_all.begin();
518
196
       itt != d_type_references_all.end();
519
       ++itt)
520
  {
521
321
    for (const Node& t : itt->second)
522
    {
523
223
      Trace("sep-process") << "  " << t << " = ";
524
223
      if (d_valuation.getModel()->hasTerm(t))
525
      {
526
444
        Node v = d_valuation.getModel()->getRepresentative(t);
527
222
        Trace("sep-process") << v << std::endl;
528
        // take minimal id
529
222
        std::map<Node, unsigned>::iterator itrc = d_type_ref_card_id.find(t);
530
222
        int tid = itrc == d_type_ref_card_id.end() ? -1 : (int)itrc->second;
531
        bool set_term_model;
532
222
        if (d_tmodel.find(v) == d_tmodel.end())
533
        {
534
205
          set_term_model = true;
535
        }else{
536
17
          set_term_model = min_id[v] > tid;
537
        }
538
222
        if (set_term_model)
539
        {
540
205
          d_tmodel[v] = t;
541
205
          min_id[v] = tid;
542
        }
543
      }
544
      else
545
      {
546
1
        Trace("sep-process") << "?" << std::endl;
547
      }
548
    }
549
  }
550
98
  Trace("sep-process") << "---" << std::endl;
551
  // build positive/negative assertion lists for labels
552
135
  std::map<Node, bool> assert_active;
553
  // get the inactive assertions
554
135
  std::map<Node, std::vector<Node> > lbl_to_assertions;
555
429
  for (NodeList::const_iterator i = d_spatial_assertions.begin();
556
429
       i != d_spatial_assertions.end();
557
       ++i)
558
  {
559
662
    Node fact = (*i);
560
331
    bool polarity = fact.getKind() != NOT;
561
662
    TNode atom = polarity ? fact : fact[0];
562
331
    Assert(atom.getKind() == SEP_LABEL);
563
662
    TNode satom = atom[0];
564
662
    TNode slbl = atom[1];
565
331
    lbl_to_assertions[slbl].push_back(fact);
566
    // check whether assertion is active : either polarity=true, or guard is not
567
    // asserted false
568
331
    assert_active[fact] = true;
569
331
    bool use_polarity = satom.getKind() == SEP_WAND ? !polarity : polarity;
570
331
    if (use_polarity)
571
    {
572
223
      if (satom.getKind() == SEP_PTO)
573
      {
574
318
        Node vv = d_valuation.getModel()->getRepresentative(satom[0]);
575
159
        if (d_pto_model.find(vv) == d_pto_model.end())
576
        {
577
282
          Trace("sep-process") << "Pto : " << satom[0] << " (" << vv << ") -> "
578
141
                               << satom[1] << std::endl;
579
141
          d_pto_model[vv] = satom[1];
580
581
          // replace this on pto-model since this term is more relevant
582
282
          TypeNode vtn = vv.getType();
583
564
          if (std::find(d_type_references_all[vtn].begin(),
584
141
                        d_type_references_all[vtn].end(),
585
564
                        satom[0])
586
423
              != d_type_references_all[vtn].end())
587
          {
588
139
            d_tmodel[vv] = satom[0];
589
          }
590
        }
591
      }
592
    }
593
    else
594
    {
595
108
      if (d_neg_guard[slbl].find(satom) != d_neg_guard[slbl].end())
596
      {
597
        // check if the guard is asserted positively
598
186
        Node guard = d_neg_guard[slbl][satom];
599
        bool value;
600
93
        if (getValuation().hasSatValue(guard, value))
601
        {
602
93
          assert_active[fact] = value;
603
        }
604
      }
605
    }
606
  }
607
  //(recursively) set inactive sub-assertions
608
429
  for (NodeList::const_iterator i = d_spatial_assertions.begin();
609
429
       i != d_spatial_assertions.end();
610
       ++i)
611
  {
612
662
    Node fact = (*i);
613
331
    if (!assert_active[fact])
614
    {
615
28
      setInactiveAssertionRec(fact, lbl_to_assertions, assert_active);
616
    }
617
  }
618
  // set up model information based on active assertions
619
429
  for (NodeList::const_iterator i = d_spatial_assertions.begin();
620
429
       i != d_spatial_assertions.end();
621
       ++i)
622
  {
623
662
    Node fact = (*i);
624
331
    bool polarity = fact.getKind() != NOT;
625
662
    TNode atom = polarity ? fact : fact[0];
626
662
    TNode satom = atom[0];
627
662
    TNode slbl = atom[1];
628
331
    if (assert_active[fact])
629
    {
630
303
      computeLabelModel(slbl);
631
    }
632
  }
633
  // debug print
634
98
  if (Trace.isOn("sep-process"))
635
  {
636
    Trace("sep-process") << "--- Current spatial assertions : " << std::endl;
637
    for( NodeList::const_iterator i = d_spatial_assertions.begin(); i != d_spatial_assertions.end(); ++i ) {
638
      Node fact = (*i);
639
      Trace("sep-process") << "  " << fact;
640
      if (!assert_active[fact])
641
      {
642
        Trace("sep-process") << " [inactive]";
643
      }
644
      Trace("sep-process") << std::endl;
645
    }
646
    Trace("sep-process") << "---" << std::endl;
647
  }
648
98
  if (Trace.isOn("sep-eqc"))
649
  {
650
    Trace("sep-eqc") << d_equalityEngine->debugPrintEqc();
651
  }
652
653
98
  bool addedLemma = false;
654
98
  bool needAddLemma = false;
655
  // check negated star / positive wand
656
98
  if (options::sepCheckNeg())
657
  {
658
    // get active labels
659
196
    std::map<Node, bool> active_lbl;
660
98
    if (options::sepMinimalRefine())
661
    {
662
      for( NodeList::const_iterator i = d_spatial_assertions.begin(); i != d_spatial_assertions.end(); ++i ) {
663
        Node fact = (*i);
664
        bool polarity = fact.getKind() != NOT;
665
        TNode atom = polarity ? fact : fact[0];
666
        TNode satom = atom[0];
667
        bool use_polarity = satom.getKind() == SEP_WAND ? !polarity : polarity;
668
        if( !use_polarity ){
669
          Assert(assert_active.find(fact) != assert_active.end());
670
          if( assert_active[fact] ){
671
            Assert(atom.getKind() == SEP_LABEL);
672
            TNode slbl = atom[1];
673
            std::map<Node, std::map<int, Node> >& lms = d_label_map[satom];
674
            if (lms.find(slbl) != lms.end())
675
            {
676
              Trace("sep-process-debug")
677
                  << "Active lbl : " << slbl << std::endl;
678
              active_lbl[slbl] = true;
679
            }
680
          }
681
        }
682
      }
683
    }
684
685
    // process spatial assertions
686
429
    for (NodeList::const_iterator i = d_spatial_assertions.begin();
687
429
         i != d_spatial_assertions.end();
688
         ++i)
689
    {
690
398
      Node fact = (*i);
691
331
      bool polarity = fact.getKind() != NOT;
692
398
      TNode atom = polarity ? fact : fact[0];
693
398
      TNode satom = atom[0];
694
695
331
      bool use_polarity = satom.getKind() == SEP_WAND ? !polarity : polarity;
696
662
      Trace("sep-process-debug")
697
331
          << "  check atom : " << satom << " use polarity " << use_polarity
698
331
          << std::endl;
699
331
      if (use_polarity)
700
      {
701
223
        continue;
702
      }
703
108
      Assert(assert_active.find(fact) != assert_active.end());
704
134
      if (!assert_active[fact])
705
      {
706
52
        Trace("sep-process-debug")
707
26
            << "--> inactive negated assertion " << satom << std::endl;
708
26
        continue;
709
      }
710
82
      Assert(atom.getKind() == SEP_LABEL);
711
149
      TNode slbl = atom[1];
712
164
      Trace("sep-process") << "--> Active negated atom : " << satom
713
82
                           << ", lbl = " << slbl << std::endl;
714
      // add refinement lemma
715
97
      if (!ContainsKey(d_label_map[satom], slbl))
716
      {
717
15
        Trace("sep-process-debug") << "  no children." << std::endl;
718
15
        Assert(satom.getKind() == SEP_PTO || satom.getKind() == SEP_EMP);
719
15
        continue;
720
      }
721
67
      needAddLemma = true;
722
134
      TypeNode tn = getReferenceType(satom);
723
67
      tn = nm->mkSetType(tn);
724
      // tn = nm->mkSetType(nm->mkRefType(tn));
725
134
      Node o_b_lbl_mval = d_label_model[slbl].getValue(tn);
726
134
      Trace("sep-process") << "    Model for " << slbl << " : " << o_b_lbl_mval
727
67
                           << std::endl;
728
729
      // get model values
730
134
      std::map<int, Node> mvals;
731
225
      for (const std::pair<const int, Node>& sub_element : d_label_map[satom][slbl])
732
      {
733
158
        int sub_index = sub_element.first;
734
316
        Node sub_lbl = sub_element.second;
735
158
        computeLabelModel(sub_lbl);
736
316
        Node lbl_mval = d_label_model[sub_lbl].getValue(tn);
737
316
        Trace("sep-process-debug")
738
158
            << "  child " << sub_index << " : " << sub_lbl
739
158
            << ", mval = " << lbl_mval << std::endl;
740
158
        mvals[sub_index] = lbl_mval;
741
      }
742
743
      // Now, assert model-instantiated implication based on the negation
744
67
      Assert(d_label_model.find(slbl) != d_label_model.end());
745
134
      std::vector<Node> conc;
746
67
      bool inst_success = true;
747
      // new refinement
748
      // instantiate the label
749
134
      std::map<Node, Node> visited;
750
      Node inst = instantiateLabel(satom,
751
                                   slbl,
752
                                   slbl,
753
                                   o_b_lbl_mval,
754
                                   visited,
755
                                   d_pto_model,
756
                                   tn,
757
134
                                   active_lbl);
758
67
      Trace("sep-inst-debug") << "    applied inst : " << inst << std::endl;
759
67
      if (inst.isNull())
760
      {
761
        inst_success = false;
762
      }
763
      else
764
      {
765
67
        inst = Rewriter::rewrite(inst);
766
67
        if (inst == (polarity ? d_true : d_false))
767
        {
768
          inst_success = false;
769
        }
770
67
        conc.push_back(polarity ? inst : inst.negate());
771
      }
772
67
      if (!inst_success)
773
      {
774
        continue;
775
      }
776
134
      std::vector<Node> lemc;
777
134
      Node pol_atom = atom;
778
67
      if (polarity)
779
      {
780
13
        pol_atom = atom.negate();
781
      }
782
67
      lemc.push_back(pol_atom);
783
67
      lemc.insert(lemc.end(), conc.begin(), conc.end());
784
134
      Node lem = nm->mkNode(OR, lemc);
785
67
      std::vector<Node>& rlems = d_refinement_lem[satom][slbl];
786
67
      if (std::find(rlems.begin(), rlems.end(), lem) == rlems.end())
787
      {
788
67
        rlems.push_back(lem);
789
134
        Trace("sep-process") << "-----> refinement lemma (#" << rlems.size()
790
67
                             << ") : " << lem << std::endl;
791
134
        Trace("sep-lemma") << "Sep::Lemma : negated star/wand refinement : "
792
67
                           << lem << std::endl;
793
67
        d_im.lemma(lem, InferenceId::SEP_REFINEMENT);
794
67
        addedLemma = true;
795
      }
796
      else
797
      {
798
        // this typically should not happen, should never happen for complete
799
        // base theories
800
        Trace("sep-process")
801
            << "*** repeated refinement lemma : " << lem << std::endl;
802
        Trace("sep-warn")
803
            << "TheorySep : WARNING : repeated refinement lemma : " << lem
804
            << "!!!" << std::endl;
805
      }
806
    }
807
196
    Trace("sep-process")
808
98
        << "...finished check of negated assertions, addedLemma=" << addedLemma
809
98
        << ", needAddLemma=" << needAddLemma << std::endl;
810
  }
811
98
  if (addedLemma)
812
  {
813
61
    return;
814
  }
815
  // must witness finite data points-to
816
74
  for (std::map<TypeNode, Node>::iterator it = d_base_label.begin();
817
74
       it != d_base_label.end();
818
       ++it)
819
  {
820
37
    TypeNode data_type = d_loc_to_data_type[it->first];
821
    // if the data type is finite
822
37
    if (!d_state.isFiniteType(data_type))
823
    {
824
37
      continue;
825
    }
826
    computeLabelModel(it->second);
827
    Trace("sep-process-debug") << "Check heap data for " << it->first << " -> "
828
                               << data_type << std::endl;
829
    std::vector<Node>& hlmodel = d_label_model[it->second].d_heap_locs_model;
830
    for (size_t j = 0, hsize = hlmodel.size(); j < hsize; j++)
831
    {
832
      Assert(hlmodel[j].getKind() == SINGLETON);
833
      Node l = hlmodel[j][0];
834
      Trace("sep-process-debug") << "  location : " << l << std::endl;
835
      if (!d_pto_model[l].isNull())
836
      {
837
        Trace("sep-process-debug")
838
            << "  points-to data witness : " << d_pto_model[l] << std::endl;
839
        continue;
840
      }
841
      needAddLemma = true;
842
      Node ll;
843
      std::map<Node, Node>::iterator itm = d_tmodel.find(l);
844
      if (itm != d_tmodel.end())
845
      {
846
        ll = itm->second;
847
      }
848
      // otherwise, could try to assign arbitrary skolem?
849
      if (!ll.isNull())
850
      {
851
        Trace("sep-process") << "Must witness label : " << ll
852
                             << ", data type is " << data_type << std::endl;
853
        Node dsk = sm->mkDummySkolem(
854
            "dsk", data_type, "pto-data for implicit location");
855
        // if location is in the heap, then something must point to it
856
        Node lem = nm->mkNode(
857
            IMPLIES,
858
            nm->mkNode(MEMBER, ll, it->second),
859
            nm->mkNode(SEP_STAR, nm->mkNode(SEP_PTO, ll, dsk), d_true));
860
        Trace("sep-lemma") << "Sep::Lemma : witness finite data-pto : " << lem
861
                           << std::endl;
862
        d_im.lemma(lem, InferenceId::SEP_WITNESS_FINITE_DATA);
863
        addedLemma = true;
864
      }
865
      else
866
      {
867
        // This should only happen if we are in an unbounded fragment
868
        Trace("sep-warn")
869
            << "TheorySep : WARNING : no term corresponding to location " << l
870
            << " in heap!!!" << std::endl;
871
      }
872
    }
873
  }
874
37
  if (addedLemma)
875
  {
876
    return;
877
  }
878
  // set up model
879
37
  Trace("sep-process-debug") << "...preparing sep model..." << std::endl;
880
37
  d_heap_locs_nptos.clear();
881
  // collect data points that are not pointed to
882
364
  for (context::CDList<Assertion>::const_iterator it = facts_begin();
883
364
       it != facts_end();
884
       ++it)
885
  {
886
654
    Node lit = (*it).d_assertion;
887
327
    if (lit.getKind() == NOT && lit[0].getKind() == SEP_PTO)
888
    {
889
2
      Node satom = lit[0];
890
2
      Node v1 = d_valuation.getModel()->getRepresentative(satom[0]);
891
2
      Node v2 = d_valuation.getModel()->getRepresentative(satom[1]);
892
2
      Trace("sep-process-debug")
893
1
          << v1 << " does not point-to " << v2 << std::endl;
894
1
      d_heap_locs_nptos[v1].push_back(v2);
895
    }
896
  }
897
898
37
  if (needAddLemma)
899
  {
900
    d_im.setIncomplete(IncompleteId::SEP);
901
  }
902
74
  Trace("sep-check") << "Sep::check(): " << level
903
74
                     << " done, conflict=" << d_state.isInConflict()
904
37
                     << std::endl;
905
}
906
907
5939
bool TheorySep::needsCheckLastEffort() {
908
5939
  return hasFacts();
909
}
910
911
void TheorySep::conflict(TNode a, TNode b) {
912
  Trace("sep-conflict") << "Sep::conflict : " << a << " " << b << std::endl;
913
  d_im.conflictEqConstantMerge(a, b);
914
}
915
916
917
293
TheorySep::HeapAssertInfo::HeapAssertInfo( context::Context* c ) : d_pto(c), d_has_neg_pto(c,false) {
918
919
293
}
920
921
37263
TheorySep::HeapAssertInfo * TheorySep::getOrMakeEqcInfo( Node n, bool doMake ) {
922
37263
  std::map< Node, HeapAssertInfo* >::iterator e_i = d_eqc_info.find( n );
923
37263
  if( e_i==d_eqc_info.end() ){
924
35362
    if( doMake ){
925
293
      HeapAssertInfo* ei = new HeapAssertInfo( getSatContext() );
926
293
      d_eqc_info[n] = ei;
927
293
      return ei;
928
    }else{
929
35069
      return NULL;
930
    }
931
  }else{
932
1901
    return (*e_i).second;
933
  }
934
}
935
936
//for now, assume all constraints are for the same heap type (ensured by logic exceptions thrown in computeReferenceType2)
937
1279
TypeNode TheorySep::getReferenceType( Node n ) {
938
1279
  Assert(!d_type_ref.isNull());
939
1279
  return d_type_ref;
940
}
941
942
TypeNode TheorySep::getDataType( Node n ) {
943
  Assert(!d_type_data.isNull());
944
  return d_type_data;
945
}
946
947
// Must process assertions at preprocess so that quantified assertions are
948
// processed properly.
949
13737
void TheorySep::ppNotifyAssertions(const std::vector<Node>& assertions) {
950
27474
  std::map<int, std::map<Node, int> > visited;
951
27474
  std::map<int, std::map<Node, std::vector<Node> > > references;
952
27474
  std::map<int, std::map<Node, bool> > references_strict;
953
138568
  for (unsigned i = 0; i < assertions.size(); i++) {
954
124831
    Trace("sep-pp") << "Process assertion : " << assertions[i] << std::endl;
955
124831
    processAssertion(assertions[i], visited, references, references_strict,
956
                     true, true, false);
957
  }
958
  // if data type is unconstrained, assume a fresh uninterpreted sort
959
13737
  if (!d_type_ref.isNull()) {
960
117
    if (d_type_data.isNull()) {
961
      d_type_data = NodeManager::currentNM()->mkSort("_sep_U");
962
      Trace("sep-type") << "Sep: assume data type " << d_type_data << std::endl;
963
      d_loc_to_data_type[d_type_ref] = d_type_data;
964
    }
965
  }
966
13737
}
967
968
//return cardinality
969
3837122
int TheorySep::processAssertion(
970
    Node n,
971
    std::map<int, std::map<Node, int> >& visited,
972
    std::map<int, std::map<Node, std::vector<Node> > >& references,
973
    std::map<int, std::map<Node, bool> >& references_strict,
974
    bool pol,
975
    bool hasPol,
976
    bool underSpatial)
977
{
978
3837122
  int index = hasPol ? ( pol ? 1 : -1 ) : 0;
979
3837122
  int card = 0;
980
3837122
  std::map< Node, int >::iterator it = visited[index].find( n );
981
3837122
  if( it==visited[index].end() ){
982
1651645
    Trace("sep-pp-debug") << "process assertion : " << n << ", index = " << index << std::endl;
983
1651645
    if( n.getKind()==kind::SEP_EMP ){
984
45
      registerRefDataTypesAtom(n);
985
45
      if( hasPol && pol ){
986
15
        references[index][n].clear();
987
15
        references_strict[index][n] = true;
988
      }else{
989
30
        card = 1;
990
      }
991
1651600
    }else if( n.getKind()==kind::SEP_PTO ){
992
367
      registerRefDataTypesAtom(n);
993
367
      if( quantifiers::TermUtil::hasBoundVarAttr( n[0] ) ){
994
2
        TypeNode tn1 = n[0].getType();
995
1
        if( d_bound_kind[tn1]!=bound_strict && d_bound_kind[tn1]!=bound_invalid ){
996
1
          d_bound_kind[tn1] = bound_invalid;
997
2
          Trace("sep-bound")
998
1
              << "reference cannot be bound (due to quantified pto)."
999
1
              << std::endl;
1000
        }
1001
      }else{
1002
366
        references[index][n].push_back( n[0] );
1003
      }
1004
367
      if( hasPol && pol ){
1005
262
        references_strict[index][n] = true;
1006
      }else{
1007
105
        card = 1;
1008
      }
1009
    }else{
1010
1651233
      bool isSpatial = n.getKind()==kind::SEP_WAND || n.getKind()==kind::SEP_STAR;
1011
1651233
      bool newUnderSpatial = underSpatial || isSpatial;
1012
1651233
      bool refStrict = isSpatial;
1013
5363524
      for( unsigned i=0; i<n.getNumChildren(); i++ ){
1014
        bool newHasPol, newPol;
1015
3712291
        QuantPhaseReq::getEntailPolarity( n, i, hasPol, pol, newHasPol, newPol );
1016
3712291
        int newIndex = newHasPol ? ( newPol ? 1 : -1 ) : 0;
1017
3712291
        int ccard = processAssertion( n[i], visited, references, references_strict, newPol, newHasPol, newUnderSpatial );
1018
        //update cardinality
1019
3712291
        if( n.getKind()==kind::SEP_STAR ){
1020
315
          card += ccard;
1021
3711976
        }else if( n.getKind()==kind::SEP_WAND ){
1022
32
          if( i==1 ){
1023
16
            card = ccard;
1024
          }
1025
3711944
        }else if( ccard>card ){
1026
176
          card = ccard;
1027
        }
1028
        //track references if we are or below a spatial operator
1029
3712291
        if( newUnderSpatial ){
1030
429
          bool add = true;
1031
429
          if( references_strict[newIndex].find( n[i] )!=references_strict[newIndex].end() ){
1032
163
            if( !isSpatial ){
1033
              if( references_strict[index].find( n )==references_strict[index].end() ){
1034
                references_strict[index][n] = true;
1035
              }else{
1036
                add = false;
1037
                //TODO: can derive static equality between sets
1038
              }
1039
            }
1040
          }else{
1041
266
            if( isSpatial ){
1042
184
              refStrict = false;
1043
            }
1044
          }
1045
429
          if( add ){
1046
820
            for( unsigned j=0; j<references[newIndex][n[i]].size(); j++ ){
1047
391
              if( std::find( references[index][n].begin(), references[index][n].end(), references[newIndex][n[i]][j] )==references[index][n].end() ){
1048
354
                references[index][n].push_back( references[newIndex][n[i]][j] );
1049
              }
1050
            }
1051
          }
1052
        }
1053
      }
1054
1651233
      if( isSpatial && refStrict ){
1055
55
        if( n.getKind()==kind::SEP_WAND ){
1056
          //TODO
1057
        }else{
1058
55
          Assert(n.getKind() == kind::SEP_STAR && hasPol && pol);
1059
55
          references_strict[index][n] = true;
1060
        }
1061
      }
1062
    }
1063
1651645
    visited[index][n] = card;
1064
  }else{
1065
2185477
    card = it->second;
1066
  }
1067
1068
3837122
  if( !underSpatial && ( !references[index][n].empty() || card>0 ) ){
1069
766
    TypeNode tn = getReferenceType( n );
1070
383
    Assert(!tn.isNull());
1071
    // add references to overall type
1072
383
    unsigned bt = d_bound_kind[tn];
1073
383
    bool add = true;
1074
383
    if( references_strict[index].find( n )!=references_strict[index].end() ){
1075
170
      Trace("sep-bound") << "Strict bound found based on " << n << ", index = " << index << std::endl;
1076
170
      if( bt!=bound_strict ){
1077
85
        d_bound_kind[tn] = bound_strict;
1078
        //d_type_references[tn].clear();
1079
85
        d_card_max[tn] = card;
1080
      }else{
1081
        //TODO: derive static equality
1082
85
        add = false;
1083
      }
1084
    }else{
1085
213
      add = bt!=bound_strict;
1086
    }
1087
791
    for( unsigned i=0; i<references[index][n].size(); i++ ){
1088
408
      if( std::find( d_type_references[tn].begin(), d_type_references[tn].end(), references[index][n][i] )==d_type_references[tn].end() ){
1089
246
        d_type_references[tn].push_back( references[index][n][i] );
1090
      }
1091
    }
1092
383
    if( add ){
1093
      //add max cardinality
1094
241
      if( card>(int)d_card_max[tn] ){
1095
57
        d_card_max[tn] = card;
1096
      }
1097
    }
1098
  }
1099
3837122
  return card;
1100
}
1101
1102
693
void TheorySep::registerRefDataTypesAtom(Node atom)
1103
{
1104
1386
  TypeNode tn1;
1105
1386
  TypeNode tn2;
1106
693
  Kind k = atom.getKind();
1107
693
  if (k == SEP_PTO || k == SEP_EMP)
1108
  {
1109
545
    tn1 = atom[0].getType();
1110
545
    tn2 = atom[1].getType();
1111
  }
1112
  else
1113
  {
1114
148
    Assert(k == SEP_STAR || k == SEP_WAND);
1115
  }
1116
694
  registerRefDataTypes(tn1, tn2, atom);
1117
692
}
1118
1119
812
void TheorySep::registerRefDataTypes(TypeNode tn1, TypeNode tn2, Node atom)
1120
{
1121
812
  if (!d_type_ref.isNull())
1122
  {
1123
692
    Assert(!atom.isNull());
1124
    // already declared, ensure compatible
1125
1929
    if ((!tn1.isNull() && !tn1.isComparableTo(d_type_ref))
1126
2076
        || (!tn2.isNull() && !tn2.isComparableTo(d_type_data)))
1127
    {
1128
      std::stringstream ss;
1129
      ss << "ERROR: the separation logic heap type has already been set to "
1130
         << d_type_ref << " -> " << d_type_data
1131
         << " but we have a constraint that uses different heap types, "
1132
            "offending atom is "
1133
         << atom << " with associated heap type " << tn1 << " -> " << tn2
1134
         << std::endl;
1135
    }
1136
692
    return;
1137
  }
1138
  // if not declared yet, and we have a separation logic constraint, throw
1139
  // an error.
1140
120
  if (!atom.isNull())
1141
  {
1142
2
    std::stringstream ss;
1143
    // error, heap not declared
1144
    ss << "ERROR: the type of the separation logic heap has not been declared "
1145
          "(e.g. via a declare-heap command), and we have a separation logic "
1146
1
          "constraint "
1147
1
       << atom << std::endl;
1148
1
    throw LogicException(ss.str());
1149
  }
1150
  // otherwise set it
1151
238
  Trace("sep-type") << "Sep: assume location type " << tn1
1152
119
                    << " is associated with data type " << tn2 << std::endl;
1153
119
  d_loc_to_data_type[tn1] = tn2;
1154
  // for now, we only allow heap constraints of one type
1155
119
  d_type_ref = tn1;
1156
119
  d_type_data = tn2;
1157
119
  d_bound_kind[tn1] = bound_default;
1158
}
1159
1160
115
void TheorySep::initializeBounds() {
1161
115
  if( !d_bounds_init ){
1162
115
    Trace("sep-bound")  << "Initialize sep bounds..." << std::endl;
1163
115
    d_bounds_init = true;
1164
115
    NodeManager* nm = NodeManager::currentNM();
1165
115
    SkolemManager* sm = nm->getSkolemManager();
1166
230
    for( std::map< TypeNode, TypeNode >::iterator it = d_loc_to_data_type.begin(); it != d_loc_to_data_type.end(); ++it ){
1167
230
      TypeNode tn = it->first;
1168
115
      Trace("sep-bound")  << "Initialize bounds for " << tn << "..." << std::endl;
1169
115
      unsigned n_emp = 0;
1170
115
      if( d_bound_kind[tn] != bound_invalid ){
1171
114
        n_emp = d_card_max[tn];
1172
1
      }else if( d_type_references[tn].empty() ){
1173
        //must include at least one constant TODO: remove?
1174
1
        n_emp = 1;
1175
      }
1176
115
      Trace("sep-bound") << "Cardinality element size : " << d_card_max[tn] << std::endl;
1177
115
      Trace("sep-bound") << "Type reference size : " << d_type_references[tn].size() << std::endl;
1178
115
      Trace("sep-bound") << "Constructing " << n_emp << " cardinality constants." << std::endl;
1179
145
      for( unsigned r=0; r<n_emp; r++ ){
1180
        Node e =
1181
60
            sm->mkDummySkolem("e", tn, "cardinality bound element for seplog");
1182
30
        d_type_references_card[tn].push_back( e );
1183
30
        d_type_ref_card_id[e] = r;
1184
      }
1185
    }
1186
  }
1187
115
}
1188
1189
285
Node TheorySep::getBaseLabel( TypeNode tn ) {
1190
285
  std::map< TypeNode, Node >::iterator it = d_base_label.find( tn );
1191
285
  if( it==d_base_label.end() ){
1192
115
    NodeManager* nm = NodeManager::currentNM();
1193
115
    SkolemManager* sm = nm->getSkolemManager();
1194
115
    initializeBounds();
1195
115
    Trace("sep") << "Make base label for " << tn << std::endl;
1196
230
    std::stringstream ss;
1197
115
    ss << "__Lb";
1198
230
    TypeNode ltn = nm->mkSetType(tn);
1199
230
    Node n_lbl = sm->mkDummySkolem(ss.str(), ltn, "base label");
1200
115
    d_base_label[tn] = n_lbl;
1201
    //make reference bound
1202
115
    Trace("sep") << "Make reference bound label for " << tn << std::endl;
1203
230
    std::stringstream ss2;
1204
115
    ss2 << "__Lu";
1205
115
    d_reference_bound[tn] = sm->mkDummySkolem(ss2.str(), ltn, "");
1206
115
    d_type_references_all[tn].insert( d_type_references_all[tn].end(), d_type_references[tn].begin(), d_type_references[tn].end() );
1207
1208
    //check whether monotonic (elements can be added to tn without effecting satisfiability)
1209
115
    bool tn_is_monotonic = true;
1210
115
    if( tn.isSort() ){
1211
      //TODO: use monotonicity inference
1212
21
      tn_is_monotonic = !getLogicInfo().isQuantified();
1213
    }else{
1214
94
      tn_is_monotonic = tn.getCardinality().isInfinite();
1215
    }
1216
    //add a reference type for maximum occurrences of empty in a constraint
1217
115
    if( options::sepDisequalC() && tn_is_monotonic ){
1218
119
      for( unsigned r=0; r<d_type_references_card[tn].size(); r++ ){
1219
50
        Node e = d_type_references_card[tn][r];
1220
        //ensure that it is distinct from all other references so far
1221
64
        for( unsigned j=0; j<d_type_references_all[tn].size(); j++ ){
1222
78
          Node eq = NodeManager::currentNM()->mkNode( kind::EQUAL, e, d_type_references_all[tn][j] );
1223
39
          d_im.lemma(eq.negate(), InferenceId::SEP_DISTINCT_REF);
1224
        }
1225
25
        d_type_references_all[tn].push_back( e );
1226
      }
1227
    }else{
1228
      //break symmetries TODO
1229
1230
21
      d_type_references_all[tn].insert( d_type_references_all[tn].end(), d_type_references_card[tn].begin(), d_type_references_card[tn].end() );
1231
    }
1232
    //Assert( !d_type_references_all[tn].empty() );
1233
1234
115
    if (d_bound_kind[tn] != bound_invalid)
1235
    {
1236
      //construct bound
1237
114
      d_reference_bound_max[tn] = mkUnion( tn, d_type_references_all[tn] );
1238
114
      Trace("sep-bound") << "overall bound for " << d_base_label[tn] << " : " << d_reference_bound_max[tn] << std::endl;
1239
1240
228
      Node slem = NodeManager::currentNM()->mkNode( kind::SUBSET, d_base_label[tn], d_reference_bound_max[tn] );
1241
114
      Trace("sep-lemma") << "Sep::Lemma: reference bound for " << tn << " : " << slem << std::endl;
1242
114
      d_im.lemma(slem, InferenceId::SEP_REF_BOUND);
1243
1244
      //symmetry breaking
1245
114
      if( d_type_references_card[tn].size()>1 ){
1246
12
        std::map< unsigned, Node > lit_mem_map;
1247
18
        for( unsigned i=0; i<d_type_references_card[tn].size(); i++ ){
1248
12
          lit_mem_map[i] = NodeManager::currentNM()->mkNode( kind::MEMBER, d_type_references_card[tn][i], d_reference_bound_max[tn]);
1249
        }
1250
12
        for( unsigned i=0; i<(d_type_references_card[tn].size()-1); i++ ){
1251
12
          std::vector< Node > children;
1252
12
          for( unsigned j=(i+1); j<d_type_references_card[tn].size(); j++ ){
1253
6
            children.push_back( lit_mem_map[j].negate() );
1254
          }
1255
6
          if( !children.empty() ){
1256
12
            Node sym_lem = children.size()==1 ? children[0] : NodeManager::currentNM()->mkNode( kind::AND, children );
1257
6
            sym_lem = NodeManager::currentNM()->mkNode( kind::IMPLIES, lit_mem_map[i].negate(), sym_lem );
1258
6
            Trace("sep-lemma") << "Sep::Lemma: symmetry breaking lemma : " << sym_lem << std::endl;
1259
6
            d_im.lemma(sym_lem, InferenceId::SEP_SYM_BREAK);
1260
          }
1261
        }
1262
      }
1263
    }
1264
1265
    //assert that nil ref is not in base label
1266
230
    Node nr = getNilRef( tn );
1267
230
    Node nrlem = NodeManager::currentNM()->mkNode( kind::MEMBER, nr, n_lbl ).negate();
1268
115
    Trace("sep-lemma") << "Sep::Lemma: sep.nil not in base label " << tn << " : " << nrlem << std::endl;
1269
115
    d_im.lemma(nrlem, InferenceId::SEP_NIL_NOT_IN_HEAP);
1270
1271
115
    return n_lbl;
1272
  }else{
1273
170
    return it->second;
1274
  }
1275
}
1276
1277
139
Node TheorySep::getNilRef( TypeNode tn ) {
1278
139
  std::map< TypeNode, Node >::iterator it = d_nil_ref.find( tn );
1279
139
  if( it==d_nil_ref.end() ){
1280
230
    Node nil = NodeManager::currentNM()->mkNullaryOperator( tn, kind::SEP_NIL );
1281
115
    setNilRef( tn, nil );
1282
115
    return nil;
1283
  }else{
1284
24
    return it->second;
1285
  }
1286
}
1287
1288
115
void TheorySep::setNilRef( TypeNode tn, Node n ) {
1289
115
  Assert(n.getType() == tn);
1290
115
  d_nil_ref[tn] = n;
1291
115
}
1292
1293
114
Node TheorySep::mkUnion( TypeNode tn, std::vector< Node >& locs ) {
1294
228
  Node u;
1295
114
  if( locs.empty() ){
1296
4
    TypeNode ltn = NodeManager::currentNM()->mkSetType(tn);
1297
2
    return NodeManager::currentNM()->mkConst(EmptySet(ltn));
1298
  }else{
1299
385
    for( unsigned i=0; i<locs.size(); i++ ){
1300
546
      Node s = locs[i];
1301
273
      Assert(!s.isNull());
1302
273
      s = NodeManager::currentNM()->mkSingleton(tn, s);
1303
273
      if( u.isNull() ){
1304
112
        u = s;
1305
      }else{
1306
161
        u = NodeManager::currentNM()->mkNode( kind::UNION, s, u );
1307
      }
1308
    }
1309
112
    return u;
1310
  }
1311
}
1312
1313
445
Node TheorySep::getLabel( Node atom, int child, Node lbl ) {
1314
445
  std::map< int, Node >::iterator it = d_label_map[atom][lbl].find( child );
1315
445
  if( it==d_label_map[atom][lbl].end() ){
1316
381
    NodeManager* nm = NodeManager::currentNM();
1317
381
    SkolemManager* sm = nm->getSkolemManager();
1318
762
    TypeNode refType = getReferenceType( atom );
1319
762
    std::stringstream ss;
1320
381
    ss << "__Lc" << child;
1321
762
    TypeNode ltn = NodeManager::currentNM()->mkSetType(refType);
1322
    //TypeNode ltn = NodeManager::currentNM()->mkSetType(NodeManager::currentNM()->mkRefType(refType));
1323
762
    Node n_lbl = sm->mkDummySkolem(ss.str(), ltn, "sep label");
1324
381
    d_label_map[atom][lbl][child] = n_lbl;
1325
381
    d_label_map_parent[n_lbl] = lbl;
1326
381
    return n_lbl;
1327
  }else{
1328
64
    return (*it).second;
1329
  }
1330
}
1331
1332
470
Node TheorySep::applyLabel( Node n, Node lbl, std::map< Node, Node >& visited ) {
1333
470
  Assert(n.getKind() != kind::SEP_LABEL);
1334
470
  if( n.getKind()==kind::SEP_STAR || n.getKind()==kind::SEP_WAND || n.getKind()==kind::SEP_PTO || n.getKind()==kind::SEP_EMP ){
1335
369
    return NodeManager::currentNM()->mkNode( kind::SEP_LABEL, n, lbl );
1336
101
  }else if( !n.getType().isBoolean() || n.getNumChildren()==0 ){
1337
24
    return n;
1338
  }else{
1339
77
    std::map< Node, Node >::iterator it = visited.find( n );
1340
77
    if( it==visited.end() ){
1341
154
      std::vector< Node > children;
1342
77
      if (n.getMetaKind() == kind::metakind::PARAMETERIZED) {
1343
        children.push_back( n.getOperator() );
1344
      }
1345
77
      bool childChanged = false;
1346
166
      for( unsigned i=0; i<n.getNumChildren(); i++ ){
1347
178
        Node aln = applyLabel( n[i], lbl, visited );
1348
89
        children.push_back( aln );
1349
89
        childChanged = childChanged || aln!=n[i];
1350
      }
1351
154
      Node ret = n;
1352
77
      if( childChanged ){
1353
75
        ret = NodeManager::currentNM()->mkNode( n.getKind(), children );
1354
      }
1355
77
      visited[n] = ret;
1356
77
      return ret;
1357
    }else{
1358
      return it->second;
1359
    }
1360
  }
1361
}
1362
1363
290
Node TheorySep::instantiateLabel(Node n,
1364
                                 Node o_lbl,
1365
                                 Node lbl,
1366
                                 Node lbl_v,
1367
                                 std::map<Node, Node>& visited,
1368
                                 std::map<Node, Node>& pto_model,
1369
                                 TypeNode rtn,
1370
                                 std::map<Node, bool>& active_lbl,
1371
                                 unsigned ind)
1372
{
1373
290
  Trace("sep-inst-debug") << "Instantiate label " << n << " " << lbl << " " << lbl_v << std::endl;
1374
290
  if( options::sepMinimalRefine() && lbl!=o_lbl && active_lbl.find( lbl )!=active_lbl.end() ){
1375
    Trace("sep-inst") << "...do not instantiate " << o_lbl << " since it has an active sublabel " << lbl << std::endl;
1376
    return Node::null();
1377
  }else{
1378
290
    if( Trace.isOn("sep-inst") ){
1379
      if( n.getKind()==kind::SEP_STAR || n.getKind()==kind::SEP_WAND  || n.getKind()==kind::SEP_PTO || n.getKind()==kind::SEP_EMP ){
1380
        for( unsigned j=0; j<ind; j++ ){ Trace("sep-inst") << "  "; }
1381
        Trace("sep-inst") << n << "[" << lbl << "] :: " << lbl_v << std::endl;
1382
      }
1383
    }
1384
290
    Assert(n.getKind() != kind::SEP_LABEL);
1385
290
    if( n.getKind()==kind::SEP_STAR || n.getKind()==kind::SEP_WAND ){
1386
82
      if( lbl==o_lbl ){
1387
134
        std::vector< Node > children;
1388
67
        children.resize( n.getNumChildren() );
1389
67
        Assert(d_label_map[n].find(lbl) != d_label_map[n].end());
1390
134
        std::map< int, Node > mvals;
1391
225
        for( std::map< int, Node >::iterator itl = d_label_map[n][lbl].begin(); itl != d_label_map[n][lbl].end(); ++itl ){
1392
316
          Node sub_lbl = itl->second;
1393
158
          int sub_index = itl->first;
1394
158
          Assert(sub_index >= 0 && sub_index < (int)children.size());
1395
158
          Trace("sep-inst-debug") << "Sublabel " << sub_index << " is " << sub_lbl << std::endl;
1396
316
          Node lbl_mval;
1397
158
          if( n.getKind()==kind::SEP_WAND && sub_index==1 ){
1398
13
            Assert(d_label_map[n][lbl].find(0) != d_label_map[n][lbl].end());
1399
26
            Node sub_lbl_0 = d_label_map[n][lbl][0];
1400
13
            computeLabelModel( sub_lbl_0 );
1401
13
            Assert(d_label_model.find(sub_lbl_0) != d_label_model.end());
1402
13
            lbl_mval = NodeManager::currentNM()->mkNode( kind::UNION, lbl, d_label_model[sub_lbl_0].getValue( rtn ) );
1403
          }else{
1404
145
            computeLabelModel( sub_lbl );
1405
145
            Assert(d_label_model.find(sub_lbl) != d_label_model.end());
1406
145
            lbl_mval = d_label_model[sub_lbl].getValue( rtn );
1407
          }
1408
158
          Trace("sep-inst-debug") << "Sublabel value is " << lbl_mval  << std::endl;
1409
158
          mvals[sub_index] = lbl_mval;
1410
158
          children[sub_index] = instantiateLabel( n[sub_index], o_lbl, sub_lbl, lbl_mval, visited, pto_model, rtn, active_lbl, ind+1 );
1411
158
          if( children[sub_index].isNull() ){
1412
            return Node::null();
1413
          }
1414
        }
1415
134
        Node empSet = NodeManager::currentNM()->mkConst(EmptySet(rtn));
1416
67
        if( n.getKind()==kind::SEP_STAR ){
1417
1418
          //disjoint contraints
1419
108
          std::vector< Node > conj;
1420
108
          std::vector< Node > bchildren;
1421
54
          bchildren.insert( bchildren.end(), children.begin(), children.end() );
1422
108
          Node vsu;
1423
108
          std::vector< Node > vs;
1424
186
          for( std::map< int, Node >::iterator itl = d_label_map[n][lbl].begin(); itl != d_label_map[n][lbl].end(); ++itl ){
1425
264
            Node sub_lbl = itl->second;
1426
264
            Node lbl_mval = d_label_model[sub_lbl].getValue( rtn );
1427
242
            for( unsigned j=0; j<vs.size(); j++ ){
1428
110
              bchildren.push_back( NodeManager::currentNM()->mkNode( kind::INTERSECTION, lbl_mval, vs[j] ).eqNode( empSet ) );
1429
            }
1430
132
            vs.push_back( lbl_mval );
1431
132
            if( vsu.isNull() ){
1432
54
              vsu = lbl_mval;
1433
            }else{
1434
78
              vsu = NodeManager::currentNM()->mkNode( kind::UNION, vsu, lbl_mval );
1435
            }
1436
          }
1437
54
          bchildren.push_back( vsu.eqNode( lbl ) );
1438
1439
54
          Assert(bchildren.size() > 1);
1440
54
          conj.push_back( NodeManager::currentNM()->mkNode( kind::AND, bchildren ) );
1441
1442
54
          if( options::sepChildRefine() ){
1443
            //child-specific refinements (TODO: use ?)
1444
            for( unsigned i=0; i<children.size(); i++ ){
1445
              std::vector< Node > tchildren;
1446
              Node mval = mvals[i];
1447
              tchildren.push_back(
1448
                  NodeManager::currentNM()->mkNode(kind::SUBSET, mval, lbl));
1449
              tchildren.push_back( children[i] );
1450
              std::vector< Node > rem_children;
1451
              for( unsigned j=0; j<children.size(); j++ ){
1452
                if( j!=i ){
1453
                  rem_children.push_back( n[j] );
1454
                }
1455
              }
1456
              std::map< Node, Node > rvisited;
1457
              Node rem = rem_children.size()==1 ? rem_children[0] : NodeManager::currentNM()->mkNode( kind::SEP_STAR, rem_children );
1458
              rem = applyLabel( rem, NodeManager::currentNM()->mkNode( kind::SETMINUS, lbl, mval ), rvisited );
1459
              tchildren.push_back( rem );
1460
              conj.push_back( NodeManager::currentNM()->mkNode( kind::AND, tchildren ) );
1461
            }
1462
          }
1463
54
          return conj.size()==1 ? conj[0] : NodeManager::currentNM()->mkNode( kind::OR, conj );
1464
        }else{
1465
26
          std::vector< Node > wchildren;
1466
          //disjoint constraints
1467
26
          Node sub_lbl_0 = d_label_map[n][lbl][0];
1468
26
          Node lbl_mval_0 = d_label_model[sub_lbl_0].getValue( rtn );
1469
13
          wchildren.push_back( NodeManager::currentNM()->mkNode( kind::INTERSECTION, lbl_mval_0, lbl ).eqNode( empSet ).negate() );
1470
1471
          //return the lemma
1472
13
          wchildren.push_back( children[0].negate() );
1473
13
          wchildren.push_back( children[1] );
1474
13
          return NodeManager::currentNM()->mkNode( kind::OR, wchildren );
1475
        }
1476
      }else{
1477
        //nested star/wand, label it and return
1478
15
        return NodeManager::currentNM()->mkNode( kind::SEP_LABEL, n, lbl_v );
1479
      }
1480
208
    }else if( n.getKind()==kind::SEP_PTO ){
1481
      //check if this pto reference is in the base label, if not, then it does not need to be added as an assumption
1482
121
      Assert(d_label_model.find(o_lbl) != d_label_model.end());
1483
242
      Node vr = d_valuation.getModel()->getRepresentative( n[0] );
1484
      // TODO(project##230): Find a safe type for the singleton operator
1485
242
      Node svr = NodeManager::currentNM()->mkSingleton(vr.getType(), vr);
1486
121
      bool inBaseHeap = std::find( d_label_model[o_lbl].d_heap_locs_model.begin(), d_label_model[o_lbl].d_heap_locs_model.end(), svr )!=d_label_model[o_lbl].d_heap_locs_model.end();
1487
121
      Trace("sep-inst-debug") << "Is in base (non-instantiating) heap : " << inBaseHeap << " for value ref " << vr << " in " << o_lbl << std::endl;
1488
242
      std::vector< Node > children;
1489
121
      if( inBaseHeap ){
1490
        // TODO(project##230): Find a safe type for the singleton operator
1491
194
        Node s = NodeManager::currentNM()->mkSingleton(n[0].getType(),  n[0]);
1492
97
        children.push_back( NodeManager::currentNM()->mkNode( kind::SEP_LABEL, NodeManager::currentNM()->mkNode( kind::SEP_PTO, n[0], n[1] ), s ) );
1493
      }else{
1494
        //look up value of data
1495
24
        std::map< Node, Node >::iterator it = pto_model.find( vr );
1496
24
        if( it!=pto_model.end() ){
1497
19
          if( n[1]!=it->second ){
1498
6
            children.push_back( NodeManager::currentNM()->mkNode( kind::EQUAL, n[1], it->second ) );
1499
          }
1500
        }else{
1501
5
          Trace("sep-inst-debug") << "Data for " << vr << " was not specified, do not add condition." << std::endl;
1502
        }
1503
      }
1504
      // TODO(project##230): Find a safe type for the singleton operator
1505
242
      Node singleton = NodeManager::currentNM()->mkSingleton(n[0].getType(), n[0]);
1506
121
      children.push_back(singleton.eqNode(lbl_v));
1507
242
      Node ret = children.empty() ? NodeManager::currentNM()->mkConst( true ) : ( children.size()==1 ? children[0] : NodeManager::currentNM()->mkNode( kind::AND, children ) );
1508
121
      Trace("sep-inst-debug") << "Return " << ret << std::endl;
1509
121
      return ret;
1510
87
    }else if( n.getKind()==kind::SEP_EMP ){
1511
      //return NodeManager::currentNM()->mkConst( lbl_v.getKind()==kind::EMPTYSET );
1512
      return lbl_v.eqNode(
1513
13
          NodeManager::currentNM()->mkConst(EmptySet(lbl_v.getType())));
1514
    }else{
1515
74
      std::map< Node, Node >::iterator it = visited.find( n );
1516
74
      if( it==visited.end() ){
1517
148
        std::vector< Node > children;
1518
74
        if (n.getMetaKind() == kind::metakind::PARAMETERIZED) {
1519
          children.push_back( n.getOperator() );
1520
        }
1521
74
        bool childChanged = false;
1522
139
        for( unsigned i=0; i<n.getNumChildren(); i++ ){
1523
130
          Node aln = instantiateLabel( n[i], o_lbl, lbl, lbl_v, visited, pto_model, rtn, active_lbl, ind );
1524
65
          if( aln.isNull() ){
1525
            return Node::null();
1526
          }else{
1527
65
            children.push_back( aln );
1528
65
            childChanged = childChanged || aln!=n[i];
1529
          }
1530
        }
1531
148
        Node ret = n;
1532
74
        if( childChanged ){
1533
65
          ret = NodeManager::currentNM()->mkNode( n.getKind(), children );
1534
        }
1535
        //careful about caching
1536
        //visited[n] = ret;
1537
74
        return ret;
1538
      }else{
1539
        return it->second;
1540
      }
1541
    }
1542
  }
1543
}
1544
1545
32
void TheorySep::setInactiveAssertionRec( Node fact, std::map< Node, std::vector< Node > >& lbl_to_assertions, std::map< Node, bool >& assert_active ) {
1546
32
  Trace("sep-process-debug") << "setInactiveAssertionRec::inactive : " << fact << std::endl;
1547
32
  assert_active[fact] = false;
1548
32
  bool polarity = fact.getKind() != kind::NOT;
1549
64
  TNode atom = polarity ? fact : fact[0];
1550
64
  TNode satom = atom[0];
1551
64
  TNode slbl = atom[1];
1552
32
  if (satom.getKind() == SEP_WAND || satom.getKind() == SEP_STAR)
1553
  {
1554
96
    for (size_t j = 0, nchild = satom.getNumChildren(); j < nchild; j++)
1555
    {
1556
128
      Node lblc = getLabel(satom, j, slbl);
1557
68
      for( unsigned k=0; k<lbl_to_assertions[lblc].size(); k++ ){
1558
4
        setInactiveAssertionRec( lbl_to_assertions[lblc][k], lbl_to_assertions, assert_active );
1559
      }
1560
    }
1561
  }
1562
32
}
1563
1564
163
void TheorySep::getLabelChildren(Node satom,
1565
                                 Node lbl,
1566
                                 std::vector<Node>& children,
1567
                                 std::vector<Node>& labels)
1568
{
1569
544
  for (size_t i = 0, nchild = satom.getNumChildren(); i < nchild; i++)
1570
  {
1571
762
    Node lblc = getLabel(satom, i, lbl);
1572
381
    Assert(!lblc.isNull());
1573
762
    std::map< Node, Node > visited;
1574
762
    Node lc = applyLabel(satom[i], lblc, visited);
1575
381
    Assert(!lc.isNull());
1576
381
    if (i == 1 && satom.getKind() == SEP_WAND)
1577
    {
1578
17
      lc = lc.negate();
1579
    }
1580
381
    children.push_back( lc );
1581
381
    labels.push_back( lblc );
1582
  }
1583
163
  Assert(children.size() > 1);
1584
163
}
1585
1586
626
void TheorySep::computeLabelModel( Node lbl ) {
1587
626
  if( !d_label_model[lbl].d_computed ){
1588
395
    d_label_model[lbl].d_computed = true;
1589
1590
    //we must get the value of lbl from the model: this is being run at last call, after the model is constructed
1591
    //Assert(...); TODO
1592
790
    Node v_val = d_valuation.getModel()->getRepresentative( lbl );
1593
395
    Trace("sep-process") << "Model value (from valuation) for " << lbl << " : " << v_val << std::endl;
1594
395
    if( v_val.getKind()!=kind::EMPTYSET ){
1595
541
      while( v_val.getKind()==kind::UNION ){
1596
98
        Assert(v_val[0].getKind() == kind::SINGLETON);
1597
98
        d_label_model[lbl].d_heap_locs_model.push_back(v_val[0]);
1598
98
        v_val = v_val[1];
1599
      }
1600
345
      if( v_val.getKind()==kind::SINGLETON ){
1601
345
        d_label_model[lbl].d_heap_locs_model.push_back( v_val );
1602
      }else{
1603
        throw Exception("Could not establish value of heap in model.");
1604
        Assert(false);
1605
      }
1606
    }
1607
838
    for( unsigned j=0; j<d_label_model[lbl].d_heap_locs_model.size(); j++ ){
1608
886
      Node u = d_label_model[lbl].d_heap_locs_model[j];
1609
443
      Assert(u.getKind() == kind::SINGLETON);
1610
443
      u = u[0];
1611
886
      Node tt;
1612
443
      std::map< Node, Node >::iterator itm = d_tmodel.find( u );
1613
443
      if( itm==d_tmodel.end() ) {
1614
        //Trace("sep-process") << "WARNING: could not find symbolic term in model for " << u << std::endl;
1615
        //Assert( false );
1616
        //tt = u;
1617
        //TypeNode tn = u.getType().getRefConstituentType();
1618
8
        TypeNode tn = u.getType();
1619
4
        Trace("sep-process") << "WARNING: could not find symbolic term in model for " << u << ", cref type " << tn << std::endl;
1620
4
        Assert(d_type_references_all.find(tn) != d_type_references_all.end());
1621
4
        Assert(!d_type_references_all[tn].empty());
1622
4
        tt = d_type_references_all[tn][0];
1623
      }else{
1624
439
        tt = itm->second;
1625
      }
1626
      // TODO(project##230): Find a safe type for the singleton operator
1627
886
      Node stt = NodeManager::currentNM()->mkSingleton(tt.getType(), tt);
1628
443
      Trace("sep-process-debug") << "...model : add " << tt << " for " << u << " in lbl " << lbl << std::endl;
1629
443
      d_label_model[lbl].d_heap_locs.push_back( stt );
1630
    }
1631
  }
1632
626
}
1633
1634
1067
Node TheorySep::getRepresentative( Node t ) {
1635
1067
  if (d_equalityEngine->hasTerm(t))
1636
  {
1637
1067
    return d_equalityEngine->getRepresentative(t);
1638
  }else{
1639
    return t;
1640
  }
1641
}
1642
1643
944
bool TheorySep::hasTerm(Node a) { return d_equalityEngine->hasTerm(a); }
1644
1645
645
bool TheorySep::areEqual( Node a, Node b ){
1646
645
  if( a==b ){
1647
44
    return true;
1648
601
  }else if( hasTerm( a ) && hasTerm( b ) ){
1649
326
    return d_equalityEngine->areEqual(a, b);
1650
  }else{
1651
275
    return false;
1652
  }
1653
}
1654
1655
bool TheorySep::areDisequal( Node a, Node b ){
1656
  if( a==b ){
1657
    return false;
1658
  }else if( hasTerm( a ) && hasTerm( b ) ){
1659
    if (d_equalityEngine->areDisequal(a, b, false))
1660
    {
1661
      return true;
1662
    }
1663
  }
1664
  return false;
1665
}
1666
1667
35665
void TheorySep::eqNotifyMerge(TNode t1, TNode t2)
1668
{
1669
35665
  HeapAssertInfo * e2 = getOrMakeEqcInfo( t2, false );
1670
35665
  if( e2 && ( !e2->d_pto.get().isNull() || e2->d_has_neg_pto.get() ) ){
1671
531
    HeapAssertInfo * e1 = getOrMakeEqcInfo( t1, true );
1672
531
    if( !e2->d_pto.get().isNull() ){
1673
530
      if( !e1->d_pto.get().isNull() ){
1674
169
        Trace("sep-pto-debug") << "While merging " << t1 << " " << t2 << ", merge pto." << std::endl;
1675
169
        mergePto( e1->d_pto.get(), e2->d_pto.get() );
1676
      }else{
1677
361
        e1->d_pto.set( e2->d_pto.get() );
1678
      }
1679
    }
1680
531
    e1->d_has_neg_pto.set( e1->d_has_neg_pto.get() || e2->d_has_neg_pto.get() );
1681
    //validate
1682
531
    validatePto( e1, t1 );
1683
  }
1684
35665
}
1685
1686
1345
void TheorySep::validatePto( HeapAssertInfo * ei, Node ei_n ) {
1687
1345
  if( !ei->d_pto.get().isNull() && ei->d_has_neg_pto.get() ){
1688
4
    for( NodeList::const_iterator i = d_spatial_assertions.begin(); i != d_spatial_assertions.end(); ++i ) {
1689
6
      Node fact = (*i);
1690
3
      if (fact.getKind() == kind::NOT)
1691
      {
1692
2
        TNode atom = fact[0];
1693
1
        Assert(atom.getKind() == kind::SEP_LABEL);
1694
2
        TNode satom = atom[0];
1695
1
        if (satom.getKind() == SEP_PTO)
1696
        {
1697
1
          if( areEqual( atom[1], ei_n ) ){
1698
1
            addPto( ei, ei_n, atom, false );
1699
          }
1700
        }
1701
      }
1702
    }
1703
    //we have now processed all pending negated pto
1704
1
    ei->d_has_neg_pto.set( false );
1705
  }
1706
1345
}
1707
1708
1068
void TheorySep::addPto( HeapAssertInfo * ei, Node ei_n, Node p, bool polarity ) {
1709
1068
  Trace("sep-pto") << "Add pto " << p << ", pol = " << polarity << " to eqc " << ei_n << std::endl;
1710
1068
  if( !ei->d_pto.get().isNull() ){
1711
243
    if( polarity ){
1712
182
      Trace("sep-pto-debug") << "...eqc " << ei_n << " already has pto " << ei->d_pto.get() << ", merge." << std::endl;
1713
182
      mergePto( ei->d_pto.get(), p );
1714
    }else{
1715
122
      Node pb = ei->d_pto.get();
1716
61
      Trace("sep-pto") << "Process positive/negated pto " << " " << pb << " " << p << std::endl;
1717
61
      Assert(pb.getKind() == kind::SEP_LABEL
1718
             && pb[0].getKind() == kind::SEP_PTO);
1719
61
      Assert(p.getKind() == kind::SEP_LABEL && p[0].getKind() == kind::SEP_PTO);
1720
61
      Assert(areEqual(pb[1], p[1]));
1721
122
      std::vector< Node > exp;
1722
61
      if( pb[1]!=p[1] ){
1723
        //if( pb[1].getKind()==kind::SINGLETON && p[1].getKind()==kind::SINGLETON ){
1724
        //  exp.push_back( pb[1][0].eqNode( p[1][0] ) );
1725
        //}else{
1726
24
        exp.push_back( pb[1].eqNode( p[1] ) );
1727
        //}
1728
      }
1729
61
      exp.push_back( pb );
1730
61
      exp.push_back( p.negate() );
1731
122
      std::vector< Node > conc;
1732
61
      if( pb[0][1]!=p[0][1] ){
1733
61
        conc.push_back( pb[0][1].eqNode( p[0][1] ).negate() );
1734
      }
1735
      //if( pb[1]!=p[1] ){
1736
      //  conc.push_back( pb[1].eqNode( p[1] ).negate() );
1737
      //}
1738
122
      Node n_conc = conc.empty() ? d_false : ( conc.size()==1 ? conc[0] : NodeManager::currentNM()->mkNode( kind::OR, conc ) );
1739
61
      Trace("sep-pto")  << "Conclusion is " << n_conc << std::endl;
1740
      // propagation for (pto x y) ^ ~(pto z w) ^ x = z => y != w
1741
61
      sendLemma( exp, n_conc, InferenceId::SEP_PTO_NEG_PROP);
1742
    }
1743
  }else{
1744
825
    if( polarity ){
1745
814
      ei->d_pto.set( p );
1746
814
      validatePto( ei, ei_n );
1747
    }else{
1748
11
      ei->d_has_neg_pto.set( true );
1749
    }
1750
  }
1751
1068
}
1752
1753
351
void TheorySep::mergePto( Node p1, Node p2 ) {
1754
351
  Trace("sep-lemma-debug") << "Merge pto " << p1 << " " << p2 << std::endl;
1755
351
  Assert(p1.getKind() == kind::SEP_LABEL && p1[0].getKind() == kind::SEP_PTO);
1756
351
  Assert(p2.getKind() == kind::SEP_LABEL && p2[0].getKind() == kind::SEP_PTO);
1757
351
  if( !areEqual( p1[0][1], p2[0][1] ) ){
1758
690
    std::vector< Node > exp;
1759
345
    if( p1[1]!=p2[1] ){
1760
232
      Assert(areEqual(p1[1], p2[1]));
1761
232
      exp.push_back( p1[1].eqNode( p2[1] ) );
1762
    }
1763
345
    exp.push_back( p1 );
1764
345
    exp.push_back( p2 );
1765
    //enforces injectiveness of pto : (pto x y) ^ (pto y w) ^ x = y => y = w
1766
345
    sendLemma( exp, p1[0][1].eqNode( p2[0][1] ), InferenceId::SEP_PTO_PROP);
1767
  }
1768
351
}
1769
1770
406
void TheorySep::sendLemma( std::vector< Node >& ant, Node conc, InferenceId id, bool infer ) {
1771
406
  Trace("sep-lemma-debug") << "Do rewrite on inference : " << conc << std::endl;
1772
406
  conc = Rewriter::rewrite( conc );
1773
406
  Trace("sep-lemma-debug") << "Got : " << conc << std::endl;
1774
406
  if( conc!=d_true ){
1775
394
    if( infer && conc!=d_false ){
1776
      Node ant_n = NodeManager::currentNM()->mkAnd(ant);
1777
      Trace("sep-lemma") << "Sep::Infer: " << conc << " from " << ant_n << " by " << id << std::endl;
1778
      d_im.addPendingFact(conc, id, ant_n);
1779
    }else{
1780
394
      if( conc==d_false ){
1781
16
        Trace("sep-lemma") << "Sep::Conflict: " << ant << " by " << id
1782
8
                           << std::endl;
1783
8
        d_im.conflictExp(id, PfRule::THEORY_INFERENCE, ant, {conc});
1784
      }else{
1785
772
        Trace("sep-lemma") << "Sep::Lemma: " << conc << " from " << ant
1786
386
                           << " by " << id << std::endl;
1787
        TrustNode trn =
1788
772
            d_im.mkLemmaExp(conc, PfRule::THEORY_INFERENCE, ant, {}, {conc});
1789
772
        d_im.addPendingLemma(
1790
772
            trn.getNode(), id, LemmaProperty::NONE, trn.getGenerator());
1791
      }
1792
    }
1793
  }
1794
406
}
1795
1796
7044
void TheorySep::doPending()
1797
{
1798
7044
  d_im.doPendingFacts();
1799
7044
  d_im.doPendingLemmas();
1800
7044
}
1801
1802
void TheorySep::debugPrintHeap( HeapInfo& heap, const char * c ) {
1803
  Trace(c) << "[" << std::endl;
1804
  Trace(c) << "  ";
1805
  for( unsigned j=0; j<heap.d_heap_locs.size(); j++ ){
1806
    Trace(c) << heap.d_heap_locs[j] << " ";
1807
  }
1808
  Trace(c) << std::endl;
1809
  Trace(c) << "]" << std::endl;
1810
}
1811
1812
528
Node TheorySep::HeapInfo::getValue( TypeNode tn ) {
1813
528
  Assert(d_heap_locs.size() == d_heap_locs_model.size());
1814
528
  if( d_heap_locs.empty() ){
1815
134
    return NodeManager::currentNM()->mkConst(EmptySet(tn));
1816
  }
1817
788
  Node curr = d_heap_locs[0];
1818
516
  for (unsigned j = 1; j < d_heap_locs.size(); j++)
1819
  {
1820
122
    curr = NodeManager::currentNM()->mkNode(kind::UNION, d_heap_locs[j], curr);
1821
  }
1822
394
  return curr;
1823
}
1824
1825
}  // namespace sep
1826
}  // namespace theory
1827
29337
}  // namespace cvc5