• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

SRI-CSL / yices2 / 16032530443

02 Jul 2025 06:08PM UTC coverage: 60.349% (-5.0%) from 65.357%
16032530443

Pull #582

github

web-flow
Merge b7e09d316 into b3af64ab1
Pull Request #582: Update ci

63716 of 105580 relevant lines covered (60.35%)

1127640.75 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

79.55
/src/context/context.c
1
/*
2
 * This file is part of the Yices SMT Solver.
3
 * Copyright (C) 2017 SRI International.
4
 *
5
 * Yices is free software: you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License as published by
7
 * the Free Software Foundation, either version 3 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * Yices is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with Yices.  If not, see <http://www.gnu.org/licenses/>.
17
 */
18

19
/*
20
 * ASSERTION CONTEXT
21
 */
22

23
#include <inttypes.h>
24

25
#include "context/context.h"
26
#include "context/context_simplifier.h"
27
#include "context/context_utils.h"
28
#include "context/internalization_codes.h"
29
#include "context/ite_flattener.h"
30
#include "solvers/bv/bvsolver.h"
31
#include "solvers/floyd_warshall/idl_floyd_warshall.h"
32
#include "solvers/floyd_warshall/rdl_floyd_warshall.h"
33
#include "solvers/funs/fun_solver.h"
34
#include "solvers/quant/quant_solver.h"
35
#include "solvers/simplex/simplex.h"
36
#include "terms/poly_buffer_terms.h"
37
#include "terms/term_utils.h"
38
#include "utils/memalloc.h"
39

40
#include "mcsat/solver.h"
41
#include "mt/thread_macros.h"
42

43
#include "api/yices_globals.h"
44

45
#define TRACE 0
46

47
#if TRACE
48

49
#include <stdio.h>
50

51
#include "io/term_printer.h"
52
#include "solvers/cdcl/smt_core_printer.h"
53

54
#endif
55

56

57

58

59

60
/**********************
61
 *  INTERNALIZATION   *
62
 *********************/
63

64
/*
65
 * Main internalization functions:
66
 * - convert a term t to an egraph term
67
 * - convert a boolean term t to a literal
68
 * - convert an integer or real term t to an arithmetic variable
69
 * - convert a bitvector term t to a bitvector variable
70
 */
71
static occ_t internalize_to_eterm(context_t *ctx, term_t t);
72
static literal_t internalize_to_literal(context_t *ctx, term_t t);
73
static thvar_t internalize_to_arith(context_t *ctx, term_t t);
74
static thvar_t internalize_to_bv(context_t *ctx, term_t t);
75

76

77

78
/****************************************
79
 *  CONSTRUCTION OF EGRAPH OCCURRENCES  *
80
 ***************************************/
81

82
/*
83
 * Create a new egraph constant of the given type
84
 */
85
static eterm_t make_egraph_constant(context_t *ctx, type_t type, int32_t id) {
1,161✔
86
  assert(type_kind(ctx->types, type) == UNINTERPRETED_TYPE ||
87
         type_kind(ctx->types, type) == SCALAR_TYPE);
88
  return egraph_make_constant(ctx->egraph, type, id);
1,161✔
89
}
90

91

92
/*
93
 * Create a new egraph variable
94
 * - type = its type
95
 */
96
static eterm_t make_egraph_variable(context_t *ctx, type_t type) {
14,044✔
97
  eterm_t u;
98
  bvar_t v;
99

100
  if (type == bool_type(ctx->types)) {
14,044✔
101
    v = create_boolean_variable(ctx->core);
×
102
    u = egraph_bvar2term(ctx->egraph, v);
×
103
  } else {
104
    //    u = egraph_make_variable(ctx->egraph, type);
105
    // it's better to use skolem_term in case type is (tuple ...)
106
    u = egraph_skolem_term(ctx->egraph, type);
14,044✔
107
  }
108
  return u;
14,044✔
109
}
110

111

112
/*
113
 * Type of arithmetic variable x
114
 */
115
static type_t type_of_arithvar(context_t *ctx, thvar_t x) {
1,560✔
116
  type_t tau;
117

118
  tau = real_type(ctx->types);
1,560✔
119
  if (ctx->arith.arith_var_is_int(ctx->arith_solver, x)) {
1,560✔
120
    tau = int_type(ctx->types);
1,423✔
121
  }
122

123
  return tau;
1,560✔
124
}
125

126

127
/*
128
 * Convert arithmetic variable x to an egraph term
129
 */
130
static occ_t translate_arithvar_to_eterm(context_t *ctx, thvar_t x) {
1,569✔
131
  eterm_t u;
132
  type_t tau;
133

134
  u = ctx->arith.eterm_of_var(ctx->arith_solver, x);
1,569✔
135
  if (u == null_eterm) {
1,569✔
136
    tau = type_of_arithvar(ctx, x);
1,560✔
137
    u = egraph_thvar2term(ctx->egraph, x, tau);
1,560✔
138
  }
139

140
  return pos_occ(u);
1,569✔
141
}
142

143
/*
144
 * Convert bit-vector variable x to an egraph term
145
 * - tau = type of x
146
 */
147
static occ_t translate_bvvar_to_eterm(context_t *ctx, thvar_t x, type_t tau) {
3,693✔
148
  eterm_t u;
149

150
  u = ctx->bv.eterm_of_var(ctx->bv_solver, x);
3,693✔
151
  if (u == null_eterm) {
3,693✔
152
    u = egraph_thvar2term(ctx->egraph, x, tau);
3,672✔
153
  }
154

155
  return pos_occ(u);
3,693✔
156
}
157

158

159
/*
160
 * Convert variable x into an eterm internalization for t
161
 * - tau = type of t
162
 * - if x is mapped to an existing eterm u, return pos_occ(u)
163
 * - otherwise, create an egraph variable u and attach x to u
164
 *   then record the converse mapping [x --> u] in the relevant
165
 *   theory solver
166
 */
167
static occ_t translate_thvar_to_eterm(context_t *ctx, thvar_t x, type_t tau) {
3,348✔
168
  if (is_arithmetic_type(tau)) {
3,348✔
169
    return translate_arithvar_to_eterm(ctx, x);
619✔
170
  } else if (is_bv_type(ctx->types, tau)) {
2,729✔
171
    return translate_bvvar_to_eterm(ctx, x, tau);
2,729✔
172
  } else {
173
    longjmp(ctx->env, INTERNAL_ERROR);
×
174
  }
175
}
176

177

178
/*
179
 * Convert internalization code x for a term t into an egraph term
180
 * - t must be a root in the internalization table and must have
181
 *   positive polarity
182
 */
183
static occ_t translate_code_to_eterm(context_t *ctx, term_t t, int32_t x) {
64,835✔
184
  occ_t u;
185
  type_t tau;
186

187
  assert(is_pos_term(t) && intern_tbl_is_root(&ctx->intern, t) &&
188
         intern_tbl_map_of_root(&ctx->intern, t) == x);
189

190
  if (code_is_eterm(x)) {
64,835✔
191
    u = code2occ(x);
64,086✔
192
  } else {
193
    // x encodes a theory variable or a literal
194
    // convert that to an egraph term
195
    tau = type_of_root(ctx, t);
749✔
196
    switch (type_kind(ctx->types, tau)) {
749✔
197
    case BOOL_TYPE:
×
198
      u = egraph_literal2occ(ctx->egraph, code2literal(x));
×
199
      break;
×
200

201
    case INT_TYPE:
611✔
202
    case REAL_TYPE:
203
      u = translate_arithvar_to_eterm(ctx, code2thvar(x));
611✔
204
      break;
611✔
205

206
    case BITVECTOR_TYPE:
138✔
207
      u = translate_bvvar_to_eterm(ctx, code2thvar(x), tau);
138✔
208
      break;
138✔
209

210
    default:
×
211
      assert(false);
212
      longjmp(ctx->env, INTERNAL_ERROR);
×
213
    }
214

215
    // remap t to u
216
    intern_tbl_remap_root(&ctx->intern, t, occ2code(u));
749✔
217
  }
218

219
  return u;
64,835✔
220
}
221

222

223
/*
224
 * Internalization error for term t
225
 * - t can't be processed because there's no egraph
226
 * - the error code depends on t's type
227
 */
228
static int32_t uf_error_code(context_t *ctx, term_t t) {
×
229
  int32_t code;
230

231
  assert(! context_has_egraph(ctx));
232

233
  switch (term_type_kind(ctx->terms, t)) {
×
234
  case UNINTERPRETED_TYPE:
×
235
    code = UTYPE_NOT_SUPPORTED;
×
236
    break;
×
237

238
  case SCALAR_TYPE:
×
239
    code = SCALAR_NOT_SUPPORTED;
×
240
    break;
×
241

242
  case FUNCTION_TYPE:
×
243
    code = UF_NOT_SUPPORTED;
×
244
    break;
×
245

246
  case TUPLE_TYPE:
×
247
    code = TUPLE_NOT_SUPPORTED;
×
248
    break;
×
249

250
  default:
×
251
    assert(false);
252
    code = INTERNAL_ERROR;
×
253
    break;
×
254
  }
255

256
  return code;
×
257
}
258

259

260
/*
261
 * Utility to filter out high-order terms:
262
 * - check whether any term in a[0 ... n-1] has function type.
263
 * - if so throw an exception (via lonjmp) if the context does not include
264
 *   the array solver.
265
 */
266
static void check_high_order_support(context_t *ctx, const term_t *a, uint32_t n) {
17,975✔
267
  uint32_t i;
268

269
  if (! context_has_fun_solver(ctx)) {
17,975✔
270
    for (i=0; i<n; i++) {
30,054✔
271
      if (is_function_term(ctx->terms, a[i])) {
17,190✔
272
        longjmp(ctx->env, HIGH_ORDER_FUN_NOT_SUPPORTED);
3✔
273
      }
274
    }
275
  }
276
}
17,972✔
277

278

279
/***********************************************
280
 *  CONVERSION OF COMPOSITES TO EGRAPH TERMS   *
281
 **********************************************/
282

283
/*
284
 * Map apply term to an eterm
285
 * - tau = type of that term
286
 */
287
static occ_t map_apply_to_eterm(context_t *ctx, composite_term_t *app, type_t tau) {
9,661✔
288
  eterm_t u;
289
  occ_t *a;
290
  uint32_t i, n;
291

292
  assert(app->arity > 0);
293
  n = app->arity;
9,661✔
294

295
  check_high_order_support(ctx, app->arg+1, n-1);
9,661✔
296

297
  a = alloc_istack_array(&ctx->istack, n);
9,661✔
298
  for (i=0; i<n; i++) {
33,957✔
299
    a[i] = internalize_to_eterm(ctx, app->arg[i]);
24,296✔
300
  }
301

302
  // a[0] = function
303
  // a[1 ... n-1] are the arguments
304
  u = egraph_make_apply(ctx->egraph, a[0], n-1, a+1, tau);
9,661✔
305
  free_istack_array(&ctx->istack, a);
9,661✔
306

307
  //  add_type_constraints(ctx, pos_occ(u), tau);
308

309
  return pos_occ(u);
9,661✔
310
}
311

312

313
/*
314
 * Build a tuple of same type as t then assert that it's equal to u1
315
 * - t must be a root in the internalization table
316
 * - u1 must be equal to t's internalization (as stored in intern_table)
317
 * This is the skolemization of (exist (x1...x_n) u1 == (tuple x1 ... x_n))
318
 *
319
 * - return the eterm u := (tuple x1 ... x_n)
320
 */
321
static eterm_t skolem_tuple(context_t *ctx, term_t t, occ_t u1) {
×
322
  type_t tau;
323
  eterm_t u;
324

325
  assert(intern_tbl_is_root(&ctx->intern, t) && is_pos_term(t) &&
326
         intern_tbl_map_of_root(&ctx->intern, t) == occ2code(u1));
327

328
  tau = intern_tbl_type_of_root(&ctx->intern, t);
×
329
  u = egraph_skolem_term(ctx->egraph, tau);
×
330
  egraph_assert_eq_axiom(ctx->egraph, u1, pos_occ(u));
×
331

332
  return u;
×
333
}
334

335

336
/*
337
 * Convert (select i t) to an egraph term
338
 * - tau must be the type of that term (should not be bool)
339
 * - if a new eterm u is created, attach a theory variable to it
340
 */
341
static occ_t map_select_to_eterm(context_t *ctx, select_term_t *s, type_t tau) {
245✔
342
  occ_t u1;
343
  eterm_t tuple;
344
  composite_t *tp;
345

346
  u1 = internalize_to_eterm(ctx, s->arg);
245✔
347
  tuple = egraph_get_tuple_in_class(ctx->egraph, term_of_occ(u1));
245✔
348
  if (tuple == null_eterm) {
245✔
349
    tuple = skolem_tuple(ctx, s->arg, u1);
×
350
  }
351

352
  tp = egraph_term_body(ctx->egraph, tuple);
245✔
353
  assert(composite_body(tp) && tp != NULL && composite_kind(tp) == COMPOSITE_TUPLE);
354

355
  return tp->child[s->idx];
245✔
356
}
357

358

359
/*
360
 * Convert a conditional expression to an egraph term
361
 * - c = conditional descriptor
362
 * - tau = type of c
363
 */
364
static occ_t map_conditional_to_eterm(context_t *ctx, conditional_t *c, type_t tau) {
12✔
365
  literal_t *a;
366
  occ_t u, v;
367
  uint32_t i, n;
368
  literal_t l;
369
  bool all_false;
370
  term_t t;
371

372
#if 0
373
  printf("---> conditional to eterm\n");
374
#endif
375

376
  t = simplify_conditional(ctx, c);
12✔
377
  if (t != NULL_TERM) {
12✔
378
    return internalize_to_eterm(ctx, t);
4✔
379
  }
380

381
  n = c->nconds;
8✔
382
  a = alloc_istack_array(&ctx->istack, n + 1);
8✔
383

384
  all_false = true;
8✔
385
  u = null_occurrence;
8✔
386

387
  for (i=0; i<n; i++) {
24✔
388
    a[i] = internalize_to_literal(ctx, c->pair[i].cond);
16✔
389
    if (a[i] == true_literal) {
16✔
390
      /*
391
       * a[0] ... a[i-1] are all reducible to false
392
       * but we can't assume that a[0] ... a[i-1] are all false_literals
393
       * since we don't know how the theory solver internalizes the
394
       * conditions.
395
       */
396
      v = internalize_to_eterm(ctx, c->pair[i].val);
×
397
      if (all_false) {
×
398
        // all previous conditions a[0 ... i-1] are false
399
        assert(u == null_occurrence);
400
        u = v;
×
401
      } else {
402
        // we assert (u == v) as a top-level equality
403
        egraph_assert_eq_axiom(ctx->egraph, u, v);
×
404
      }
405
      goto done;
×
406
    }
407
    if (a[i] != false_literal) {
16✔
408
      if (all_false) {
13✔
409
        assert(u == null_occurrence);
410
        u = pos_occ(make_egraph_variable(ctx, tau));
7✔
411
        all_false = false;
7✔
412
      }
413
      // one clause for a[i] => (u = v[i])
414
      v = internalize_to_eterm(ctx, c->pair[i].val);
13✔
415
      l = egraph_make_eq(ctx->egraph, u, v);
13✔
416
      add_binary_clause(ctx->core, not(a[i]), l);
13✔
417
    }
418
  }
419

420
  if (all_false) {
8✔
421
    assert(u == null_occurrence);
422
    u = internalize_to_eterm(ctx, c->defval);
1✔
423
    goto done;
1✔
424
  }
425

426
  // clause for the default value
427
  assert(u != null_occurrence);
428
  v = internalize_to_eterm(ctx, c->defval);
7✔
429
  l = egraph_make_eq(ctx->egraph, u, v);
7✔
430
  a[n] = l;
7✔
431
  add_clause(ctx->core, n+1, a);
7✔
432

433
 done:
8✔
434
  free_istack_array(&ctx->istack, a);
8✔
435

436
  return u;
8✔
437
}
438

439

440
/*
441
 * Auxiliary function for flattening if-then-else
442
 * - v contains a conjunction of n literals: l0 /\ ... /\ l_n
443
 * - we something like (l0 /\ ... /\ l_n implies (x = y))
444
 *   (i.e., (not l0) \/ ... \/ (not l_n) \/ (x = y)
445
 * - this function negates all the literals in place
446
 */
447
static void ite_prepare_antecedents(ivector_t *v) {
8,346✔
448
  uint32_t i, n;
449

450
  n = v->size;
8,346✔
451
  for (i=0; i<n; i++) {
20,404✔
452
    v->data[i] = not(v->data[i]);
12,058✔
453
  }
454
}
8,346✔
455

456

457
/*
458
 * Convert nested if-then-else to  an egraph term
459
 * - ite = term of the form (ite c1 t1 t2)
460
 * - c = internalization of c1
461
 * - tau = type of the term (ite c1 t1 t2)
462
 */
463
static occ_t flatten_ite_to_eterm(context_t *ctx, composite_term_t *ite, literal_t c, type_t tau) {
1,168✔
464
  ite_flattener_t *flattener;
465
  ivector_t *buffer;
466
  term_t x;
467
  occ_t u, v;
468
  literal_t l;
469

470
  u = pos_occ(make_egraph_variable(ctx, tau));
1,168✔
471

472
  flattener = objstack_alloc(&ctx->ostack, sizeof(ite_flattener_t), (cleaner_t) delete_ite_flattener);
1,168✔
473
  init_ite_flattener(flattener);
1,168✔
474

475
  ite_flattener_push(flattener, ite, c);
1,168✔
476

477
  while (ite_flattener_is_nonempty(flattener)) {
4,210✔
478
    if (ite_flattener_last_lit_false(flattener)) {
3,042✔
479
      // dead branch
480
      ite_flattener_next_branch(flattener);
6✔
481
      continue;
6✔
482
    }
483
    assert(ite_flattener_branch_is_live(flattener));
484

485
    x = ite_flattener_leaf(flattener);
3,036✔
486
    x = intern_tbl_get_root(&ctx->intern, x);
3,036✔
487

488
    /*
489
     * x is the current leaf.
490
     * If it's (ite ...) then we can expand the tree by pushing x.
491
     *
492
     * Heuristic: we don't do it if x is a shared term or if it's
493
     * already internalized.
494
     * - we also need a cutoff since the number of branches grows
495
     *   exponentially.
496
     */
497
    if (is_pos_term(x) &&
6,072✔
498
        is_ite_term(ctx->terms, x) &&
3,036✔
499
        !intern_tbl_root_is_mapped(&ctx->intern, x) &&
2,473✔
500
        term_is_not_shared(&ctx->sharing, x)) {
774✔
501
      /*
502
       * x is of the form (ite c a b) and not internalized already,
503
       * we push (ite c a b) on the flattener.
504
       */
505
      ite = ite_term_desc(ctx->terms, x);
353✔
506
      assert(ite->arity == 3);
507
      c = internalize_to_literal(ctx, ite->arg[0]);
353✔
508
      ite_flattener_push(flattener, ite, c);
353✔
509
    } else {
510
      /*
511
       * Add the clause [branch conditions => x = u]
512
       */
513
      v = internalize_to_eterm(ctx, x);
2,683✔
514
      l = egraph_make_eq(ctx->egraph, u, v);
2,683✔
515

516
      buffer = &ctx->aux_vector;
2,683✔
517
      assert(buffer->size == 0);
518
      ite_flattener_get_clause(flattener, buffer);
2,683✔
519
      ite_prepare_antecedents(buffer);
2,683✔
520
      ivector_push(buffer, l);
2,683✔
521
      add_clause(ctx->core, buffer->size, buffer->data);
2,683✔
522
      ivector_reset(buffer);
2,683✔
523

524
      ite_flattener_next_branch(flattener);
2,683✔
525
    }
526
  }
527

528
  //  delete_ite_flattener(&flattener);
529
  objstack_pop(&ctx->ostack);
1,168✔
530

531
  return u;
1,168✔
532
}
533

534

535
/*
536
 * Convert (ite c t1 t2) to an egraph term
537
 * - tau = type of (ite c t1 t2)
538
 */
539
static occ_t map_ite_to_eterm(context_t *ctx, composite_term_t *ite, type_t tau) {
2,168✔
540
  conditional_t *d;
541
  eterm_t u;
542
  occ_t u1, u2, u3;
543
  literal_t c, l1, l2;
544

545
  // NOTE: we could reject (ite c t1 t2) when t1 and t2 are functions and we
546
  // don't have a function/array solver. But it looks like the egraph can
547
  // handle it so we accept it. Strictly, it's not a part of UF or QF_UF.
548

549
  d = context_make_conditional(ctx, ite);
2,168✔
550
  if (d != NULL) {
2,168✔
551
    u1 = map_conditional_to_eterm(ctx, d, tau);
12✔
552
    context_free_conditional(ctx, d);
12✔
553
    return u1;
12✔
554
  }
555

556
  c = internalize_to_literal(ctx, ite->arg[0]);
2,156✔
557
  if (c == true_literal) {
2,156✔
558
    return internalize_to_eterm(ctx, ite->arg[1]);
110✔
559
  }
560
  if (c == false_literal) {
2,046✔
561
    return internalize_to_eterm(ctx, ite->arg[2]);
15✔
562
  }
563

564
  if (context_ite_flattening_enabled(ctx)) {
2,031✔
565
    return flatten_ite_to_eterm(ctx, ite, c, tau);
1,168✔
566
  }
567

568
  u2 = internalize_to_eterm(ctx, ite->arg[1]);
863✔
569
  u3 = internalize_to_eterm(ctx, ite->arg[2]);
863✔
570

571
  if (context_keep_ite_enabled(ctx)) {
863✔
572
    // build the if-then-else in the egraph
573
    u1 = egraph_literal2occ(ctx->egraph, c);
2✔
574
    u = egraph_make_ite(ctx->egraph, u1, u2, u3, tau);
2✔
575
  } else {
576
    // eliminate the if-then-else
577
    u = make_egraph_variable(ctx, tau);
861✔
578
    l1 = egraph_make_eq(ctx->egraph, pos_occ(u), u2);
861✔
579
    l2 = egraph_make_eq(ctx->egraph, pos_occ(u), u3);
861✔
580

581
    assert_ite(&ctx->gate_manager, c, l1, l2, true);
861✔
582
  }
583

584
  return pos_occ(u);
863✔
585
}
586

587

588

589
/*
590
 * Convert (update f t_1 ... t_n v) to a term
591
 * - tau = type of that term
592
 */
593
static occ_t map_update_to_eterm(context_t *ctx, composite_term_t *update, type_t tau) {
11,450✔
594
  eterm_t u;
595
  occ_t *a;
596
  uint32_t i, n;
597

598
  assert(update->arity > 2);
599

600
  n = update->arity;
11,450✔
601
  a = alloc_istack_array(&ctx->istack, n);
11,450✔
602
  for (i=0; i<n; i++) {
45,800✔
603
    a[i] = internalize_to_eterm(ctx, update->arg[i]);
34,350✔
604
  }
605

606
  // a[0]: function f
607
  // a[1] ... a[n-2]: t_1 .. t_{n-2}
608
  // a[n-1]: new value v
609
  u = egraph_make_update(ctx->egraph, a[0], n-2, a+1, a[n-1], tau);
11,450✔
610

611
  free_istack_array(&ctx->istack, a);
11,450✔
612

613
  return pos_occ(u);
11,450✔
614
}
615

616

617

618
/*
619
 * Convert (tuple t_1 ... t_n) to a term
620
 * - tau = type of the tuple
621
 */
622
static occ_t map_tuple_to_eterm(context_t *ctx, composite_term_t *tuple, type_t tau) {
190✔
623
  eterm_t u;
624
  occ_t *a;
625
  uint32_t i, n;
626

627
  n = tuple->arity;
190✔
628

629
  check_high_order_support(ctx, tuple->arg, n);
190✔
630

631
  a = alloc_istack_array(&ctx->istack, n);
190✔
632
  for (i=0; i<n; i++) {
560✔
633
    a[i] = internalize_to_eterm(ctx, tuple->arg[i]);
370✔
634
  }
635

636
  u = egraph_make_tuple(ctx->egraph, n, a, tau);
190✔
637
  free_istack_array(&ctx->istack, a);
190✔
638

639
  return pos_occ(u);
190✔
640
}
641

642

643
/*
644
 * Convert arithmetic and bitvector constants to eterm
645
 * - check whether the relevant solver exists first
646
 * - then map the constant to a solver variable x
647
 *   and convert x to an egraph occurrence
648
 */
649
static occ_t map_arith_constant_to_eterm(context_t *ctx, rational_t *q) {
337✔
650
  thvar_t x;
651

652
  if (! context_has_arith_solver(ctx)) {
337✔
653
    longjmp(ctx->env, ARITH_NOT_SUPPORTED);
×
654
  }
655

656
  x = ctx->arith.create_const(ctx->arith_solver, q);
337✔
657
  return translate_arithvar_to_eterm(ctx, x);
337✔
658
}
659

660
static occ_t map_bvconst64_to_eterm(context_t *ctx, bvconst64_term_t *c) {
808✔
661
  thvar_t x;
662
  type_t tau;
663

664
  if (! context_has_bv_solver(ctx)) {
808✔
665
    longjmp(ctx->env, BV_NOT_SUPPORTED);
×
666
  }
667

668
  x = ctx->bv.create_const64(ctx->bv_solver, c);
808✔
669
  tau = bv_type(ctx->types, c->bitsize);
808✔
670

671
  return translate_bvvar_to_eterm(ctx, x, tau);
808✔
672
}
673

674
static occ_t map_bvconst_to_eterm(context_t *ctx, bvconst_term_t *c) {
18✔
675
  thvar_t x;
676
  type_t tau;
677

678
  if (! context_has_bv_solver(ctx)) {
18✔
679
    longjmp(ctx->env, BV_NOT_SUPPORTED);
×
680
  }
681

682
  x = ctx->bv.create_const(ctx->bv_solver, c);
18✔
683
  tau = bv_type(ctx->types, c->bitsize);
18✔
684

685
  return translate_bvvar_to_eterm(ctx, x, tau);
18✔
686
}
687

688

689

690
/***************************************
691
 *  AXIOMS FOR DIV/MOD/FLOOR/CEIL/ABS  *
692
 **************************************/
693

694
/*
695
 * Auxiliary function: p and map to represent (x - y)
696
 * - in polynomial p, only the coefficients are relevant
697
 * - map[0] stores x and map[1] stores y
698
 * - both p map must be large enough (at least 2 elements)
699
 */
700
static void context_store_diff_poly(polynomial_t *p, thvar_t *map, thvar_t x, thvar_t y) {
×
701
  p->nterms = 2;
×
702
  p->mono[0].var = 1;
×
703
  q_set_one(&p->mono[0].coeff);       // coeff of x = 1
×
704
  p->mono[1].var = 2;
×
705
  q_set_minus_one(&p->mono[1].coeff); // coeff of y = -1
×
706
  p->mono[2].var = max_idx; // end marker
×
707

708
  map[0] = x;
×
709
  map[1] = y;
×
710
}
×
711

712

713
/*
714
 * Same thing for the polynomial (x - y - 1)
715
 */
716
static void context_store_diff_minus_one_poly(polynomial_t *p, thvar_t *map, thvar_t x, thvar_t y) {
×
717
  p->nterms = 3;
×
718
  p->mono[0].var = const_idx;
×
719
  q_set_minus_one(&p->mono[0].coeff);  // constant = -1
×
720
  p->mono[1].var = 1;
×
721
  q_set_one(&p->mono[1].coeff);        // coeff of x = 1
×
722
  p->mono[2].var = 2;
×
723
  q_set_minus_one(&p->mono[2].coeff);  // coeff of y = -1
×
724
  p->mono[3].var = max_idx;
×
725

726
  map[0] = null_thvar; // constant
×
727
  map[1] = x;
×
728
  map[2] = y;
×
729
}
×
730

731

732
/*
733
 * Same thing for the polynomial (x + y)
734
 */
735
static void context_store_sum_poly(polynomial_t *p, thvar_t *map, thvar_t x, thvar_t y) {
2✔
736
  p->nterms = 2;
2✔
737
  p->mono[0].var = 1;
2✔
738
  q_set_one(&p->mono[0].coeff); // coeff of x = 1
2✔
739
  p->mono[1].var = 2;
2✔
740
  q_set_one(&p->mono[1].coeff); // coeff of y = 1
2✔
741
  p->mono[2].var = max_idx;
2✔
742

743
  map[0] = x;
2✔
744
  map[1] = y;
2✔
745
}
2✔
746

747

748
/*
749
 * The lower bound on y = (div x k)  is (k * y <= x) or (x - k * y >= 0)
750
 * We store the polynomial x - k * y
751
 */
752
static void context_store_div_lower_bound(polynomial_t *p, thvar_t *map, thvar_t x, thvar_t y, const rational_t *k) {
20✔
753
  p->nterms = 2;
20✔
754
  p->mono[0].var = 1;
20✔
755
  q_set_one(&p->mono[0].coeff);    // coeff of x = 1
20✔
756
  p->mono[1].var = 2;
20✔
757
  q_set_neg(&p->mono[1].coeff, k); // coeff of y = -k
20✔
758
  p->mono[2].var = max_idx;
20✔
759

760
  map[0] = x;
20✔
761
  map[1] = y;
20✔
762
}
20✔
763

764

765
/*
766
 * For converting (divides k x), we use (divides k x) <=> (x <= k * (div x k))
767
 * or (k * y - x >= 0) for y = (div x k).
768
 * We store the polynomial - x + k * y.
769
 */
770
static void context_store_divides_constraint(polynomial_t *p, thvar_t *map, thvar_t x, thvar_t y, const rational_t *k) {
8✔
771
  p->nterms = 2;
8✔
772
  p->mono[0].var = 1;
8✔
773
  q_set_minus_one(&p->mono[0].coeff);  // coeff of x = -1
8✔
774
  p->mono[1].var = 2;
8✔
775
  q_set(&p->mono[1].coeff, k);         // coeff of y = k
8✔
776
  p->mono[2].var = max_idx;
8✔
777

778
  map[0] = x;
8✔
779
  map[1] = y;
8✔
780
}
8✔
781

782
/*
783
 * Upper bound on y = (div x k) when both x and y are integer:
784
 * We have x <= k * y + |k| - 1 or (-x + k y + |k| - 1 >= 0).
785
 *
786
 * We store the polynomial - x + k y + |k| - 1
787
 *
788
 * NOTE: we don't normalize the constant (|k| - 1) to zero if |k| = 1.
789
 * This is safe as the simplex solver does not care.
790
 */
791
static void context_store_integer_div_upper_bound(polynomial_t *p, thvar_t *map, thvar_t x, thvar_t y, const rational_t *k) {
19✔
792
  p->nterms = 3;
19✔
793
  p->mono[0].var = const_idx;
19✔
794
  q_set_abs(&p->mono[0].coeff, k);
19✔
795
  q_sub_one(&p->mono[0].coeff);        // constant term = |k| - 1
19✔
796
  p->mono[1].var = 1;
19✔
797
  q_set_minus_one(&p->mono[1].coeff);  // coeff of x = -1
19✔
798
  p->mono[2].var = 2;
19✔
799
  q_set(&p->mono[2].coeff, k);         // coeff of y = k
19✔
800
  p->mono[3].var = max_idx;
19✔
801

802
  map[0] = null_thvar;
19✔
803
  map[1] = x;
19✔
804
  map[2] = y;
19✔
805
}
19✔
806

807
/*
808
 * Upper bound on y = (div x k) when x or k is not an integer.
809
 * We have x < k * y + |k| or x - k*y - |k| < 0 or (not (x - k*y - |k| >= 0))
810
 *
811
 * We store the polynomial x - ky - |k|
812
 */
813
static void context_store_rational_div_upper_bound(polynomial_t *p, thvar_t *map, thvar_t x, thvar_t y, const rational_t *k) {
1✔
814
  p->nterms = 3;
1✔
815
  p->mono[0].var = const_idx;
1✔
816
  q_set_abs(&p->mono[0].coeff, k);
1✔
817
  q_neg(&p->mono[0].coeff);           // constant term: -|k|
1✔
818
  p->mono[1].var = 1;
1✔
819
  q_set_one(&p->mono[1].coeff);       // coeff of x = +1
1✔
820
  p->mono[2].var = 2;
1✔
821
  q_set_neg(&p->mono[2].coeff, k);    // coeff of y = -k
1✔
822
  p->mono[3].var = max_idx;
1✔
823

824
  map[0] = null_thvar;
1✔
825
  map[1] = x;
1✔
826
  map[2] = y;
1✔
827
}
1✔
828

829

830
/*
831
 * Polynomial x - y + k d (for asserting y = k * (div y k) + (mod y k)
832
 * - d is assumed to be (div y k)
833
 * - x is assumed to be (mod y k)
834
 */
835
static void context_store_divmod_eq(polynomial_t *p, thvar_t *map, thvar_t x, thvar_t y, thvar_t d, const rational_t *k) {
11✔
836
  p->nterms = 3;
11✔
837
  p->mono[0].var = 1;
11✔
838
  q_set_one(&p->mono[0].coeff);       // coefficient of x = 1
11✔
839
  p->mono[1].var = 2;
11✔
840
  q_set_minus_one(&p->mono[1].coeff); // coefficient of y = -1
11✔
841
  p->mono[2].var = 3;
11✔
842
  q_set(&p->mono[2].coeff, k);        // coefficient of d = k
11✔
843
  p->mono[3].var = max_idx;
11✔
844

845
  map[0] = x;
11✔
846
  map[1] = y;
11✔
847
  map[2] = d;
11✔
848
}
11✔
849

850

851
/*
852
 * Bound on x = (mod y k) assuming x and k are integer:
853
 * - the bound is x <= |k| - 1 (i.e., |k| - 1 - x >= 0)
854
 *   so we construct |k| - 1 - x
855
 */
856
static void context_store_integer_mod_bound(polynomial_t *p, thvar_t *map, thvar_t x, const rational_t *k) {
10✔
857
  p->nterms = 2;
10✔
858
  p->mono[0].var = const_idx;
10✔
859
  q_set_abs(&p->mono[0].coeff, k);
10✔
860
  q_sub_one(&p->mono[0].coeff);        // constant = |k| - 1
10✔
861
  p->mono[1].var = 1;
10✔
862
  q_set_minus_one(&p->mono[1].coeff);  // coeff of x = -1
10✔
863
  p->mono[2].var = max_idx;
10✔
864

865
  map[0] = null_thvar;
10✔
866
  map[1] = x;
10✔
867
}
10✔
868

869

870
/*
871
 * Bound on x = (mod y k) when x or k are rational
872
 * - the bound is x < |k| or x - |k| < 0 or (not (x - |k| >= 0))
873
 *   so we construct x - |k|
874
 */
875
static void context_store_rational_mod_bound(polynomial_t *p, thvar_t *map, thvar_t x, const rational_t *k) {
1✔
876
  p->nterms = 2;
1✔
877
  p->mono[0].var = const_idx;
1✔
878
  q_set_abs(&p->mono[0].coeff, k);
1✔
879
  q_neg(&p->mono[0].coeff);            // constant = -|k|
1✔
880
  p->mono[1].var = 1;
1✔
881
  q_set_one(&p->mono[1].coeff);        // coeff of x = +1
1✔
882
  p->mono[2].var = max_idx;
1✔
883

884
  map[0] = null_thvar;
1✔
885
  map[1] = x;
1✔
886
}
1✔
887

888

889
/*
890
 * Assert constraints for x := floor(y)
891
 * - both x and y are variables in the arithmetic solver
892
 * - x has type integer
893
 *
894
 * We assert (x <= y && y < x+1)
895
 */
896
static void assert_floor_axioms(context_t *ctx, thvar_t x, thvar_t y) {
×
897
  polynomial_t *p;
898
  thvar_t map[3];
899

900
  assert(ctx->arith.arith_var_is_int(ctx->arith_solver, x));
901

902
  p = context_get_aux_poly(ctx, 4);
×
903

904
  // assert (y - x >= 0)
905
  context_store_diff_poly(p, map, y, x);
×
906
  ctx->arith.assert_poly_ge_axiom(ctx->arith_solver, p, map, true);
×
907

908
  // assert (y - x - 1 < 0) <=> (not (y - x - 1) >= 0)
909
  context_store_diff_minus_one_poly(p, map, y, x);
×
910
  ctx->arith.assert_poly_ge_axiom(ctx->arith_solver, p, map, false);
×
911
}
×
912

913

914
/*
915
 * Assert constraints for x == ceil(y)
916
 * - both x and y are variables in the arithmetic solver
917
 * - x has type integer
918
 *
919
 * We assert (x - 1 < y && y <= x)
920
 */
921
static void assert_ceil_axioms(context_t *ctx, thvar_t x, thvar_t y) {
×
922
  polynomial_t *p;
923
  thvar_t map[3];
924

925
  assert(ctx->arith.arith_var_is_int(ctx->arith_solver, x));
926

927
  p = context_get_aux_poly(ctx, 4);
×
928

929
  // assert (x - y >= 0)
930
  context_store_diff_poly(p, map, x, y);
×
931
  ctx->arith.assert_poly_ge_axiom(ctx->arith_solver, p, map, true);
×
932

933
  // assert (x - y - 1 < 0) <=> (not (x - y - 1) >= 0)
934
  context_store_diff_minus_one_poly(p, map, x, y);
×
935
  ctx->arith.assert_poly_ge_axiom(ctx->arith_solver, p, map, false);
×
936
}
×
937

938

939
/*
940
 * Assert constraints for x == abs(y)
941
 * - x and y must be variables in the arithmetic solver
942
 *
943
 * We assert (x >= 0) AND ((x == y) or (x == -y))
944
 */
945
static void assert_abs_axioms(context_t *ctx, thvar_t x, thvar_t y) {
2✔
946
  polynomial_t *p;
947
  thvar_t map[2];
948
  literal_t l1, l2;
949

950
  // assert (x >= 0)
951
  ctx->arith.assert_ge_axiom(ctx->arith_solver, x, true);
2✔
952

953
  // create l1 := (x == y)
954
  l1 = ctx->arith.create_vareq_atom(ctx->arith_solver, x, y);
2✔
955

956
  // create l2 := (x == -y) that is (x + y == 0)
957
  p = context_get_aux_poly(ctx, 3);
2✔
958
  context_store_sum_poly(p, map, x, y);
2✔
959
  l2 = ctx->arith.create_poly_eq_atom(ctx->arith_solver, p, map);
2✔
960

961
  // assert (or l1 l2)
962
  add_binary_clause(ctx->core, l1, l2);
2✔
963
}
2✔
964

965

966
/*
967
 * Constraints for x == (div y k)
968
 * - x and y must be variables in the arithmetic solver
969
 * - x must be an integer variable
970
 * - k is a non-zero rational constant
971
 *
972
 * If k and y are integer, we assert
973
 *   k * x <= y <= k * x + |k| - 1
974
 *
975
 * Otherwise, we assert
976
 *   k * x <= y < k * x + |k|
977
 */
978
static void assert_div_axioms(context_t *ctx, thvar_t x, thvar_t y, const rational_t *k) {
20✔
979
  polynomial_t *p;
980
  thvar_t map[3];
981

982
  p = context_get_aux_poly(ctx, 4);
20✔
983

984
  // assert k*x <= y (i.e., y - k*x >= 0)
985
  context_store_div_lower_bound(p, map, y, x, k);
20✔
986
  ctx->arith.assert_poly_ge_axiom(ctx->arith_solver, p, map, true);
20✔
987

988
  if (ctx->arith.arith_var_is_int(ctx->arith_solver, y) && q_is_integer(k)) {
20✔
989
    // y and k are both integer
990
    // assert y <= k*x + |k| - 1 (i.e., - y + k x + |k| - 1 >= 0)
991
    context_store_integer_div_upper_bound(p, map, y, x, k);
19✔
992
    ctx->arith.assert_poly_ge_axiom(ctx->arith_solver, p, map, true);
19✔
993

994
  } else {
995
    // assert y < k*x + |k| (i.e., y - k*x - |k| < 0) or (not (y - k*x - |k| >= 0))
996
    context_store_rational_div_upper_bound(p, map, y, x, k);
1✔
997
    ctx->arith.assert_poly_ge_axiom(ctx->arith_solver, p, map, false);
1✔
998
  }
999
}
20✔
1000

1001

1002
/*
1003
 * Constraints for x == (mod y k)
1004
 * - d must be the variable equal to (div y k)
1005
 * - x and y must be variables in the arithmetic solver
1006
 * - k is a non-zero rational constant.
1007
 *
1008
 * We assert x = y - k * d (i.e., (mod y k) = x - k * (div y k))
1009
 * and 0 <= x < |k|.
1010
 *
1011
 * NOTE: The 0 <= x < |k| part is redundant. It's implied by the
1012
 * div_axioms for d = (div y k). It's cheap enough that I can't
1013
 * see a problem with adding it anyway (it's just an interval for x).
1014
 */
1015
static void assert_mod_axioms(context_t *ctx, thvar_t x, thvar_t y, thvar_t d, const rational_t *k) {
11✔
1016
  polynomial_t *p;
1017
  thvar_t map[3];
1018

1019
  p = context_get_aux_poly(ctx, 4);
11✔
1020

1021
  // assert y = k * d + x (i.e., x - y + k *d = 0)
1022
  context_store_divmod_eq(p, map, x, y, d, k);
11✔
1023
  ctx->arith.assert_poly_eq_axiom(ctx->arith_solver, p, map, true);
11✔
1024

1025
  // assert x >= 0
1026
  ctx->arith.assert_ge_axiom(ctx->arith_solver, x, true);
11✔
1027

1028
  if (ctx->arith.arith_var_is_int(ctx->arith_solver, x) && q_is_integer(k)) {
11✔
1029
    // both x and |k| are integer
1030
    // assert x <= |k| - 1, i.e., -x + |k| - 1 >= 0
1031
    context_store_integer_mod_bound(p, map, x, k);
10✔
1032
    ctx->arith.assert_poly_ge_axiom(ctx->arith_solver, p, map, true);
10✔
1033
  } else {
1034
    // assert x < |k|, i.e., x - |k| <0, i.e., (not (x - |k| >= 0))
1035
    context_store_rational_mod_bound(p, map, x, k);
1✔
1036
    ctx->arith.assert_poly_ge_axiom(ctx->arith_solver, p, map, false);
1✔
1037
  }
1038
}
11✔
1039

1040

1041

1042
/******************************************************
1043
 *  CONVERSION OF COMPOSITES TO ARITHMETIC VARIABLES  *
1044
 *****************************************************/
1045

1046
/*
1047
 * Convert a conditional to an arithmetic variable
1048
 * - if is_int is true, the variable is integer otherwise, it's real
1049
 */
1050
static thvar_t map_conditional_to_arith(context_t *ctx, conditional_t *c, bool is_int) {
239✔
1051
  literal_t *a;
1052
  uint32_t i, n;
1053
  thvar_t x, v;
1054
  bool all_false;
1055
  term_t t;
1056

1057
#if 0
1058
  printf("---> conditional to arith\n");
1059
#endif
1060

1061
  t = simplify_conditional(ctx, c);
239✔
1062
  if (t != NULL_TERM) {
239✔
1063
    return internalize_to_arith(ctx, t);
13✔
1064
  }
1065

1066
  n = c->nconds;
226✔
1067
  a = alloc_istack_array(&ctx->istack, n);
226✔
1068

1069
  all_false = true;
226✔
1070
  v = null_thvar;
226✔
1071

1072
  for (i=0; i<n; i++) {
703✔
1073
    a[i] = internalize_to_literal(ctx, c->pair[i].cond);
485✔
1074
    if (a[i] == true_literal) {
485✔
1075
      /*
1076
       * a[0] ... a[i-1] are all reducible to false
1077
       * but we can't assume v == null_thvar, since
1078
       * we don't know how the theory solver internalizes
1079
       * the conditions (i.e., some of them may not be false_literal).
1080
       */
1081
      x = internalize_to_arith(ctx, c->pair[i].val);
8✔
1082
      if (all_false) {
8✔
1083
        assert(v == null_thvar);
1084
        v = x;
6✔
1085
      } else {
1086
        // assert (v == x) in the arithmetic solver
1087
        ctx->arith.assert_vareq_axiom(ctx->arith_solver, v, x, true);
2✔
1088
      }
1089
      goto done;
8✔
1090
    }
1091
    if (a[i] != false_literal) {
477✔
1092
      if (all_false) {
460✔
1093
        assert(v == null_thvar);
1094
        v = ctx->arith.create_var(ctx->arith_solver, is_int);
215✔
1095
        all_false = false;
215✔
1096
      }
1097
      // clause for a[i] => (v = c->pair[i].val)
1098
      x = internalize_to_arith(ctx, c->pair[i].val);
460✔
1099
      ctx->arith.assert_cond_vareq_axiom(ctx->arith_solver, a[i], v, x);
460✔
1100
    }
1101
  }
1102

1103
  if (all_false) {
218✔
1104
    assert(v == null_thvar);
1105
    v = internalize_to_arith(ctx, c->defval);
5✔
1106
    goto done;
5✔
1107
  }
1108

1109
  /*
1110
   * last clause (only if some a[i] isn't false):
1111
   * (a[0] \/ ... \/ a[n-1] \/ v == c->defval)
1112
   */
1113
  assert(v != null_thvar);
1114
  x = internalize_to_arith(ctx, c->defval);
213✔
1115
  ctx->arith.assert_clause_vareq_axiom(ctx->arith_solver, n, a, v, x);
213✔
1116

1117
 done:
226✔
1118
  free_istack_array(&ctx->istack, a);
226✔
1119
  return v;
226✔
1120
}
1121

1122

1123
/*
1124
 * Convert nested if-then-else to  an arithmetic variable
1125
 * - ite = term of the form (ite c1 t1 t2)
1126
 * - c = internalization of c1
1127
 * - is_int = true if the if-then-else term is integer (otherwise it's real)
1128
 */
1129
static thvar_t flatten_ite_to_arith(context_t *ctx, composite_term_t *ite, literal_t c, bool is_int) {
2,520✔
1130
  ite_flattener_t *flattener;
1131
  ivector_t *buffer;
1132
  term_t x;
1133
  thvar_t u, v;
1134

1135
  u = ctx->arith.create_var(ctx->arith_solver, is_int);
2,520✔
1136

1137
  flattener = objstack_alloc(&ctx->ostack, sizeof(ite_flattener_t), (cleaner_t) delete_ite_flattener);
2,520✔
1138
  init_ite_flattener(flattener);
2,520✔
1139

1140
  ite_flattener_push(flattener, ite, c);
2,520✔
1141

1142
  while (ite_flattener_is_nonempty(flattener)) {
8,820✔
1143
    if (ite_flattener_last_lit_false(flattener)) {
6,300✔
1144
      // dead branch
1145
      ite_flattener_next_branch(flattener);
7✔
1146
      continue;
7✔
1147
    }
1148
    assert(ite_flattener_branch_is_live(flattener));
1149

1150
    x = ite_flattener_leaf(flattener);
6,293✔
1151
    x = intern_tbl_get_root(&ctx->intern, x);
6,293✔
1152

1153
    /*
1154
     * x is the current leaf
1155
     * If x is of the form (ite c a b) we can push (ite c a b) on the flattener.
1156
     *
1157
     * Heuristics: don't push the term if x is already internalized or if it's
1158
     * shared.
1159
     */
1160
    if (is_pos_term(x) &&
12,586✔
1161
        is_ite_term(ctx->terms, x) &&
6,293✔
1162
        !intern_tbl_root_is_mapped(&ctx->intern, x) &&
3,934✔
1163
        term_is_not_shared(&ctx->sharing, x)) {
1,256✔
1164
      ite = ite_term_desc(ctx->terms, x);
630✔
1165
      assert(ite->arity == 3);
1166
      c = internalize_to_literal(ctx, ite->arg[0]);
630✔
1167
      ite_flattener_push(flattener, ite, c);
630✔
1168
    } else {
1169
      /*
1170
       * Add the clause [branch conditions => x = u]
1171
       */
1172
      v = internalize_to_arith(ctx, x);
5,663✔
1173

1174
      buffer = &ctx->aux_vector;
5,663✔
1175
      assert(buffer->size == 0);
1176
      ite_flattener_get_clause(flattener, buffer);
5,663✔
1177
      ite_prepare_antecedents(buffer);
5,663✔
1178
      // assert [buffer \/ v = u]
1179
      ctx->arith.assert_clause_vareq_axiom(ctx->arith_solver, buffer->size, buffer->data, v, u);
5,663✔
1180
      ivector_reset(buffer);
5,663✔
1181

1182
      ite_flattener_next_branch(flattener);
5,663✔
1183
    }
1184
  }
1185

1186
  //  delete_ite_flattener(&flattener);
1187
   objstack_pop(&ctx->ostack);
2,520✔
1188

1189
  return u;
2,520✔
1190
}
1191

1192
/*
1193
 * Convert if-then-else to an arithmetic variable
1194
 * - if is_int is true, the if-then-else term is integer
1195
 * - otherwise, it's real
1196
 */
1197
static thvar_t map_ite_to_arith(context_t *ctx, composite_term_t *ite, bool is_int) {
3,022✔
1198
  conditional_t *d;
1199
  literal_t c;
1200
  thvar_t v, x;
1201

1202
  assert(ite->arity == 3);
1203

1204
  d = context_make_conditional(ctx, ite);
3,022✔
1205
  if (d != NULL) {
3,022✔
1206
    v = map_conditional_to_arith(ctx, d, is_int);
239✔
1207
    context_free_conditional(ctx, d);
239✔
1208
    return v;
239✔
1209
  }
1210

1211
  c = internalize_to_literal(ctx, ite->arg[0]); // condition
2,783✔
1212
  if (c == true_literal) {
2,783✔
1213
    return internalize_to_arith(ctx, ite->arg[1]);
77✔
1214
  }
1215
  if (c == false_literal) {
2,706✔
1216
    return internalize_to_arith(ctx, ite->arg[2]);
115✔
1217
  }
1218

1219
  if (context_ite_flattening_enabled(ctx)) {
2,591✔
1220
    return flatten_ite_to_arith(ctx, ite, c, is_int);
2,520✔
1221
  }
1222

1223

1224
  /*
1225
   * no simplification: create a fresh variable v and assert (c ==> v = t1)
1226
   * and (not c ==> v = t2)
1227
   */
1228
  v = ctx->arith.create_var(ctx->arith_solver, is_int);
71✔
1229

1230
  x = internalize_to_arith(ctx, ite->arg[1]);
71✔
1231
  ctx->arith.assert_cond_vareq_axiom(ctx->arith_solver, c, v, x); // c ==> v = t1
71✔
1232

1233
  x = internalize_to_arith(ctx, ite->arg[2]);
71✔
1234
  ctx->arith.assert_cond_vareq_axiom(ctx->arith_solver, not(c), v, x); // (not c) ==> v = t2
71✔
1235

1236
  return v;
71✔
1237
}
1238

1239

1240
/*
1241
 * Assert the bounds on t when t is an arithmetic, special if-then-else
1242
 * - x = arithmetic variable mapped to t in the arithmetic solver
1243
 */
1244
static void assert_ite_bounds(context_t *ctx, term_t t, thvar_t x) {
792✔
1245
  term_table_t *terms;
1246
  polynomial_t *p;
1247
  term_t lb, ub;
1248
  thvar_t map[2];
1249

1250
  terms = ctx->terms;
792✔
1251
  assert(is_arithmetic_term(terms, t));
1252

1253
  // get lower and upper bound on t. Both are rational constants
1254
  term_finite_domain_bounds(terms, t, &lb, &ub);
792✔
1255

1256
#if 0
1257
  printf("assert ite bound:\n  term: ");
1258
  print_term_name(stdout, terms, t);
1259
  printf("\n");
1260
  printf("  lower bound: ");
1261
  print_term_full(stdout, terms, lb);
1262
  printf("\n");
1263
  printf("  upper bound: ");
1264
  print_term_full(stdout, terms, ub);
1265
  printf("\n");
1266
#endif
1267

1268
  /*
1269
   * prepare polynomial p:
1270
   * first monomial is a constant, second monomial is either +t or -t
1271
   * map[0] = null (what's mapped to const_idx)
1272
   * map[1] = x = (what's mapped to t)
1273
   */
1274
  p = context_get_aux_poly(ctx, 3);
792✔
1275
  p->nterms = 2;
792✔
1276
  p->mono[0].var = const_idx;
792✔
1277
  p->mono[1].var = t;
792✔
1278
  p->mono[2].var = max_idx;
792✔
1279
  map[0] = null_thvar;
792✔
1280
  map[1] = x;
792✔
1281

1282

1283
  // first bound: t >= lb
1284
  q_set_neg(&p->mono[0].coeff, rational_term_desc(terms, lb)); // -lb
792✔
1285
  q_set_one(&p->mono[1].coeff); // +t
792✔
1286
  ctx->arith.assert_poly_ge_axiom(ctx->arith_solver, p, map, true); // assert -lb + t >= 0
792✔
1287

1288
  // second bound: t <= ub
1289
  q_set(&p->mono[0].coeff, rational_term_desc(terms, ub));  // +ub
792✔
1290
  q_set_minus_one(&p->mono[1].coeff);  // -t
792✔
1291
  ctx->arith.assert_poly_ge_axiom(ctx->arith_solver, p, map, true); // assert +ub - t >= 0
792✔
1292
}
792✔
1293

1294

1295
/*
1296
 * Convert a power product to an arithmetic variable
1297
 */
1298
static thvar_t map_pprod_to_arith(context_t *ctx, pprod_t *p) {
×
1299
  uint32_t i, n;
1300
  thvar_t *a;
1301
  thvar_t x;
1302

1303
  n = p->len;
×
1304
  a = alloc_istack_array(&ctx->istack, n);
×
1305
  for (i=0; i<n; i++) {
×
1306
    a[i] = internalize_to_arith(ctx, p->prod[i].var);
×
1307
  }
1308

1309
  x = ctx->arith.create_pprod(ctx->arith_solver, p, a);
×
1310
  free_istack_array(&ctx->istack, a);
×
1311

1312
  return x;
×
1313
}
1314

1315

1316
/*
1317
 * Convert polynomial p to an arithmetic variable
1318
 */
1319
static thvar_t map_poly_to_arith(context_t *ctx, polynomial_t *p) {
1,762✔
1320
  uint32_t i, n;
1321
  thvar_t *a;
1322
  thvar_t x;
1323

1324
  n = p->nterms;
1,762✔
1325
  a = alloc_istack_array(&ctx->istack, n);
1,762✔
1326

1327
  // skip the constant if any
1328
  i = 0;
1,762✔
1329
  if (p->mono[0].var == const_idx) {
1,762✔
1330
    a[0] = null_thvar;
467✔
1331
    i ++;
467✔
1332
  }
1333

1334
  // deal with the non-constant monomials
1335
  while (i<n) {
5,035✔
1336
    a[i] = internalize_to_arith(ctx, p->mono[i].var);
3,273✔
1337
    i ++;
3,273✔
1338
  }
1339

1340
  // build the polynomial
1341
  x = ctx->arith.create_poly(ctx->arith_solver, p, a);
1,762✔
1342
  free_istack_array(&ctx->istack, a);
1,762✔
1343

1344
  return x;
1,762✔
1345
}
1346

1347

1348
/*
1349
 * Auxiliary function: return y := (floor x)
1350
 * - check the divmod table first.
1351
 *   If there's a record for (floor x), return the corresponding variable.
1352
 * - Otherwise, create a fresh integer variable y,
1353
 *   assert the axioms for y = (floor x)
1354
 *   add a record to the divmod table and return y.
1355
 */
1356
static thvar_t get_floor(context_t *ctx, thvar_t x) {
×
1357
  thvar_t y;
1358

1359
  y = context_find_var_for_floor(ctx, x);
×
1360
  if (y == null_thvar) {
×
1361
    y = ctx->arith.create_var(ctx->arith_solver, true); // y is an integer variable
×
1362
    assert_floor_axioms(ctx, y, x); // assert y = floor(x)
×
1363
    context_record_floor(ctx, x, y); // save the mapping y --> floor(x)
×
1364
  }
1365

1366
  return y;
×
1367
}
1368

1369

1370
/*
1371
 * Return y := (div x k)
1372
 * - check the divmod table first
1373
 * - if (div x k) has already been processed, return the corresponding variable
1374
 * - otherwise create a new variable y, assert the axioms for y = (div x k)
1375
 *   add a record in the divmod table, and return y.
1376
 */
1377
static thvar_t get_div(context_t *ctx, thvar_t x, const rational_t *k) {
24✔
1378
  thvar_t y;
1379

1380
  y = context_find_var_for_div(ctx, x, k);
24✔
1381
  if (y == null_thvar) {
24✔
1382
    // create y := (div x k)
1383
    y = ctx->arith.create_var(ctx->arith_solver, true); // y is an integer
20✔
1384
    assert_div_axioms(ctx, y, x, k);
20✔
1385
    context_record_div(ctx, x, k, y);
20✔
1386
  }
1387

1388
  return y;
24✔
1389
}
1390

1391

1392

1393
/*
1394
 * Convert (floor t) to an arithmetic variable
1395
 */
1396
static thvar_t map_floor_to_arith(context_t *ctx, term_t t) {
×
1397
  thvar_t x, y;
1398

1399
  x = internalize_to_arith(ctx, t);
×
1400
  if (ctx->arith.arith_var_is_int(ctx->arith_solver, x)) {
×
1401
    // x is integer so (floor x) = x
1402
    y = x;
×
1403
  } else {
1404
    y = get_floor(ctx, x);
×
1405
  }
1406

1407
  return y;
×
1408
}
1409

1410

1411
/*
1412
 * Convert (ceil t) to an arithmetic variable
1413
 */
1414
static thvar_t map_ceil_to_arith(context_t *ctx, term_t t) {
×
1415
  thvar_t x, y;
1416

1417
  x = internalize_to_arith(ctx, t);
×
1418
  if (ctx->arith.arith_var_is_int(ctx->arith_solver, x)) {
×
1419
    // x is integer so (ceil x) = x
1420
    y = x;
×
1421
  } else {
1422
    y = context_find_var_for_ceil(ctx, x);
×
1423
    if (y == null_thvar) {
×
1424
      y = ctx->arith.create_var(ctx->arith_solver, true); // y is an integer variable
×
1425
      assert_ceil_axioms(ctx, y, x); // assert y = ceil(x)
×
1426
      context_record_ceil(ctx, x, y); // save the mapping y --> ceil(x)
×
1427
    }
1428
  }
1429

1430
  return y;
×
1431
}
1432

1433

1434
/*
1435
 * Convert (abs t) to an arithmetic variable
1436
 */
1437
static thvar_t map_abs_to_arith(context_t *ctx, term_t t) {
2✔
1438
  thvar_t x, y;
1439
  bool is_int;
1440

1441
  x = internalize_to_arith(ctx, t);
2✔
1442
  is_int = ctx->arith.arith_var_is_int(ctx->arith_solver, x);
2✔
1443
  y = ctx->arith.create_var(ctx->arith_solver, is_int); // y := abs(x) has the same type as x
2✔
1444
  assert_abs_axioms(ctx, y, x);
2✔
1445

1446
  return y;
2✔
1447
}
1448

1449

1450
/*
1451
 * Auxiliary function: check whether t is a non-zero arithmetic constant
1452
 * - if so, store t's value in *val
1453
 */
1454
static bool is_non_zero_rational(term_table_t *tbl, term_t t, rational_t *val) {
16✔
1455
  assert(is_arithmetic_term(tbl, t));
1456

1457
  if (term_kind(tbl, t) == ARITH_CONSTANT) {
16✔
1458
    q_set(val, rational_term_desc(tbl, t));
16✔
1459
    return q_is_nonzero(val);
16✔
1460
  }
1461
  return false;
×
1462
}
1463

1464

1465
/*
1466
 * Error in division: either the divisor is zero or is non-constant
1467
 */
1468
static void __attribute__((noreturn))  bad_divisor(context_t *ctx, term_t t) {
2✔
1469
  term_table_t *tbl;
1470
  int code;
1471

1472
  tbl = ctx->terms;
2✔
1473
  assert(is_arithmetic_term(tbl, t) && is_pos_term(t));
1474

1475
  code = FORMULA_NOT_LINEAR;
2✔
1476
  if (term_kind(tbl, t) == ARITH_CONSTANT && q_is_zero(rational_term_desc(tbl, t))) {
2✔
1477
    code = DIV_BY_ZERO;
2✔
1478
  }
1479
  longjmp(ctx->env, code);
2✔
1480
}
1481

1482
/*
1483
 * Convert (/ t1 t2) to an arithmetic variable
1484
 * - t2 must be a non-zero arithmetic constant
1485
 */
1486
static thvar_t map_rdiv_to_arith(context_t *ctx, composite_term_t *div) {
2✔
1487
  // Could try to evaluate t2 then check whether that's a constant
1488
  assert(div->arity == 2);
1489
  bad_divisor(ctx, div->arg[1]);
2✔
1490
}
1491

1492

1493
/*
1494
 * Convert (div t1 t2) to an arithmetic variable.
1495
 * - fails if t2 is not an arithmetic constant or if it's zero
1496
 */
1497
static thvar_t map_idiv_to_arith(context_t *ctx, composite_term_t *div) {
5✔
1498
  rational_t k;
1499
  thvar_t x, y;
1500

1501
  assert(div->arity == 2);
1502

1503
  q_init(&k);
5✔
1504
  if (is_non_zero_rational(ctx->terms, div->arg[1], &k)) { // k := value of t2
5✔
1505
    assert(q_is_nonzero(&k));
1506
    x = internalize_to_arith(ctx, div->arg[0]); // t1
5✔
1507
    y = get_div(ctx, x, &k);
5✔
1508

1509
  } else {
1510
    // division by a non-constant or by zero: not supported by default
1511
    // arithmetic solver for now
1512
    q_clear(&k);
×
1513
    bad_divisor(ctx, div->arg[1]);
×
1514
  }
1515
  q_clear(&k);
5✔
1516

1517
  return y;
5✔
1518
}
1519

1520

1521
/*
1522
 * Convert (mod t1 t2) to an arithmetic variable
1523
 * - t2 must be a non-zero constant
1524
 */
1525
static thvar_t map_mod_to_arith(context_t *ctx, composite_term_t *mod) {
11✔
1526
  rational_t k;
1527
  thvar_t x, y, r;
1528
  bool is_int;
1529

1530
  assert(mod->arity == 2);
1531

1532
  q_init(&k);
11✔
1533
  if (is_non_zero_rational(ctx->terms, mod->arg[1], &k)) { // k := divider
11✔
1534
    x = internalize_to_arith(ctx, mod->arg[0]);
11✔
1535

1536
    // get y := (div x k)
1537
    assert(q_is_nonzero(&k));
1538
    y = get_div(ctx, x, &k);
11✔
1539

1540
    /*
1541
     * r := (mod x k) is x - k * y where y is an integer.
1542
     * If both x and k are integer, then r has integer type. Otherwise,
1543
     * r is a real variable.
1544
     */
1545
    is_int = ctx->arith.arith_var_is_int(ctx->arith_solver, x) && q_is_integer(&k);
11✔
1546
    r = ctx->arith.create_var(ctx->arith_solver, is_int);
11✔
1547
    assert_mod_axioms(ctx, r, x, y, &k);
11✔
1548

1549
  } else {
1550
    // Non-constant or zero divider
1551
    q_clear(&k);
×
1552
    bad_divisor(ctx, mod->arg[1]);
×
1553
  }
1554

1555
  q_clear(&k);
11✔
1556

1557
  return r;
11✔
1558
}
1559

1560

1561

1562
/******************************************************
1563
 *  CONVERSION OF COMPOSITES TO BIT-VECTOR VARIABLES  *
1564
 *****************************************************/
1565

1566
/*
1567
 * Convert if-then-else to a bitvector variable
1568
 */
1569
static thvar_t map_ite_to_bv(context_t *ctx, composite_term_t *ite) {
20,779✔
1570
  literal_t c;
1571
  thvar_t x, y;
1572

1573
  assert(ite->arity == 3);
1574

1575
  c = internalize_to_literal(ctx, ite->arg[0]);
20,779✔
1576
  if (c == true_literal) {
20,779✔
1577
    return internalize_to_bv(ctx, ite->arg[1]);
23✔
1578
  }
1579
  if (c == false_literal) {
20,756✔
1580
    return internalize_to_bv(ctx, ite->arg[2]);
2,082✔
1581
  }
1582

1583
  // no simplification
1584
  x = internalize_to_bv(ctx, ite->arg[1]);
18,674✔
1585
  y = internalize_to_bv(ctx, ite->arg[2]);
18,674✔
1586

1587
  return ctx->bv.create_bvite(ctx->bv_solver, c, x, y);
18,674✔
1588
}
1589

1590

1591
/*
1592
 * Array of bits b
1593
 * - hackish: we locally disable flattening here
1594
 */
1595
static thvar_t map_bvarray_to_bv(context_t *ctx, composite_term_t *b) {
10,481✔
1596
  uint32_t i, n;
1597
  uint32_t save_options;
1598
  literal_t *a;
1599
  thvar_t x;
1600

1601
  n = b->arity;
10,481✔
1602
  a = alloc_istack_array(&ctx->istack, n);
10,481✔
1603

1604
  save_options = ctx->options;
10,481✔
1605
  disable_diseq_and_or_flattening(ctx);
10,481✔
1606
  for (i=0; i<n; i++) {
351,964✔
1607
    a[i] = internalize_to_literal(ctx, b->arg[i]);
341,483✔
1608
  }
1609
  ctx->options = save_options;
10,481✔
1610

1611
  x = ctx->bv.create_bvarray(ctx->bv_solver, a, n);
10,481✔
1612

1613
  free_istack_array(&ctx->istack, a);
10,481✔
1614

1615
  return x;
10,481✔
1616
}
1617

1618

1619
/*
1620
 * Unsigned division: quotient (div u v)
1621
 */
1622
static thvar_t map_bvdiv_to_bv(context_t *ctx, composite_term_t *div) {
74✔
1623
  thvar_t x, y;
1624

1625
  assert(div->arity == 2);
1626
  x = internalize_to_bv(ctx, div->arg[0]);
74✔
1627
  y = internalize_to_bv(ctx, div->arg[1]);
74✔
1628

1629
  return ctx->bv.create_bvdiv(ctx->bv_solver, x, y);
74✔
1630
}
1631

1632

1633
/*
1634
 * Unsigned division: remainder (rem u v)
1635
 */
1636
static thvar_t map_bvrem_to_bv(context_t *ctx, composite_term_t *rem) {
82✔
1637
  thvar_t x, y;
1638

1639
  assert(rem->arity == 2);
1640
  x = internalize_to_bv(ctx, rem->arg[0]);
82✔
1641
  y = internalize_to_bv(ctx, rem->arg[1]);
82✔
1642

1643
  return ctx->bv.create_bvrem(ctx->bv_solver, x, y);
82✔
1644
}
1645

1646

1647
/*
1648
 * Signed division/rounding toward 0: quotient (sdiv u v)
1649
 */
1650
static thvar_t map_bvsdiv_to_bv(context_t *ctx, composite_term_t *sdiv) {
87✔
1651
  thvar_t x, y;
1652

1653
  assert(sdiv->arity == 2);
1654
  x = internalize_to_bv(ctx, sdiv->arg[0]);
87✔
1655
  y = internalize_to_bv(ctx, sdiv->arg[1]);
87✔
1656

1657
  return ctx->bv.create_bvsdiv(ctx->bv_solver, x, y);
87✔
1658
}
1659

1660

1661
/*
1662
 * Signed division/rounding toward 0: remainder (srem u v)
1663
 */
1664
static thvar_t map_bvsrem_to_bv(context_t *ctx, composite_term_t *srem) {
77✔
1665
  thvar_t x, y;
1666

1667
  assert(srem->arity == 2);
1668
  x = internalize_to_bv(ctx, srem->arg[0]);
77✔
1669
  y = internalize_to_bv(ctx, srem->arg[1]);
77✔
1670

1671
  return ctx->bv.create_bvsrem(ctx->bv_solver, x, y);
77✔
1672
}
1673

1674

1675
/*
1676
 * Signed division/rounding toward -infinity: remainder (smod u v)
1677
 */
1678
static thvar_t map_bvsmod_to_bv(context_t *ctx, composite_term_t *smod) {
×
1679
  thvar_t x, y;
1680

1681
  assert(smod->arity == 2);
1682
  x = internalize_to_bv(ctx, smod->arg[0]);
×
1683
  y = internalize_to_bv(ctx, smod->arg[1]);
×
1684

1685
  return ctx->bv.create_bvsmod(ctx->bv_solver, x, y);
×
1686
}
1687

1688

1689
/*
1690
 * Left shift: (shl u v)
1691
 */
1692
static thvar_t map_bvshl_to_bv(context_t *ctx, composite_term_t *shl) {
71✔
1693
  thvar_t x, y;
1694

1695
  assert(shl->arity == 2);
1696
  x = internalize_to_bv(ctx, shl->arg[0]);
71✔
1697
  y = internalize_to_bv(ctx, shl->arg[1]);
71✔
1698

1699
  return ctx->bv.create_bvshl(ctx->bv_solver, x, y);
71✔
1700
}
1701

1702

1703
/*
1704
 * Logical shift right: (lshr u v)
1705
 */
1706
static thvar_t map_bvlshr_to_bv(context_t *ctx, composite_term_t *lshr) {
204✔
1707
  thvar_t x, y;
1708

1709
  assert(lshr->arity == 2);
1710
  x = internalize_to_bv(ctx, lshr->arg[0]);
204✔
1711
  y = internalize_to_bv(ctx, lshr->arg[1]);
204✔
1712

1713
  return ctx->bv.create_bvlshr(ctx->bv_solver, x, y);
204✔
1714
}
1715

1716

1717
/*
1718
 * Arithmetic shift right: (ashr u v)
1719
 */
1720
static thvar_t map_bvashr_to_bv(context_t *ctx, composite_term_t *ashr) {
62✔
1721
  thvar_t x, y;
1722

1723
  assert(ashr->arity == 2);
1724
  x = internalize_to_bv(ctx, ashr->arg[0]);
62✔
1725
  y = internalize_to_bv(ctx, ashr->arg[1]);
62✔
1726

1727
  return ctx->bv.create_bvashr(ctx->bv_solver, x, y);
62✔
1728
}
1729

1730

1731

1732
/*
1733
 * TODO: check for simplifications in bitvector arithmetic
1734
 * before translation to bitvector variables.
1735
 *
1736
 * This matters for the wienand-cav2008 benchmarks.
1737
 */
1738

1739
/*
1740
 * Power product
1741
 */
1742
static thvar_t map_pprod_to_bv(context_t *ctx, pprod_t *p) {
565✔
1743
  uint32_t i, n;
1744
  thvar_t *a;
1745
  thvar_t x;
1746

1747
  n = p->len;
565✔
1748
  a = alloc_istack_array(&ctx->istack, n);
565✔
1749
  for (i=0; i<n; i++) {
1,666✔
1750
    a[i] = internalize_to_bv(ctx, p->prod[i].var);
1,101✔
1751
  }
1752

1753
  x = ctx->bv.create_pprod(ctx->bv_solver, p, a);
565✔
1754
  free_istack_array(&ctx->istack, a);
565✔
1755

1756
  return x;
565✔
1757
}
1758

1759

1760
/*
1761
 * Bitvector polynomial, 64bit coefficients
1762
 */
1763
static thvar_t map_bvpoly64_to_bv(context_t *ctx, bvpoly64_t *p) {
9,541✔
1764
  uint32_t i, n;
1765
  thvar_t *a;
1766
  thvar_t x;
1767

1768
  assert(p->nterms > 0);
1769

1770
  n = p->nterms;
9,541✔
1771
  a = alloc_istack_array(&ctx->istack, n);
9,541✔
1772

1773
  // skip the constant if any
1774
  i = 0;
9,541✔
1775
  if (p->mono[0].var == const_idx) {
9,541✔
1776
    a[0] = null_thvar;
2,058✔
1777
    i ++;
2,058✔
1778
  }
1779

1780
  // non-constant monomials
1781
  while (i < n) {
21,930✔
1782
    a[i] = internalize_to_bv(ctx, p->mono[i].var);
12,389✔
1783
    i ++;
12,389✔
1784
  }
1785

1786
  x = ctx->bv.create_poly64(ctx->bv_solver, p, a);
9,541✔
1787
  free_istack_array(&ctx->istack, a);
9,541✔
1788

1789
  return x;
9,541✔
1790
}
1791

1792

1793
/*
1794
 * Bitvector polynomial, coefficients have more than 64bits
1795
 */
1796
static thvar_t map_bvpoly_to_bv(context_t *ctx, bvpoly_t *p) {
607✔
1797
  uint32_t i, n;
1798
  thvar_t *a;
1799
  thvar_t x;
1800

1801
  assert(p->nterms > 0);
1802

1803
  n = p->nterms;
607✔
1804
  a = alloc_istack_array(&ctx->istack, n);
607✔
1805

1806
  // skip the constant if any
1807
  i = 0;
607✔
1808
  if (p->mono[0].var == const_idx) {
607✔
1809
    a[0] = null_thvar;
539✔
1810
    i ++;
539✔
1811
  }
1812

1813
  // non-constant monomials
1814
  while (i < n) {
1,268✔
1815
    a[i] = internalize_to_bv(ctx, p->mono[i].var);
661✔
1816
    i ++;
661✔
1817
  }
1818

1819
  x = ctx->bv.create_poly(ctx->bv_solver, p, a);
607✔
1820
  free_istack_array(&ctx->istack, a);
607✔
1821

1822
  return x;
607✔
1823
}
1824

1825

1826
#if 0
1827
/*
1828
 * Bvpoly buffer: b must be normalized.
1829
 * - not optimal but this shouldn't be called often.
1830
 */
1831
static thvar_t map_bvpoly_buffer_to_bv(context_t *ctx, bvpoly_buffer_t *b) {
1832
  bvpoly64_t *p;
1833
  bvpoly_t *q;
1834
  uint32_t n;
1835
  thvar_t x;
1836

1837
  n = bvpoly_buffer_bitsize(b);
1838

1839
  if (bvpoly_buffer_is_zero(b)) {
1840
    x = ctx->bv.create_zero(ctx->bv_solver, n);
1841
  } else if (n <= 64) {
1842
    p = bvpoly_buffer_getpoly64(b);
1843
    x = map_bvpoly64_to_bv(ctx, p);
1844
    free_bvpoly64(p);
1845
  } else {
1846
    q = bvpoly_buffer_getpoly(b);
1847
    x = map_bvpoly_to_bv(ctx, q);
1848
    free_bvpoly(q);
1849
  }
1850

1851
  return x;
1852
}
1853

1854
#endif
1855

1856
/****************************
1857
 *  CONVERSION TO LITERALS  *
1858
 ***************************/
1859

1860
/*
1861
 * Boolean if-then-else
1862
 */
1863
static literal_t map_ite_to_literal(context_t *ctx, composite_term_t *ite) {
1,863✔
1864
  literal_t l1, l2, l3;
1865

1866
  assert(ite->arity == 3);
1867
  l1 = internalize_to_literal(ctx, ite->arg[0]); // condition
1,863✔
1868
  if (l1 == true_literal) {
1,863✔
1869
    return internalize_to_literal(ctx, ite->arg[1]);
32✔
1870
  }
1871
  if (l1 == false_literal) {
1,831✔
1872
    return internalize_to_literal(ctx, ite->arg[2]);
71✔
1873
  }
1874

1875
  l2 = internalize_to_literal(ctx, ite->arg[1]);
1,760✔
1876
  l3 = internalize_to_literal(ctx, ite->arg[2]);
1,760✔
1877

1878
  return mk_ite_gate(&ctx->gate_manager, l1, l2, l3);
1,760✔
1879
}
1880

1881

1882
/*
1883
 * Generic equality: (eq t1 t2)
1884
 * - t1 and t2 are not arithmetic or bitvector terms
1885
 */
1886
static literal_t map_eq_to_literal(context_t *ctx, composite_term_t *eq) {
14,505✔
1887
  occ_t u, v;
1888
  literal_t l1, l2, l;
1889

1890
  assert(eq->arity == 2);
1891

1892
  if (is_boolean_term(ctx->terms, eq->arg[0])) {
14,505✔
1893
    assert(is_boolean_term(ctx->terms, eq->arg[1]));
1894

1895
    l1 = internalize_to_literal(ctx, eq->arg[0]);
9,045✔
1896
    l2 = internalize_to_literal(ctx, eq->arg[1]);
9,045✔
1897
    l = mk_iff_gate(&ctx->gate_manager, l1, l2);
9,045✔
1898
  } else {
1899
    // filter out high-order terms. It's enough to check eq->arg[0]
1900
    check_high_order_support(ctx, eq->arg, 1);
5,460✔
1901

1902
    u = internalize_to_eterm(ctx, eq->arg[0]);
5,460✔
1903
    v = internalize_to_eterm(ctx, eq->arg[1]);
5,460✔
1904
    l = egraph_make_eq(ctx->egraph, u, v);
5,460✔
1905
  }
1906

1907
  return l;
14,505✔
1908
}
1909

1910

1911
/*
1912
 * (or t1 ... t_n)
1913
 */
1914
static literal_t map_or_to_literal(context_t *ctx, composite_term_t *or) {
79,564✔
1915
  int32_t *a;
1916
  ivector_t *v;
1917
  literal_t l;
1918
  uint32_t i, n;
1919

1920
  if (context_flatten_or_enabled(ctx)) {
79,564✔
1921
    // flatten (or ...): store result in v
1922
    v = &ctx->aux_vector;
13,071✔
1923
    assert(v->size == 0);
1924
    flatten_or_term(ctx, v, or);
13,071✔
1925

1926
    // try easy simplification
1927
    n = v->size;
13,071✔
1928
    if (disjunct_is_true(ctx, v->data, n)) {
13,071✔
1929
      ivector_reset(v);
334✔
1930
      return true_literal;
334✔
1931
    }
1932

1933
    // make a copy of v
1934
    a = alloc_istack_array(&ctx->istack, n);
12,737✔
1935
    for (i=0; i<n; i++) {
70,268✔
1936
      a[i] = v->data[i];
57,531✔
1937
    }
1938
    ivector_reset(v);
12,737✔
1939

1940
    // internalize a[0 ... n-1]
1941
    for (i=0; i<n; i++) {
69,295✔
1942
      l = internalize_to_literal(ctx, a[i]);
56,840✔
1943
      if (l == true_literal) goto done;
56,840✔
1944
      a[i] = l;
56,558✔
1945
    }
1946

1947
  } else {
1948
    // no flattening
1949
    n = or->arity;
66,493✔
1950
    if (disjunct_is_true(ctx, or->arg, n)) {
66,493✔
1951
      return true_literal;
204✔
1952
    }
1953

1954
    a = alloc_istack_array(&ctx->istack, n);
66,289✔
1955
    for (i=0; i<n; i++) {
249,261✔
1956
      l = internalize_to_literal(ctx, or->arg[i]);
183,283✔
1957
      if (l == true_literal) goto done;
183,283✔
1958
      a[i] = l;
182,972✔
1959
    }
1960
  }
1961

1962
  l = mk_or_gate(&ctx->gate_manager, n, a);
78,433✔
1963

1964
 done:
79,026✔
1965
  free_istack_array(&ctx->istack, a);
79,026✔
1966

1967
  return l;
79,026✔
1968
}
1969

1970

1971
/*
1972
 * (xor t1 ... t_n)
1973
 */
1974
static literal_t map_xor_to_literal(context_t *ctx, composite_term_t *xor) {
×
1975
  int32_t *a;
1976
  literal_t l;
1977
  uint32_t i, n;
1978

1979
  n = xor->arity;
×
1980
  a = alloc_istack_array(&ctx->istack, n);
×
1981
  for (i=0; i<n; i++) {
×
1982
    a[i] = internalize_to_literal(ctx, xor->arg[i]);
×
1983
  }
1984

1985
  l = mk_xor_gate(&ctx->gate_manager, n, a);
×
1986
  free_istack_array(&ctx->istack, a);
×
1987

1988
  return l;
×
1989
}
1990

1991

1992
/*
1993
 * Convert (p t_1 .. t_n) to a literal
1994
 * - create an egraph atom
1995
 */
1996
static literal_t map_apply_to_literal(context_t *ctx, composite_term_t *app) {
4,358✔
1997
  occ_t *a;
1998
  uint32_t i, n;
1999
  literal_t l;
2000

2001
  assert(app->arity > 0);
2002
  n = app->arity;
4,358✔
2003
  a = alloc_istack_array(&ctx->istack, n);
4,358✔
2004
  for (i=0; i<n; i++) {
16,295✔
2005
    a[i] = internalize_to_eterm(ctx, app->arg[i]);
11,937✔
2006
  }
2007

2008
  // a[0] = predicate
2009
  // a[1 ...n-1] = arguments
2010
  l = egraph_make_pred(ctx->egraph, a[0], n-1, a + 1);
4,358✔
2011
  free_istack_array(&ctx->istack, a);
4,358✔
2012

2013
  return l;
4,358✔
2014
}
2015

2016

2017

2018
/*
2019
 * Auxiliary function: translate (distinct a[0 ... n-1]) to a literal,
2020
 * when a[0] ... a[n-1] are arithmetic variables.
2021
 *
2022
 * We expand this into a quadratic number of disequalities.
2023
 */
2024
static literal_t make_arith_distinct(context_t *ctx, uint32_t n, thvar_t *a) {
158✔
2025
  uint32_t i, j;
2026
  ivector_t *v;
2027
  literal_t l;
2028

2029
  assert(n >= 2);
2030

2031
  v = &ctx->aux_vector;
158✔
2032
  assert(v->size == 0);
2033
  for (i=0; i<n-1; i++) {
3,776✔
2034
    for (j=i+1; j<n; j++) {
48,648✔
2035
      l = ctx->arith.create_vareq_atom(ctx->arith_solver, a[i], a[j]);
45,030✔
2036
      ivector_push(v, l);
45,030✔
2037
    }
2038
  }
2039
  l = mk_or_gate(&ctx->gate_manager, v->size, v->data);
158✔
2040
  ivector_reset(v);
158✔
2041

2042
  return not(l);
158✔
2043
}
2044

2045

2046
/*
2047
 * Auxiliary function: translate (distinct a[0 ... n-1]) to a literal,
2048
 * when a[0] ... a[n-1] are bitvector variables.
2049
 *
2050
 * We expand this into a quadratic number of disequalities.
2051
 */
2052
static literal_t make_bv_distinct(context_t *ctx, uint32_t n, thvar_t *a) {
2✔
2053
  uint32_t i, j;
2054
  ivector_t *v;
2055
  literal_t l;
2056

2057
  assert(n >= 2);
2058

2059
  v = &ctx->aux_vector;
2✔
2060
  assert(v->size == 0);
2061
  for (i=0; i<n-1; i++) {
11✔
2062
    for (j=i+1; j<n; j++) {
40✔
2063
      l = ctx->bv.create_eq_atom(ctx->bv_solver, a[i], a[j]);
31✔
2064
      ivector_push(v, l);
31✔
2065
    }
2066
  }
2067
  l = mk_or_gate(&ctx->gate_manager, v->size, v->data);
2✔
2068
  ivector_reset(v);
2✔
2069

2070
  return not(l);
2✔
2071
}
2072

2073

2074
/*
2075
 * Convert (distinct t_1 ... t_n) to a literal
2076
 */
2077
static literal_t map_distinct_to_literal(context_t *ctx, composite_term_t *distinct) {
9✔
2078
  int32_t *a;
2079
  literal_t l;
2080
  uint32_t i, n;
2081

2082
  n = distinct->arity;
9✔
2083
  a = alloc_istack_array(&ctx->istack, n);
9✔
2084
  if (context_has_egraph(ctx)) {
9✔
2085
    // fail if arguments are functions and we don't support high-order terms
2086
    // checking the first argument is enough since they all have the same type
2087
    check_high_order_support(ctx, distinct->arg, 1);
6✔
2088

2089
    // default: translate to the egraph
2090
    for (i=0; i<n; i++) {
25✔
2091
      a[i] = internalize_to_eterm(ctx, distinct->arg[i]);
19✔
2092
    }
2093
    l = egraph_make_distinct(ctx->egraph, n, a);
6✔
2094

2095
  } else if (is_arithmetic_term(ctx->terms, distinct->arg[0])) {
3✔
2096
    // translate to arithmetic variables
2097
    for (i=0; i<n; i++) {
12✔
2098
      a[i] = internalize_to_arith(ctx, distinct->arg[i]);
9✔
2099
    }
2100
    l = make_arith_distinct(ctx, n, a);
3✔
2101

2102
  } else if (is_bitvector_term(ctx->terms, distinct->arg[0])) {
×
2103
    // translate to bitvector variables
2104
    for (i=0; i<n; i++) {
×
2105
      a[i] = internalize_to_bv(ctx, distinct->arg[i]);
×
2106
    }
2107
    l = make_bv_distinct(ctx, n, a);
×
2108

2109
  } else {
2110
    longjmp(ctx->env, uf_error_code(ctx, distinct->arg[0]));
×
2111
  }
2112

2113
  free_istack_array(&ctx->istack, a);
9✔
2114

2115
  return l;
9✔
2116
}
2117

2118

2119

2120
/*
2121
 * Arithmetic atom: p == 0
2122
 */
2123
static literal_t map_poly_eq_to_literal(context_t *ctx, polynomial_t *p) {
1,192✔
2124
  uint32_t i, n;
2125
  thvar_t *a;
2126
  literal_t l;
2127

2128
  n = p->nterms;
1,192✔
2129
  a = alloc_istack_array(&ctx->istack, n);
1,192✔
2130

2131
  // skip the constant if any
2132
  i = 0;
1,192✔
2133
  if (p->mono[0].var == const_idx) {
1,192✔
2134
    a[0] = null_thvar;
570✔
2135
    i ++;
570✔
2136
  }
2137

2138
  // deal with the non-constant monomials
2139
  while (i<n) {
4,166✔
2140
    a[i] = internalize_to_arith(ctx, p->mono[i].var);
2,974✔
2141
    i ++;
2,974✔
2142
  }
2143

2144
  // build the atom
2145
  l = ctx->arith.create_poly_eq_atom(ctx->arith_solver, p, a);
1,192✔
2146
  free_istack_array(&ctx->istack, a);
1,192✔
2147

2148
  return l;
1,192✔
2149
}
2150

2151

2152
/*
2153
 * Arithmetic atom: (p >= 0)
2154
 */
2155
static literal_t map_poly_ge_to_literal(context_t *ctx, polynomial_t *p) {
13,491✔
2156
  uint32_t i, n;
2157
  thvar_t *a;
2158
  literal_t l;
2159

2160
  n = p->nterms;
13,491✔
2161
  a = alloc_istack_array(&ctx->istack, n);
13,491✔
2162

2163
  // skip the constant if any
2164
  i = 0;
13,491✔
2165
  if (p->mono[0].var == const_idx) {
13,491✔
2166
    a[0] = null_thvar;
6,316✔
2167
    i ++;
6,316✔
2168
  }
2169

2170
  // deal with the non-constant monomials
2171
  while (i<n) {
44,644✔
2172
    a[i] = internalize_to_arith(ctx, p->mono[i].var);
31,153✔
2173
    i ++;
31,153✔
2174
  }
2175

2176
  // build the atom
2177
  l = ctx->arith.create_poly_ge_atom(ctx->arith_solver, p, a);
13,491✔
2178
  free_istack_array(&ctx->istack, a);
13,491✔
2179

2180
  return l;
13,491✔
2181
}
2182

2183

2184
/*
2185
 * Arithmetic atom: (t >= 0)
2186
 */
2187
static literal_t map_arith_geq_to_literal(context_t *ctx, term_t t) {
13,837✔
2188
  term_table_t *terms;
2189
  thvar_t x;
2190
  literal_t l;
2191

2192
  terms = ctx->terms;
13,837✔
2193
  if (term_kind(terms, t) == ARITH_POLY) {
13,837✔
2194
    l = map_poly_ge_to_literal(ctx, poly_term_desc(terms, t));
13,491✔
2195
  } else {
2196
    x = internalize_to_arith(ctx, t);
346✔
2197
    l = ctx->arith.create_ge_atom(ctx->arith_solver, x);
346✔
2198
  }
2199

2200
  return l;
13,837✔
2201
}
2202

2203

2204

2205
/*
2206
 * Arithmetic equalities (eq t1 t2)
2207
 * 1) try to flatten the if-then-elses
2208
 * 2) also apply cheap lift-if rule: (eq (ite c t1 t2) u1) --> (ite c (eq t1 u1) (eq t2 u1))
2209
 */
2210
static literal_t map_arith_bineq(context_t *ctx, term_t t1, term_t u1);
2211

2212
/*
2213
 * Lift equality: (eq (ite c u1 u2) t) --> (ite c (eq u1 t) (eq u2 t))
2214
 */
2215
static literal_t map_ite_arith_bineq(context_t *ctx, composite_term_t *ite, term_t t) {
42,609✔
2216
  literal_t l1, l2, l3;
2217

2218
  assert(ite->arity == 3);
2219
  l1 = internalize_to_literal(ctx, ite->arg[0]);
42,609✔
2220
  if (l1 == true_literal) {
42,609✔
2221
    // (eq (ite true u1 u2) t) --> (eq u1 t)
2222
    return map_arith_bineq(ctx, ite->arg[1], t);
7✔
2223
  }
2224
  if (l1 == false_literal) {
42,602✔
2225
    // (eq (ite true u1 u2) t) --> (eq u2 t)
2226
    return map_arith_bineq(ctx, ite->arg[2], t);
109✔
2227
  }
2228

2229
  // apply lift-if here
2230
  l2 = map_arith_bineq(ctx, ite->arg[1], t);
42,493✔
2231
  l3 = map_arith_bineq(ctx, ite->arg[2], t);
42,493✔
2232

2233
  return mk_ite_gate(&ctx->gate_manager, l1, l2, l3);
42,493✔
2234
}
2235

2236
static literal_t map_arith_bineq_aux(context_t *ctx, term_t t1, term_t t2) {
53,484✔
2237
  term_table_t *terms;
2238
  thvar_t x, y;
2239
  occ_t u, v;
2240
  literal_t l;
2241

2242
  /*
2243
   * Try to apply lift-if rule: (eq (ite c u1 u2) t2) --> (ite c (eq u1 t2) (eq u2 t2))
2244
   * do this only if t2 is not an if-then-else term.
2245
   *
2246
   * Otherwise add the atom (eq t1 t2) to the egraph if possible
2247
   * or create (eq t1 t2) in the arithmetic solver.
2248
   */
2249
  terms = ctx->terms;
53,484✔
2250
  if (is_ite_term(terms, t1) && ! is_ite_term(terms, t2)) {
53,484✔
2251
    l = map_ite_arith_bineq(ctx, ite_term_desc(terms, t1), t2);
38,573✔
2252
  } else if (is_ite_term(terms, t2) && !is_ite_term(terms, t1)) {
14,911✔
2253
    l = map_ite_arith_bineq(ctx, ite_term_desc(terms, t2), t1);
4,036✔
2254
  } else if (context_has_egraph(ctx)) {
10,875✔
2255
    u = internalize_to_eterm(ctx, t1);
3,754✔
2256
    v = internalize_to_eterm(ctx, t2);
3,754✔
2257
    l = egraph_make_eq(ctx->egraph, u, v);
3,754✔
2258
  } else {
2259
    x = internalize_to_arith(ctx, t1);
7,121✔
2260
    y = internalize_to_arith(ctx, t2);
7,121✔
2261
    l = ctx->arith.create_vareq_atom(ctx->arith_solver, x, y);
7,121✔
2262
  }
2263

2264
  return l;
53,484✔
2265
}
2266

2267
static literal_t map_arith_bineq(context_t *ctx, term_t t1, term_t u1) {
95,341✔
2268
  ivector_t *v;
2269
  int32_t *a;
2270
  uint32_t i, n;
2271
  term_t t2, u2;
2272
  literal_t l;
2273

2274
  t1 = intern_tbl_get_root(&ctx->intern, t1);
95,341✔
2275
  u1 = intern_tbl_get_root(&ctx->intern, u1);
95,341✔
2276

2277
  if (t1 == u1) {
95,341✔
2278
    return true_literal;
3,222✔
2279
  }
2280

2281
  /*
2282
   * Check the cache
2283
   */
2284
  l = find_in_eq_cache(ctx, t1, u1);
92,119✔
2285
  if (l == null_literal) {
92,119✔
2286
    /*
2287
     * The pair (t1, u1) is not mapped already.
2288
     * Try to flatten the if-then-else equalities
2289
     */
2290
    v = &ctx->aux_vector;
53,411✔
2291
    assert(v->size == 0);
2292
    t2 = flatten_ite_equality(ctx, v, t1, u1);
53,411✔
2293
    u2 = flatten_ite_equality(ctx, v, u1, t2);
53,411✔
2294

2295
    /*
2296
     * (t1 == u1) is equivalent to (and (t2 == u2) v[0] ... v[n-1])
2297
     * where v[i] = element i of v
2298
     */
2299
    n = v->size;
53,411✔
2300
    if (n == 0) {
53,411✔
2301
      // empty v: return (t2 == u2)
2302
      assert(t1 == t2 && u1 == u2);
2303
      l = map_arith_bineq_aux(ctx, t2, u2);
10,660✔
2304

2305
    } else {
2306
      // build (and (t2 == u2) v[0] ... v[n-1])
2307
      // first make a copy of v into a[0 .. n-1]
2308
      a = alloc_istack_array(&ctx->istack, n+1);
42,751✔
2309
      for (i=0; i<n; i++) {
766,594✔
2310
        a[i] = v->data[i];
723,843✔
2311
      }
2312
      ivector_reset(v);
42,751✔
2313

2314
      // build the internalization of a[0 .. n-1]
2315
      for (i=0; i<n; i++) {
766,594✔
2316
        a[i] = internalize_to_literal(ctx, a[i]);
723,843✔
2317
      }
2318
      a[n] = map_arith_bineq_aux(ctx, t2, u2);
42,751✔
2319

2320
      // build (and a[0] ... a[n])
2321
      l = mk_and_gate(&ctx->gate_manager, n+1, a);
42,751✔
2322
      free_istack_array(&ctx->istack, a);
42,751✔
2323
    }
2324

2325
    /*
2326
     * Store the mapping (t1, u1) --> l in the cache
2327
     */
2328
    add_to_eq_cache(ctx, t1, u1, l);
53,411✔
2329
  }
2330

2331
  return l;
92,119✔
2332
}
2333

2334

2335
static inline literal_t map_arith_bineq_to_literal(context_t *ctx, composite_term_t *eq) {
8,947✔
2336
  assert(eq->arity == 2);
2337
  return map_arith_bineq(ctx, eq->arg[0], eq->arg[1]);
8,947✔
2338
}
2339

2340

2341

2342
/*
2343
 * Arithmetic atom: (t == 0)
2344
 */
2345
static literal_t map_arith_eq_to_literal(context_t *ctx, term_t t) {
2,229✔
2346
  term_table_t *terms;
2347
  thvar_t x;
2348
  literal_t l;
2349

2350
  terms = ctx->terms;
2,229✔
2351
  if (term_kind(terms, t) == ARITH_POLY) {
2,229✔
2352
    l = map_poly_eq_to_literal(ctx, poly_term_desc(terms, t));
1,192✔
2353
  } else if (is_ite_term(terms, t)) {
1,037✔
2354
    l = map_arith_bineq(ctx, t, zero_term);
426✔
2355
  } else {
2356
    x = internalize_to_arith(ctx, t);
611✔
2357
    l = ctx->arith.create_eq_atom(ctx->arith_solver, x);
611✔
2358
  }
2359
  return l;
2,229✔
2360
}
2361

2362

2363
/*
2364
 * DIVIDES AND IS_INT ATOMS
2365
 */
2366

2367
/*
2368
 * We use the rules
2369
 * - (is_int x)    <=> (x <= floor(x))
2370
 * - (divides k x) <=> (x <= k * div(x, k))
2371
 */
2372

2373
// atom (is_int t)
2374
static literal_t map_arith_is_int_to_literal(context_t *ctx, term_t t) {
×
2375
  polynomial_t *p;
2376
  thvar_t map[2];
2377
  thvar_t x, y;
2378
  literal_t l;
2379

2380
  x = internalize_to_arith(ctx, t);
×
2381
  if (ctx->arith.arith_var_is_int(ctx->arith_solver, x)) {
×
2382
    l = true_literal;
×
2383
  } else {
2384
    // we convert (is_int x) to (x <= floor(x))
2385
    y = get_floor(ctx, x); // y is floor x
×
2386
    p = context_get_aux_poly(ctx, 3);
×
2387
    context_store_diff_poly(p, map, y, x); // (p, map) := (y - x)
×
2388
    // atom (x <= y) is the same as (p >= 0)
2389
    l = ctx->arith.create_poly_ge_atom(ctx->arith_solver, p, map);
×
2390
  }
2391

2392
  return l;
×
2393
}
2394

2395
// atom (divides k t)  we assume k != 0
2396
static literal_t map_arith_divides_to_literal(context_t *ctx, composite_term_t *divides) {
8✔
2397
  rational_t k;
2398
  polynomial_t *p;
2399
  thvar_t map[2];
2400
  thvar_t x, y;
2401
  term_t d;
2402
  literal_t l;
2403

2404
  assert(divides->arity == 2);
2405

2406
  d = divides->arg[0];
8✔
2407
  if (term_kind(ctx->terms, d) == ARITH_CONSTANT) {
8✔
2408
    // make a copy of divides->arg[0] in k
2409
    q_init(&k);
8✔
2410
    q_set(&k, rational_term_desc(ctx->terms, d));
8✔
2411
    assert(q_is_nonzero(&k));
2412

2413
    x = internalize_to_arith(ctx, divides->arg[1]); // this is t
8✔
2414
    y = get_div(ctx, x, &k);  // y := (div x k)
8✔
2415
    p = context_get_aux_poly(ctx, 3);
8✔
2416
    context_store_divides_constraint(p, map, x, y, &k); // p is (- x + k * y)
8✔
2417
    // atom (x <= k * y) is (p >= 0)
2418
    l = ctx->arith.create_poly_ge_atom(ctx->arith_solver, p, map);
8✔
2419

2420
    q_clear(&k);
8✔
2421

2422
    return l;
8✔
2423

2424
  } else {
2425
    // k is not a constant: not supported
2426
    longjmp(ctx->env, FORMULA_NOT_LINEAR);
×
2427
  }
2428
}
2429

2430

2431
/*
2432
 * BITVECTOR ATOMS
2433
 */
2434

2435
/*
2436
 * Auxiliary function: atom for (t == 0)
2437
 */
2438
static literal_t map_bveq0_to_literal(context_t *ctx, term_t t) {
10✔
2439
  uint32_t n;
2440
  thvar_t x, y;
2441

2442
  t = intern_tbl_get_root(&ctx->intern, t);
10✔
2443
  n = term_bitsize(ctx->terms, t);
10✔
2444
  x = internalize_to_bv(ctx, t);
10✔
2445
  y = ctx->bv.create_zero(ctx->bv_solver, n);
10✔
2446

2447
  return ctx->bv.create_eq_atom(ctx->bv_solver, x, y);
10✔
2448
}
2449

2450
static literal_t map_bveq_to_literal(context_t *ctx, composite_term_t *eq) {
3,999✔
2451
  bveq_simp_t simp;
2452
  term_t t, t1, t2;
2453
  thvar_t x, y;
2454

2455
  assert(eq->arity == 2);
2456

2457
  /*
2458
   * Apply substitution then check for simplifications
2459
   */
2460
  t1 = intern_tbl_get_root(&ctx->intern, eq->arg[0]);
3,999✔
2461
  t2 = intern_tbl_get_root(&ctx->intern, eq->arg[1]);
3,999✔
2462
  t = simplify_bitvector_eq(ctx, t1, t2);
3,999✔
2463
  if (t != NULL_TERM) {
3,999✔
2464
    // (bveq t1 t2) is equivalent to t
2465
    return internalize_to_literal(ctx, t);
72✔
2466
  }
2467

2468
  /*
2469
   * More simplifications
2470
   */
2471
  try_arithmetic_bveq_simplification(ctx, &simp, t1, t2);
3,927✔
2472
  switch (simp.code) {
3,927✔
2473
  case BVEQ_CODE_TRUE:
×
2474
    return true_literal;
×
2475

2476
  case BVEQ_CODE_FALSE:
×
2477
    return false_literal;
×
2478

2479
  case BVEQ_CODE_REDUCED:
11✔
2480
    t1 = intern_tbl_get_root(&ctx->intern, simp.left);
11✔
2481
    t2 = intern_tbl_get_root(&ctx->intern, simp.right);
11✔
2482
    break;
11✔
2483

2484
  case BVEQ_CODE_REDUCED0:
10✔
2485
    // (t1 == t2) is reduced to (simp.left == 0)
2486
    // we create the atom directly here:
2487
    return map_bveq0_to_literal(ctx, simp.left);
10✔
2488

2489
  default:
3,906✔
2490
    break;
3,906✔
2491
  }
2492

2493
  if (equal_bitvector_factors(ctx, t1, t2)) {
3,917✔
2494
    return true_literal;
×
2495
  }
2496

2497
  /*
2498
   * NOTE: creating (eq t1 t2) in the egraph instead makes things worse
2499
   */
2500
  x = internalize_to_bv(ctx, t1);
3,917✔
2501
  y = internalize_to_bv(ctx, t2);
3,917✔
2502
  return ctx->bv.create_eq_atom(ctx->bv_solver, x, y);
3,917✔
2503
}
2504

2505
static literal_t map_bvge_to_literal(context_t *ctx, composite_term_t *ge) {
2,174✔
2506
  thvar_t x, y;
2507

2508
  assert(ge->arity == 2);
2509
  x = internalize_to_bv(ctx, ge->arg[0]);
2,174✔
2510
  y = internalize_to_bv(ctx, ge->arg[1]);
2,174✔
2511

2512
  return ctx->bv.create_ge_atom(ctx->bv_solver, x, y);
2,174✔
2513
}
2514

2515
static literal_t map_bvsge_to_literal(context_t *ctx, composite_term_t *sge) {
2,431✔
2516
  thvar_t x, y;
2517

2518
  assert(sge->arity == 2);
2519
  x = internalize_to_bv(ctx, sge->arg[0]);
2,431✔
2520
  y = internalize_to_bv(ctx, sge->arg[1]);
2,431✔
2521

2522
  return ctx->bv.create_sge_atom(ctx->bv_solver, x, y);
2,431✔
2523
}
2524

2525

2526
// Select bit
2527
static literal_t map_bit_select_to_literal(context_t *ctx, select_term_t *select) {
135,402✔
2528
  term_t t, s;
2529
  thvar_t x;
2530

2531
  /*
2532
   * Apply substitution then try to simplify
2533
   */
2534
  t = intern_tbl_get_root(&ctx->intern, select->arg);
135,402✔
2535
  s = extract_bit(ctx->terms, t, select->idx);
135,402✔
2536
  if (s != NULL_TERM) {
135,402✔
2537
    // (select t i) is s
2538
    return internalize_to_literal(ctx, s);
21,953✔
2539
  } else {
2540
    // no simplification
2541
    x = internalize_to_bv(ctx, t);
113,449✔
2542
    return ctx->bv.select_bit(ctx->bv_solver, x, select->idx);
113,449✔
2543
  }
2544
}
2545

2546

2547
/****************************************
2548
 *  INTERNALIZATION TO ETERM: TOPLEVEL  *
2549
 ***************************************/
2550

2551
static occ_t internalize_to_eterm(context_t *ctx, term_t t) {
103,128✔
2552
  term_table_t *terms;
2553
  term_t r;
2554
  uint32_t polarity;
2555
  int32_t code;
2556
  int32_t exception;
2557
  type_t tau;
2558
  occ_t u;
2559
  literal_t l;
2560
  thvar_t x;
2561

2562
  if (! context_has_egraph(ctx)) {
103,128✔
2563
    exception = uf_error_code(ctx, t);
×
2564
    goto abort;
×
2565
  }
2566

2567
  r = intern_tbl_get_root(&ctx->intern, t);
103,128✔
2568
  polarity = polarity_of(r);
103,128✔
2569
  r  = unsigned_term(r);
103,128✔
2570

2571
  /*
2572
   * r is a positive root in the internalization table
2573
   * polarity is 0 or 1
2574
   * if polarity is 0, then t is equal to r by substitution
2575
   * if polarity is 1, then t is equal to (not r)
2576
   */
2577
  if (intern_tbl_root_is_mapped(&ctx->intern, r)) {
103,128✔
2578
    /*
2579
     * r already internalized
2580
     */
2581
    code = intern_tbl_map_of_root(&ctx->intern, r);
64,835✔
2582
    u = translate_code_to_eterm(ctx, r, code);
64,835✔
2583
  } else {
2584
    /*
2585
     * Compute r's internalization:
2586
     * - if it's a boolean term, convert r to a literal l then
2587
     *   remap l to an egraph term
2588
     * - otherwise, recursively construct an egraph term and map it to r
2589
     */
2590
    terms = ctx->terms;
38,293✔
2591
    tau = type_of_root(ctx, r);
38,293✔
2592
    if (is_boolean_type(tau)) {
38,293✔
2593
      l = internalize_to_literal(ctx, r);
28✔
2594
      u = egraph_literal2occ(ctx->egraph, l);
28✔
2595
      intern_tbl_remap_root(&ctx->intern, r, occ2code(u));
28✔
2596
    } else {
2597
      /*
2598
       * r is not a boolean term
2599
       */
2600
      assert(polarity == 0);
2601

2602
      switch (term_kind(terms, r)) {
38,265✔
2603
      case CONSTANT_TERM:
1,161✔
2604
        u = pos_occ(make_egraph_constant(ctx, tau, constant_term_index(terms, r)));
1,161✔
2605
        break;
1,161✔
2606

2607
      case ARITH_CONSTANT:
337✔
2608
        u = map_arith_constant_to_eterm(ctx, rational_term_desc(terms, r));
337✔
2609
        break;
337✔
2610

2611
      case BV64_CONSTANT:
808✔
2612
        u = map_bvconst64_to_eterm(ctx, bvconst64_term_desc(terms, r));
808✔
2613
        break;
808✔
2614

2615
      case BV_CONSTANT:
18✔
2616
        u = map_bvconst_to_eterm(ctx, bvconst_term_desc(terms, r));
18✔
2617
        break;
18✔
2618

2619
      case VARIABLE:
×
2620
        exception = FREE_VARIABLE_IN_FORMULA;
×
2621
        goto abort;
×
2622

2623
      case UNINTERPRETED_TERM:
12,008✔
2624
        u = pos_occ(make_egraph_variable(ctx, tau));
12,008✔
2625
        //        add_type_constraints(ctx, u, tau);
2626
        break;
12,008✔
2627

2628
      case ARITH_FLOOR:
×
2629
        assert(is_integer_type(tau));
2630
        x = map_floor_to_arith(ctx, arith_floor_arg(terms, r));
×
2631
        u = translate_arithvar_to_eterm(ctx, x);
×
2632
        break;
×
2633

2634
      case ARITH_CEIL:
×
2635
        assert(is_integer_type(tau));
2636
        x = map_ceil_to_arith(ctx, arith_ceil_arg(terms, r));
×
2637
        u = translate_arithvar_to_eterm(ctx, x);
×
2638
        break;
×
2639

2640
      case ARITH_ABS:
×
2641
        x = map_abs_to_arith(ctx, arith_abs_arg(terms, r));
×
2642
        u = translate_arithvar_to_eterm(ctx, x);
×
2643
        break;
×
2644

2645
      case ITE_TERM:
2,168✔
2646
      case ITE_SPECIAL:
2647
        u = map_ite_to_eterm(ctx, ite_term_desc(terms, r), tau);
2,168✔
2648
        break;
2,168✔
2649

2650
      case APP_TERM:
6,529✔
2651
        u = map_apply_to_eterm(ctx, app_term_desc(terms, r), tau);
6,529✔
2652
        break;
6,529✔
2653

2654
      case ARITH_RDIV:
×
2655
        assert(is_arithmetic_type(tau));
2656
        x = map_rdiv_to_arith(ctx, arith_rdiv_term_desc(terms, r));
×
2657
        u = translate_arithvar_to_eterm(ctx, x);
×
2658
        break;
×
2659

2660
      case ARITH_IDIV:
×
2661
        assert(is_integer_type(tau));
2662
        x = map_idiv_to_arith(ctx, arith_idiv_term_desc(terms, r));
×
2663
        u = translate_arithvar_to_eterm(ctx, x); // (div t u) has type int
×
2664
        break;
×
2665

2666
      case ARITH_MOD:
2✔
2667
        x = map_mod_to_arith(ctx, arith_mod_term_desc(terms, r));
2✔
2668
        u = translate_arithvar_to_eterm(ctx, x);
2✔
2669
        break;
2✔
2670

2671
      case TUPLE_TERM:
190✔
2672
        u = map_tuple_to_eterm(ctx, tuple_term_desc(terms, r), tau);
190✔
2673
        break;
190✔
2674

2675
      case SELECT_TERM:
245✔
2676
        u = map_select_to_eterm(ctx, select_term_desc(terms, r), tau);
245✔
2677
        break;
245✔
2678

2679
      case UPDATE_TERM:
11,450✔
2680
        u = map_update_to_eterm(ctx, update_term_desc(terms, r), tau);
11,450✔
2681
        break;
11,450✔
2682

2683
      case LAMBDA_TERM:
1✔
2684
        // not ready for lambda terms yet:
2685
        exception = LAMBDAS_NOT_SUPPORTED;
1✔
2686
        goto abort;
1✔
2687

2688
      case BV_ARRAY:
1,711✔
2689
        x = map_bvarray_to_bv(ctx, bvarray_term_desc(terms, r));
1,711✔
2690
        u = translate_thvar_to_eterm(ctx, x, tau);
1,711✔
2691
        break;
1,711✔
2692

2693
      case BV_DIV:
1✔
2694
        x = map_bvdiv_to_bv(ctx, bvdiv_term_desc(terms, r));
1✔
2695
        u = translate_thvar_to_eterm(ctx, x, tau);
1✔
2696
        break;
1✔
2697

2698
      case BV_REM:
2✔
2699
        x = map_bvrem_to_bv(ctx, bvrem_term_desc(terms, r));
2✔
2700
        u = translate_thvar_to_eterm(ctx, x, tau);
2✔
2701
        break;
2✔
2702

2703
      case BV_SDIV:
1✔
2704
        x = map_bvsdiv_to_bv(ctx, bvsdiv_term_desc(terms, r));
1✔
2705
        u = translate_thvar_to_eterm(ctx, x, tau);
1✔
2706
        break;
1✔
2707

2708
      case BV_SREM:
1✔
2709
        x = map_bvsrem_to_bv(ctx, bvsrem_term_desc(terms, r));
1✔
2710
        u = translate_thvar_to_eterm(ctx, x, tau);
1✔
2711
        break;
1✔
2712

2713
      case BV_SMOD:
×
2714
        x = map_bvsmod_to_bv(ctx, bvsmod_term_desc(terms, r));
×
2715
        u = translate_thvar_to_eterm(ctx, x, tau);
×
2716
        break;
×
2717

2718
      case BV_SHL:
×
2719
        x = map_bvshl_to_bv(ctx, bvshl_term_desc(terms, r));
×
2720
        u = translate_thvar_to_eterm(ctx, x, tau);
×
2721
        break;
×
2722

2723
      case BV_LSHR:
1✔
2724
        x = map_bvlshr_to_bv(ctx, bvlshr_term_desc(terms, r));
1✔
2725
        u = translate_thvar_to_eterm(ctx, x, tau);
1✔
2726
        break;
1✔
2727

2728
      case BV_ASHR:
1✔
2729
        x = map_bvashr_to_bv(ctx, bvashr_term_desc(terms, r));
1✔
2730
        u = translate_thvar_to_eterm(ctx, x, tau);
1✔
2731
        break;
1✔
2732

2733
      case POWER_PRODUCT:
1✔
2734
        if (is_arithmetic_type(tau)) {
1✔
2735
          x = map_pprod_to_arith(ctx, pprod_term_desc(terms, r));
×
2736
        } else {
2737
          assert(is_bv_type(ctx->types, tau));
2738
          x = map_pprod_to_bv(ctx, pprod_term_desc(terms, r));
1✔
2739
        }
2740
        u = translate_thvar_to_eterm(ctx, x, tau);
1✔
2741
        break;
1✔
2742

2743
      case ARITH_POLY:
619✔
2744
        x = map_poly_to_arith(ctx, poly_term_desc(terms, r));
619✔
2745
        u = translate_thvar_to_eterm(ctx, x, tau);
619✔
2746
        break;
619✔
2747

2748
      case BV64_POLY:
1,008✔
2749
        x = map_bvpoly64_to_bv(ctx, bvpoly64_term_desc(terms, r));
1,008✔
2750
        u = translate_thvar_to_eterm(ctx, x, tau);
1,008✔
2751
        break;
1,008✔
2752

2753
      case BV_POLY:
2✔
2754
        x = map_bvpoly_to_bv(ctx, bvpoly_term_desc(terms, r));
2✔
2755
        u = translate_thvar_to_eterm(ctx, x, tau);
2✔
2756
        break;
2✔
2757

2758
      default:
×
2759
        exception = INTERNAL_ERROR;
×
2760
        goto abort;
×
2761
      }
2762

2763
      // store the mapping r --> u
2764
      intern_tbl_map_root(&ctx->intern, r, occ2code(u));
38,264✔
2765
    }
2766
  }
2767

2768
  // fix the polarity
2769
  return u ^ polarity;
103,127✔
2770

2771
 abort:
1✔
2772
  longjmp(ctx->env, exception);
1✔
2773
}
2774

2775

2776

2777

2778
/****************************************
2779
 *  CONVERSION TO ARITHMETIC VARIABLES  *
2780
 ***************************************/
2781

2782
/*
2783
 * Translate internalization code x to an arithmetic variable
2784
 * - if the code is for an egraph term u, then we return the
2785
 *   theory variable attached to u in the egraph.
2786
 * - otherwise, x must be the code of an arithmetic variable v,
2787
 *   we return v.
2788
 */
2789
static thvar_t translate_code_to_arith(context_t *ctx, int32_t x) {
69,874✔
2790
  eterm_t u;
2791
  thvar_t v;
2792

2793
  assert(code_is_valid(x));
2794

2795
  if (code_is_eterm(x)) {
69,874✔
2796
    u = code2eterm(x);
6,521✔
2797
    assert(ctx->egraph != NULL && egraph_term_is_arith(ctx->egraph, u));
2798
    v = egraph_term_base_thvar(ctx->egraph, u);
6,521✔
2799
  } else {
2800
    v = code2thvar(x);
63,353✔
2801
  }
2802

2803
  assert(v != null_thvar);
2804
  return v;
69,874✔
2805
}
2806

2807

2808
static thvar_t internalize_to_arith(context_t *ctx, term_t t) {
80,745✔
2809
  term_table_t *terms;
2810
  int32_t exception;
2811
  int32_t code;
2812
  term_t r;
2813
  occ_t u;
2814
  thvar_t x;
2815

2816
  assert(is_arithmetic_term(ctx->terms, t));
2817

2818
  if (! context_has_arith_solver(ctx)) {
80,745✔
2819
    exception = ARITH_NOT_SUPPORTED;
×
2820
    goto abort;
×
2821
  }
2822

2823
  /*
2824
   * Apply term substitution: t --> r
2825
   */
2826
  r = intern_tbl_get_root(&ctx->intern, t);
80,745✔
2827
  if (intern_tbl_root_is_mapped(&ctx->intern, r)) {
80,745✔
2828
    /*
2829
     * r already internalized
2830
     */
2831
    code = intern_tbl_map_of_root(&ctx->intern, r);
69,715✔
2832
    x = translate_code_to_arith(ctx, code);
69,715✔
2833

2834
  } else {
2835
    /*
2836
     * Compute the internalization
2837
     */
2838
    terms = ctx->terms;
11,030✔
2839

2840
    switch (term_kind(terms, r)) {
11,030✔
2841
    case ARITH_CONSTANT:
1,268✔
2842
      x = ctx->arith.create_const(ctx->arith_solver, rational_term_desc(terms, r));
1,268✔
2843
      intern_tbl_map_root(&ctx->intern, r, thvar2code(x));
1,268✔
2844
      break;
1,268✔
2845

2846
    case UNINTERPRETED_TERM:
5,362✔
2847
      x = ctx->arith.create_var(ctx->arith_solver, is_integer_root(ctx, r));
5,362✔
2848
      intern_tbl_map_root(&ctx->intern, r, thvar2code(x));
5,362✔
2849
      //      printf("mapping: %s --> i!%d\n", term_name(ctx->terms, r), (int) x);
2850
      //      fflush(stdout);
2851
      break;
5,362✔
2852

2853
    case ARITH_FLOOR:
×
2854
      x = map_floor_to_arith(ctx, arith_floor_arg(terms, r));
×
2855
      intern_tbl_map_root(&ctx->intern, r, thvar2code(x));
×
2856
      break;
×
2857

2858
    case ARITH_CEIL:
×
2859
      x = map_ceil_to_arith(ctx, arith_ceil_arg(terms, r));
×
2860
      intern_tbl_map_root(&ctx->intern, r, thvar2code(x));
×
2861
      break;
×
2862

2863
    case ARITH_ABS:
2✔
2864
      x = map_abs_to_arith(ctx, arith_abs_arg(terms, r));
2✔
2865
      intern_tbl_map_root(&ctx->intern, r, thvar2code(x));
2✔
2866
      break;
2✔
2867

2868
    case ITE_TERM:
2,230✔
2869
      x = map_ite_to_arith(ctx, ite_term_desc(terms, r), is_integer_root(ctx, r));
2,230✔
2870
      intern_tbl_map_root(&ctx->intern, r, thvar2code(x));
2,230✔
2871
      break;
2,230✔
2872

2873
    case ITE_SPECIAL:
792✔
2874
      x = map_ite_to_arith(ctx, ite_term_desc(terms, r), is_integer_root(ctx, r));
792✔
2875
      intern_tbl_map_root(&ctx->intern, r, thvar2code(x));
792✔
2876
      if (context_ite_bounds_enabled(ctx)) {
792✔
2877
        assert_ite_bounds(ctx, r, x);
792✔
2878
      }
2879
      break;
792✔
2880

2881
    case APP_TERM:
682✔
2882
      u = map_apply_to_eterm(ctx, app_term_desc(terms, r), type_of_root(ctx, r));
682✔
2883
      assert(egraph_term_is_arith(ctx->egraph, term_of_occ(u)));
2884
      intern_tbl_map_root(&ctx->intern, r, occ2code(u));
682✔
2885
      x = egraph_term_base_thvar(ctx->egraph, term_of_occ(u));
682✔
2886
      assert(x != null_thvar);
2887
      break;
682✔
2888

2889
    case ARITH_RDIV:
2✔
2890
      x = map_rdiv_to_arith(ctx, arith_rdiv_term_desc(terms, r));
2✔
2891
      intern_tbl_map_root(&ctx->intern, r, thvar2code(x));
×
2892
      break;
×
2893

2894
    case ARITH_IDIV:
5✔
2895
      x = map_idiv_to_arith(ctx, arith_idiv_term_desc(terms, r));
5✔
2896
      intern_tbl_map_root(&ctx->intern, r, thvar2code(x));
5✔
2897
      break;
5✔
2898

2899
    case ARITH_MOD:
9✔
2900
      x = map_mod_to_arith(ctx, arith_mod_term_desc(terms, r));
9✔
2901
      intern_tbl_map_root(&ctx->intern, r, thvar2code(x));
9✔
2902
      break;
9✔
2903

2904
    case SELECT_TERM:
×
2905
      u = map_select_to_eterm(ctx, select_term_desc(terms, r), type_of_root(ctx, r));
×
2906
      assert(egraph_term_is_arith(ctx->egraph, term_of_occ(u)));
2907
      intern_tbl_map_root(&ctx->intern, r, occ2code(u));
×
2908
      x = egraph_term_base_thvar(ctx->egraph, term_of_occ(u));
×
2909
      assert(x != null_thvar);
2910
      break;
×
2911

2912
    case POWER_PRODUCT:
×
2913
      x = map_pprod_to_arith(ctx, pprod_term_desc(terms, r));
×
2914
      intern_tbl_map_root(&ctx->intern, r, thvar2code(x));
×
2915
      break;
×
2916

2917
    case ARITH_POLY:
678✔
2918
      x = map_poly_to_arith(ctx, poly_term_desc(terms, r));
678✔
2919
      intern_tbl_map_root(&ctx->intern, r, thvar2code(x));
678✔
2920
      break;
678✔
2921

2922
    case VARIABLE:
×
2923
      exception = FREE_VARIABLE_IN_FORMULA;
×
2924
      goto abort;
×
2925

2926
    default:
×
2927
      exception = INTERNAL_ERROR;
×
2928
      goto abort;
×
2929
    }
2930

2931
  }
2932

2933
  return x;
80,743✔
2934

2935
 abort:
×
2936
  longjmp(ctx->env, exception);
×
2937
}
2938

2939

2940

2941
/***************************************
2942
 *  CONVERSION TO BITVECTOR VARIABLES  *
2943
 **************************************/
2944

2945
/*
2946
 * Translate internalization code x to a bitvector variable
2947
 * - if x is for an egraph term u, then we return the theory variable
2948
 *   attached to u in the egraph.
2949
 * - otherwise, x must be the code of a bitvector variable v, so we return v.
2950
 */
2951
static thvar_t translate_code_to_bv(context_t *ctx, int32_t x) {
176,663✔
2952
  eterm_t u;
2953
  thvar_t v;
2954

2955
  assert(code_is_valid(x));
2956

2957
  if (code_is_eterm(x)) {
176,663✔
2958
    u = code2eterm(x);
29,162✔
2959
    assert(ctx->egraph != NULL && egraph_term_is_bv(ctx->egraph, u));
2960
    v = egraph_term_base_thvar(ctx->egraph, u);
29,162✔
2961
  } else {
2962
    v = code2thvar(x);
147,501✔
2963
  }
2964

2965
  assert(v != null_thvar);
2966

2967
  return v;
176,663✔
2968
}
2969

2970
/*
2971
 * Place holders for now
2972
 */
2973
static thvar_t internalize_to_bv(context_t *ctx, term_t t) {
233,661✔
2974
  term_table_t *terms;
2975
  int32_t exception;
2976
  int32_t code;
2977
  term_t r;
2978
  occ_t u;
2979
  thvar_t x;
2980

2981
  assert(is_bitvector_term(ctx->terms, t));
2982

2983
  if (! context_has_bv_solver(ctx)) {
233,661✔
2984
    exception = BV_NOT_SUPPORTED;
×
2985
    goto abort;
×
2986
  }
2987

2988
  /*
2989
   * Apply the term substitution: t --> r
2990
   */
2991
  r = intern_tbl_get_root(&ctx->intern, t);
233,661✔
2992
  if (intern_tbl_root_is_mapped(&ctx->intern, r)) {
233,661✔
2993
    // r is already internalized
2994
    code = intern_tbl_map_of_root(&ctx->intern, r);
176,663✔
2995
    x = translate_code_to_bv(ctx, code);
176,663✔
2996
  } else {
2997
    // compute r's internalization
2998
    terms = ctx->terms;
56,998✔
2999

3000
    switch (term_kind(terms, r)) {
56,998✔
3001
    case BV64_CONSTANT:
5,281✔
3002
      x = ctx->bv.create_const64(ctx->bv_solver, bvconst64_term_desc(terms, r));
5,281✔
3003
      intern_tbl_map_root(&ctx->intern, r, thvar2code(x));
5,281✔
3004
      break;
5,281✔
3005

3006
    case BV_CONSTANT:
439✔
3007
      x = ctx->bv.create_const(ctx->bv_solver, bvconst_term_desc(terms, r));
439✔
3008
      intern_tbl_map_root(&ctx->intern, r, thvar2code(x));
439✔
3009
      break;
439✔
3010

3011
    case UNINTERPRETED_TERM:
8,927✔
3012
      x = ctx->bv.create_var(ctx->bv_solver, term_bitsize(terms, r));
8,927✔
3013
      intern_tbl_map_root(&ctx->intern, r, thvar2code(x));
8,927✔
3014
      break;
8,927✔
3015

3016
    case ITE_TERM:
20,779✔
3017
    case ITE_SPECIAL:
3018
      x = map_ite_to_bv(ctx, ite_term_desc(terms, r));
20,779✔
3019
      intern_tbl_map_root(&ctx->intern, r, thvar2code(x));
20,779✔
3020
      break;
20,779✔
3021

3022
    case APP_TERM:
2,450✔
3023
      u = map_apply_to_eterm(ctx, app_term_desc(terms, r), type_of_root(ctx, r));
2,450✔
3024
      assert(egraph_term_is_bv(ctx->egraph, term_of_occ(u)));
3025
      intern_tbl_map_root(&ctx->intern, r, occ2code(u));
2,450✔
3026
      x = egraph_term_base_thvar(ctx->egraph, term_of_occ(u));
2,450✔
3027
      assert(x != null_thvar);
3028
      break;
2,450✔
3029

3030
    case BV_ARRAY:
8,770✔
3031
      x = map_bvarray_to_bv(ctx, bvarray_term_desc(terms, r));
8,770✔
3032
      intern_tbl_map_root(&ctx->intern, r, thvar2code(x));
8,770✔
3033
      break;
8,770✔
3034

3035
    case BV_DIV:
73✔
3036
      x = map_bvdiv_to_bv(ctx, bvdiv_term_desc(terms, r));
73✔
3037
      intern_tbl_map_root(&ctx->intern, r, thvar2code(x));
73✔
3038
      break;
73✔
3039

3040
    case BV_REM:
80✔
3041
      x = map_bvrem_to_bv(ctx, bvrem_term_desc(terms, r));
80✔
3042
      intern_tbl_map_root(&ctx->intern, r, thvar2code(x));
80✔
3043
      break;
80✔
3044

3045
    case BV_SDIV:
86✔
3046
      x = map_bvsdiv_to_bv(ctx, bvsdiv_term_desc(terms, r));
86✔
3047
      intern_tbl_map_root(&ctx->intern, r, thvar2code(x));
86✔
3048
      break;
86✔
3049

3050
    case BV_SREM:
76✔
3051
      x = map_bvsrem_to_bv(ctx, bvsrem_term_desc(terms, r));
76✔
3052
      intern_tbl_map_root(&ctx->intern, r, thvar2code(x));
76✔
3053
      break;
76✔
3054

3055
    case BV_SMOD:
×
3056
      x = map_bvsmod_to_bv(ctx, bvsmod_term_desc(terms, r));
×
3057
      intern_tbl_map_root(&ctx->intern, r, thvar2code(x));
×
3058
      break;
×
3059

3060
    case BV_SHL:
71✔
3061
      x = map_bvshl_to_bv(ctx, bvshl_term_desc(terms, r));
71✔
3062
      intern_tbl_map_root(&ctx->intern, r, thvar2code(x));
71✔
3063
      break;
71✔
3064

3065
    case BV_LSHR:
203✔
3066
      x = map_bvlshr_to_bv(ctx, bvlshr_term_desc(terms, r));
203✔
3067
      intern_tbl_map_root(&ctx->intern, r, thvar2code(x));
203✔
3068
      break;
203✔
3069

3070
    case BV_ASHR:
61✔
3071
      x = map_bvashr_to_bv(ctx, bvashr_term_desc(terms, r));
61✔
3072
      intern_tbl_map_root(&ctx->intern, r, thvar2code(x));
61✔
3073
      break;
61✔
3074

3075
    case SELECT_TERM:
×
3076
      u = map_select_to_eterm(ctx, select_term_desc(terms, r), type_of_root(ctx, r));
×
3077
      assert(egraph_term_is_bv(ctx->egraph, term_of_occ(u)));
3078
      intern_tbl_map_root(&ctx->intern, r, occ2code(u));
×
3079
      x = egraph_term_base_thvar(ctx->egraph, term_of_occ(u));
×
3080
      assert(x != null_thvar);
3081
      break;
×
3082

3083
    case POWER_PRODUCT:
564✔
3084
      x = map_pprod_to_bv(ctx, pprod_term_desc(terms, r));
564✔
3085
      intern_tbl_map_root(&ctx->intern, r, thvar2code(x));
564✔
3086
      break;
564✔
3087

3088
    case BV64_POLY:
8,533✔
3089
      x = map_bvpoly64_to_bv(ctx, bvpoly64_term_desc(terms, r));
8,533✔
3090
      intern_tbl_map_root(&ctx->intern, r, thvar2code(x));
8,533✔
3091
      break;
8,533✔
3092

3093
    case BV_POLY:
605✔
3094
      x = map_bvpoly_to_bv(ctx, bvpoly_term_desc(terms, r));
605✔
3095
      intern_tbl_map_root(&ctx->intern, r, thvar2code(x));
605✔
3096
      break;
605✔
3097

3098
    case VARIABLE:
×
3099
      exception = FREE_VARIABLE_IN_FORMULA;
×
3100
      goto abort;
×
3101

3102
    default:
×
3103
      exception = INTERNAL_ERROR;
×
3104
      goto abort;
×
3105
    }
3106
  }
3107

3108
  return x;
233,661✔
3109

3110
 abort:
×
3111
  longjmp(ctx->env, exception);
×
3112
}
3113

3114

3115

3116

3117

3118

3119
/****************************
3120
 *  CONVERSION TO LITERALS  *
3121
 ***************************/
3122

3123
/*
3124
 * Translate an internalization code x to a literal
3125
 * - if x is the code of an egraph occurrence u, we return the
3126
 *   theory variable for u in the egraph
3127
 * - otherwise, x should be the code of a literal l in the core
3128
 */
3129
static literal_t translate_code_to_literal(context_t *ctx, int32_t x) {
1,296,272✔
3130
  occ_t u;
3131
  literal_t l;
3132

3133
  assert(code_is_valid(x));
3134
  if (code_is_eterm(x)) {
1,296,272✔
3135
    u = code2occ(x);
126,769✔
3136
    if (term_of_occ(u) == true_eterm) {
126,769✔
3137
      l = mk_lit(const_bvar, polarity_of(u));
126,769✔
3138

3139
      assert((u == true_occ && l == true_literal) ||
3140
             (u == false_occ && l == false_literal));
3141
    } else {
3142
      assert(ctx->egraph != NULL);
3143
      l = egraph_occ2literal(ctx->egraph, u);
×
3144
    }
3145
  } else {
3146
    l = code2literal(x);
1,169,503✔
3147
  }
3148

3149
  return l;
1,296,272✔
3150
}
3151

3152
static literal_t internalize_to_literal(context_t *ctx, term_t t) {
1,570,383✔
3153
  term_table_t *terms;
3154
  int32_t code;
3155
  uint32_t polarity;
3156
  term_t r;
3157
  literal_t l;
3158
  occ_t u;
3159

3160
  assert(is_boolean_term(ctx->terms, t));
3161

3162
  r = intern_tbl_get_root(&ctx->intern, t);
1,570,383✔
3163
  polarity = polarity_of(r);
1,570,383✔
3164
  r = unsigned_term(r);
1,570,383✔
3165

3166
  /*
3167
   * At this point:
3168
   * 1) r is a positive root in the internalization table
3169
   * 2) polarity is 1 or 0
3170
   * 3) if polarity is 0, then t is equal to r by substitution
3171
   *    if polarity is 1, then t is equal to (not r)
3172
   *
3173
   * We get l := internalization of r
3174
   * then return l or (not l) depending on polarity.
3175
   */
3176

3177
  if (intern_tbl_root_is_mapped(&ctx->intern, r)) {
1,570,383✔
3178
    /*
3179
     * r already internalized
3180
     */
3181
    code = intern_tbl_map_of_root(&ctx->intern, r);
1,296,272✔
3182
    l = translate_code_to_literal(ctx, code);
1,296,272✔
3183

3184
  } else {
3185
    /*
3186
     * Recursively compute r's internalization
3187
     */
3188
    terms = ctx->terms;
274,111✔
3189
    switch (term_kind(terms, r)) {
274,111✔
3190
    case CONSTANT_TERM:
×
3191
      assert(r == true_term);
3192
      l = true_literal;
×
3193
      break;
×
3194

3195
    case VARIABLE:
×
3196
      longjmp(ctx->env, FREE_VARIABLE_IN_FORMULA);
×
3197
      break;
3198

3199
    case UNINTERPRETED_TERM:
4,785✔
3200
      l = pos_lit(create_boolean_variable(ctx->core));
4,785✔
3201
      break;
4,785✔
3202

3203
    case ITE_TERM:
1,863✔
3204
    case ITE_SPECIAL:
3205
      l = map_ite_to_literal(ctx, ite_term_desc(terms, r));
1,863✔
3206
      break;
1,863✔
3207

3208
    case EQ_TERM:
14,505✔
3209
      l = map_eq_to_literal(ctx, eq_term_desc(terms, r));
14,505✔
3210
      break;
14,505✔
3211

3212
    case OR_TERM:
79,564✔
3213
      l = map_or_to_literal(ctx, or_term_desc(terms, r));
79,564✔
3214
      break;
79,564✔
3215

3216
    case XOR_TERM:
×
3217
      l = map_xor_to_literal(ctx, xor_term_desc(terms, r));
×
3218
      break;
×
3219

3220
    case ARITH_IS_INT_ATOM:
×
3221
      l = map_arith_is_int_to_literal(ctx, arith_is_int_arg(terms, r));
×
3222
      break;
×
3223

3224
    case ARITH_EQ_ATOM:
2,229✔
3225
      l = map_arith_eq_to_literal(ctx, arith_eq_arg(terms, r));
2,229✔
3226
      break;
2,229✔
3227

3228
    case ARITH_GE_ATOM:
13,837✔
3229
      l = map_arith_geq_to_literal(ctx, arith_ge_arg(terms, r));
13,837✔
3230
      break;
13,837✔
3231

3232
    case ARITH_BINEQ_ATOM:
8,947✔
3233
      l = map_arith_bineq_to_literal(ctx, arith_bineq_atom_desc(terms, r));
8,947✔
3234
      break;
8,947✔
3235

3236
    case ARITH_DIVIDES_ATOM:
8✔
3237
      l = map_arith_divides_to_literal(ctx, arith_divides_atom_desc(terms, r));
8✔
3238
      break;
8✔
3239

3240
    case APP_TERM:
4,358✔
3241
      l = map_apply_to_literal(ctx, app_term_desc(terms, r));
4,358✔
3242
      break;
4,358✔
3243

3244
    case SELECT_TERM:
×
3245
      u = map_select_to_eterm(ctx, select_term_desc(terms, r), bool_type(ctx->types));
×
3246
      assert(egraph_term_is_bool(ctx->egraph, term_of_occ(u)));
3247
      intern_tbl_map_root(&ctx->intern, r, occ2code(u));
×
3248
      l = egraph_occ2literal(ctx->egraph, u);
×
3249
      // we don't want to map r to l here
3250
      goto done;
×
3251

3252
    case DISTINCT_TERM:
9✔
3253
      l = map_distinct_to_literal(ctx, distinct_term_desc(terms, r));
9✔
3254
      break;
9✔
3255

3256
    case FORALL_TERM:
×
3257
      if (context_in_strict_mode(ctx)) {
×
3258
        longjmp(ctx->env, QUANTIFIERS_NOT_SUPPORTED);
×
3259
      }
3260
      // lax mode: turn forall into a proposition
3261
      l = pos_lit(create_boolean_variable(ctx->core));
×
3262
      break;
×
3263

3264
    case BIT_TERM:
135,402✔
3265
      l = map_bit_select_to_literal(ctx, bit_term_desc(terms, r));
135,402✔
3266
      break;
135,402✔
3267

3268
    case BV_EQ_ATOM:
3,999✔
3269
      l = map_bveq_to_literal(ctx, bveq_atom_desc(terms, r));
3,999✔
3270
      break;
3,999✔
3271

3272
    case BV_GE_ATOM:
2,174✔
3273
      l = map_bvge_to_literal(ctx, bvge_atom_desc(terms, r));
2,174✔
3274
      break;
2,174✔
3275

3276
    case BV_SGE_ATOM:
2,431✔
3277
      l = map_bvsge_to_literal(ctx, bvsge_atom_desc(terms, r));
2,431✔
3278
      break;
2,431✔
3279

3280
    default:
×
3281
      longjmp(ctx->env, INTERNAL_ERROR);
×
3282
      break;
3283
    }
3284

3285
    // map r to l in the internalization table
3286
    intern_tbl_map_root(&ctx->intern, r, literal2code(l));
274,111✔
3287
  }
3288

3289
 done:
1,570,383✔
3290
  return l ^ polarity;
1,570,383✔
3291
}
3292

3293

3294

3295
/******************************************************
3296
 *  TOP-LEVEL ASSERTIONS: TERMS ALREADY INTERNALIZED  *
3297
 *****************************************************/
3298

3299
/*
3300
 * Assert (x == tt) for an internalization code x
3301
 */
3302
static void assert_internalization_code(context_t *ctx, int32_t x, bool tt) {
501✔
3303
  occ_t g;
3304
  literal_t l;
3305

3306
  assert(code_is_valid(x));
3307

3308
  if (code_is_eterm(x)) {
501✔
3309
    // normalize to assertion (g == true)
3310
    g = code2occ(x);
10✔
3311
    if (! tt) g = opposite_occ(g);
10✔
3312

3313
    // We must deal with 'true_occ/false_occ' separately
3314
    // since they may be used even if there's no actual egraph.
3315
    if (g == false_occ) {
10✔
3316
      longjmp(ctx->env, TRIVIALLY_UNSAT);
4✔
3317
    } else if (g != true_occ) {
6✔
3318
      assert(ctx->egraph != NULL);
3319
      if (!context_quant_enabled(ctx) || egraph_is_at_base_level(ctx->egraph)) {
×
3320
        egraph_assert_axiom(ctx->egraph, g);
×
3321
      } else {
3322
        l = egraph_make_eq(ctx->egraph, g, true_occ);
×
3323
        add_unit_clause(ctx->core, l);
×
3324
      }
3325
    }
3326
  } else {
3327
    l = code2literal(x);
491✔
3328
    if (! tt) l = not(l);
491✔
3329
    add_unit_clause(ctx->core, l);
491✔
3330
  }
3331
}
497✔
3332

3333
/*
3334
 * Assert t == true where t is a term that's already mapped
3335
 * either to a literal or to an egraph occurrence.
3336
 * - t must be a root in the internalization table
3337
 */
3338
static void assert_toplevel_intern(context_t *ctx, term_t t) {
330✔
3339
  int32_t code;
3340
  bool tt;
3341

3342
  assert(is_boolean_term(ctx->terms, t) &&
3343
         intern_tbl_is_root(&ctx->intern, t) &&
3344
         intern_tbl_root_is_mapped(&ctx->intern, t));
3345

3346
  tt = is_pos_term(t);
330✔
3347
  t = unsigned_term(t);
330✔
3348
  code = intern_tbl_map_of_root(&ctx->intern, t);
330✔
3349

3350
  assert_internalization_code(ctx, code, tt);
330✔
3351
}
330✔
3352

3353

3354

3355

3356

3357

3358

3359
/********************************
3360
 *   ARITHMETIC SUBSTITUTIONS   *
3361
 *******************************/
3362

3363
/*
3364
 * TODO: improve this in the integer case:
3365
 * - all_int is based on p's type in the term table and does
3366
 *   not take the context's substitutions into account.
3367
 * - integral_poly_after_div requires all coefficients
3368
 *   to be integer. This could be generalized to polynomials
3369
 *   with integer variables and rational coefficients.
3370
 */
3371

3372
/*
3373
 * Check whether term t can be eliminated by an arithmetic substitution
3374
 * - t's root must be uninterpreted and not internalized yet
3375
 */
3376
static bool is_elimination_candidate(context_t *ctx, term_t t) {
2,742✔
3377
  term_t r;
3378

3379
  r = intern_tbl_get_root(&ctx->intern, t);
2,742✔
3380
  return intern_tbl_root_is_free(&ctx->intern, r);
2,742✔
3381
}
3382

3383

3384
/*
3385
 * Replace every variable of t by the root of t in the internalization table
3386
 * - the result is stored in buffer
3387
 */
3388
static void apply_renaming_to_poly(context_t *ctx, polynomial_t *p,  poly_buffer_t *buffer) {
544✔
3389
  uint32_t i, n;
3390
  term_t t;
3391

3392
  reset_poly_buffer(buffer);
544✔
3393

3394
  assert(poly_buffer_is_zero(buffer));
3395

3396
  n = p->nterms;
544✔
3397
  for (i=0; i<n; i++) {
2,599✔
3398
    t = p->mono[i].var;
2,055✔
3399
    if (t == const_idx) {
2,055✔
3400
      poly_buffer_add_const(buffer, &p->mono[i].coeff);
180✔
3401
    } else {
3402
      // replace t by its root
3403
      t = intern_tbl_get_root(&ctx->intern, t);
1,875✔
3404
      poly_buffer_addmul_term(ctx->terms, buffer, t, &p->mono[i].coeff);
1,875✔
3405
    }
3406
  }
3407

3408
  normalize_poly_buffer(buffer);
544✔
3409
}
544✔
3410

3411

3412
/*
3413
 * Auxiliary function: check whether p/a is an integral polynomial
3414
 * assuming all variables and coefficients of p are integer.
3415
 * - check whether all coefficients are multiple of a
3416
 * - a must be non-zero
3417
 */
3418
static bool integralpoly_after_div(poly_buffer_t *buffer, rational_t *a) {
318✔
3419
  uint32_t i, n;
3420

3421
  if (q_is_one(a) || q_is_minus_one(a)) {
318✔
3422
    return true;
265✔
3423
  }
3424

3425
  n = buffer->nterms;
53✔
3426
  for (i=0; i<n; i++) {
64✔
3427
    if (! q_divides(a, &buffer->mono[i].coeff)) return false;
63✔
3428
  }
3429
  return true;
1✔
3430
}
3431

3432

3433
/*
3434
 * Check whether a top-level assertion (p == 0) can be
3435
 * rewritten (t == q) where t is not internalized yet.
3436
 * - all_int is true if p is an integer polynomial (i.e.,
3437
 *   all coefficients and all terms of p are integer).
3438
 * - p = input polynomial
3439
 * - return t or null_term if no adequate t is found
3440
 */
3441
static term_t try_poly_substitution(context_t *ctx, poly_buffer_t *buffer, bool all_int) {
544✔
3442
  uint32_t i, n;
3443
  term_t t;
3444

3445
  // check for a free variable in buffer
3446
  n = buffer->nterms;
544✔
3447
  for (i=0; i<n; i++) {
1,128✔
3448
    t = buffer->mono[i].var;
1,049✔
3449
    if (t != const_idx && is_elimination_candidate(ctx, t)) {
1,049✔
3450
      if (in_real_class(ctx, t) ||
518✔
3451
          (all_int && integralpoly_after_div(buffer, &buffer->mono[i].coeff))) {
318✔
3452
        // t is candidate for elimination
3453
        return t;
465✔
3454
      }
3455
    }
3456
  }
3457

3458
  return NULL_TERM;
79✔
3459
}
3460

3461

3462
/*
3463
 * Build polynomial - p/a + x in the context's aux_poly buffer
3464
 * where a = coefficient of x in p
3465
 * - x must occur in p
3466
 */
3467
static polynomial_t *build_poly_substitution(context_t *ctx, poly_buffer_t *buffer, term_t x) {
465✔
3468
  polynomial_t *q;
3469
  monomial_t *mono;
3470
  uint32_t i, n;
3471
  term_t y;
3472
  rational_t *a;
3473

3474
  n = buffer->nterms;
465✔
3475

3476
  // first get coefficient of x in buffer
3477
  a = NULL; // otherwise GCC complains
465✔
3478
  for (i=0; i<n; i++) {
2,283✔
3479
    y = buffer->mono[i].var;
1,818✔
3480
    if (y == x) {
1,818✔
3481
      a = &buffer->mono[i].coeff;
465✔
3482
    }
3483
  }
3484
  assert(a != NULL && n > 0);
3485

3486
  q = context_get_aux_poly(ctx, n);
465✔
3487
  q->nterms = n-1;
465✔
3488
  mono = q->mono;
465✔
3489

3490
  // compute - buffer/a (but skip monomial a.x)
3491
  for (i=0; i<n; i++) {
2,283✔
3492
    y = buffer->mono[i].var;
1,818✔
3493
    if (y != x) {
1,818✔
3494
      mono->var = y;
1,353✔
3495
      q_set_neg(&mono->coeff, &buffer->mono[i].coeff);
1,353✔
3496
      q_div(&mono->coeff, a);
1,353✔
3497
      mono ++;
1,353✔
3498
    }
3499
  }
3500

3501
  // end marker
3502
  mono->var = max_idx;
465✔
3503

3504
  return q;
465✔
3505
}
3506

3507

3508

3509
/*
3510
 * Try to eliminate a toplevel equality (p == 0) by variable substitution:
3511
 * - i.e., try to rewrite p == 0 into (x - q) == 0 where x is a free variable
3512
 *   then store the substitution x --> q in the internalization table.
3513
 * - all_int is true if p is an integer polynomial (i.e., all variables and all
3514
 *   coefficients of p are integer)
3515
 *
3516
 * - return true if the elimination succeeds
3517
 * - return false otherwise
3518
 */
3519
static bool try_arithvar_elim(context_t *ctx, polynomial_t *p, bool all_int) {
544✔
3520
  poly_buffer_t *buffer;
3521
  polynomial_t *q;
3522
  uint32_t i, n;
3523
  term_t t, u, r;
3524
  thvar_t x;
3525

3526
  /*
3527
   * First pass: internalize every term of p that's not a variable
3528
   * - we do that first to avoid circular substitutions (occurs-check)
3529
   */
3530
  n = p->nterms;
544✔
3531
  for (i=0; i<n; i++) {
2,599✔
3532
    t = p->mono[i].var;
2,055✔
3533
    if (t != const_idx && ! is_elimination_candidate(ctx, t)) {
2,055✔
3534
      (void) internalize_to_arith(ctx, t);
599✔
3535
    }
3536
  }
3537

3538

3539
  /*
3540
   * Apply variable renaming: this is to avoid circularities
3541
   * if p is of the form ... + a x + ... + b y + ...
3542
   * where both x and y are variables in the same class (i.e.,
3543
   * both are elimination candidates).
3544
   */
3545
  buffer = context_get_poly_buffer(ctx);
544✔
3546
  apply_renaming_to_poly(ctx, p, buffer);
544✔
3547

3548
  /*
3549
   * Search for a variable to substitute
3550
   */
3551
  u = try_poly_substitution(ctx, buffer, all_int);
544✔
3552
  if (u == NULL_TERM) {
544✔
3553
    return false; // no substitution found
79✔
3554
  }
3555

3556
  /*
3557
   * buffer is of the form a.u + p0, we rewrite (buffer == 0) to (u == q)
3558
   * where q = -1/a * p0
3559
   */
3560
  q = build_poly_substitution(ctx, buffer, u); // q is in ctx->aux_poly
465✔
3561

3562
  // convert q to a theory variable in the arithmetic solver
3563
  x = map_poly_to_arith(ctx, q);
465✔
3564

3565
  // map u (and its root) to x
3566
  r = intern_tbl_get_root(&ctx->intern, u);
465✔
3567
  assert(intern_tbl_root_is_free(&ctx->intern, r) && is_pos_term(r));
3568
  intern_tbl_map_root(&ctx->intern, r, thvar2code(x));
465✔
3569

3570
#if TRACE
3571
  printf("---> toplevel equality: ");
3572
  print_polynomial(stdout, p);
3573
  printf(" == 0\n");
3574
  printf("     simplified to ");
3575
  print_term(stdout, ctx->terms, u);
3576
  printf(" := ");
3577
  print_polynomial(stdout, q);
3578
  printf("\n");
3579
#endif
3580

3581
  return true;
465✔
3582
}
3583

3584

3585

3586

3587

3588

3589

3590
/******************************************************
3591
 *  TOP-LEVEL ARITHMETIC EQUALITIES OR DISEQUALITIES  *
3592
 *****************************************************/
3593

3594
static void assert_arith_bineq(context_t *ctx, term_t t1, term_t t2, bool tt);
3595

3596
/*
3597
 * Top-level equality: t == (ite c u1 u2) between arithmetic terms
3598
 * - apply lift-if rule: (t == (ite c u1 u2)) --> (ite c (t == u1) (t == u2)
3599
 * - if tt is true: assert the equality otherwise assert the disequality
3600
 */
3601
static void assert_ite_arith_bineq(context_t *ctx, composite_term_t *ite, term_t t, bool tt) {
451✔
3602
  literal_t l1, l2, l3;
3603

3604
  assert(ite->arity == 3);
3605

3606
  l1 = internalize_to_literal(ctx, ite->arg[0]);
451✔
3607
  if (l1 == true_literal) {
451✔
3608
    // (ite c u1 u2) --> u1
3609
    assert_arith_bineq(ctx, ite->arg[1], t, tt);
3✔
3610
  } else if (l1 == false_literal) {
448✔
3611
    // (ite c u1 u2) --> u2
3612
    assert_arith_bineq(ctx, ite->arg[2], t, tt);
15✔
3613
  } else {
3614
    l2 = map_arith_bineq(ctx, ite->arg[1], t); // (u1 == t)
433✔
3615
    l3 = map_arith_bineq(ctx, ite->arg[2], t); // (u2 == t)
433✔
3616
    assert_ite(&ctx->gate_manager, l1, l2, l3, tt);
433✔
3617
  }
3618
}
451✔
3619

3620

3621
/*
3622
 * Try substitution t1 := t2
3623
 * - both are arithmetic terms and roots in the internalization table
3624
 */
3625
static void try_arithvar_bineq_elim(context_t *ctx, term_t t1, term_t t2) {
164✔
3626
  intern_tbl_t *intern;
3627
  thvar_t x, y;
3628
  int32_t code;
3629

3630
  assert(is_pos_term(t1) && intern_tbl_is_root(&ctx->intern, t1) &&
3631
         intern_tbl_root_is_free(&ctx->intern, t1));
3632

3633
  intern = &ctx->intern;
164✔
3634

3635
  if (is_constant_term(ctx->terms, t2)) {
164✔
3636
    if (intern_tbl_valid_const_subst(intern, t1, t2)) {
2✔
3637
      intern_tbl_add_subst(intern, t1, t2);
2✔
3638
    } else {
3639
      // unsat by type incompatibility
3640
      longjmp(ctx->env, TRIVIALLY_UNSAT);
×
3641
    }
3642

3643
  } else if (intern_tbl_sound_subst(intern, t1, t2)) {
162✔
3644
    /*
3645
     * Internalize t2 to x.
3646
     * If t1 is still free after that, we can map t1 to x
3647
     * otherwise, t2 depends on t1 so we can't substitute.
3648
     */
3649
    x = internalize_to_arith(ctx, t2);
161✔
3650
    if (intern_tbl_root_is_free(intern, t1)) {
161✔
3651
      intern_tbl_map_root(&ctx->intern, t1, thvar2code(x));
2✔
3652
    } else {
3653
      assert(intern_tbl_root_is_mapped(intern, t1));
3654
      code = intern_tbl_map_of_root(intern, t1);
159✔
3655
      y = translate_code_to_arith(ctx, code);
159✔
3656

3657
      // assert x == y in the arithmetic solver
3658
      ctx->arith.assert_vareq_axiom(ctx->arith_solver, x, y, true);
159✔
3659
    }
3660
  } else {
3661
    x = internalize_to_arith(ctx, t1);
1✔
3662
    y = internalize_to_arith(ctx, t2);
1✔
3663
    ctx->arith.assert_vareq_axiom(ctx->arith_solver, x, y, true);
1✔
3664
  }
3665
}
164✔
3666

3667

3668
/*
3669
 * Top-level arithmetic equality t1 == t2:
3670
 * - if tt is true: assert t1 == t2 otherwise assert (t1 != t2)
3671
 * - both t1 and t2 are arithmetic terms and roots in the internalization table
3672
 * - the equality (t1 == t2) is not reducible by if-then-else flattening
3673
 */
3674
static void assert_arith_bineq_aux(context_t *ctx, term_t t1, term_t t2, bool tt) {
3,334✔
3675
  term_table_t *terms;
3676
  intern_tbl_t *intern;;
3677
  bool free1, free2;
3678
  thvar_t x, y;
3679
  occ_t u, v;
3680

3681
  assert(is_pos_term(t1) && intern_tbl_is_root(&ctx->intern, t1) &&
3682
         is_pos_term(t2) && intern_tbl_is_root(&ctx->intern, t2));
3683

3684
  terms = ctx->terms;
3,334✔
3685
  if (is_ite_term(terms, t1) && !is_ite_term(terms, t2)) {
3,334✔
3686
    assert_ite_arith_bineq(ctx, ite_term_desc(terms, t1), t2, tt);
112✔
3687
    return;
112✔
3688
  }
3689

3690
  if (is_ite_term(terms, t2) && !is_ite_term(terms, t1)) {
3,222✔
3691
    assert_ite_arith_bineq(ctx, ite_term_desc(terms, t2), t1, tt);
339✔
3692
    return;
339✔
3693
  }
3694

3695
  if (tt && context_arith_elim_enabled(ctx)) {
2,883✔
3696
    /*
3697
     * try a substitution
3698
     */
3699
    intern = &ctx->intern;
339✔
3700
    free1 = intern_tbl_root_is_free(intern, t1);
339✔
3701
    free2 = intern_tbl_root_is_free(intern, t2);
339✔
3702

3703
    if (free1 && free2) {
339✔
3704
      if (t1 != t2) {
1✔
3705
        intern_tbl_merge_classes(intern, t1, t2);
1✔
3706
      }
3707
      return;
1✔
3708
    }
3709

3710
    if (free1) {
338✔
3711
      try_arithvar_bineq_elim(ctx, t1, t2);
162✔
3712
      return;
162✔
3713
    }
3714

3715
    if (free2) {
176✔
3716
      try_arithvar_bineq_elim(ctx, t2, t1);
2✔
3717
      return;
2✔
3718
    }
3719

3720
  }
3721

3722
  /*
3723
   * Default: assert the constraint in the egraph or in the arithmetic
3724
   * solver if there's no egraph.
3725
   */
3726
  if (context_has_egraph(ctx)) {
2,718✔
3727
    u = internalize_to_eterm(ctx, t1);
1,658✔
3728
    v = internalize_to_eterm(ctx, t2);
1,658✔
3729
    if (!context_quant_enabled(ctx) || egraph_is_at_base_level(ctx->egraph)) {
1,658✔
3730
      if (tt) {
1,658✔
3731
        egraph_assert_eq_axiom(ctx->egraph, u, v);
162✔
3732
      } else {
3733
        egraph_assert_diseq_axiom(ctx->egraph, u, v);
1,496✔
3734
      }
3735
    } else {
3736
      literal_t l = egraph_make_eq(ctx->egraph, u, v);
×
3737
      if (tt) {
×
3738
        add_unit_clause(ctx->core, l);
×
3739
      } else {
3740
        add_unit_clause(ctx->core, not(l));
×
3741
      }
3742
    }
3743
  } else {
3744
    x = internalize_to_arith(ctx, t1);
1,060✔
3745
    y = internalize_to_arith(ctx, t2);
1,060✔
3746
    ctx->arith.assert_vareq_axiom(ctx->arith_solver, x, y, tt);
1,060✔
3747
  }
3748
}
3749

3750

3751

3752

3753
/*****************************************************
3754
 *  INTERNALIZATION OF TOP-LEVEL ATOMS AND FORMULAS  *
3755
 ****************************************************/
3756

3757
/*
3758
 * Recursive function: assert (t == tt) for a boolean term t
3759
 * - this is used when a toplevel formula simplifies to t
3760
 *   For example (ite c t u) --> t if c is true.
3761
 * - t is not necessarily a root in the internalization table
3762
 */
3763
static void assert_term(context_t *ctx, term_t t, bool tt);
3764

3765

3766
/*
3767
 * Top-level predicate: (p t_1 .. t_n)
3768
 * - if tt is true: assert (p t_1 ... t_n)
3769
 * - if tt is false: assert (not (p t_1 ... t_n))
3770
 */
3771
static void assert_toplevel_apply(context_t *ctx, composite_term_t *app, bool tt) {
252✔
3772
  occ_t *a;
3773
  uint32_t i, n;
3774

3775
  assert(app->arity > 0);
3776

3777
  n = app->arity;
252✔
3778

3779
  check_high_order_support(ctx, app->arg+1, n-1);
252✔
3780

3781
  a = alloc_istack_array(&ctx->istack, n);
252✔
3782
  for (i=0; i<n; i++) {
1,030✔
3783
    a[i] = internalize_to_eterm(ctx, app->arg[i]);
778✔
3784
  }
3785

3786
  if (!context_quant_enabled(ctx) || egraph_is_at_base_level(ctx->egraph)) {
252✔
3787
    if (tt) {
242✔
3788
      egraph_assert_pred_axiom(ctx->egraph, a[0], n-1, a+1);
214✔
3789
    } else {
3790
      egraph_assert_notpred_axiom(ctx->egraph, a[0], n-1, a+1);
28✔
3791
    }
3792
  } else {
3793
    literal_t l = egraph_make_pred(ctx->egraph, a[0], n-1, a+1);
10✔
3794
    if (tt) {
10✔
3795
      add_unit_clause(ctx->core, l);
3✔
3796
    } else {
3797
      add_unit_clause(ctx->core, not(l));
7✔
3798
    }
3799
  }
3800

3801
  free_istack_array(&ctx->istack, a);
252✔
3802
}
252✔
3803

3804

3805
/*
3806
 * Top-level (select i t)
3807
 * - if tt is true: assert (select i t)
3808
 * - if tt is false: assert (not (select i t))
3809
 */
3810
static void assert_toplevel_select(context_t *ctx, select_term_t *select, bool tt) {
×
3811
  occ_t u;
3812

3813
  u = map_select_to_eterm(ctx, select, bool_type(ctx->types));
×
3814
  if (! tt) {
×
3815
    u = opposite_occ(u);
×
3816
  }
3817
  if (!context_quant_enabled(ctx) || egraph_is_at_base_level(ctx->egraph)) {
×
3818
    egraph_assert_axiom(ctx->egraph, u);
×
3819
  } else {
3820
    literal_t l = egraph_make_eq(ctx->egraph, u, true_occ);
×
3821
    add_unit_clause(ctx->core, l);
×
3822
  }
3823
}
×
3824

3825

3826
/*
3827
 * Top-level equality between Boolean terms
3828
 * - if tt is true, assert t1 == t2
3829
 * - if tt is false, assert t1 != t2
3830
 */
3831
static void assert_toplevel_iff(context_t *ctx, term_t t1, term_t t2, bool tt) {
2,735✔
3832
  term_t t;
3833
  literal_t l1, l2;
3834

3835
  /*
3836
   * Apply substitution then try flattening
3837
   */
3838
  t1 = intern_tbl_get_root(&ctx->intern, t1);
2,735✔
3839
  t2 = intern_tbl_get_root(&ctx->intern, t2);
2,735✔
3840
  if (t1 == t2) {
2,735✔
3841
    // (eq t1 t2) is true
3842
    if (!tt) {
×
3843
      longjmp(ctx->env, TRIVIALLY_UNSAT);
×
3844
    }
3845
  }
3846
  // try simplification
3847
  t = simplify_bool_eq(ctx, t1, t2);
2,735✔
3848
  if (t != NULL_TERM) {
2,735✔
3849
    // (eq t1 t2) is equivalent to t
3850
    assert_term(ctx, t, tt) ;
17✔
3851
  } else {
3852
    // no simplification
3853
    l1 = internalize_to_literal(ctx, t1);
2,718✔
3854
    l2 = internalize_to_literal(ctx, t2);
2,718✔
3855
    assert_iff(&ctx->gate_manager, l1, l2, tt);
2,718✔
3856

3857
#if 0
3858
    if (tt) {
3859
      printf("top assert: (eq ");
3860
      print_literal(stdout, l1);
3861
      printf(" ");
3862
      print_literal(stdout, l2);
3863
      printf(")\n");
3864
    } else {
3865
      printf("top assert: (xor ");
3866
      print_literal(stdout, l1);
3867
      printf(" ");
3868
      print_literal(stdout, l2);
3869
      printf(")\n");
3870
    }
3871
#endif
3872
  }
3873
}
2,735✔
3874

3875
/*
3876
 * Top-level equality assertion (eq t1 t2):
3877
 * - if tt is true, assert (t1 == t2)
3878
 *   if tt is false, assert (t1 != t2)
3879
 */
3880
static void assert_toplevel_eq(context_t *ctx, composite_term_t *eq, bool tt) {
5,126✔
3881
  occ_t u1, u2;
3882

3883
  assert(eq->arity == 2);
3884

3885
  if (is_boolean_term(ctx->terms, eq->arg[0])) {
5,126✔
3886
    assert(is_boolean_term(ctx->terms, eq->arg[1]));
3887
    assert_toplevel_iff(ctx, eq->arg[0], eq->arg[1], tt);
2,735✔
3888
  } else {
3889
    // filter out high-order terms. It's enough to check eq->arg[0]
3890
    check_high_order_support(ctx, eq->arg, 1);
2,391✔
3891

3892
    u1 = internalize_to_eterm(ctx, eq->arg[0]);
2,390✔
3893
    u2 = internalize_to_eterm(ctx, eq->arg[1]);
2,390✔
3894
    if (!context_quant_enabled(ctx) || egraph_is_at_base_level(ctx->egraph)) {
2,389✔
3895
      if (tt) {
2,375✔
3896
        egraph_assert_eq_axiom(ctx->egraph, u1, u2);
1,321✔
3897
      } else {
3898
        egraph_assert_diseq_axiom(ctx->egraph, u1, u2);
1,054✔
3899
      }
3900
    } else {
3901
      literal_t l = egraph_make_eq(ctx->egraph, u1, u2);
14✔
3902
      if (tt) {
14✔
3903
        add_unit_clause(ctx->core, l);
9✔
3904
      } else {
3905
        add_unit_clause(ctx->core, not(l));
5✔
3906
      }
3907
    }
3908
  }
3909
}
5,124✔
3910

3911

3912
/*
3913
 * Assertion (distinct a[0] .... a[n-1]) == tt
3914
 * when a[0] ... a[n-1] are arithmetic variables.
3915
 */
3916
static void assert_arith_distinct(context_t *ctx, uint32_t n, thvar_t *a, bool tt) {
155✔
3917
  literal_t l;
3918

3919
  l = make_arith_distinct(ctx, n, a);
155✔
3920
  if (! tt) {
155✔
3921
    l = not(l);
4✔
3922
  }
3923
  add_unit_clause(ctx->core, l);
155✔
3924
}
155✔
3925

3926

3927
/*
3928
 * Assertion (distinct a[0] .... a[n-1]) == tt
3929
 * when a[0] ... a[n-1] are bitvector variables.
3930
 */
3931
static void assert_bv_distinct(context_t *ctx, uint32_t n, thvar_t *a, bool tt) {
2✔
3932
  literal_t l;
3933

3934
  l = make_bv_distinct(ctx, n, a);
2✔
3935
  if (! tt) {
2✔
3936
    l = not(l);
1✔
3937
  }
3938
  add_unit_clause(ctx->core, l);
2✔
3939
}
2✔
3940

3941

3942
/*
3943
 * Generic (distinct t1 .. t_n)
3944
 * - if tt: assert (distinct t_1 ... t_n)
3945
 * - otherwise: assert (not (distinct t_1 ... t_n))
3946
 */
3947
static void assert_toplevel_distinct(context_t *ctx, composite_term_t *distinct, bool tt) {
172✔
3948
  uint32_t i, n;
3949
  int32_t *a;
3950

3951
  n = distinct->arity;
172✔
3952
  assert(n >= 2);
3953

3954
  a = alloc_istack_array(&ctx->istack, n);
172✔
3955

3956
  if (context_has_egraph(ctx)) {
172✔
3957
    // fail if arguments have function types and we don't
3958
    // have a function/array solver
3959
    check_high_order_support(ctx, distinct->arg, 1);
15✔
3960

3961
    // forward the assertion to the egraph
3962
    for (i=0; i<n; i++) {
63✔
3963
      a[i] = internalize_to_eterm(ctx, distinct->arg[i]);
50✔
3964
    }
3965

3966
    if (!context_quant_enabled(ctx) || egraph_is_at_base_level(ctx->egraph)) {
13✔
3967
      if (tt) {
13✔
3968
        egraph_assert_distinct_axiom(ctx->egraph, n, a);
9✔
3969
      } else {
3970
        egraph_assert_notdistinct_axiom(ctx->egraph, n, a);
4✔
3971
      }
3972
    } else {
3973
      literal_t l = egraph_make_distinct(ctx->egraph, n, a);
×
3974
      if (tt) {
×
3975
        add_unit_clause(ctx->core, l);
×
3976
      } else {
3977
        add_unit_clause(ctx->core, not(l));
×
3978
      }
3979
    }
3980

3981
  } else if (is_arithmetic_term(ctx->terms, distinct->arg[0])) {
157✔
3982
    // translate to arithmetic then assert
3983
    for (i=0; i<n; i++) {
3,922✔
3984
      a[i] = internalize_to_arith(ctx, distinct->arg[i]);
3,767✔
3985
    }
3986
    assert_arith_distinct(ctx, n, a, tt);
155✔
3987

3988
  } else if (is_bitvector_term(ctx->terms, distinct->arg[0])) {
2✔
3989
    // translate to bitvectors then assert
3990
    for (i=0; i<n; i++) {
13✔
3991
      a[i] = internalize_to_bv(ctx, distinct->arg[i]);
11✔
3992
    }
3993
    assert_bv_distinct(ctx, n, a, tt);
2✔
3994

3995
  } else {
3996
    longjmp(ctx->env, uf_error_code(ctx, distinct->arg[0]));
×
3997
  }
3998

3999
  free_istack_array(&ctx->istack, a);
170✔
4000
}
170✔
4001

4002

4003

4004
/*
4005
 * Top-level arithmetic equality t1 == u1:
4006
 * - t1 and u1 are arithmetic terms
4007
 * - if tt is true assert (t1 == u1) otherwise assert (t1 != u1)
4008
 * - apply lift-if simplifications and variable elimination
4009
 */
4010
static void assert_arith_bineq(context_t *ctx, term_t t1, term_t u1, bool tt) {
3,407✔
4011
  ivector_t *v;
4012
  int32_t *a;
4013
  uint32_t i, n;
4014
  term_t t2, u2;
4015

4016
  /*
4017
   * Apply substitutions then try if-then-else flattening
4018
   */
4019
  t1 = intern_tbl_get_root(&ctx->intern, t1);
3,407✔
4020
  u1 = intern_tbl_get_root(&ctx->intern, u1);
3,407✔
4021

4022
  v = &ctx->aux_vector;
3,407✔
4023
  assert(v->size == 0);
4024
  t2 = flatten_ite_equality(ctx, v, t1, u1);
3,407✔
4025
  u2 = flatten_ite_equality(ctx, v, u1, t2);
3,407✔
4026

4027
  /*
4028
   * (t1 == u1) is now equivalent to
4029
   * the conjunction of (t2 == u2) and all the terms in v
4030
   */
4031
  n = v->size;
3,407✔
4032
  if (n == 0) {
3,407✔
4033
    /*
4034
     * The simple flattening did not work.
4035
     */
4036
    assert(t1 == t2 && u1 == u2);
4037
    assert_arith_bineq_aux(ctx, t2, u2, tt);
3,321✔
4038

4039
  } else {
4040
    // make a copy of v[0 ... n-1]
4041
    // and reserve a[n] for the literal (eq t2 u2)
4042
    a = alloc_istack_array(&ctx->istack, n+1);
86✔
4043
    for (i=0; i<n; i++) {
1,531✔
4044
      a[i] = v->data[i];
1,445✔
4045
    }
4046
    ivector_reset(v);
86✔
4047

4048
    if (tt) {
86✔
4049
      // assert (and a[0] ... a[n-1] (eq t2 u2))
4050
      for (i=0; i<n; i++) {
340✔
4051
        assert_term(ctx, a[i], true);
327✔
4052
      }
4053

4054
      /*
4055
       * The assertions a[0] ... a[n-1] may have
4056
       * caused roots to be merged. So we must
4057
       * apply term substitution again.
4058
       */
4059
      t2 = intern_tbl_get_root(&ctx->intern, t2);
13✔
4060
      u2 = intern_tbl_get_root(&ctx->intern, u2);
13✔
4061
      assert_arith_bineq_aux(ctx, t2, u2, true);
13✔
4062

4063
    } else {
4064
      // assert (or (not a[0]) ... (not a[n-1]) (not (eq t2 u2)))
4065
      for (i=0; i<n; i++) {
1,191✔
4066
        a[i] = not(internalize_to_literal(ctx, a[i]));
1,118✔
4067
      }
4068
      a[n] = not(map_arith_bineq_aux(ctx, t2, u2));
73✔
4069

4070
      add_clause(ctx->core, n+1, a);
73✔
4071
    }
4072

4073
    free_istack_array(&ctx->istack, a);
86✔
4074
  }
4075
}
3,407✔
4076

4077

4078
/*
4079
 * Top-level arithmetic assertion:
4080
 * - if tt is true, assert p == 0
4081
 * - if tt is false, assert p != 0
4082
 */
4083
static void assert_toplevel_poly_eq(context_t *ctx, polynomial_t *p, bool tt) {
1,273✔
4084
  uint32_t i, n;
4085
  thvar_t *a;
4086

4087
  n = p->nterms;
1,273✔
4088
  a = alloc_istack_array(&ctx->istack, n);;
1,273✔
4089
  // skip the constant if any
4090
  i = 0;
1,273✔
4091
  if (p->mono[0].var == const_idx) {
1,273✔
4092
    a[0] = null_thvar;
1,208✔
4093
    i ++;
1,208✔
4094
  }
4095

4096
  // deal with the non-constant monomials
4097
  while (i<n) {
3,928✔
4098
    a[i] = internalize_to_arith(ctx, p->mono[i].var);
2,655✔
4099
    i ++;
2,655✔
4100
  }
4101

4102
  // assertion
4103
  ctx->arith.assert_poly_eq_axiom(ctx->arith_solver, p, a, tt);
1,273✔
4104
  free_istack_array(&ctx->istack, a);
1,273✔
4105
}
1,273✔
4106

4107

4108

4109
/*
4110
 * Top-level arithmetic equality:
4111
 * - t is an arithmetic term
4112
 * - if tt is true, assert (t == 0)
4113
 * - otherwise, assert (t != 0)
4114
 */
4115
static void assert_toplevel_arith_eq(context_t *ctx, term_t t, bool tt) {
2,266✔
4116
  term_table_t *terms;
4117
  polynomial_t *p;
4118
  bool all_int;
4119
  thvar_t x;
4120

4121
  assert(is_arithmetic_term(ctx->terms, t));
4122

4123
  terms = ctx->terms;
2,266✔
4124
  if (tt && context_arith_elim_enabled(ctx) && term_kind(terms, t) == ARITH_POLY) {
2,266✔
4125
    /*
4126
     * Polynomial equality: a_1 t_1 + ... + a_n t_n = 0
4127
     * attempt to eliminate one of t_1 ... t_n
4128
     */
4129
    p = poly_term_desc(terms, t);
544✔
4130
    all_int = is_integer_term(terms, t);
544✔
4131
    if (try_arithvar_elim(ctx, p, all_int)) { // elimination worked
544✔
4132
      return;
465✔
4133
    }
4134
  }
4135

4136
  // default
4137
  if (term_kind(terms, t) == ARITH_POLY) {
1,801✔
4138
    assert_toplevel_poly_eq(ctx, poly_term_desc(terms, t), tt);
1,273✔
4139
  } else if (is_ite_term(terms, t)) {
528✔
4140
    assert_arith_bineq(ctx, t, zero_term, tt);
8✔
4141
  } else {
4142
    x = internalize_to_arith(ctx, t);
520✔
4143
    ctx->arith.assert_eq_axiom(ctx->arith_solver, x, tt);
519✔
4144
  }
4145
}
4146

4147

4148

4149
/*
4150
 * Top-level arithmetic assertion:
4151
 * - if tt is true, assert p >= 0
4152
 * - if tt is false, assert p < 0
4153
 */
4154
static void assert_toplevel_poly_geq(context_t *ctx, polynomial_t *p, bool tt) {
5,925✔
4155
  uint32_t i, n;
4156
  thvar_t *a;
4157

4158
  n = p->nterms;
5,925✔
4159
  a = alloc_istack_array(&ctx->istack, n);;
5,925✔
4160
  // skip the constant if any
4161
  i = 0;
5,925✔
4162
  if (p->mono[0].var == const_idx) {
5,925✔
4163
    a[0] = null_thvar;
5,104✔
4164
    i ++;
5,104✔
4165
  }
4166

4167
  // deal with the non-constant monomials
4168
  while (i<n) {
15,592✔
4169
    a[i] = internalize_to_arith(ctx, p->mono[i].var);
9,668✔
4170
    i ++;
9,667✔
4171
  }
4172

4173
  // assertion
4174
  ctx->arith.assert_poly_ge_axiom(ctx->arith_solver, p, a, tt);
5,924✔
4175
  free_istack_array(&ctx->istack, a);
5,924✔
4176
}
5,924✔
4177

4178

4179

4180
/*
4181
 * Top-level arithmetic inequality:
4182
 * - t is an arithmetic term
4183
 * - if tt is true, assert (t >= 0)
4184
 * - if tt is false, assert (t < 0)
4185
 */
4186
static void assert_toplevel_arith_geq(context_t *ctx, term_t t, bool tt) {
7,848✔
4187
  term_table_t *terms;
4188
  thvar_t x;
4189

4190
  assert(is_arithmetic_term(ctx->terms, t));
4191

4192
  terms = ctx->terms;
7,848✔
4193
  if (term_kind(terms, t) == ARITH_POLY) {
7,848✔
4194
    assert_toplevel_poly_geq(ctx, poly_term_desc(terms, t), tt);
5,925✔
4195
  } else {
4196
    x = internalize_to_arith(ctx, t);
1,923✔
4197
    ctx->arith.assert_ge_axiom(ctx->arith_solver, x, tt);
1,923✔
4198
  }
4199
}
7,847✔
4200

4201

4202
/*
4203
 * Top-level binary equality: (eq t u)
4204
 * - both t and u are arithmetic terms
4205
 * - if tt is true, assert (t == u)
4206
 * - if tt is false, assert (t != u)
4207
 */
4208
static void assert_toplevel_arith_bineq(context_t *ctx, composite_term_t *eq, bool tt) {
3,381✔
4209
  assert(eq->arity == 2);
4210
  assert_arith_bineq(ctx, eq->arg[0], eq->arg[1], tt);
3,381✔
4211
}
3,381✔
4212

4213

4214

4215
/*
4216
 * Top-level (is_int t)
4217
 * - t is an arithmetic term
4218
 * - if tt is true, assert (t <= (floor t))
4219
 * - if tt is false, asssert (t > (floor t))
4220
 *
4221
 * NOTE: instead of asserting (t <= (floor t)) we could create a fresh
4222
 * integer variable z and assert (t = z).
4223
 */
4224
static void assert_toplevel_arith_is_int(context_t *ctx, term_t t, bool tt) {
×
4225
  polynomial_t *p;
4226
  thvar_t map[2];
4227
  thvar_t x, y;
4228

4229
  x = internalize_to_arith(ctx, t);
×
4230
  if (ctx->arith.arith_var_is_int(ctx->arith_solver, x)) {
×
4231
    if (!tt) {
×
4232
      longjmp(ctx->env, TRIVIALLY_UNSAT);
×
4233
    }
4234
  } else {
4235
    // x is not an integer variable
4236
    y = get_floor(ctx, x); // y := (floor x)
×
4237
    p = context_get_aux_poly(ctx, 3);
×
4238
    context_store_diff_poly(p, map, y, x); // (p, map) stores (y - x)
×
4239
    // assert either (p >= 0) --> (x <= floor(x))
4240
    // or (p < 0) --> (x > (floor x)
4241
    ctx->arith.assert_poly_ge_axiom(ctx->arith_solver, p, map, tt);
×
4242
  }
4243
}
×
4244

4245

4246
/*
4247
 * Top-level (divides k t)
4248
 * - if tt is true, assert (t <= k * (div t k))
4249
 * - if tt is false, assert (t > k * (div t k))
4250
 *
4251
 * We assume (k != 0) since (divides 0 t) is rewritten to (t == 0) by
4252
 * the term manager.
4253
 *
4254
 * NOTE: instead of asserting (t <= k * (div t k)) we could create a fresh
4255
 * integer variable z and assert (t = k * z).
4256
 */
4257
static void assert_toplevel_arith_divides(context_t *ctx, composite_term_t *divides, bool tt) {
×
4258
  rational_t k;
4259
  polynomial_t *p;
4260
  thvar_t map[2];
4261
  thvar_t x, y;
4262
  term_t d;
4263

4264
  assert(divides->arity == 2);
4265

4266
  d = divides->arg[0];
×
4267
  if (term_kind(ctx->terms, d) == ARITH_CONSTANT) {
×
4268
    // copy the divider
4269
    q_init(&k);
×
4270
    q_set(&k, rational_term_desc(ctx->terms, d));
×
4271
    assert(q_is_nonzero(&k));
4272

4273
    x = internalize_to_arith(ctx, divides->arg[1]);
×
4274
    y = get_div(ctx, x, &k);  // y := (div x k);
×
4275
    p = context_get_aux_poly(ctx, 3);
×
4276
    context_store_divides_constraint(p, map, x, y, &k); // p is (- x + k * y)
×
4277

4278
    // if tt, assert (p >= 0) <=> x <= k * y
4279
    // if not tt, assert (p < 0) <=> x > k * y
4280
    ctx->arith.assert_poly_ge_axiom(ctx->arith_solver, p, map, tt);
×
4281

4282
    q_clear(&k);
×
4283
  } else {
4284
    // not a constant divider: not supported
4285
    longjmp(ctx->env, FORMULA_NOT_LINEAR);
×
4286
  }
4287
}
×
4288

4289

4290

4291

4292

4293

4294
/*
4295
 * Top-level conditional
4296
 * - c = conditional descriptor
4297
 * - if tt is true: assert c otherwise assert not c
4298
 *
4299
 * - c->nconds = number of clauses in the conditional
4300
 * - for each clause i: c->pair[i] = <cond, val>
4301
 * - c->defval = default value
4302
 */
4303
static void assert_toplevel_conditional(context_t *ctx, conditional_t *c, bool tt) {
13✔
4304
  uint32_t i, n;
4305
  literal_t *a;
4306
  literal_t l;
4307
  bool all_false;
4308
  term_t t;
4309

4310
#if 0
4311
  printf("---> toplevel conditional\n");
4312
#endif
4313

4314
  t = simplify_conditional(ctx, c);
13✔
4315
  if (t != NULL_TERM) {
13✔
4316
    assert_term(ctx, t, tt);
2✔
4317
    return;
2✔
4318
  }
4319

4320
  n = c->nconds;
11✔
4321
  a = alloc_istack_array(&ctx->istack, n + 1);
11✔
4322

4323
  all_false = true;
11✔
4324
  for (i=0; i<n; i++) {
72✔
4325
    // a[i] = condition for pair[i]
4326
    a[i] = internalize_to_literal(ctx, c->pair[i].cond);
62✔
4327
    if (a[i] == true_literal) {
62✔
4328
      // if a[i] is true, all other conditions must be false
4329
      assert_term(ctx, c->pair[i].val, tt);
1✔
4330
      goto done;
1✔
4331
    }
4332
    if (a[i] != false_literal) {
61✔
4333
      // l = value for pair[i]
4334
      l = signed_literal(internalize_to_literal(ctx, c->pair[i].val), tt);
58✔
4335
      add_binary_clause(ctx->core, not(a[i]), l); // a[i] => v[i]
58✔
4336
      all_false = false;
58✔
4337
    }
4338
  }
4339

4340
  if (all_false) {
10✔
4341
    // all a[i]s are false: no need for a clause
4342
    assert_term(ctx, c->defval, tt);
1✔
4343
    goto done;
1✔
4344
  }
4345

4346
  // last clause: (a[0] \/ .... \/ a[n] \/ +/-defval)
4347
  a[n] = signed_literal(internalize_to_literal(ctx, c->defval), tt);
9✔
4348
  add_clause(ctx->core, n+1, a);
9✔
4349

4350
  // cleanup
4351
 done:
11✔
4352
  free_istack_array(&ctx->istack, a);
11✔
4353
}
4354

4355

4356

4357
/*
4358
 * Top-level boolean if-then-else (ite c t1 t2)
4359
 * - if tt is true: assert (ite c t1 t2)
4360
 * - if tt is false: assert (not (ite c t1 t2))
4361
 */
4362
static void assert_toplevel_ite(context_t *ctx, composite_term_t *ite, bool tt) {
70✔
4363
  conditional_t *d;
4364
  literal_t l1, l2, l3;
4365

4366
  assert(ite->arity == 3);
4367

4368
  // high-order ite should work. See map_ite_to_eterm
4369

4370
  d = context_make_conditional(ctx, ite);
70✔
4371
  if (d != NULL) {
70✔
4372
    assert_toplevel_conditional(ctx, d, tt);
13✔
4373
    context_free_conditional(ctx, d);
13✔
4374
    return;
13✔
4375
  }
4376

4377
  l1 = internalize_to_literal(ctx, ite->arg[0]);
57✔
4378
  if (l1 == true_literal) {
57✔
4379
    assert_term(ctx, ite->arg[1], tt);
5✔
4380
  } else if (l1 == false_literal) {
52✔
4381
    assert_term(ctx, ite->arg[2], tt);
2✔
4382
  } else {
4383
    l2 = internalize_to_literal(ctx, ite->arg[1]);
50✔
4384
    l3 = internalize_to_literal(ctx, ite->arg[2]);
50✔
4385
    assert_ite(&ctx->gate_manager, l1, l2, l3, tt);
50✔
4386
  }
4387
}
4388

4389

4390
/*
4391
 * Top-level (or t1 ... t_n)
4392
 * - it tt is true: add a clause
4393
 * - it tt is false: assert (not t1) ... (not t_n)
4394
 */
4395
static void assert_toplevel_or(context_t *ctx, composite_term_t *or, bool tt) {
32,428✔
4396
  ivector_t *v;
4397
  int32_t *a;
4398
  uint32_t i, n;
4399

4400
  if (tt) {
32,428✔
4401
    if (context_flatten_or_enabled(ctx)) {
32,412✔
4402
      // Flatten into vector v
4403
      v = &ctx->aux_vector;
14,476✔
4404
      assert(v->size == 0);
4405
      flatten_or_term(ctx, v, or);
14,476✔
4406

4407
      // if v contains a true_term, ignore the clause
4408
      n = v->size;
14,476✔
4409
      if (disjunct_is_true(ctx, v->data, n)) {
14,476✔
4410
        ivector_reset(v);
803✔
4411
        return;
803✔
4412
      }
4413

4414
      // make a copy of v
4415
      a = alloc_istack_array(&ctx->istack, n);
13,673✔
4416
      for (i=0; i<n; i++) {
57,634✔
4417
        a[i] = v->data[i];
43,961✔
4418
      }
4419
      ivector_reset(v);
13,673✔
4420

4421
      for (i=0; i<n; i++) {
57,311✔
4422
        a[i] = internalize_to_literal(ctx, a[i]);
43,791✔
4423
        if (a[i] == true_literal) goto done;
43,791✔
4424
      }
4425

4426
    } else {
4427
      /*
4428
       * No flattening
4429
       */
4430
      n = or->arity;
17,936✔
4431
      if (disjunct_is_true(ctx, or->arg, n)) {
17,936✔
4432
        return;
2,503✔
4433
      }
4434

4435
      a = alloc_istack_array(&ctx->istack, n);
15,433✔
4436
      for (i=0; i<n; i++) {
113,590✔
4437
        a[i] = internalize_to_literal(ctx, or->arg[i]);
98,257✔
4438
        if (a[i] == true_literal) goto done;
98,257✔
4439
      }
4440
    }
4441

4442
    // assert (or a[0] ... a[n-1])
4443
    add_clause(ctx->core, n, a);
28,853✔
4444

4445
  done:
29,106✔
4446
    free_istack_array(&ctx->istack, a);
29,106✔
4447

4448
  } else {
4449
    /*
4450
     * Propagate to children:
4451
     *  (or t_0 ... t_n-1) is false
4452
     * so all children must be false too
4453
     */
4454
    n = or->arity;
16✔
4455
    for (i=0; i<n; i++) {
48✔
4456
      assert_term(ctx, or->arg[i], false);
32✔
4457
    }
4458
  }
4459

4460
}
4461

4462

4463
/*
4464
 * Top-level (xor t1 ... t_n) == tt
4465
 */
4466
static void assert_toplevel_xor(context_t *ctx, composite_term_t *xor, bool tt) {
7✔
4467
  int32_t *a;
4468
  uint32_t i, n;
4469

4470
  n = xor->arity;
7✔
4471
  a = alloc_istack_array(&ctx->istack, n);
7✔
4472
  for (i=0; i<n; i++) {
58✔
4473
    a[i] = internalize_to_literal(ctx, xor->arg[i]);
51✔
4474
  }
4475

4476
  assert_xor(&ctx->gate_manager, n, a, tt);
7✔
4477
  free_istack_array(&ctx->istack, a);
7✔
4478
}
7✔
4479

4480

4481

4482
/*
4483
 * Top-level bit select
4484
 */
4485
static void assert_toplevel_bit_select(context_t *ctx, select_term_t *select, bool tt) {
31,337✔
4486
  term_t t, s;
4487
  thvar_t x;
4488

4489
  /*
4490
   * Check for simplification
4491
   */
4492
  t = intern_tbl_get_root(&ctx->intern, select->arg);
31,337✔
4493
  s = extract_bit(ctx->terms, t, select->idx);
31,337✔
4494
  if (s != NULL_TERM) {
31,337✔
4495
    // (select t i) is s
4496
    assert_term(ctx, s, tt);
63✔
4497
  } else {
4498
    // no simplification
4499
    x = internalize_to_bv(ctx, select->arg);
31,274✔
4500
    ctx->bv.set_bit(ctx->bv_solver, x, select->idx, tt);
31,274✔
4501
  }
4502
}
31,336✔
4503

4504

4505
/*
4506
 * Top-level bitvector atoms
4507
 */
4508
// Auxiliary function: assert (t == 0) or (t != 0) depending on tt
4509
static void assert_toplevel_bveq0(context_t *ctx, term_t t, bool tt) {
15✔
4510
  uint32_t n;
4511
  thvar_t x, y;
4512

4513
  t = intern_tbl_get_root(&ctx->intern, t);
15✔
4514
  n = term_bitsize(ctx->terms, t);
15✔
4515
  x = internalize_to_bv(ctx, t);
15✔
4516
  y = ctx->bv.create_zero(ctx->bv_solver, n);
15✔
4517
  ctx->bv.assert_eq_axiom(ctx->bv_solver, x, y, tt);
15✔
4518
}
15✔
4519

4520

4521
/*
4522
 * Experimental: when t1 and t2 have a common factor C:
4523
 *   t1 = C * u1
4524
 *   t2 = C * u2
4525
 * then we have (t1 /= t2) implies (u1 /= u2).
4526
 * So we can add (u1 /= u2) when (t1 /= t2) is asserted.
4527
 * This is redundant but it may help solving the problem, especially if C is a
4528
 * complex expression.
4529
 */
4530
static void assert_factored_inequality(context_t *ctx, bvfactoring_t *f) {
8✔
4531
  term_t u1, u2;
4532
  thvar_t x, y;
4533

4534
  assert(f->code == BVFACTOR_FOUND);
4535

4536
  //  printf("Asserting factored inequality\n\n");
4537

4538
  u1 = bitvector_factoring_left_term(ctx, f);
8✔
4539
  u2 = bitvector_factoring_right_term(ctx, f);
8✔
4540
  x = internalize_to_bv(ctx, u1);
8✔
4541
  y = internalize_to_bv(ctx, u2);
8✔
4542
  ctx->bv.assert_eq_axiom(ctx->bv_solver, x,  y, false);
8✔
4543
}
8✔
4544

4545
static void assert_toplevel_bveq(context_t *ctx, composite_term_t *eq, bool tt) {
7,180✔
4546
  bveq_simp_t simp;
4547
  ivector_t *v;
4548
  int32_t *a;
4549
  term_t t, t1, t2;
4550
  thvar_t x, y;
4551
  uint32_t i, n;
4552

4553
  assert(eq->arity == 2);
4554

4555
  t1 = intern_tbl_get_root(&ctx->intern, eq->arg[0]);
7,180✔
4556
  t2 = intern_tbl_get_root(&ctx->intern, eq->arg[1]);
7,180✔
4557
  t = simplify_bitvector_eq(ctx, t1, t2);
7,180✔
4558
  if (t != NULL_TERM) {
7,180✔
4559
    // (bveq t1 t2) is equivalent to t
4560
    assert_term(ctx, t, tt);
18✔
4561
    return;
48✔
4562
  }
4563

4564
  if (tt) {
7,162✔
4565
    // try to flatten to a conjunction of terms
4566
    v = &ctx->aux_vector;
6,162✔
4567
    assert(v->size == 0);
4568
    if (bveq_flattens(ctx->terms, t1, t2, v)) {
6,162✔
4569
      /*
4570
       * (bveq t1 t2) is equivalent to (and v[0] ... v[k])
4571
       * (bveq t1 t2) is true at the toplevel so v[0] ... v[k] must all be true
4572
       */
4573

4574
      // make a copy of v
4575
      n = v->size;
19✔
4576
      a = alloc_istack_array(&ctx->istack, n);
19✔
4577
      for (i=0; i<n; i++) {
579✔
4578
        a[i] = v->data[i];
560✔
4579
      }
4580
      ivector_reset(v);
19✔
4581

4582
      // assert
4583
      for (i=0; i<n; i++) {
548✔
4584
        assert_term(ctx, a[i], true);
530✔
4585
      }
4586

4587
      free_istack_array(&ctx->istack, a);
18✔
4588
      return;
18✔
4589
    }
4590

4591
    // flattening failed
4592
    ivector_reset(v);
6,143✔
4593
  }
4594

4595
  /*
4596
   * Try more simplifications
4597
   */
4598
  try_arithmetic_bveq_simplification(ctx, &simp, t1, t2);
7,143✔
4599
  switch (simp.code) {
7,143✔
4600
  case BVEQ_CODE_TRUE:
×
4601
    if (!tt) longjmp(ctx->env, TRIVIALLY_UNSAT);
×
4602
    break;
×
4603

4604
  case BVEQ_CODE_FALSE:
×
4605
    if (tt) longjmp(ctx->env, TRIVIALLY_UNSAT);
×
4606
    break;
×
4607

4608
  case BVEQ_CODE_REDUCED:
33✔
4609
    t1 = intern_tbl_get_root(&ctx->intern, simp.left);
33✔
4610
    t2 = intern_tbl_get_root(&ctx->intern, simp.right);
33✔
4611
    break;
33✔
4612

4613
  case BVEQ_CODE_REDUCED0:
15✔
4614
    // reduced to simp.left == 0
4615
    assert_toplevel_bveq0(ctx, simp.left, tt);
15✔
4616
    return;
15✔
4617

4618
  default:
7,095✔
4619
    break;
7,095✔
4620
  }
4621

4622
  /*
4623
   * Try Factoring
4624
   */
4625
  if (!tt) {
7,128✔
4626
    bvfactoring_t *factoring;
4627
    bool eq = false;
987✔
4628

4629
    factoring = objstack_alloc(&ctx->ostack, sizeof(bvfactoring_t), (cleaner_t) delete_bvfactoring);
987✔
4630
    init_bvfactoring(factoring);
987✔
4631

4632
    try_bitvector_factoring(ctx, factoring, t1, t2);
987✔
4633
    switch (factoring->code) {
987✔
4634
    case BVFACTOR_EQUAL:
4✔
4635
      eq = true;
4✔
4636
      break;
4✔
4637

4638
    case BVFACTOR_FOUND:
8✔
4639
      assert_factored_inequality(ctx, factoring);
8✔
4640
      break;
8✔
4641

4642
    default:
975✔
4643
      break;
975✔
4644
    }
4645
    // delete_bvfactoring(&factoring);
4646
    objstack_pop(&ctx->ostack);
987✔
4647

4648
    if (eq) {
987✔
4649
      longjmp(ctx->env, TRIVIALLY_UNSAT);
4✔
4650
    }
4651
  }
4652

4653
  /*
4654
   * NOTE: asserting (eq t1 t2) in the egraph instead makes things worse
4655
   */
4656
  x = internalize_to_bv(ctx, t1);
7,124✔
4657
  y = internalize_to_bv(ctx, t2);
7,124✔
4658
  ctx->bv.assert_eq_axiom(ctx->bv_solver, x,  y, tt);
7,124✔
4659
}
4660

4661
static void assert_toplevel_bvge(context_t *ctx, composite_term_t *ge, bool tt) {
909✔
4662
  thvar_t x, y;
4663

4664
  assert(ge->arity == 2);
4665

4666
  x = internalize_to_bv(ctx, ge->arg[0]);
909✔
4667
  y = internalize_to_bv(ctx, ge->arg[1]);
909✔
4668
  ctx->bv.assert_ge_axiom(ctx->bv_solver, x,  y, tt);
909✔
4669
}
909✔
4670

4671
static void assert_toplevel_bvsge(context_t *ctx, composite_term_t *sge, bool tt) {
429✔
4672
  thvar_t x, y;
4673

4674
  assert(sge->arity == 2);
4675

4676
  x = internalize_to_bv(ctx, sge->arg[0]);
429✔
4677
  y = internalize_to_bv(ctx, sge->arg[1]);
429✔
4678
  ctx->bv.assert_sge_axiom(ctx->bv_solver, x,  y, tt);
429✔
4679
}
429✔
4680

4681

4682

4683
/*
4684
 * Top-level formula t:
4685
 * - t is a boolean term (or the negation of a boolean term)
4686
 * - t must be a root in the internalization table and must be mapped to true
4687
 */
4688
static void assert_toplevel_formula(context_t *ctx, term_t t) {
90,579✔
4689
  term_table_t *terms;
4690
  int32_t code;
4691
  bool tt;
4692

4693
  assert(is_boolean_term(ctx->terms, t) &&
4694
         intern_tbl_is_root(&ctx->intern, t) &&
4695
         term_is_true(ctx, t));
4696

4697
  tt = is_pos_term(t);
90,579✔
4698
  t = unsigned_term(t);
90,579✔
4699

4700
  /*
4701
   * Now: t is a root and has positive polarity
4702
   * - tt indicates whether we assert t or (not t):
4703
   *   tt true: assert t
4704
   *   tt false: assert (not t)
4705
   */
4706
  terms = ctx->terms;
90,579✔
4707
  switch (term_kind(terms, t)) {
90,579✔
4708
  case CONSTANT_TERM:
×
4709
  case UNINTERPRETED_TERM:
4710
    // should be eliminated by flattening
4711
    code = INTERNAL_ERROR;
×
4712
    goto abort;
×
4713

4714
  case ITE_TERM:
68✔
4715
  case ITE_SPECIAL:
4716
    assert_toplevel_ite(ctx, ite_term_desc(terms, t), tt);
68✔
4717
    break;
68✔
4718

4719
  case OR_TERM:
32,397✔
4720
    assert_toplevel_or(ctx, or_term_desc(terms, t), tt);
32,397✔
4721
    break;
32,397✔
4722

4723
  case XOR_TERM:
7✔
4724
    assert_toplevel_xor(ctx, xor_term_desc(terms, t), tt);
7✔
4725
    break;
7✔
4726

4727
  case EQ_TERM:
5,115✔
4728
    assert_toplevel_eq(ctx, eq_term_desc(terms, t), tt);
5,115✔
4729
    break;
5,113✔
4730

4731
  case ARITH_IS_INT_ATOM:
×
4732
    assert_toplevel_arith_is_int(ctx, arith_is_int_arg(terms, t), tt);
×
4733
    break;
×
4734

4735
  case ARITH_EQ_ATOM:
2,262✔
4736
    assert_toplevel_arith_eq(ctx, arith_eq_arg(terms, t), tt);
2,262✔
4737
    break;
2,261✔
4738

4739
  case ARITH_GE_ATOM:
7,841✔
4740
    assert_toplevel_arith_geq(ctx, arith_ge_arg(terms, t), tt);
7,841✔
4741
    break;
7,840✔
4742

4743
  case ARITH_BINEQ_ATOM:
3,200✔
4744
    assert_toplevel_arith_bineq(ctx, arith_bineq_atom_desc(terms, t), tt);
3,200✔
4745
    break;
3,200✔
4746

4747
  case ARITH_DIVIDES_ATOM:
×
4748
    assert_toplevel_arith_divides(ctx, arith_divides_atom_desc(terms, t), tt);
×
4749
    break;
×
4750

4751
  case APP_TERM:
250✔
4752
    assert_toplevel_apply(ctx, app_term_desc(terms, t), tt);
250✔
4753
    break;
250✔
4754

4755
  case SELECT_TERM:
×
4756
    assert_toplevel_select(ctx, select_term_desc(terms, t), tt);
×
4757
    break;
×
4758

4759
  case DISTINCT_TERM:
172✔
4760
    assert_toplevel_distinct(ctx, distinct_term_desc(terms, t), tt);
172✔
4761
    break;
170✔
4762

4763
  case VARIABLE:
×
4764
    code = FREE_VARIABLE_IN_FORMULA;
×
4765
    goto abort;
×
4766

4767
  case FORALL_TERM:
×
4768
    if (context_in_strict_mode(ctx)) {
×
4769
      code = QUANTIFIERS_NOT_SUPPORTED;
×
4770
      goto abort;
×
4771
    }
4772
    break;
×
4773

4774
  case BIT_TERM:
30,794✔
4775
    assert_toplevel_bit_select(ctx, bit_term_desc(terms, t), tt);
30,794✔
4776
    break;
30,794✔
4777

4778
  case BV_EQ_ATOM:
7,141✔
4779
    assert_toplevel_bveq(ctx, bveq_atom_desc(terms, t), tt);
7,141✔
4780
    break;
7,133✔
4781

4782
  case BV_GE_ATOM:
907✔
4783
    assert_toplevel_bvge(ctx, bvge_atom_desc(terms, t), tt);
907✔
4784
    break;
907✔
4785

4786
  case BV_SGE_ATOM:
425✔
4787
    assert_toplevel_bvsge(ctx, bvsge_atom_desc(terms, t), tt);
425✔
4788
    break;
425✔
4789

4790
  default:
×
4791
    code = INTERNAL_ERROR;
×
4792
    goto abort;
×
4793
  }
4794

4795
  return;
90,565✔
4796

4797
 abort:
×
4798
  longjmp(ctx->env, code);
×
4799
}
4800

4801

4802

4803
/*
4804
 * Assert (t == tt) for a boolean term t:
4805
 * - if t is not internalized, record the mapping
4806
 *   (root t) --> tt in the internalization table
4807
 */
4808
static void assert_term(context_t *ctx, term_t t, bool tt) {
998✔
4809
  term_table_t *terms;
4810
  int32_t code;
4811

4812
  assert(is_boolean_term(ctx->terms, t));
4813

4814
  /*
4815
   * Apply substitution + fix polarity
4816
   */
4817
  t = intern_tbl_get_root(&ctx->intern, t);
998✔
4818
  tt ^= is_neg_term(t);
998✔
4819
  t = unsigned_term(t);
998✔
4820

4821
  if (intern_tbl_root_is_mapped(&ctx->intern, t)) {
998✔
4822
    /*
4823
     * The root is already mapped:
4824
     * Either t is already internalized, or it occurs in
4825
     * one of the vectors top_eqs, top_atoms, top_formulas
4826
     * and it will be internalized/asserted later.
4827
     */
4828
    code = intern_tbl_map_of_root(&ctx->intern, t);
171✔
4829
    assert_internalization_code(ctx, code, tt);
171✔
4830

4831
  } else {
4832
    // store the mapping t --> tt
4833
    intern_tbl_map_root(&ctx->intern, t, bool2code(tt));
827✔
4834

4835
    // internalize and assert
4836
    terms = ctx->terms;
827✔
4837
    switch (term_kind(terms, t)) {
827✔
4838
    case CONSTANT_TERM:
×
4839
      // should always be internalized
4840
      code = INTERNAL_ERROR;
×
4841
      goto abort;
×
4842

4843
    case UNINTERPRETED_TERM:
1✔
4844
      // nothing to do: t --> true/false in the internalization table
4845
      break;
1✔
4846

4847
    case ITE_TERM:
2✔
4848
    case ITE_SPECIAL:
4849
      assert_toplevel_ite(ctx, ite_term_desc(terms, t), tt);
2✔
4850
      break;
2✔
4851

4852
    case OR_TERM:
31✔
4853
      assert_toplevel_or(ctx, or_term_desc(terms, t), tt);
31✔
4854
      break;
31✔
4855

4856
    case XOR_TERM:
×
4857
      assert_toplevel_xor(ctx, xor_term_desc(terms, t), tt);
×
4858
      break;
×
4859

4860
    case EQ_TERM:
11✔
4861
      assert_toplevel_eq(ctx, eq_term_desc(terms, t), tt);
11✔
4862
      break;
11✔
4863

4864
    case ARITH_IS_INT_ATOM:
×
4865
      assert_toplevel_arith_is_int(ctx, arith_is_int_arg(terms, t), tt);
×
4866
      break;
×
4867

4868
    case ARITH_EQ_ATOM:
4✔
4869
      assert_toplevel_arith_eq(ctx, arith_eq_arg(terms, t), tt);
4✔
4870
      break;
4✔
4871

4872
    case ARITH_GE_ATOM:
7✔
4873
      assert_toplevel_arith_geq(ctx, arith_ge_arg(terms, t), tt);
7✔
4874
      break;
7✔
4875

4876
    case ARITH_BINEQ_ATOM:
181✔
4877
      assert_toplevel_arith_bineq(ctx, arith_bineq_atom_desc(terms, t), tt);
181✔
4878
      break;
181✔
4879

4880
    case ARITH_DIVIDES_ATOM:
×
4881
      assert_toplevel_arith_divides(ctx, arith_divides_atom_desc(terms, t), tt);
×
4882
      break;
×
4883

4884
    case APP_TERM:
2✔
4885
      assert_toplevel_apply(ctx, app_term_desc(terms, t), tt);
2✔
4886
      break;
2✔
4887

4888
    case SELECT_TERM:
×
4889
      assert_toplevel_select(ctx, select_term_desc(terms, t), tt);
×
4890
      break;
×
4891

4892
    case DISTINCT_TERM:
×
4893
      assert_toplevel_distinct(ctx, distinct_term_desc(terms, t), tt);
×
4894
      break;
×
4895

4896
    case VARIABLE:
×
4897
      code = FREE_VARIABLE_IN_FORMULA;
×
4898
      goto abort;
×
4899

4900
    case FORALL_TERM:
×
4901
      if (context_in_strict_mode(ctx)) {
×
4902
        code = QUANTIFIERS_NOT_SUPPORTED;
×
4903
        goto abort;
×
4904
      }
4905
      break;
×
4906

4907
    case BIT_TERM:
543✔
4908
      assert_toplevel_bit_select(ctx, bit_term_desc(terms, t), tt);
543✔
4909
      break;
542✔
4910

4911
    case BV_EQ_ATOM:
39✔
4912
      assert_toplevel_bveq(ctx, bveq_atom_desc(terms, t), tt);
39✔
4913
      break;
39✔
4914

4915
    case BV_GE_ATOM:
2✔
4916
      assert_toplevel_bvge(ctx, bvge_atom_desc(terms, t), tt);
2✔
4917
      break;
2✔
4918

4919
    case BV_SGE_ATOM:
4✔
4920
      assert_toplevel_bvsge(ctx, bvsge_atom_desc(terms, t), tt);
4✔
4921
      break;
4✔
4922

4923
    default:
×
4924
      code = INTERNAL_ERROR;
×
4925
      goto abort;
×
4926
    }
4927
  }
4928

4929
  return;
993✔
4930

4931
 abort:
×
4932
  longjmp(ctx->env, code);
×
4933
}
4934

4935

4936

4937

4938
/************************
4939
 *  PARAMETERS/OPTIONS  *
4940
 ***********************/
4941

4942
/*
4943
 * Map architecture id to theories word
4944
 */
4945
static const uint32_t arch2theories[NUM_ARCH] = {
4946
  0,                           //  CTX_ARCH_NOSOLVERS --> empty theory
4947

4948
  UF_MASK,                     //  CTX_ARCH_EG
4949
  ARITH_MASK,                  //  CTX_ARCH_SPLX
4950
  IDL_MASK,                    //  CTX_ARCH_IFW
4951
  RDL_MASK,                    //  CTX_ARCH_RFW
4952
  BV_MASK,                     //  CTX_ARCH_BV
4953
  UF_MASK|FUN_MASK,            //  CTX_ARCH_EGFUN
4954
  UF_MASK|ARITH_MASK,          //  CTX_ARCH_EGSPLX
4955
  UF_MASK|BV_MASK,             //  CTX_ARCH_EGBV
4956
  UF_MASK|ARITH_MASK|FUN_MASK, //  CTX_ARCH_EGFUNSPLX
4957
  UF_MASK|BV_MASK|FUN_MASK,    //  CTX_ARCH_EGFUNBV
4958
  UF_MASK|BV_MASK|ARITH_MASK,  //  CTX_ARCH_EGSPLXBV
4959
  ALLTH_MASK,                  //  CTX_ARCH_EGFUNSPLXBV
4960

4961
  IDL_MASK,                    //  CTX_ARCH_AUTO_IDL
4962
  RDL_MASK,                    //  CTX_ARCH_AUTO_RDL
4963

4964
  UF_MASK|ARITH_MASK|FUN_MASK  //  CTX_ARCH_MCSAT
4965
};
4966

4967

4968
/*
4969
 * Each architecture has a fixed set of solver components:
4970
 * - the set of components is stored as a bit vector (on 8bits)
4971
 * - this uses the following bit-masks
4972
 * For the AUTO_xxx architecture, nothing is required initially,
4973
 * so the bitmask is 0.
4974
 */
4975
#define EGRPH  0x1
4976
#define SPLX   0x2
4977
#define IFW    0x4
4978
#define RFW    0x8
4979
#define BVSLVR 0x10
4980
#define FSLVR  0x20
4981
#define MCSAT  0x40
4982

4983
static const uint8_t arch_components[NUM_ARCH] = {
4984
  0,                        //  CTX_ARCH_NOSOLVERS
4985

4986
  EGRPH,                    //  CTX_ARCH_EG
4987
  SPLX,                     //  CTX_ARCH_SPLX
4988
  IFW,                      //  CTX_ARCH_IFW
4989
  RFW,                      //  CTX_ARCH_RFW
4990
  BVSLVR,                   //  CTX_ARCH_BV
4991
  EGRPH|FSLVR,              //  CTX_ARCH_EGFUN
4992
  EGRPH|SPLX,               //  CTX_ARCH_EGSPLX
4993
  EGRPH|BVSLVR,             //  CTX_ARCH_EGBV
4994
  EGRPH|SPLX|FSLVR,         //  CTX_ARCH_EGFUNSPLX
4995
  EGRPH|BVSLVR|FSLVR,       //  CTX_ARCH_EGFUNBV
4996
  EGRPH|SPLX|BVSLVR,        //  CTX_ARCH_EGSPLXBV
4997
  EGRPH|SPLX|BVSLVR|FSLVR,  //  CTX_ARCH_EGFUNSPLXBV
4998

4999
  0,                        //  CTX_ARCH_AUTO_IDL
5000
  0,                        //  CTX_ARCH_AUTO_RDL
5001

5002
  MCSAT                     //  CTX_ARCH_MCSAT
5003
};
5004

5005

5006
/*
5007
 * Smt mode for a context mode
5008
 */
5009
static const smt_mode_t core_mode[NUM_MODES] = {
5010
  SMT_MODE_BASIC,       // one check
5011
  SMT_MODE_BASIC,       // multichecks
5012
  SMT_MODE_PUSHPOP,     // push/pop
5013
  SMT_MODE_INTERACTIVE, // interactive
5014
};
5015

5016

5017
/*
5018
 * Flags for a context mode
5019
 */
5020
static const uint32_t mode2options[NUM_MODES] = {
5021
  0,
5022
  MULTICHECKS_OPTION_MASK,
5023
  MULTICHECKS_OPTION_MASK|PUSHPOP_OPTION_MASK,
5024
  MULTICHECKS_OPTION_MASK|PUSHPOP_OPTION_MASK|CLEANINT_OPTION_MASK,
5025
};
5026

5027

5028

5029

5030

5031

5032
/*
5033
 * SIMPLEX OPTIONS
5034
 */
5035

5036
/*
5037
 * Which version of the arithmetic solver is present
5038
 */
5039
bool context_has_idl_solver(context_t *ctx) {
8✔
5040
  uint8_t solvers;
5041
  solvers = arch_components[ctx->arch];
8✔
5042
  return ctx->arith_solver != NULL && (solvers & IFW);
8✔
5043
}
5044

5045
bool context_has_rdl_solver(context_t *ctx) {
×
5046
  uint8_t solvers;
5047
  solvers = arch_components[ctx->arch];
×
5048
  return ctx->arith_solver != NULL && (solvers & RFW);
×
5049
}
5050

5051
bool context_has_simplex_solver(context_t *ctx) {
23,110✔
5052
  uint8_t solvers;
5053
  solvers = arch_components[ctx->arch];
23,110✔
5054
  return ctx->arith_solver != NULL && (solvers & SPLX);
23,110✔
5055
}
5056

5057

5058
/*
5059
 * If the simplex solver already exists, the options are propagated.
5060
 * Otherwise, they are recorded into the option flags. They will
5061
 * be set up when the simplex solver is created.
5062
 */
5063
void enable_splx_eager_lemmas(context_t *ctx) {
167✔
5064
  ctx->options |= SPLX_EGRLMAS_OPTION_MASK;
167✔
5065
  if (context_has_simplex_solver(ctx)) {
167✔
5066
    simplex_enable_eager_lemmas(ctx->arith_solver);
167✔
5067
  }
5068
}
167✔
5069

5070
void disable_splx_eager_lemmas(context_t *ctx) {
×
5071
  ctx->options &= ~SPLX_EGRLMAS_OPTION_MASK;
×
5072
  if (context_has_simplex_solver(ctx)) {
×
5073
    simplex_disable_eager_lemmas(ctx->arith_solver);
×
5074
  }
5075
}
×
5076

5077

5078
void enable_splx_periodic_icheck(context_t *ctx) {
120✔
5079
  ctx->options |= SPLX_ICHECK_OPTION_MASK;
120✔
5080
  if (context_has_simplex_solver(ctx)) {
120✔
5081
    simplex_enable_periodic_icheck(ctx->arith_solver);
120✔
5082
  }
5083
}
120✔
5084

5085
void disable_splx_periodic_icheck(context_t *ctx) {
×
5086
  ctx->options &= ~SPLX_ICHECK_OPTION_MASK;
×
5087
  if (context_has_simplex_solver(ctx)) {
×
5088
    simplex_disable_periodic_icheck(ctx->arith_solver);
×
5089
  }
5090
}
×
5091

5092
void enable_splx_eqprop(context_t *ctx) {
72✔
5093
  ctx->options |= SPLX_EQPROP_OPTION_MASK;
72✔
5094
  if (context_has_simplex_solver(ctx)) {
72✔
5095
    simplex_enable_eqprop(ctx->arith_solver);
72✔
5096
  }
5097
}
72✔
5098

5099
void disable_splx_eqprop(context_t *ctx) {
×
5100
  ctx->options &= ~SPLX_EQPROP_OPTION_MASK;
×
5101
  if (context_has_simplex_solver(ctx)) {
×
5102
    simplex_disable_eqprop(ctx->arith_solver);
×
5103
  }
5104
}
×
5105

5106

5107

5108

5109
/******************
5110
 *  EMPTY SOLVER  *
5111
 *****************/
5112

5113
/*
5114
 * We need an empty theory solver for initializing
5115
 * the core if the architecture is NOSOLVERS.
5116
 */
5117
static void donothing(void *solver) {
×
5118
}
×
5119

5120
static void null_backtrack(void *solver, uint32_t backlevel) {
×
5121
}
×
5122

5123
static bool null_propagate(void *solver) {
×
5124
  return true;
×
5125
}
5126

5127
static fcheck_code_t null_final_check(void *solver) {
×
5128
  return FCHECK_SAT;
×
5129
}
5130

5131
static th_ctrl_interface_t null_ctrl = {
5132
  donothing,        // start_internalization
5133
  donothing,        // start_search
5134
  null_propagate,   // propagate
5135
  null_final_check, // final check
5136
  donothing,        // increase_decision_level
5137
  null_backtrack,   // backtrack
5138
  donothing,        // push
5139
  donothing,        // pop
5140
  donothing,        // reset
5141
  donothing,        // clear
5142
};
5143

5144

5145
// for the smt interface, nothing should be called since there are no atoms
5146
static th_smt_interface_t null_smt = {
5147
  NULL, NULL, NULL, NULL, NULL,
5148
};
5149

5150

5151

5152

5153
/****************************
5154
 *  ARCHITECTURE & SOLVERS  *
5155
 ***************************/
5156

5157
/*
5158
 * Check whether a given architecture includes a specific solver
5159
 */
5160
bool context_arch_has_egraph(context_arch_t arch) {
416✔
5161
  return arch_components[arch] & EGRPH;
416✔
5162
}
5163

5164
bool context_arch_has_bv(context_arch_t arch) {
×
5165
  return arch_components[arch] & BVSLVR;
×
5166
}
5167

5168
bool context_arch_has_fun(context_arch_t arch) {
×
5169
  return arch_components[arch] & FSLVR;
×
5170
}
5171

5172
bool context_arch_has_arith(context_arch_t arch) {
×
5173
  return arch_components[arch] & (SPLX|IFW|RFW);
×
5174
}
5175

5176
bool context_arch_has_mcsat(context_arch_t arch) {
×
5177
  return arch_components[arch] & MCSAT;
×
5178
}
5179

5180
bool context_arch_has_simplex(context_arch_t arch) {
×
5181
  return arch_components[arch] & SPLX;
×
5182
}
5183

5184
bool context_arch_has_ifw(context_arch_t arch) {
×
5185
  return arch_components[arch] & IFW;
×
5186
}
5187

5188
bool context_arch_has_rfw(context_arch_t arch) {
×
5189
  return arch_components[arch] & RFW;
×
5190
}
5191

5192

5193
/****************************
5194
 *  SOLVER INITIALIZATION   *
5195
 ***************************/
5196

5197
/*
5198
 * Create and initialize the egraph
5199
 * - the core must be created first
5200
 */
5201
static void create_egraph(context_t *ctx) {
4,773✔
5202
  egraph_t *egraph;
5203

5204
  assert(ctx->egraph == NULL);
5205

5206
  egraph = (egraph_t *) safe_malloc(sizeof(egraph_t));
4,773✔
5207
  init_egraph(egraph, ctx->types);
4,773✔
5208
  ctx->egraph = egraph;
4,773✔
5209
}
4,773✔
5210

5211

5212
/*
5213
 * Create and initialize the mcsat solver
5214
 */
5215
static void create_mcsat(context_t *ctx) {
×
5216
  assert(ctx->mcsat == NULL);
5217
  ctx->mcsat = mcsat_new(ctx);
×
5218
}
×
5219

5220

5221

5222
/*
5223
 * Create and initialize the idl solver and attach it to the core
5224
 * - there must be no other solvers and no egraph
5225
 * - if automatic is true, attach the solver to the core, otherwise
5226
 *   initialize the core
5227
 * - copy the solver's internalization interface into arith
5228
 */
5229
static void create_idl_solver(context_t *ctx, bool automatic) {
26✔
5230
  idl_solver_t *solver;
5231
  smt_mode_t cmode;
5232

5233
  assert(ctx->egraph == NULL && ctx->arith_solver == NULL && ctx->bv_solver == NULL &&
5234
         ctx->fun_solver == NULL && ctx->core != NULL);
5235

5236
  cmode = core_mode[ctx->mode];
26✔
5237
  solver = (idl_solver_t *) safe_malloc(sizeof(idl_solver_t));
26✔
5238
  init_idl_solver(solver, ctx->core, &ctx->gate_manager);
26✔
5239
  if (automatic) {
26✔
5240
    smt_core_reset_thsolver(ctx->core, solver, idl_ctrl_interface(solver),
26✔
5241
                            idl_smt_interface(solver));
5242
  } else {
5243
    init_smt_core(ctx->core, CTX_DEFAULT_CORE_SIZE, solver, idl_ctrl_interface(solver),
×
5244
                  idl_smt_interface(solver), cmode);
5245
  }
5246
  idl_solver_init_jmpbuf(solver, &ctx->env);
26✔
5247
  ctx->arith_solver = solver;
26✔
5248
  ctx->arith = *idl_arith_interface(solver);
26✔
5249
}
26✔
5250

5251

5252
/*
5253
 * Create and initialize the rdl solver and attach it to the core.
5254
 * - there must be no other solvers and no egraph
5255
 * - if automatic is true, attach rdl to the core, otherwise
5256
 *   initialize the core
5257
 * - copy the solver's internalization interface in ctx->arith
5258
 */
5259
static void create_rdl_solver(context_t *ctx, bool automatic) {
8✔
5260
  rdl_solver_t *solver;
5261
  smt_mode_t cmode;
5262

5263
  assert(ctx->egraph == NULL && ctx->arith_solver == NULL && ctx->bv_solver == NULL &&
5264
         ctx->fun_solver == NULL && ctx->core != NULL);
5265

5266
  cmode = core_mode[ctx->mode];
8✔
5267
  solver = (rdl_solver_t *) safe_malloc(sizeof(rdl_solver_t));
8✔
5268
  init_rdl_solver(solver, ctx->core, &ctx->gate_manager);
8✔
5269
  if (automatic) {
8✔
5270
    smt_core_reset_thsolver(ctx->core, solver, rdl_ctrl_interface(solver),
8✔
5271
                            rdl_smt_interface(solver));
5272
  } else {
5273
    init_smt_core(ctx->core, CTX_DEFAULT_CORE_SIZE, solver, rdl_ctrl_interface(solver),
×
5274
                  rdl_smt_interface(solver), cmode);
5275
  }
5276
  rdl_solver_init_jmpbuf(solver, &ctx->env);
8✔
5277
  ctx->arith_solver = solver;
8✔
5278
  ctx->arith = *rdl_arith_interface(solver);
8✔
5279
}
8✔
5280

5281

5282
/*
5283
 * Create an initialize the simplex solver and attach it to the core
5284
 * or to the egraph if the egraph exists.
5285
 * - if automatic is true, this is part of auto_idl or auto_rdl. So the
5286
 *   core is already initialized.
5287
 */
5288
static void create_simplex_solver(context_t *ctx, bool automatic) {
4,885✔
5289
  simplex_solver_t *solver;
5290
  smt_mode_t cmode;
5291

5292
  assert(ctx->arith_solver == NULL && ctx->core != NULL);
5293

5294
  cmode = core_mode[ctx->mode];
4,885✔
5295
  solver = (simplex_solver_t *) safe_malloc(sizeof(simplex_solver_t));
4,885✔
5296
  init_simplex_solver(solver, ctx->core, &ctx->gate_manager, ctx->egraph);
4,885✔
5297

5298
  // set simplex options
5299
  if (splx_eager_lemmas_enabled(ctx)) {
4,885✔
5300
    simplex_enable_eager_lemmas(solver);
×
5301
  }
5302
  if (splx_periodic_icheck_enabled(ctx)) {
4,885✔
5303
    simplex_enable_periodic_icheck(solver);
×
5304
  }
5305
  if (splx_eqprop_enabled(ctx)) {
4,885✔
5306
    simplex_enable_eqprop(solver);
×
5307
  }
5308

5309
  // row saving must be enabled unless we're in ONECHECK mode
5310
  if (ctx->mode != CTX_MODE_ONECHECK) {
4,885✔
5311
    simplex_enable_row_saving(solver);
4,741✔
5312
  }
5313

5314
  if (ctx->egraph != NULL) {
4,885✔
5315
    // attach the simplex solver as a satellite solver to the egraph
5316
    egraph_attach_arithsolver(ctx->egraph, solver, simplex_ctrl_interface(solver),
4,320✔
5317
                              simplex_smt_interface(solver), simplex_egraph_interface(solver),
5318
                              simplex_arith_egraph_interface(solver));
5319
  } else if (!automatic) {
565✔
5320
    // attach simplex to the core and initialize the core
5321
    init_smt_core(ctx->core, CTX_DEFAULT_CORE_SIZE, solver, simplex_ctrl_interface(solver),
564✔
5322
                  simplex_smt_interface(solver), cmode);
5323
  } else {
5324
    // the core is already initialized: attach simplex
5325
    smt_core_reset_thsolver(ctx->core, solver, simplex_ctrl_interface(solver),
1✔
5326
                            simplex_smt_interface(solver));
5327
  }
5328

5329
  simplex_solver_init_jmpbuf(solver, &ctx->env);
4,885✔
5330
  ctx->arith_solver = solver;
4,885✔
5331
  ctx->arith = *simplex_arith_interface(solver);
4,885✔
5332
}
4,885✔
5333

5334

5335
/*
5336
 * Create IDL/SIMPLEX solver based on ctx->dl_profile
5337
 */
5338
static void create_auto_idl_solver(context_t *ctx) {
27✔
5339
  dl_data_t *profile;
5340
  int32_t bound;
5341
  double atom_density;
5342

5343
  assert(ctx->dl_profile != NULL);
5344
  profile = ctx->dl_profile;
27✔
5345

5346
  if (q_is_smallint(&profile->path_bound)) {
27✔
5347
    bound = q_get_smallint(&profile->path_bound);
26✔
5348
  } else {
5349
    bound = INT32_MAX;
1✔
5350
  }
5351

5352
  if (bound >= 1073741824) {
27✔
5353
    // simplex required because of arithmetic overflow
5354
    create_simplex_solver(ctx, true);
1✔
5355
    ctx->arch = CTX_ARCH_SPLX;
1✔
5356
  } else if (profile->num_vars >= 1000) {
26✔
5357
    // too many variables for FW
5358
    create_simplex_solver(ctx, true);
×
5359
    ctx->arch = CTX_ARCH_SPLX;
×
5360
  } else if (profile->num_vars <= 200 || profile->num_eqs == 0) {
26✔
5361
    // use FW for now, until we've tested SIMPLEX more
5362
    // 0 equalities usually means a scheduling problem
5363
    // --flatten works better on IDL/FW
5364
    create_idl_solver(ctx, true);
26✔
5365
    ctx->arch = CTX_ARCH_IFW;
26✔
5366
    enable_diseq_and_or_flattening(ctx);
26✔
5367

5368
  } else {
5369

5370
    // problem density
5371
    if (profile->num_vars > 0) {
×
5372
      atom_density = ((double) profile->num_atoms)/profile->num_vars;
×
5373
    } else {
5374
      atom_density = 0;
×
5375
    }
5376

5377
    if (atom_density >= 10.0) {
×
5378
      // high density: use FW
5379
      create_idl_solver(ctx, true);
×
5380
      ctx->arch = CTX_ARCH_IFW;
×
5381
      enable_diseq_and_or_flattening(ctx);
×
5382
    } else {
5383
      create_simplex_solver(ctx, true);
×
5384
      ctx->arch = CTX_ARCH_SPLX;
×
5385
    }
5386
  }
5387
}
27✔
5388

5389

5390
/*
5391
 * Create RDL/SIMPLEX solver based on ctx->dl_profile
5392
 */
5393
static void create_auto_rdl_solver(context_t *ctx) {
8✔
5394
  dl_data_t *profile;
5395
  double atom_density;
5396

5397
  assert(ctx->dl_profile != NULL);
5398
  profile = ctx->dl_profile;
8✔
5399

5400
  if (profile->num_vars >= 1000) {
8✔
5401
    create_simplex_solver(ctx, true);
×
5402
    ctx->arch = CTX_ARCH_SPLX;
×
5403
  } else if (profile->num_vars <= 200 || profile->num_eqs == 0) {
8✔
5404
    create_rdl_solver(ctx, true);
8✔
5405
    ctx->arch = CTX_ARCH_RFW;
8✔
5406
  } else {
5407
    // problem density
5408
    if (profile->num_vars > 0) {
×
5409
      atom_density = ((double) profile->num_atoms)/profile->num_vars;
×
5410
    } else {
5411
      atom_density = 0;
×
5412
    }
5413

5414
    if (atom_density >= 7.0) {
×
5415
      // high density: use FW
5416
      create_rdl_solver(ctx, true);
×
5417
      ctx->arch = CTX_ARCH_RFW;
×
5418
    } else {
5419
      // low-density: use SIMPLEX
5420
      create_simplex_solver(ctx, true);
×
5421
      ctx->arch = CTX_ARCH_SPLX;
×
5422
    }
5423
  }
5424
}
8✔
5425

5426

5427

5428
/*
5429
 * Create the bitvector solver
5430
 * - attach it to the egraph if there's an egraph
5431
 * - attach it to the core and initialize the core otherwise
5432
 */
5433
static void create_bv_solver(context_t *ctx) {
7,135✔
5434
  bv_solver_t *solver;
5435
  smt_mode_t cmode;
5436

5437
  assert(ctx->bv_solver == NULL && ctx->core != NULL);
5438

5439
  cmode = core_mode[ctx->mode];
7,135✔
5440
  solver = (bv_solver_t *) safe_malloc(sizeof(bv_solver_t));
7,135✔
5441
  init_bv_solver(solver, ctx->core, ctx->egraph);
7,135✔
5442

5443
  if (ctx->egraph != NULL) {
7,135✔
5444
    // attach as a satellite to the egraph
5445
    egraph_attach_bvsolver(ctx->egraph, solver, bv_solver_ctrl_interface(solver),
4,406✔
5446
                           bv_solver_smt_interface(solver), bv_solver_egraph_interface(solver),
5447
                           bv_solver_bv_egraph_interface(solver));
5448
  } else {
5449
    // attach to the core and initialize the core
5450
    init_smt_core(ctx->core, CTX_DEFAULT_CORE_SIZE, solver, bv_solver_ctrl_interface(solver),
2,729✔
5451
                  bv_solver_smt_interface(solver), cmode);
5452
  }
5453

5454
  // EXPERIMENT
5455
  //  smt_core_make_etable(ctx->core);
5456
  // END
5457

5458
  bv_solver_init_jmpbuf(solver, &ctx->env);
7,135✔
5459
  ctx->bv_solver = solver;
7,135✔
5460
  ctx->bv = *bv_solver_bv_interface(solver);
7,135✔
5461
}
7,135✔
5462

5463

5464
/*
5465
 * Create the array/function theory solver and attach it to the egraph
5466
 */
5467
static void create_fun_solver(context_t *ctx) {
4,413✔
5468
  fun_solver_t *solver;
5469

5470
  assert(ctx->egraph != NULL && ctx->fun_solver == NULL);
5471

5472
  solver = (fun_solver_t *) safe_malloc(sizeof(fun_solver_t));
4,413✔
5473
  init_fun_solver(solver, ctx->core, &ctx->gate_manager, ctx->egraph, ctx->types);
4,413✔
5474
  egraph_attach_funsolver(ctx->egraph, solver, fun_solver_ctrl_interface(solver),
4,413✔
5475
                          fun_solver_egraph_interface(solver),
5476
                          fun_solver_fun_egraph_interface(solver));
5477

5478
  ctx->fun_solver = solver;
4,413✔
5479
}
4,413✔
5480

5481

5482
/*
5483
 * Allocate and initialize solvers based on architecture and mode
5484
 * - core and gate manager must exist at this point
5485
 * - if the architecture is either AUTO_IDL or AUTO_RDL, no theory solver
5486
 *   is allocated yet, and the core is initialized for Boolean only
5487
 * - otherwise, all components are ready and initialized, including the core.
5488
 */
5489
static void init_solvers(context_t *ctx) {
8,102✔
5490
  uint8_t solvers;
5491
  smt_core_t *core;
5492
  smt_mode_t cmode;
5493
  egraph_t *egraph;
5494

5495
  solvers = arch_components[ctx->arch];
8,102✔
5496

5497
  ctx->egraph = NULL;
8,102✔
5498
  ctx->arith_solver = NULL;
8,102✔
5499
  ctx->bv_solver = NULL;
8,102✔
5500
  ctx->fun_solver = NULL;
8,102✔
5501
  ctx->quant_solver = NULL;
8,102✔
5502

5503
  // Create egraph first, then satellite solvers
5504
  if (solvers & EGRPH) {
8,102✔
5505
    create_egraph(ctx);
4,773✔
5506
  }
5507

5508
  // Create mcsat
5509
  if (solvers & MCSAT) {
8,102✔
5510
    create_mcsat(ctx);
×
5511
  }
5512

5513
  // Arithmetic solver
5514
  if (solvers & SPLX) {
8,102✔
5515
    create_simplex_solver(ctx, false);
4,884✔
5516
  } else if (solvers & IFW) {
3,218✔
5517
    create_idl_solver(ctx, false);
×
5518
  } else if (solvers & RFW) {
3,218✔
5519
    create_rdl_solver(ctx, false);
×
5520
  }
5521

5522
  // Bitvector solver
5523
  if (solvers & BVSLVR) {
8,102✔
5524
    create_bv_solver(ctx);
7,135✔
5525
  }
5526

5527
  // Array solver
5528
  if (solvers & FSLVR) {
8,102✔
5529
    create_fun_solver(ctx);
4,413✔
5530
  }
5531

5532
  /*
5533
   * At this point all solvers are ready and initialized, except the
5534
   * egraph and core if the egraph is present or the core if there are
5535
   * no solvers, or if arch is AUTO_IDL or AUTO_RDL.
5536
   */
5537
  cmode = core_mode[ctx->mode];   // initialization mode for the core
8,102✔
5538
  egraph = ctx->egraph;
8,102✔
5539
  core = ctx->core;
8,102✔
5540
  if (egraph != NULL) {
8,102✔
5541
    init_smt_core(core, CTX_DEFAULT_CORE_SIZE, egraph, egraph_ctrl_interface(egraph),
4,773✔
5542
                  egraph_smt_interface(egraph), cmode);
5543
    egraph_attach_core(egraph, core);
4,773✔
5544

5545
  } else if (solvers == 0) {
3,329✔
5546
    /*
5547
     * Boolean solver only. If arch if AUTO_IDL or AUTO_RDL, the
5548
     * theory solver will be changed later by create_auto_idl_solver
5549
     * or create_auto_rdl_solver.
5550
     */
5551
    assert(ctx->arith_solver == NULL && ctx->bv_solver == NULL && ctx->fun_solver == NULL);
5552
    init_smt_core(core, CTX_DEFAULT_CORE_SIZE, NULL, &null_ctrl, &null_smt, cmode);
36✔
5553
  } else if (solvers == MCSAT) {
3,293✔
5554
    /*
5555
     * MCsat solver only, we create the core, but never use it.
5556
     */
5557
    assert(ctx->egraph == NULL && ctx->arith_solver == NULL &&
5558
           ctx->bv_solver == NULL && ctx->fun_solver == NULL);
5559
    init_smt_core(core, CTX_DEFAULT_CORE_SIZE, NULL, &null_ctrl, &null_smt, cmode);
×
5560
  }
5561

5562
  /*
5563
   * Optimization: if the arch is NOSOLVERS or BV then we set bool_only in the core
5564
   */
5565
  if (ctx->arch == CTX_ARCH_NOSOLVERS || ctx->arch == CTX_ARCH_BV) {
8,102✔
5566
    smt_core_set_bool_only(core);
2,729✔
5567
  }
5568
}
8,102✔
5569

5570

5571

5572

5573
/*
5574
 * Delete the arithmetic solver
5575
 */
5576
static void delete_arith_solver(context_t *ctx) {
4,919✔
5577
  uint8_t solvers;
5578

5579
  assert(ctx->arith_solver != NULL);
5580

5581
  solvers = arch_components[ctx->arch];
4,919✔
5582
  if (solvers & IFW) {
4,919✔
5583
    delete_idl_solver(ctx->arith_solver);
26✔
5584
  } else if (solvers & RFW) {
4,893✔
5585
    delete_rdl_solver(ctx->arith_solver);
8✔
5586
  } else if (solvers & SPLX) {
4,885✔
5587
    delete_simplex_solver(ctx->arith_solver);
4,885✔
5588
  }
5589
  safe_free(ctx->arith_solver);
4,919✔
5590
  ctx->arith_solver = NULL;
4,919✔
5591
}
4,919✔
5592

5593

5594

5595

5596
/*****************************
5597
 *  CONTEXT INITIALIZATION   *
5598
 ****************************/
5599

5600
/*
5601
 * Check mode and architecture
5602
 */
5603
#ifndef NDEBUG
5604
static inline bool valid_mode(context_mode_t mode) {
5605
  return CTX_MODE_ONECHECK <= mode && mode <= CTX_MODE_INTERACTIVE;
5606
}
5607

5608
static inline bool valid_arch(context_arch_t arch) {
5609
  return CTX_ARCH_NOSOLVERS <= arch && arch <= CTX_ARCH_MCSAT;
5610
}
5611
#endif
5612

5613

5614
/*
5615
 * Initialize ctx for the given mode and architecture
5616
 * - terms = term table for that context
5617
 * - qflag = true means quantifiers allowed
5618
 * - qflag = false means no quantifiers
5619
 */
5620
void init_context(context_t *ctx, term_table_t *terms, smt_logic_t logic,
8,102✔
5621
                  context_mode_t mode, context_arch_t arch, bool qflag) {
5622
  assert(valid_mode(mode) && valid_arch(arch));
5623

5624
  /*
5625
   * Set architecture and options
5626
   */
5627
  ctx->mode = mode;
8,102✔
5628
  ctx->arch = arch;
8,102✔
5629
  ctx->logic = logic;
8,102✔
5630
  ctx->theories = arch2theories[arch];
8,102✔
5631
  ctx->options = mode2options[mode];
8,102✔
5632
  if (qflag) {
8,102✔
5633
    // quantifiers require egraph
5634
    assert((ctx->theories & UF_MASK) != 0);
5635
    ctx->theories |= QUANT_MASK;
×
5636
  }
5637

5638
  ctx->base_level = 0;
8,102✔
5639

5640
  /*
5641
   * The core is always needed: allocate it here. It's not initialized yet.
5642
   * The other solver are optionals.
5643
   *
5644
   * TODO: we could skip this when we use MCSAT (since then the core is
5645
   * not needed).
5646
   */
5647
  ctx->core = (smt_core_t *) safe_malloc(sizeof(smt_core_t));
8,102✔
5648
  ctx->egraph = NULL;
8,102✔
5649
  ctx->mcsat = NULL;
8,102✔
5650
  ctx->arith_solver = NULL;
8,102✔
5651
  ctx->bv_solver = NULL;
8,102✔
5652
  ctx->fun_solver = NULL;
8,102✔
5653
  ctx->quant_solver = NULL;
8,102✔
5654

5655
  /*
5656
   * Global tables + gate manager
5657
   */
5658
  ctx->types = terms->types;
8,102✔
5659
  ctx->terms = terms;
8,102✔
5660
  init_gate_manager(&ctx->gate_manager, ctx->core);
8,102✔
5661

5662
  /*
5663
   * Simplification/internalization support
5664
   */
5665
  init_intern_tbl(&ctx->intern, 0, terms);
8,102✔
5666
  init_ivector(&ctx->top_eqs, CTX_DEFAULT_VECTOR_SIZE);
8,102✔
5667
  init_ivector(&ctx->top_atoms, CTX_DEFAULT_VECTOR_SIZE);
8,102✔
5668
  init_ivector(&ctx->top_formulas, CTX_DEFAULT_VECTOR_SIZE);
8,102✔
5669
  init_ivector(&ctx->top_interns, CTX_DEFAULT_VECTOR_SIZE);
8,102✔
5670

5671
  /*
5672
   * Force the internalization mapping for true and false
5673
   * - true  term --> true_occ
5674
   * - false term --> false_occ
5675
   * This mapping holds even if there's no egraph.
5676
   */
5677
  intern_tbl_map_root(&ctx->intern, true_term, bool2code(true));
8,102✔
5678

5679
  /*
5680
   * Auxiliary internalization buffers
5681
   */
5682
  init_ivector(&ctx->subst_eqs, CTX_DEFAULT_VECTOR_SIZE);
8,102✔
5683
  init_ivector(&ctx->aux_eqs, CTX_DEFAULT_VECTOR_SIZE);
8,102✔
5684
  init_ivector(&ctx->aux_atoms, CTX_DEFAULT_VECTOR_SIZE);
8,102✔
5685
  init_ivector(&ctx->aux_vector, CTX_DEFAULT_VECTOR_SIZE);
8,102✔
5686
  init_int_queue(&ctx->queue, 0);
8,102✔
5687
  init_istack(&ctx->istack);
8,102✔
5688
  init_objstack(&ctx->ostack);
8,102✔
5689
  init_sharing_map(&ctx->sharing, &ctx->intern);
8,102✔
5690
  init_objstore(&ctx->cstore, sizeof(conditional_t), 32);
8,102✔
5691
  init_assumption_stack(&ctx->assumptions);
8,102✔
5692

5693
  ctx->subst = NULL;
8,102✔
5694
  ctx->marks = NULL;
8,102✔
5695
  ctx->cache = NULL;
8,102✔
5696
  ctx->small_cache = NULL;
8,102✔
5697
  ctx->edge_map = NULL;
8,102✔
5698
  ctx->eq_cache = NULL;
8,102✔
5699
  ctx->divmod_table = NULL;
8,102✔
5700
  ctx->explorer = NULL;
8,102✔
5701

5702
  ctx->dl_profile = NULL;
8,102✔
5703
  ctx->arith_buffer = NULL;
8,102✔
5704
  ctx->poly_buffer = NULL;
8,102✔
5705
  ctx->aux_poly = NULL;
8,102✔
5706
  ctx->aux_poly_size = 0;
8,102✔
5707

5708
  ctx->bvpoly_buffer = NULL;
8,102✔
5709

5710
  q_init(&ctx->aux);
8,102✔
5711
  init_bvconstant(&ctx->bv_buffer);
8,102✔
5712

5713
  ctx->trace = NULL;
8,102✔
5714

5715
  // mcsat options default
5716
  init_mcsat_options(&ctx->mcsat_options);
8,102✔
5717
  init_ivector(&ctx->mcsat_var_order, CTX_DEFAULT_VECTOR_SIZE);
8,102✔
5718
  init_ivector(&ctx->mcsat_initial_var_order, CTX_DEFAULT_VECTOR_SIZE);
8,102✔
5719
  /*
5720
   * Allocate and initialize the solvers and core
5721
   * NOTE: no theory solver yet if arch is AUTO_IDL or AUTO_RDL
5722
   */
5723
  init_solvers(ctx);
8,102✔
5724

5725
  ctx->en_quant = false;
8,102✔
5726
}
8,102✔
5727

5728

5729

5730

5731
/*
5732
 * Delete ctx
5733
 */
5734
void delete_context(context_t *ctx) {
8,102✔
5735
  if (ctx->core != NULL) {
8,102✔
5736
    delete_smt_core(ctx->core);
8,102✔
5737
    safe_free(ctx->core);
8,102✔
5738
    ctx->core = NULL;
8,102✔
5739
  }
5740

5741
  if (ctx->mcsat != NULL) {
8,102✔
5742
    mcsat_destruct(ctx->mcsat);
×
5743
    safe_free(ctx->mcsat);
×
5744
    ctx->mcsat = NULL;
×
5745
  }
5746

5747
  if (ctx->egraph != NULL) {
8,102✔
5748
    delete_egraph(ctx->egraph);
4,773✔
5749
    safe_free(ctx->egraph);
4,773✔
5750
    ctx->egraph = NULL;
4,773✔
5751
  }
5752

5753
  if (ctx->arith_solver != NULL) {
8,102✔
5754
    delete_arith_solver(ctx);
4,919✔
5755
  }
5756

5757
  if (ctx->fun_solver != NULL) {
8,102✔
5758
    delete_fun_solver(ctx->fun_solver);
4,413✔
5759
    safe_free(ctx->fun_solver);
4,413✔
5760
    ctx->fun_solver = NULL;
4,413✔
5761
  }
5762

5763
  if (ctx->quant_solver != NULL) {
8,102✔
5764
    delete_quant_solver(ctx->quant_solver);
52✔
5765
    safe_free(ctx->quant_solver);
52✔
5766
    ctx->quant_solver = NULL;
52✔
5767
  }
5768

5769
  if (ctx->bv_solver != NULL) {
8,102✔
5770
    delete_bv_solver(ctx->bv_solver);
7,135✔
5771
    safe_free(ctx->bv_solver);
7,135✔
5772
    ctx->bv_solver = NULL;
7,135✔
5773
  }
5774

5775
  delete_gate_manager(&ctx->gate_manager);
8,102✔
5776
  /* delete_mcsat_options(&ctx->mcsat_options); // if used then the same memory is freed twice */
5777
  delete_ivector(&ctx->mcsat_var_order);
8,102✔
5778
  delete_ivector(&ctx->mcsat_initial_var_order);
8,102✔
5779

5780
  delete_intern_tbl(&ctx->intern);
8,102✔
5781
  delete_ivector(&ctx->top_eqs);
8,102✔
5782
  delete_ivector(&ctx->top_atoms);
8,102✔
5783
  delete_ivector(&ctx->top_formulas);
8,102✔
5784
  delete_ivector(&ctx->top_interns);
8,102✔
5785

5786
  delete_ivector(&ctx->subst_eqs);
8,102✔
5787
  delete_ivector(&ctx->aux_eqs);
8,102✔
5788
  delete_ivector(&ctx->aux_atoms);
8,102✔
5789
  delete_ivector(&ctx->aux_vector);
8,102✔
5790
  delete_int_queue(&ctx->queue);
8,102✔
5791
  delete_istack(&ctx->istack);
8,102✔
5792
  delete_objstack(&ctx->ostack);
8,102✔
5793
  delete_sharing_map(&ctx->sharing);
8,102✔
5794
  delete_objstore(&ctx->cstore);
8,102✔
5795
  delete_assumption_stack(&ctx->assumptions);
8,102✔
5796

5797
  context_free_subst(ctx);
8,102✔
5798
  context_free_marks(ctx);
8,102✔
5799
  context_free_cache(ctx);
8,102✔
5800
  context_free_small_cache(ctx);
8,102✔
5801
  context_free_eq_cache(ctx);
8,102✔
5802
  context_free_divmod_table(ctx);
8,102✔
5803
  context_free_explorer(ctx);
8,102✔
5804

5805
  context_free_dl_profile(ctx);
8,102✔
5806
  context_free_edge_map(ctx);
8,102✔
5807
  context_free_arith_buffer(ctx);
8,102✔
5808
  context_free_poly_buffer(ctx);
8,102✔
5809
  context_free_aux_poly(ctx);
8,102✔
5810

5811
  context_free_bvpoly_buffer(ctx);
8,102✔
5812

5813
  q_clear(&ctx->aux);
8,102✔
5814
  delete_bvconstant(&ctx->bv_buffer);
8,102✔
5815
}
8,102✔
5816

5817

5818

5819
/*
5820
 * Reset: remove all assertions and clear all internalization tables
5821
 */
5822
void reset_context(context_t *ctx) {
1✔
5823
  ctx->base_level = 0;
1✔
5824

5825
  reset_smt_core(ctx->core); // this propagates reset to all solvers
1✔
5826

5827
  if (ctx->mcsat != NULL) {
1✔
5828
    mcsat_reset(ctx->mcsat);
×
5829
  }
5830

5831
  reset_gate_manager(&ctx->gate_manager);
1✔
5832

5833
  ivector_reset(&ctx->mcsat_var_order);
1✔
5834
  ivector_reset(&ctx->mcsat_initial_var_order);
1✔
5835

5836
  reset_intern_tbl(&ctx->intern);
1✔
5837
  ivector_reset(&ctx->top_eqs);
1✔
5838
  ivector_reset(&ctx->top_atoms);
1✔
5839
  ivector_reset(&ctx->top_formulas);
1✔
5840
  ivector_reset(&ctx->top_interns);
1✔
5841

5842
  // Force the internalization mapping for true and false
5843
  intern_tbl_map_root(&ctx->intern, true_term, bool2code(true));
1✔
5844

5845
  ivector_reset(&ctx->subst_eqs);
1✔
5846
  ivector_reset(&ctx->aux_eqs);
1✔
5847
  ivector_reset(&ctx->aux_atoms);
1✔
5848
  ivector_reset(&ctx->aux_vector);
1✔
5849
  int_queue_reset(&ctx->queue);
1✔
5850
  reset_istack(&ctx->istack);
1✔
5851
  reset_objstack(&ctx->ostack);
1✔
5852
  reset_sharing_map(&ctx->sharing);
1✔
5853
  reset_objstore(&ctx->cstore);
1✔
5854
  reset_assumption_stack(&ctx->assumptions);
1✔
5855

5856
  context_free_subst(ctx);
1✔
5857
  context_free_marks(ctx);
1✔
5858
  context_reset_small_cache(ctx);
1✔
5859
  context_reset_eq_cache(ctx);
1✔
5860
  context_reset_divmod_table(ctx);
1✔
5861
  context_reset_explorer(ctx);
1✔
5862

5863
  context_free_arith_buffer(ctx);
1✔
5864
  context_reset_poly_buffer(ctx);
1✔
5865
  context_free_aux_poly(ctx);
1✔
5866
  context_free_dl_profile(ctx);
1✔
5867

5868
  context_free_bvpoly_buffer(ctx);
1✔
5869

5870
  q_clear(&ctx->aux);
1✔
5871
}
1✔
5872

5873

5874
/*
5875
 * Add tracer to ctx and ctx->core
5876
 */
5877
void context_set_trace(context_t *ctx, tracer_t *trace) {
×
5878
  assert(ctx->trace == NULL);
5879
  ctx->trace = trace;
×
5880
  smt_core_set_trace(ctx->core, trace);
×
5881
  if (ctx->mcsat != NULL) {
×
5882
    mcsat_set_tracer(ctx->mcsat, trace);
×
5883
  }
5884
}
×
5885

5886

5887
/*
5888
 * Push and pop
5889
 */
5890
void context_push(context_t *ctx) {
7,163✔
5891
  assert(context_supports_pushpop(ctx));
5892
  smt_push(ctx->core);  // propagates to all solvers
7,163✔
5893
  if (ctx->mcsat != NULL) {
7,163✔
5894
    mcsat_push(ctx->mcsat);
×
5895
  }
5896
  intern_tbl_push(&ctx->intern);
7,163✔
5897
  assumption_stack_push(&ctx->assumptions);
7,163✔
5898
  context_eq_cache_push(ctx);
7,163✔
5899
  context_divmod_table_push(ctx);
7,163✔
5900

5901
  ctx->base_level ++;
7,163✔
5902
}
7,163✔
5903

5904
void context_pop(context_t *ctx) {
231✔
5905
  assert(context_supports_pushpop(ctx) && ctx->base_level > 0);
5906
  smt_pop(ctx->core);   // propagates to all solvers
231✔
5907
  if (ctx->mcsat != NULL) {
231✔
5908
    mcsat_pop(ctx->mcsat);
×
5909
  }
5910
  intern_tbl_pop(&ctx->intern);
231✔
5911
  assumption_stack_pop(&ctx->assumptions);
231✔
5912
  context_eq_cache_pop(ctx);
231✔
5913
  context_divmod_table_pop(ctx);
231✔
5914

5915
  ctx->base_level --;
231✔
5916
}
231✔
5917

5918

5919

5920

5921

5922
/****************************
5923
 *   ASSERTIONS AND CHECK   *
5924
 ***************************/
5925

5926
/*
5927
 * Build the sharing data
5928
 * - processes all the assertions in vectors top_eqs, top_atoms, top_formulas
5929
 * - this function should be called after building the substitutions
5930
 */
5931
static void context_build_sharing_data(context_t *ctx) {
37,703✔
5932
  sharing_map_t *map;
5933

5934
  map = &ctx->sharing;
37,703✔
5935
  reset_sharing_map(map);
37,703✔
5936
  sharing_map_add_terms(map, ctx->top_eqs.data, ctx->top_eqs.size);
37,703✔
5937
  sharing_map_add_terms(map, ctx->top_atoms.data, ctx->top_atoms.size);
37,703✔
5938
  sharing_map_add_terms(map, ctx->top_formulas.data, ctx->top_formulas.size);
37,703✔
5939
}
37,703✔
5940

5941

5942
#if 0
5943
/*
5944
 * PROVISIONAL: SHOW ASSERTIONS
5945
 */
5946
static void context_show_assertions(const context_t *ctx, uint32_t n, const term_t *a) {
5947
  pp_area_t area;
5948
  yices_pp_t printer;
5949
  uint32_t i;
5950

5951
  area.width = 80;
5952
  area.height = UINT32_MAX;
5953
  area.offset = 0;
5954
  area.stretch = false;
5955
  area.truncate = false;
5956
  init_yices_pp(&printer, stdout, &area, PP_VMODE, 0);
5957

5958
  for (i=0; i<n; i++) {
5959
    pp_term_full(&printer, ctx->terms, a[i]);
5960
    flush_yices_pp(&printer);
5961
  }
5962
  delete_yices_pp(&printer, true);
5963
}
5964
#endif
5965

5966
/*
5967
 * Flatten and internalize assertions a[0 ... n-1]
5968
 * - all elements a[i] must be valid boolean term in ctx->terms
5969
 * - return code:
5970
 *   TRIVIALLY_UNSAT if there's an easy contradiction
5971
 *   CTX_NO_ERROR if the assertions were processed without error
5972
 *   a negative error code otherwise.
5973
 */
5974
static int32_t context_process_assertions(context_t *ctx, uint32_t n, const term_t *a) {
38,013✔
5975
  ivector_t *v;
5976
  uint32_t i;
5977
  int code;
5978

5979
  ivector_reset(&ctx->top_eqs);
38,013✔
5980
  ivector_reset(&ctx->top_atoms);
38,013✔
5981
  ivector_reset(&ctx->top_formulas);
38,013✔
5982
  ivector_reset(&ctx->top_interns);
38,013✔
5983
  ivector_reset(&ctx->subst_eqs);
38,013✔
5984
  ivector_reset(&ctx->aux_eqs);
38,013✔
5985
  ivector_reset(&ctx->aux_atoms);
38,013✔
5986

5987
  code = setjmp(ctx->env);
38,013✔
5988
  if (code == 0) {
38,337✔
5989

5990
    // If using MCSAT, just check and done
5991
    if (ctx->mcsat != NULL) {
38,013✔
5992
      // TBD: quant support
5993
      assert(!context_quant_enabled(ctx));
5994
      code = mcsat_assert_formulas(ctx->mcsat, n, a);
×
5995
      goto done;
×
5996
    }
5997

5998
#if 0
5999
    printf("\n=== Context: process assertions ===\n");
6000
    context_show_assertions(ctx, n, a);
6001
    printf("===\n\n");
6002
#endif
6003

6004
    // flatten
6005
    for (i=0; i<n; i++) {
107,126✔
6006
      flatten_assertion(ctx, a[i]);
69,422✔
6007
    }
6008

6009
    trace_printf(ctx->trace, 6, "(done flattening)\n");
37,704✔
6010

6011
    /*
6012
     * At this point, the assertions are stored into the vectors
6013
     * top_eqs, top_atoms, top_formulas, and top_interns
6014
     * - more top-level equalities may be in subst_eqs
6015
     * - ctx->intern stores the internalized terms and the variable
6016
     *   substitutions.
6017
     */
6018

6019
    switch (ctx->arch) {
37,704✔
6020
    // TBD: make sure following preprocessings work with quant enabled
6021
    case CTX_ARCH_EG:
3,328✔
6022
      /*
6023
       * UF problem: we must process subst_eqs last since the
6024
       * preprocessing may add new equalities in aux_eqs that may end
6025
       * up in subst_eqs after the call to process_aux_eqs.
6026
       */
6027
      if (context_breaksym_enabled(ctx)) {
3,328✔
6028
        break_uf_symmetries(ctx);
30✔
6029
      }
6030
      if (context_eq_abstraction_enabled(ctx)) {
3,328✔
6031
        analyze_uf(ctx);
63✔
6032
      }
6033
      if (ctx->aux_eqs.size > 0) {
3,328✔
6034
        process_aux_eqs(ctx);
6✔
6035
      }
6036
      if (ctx->subst_eqs.size > 0) {
3,327✔
6037
        context_process_candidate_subst(ctx);
10✔
6038
      }
6039
      break;
3,327✔
6040

6041
    case CTX_ARCH_AUTO_IDL:
27✔
6042
      /*
6043
       * For difference logic, we must process the subst_eqs first
6044
       * (otherwise analyze_diff_logic may give wrong results).
6045
       */
6046
      if (ctx->subst_eqs.size > 0) {
27✔
6047
        context_process_candidate_subst(ctx);
×
6048
      }
6049
      analyze_diff_logic(ctx, true);
27✔
6050
      create_auto_idl_solver(ctx);
27✔
6051
      break;
27✔
6052

6053
    case CTX_ARCH_AUTO_RDL:
8✔
6054
      /*
6055
       * Difference logic, we must process the subst_eqs first
6056
       */
6057
      trace_printf(ctx->trace, 6, "(auto-idl solver)\n");
8✔
6058
      if (ctx->subst_eqs.size > 0) {
8✔
6059
        context_process_candidate_subst(ctx);
×
6060
      }
6061
      analyze_diff_logic(ctx, false);
8✔
6062
      create_auto_rdl_solver(ctx);
8✔
6063
      break;
8✔
6064

6065
    case CTX_ARCH_SPLX:
666✔
6066
      /*
6067
       * Simplex, like EG, may add aux_atoms so we must process
6068
       * subst_eqs last here.
6069
       */
6070
      trace_printf(ctx->trace, 6, "(Simplex solver)\n");
666✔
6071
      // more optional processing
6072
      if (context_cond_def_preprocessing_enabled(ctx)) {
666✔
6073
        process_conditional_definitions(ctx);
52✔
6074
        if (ctx->aux_eqs.size > 0) {
52✔
6075
          process_aux_eqs(ctx);
2✔
6076
        }
6077
        if (ctx->aux_atoms.size > 0) {
52✔
6078
          process_aux_atoms(ctx);
3✔
6079
        }
6080
      }
6081
      if (ctx->subst_eqs.size > 0) {
666✔
6082
        context_process_candidate_subst(ctx);
16✔
6083
      }
6084
      break;
666✔
6085

6086
    default:
33,675✔
6087
      /*
6088
       * Process the candidate variable substitutions if any
6089
       */
6090
      if (ctx->subst_eqs.size > 0) {
33,675✔
6091
        context_process_candidate_subst(ctx);
1,807✔
6092
      }
6093
      break;
33,675✔
6094
    }
6095

6096
    /*
6097
     * Sharing
6098
     */
6099
    context_build_sharing_data(ctx);
37,703✔
6100

6101
    /*
6102
     * Notify the core + solver(s)
6103
     */
6104
    if (!context_quant_enabled(ctx)) {
37,703✔
6105
        // TBD: make sure this is correct
6106
      internalization_start(ctx->core);
35,170✔
6107
    }
6108

6109
    /*
6110
     * Assert top_eqs, top_atoms, top_formulas, top_interns
6111
     */
6112
    code = CTX_NO_ERROR;
37,703✔
6113

6114
    // first: all terms that are already internalized
6115
    v = &ctx->top_interns;
37,703✔
6116
    n = v->size;
37,703✔
6117
    if (n > 0) {
37,703✔
6118
      trace_printf(ctx->trace, 6, "(asserting  %"PRIu32" existing terms)\n", n);
226✔
6119
      i = 0;
226✔
6120
      do {
6121
        assert_toplevel_intern(ctx, v->data[i]);
330✔
6122
        i ++;
330✔
6123
      } while (i < n);
330✔
6124

6125
      // one round of propagation
6126
      if (!context_quant_enabled(ctx) && ! base_propagate(ctx->core)) {
226✔
6127
        code = TRIVIALLY_UNSAT;
10✔
6128
        goto done;
10✔
6129
      }
6130
    }
6131

6132
    // second: all top-level equalities
6133
    v = &ctx->top_eqs;
37,693✔
6134
    n = v->size;
37,693✔
6135
    if (n > 0) {
37,693✔
6136
      trace_printf(ctx->trace, 6, "(asserting  %"PRIu32" top-level equalities)\n", n);
5,388✔
6137
      i = 0;
5,388✔
6138
      do {
6139
        assert_toplevel_formula(ctx, v->data[i]);
9,448✔
6140
        i ++;
9,445✔
6141
      } while (i < n);
9,445✔
6142

6143
      // one round of propagation
6144
      if (!context_quant_enabled(ctx) && ! base_propagate(ctx->core)) {
5,385✔
6145
        code = TRIVIALLY_UNSAT;
64✔
6146
        goto done;
64✔
6147
      }
6148
    }
6149

6150
    // third: all top-level atoms (other than equalities)
6151
    v = &ctx->top_atoms;
37,626✔
6152
    n = v->size;
37,626✔
6153
    if (n > 0) {
37,626✔
6154
      trace_printf(ctx->trace, 6, "(asserting  %"PRIu32" top-level atoms)\n", n);
3,845✔
6155
      i = 0;
3,845✔
6156
      do {
6157
        assert_toplevel_formula(ctx, v->data[i]);
45,931✔
6158
        i ++;
45,920✔
6159
      } while (i < n);
45,920✔
6160

6161
      // one round of propagation
6162
      if (!context_quant_enabled(ctx) && ! base_propagate(ctx->core)) {
3,834✔
6163
        code = TRIVIALLY_UNSAT;
57✔
6164
        goto done;
57✔
6165
      }
6166
    }
6167

6168
    // last: all non-atomic, formulas
6169
    v =  &ctx->top_formulas;
37,558✔
6170
    n = v->size;
37,558✔
6171
    if (n > 0) {
37,558✔
6172
      trace_printf(ctx->trace, 6, "(asserting  %"PRIu32" top-level formulas)\n", n);
11,967✔
6173
      i = 0;
11,967✔
6174
      do {
6175
        assert_toplevel_formula(ctx, v->data[i]);
35,200✔
6176
        i ++;
35,200✔
6177
      } while (i < n);
35,200✔
6178

6179
      // one round of propagation
6180
      if (!context_quant_enabled(ctx) && ! base_propagate(ctx->core)) {
11,967✔
6181
        code = TRIVIALLY_UNSAT;
63✔
6182
        goto done;
63✔
6183
      }
6184
    }
6185

6186
  } else {
6187
    /*
6188
     * Exception: return from longjmp(ctx->env, code);
6189
     */
6190
    ivector_reset(&ctx->aux_vector);
324✔
6191
    reset_istack(&ctx->istack);
324✔
6192
    reset_objstack(&ctx->ostack);
324✔
6193
    int_queue_reset(&ctx->queue);
324✔
6194
    context_free_subst(ctx);
324✔
6195
    context_free_marks(ctx);
324✔
6196
  }
6197

6198
 done:
38,013✔
6199
  return code;
38,013✔
6200
}
6201

6202
/*
6203
 * Assert all formulas f[0] ... f[n-1]
6204
 * The context status must be IDLE.
6205
 *
6206
 * Return code:
6207
 * - TRIVIALLY_UNSAT means that an inconsistency is detected
6208
 *   (in that case the context status is set to UNSAT)
6209
 * - CTX_NO_ERROR means no internalization error and status not
6210
 *   determined
6211
 * - otherwise, the code is negative to report an error.
6212
 */
6213
int32_t _o_assert_formulas(context_t *ctx, uint32_t n, const term_t *f) {
35,479✔
6214
  int32_t code;
6215

6216
  assert(ctx->arch == CTX_ARCH_AUTO_IDL ||
6217
         ctx->arch == CTX_ARCH_AUTO_RDL ||
6218
         smt_status(ctx->core) == STATUS_IDLE);
6219
  assert(!context_quant_enabled(ctx));
6220

6221
  code = context_process_assertions(ctx, n, f);
35,479✔
6222
  if (code == TRIVIALLY_UNSAT) {
35,479✔
6223
    if (ctx->arch == CTX_ARCH_AUTO_IDL || ctx->arch == CTX_ARCH_AUTO_RDL) {
511✔
6224
      // cleanup: reset arch/config to 'no theory'
6225
      assert(ctx->arith_solver == NULL && ctx->bv_solver == NULL && ctx->fun_solver == NULL &&
6226
             ctx->mode == CTX_MODE_ONECHECK);
6227
      ctx->arch = CTX_ARCH_NOSOLVERS;
1✔
6228
      ctx->theories = 0;
1✔
6229
      ctx->options = 0;
1✔
6230
    }
6231

6232
    if( smt_status(ctx->core) != STATUS_UNSAT) {
511✔
6233
      // force UNSAT in the core
6234
      add_empty_clause(ctx->core);
317✔
6235
      ctx->core->status = STATUS_UNSAT;
317✔
6236
    }
6237
  }
6238

6239
  return code;
35,479✔
6240
}
6241

6242
/*
6243
 * Assert all formulas f[0] ... f[n-1] during quantifier instantiation
6244
 * The context status must be SEARCHING.
6245
 *
6246
 * Return code:
6247
 * - TRIVIALLY_UNSAT means that an inconsistency is detected
6248
 *   (in that case the context status is set to UNSAT)
6249
 * - CTX_NO_ERROR means no internalization error and status not
6250
 *   determined
6251
 * - otherwise, the code is negative to report an error.
6252
 */
6253
int32_t quant_assert_formulas(context_t *ctx, uint32_t n, const term_t *f) {
2,534✔
6254
  int32_t code;
6255

6256
  assert(context_quant_enabled(ctx));
6257
  assert(smt_status(ctx->core) == STATUS_SEARCHING);
6258

6259
  code = context_process_assertions(ctx, n, f);
2,534✔
6260
  if (code == TRIVIALLY_UNSAT) {
2,534✔
6261
    if (ctx->arch == CTX_ARCH_AUTO_IDL || ctx->arch == CTX_ARCH_AUTO_RDL) {
1✔
6262
      // cleanup: reset arch/config to 'no theory'
6263
      assert(ctx->arith_solver == NULL && ctx->bv_solver == NULL && ctx->fun_solver == NULL &&
6264
      ctx->mode == CTX_MODE_ONECHECK);
6265
      ctx->arch = CTX_ARCH_NOSOLVERS;
×
6266
      ctx->theories = 0;
×
6267
      ctx->options = 0;
×
6268
    }
6269

6270
    if( smt_status(ctx->core) != STATUS_UNSAT) {
1✔
6271
      // force UNSAT in the core
6272
      add_empty_clause(ctx->core);
1✔
6273
      ctx->core->status = STATUS_UNSAT;
1✔
6274
    }
6275
  }
6276

6277
  return code;
2,534✔
6278
}
6279

6280
int32_t assert_formulas(context_t *ctx, uint32_t n, const term_t *f) {
7,883✔
6281
  MT_PROTECT(int32_t, __yices_globals.lock, _o_assert_formulas(ctx, n, f));
7,883✔
6282
}
6283

6284

6285

6286

6287
/*
6288
 * Assert a boolean formula f.
6289
 *
6290
 * The context status must be IDLE.
6291
 *
6292
 * Return code:
6293
 * - TRIVIALLY_UNSAT means that an inconsistency is detected
6294
 *   (in that case the context status is set to UNSAT)
6295
 * - CTX_NO_ERROR means no internalization error and status not
6296
 *   determined
6297
 * - otherwise, the code is negative. The assertion could
6298
 *   not be processed.
6299
 */
6300
int32_t _o_assert_formula(context_t *ctx, term_t f) {
27,596✔
6301
  return _o_assert_formulas(ctx, 1, &f);
27,596✔
6302
}
6303

6304
int32_t assert_formula(context_t *ctx, term_t f) {
27,596✔
6305
  MT_PROTECT(int32_t, __yices_globals.lock, _o_assert_formula(ctx, f));
27,596✔
6306
}
6307

6308

6309
/*
6310
 * Convert boolean term t to a literal l in context ctx
6311
 * - t must be a boolean term
6312
 * - return a negative code if there's an error
6313
 * - return a literal (l >= 0) otherwise.
6314
 */
6315
int32_t context_internalize(context_t *ctx, term_t t) {
104✔
6316
  int code;
6317
  literal_t l;
6318

6319
  ivector_reset(&ctx->top_eqs);
104✔
6320
  ivector_reset(&ctx->top_atoms);
104✔
6321
  ivector_reset(&ctx->top_formulas);
104✔
6322
  ivector_reset(&ctx->top_interns);
104✔
6323
  ivector_reset(&ctx->subst_eqs);
104✔
6324
  ivector_reset(&ctx->aux_eqs);
104✔
6325

6326
  code = setjmp(ctx->env);
104✔
6327
  if (code == 0) {
104✔
6328
    // we must call internalization start first
6329
    if (!context_quant_enabled(ctx)) {
104✔
6330
      // TBD: make sure this is correct
6331
      internalization_start(ctx->core);
×
6332
    }
6333
    l = internalize_to_literal(ctx, t);
104✔
6334
  } else {
6335
    assert(code < 0);
6336
    /*
6337
     * Clean up
6338
     */
6339
    ivector_reset(&ctx->aux_vector);
×
6340
    reset_istack(&ctx->istack);
×
6341
    int_queue_reset(&ctx->queue);
×
6342
    context_free_subst(ctx);
×
6343
    context_free_marks(ctx);
×
6344
    l = code;
×
6345
  }
6346

6347
  return l;
104✔
6348
}
6349

6350

6351
/*
6352
 * Build an assumption for Boolean term t:
6353
 * - this converts t to a literal l in context ctx
6354
 *   then create an indicator variable x in the core
6355
 *   and add the clause (x => l) in the core.
6356
 * - return a negative code if t can't be internalized
6357
 * - return the literal x otherwise (where x>=0).
6358
 */
6359
int32_t context_add_assumption(context_t *ctx, term_t t) {
×
6360
  int32_t l, x;
6361

6362
  // check if we already have an assumption literal for t
6363
  x = assumption_literal_for_term(&ctx->assumptions, t);
×
6364
  if (x < 0) {
×
6365
    l = context_internalize(ctx, t);
×
6366
    if (l < 0) return l; // error code
×
6367

6368
    x = pos_lit(create_boolean_variable(ctx->core));
×
6369
    add_binary_clause(ctx->core, not(x), l); // clause (x implies l)
×
6370

6371
    assumption_stack_add(&ctx->assumptions, t, x);
×
6372
  }
6373

6374
  return x;
×
6375
}
6376

6377

6378

6379
/*
6380
 * PROVISIONAL: FOR TESTING/DEBUGGING
6381
 */
6382

6383
/*
6384
 * Preprocess formula f or array of formulas f[0 ... n-1]
6385
 * - this does flattening + build substitutions
6386
 * - return code: as in assert_formulas
6387
 * - the result is stored in the internal vectors
6388
 *     ctx->top_interns
6389
 *     ctx->top_eqs
6390
 *     ctx->top_atoms
6391
 *     ctx->top_formulas
6392
 *   + ctx->intern stores substitutions
6393
 */
6394
int32_t context_process_formulas(context_t *ctx, uint32_t n, term_t *f) {
×
6395
  uint32_t i;
6396
  int code;
6397

6398
  ivector_reset(&ctx->top_eqs);
×
6399
  ivector_reset(&ctx->top_atoms);
×
6400
  ivector_reset(&ctx->top_formulas);
×
6401
  ivector_reset(&ctx->top_interns);
×
6402
  ivector_reset(&ctx->subst_eqs);
×
6403
  ivector_reset(&ctx->aux_eqs);
×
6404
  ivector_reset(&ctx->aux_atoms);
×
6405

6406
  code = setjmp(ctx->env);
×
6407
  if (code == 0) {
×
6408
    // flatten
6409
    for (i=0; i<n; i++) {
×
6410
      flatten_assertion(ctx, f[i]);
×
6411
    }
6412

6413
    /*
6414
     * At this point, the assertions are stored into the vectors
6415
     * top_eqs, top_atoms, top_formulas, and top_interns
6416
     * - more top-level equalities may be in subst_eqs
6417
     * - ctx->intern stores the internalized terms and the variable
6418
     *   substitutions.
6419
     */
6420

6421
    switch (ctx->arch) {
×
6422
    case CTX_ARCH_EG:
×
6423
      /*
6424
       * UF problem: we must process subst_eqs last since the
6425
       * preprocessing may add new equalities in aux_eqs that may end
6426
       * up in subst_eqs after the call to process_aux_eqs.
6427
       */
6428
      if (context_breaksym_enabled(ctx)) {
×
6429
        break_uf_symmetries(ctx);
×
6430
      }
6431
      if (context_eq_abstraction_enabled(ctx)) {
×
6432
        analyze_uf(ctx);
×
6433
      }
6434
      if (ctx->aux_eqs.size > 0) {
×
6435
        process_aux_eqs(ctx);
×
6436
      }
6437
      if (ctx->subst_eqs.size > 0) {
×
6438
        context_process_candidate_subst(ctx);
×
6439
      }
6440
      break;
×
6441

6442
    case CTX_ARCH_AUTO_IDL:
×
6443
      /*
6444
       * For difference logic, we must process the subst_eqs first
6445
       * (otherwise analyze_diff_logic may give wrong results).
6446
       */
6447
      if (ctx->subst_eqs.size > 0) {
×
6448
        context_process_candidate_subst(ctx);
×
6449
      }
6450
      analyze_diff_logic(ctx, true);
×
6451
      create_auto_idl_solver(ctx);
×
6452
      break;
×
6453

6454
    case CTX_ARCH_AUTO_RDL:
×
6455
      /*
6456
       * Difference logic, we must process the subst_eqs first
6457
       */
6458
      if (ctx->subst_eqs.size > 0) {
×
6459
        context_process_candidate_subst(ctx);
×
6460
      }
6461
      analyze_diff_logic(ctx, false);
×
6462
      create_auto_rdl_solver(ctx);
×
6463
      break;
×
6464

6465
    case CTX_ARCH_SPLX:
×
6466
      /*
6467
       * Simplex, like EG, may add aux_atoms so we must process
6468
       * subst_eqs last here.
6469
       */
6470
      // more optional processing
6471
      if (context_cond_def_preprocessing_enabled(ctx)) {
×
6472
        process_conditional_definitions(ctx);
×
6473
        if (ctx->aux_eqs.size > 0) {
×
6474
          process_aux_eqs(ctx);
×
6475
        }
6476
        if (ctx->aux_atoms.size > 0) {
×
6477
          process_aux_atoms(ctx);
×
6478
        }
6479
      }
6480
      if (ctx->subst_eqs.size > 0) {
×
6481
        context_process_candidate_subst(ctx);
×
6482
      }
6483
      break;
×
6484

6485
    default:
×
6486
      /*
6487
       * Process the candidate variable substitutions if any
6488
       */
6489
      if (ctx->subst_eqs.size > 0) {
×
6490
        context_process_candidate_subst(ctx);
×
6491
      }
6492
      break;
×
6493
    }
6494

6495
    /*
6496
     * Sharing
6497
     */
6498
    context_build_sharing_data(ctx);
×
6499

6500
    code = CTX_NO_ERROR;
×
6501

6502
  } else {
6503
    /*
6504
     * Exception: return from longjmp(ctx->env, code);
6505
     */
6506
    ivector_reset(&ctx->aux_vector);
×
6507
    reset_istack(&ctx->istack);
×
6508
    int_queue_reset(&ctx->queue);
×
6509
    context_free_subst(ctx);
×
6510
    context_free_marks(ctx);
×
6511
  }
6512

6513
  return code;
×
6514
}
6515

6516
int32_t context_process_formula(context_t *ctx, term_t f) {
×
6517
  return context_process_formulas(ctx, 1, &f);
×
6518
}
6519

6520

6521

6522
/*
6523
 * The search function 'check_context' is defined in context_solver.c
6524
 */
6525

6526

6527
/*
6528
 * Interrupt the search.
6529
 */
6530
void context_stop_search(context_t *ctx) {
3✔
6531
  if (ctx->mcsat == NULL) {
3✔
6532
    stop_search(ctx->core);
3✔
6533
    if (context_has_simplex_solver(ctx)) {
3✔
6534
      simplex_stop_search(ctx->arith_solver);
3✔
6535
    }
6536
  } else {
6537
    mcsat_stop_search(ctx->mcsat);
×
6538
  }
6539
}
3✔
6540

6541

6542

6543
/*
6544
 * Cleanup: restore ctx to a good state after check_context
6545
 * is interrupted.
6546
 */
6547
void context_cleanup(context_t *ctx) {
1✔
6548
  // restore the state to IDLE, propagate to all solvers (via pop)
6549
  assert(context_supports_cleaninterrupt(ctx));
6550
  if (ctx->mcsat == NULL) {
1✔
6551
    smt_cleanup(ctx->core);
1✔
6552
  } else {
6553
    mcsat_clear(ctx->mcsat);
×
6554
  }
6555
}
1✔
6556

6557

6558

6559
/*
6560
 * Clear: prepare for more assertions and checks
6561
 * - free the boolean assignment
6562
 * - reset the status to IDLE
6563
 */
6564
void context_clear(context_t *ctx) {
21,810✔
6565
  assert(context_supports_multichecks(ctx));
6566
  if (ctx->mcsat == NULL) {
21,810✔
6567
    smt_clear(ctx->core);
21,810✔
6568
  } else {
6569
    mcsat_clear(ctx->mcsat);
×
6570
  }
6571
}
21,810✔
6572

6573

6574

6575
/*
6576
 * Clear_unsat: prepare for pop if the status is UNSAT
6577
 * - remove assumptions if any
6578
 *
6579
 * - if clean interrupt is enabled, then there may be a mismatch between
6580
 *   the context's base_level and the core base_level.
6581
 * - it's possible to have ctx->core.base_level = ctx->base_level + 1
6582
 * - this happens because start_search in smt_core does an internal smt_push
6583
 *   to allow the core to be restored to a clean state if search is interrupted.
6584
 * - if search is not interrupted and the search returns UNSAT, then we're
6585
 *   in a state with core base level = context base level + 1.
6586
 */
6587
void context_clear_unsat(context_t *ctx) {
312✔
6588
  if (ctx->mcsat == NULL) {
312✔
6589
    smt_clear_unsat(ctx->core);
312✔
6590
    assert(smt_base_level(ctx->core) == ctx->base_level);
6591
  } else {
6592
    mcsat_clear(ctx->mcsat);
×
6593
  }
6594
}
312✔
6595

6596

6597

6598
/*
6599
 * Add the blocking clause to ctx
6600
 * - ctx->status must be either SAT or UNKNOWN
6601
 * - this collects all decision literals in the current truth assignment
6602
 *   (say l_1, ..., l_k) then clears the current assignment and adds the
6603
 *  clause ((not l_1) \/ ... \/ (not l_k)).
6604
 *
6605
 * Return code:
6606
 * - TRIVIALLY_UNSAT: means that the blocking clause is empty (i.e., k = 0)
6607
 *   (in that case, the context status is set to UNSAT)
6608
 * - CTX_NO_ERROR: means that the blocking clause is not empty (i.e., k > 0)
6609
 *   (In this case, the context status is set to IDLE)
6610
 */
6611
int32_t assert_blocking_clause(context_t *ctx) {
14,854✔
6612
  ivector_t *v;
6613
  uint32_t i, n;
6614
  int32_t code;
6615

6616
  assert(smt_status(ctx->core) == STATUS_SAT ||
6617
         smt_status(ctx->core) == STATUS_UNKNOWN);
6618

6619
  // get decision literals and build the blocking clause
6620
  v = &ctx->aux_vector;
14,854✔
6621
  assert(v->size == 0);
6622
  collect_decision_literals(ctx->core, v);
14,854✔
6623
  n = v->size;
14,854✔
6624
  for (i=0; i<n; i++) {
26,263✔
6625
    v->data[i] = not(v->data[i]);
11,409✔
6626
  }
6627

6628
  // prepare for the new assertion + notify solvers of a new assertion
6629
  context_clear(ctx);
14,854✔
6630
  internalization_start(ctx->core);
14,854✔
6631

6632
  // add the blocking clause
6633
  add_clause(ctx->core, n, v->data);
14,854✔
6634
  ivector_reset(v);
14,854✔
6635

6636
  // force UNSAT if n = 0
6637
  code = CTX_NO_ERROR;
14,854✔
6638
  if (n == 0) {
14,854✔
6639
    code = TRIVIALLY_UNSAT;
6,816✔
6640
    ctx->core->status = STATUS_UNSAT;
6,816✔
6641
  }
6642

6643
  assert(n == 0 || smt_status(ctx->core) == STATUS_IDLE);
6644

6645
  return code;
14,854✔
6646
}
6647

6648

6649

6650

6651
/********************************
6652
 *  GARBAGE COLLECTION SUPPORT  *
6653
 *******************************/
6654

6655
/*
6656
 * Marker for all terms present in the eq_map
6657
 * - aux = the relevant term table.
6658
 * - each record p stores <k0, k1, val> where k0 and k1 are both
6659
 *   terms in aux and val is a literal in the core
6660
 */
6661
static void ctx_mark_eq(void *aux, const pmap2_rec_t *p) {
×
6662
  term_table_set_gc_mark(aux, index_of(p->k0));
×
6663
  term_table_set_gc_mark(aux, index_of(p->k1));
×
6664
}
×
6665

6666

6667
/*
6668
 * Go through all data structures in ctx and mark all terms and types
6669
 * that they use.
6670
 */
6671
void context_gc_mark(context_t *ctx) {
1✔
6672
  if (ctx->egraph != NULL) {
1✔
6673
    egraph_gc_mark(ctx->egraph);
1✔
6674
  }
6675
  if (ctx->fun_solver != NULL) {
1✔
6676
    fun_solver_gc_mark(ctx->fun_solver);
1✔
6677
  }
6678

6679
  intern_tbl_gc_mark(&ctx->intern);
1✔
6680

6681
  // empty all the term vectors to be safe
6682
  ivector_reset(&ctx->top_eqs);
1✔
6683
  ivector_reset(&ctx->top_atoms);
1✔
6684
  ivector_reset(&ctx->top_formulas);
1✔
6685
  ivector_reset(&ctx->top_interns);
1✔
6686
  ivector_reset(&ctx->subst_eqs);
1✔
6687
  ivector_reset(&ctx->aux_eqs);
1✔
6688

6689
  if (ctx->eq_cache != NULL) {
1✔
6690
    pmap2_iterate(ctx->eq_cache, ctx->terms, ctx_mark_eq);
×
6691
  }
6692

6693
  if (ctx->mcsat != NULL) {
1✔
6694
    mcsat_gc_mark(ctx->mcsat);
×
6695
  }
6696
}
1✔
6697

STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc