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

SRI-CSL / yices2 / 22888904465

10 Mar 2026 05:41AM UTC coverage: 66.728% (+0.2%) from 66.539%
22888904465

Pull #611

github

disteph
Add broad CDCL(T)+supplementary-MCSAT API coverage
Pull Request #611: Draft: wrap MCSAT as a Nelson-Oppen theory solver in CDCL(T) architecture

690 of 1006 new or added lines in 15 files covered. (68.59%)

2 existing lines in 2 files now uncovered.

84087 of 126014 relevant lines covered (66.73%)

1691137.23 hits per line

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

80.42
/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 "api/context_config.h"
26
#include "context/context.h"
27
#include "context/context_simplifier.h"
28
#include "context/context_utils.h"
29
#include "context/internalization_codes.h"
30
#include "context/ite_flattener.h"
31
#include "solvers/bv/bvsolver.h"
32
#include "solvers/floyd_warshall/idl_floyd_warshall.h"
33
#include "solvers/floyd_warshall/rdl_floyd_warshall.h"
34
#include "solvers/funs/fun_solver.h"
35
#include "solvers/mcsat_satellite.h"
36
#include "solvers/quant/quant_solver.h"
37
#include "solvers/simplex/simplex.h"
38
#include "terms/poly_buffer_terms.h"
39
#include "terms/term_explorer.h"
40
#include "terms/term_utils.h"
41
#include "utils/int_hash_map.h"
42
#include "utils/memalloc.h"
43

44
#include "mcsat/solver.h"
45
#include "mt/thread_macros.h"
46

47
#include "api/yices_globals.h"
48

49
#define TRACE 0
50

51
#if TRACE
52

53
#include <stdio.h>
54

55
#include "io/term_printer.h"
56
#include "solvers/cdcl/smt_core_printer.h"
57

58
#endif
59

60

61

62

63

64
/**********************
65
 *  INTERNALIZATION   *
66
 *********************/
67

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

80
static inline mcsat_satellite_t *context_mcsat_satellite(context_t *ctx);
81
static bool context_atom_requires_mcsat(context_t *ctx, term_t atom);
82
static literal_t map_mcsat_atom_to_literal(context_t *ctx, term_t atom);
83
static int32_t context_enable_mcsat_supplement(context_t *ctx);
84
static void context_disable_mcsat_supplement(context_t *ctx);
85
static bool context_assertions_need_mcsat_supplement(context_t *ctx, uint32_t n, const term_t *a);
86
static int32_t context_seed_mcsat_satellite(context_t *ctx);
87

88

89
/*
90
 * Supplementary MCSAT support (CDCL(T) mode)
91
 */
92
static inline mcsat_satellite_t *context_mcsat_satellite(context_t *ctx) {
54✔
93
  if (ctx->egraph == NULL) {
54✔
NEW
94
    return NULL;
×
95
  }
96
  return (mcsat_satellite_t *) ctx->egraph->th[ETYPE_MCSAT];
54✔
97
}
98

99
static inline bool divisor_requires_mcsat(term_table_t *terms, term_t t) {
9✔
100
  t = unsigned_term(t);
9✔
101
  return term_kind(terms, t) != ARITH_CONSTANT;
9✔
102
}
103

104
/*
105
 * Detect whether t contains arithmetic or finite-field subterms.
106
 * This is used to route all relevant arithmetic/FF atoms to the supplementary
107
 * MCSAT context once supplementation is active.
108
 */
109
static bool term_contains_arith_or_ff(context_t *ctx, term_t t, int_hmap_t *cache) {
40✔
110
  term_table_t *terms;
111
  int_hmap_pair_t *p;
112
  type_t tau;
113
  type_kind_t tkind;
114
  bool found;
115
  uint32_t i, nchildren;
116

117
  if (t < 0) {
40✔
NEW
118
    return false;
×
119
  }
120

121
  t = unsigned_term(t);
40✔
122
  p = int_hmap_find(cache, t);
40✔
123
  if (p != NULL) {
40✔
NEW
124
    return p->val != 0;
×
125
  }
126

127
  terms = ctx->terms;
40✔
128
  tau = term_type(terms, t);
40✔
129
  tkind = type_kind(terms->types, tau);
40✔
130

131
  /*
132
   * Variables of arithmetic/finite-field type may not satisfy
133
   * is_arithmetic_term/is_finitefield_term, so include type-based detection.
134
   */
135
  found = is_arithmetic_term(terms, t) || is_finitefield_term(terms, t) ||
63✔
136
          tkind == INT_TYPE || tkind == REAL_TYPE || is_ff_type(terms->types, tau);
63✔
137

138
  if (!found) {
40✔
139
    if (term_is_projection(terms, t)) {
20✔
NEW
140
      found = term_contains_arith_or_ff(ctx, proj_term_arg(terms, t), cache);
×
141

142
    } else if (term_is_sum(terms, t)) {
20✔
NEW
143
      nchildren = term_num_children(terms, t);
×
NEW
144
      for (i = 0; i < nchildren && !found; i++) {
×
145
        term_t child;
146
        mpq_t q;
NEW
147
        mpq_init(q);
×
NEW
148
        sum_term_component(terms, t, i, q, &child);
×
NEW
149
        found = term_contains_arith_or_ff(ctx, child, cache);
×
NEW
150
        mpq_clear(q);
×
151
      }
152

153
    } else if (term_is_bvsum(terms, t)) {
20✔
154
      int32_t *aux;
155
      uint32_t nbits;
156
      term_t child;
157

NEW
158
      nbits = term_bitsize(terms, t);
×
NEW
159
      aux = (int32_t *) safe_malloc(nbits * sizeof(int32_t));
×
NEW
160
      nchildren = term_num_children(terms, t);
×
NEW
161
      for (i = 0; i < nchildren && !found; i++) {
×
NEW
162
        bvsum_term_component(terms, t, i, aux, &child);
×
NEW
163
        found = term_contains_arith_or_ff(ctx, child, cache);
×
164
      }
NEW
165
      safe_free(aux);
×
166

167
    } else if (term_is_product(terms, t)) {
20✔
NEW
168
      nchildren = term_num_children(terms, t);
×
NEW
169
      for (i = 0; i < nchildren && !found; i++) {
×
170
        term_t child;
171
        uint32_t exp;
NEW
172
        product_term_component(terms, t, i, &child, &exp);
×
NEW
173
        found = term_contains_arith_or_ff(ctx, child, cache);
×
174
      }
175

176
    } else if (term_is_composite(terms, t)) {
20✔
177
      nchildren = term_num_children(terms, t);
20✔
178
      for (i = 0; i < nchildren && !found; i++) {
40✔
179
        found = term_contains_arith_or_ff(ctx, term_child(terms, t, i), cache);
20✔
180
      }
181
    }
182
  }
183

184
  p = int_hmap_get(cache, t);
40✔
185
  p->val = found ? 1 : 0;
40✔
186
  return found;
40✔
187
}
188

189
static bool term_requires_mcsat_supplement(context_t *ctx, term_t t, int_hmap_t *cache) {
1,703,008✔
190
  term_table_t *terms;
191
  int_hmap_pair_t *p;
192
  bool trigger;
193
  uint32_t i, nchildren;
194

195
  if (t < 0) {
1,703,008✔
196
    return false;
3,579✔
197
  }
198

199
  t = unsigned_term(t);
1,699,429✔
200
  p = int_hmap_find(cache, t);
1,699,429✔
201
  if (p != NULL) {
1,699,429✔
202
    return p->val != 0;
873,503✔
203
  }
204

205
  terms = ctx->terms;
825,926✔
206
  trigger = false;
825,926✔
207

208
  // finite-field usage
209
  if (is_finitefield_term(terms, t)) {
825,926✔
NEW
210
    trigger = true;
×
211
  }
212

213
  if (!trigger) {
825,926✔
214
    switch (term_kind(terms, t)) {
825,926✔
215
    case ARITH_ROOT_ATOM:
5✔
216
    case ARITH_FF_CONSTANT:
217
    case ARITH_FF_POLY:
218
    case ARITH_FF_EQ_ATOM:
219
    case ARITH_FF_BINEQ_ATOM:
220
      trigger = true;
5✔
221
      break;
5✔
222

223
    case ARITH_RDIV:
5✔
224
      trigger = divisor_requires_mcsat(terms, arith_rdiv_term_desc(terms, t)->arg[1]);
5✔
225
      break;
5✔
226

NEW
227
    case ARITH_IDIV:
×
NEW
228
      trigger = divisor_requires_mcsat(terms, arith_idiv_term_desc(terms, t)->arg[1]);
×
NEW
229
      break;
×
230

231
    case ARITH_MOD:
4✔
232
      trigger = divisor_requires_mcsat(terms, arith_mod_term_desc(terms, t)->arg[1]);
4✔
233
      break;
4✔
234

NEW
235
    case ARITH_DIVIDES_ATOM:
×
NEW
236
      trigger = divisor_requires_mcsat(terms, arith_divides_atom_desc(terms, t)->arg[0]);
×
NEW
237
      break;
×
238

239
    default:
825,912✔
240
      break;
825,912✔
241
    }
242
  }
243

244
  // arithmetic nonlinearity (including deep products in arithmetic predicates)
245
  if (!trigger && is_arithmetic_term(terms, t) && term_degree(terms, t) > 1) {
825,926✔
246
    trigger = true;
7✔
247
  }
248

249
  if (!trigger) {
825,926✔
250
    if (term_is_projection(terms, t)) {
825,912✔
251
      trigger = term_requires_mcsat_supplement(ctx, proj_term_arg(terms, t), cache);
352,607✔
252

253
    } else if (term_is_sum(terms, t)) {
473,305✔
254
      nchildren = term_num_children(terms, t);
4,591✔
255
      for (i=0; i<nchildren && !trigger; i++) {
16,538✔
256
        term_t child;
257
        mpq_t q;
258
        mpq_init(q);
11,947✔
259
        sum_term_component(terms, t, i, q, &child);
11,947✔
260
        trigger = term_requires_mcsat_supplement(ctx, child, cache);
11,947✔
261
        mpq_clear(q);
11,947✔
262
      }
263

264
    } else if (term_is_bvsum(terms, t)) {
468,714✔
265
      int32_t *aux;
266
      uint32_t nbits;
267
      term_t child;
268

269
      nbits = term_bitsize(terms, t);
5,832✔
270
      aux = (int32_t *) safe_malloc(nbits * sizeof(int32_t));
5,832✔
271
      nchildren = term_num_children(terms, t);
5,832✔
272
      for (i=0; i<nchildren && !trigger; i++) {
13,394✔
273
        bvsum_term_component(terms, t, i, aux, &child);
7,562✔
274
        trigger = term_requires_mcsat_supplement(ctx, child, cache);
7,562✔
275
      }
276
      safe_free(aux);
5,832✔
277

278
    } else if (term_is_product(terms, t)) {
462,882✔
279
      nchildren = term_num_children(terms, t);
66✔
280
      for (i=0; i<nchildren && !trigger; i++) {
196✔
281
        term_t child;
282
        uint32_t exp;
283
        product_term_component(terms, t, i, &child, &exp);
130✔
284
        trigger = term_requires_mcsat_supplement(ctx, child, cache);
130✔
285
      }
286

287
    } else if (term_is_composite(terms, t)) {
462,816✔
288
      nchildren = term_num_children(terms, t);
293,659✔
289
      for (i=0; i<nchildren && !trigger; i++) {
1,576,786✔
290
        trigger = term_requires_mcsat_supplement(ctx, term_child(terms, t, i), cache);
1,283,127✔
291
      }
292
    }
293
  }
294

295
  p = int_hmap_get(cache, t);
825,926✔
296
  p->val = trigger ? 1 : 0;
825,926✔
297
  return trigger;
825,926✔
298
}
299

300
static bool context_assertions_need_mcsat_supplement(context_t *ctx, uint32_t n, const term_t *a) {
25,768✔
301
  int_hmap_t cache;
302
  uint32_t i;
303
  bool trigger;
304

305
  init_int_hmap(&cache, 0);
25,768✔
306
  trigger = false;
25,768✔
307
  for (i=0; i<n && !trigger; i++) {
73,403✔
308
    trigger = term_requires_mcsat_supplement(ctx, a[i], &cache);
47,635✔
309
  }
310
  delete_int_hmap(&cache);
25,768✔
311

312
  return trigger;
25,768✔
313
}
314

315
static int32_t context_enable_mcsat_supplement(context_t *ctx) {
14✔
316
  mcsat_satellite_t *sat;
317
  int32_t code;
318

319
  if (ctx->mcsat_supplement_active) {
14✔
320
    return CTX_NO_ERROR;
4✔
321
  }
322
  if (ctx->arch == CTX_ARCH_MCSAT || ctx->egraph == NULL) {
10✔
NEW
323
    return CONTEXT_UNSUPPORTED_THEORY;
×
324
  }
325

326
  sat = new_mcsat_satellite(ctx);
10✔
327
  egraph_attach_mcsat_solver(ctx->egraph, sat,
10✔
328
                             mcsat_satellite_ctrl_interface(sat),
329
                             mcsat_satellite_smt_interface(sat),
330
                             mcsat_satellite_egraph_interface(sat));
331
  ctx->mcsat_supplement_active = true;
10✔
332

333
  code = context_seed_mcsat_satellite(ctx);
10✔
334
  if (code < 0) {
10✔
NEW
335
    context_disable_mcsat_supplement(ctx);
×
NEW
336
    return code;
×
337
  }
338

339
  return CTX_NO_ERROR;
10✔
340
}
341

342
static void context_disable_mcsat_supplement(context_t *ctx) {
10✔
343
  mcsat_satellite_t *sat;
344

345
  if (!ctx->mcsat_supplement_active || ctx->egraph == NULL) {
10✔
NEW
346
    return;
×
347
  }
348

349
  sat = context_mcsat_satellite(ctx);
10✔
350
  egraph_detach_mcsat_solver(ctx->egraph);
10✔
351
  delete_mcsat_satellite(sat);
10✔
352
  ctx->mcsat_supplement_active = false;
10✔
353
}
354

355
/*
356
 * Import already-internalized arithmetic/FF atoms when supplementary MCSAT
357
 * is activated after earlier assertions.
358
 */
359
static int32_t context_seed_mcsat_satellite(context_t *ctx) {
10✔
360
  mcsat_satellite_t *sat;
361
  term_table_t *terms;
362
  uint32_t i, n;
363
  uint32_t seeded = 0;
10✔
364
  bool trace_seed;
365

366
  sat = context_mcsat_satellite(ctx);
10✔
367
  if (sat == NULL) {
10✔
NEW
368
    return CTX_NO_ERROR;
×
369
  }
370

371
  terms = ctx->terms;
10✔
372
  trace_seed = tracing_tag(ctx->trace, "mcsat::supplement::seed");
10✔
373
  n = intern_tbl_num_terms(&ctx->intern);
10✔
374
  for (i = 1; i < n; i++) {
156✔
375
    term_t t;
376
    int32_t code;
377
    literal_t l;
378

379
    if (!good_term_idx(terms, i)) {
146✔
NEW
380
      continue;
×
381
    }
382

383
    t = pos_term(i);
146✔
384
    if (!is_boolean_term(terms, t) ||
146✔
385
        !intern_tbl_is_root(&ctx->intern, t) ||
110✔
386
        !intern_tbl_root_is_mapped(&ctx->intern, t)) {
110✔
387
      continue;
132✔
388
    }
389

390
    if (!context_atom_requires_mcsat(ctx, t)) {
14✔
391
      continue;
10✔
392
    }
393

394
    code = intern_tbl_map_of_root(&ctx->intern, t);
4✔
395
    if (code_is_var(code)) {
4✔
NEW
396
      l = code2literal(code);
×
397
    } else {
398
      occ_t u = code2occ(code);
4✔
399
      if (u == true_occ) {
4✔
400
        l = true_literal;
4✔
NEW
401
      } else if (u == false_occ) {
×
NEW
402
        l = false_literal;
×
NEW
403
      } else if (ctx->egraph != NULL) {
×
NEW
404
        l = egraph_occ2literal(ctx->egraph, u);
×
405
      } else {
NEW
406
        continue;
×
407
      }
408
    }
409

410
    code = mcsat_satellite_register_atom(sat, t, l, NULL);
4✔
411
    if (code < 0) {
4✔
NEW
412
      return code;
×
413
    }
414
    seeded ++;
4✔
415
    if (trace_seed) {
4✔
NEW
416
      trace_printf(ctx->trace, 1, "mcsat supplement seed: lit=%"PRId32" atom=", l);
×
NEW
417
      trace_pp_term(ctx->trace, 1, terms, t);
×
418
    }
419
  }
420

421
  if (trace_seed) {
10✔
NEW
422
    trace_printf(ctx->trace, 1, "mcsat supplement seeded %"PRIu32" atoms\n", seeded);
×
423
  }
424

425
  return CTX_NO_ERROR;
10✔
426
}
427

428
static bool mcsat_satellite_candidate_atom(term_table_t *terms, term_t atom) {
38✔
429
  switch (term_kind(terms, atom)) {
38✔
430
  case EQ_TERM:
20✔
431
  case DISTINCT_TERM:
432
  case ARITH_ROOT_ATOM:
433
  case ARITH_FF_EQ_ATOM:
434
  case ARITH_FF_BINEQ_ATOM:
435
  case ARITH_IS_INT_ATOM:
436
  case ARITH_EQ_ATOM:
437
  case ARITH_GE_ATOM:
438
  case ARITH_BINEQ_ATOM:
439
  case ARITH_DIVIDES_ATOM:
440
    return true;
20✔
441

442
  default:
18✔
443
    return false;
18✔
444
  }
445
}
446

447
static bool context_atom_requires_mcsat(context_t *ctx, term_t atom) {
556,813✔
448
  int_hmap_t cache;
449
  bool trigger, all_arith_mode;
450

451
  atom = unsigned_term(atom);
556,813✔
452
  if (!ctx->mcsat_supplement_active || !is_boolean_term(ctx->terms, atom)) {
556,813✔
453
    return false;
556,775✔
454
  }
455
  if (!mcsat_satellite_candidate_atom(ctx->terms, atom)) {
38✔
456
    return false;
18✔
457
  }
458

459
  init_int_hmap(&cache, 0);
20✔
460
  /*
461
   * In supplementary mode, route every arithmetic/FF atom to MCSAT so that
462
   * MCSAT sees the complete arithmetic conjunction (not just unsupported atoms).
463
   */
464
  all_arith_mode = true;
20✔
465
  if (all_arith_mode) {
20✔
466
    trigger = term_contains_arith_or_ff(ctx, atom, &cache);
20✔
467
  } else {
NEW
468
    trigger = term_requires_mcsat_supplement(ctx, atom, &cache);
×
469
  }
470
  delete_int_hmap(&cache);
20✔
471

472
  return trigger;
20✔
473
}
474

475
static literal_t map_mcsat_atom_to_literal(context_t *ctx, term_t atom) {
16✔
476
  mcsat_satellite_t *sat;
477
  literal_t l;
478
  void *obj;
479
  int32_t code;
480

481
  sat = context_mcsat_satellite(ctx);
16✔
482
  assert(sat != NULL);
483

484
  l = pos_lit(create_boolean_variable(ctx->core));
16✔
485
  obj = NULL;
16✔
486
  code = mcsat_satellite_register_atom(sat, atom, l, &obj);
16✔
487
  if (code < 0) {
16✔
NEW
488
    longjmp(ctx->env, code);
×
489
  }
490

491
  attach_atom_to_bvar(ctx->core, var_of(l), tagged_mcsat_atom(obj));
16✔
492
  return l;
16✔
493
}
494

495

496

497
/****************************************
498
 *  CONSTRUCTION OF EGRAPH OCCURRENCES  *
499
 ***************************************/
500

501
/*
502
 * Create a new egraph constant of the given type
503
 */
504
static eterm_t make_egraph_constant(context_t *ctx, type_t type, int32_t id) {
1,164✔
505
  assert(type_kind(ctx->types, type) == UNINTERPRETED_TYPE ||
506
         type_kind(ctx->types, type) == SCALAR_TYPE);
507
  return egraph_make_constant(ctx->egraph, type, id);
1,164✔
508
}
509

510

511
/*
512
 * Create a new egraph variable
513
 * - type = its type
514
 */
515
static eterm_t make_egraph_variable(context_t *ctx, type_t type) {
14,065✔
516
  eterm_t u;
517
  bvar_t v;
518

519
  if (type == bool_type(ctx->types)) {
14,065✔
520
    v = create_boolean_variable(ctx->core);
×
521
    u = egraph_bvar2term(ctx->egraph, v);
×
522
  } else {
523
    //    u = egraph_make_variable(ctx->egraph, type);
524
    // it's better to use skolem_term in case type is (tuple ...)
525
    u = egraph_skolem_term(ctx->egraph, type);
14,065✔
526
  }
527
  return u;
14,065✔
528
}
529

530

531
/*
532
 * Type of arithmetic variable x
533
 */
534
static type_t type_of_arithvar(context_t *ctx, thvar_t x) {
1,560✔
535
  type_t tau;
536

537
  tau = real_type(ctx->types);
1,560✔
538
  if (ctx->arith.arith_var_is_int(ctx->arith_solver, x)) {
1,560✔
539
    tau = int_type(ctx->types);
1,423✔
540
  }
541

542
  return tau;
1,560✔
543
}
544

545

546
/*
547
 * Convert arithmetic variable x to an egraph term
548
 */
549
static occ_t translate_arithvar_to_eterm(context_t *ctx, thvar_t x) {
1,569✔
550
  eterm_t u;
551
  type_t tau;
552

553
  u = ctx->arith.eterm_of_var(ctx->arith_solver, x);
1,569✔
554
  if (u == null_eterm) {
1,569✔
555
    tau = type_of_arithvar(ctx, x);
1,560✔
556
    u = egraph_thvar2term(ctx->egraph, x, tau);
1,560✔
557
  }
558

559
  return pos_occ(u);
1,569✔
560
}
561

562
/*
563
 * Convert bit-vector variable x to an egraph term
564
 * - tau = type of x
565
 */
566
static occ_t translate_bvvar_to_eterm(context_t *ctx, thvar_t x, type_t tau) {
3,693✔
567
  eterm_t u;
568

569
  u = ctx->bv.eterm_of_var(ctx->bv_solver, x);
3,693✔
570
  if (u == null_eterm) {
3,693✔
571
    u = egraph_thvar2term(ctx->egraph, x, tau);
3,672✔
572
  }
573

574
  return pos_occ(u);
3,693✔
575
}
576

577

578
/*
579
 * Convert variable x into an eterm internalization for t
580
 * - tau = type of t
581
 * - if x is mapped to an existing eterm u, return pos_occ(u)
582
 * - otherwise, create an egraph variable u and attach x to u
583
 *   then record the converse mapping [x --> u] in the relevant
584
 *   theory solver
585
 */
586
static occ_t translate_thvar_to_eterm(context_t *ctx, thvar_t x, type_t tau) {
3,348✔
587
  if (is_arithmetic_type(tau)) {
3,348✔
588
    return translate_arithvar_to_eterm(ctx, x);
619✔
589
  } else if (is_bv_type(ctx->types, tau)) {
2,729✔
590
    return translate_bvvar_to_eterm(ctx, x, tau);
2,729✔
591
  } else {
592
    longjmp(ctx->env, INTERNAL_ERROR);
×
593
  }
594
}
595

596

597
/*
598
 * Convert internalization code x for a term t into an egraph term
599
 * - t must be a root in the internalization table and must have
600
 *   positive polarity
601
 */
602
static occ_t translate_code_to_eterm(context_t *ctx, term_t t, int32_t x) {
64,857✔
603
  occ_t u;
604
  type_t tau;
605

606
  assert(is_pos_term(t) && intern_tbl_is_root(&ctx->intern, t) &&
607
         intern_tbl_map_of_root(&ctx->intern, t) == x);
608

609
  if (code_is_eterm(x)) {
64,857✔
610
    u = code2occ(x);
64,108✔
611
  } else {
612
    // x encodes a theory variable or a literal
613
    // convert that to an egraph term
614
    tau = type_of_root(ctx, t);
749✔
615
    switch (type_kind(ctx->types, tau)) {
749✔
616
    case BOOL_TYPE:
×
617
      u = egraph_literal2occ(ctx->egraph, code2literal(x));
×
618
      break;
×
619

620
    case INT_TYPE:
611✔
621
    case REAL_TYPE:
622
      u = translate_arithvar_to_eterm(ctx, code2thvar(x));
611✔
623
      break;
611✔
624

625
    case BITVECTOR_TYPE:
138✔
626
      u = translate_bvvar_to_eterm(ctx, code2thvar(x), tau);
138✔
627
      break;
138✔
628

629
    default:
×
630
      assert(false);
631
      longjmp(ctx->env, INTERNAL_ERROR);
×
632
    }
633

634
    // remap t to u
635
    intern_tbl_remap_root(&ctx->intern, t, occ2code(u));
749✔
636
  }
637

638
  return u;
64,857✔
639
}
640

641

642
/*
643
 * Internalization error for term t
644
 * - t can't be processed because there's no egraph
645
 * - the error code depends on t's type
646
 */
647
static int32_t uf_error_code(context_t *ctx, term_t t) {
×
648
  int32_t code;
649

650
  assert(! context_has_egraph(ctx));
651

652
  switch (term_type_kind(ctx->terms, t)) {
×
653
  case UNINTERPRETED_TYPE:
×
654
    code = UTYPE_NOT_SUPPORTED;
×
655
    break;
×
656

657
  case SCALAR_TYPE:
×
658
    code = SCALAR_NOT_SUPPORTED;
×
659
    break;
×
660

661
  case FUNCTION_TYPE:
×
662
    code = UF_NOT_SUPPORTED;
×
663
    break;
×
664

665
  case TUPLE_TYPE:
×
666
    code = TUPLE_NOT_SUPPORTED;
×
667
    break;
×
668

669
  default:
×
670
    assert(false);
671
    code = INTERNAL_ERROR;
×
672
    break;
×
673
  }
674

675
  return code;
×
676
}
677

678

679
/*
680
 * Utility to filter out high-order terms:
681
 * - check whether any term in a[0 ... n-1] has function type.
682
 * - if so throw an exception (via lonjmp) if the context does not include
683
 *   the array solver.
684
 */
685
static void check_high_order_support(context_t *ctx, const term_t *a, uint32_t n) {
17,982✔
686
  uint32_t i;
687

688
  if (! context_has_fun_solver(ctx)) {
17,982✔
689
    for (i=0; i<n; i++) {
30,058✔
690
      if (is_function_term(ctx->terms, a[i])) {
17,192✔
691
        longjmp(ctx->env, HIGH_ORDER_FUN_NOT_SUPPORTED);
3✔
692
      }
693
    }
694
  }
695
}
17,979✔
696

697

698
/***********************************************
699
 *  CONVERSION OF COMPOSITES TO EGRAPH TERMS   *
700
 **********************************************/
701

702
/*
703
 * Map apply term to an eterm
704
 * - tau = type of that term
705
 */
706
static occ_t map_apply_to_eterm(context_t *ctx, composite_term_t *app, type_t tau) {
9,665✔
707
  eterm_t u;
708
  occ_t *a;
709
  uint32_t i, n;
710

711
  assert(app->arity > 0);
712
  n = app->arity;
9,665✔
713

714
  check_high_order_support(ctx, app->arg+1, n-1);
9,665✔
715

716
  a = alloc_istack_array(&ctx->istack, n);
9,665✔
717
  for (i=0; i<n; i++) {
33,969✔
718
    a[i] = internalize_to_eterm(ctx, app->arg[i]);
24,304✔
719
  }
720

721
  // a[0] = function
722
  // a[1 ... n-1] are the arguments
723
  u = egraph_make_apply(ctx->egraph, a[0], n-1, a+1, tau);
9,665✔
724
  free_istack_array(&ctx->istack, a);
9,665✔
725

726
  //  add_type_constraints(ctx, pos_occ(u), tau);
727

728
  return pos_occ(u);
9,665✔
729
}
730

731

732
/*
733
 * Build a tuple of same type as t then assert that it's equal to u1
734
 * - t must be a root in the internalization table
735
 * - u1 must be equal to t's internalization (as stored in intern_table)
736
 * This is the skolemization of (exist (x1...x_n) u1 == (tuple x1 ... x_n))
737
 *
738
 * - return the eterm u := (tuple x1 ... x_n)
739
 */
740
static eterm_t skolem_tuple(context_t *ctx, term_t t, occ_t u1) {
×
741
  type_t tau;
742
  eterm_t u;
743

744
  assert(intern_tbl_is_root(&ctx->intern, t) && is_pos_term(t) &&
745
         intern_tbl_map_of_root(&ctx->intern, t) == occ2code(u1));
746

747
  tau = intern_tbl_type_of_root(&ctx->intern, t);
×
748
  u = egraph_skolem_term(ctx->egraph, tau);
×
749
  egraph_assert_eq_axiom(ctx->egraph, u1, pos_occ(u));
×
750

751
  return u;
×
752
}
753

754

755
/*
756
 * Convert (select i t) to an egraph term
757
 * - tau must be the type of that term (should not be bool)
758
 * - if a new eterm u is created, attach a theory variable to it
759
 */
760
static occ_t map_select_to_eterm(context_t *ctx, select_term_t *s, type_t tau) {
245✔
761
  occ_t u1;
762
  eterm_t tuple;
763
  composite_t *tp;
764

765
  u1 = internalize_to_eterm(ctx, s->arg);
245✔
766
  tuple = egraph_get_tuple_in_class(ctx->egraph, term_of_occ(u1));
245✔
767
  if (tuple == null_eterm) {
245✔
768
    tuple = skolem_tuple(ctx, s->arg, u1);
×
769
  }
770

771
  tp = egraph_term_body(ctx->egraph, tuple);
245✔
772
  assert(composite_body(tp) && tp != NULL && composite_kind(tp) == COMPOSITE_TUPLE);
773

774
  return tp->child[s->idx];
245✔
775
}
776

777

778
/*
779
 * Convert a conditional expression to an egraph term
780
 * - c = conditional descriptor
781
 * - tau = type of c
782
 */
783
static occ_t map_conditional_to_eterm(context_t *ctx, conditional_t *c, type_t tau) {
12✔
784
  literal_t *a;
785
  occ_t u, v;
786
  uint32_t i, n;
787
  literal_t l;
788
  bool all_false;
789
  term_t t;
790

791
#if 0
792
  printf("---> conditional to eterm\n");
793
#endif
794

795
  t = simplify_conditional(ctx, c);
12✔
796
  if (t != NULL_TERM) {
12✔
797
    return internalize_to_eterm(ctx, t);
4✔
798
  }
799

800
  n = c->nconds;
8✔
801
  a = alloc_istack_array(&ctx->istack, n + 1);
8✔
802

803
  all_false = true;
8✔
804
  u = null_occurrence;
8✔
805

806
  for (i=0; i<n; i++) {
24✔
807
    a[i] = internalize_to_literal(ctx, c->pair[i].cond);
16✔
808
    if (a[i] == true_literal) {
16✔
809
      /*
810
       * a[0] ... a[i-1] are all reducible to false
811
       * but we can't assume that a[0] ... a[i-1] are all false_literals
812
       * since we don't know how the theory solver internalizes the
813
       * conditions.
814
       */
815
      v = internalize_to_eterm(ctx, c->pair[i].val);
×
816
      if (all_false) {
×
817
        // all previous conditions a[0 ... i-1] are false
818
        assert(u == null_occurrence);
819
        u = v;
×
820
      } else {
821
        // we assert (u == v) as a top-level equality
822
        egraph_assert_eq_axiom(ctx->egraph, u, v);
×
823
      }
824
      goto done;
×
825
    }
826
    if (a[i] != false_literal) {
16✔
827
      if (all_false) {
13✔
828
        assert(u == null_occurrence);
829
        u = pos_occ(make_egraph_variable(ctx, tau));
7✔
830
        all_false = false;
7✔
831
      }
832
      // one clause for a[i] => (u = v[i])
833
      v = internalize_to_eterm(ctx, c->pair[i].val);
13✔
834
      l = egraph_make_eq(ctx->egraph, u, v);
13✔
835
      add_binary_clause(ctx->core, not(a[i]), l);
13✔
836
    }
837
  }
838

839
  if (all_false) {
8✔
840
    assert(u == null_occurrence);
841
    u = internalize_to_eterm(ctx, c->defval);
1✔
842
    goto done;
1✔
843
  }
844

845
  // clause for the default value
846
  assert(u != null_occurrence);
847
  v = internalize_to_eterm(ctx, c->defval);
7✔
848
  l = egraph_make_eq(ctx->egraph, u, v);
7✔
849
  a[n] = l;
7✔
850
  add_clause(ctx->core, n+1, a);
7✔
851

852
 done:
8✔
853
  free_istack_array(&ctx->istack, a);
8✔
854

855
  return u;
8✔
856
}
857

858

859
/*
860
 * Auxiliary function for flattening if-then-else
861
 * - v contains a conjunction of n literals: l0 /\ ... /\ l_n
862
 * - we something like (l0 /\ ... /\ l_n implies (x = y))
863
 *   (i.e., (not l0) \/ ... \/ (not l_n) \/ (x = y)
864
 * - this function negates all the literals in place
865
 */
866
static void ite_prepare_antecedents(ivector_t *v) {
8,348✔
867
  uint32_t i, n;
868

869
  n = v->size;
8,348✔
870
  for (i=0; i<n; i++) {
20,408✔
871
    v->data[i] = not(v->data[i]);
12,060✔
872
  }
873
}
8,348✔
874

875

876
/*
877
 * Convert nested if-then-else to  an egraph term
878
 * - ite = term of the form (ite c1 t1 t2)
879
 * - c = internalization of c1
880
 * - tau = type of the term (ite c1 t1 t2)
881
 */
882
static occ_t flatten_ite_to_eterm(context_t *ctx, composite_term_t *ite, literal_t c, type_t tau) {
1,168✔
883
  ite_flattener_t *flattener;
884
  ivector_t *buffer;
885
  term_t x;
886
  occ_t u, v;
887
  literal_t l;
888

889
  u = pos_occ(make_egraph_variable(ctx, tau));
1,168✔
890

891
  flattener = objstack_alloc(&ctx->ostack, sizeof(ite_flattener_t), (cleaner_t) delete_ite_flattener);
1,168✔
892
  init_ite_flattener(flattener);
1,168✔
893

894
  ite_flattener_push(flattener, ite, c);
1,168✔
895

896
  while (ite_flattener_is_nonempty(flattener)) {
4,210✔
897
    if (ite_flattener_last_lit_false(flattener)) {
3,042✔
898
      // dead branch
899
      ite_flattener_next_branch(flattener);
6✔
900
      continue;
6✔
901
    }
902
    assert(ite_flattener_branch_is_live(flattener));
903

904
    x = ite_flattener_leaf(flattener);
3,036✔
905
    x = intern_tbl_get_root(&ctx->intern, x);
3,036✔
906

907
    /*
908
     * x is the current leaf.
909
     * If it's (ite ...) then we can expand the tree by pushing x.
910
     *
911
     * Heuristic: we don't do it if x is a shared term or if it's
912
     * already internalized.
913
     * - we also need a cutoff since the number of branches grows
914
     *   exponentially.
915
     */
916
    if (is_pos_term(x) &&
6,072✔
917
        is_ite_term(ctx->terms, x) &&
3,036✔
918
        !intern_tbl_root_is_mapped(&ctx->intern, x) &&
2,473✔
919
        term_is_not_shared(&ctx->sharing, x)) {
774✔
920
      /*
921
       * x is of the form (ite c a b) and not internalized already,
922
       * we push (ite c a b) on the flattener.
923
       */
924
      ite = ite_term_desc(ctx->terms, x);
353✔
925
      assert(ite->arity == 3);
926
      c = internalize_to_literal(ctx, ite->arg[0]);
353✔
927
      ite_flattener_push(flattener, ite, c);
353✔
928
    } else {
929
      /*
930
       * Add the clause [branch conditions => x = u]
931
       */
932
      v = internalize_to_eterm(ctx, x);
2,683✔
933
      l = egraph_make_eq(ctx->egraph, u, v);
2,683✔
934

935
      buffer = &ctx->aux_vector;
2,683✔
936
      assert(buffer->size == 0);
937
      ite_flattener_get_clause(flattener, buffer);
2,683✔
938
      ite_prepare_antecedents(buffer);
2,683✔
939
      ivector_push(buffer, l);
2,683✔
940
      add_clause(ctx->core, buffer->size, buffer->data);
2,683✔
941
      ivector_reset(buffer);
2,683✔
942

943
      ite_flattener_next_branch(flattener);
2,683✔
944
    }
945
  }
946

947
  //  delete_ite_flattener(&flattener);
948
  objstack_pop(&ctx->ostack);
1,168✔
949

950
  return u;
1,168✔
951
}
952

953

954
/*
955
 * Convert (ite c t1 t2) to an egraph term
956
 * - tau = type of (ite c t1 t2)
957
 */
958
static occ_t map_ite_to_eterm(context_t *ctx, composite_term_t *ite, type_t tau) {
2,168✔
959
  conditional_t *d;
960
  eterm_t u;
961
  occ_t u1, u2, u3;
962
  literal_t c, l1, l2;
963

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

968
  d = context_make_conditional(ctx, ite);
2,168✔
969
  if (d != NULL) {
2,168✔
970
    u1 = map_conditional_to_eterm(ctx, d, tau);
12✔
971
    context_free_conditional(ctx, d);
12✔
972
    return u1;
12✔
973
  }
974

975
  c = internalize_to_literal(ctx, ite->arg[0]);
2,156✔
976
  if (c == true_literal) {
2,156✔
977
    return internalize_to_eterm(ctx, ite->arg[1]);
110✔
978
  }
979
  if (c == false_literal) {
2,046✔
980
    return internalize_to_eterm(ctx, ite->arg[2]);
15✔
981
  }
982

983
  if (context_ite_flattening_enabled(ctx)) {
2,031✔
984
    return flatten_ite_to_eterm(ctx, ite, c, tau);
1,168✔
985
  }
986

987
  u2 = internalize_to_eterm(ctx, ite->arg[1]);
863✔
988
  u3 = internalize_to_eterm(ctx, ite->arg[2]);
863✔
989

990
  if (context_keep_ite_enabled(ctx)) {
863✔
991
    // build the if-then-else in the egraph
992
    u1 = egraph_literal2occ(ctx->egraph, c);
2✔
993
    u = egraph_make_ite(ctx->egraph, u1, u2, u3, tau);
2✔
994
  } else {
995
    // eliminate the if-then-else
996
    u = make_egraph_variable(ctx, tau);
861✔
997
    l1 = egraph_make_eq(ctx->egraph, pos_occ(u), u2);
861✔
998
    l2 = egraph_make_eq(ctx->egraph, pos_occ(u), u3);
861✔
999

1000
    assert_ite(&ctx->gate_manager, c, l1, l2, true);
861✔
1001
  }
1002

1003
  return pos_occ(u);
863✔
1004
}
1005

1006

1007

1008
/*
1009
 * Convert (update f t_1 ... t_n v) to a term
1010
 * - tau = type of that term
1011
 */
1012
static occ_t map_update_to_eterm(context_t *ctx, composite_term_t *update, type_t tau) {
11,470✔
1013
  eterm_t u;
1014
  occ_t *a;
1015
  uint32_t i, n;
1016

1017
  assert(update->arity > 2);
1018

1019
  n = update->arity;
11,470✔
1020
  a = alloc_istack_array(&ctx->istack, n);
11,470✔
1021
  for (i=0; i<n; i++) {
45,880✔
1022
    a[i] = internalize_to_eterm(ctx, update->arg[i]);
34,410✔
1023
  }
1024

1025
  // a[0]: function f
1026
  // a[1] ... a[n-2]: t_1 .. t_{n-2}
1027
  // a[n-1]: new value v
1028
  u = egraph_make_update(ctx->egraph, a[0], n-2, a+1, a[n-1], tau);
11,470✔
1029

1030
  free_istack_array(&ctx->istack, a);
11,470✔
1031

1032
  return pos_occ(u);
11,470✔
1033
}
1034

1035

1036

1037
/*
1038
 * Convert (tuple t_1 ... t_n) to a term
1039
 * - tau = type of the tuple
1040
 */
1041
static occ_t map_tuple_to_eterm(context_t *ctx, composite_term_t *tuple, type_t tau) {
190✔
1042
  eterm_t u;
1043
  occ_t *a;
1044
  uint32_t i, n;
1045

1046
  n = tuple->arity;
190✔
1047

1048
  check_high_order_support(ctx, tuple->arg, n);
190✔
1049

1050
  a = alloc_istack_array(&ctx->istack, n);
190✔
1051
  for (i=0; i<n; i++) {
560✔
1052
    a[i] = internalize_to_eterm(ctx, tuple->arg[i]);
370✔
1053
  }
1054

1055
  u = egraph_make_tuple(ctx->egraph, n, a, tau);
190✔
1056
  free_istack_array(&ctx->istack, a);
190✔
1057

1058
  return pos_occ(u);
190✔
1059
}
1060

1061

1062
/*
1063
 * Convert arithmetic and bitvector constants to eterm
1064
 * - check whether the relevant solver exists first
1065
 * - then map the constant to a solver variable x
1066
 *   and convert x to an egraph occurrence
1067
 */
1068
static occ_t map_arith_constant_to_eterm(context_t *ctx, rational_t *q) {
337✔
1069
  thvar_t x;
1070

1071
  if (! context_has_arith_solver(ctx)) {
337✔
1072
    longjmp(ctx->env, ARITH_NOT_SUPPORTED);
×
1073
  }
1074

1075
  x = ctx->arith.create_const(ctx->arith_solver, q);
337✔
1076
  return translate_arithvar_to_eterm(ctx, x);
337✔
1077
}
1078

1079
static occ_t map_bvconst64_to_eterm(context_t *ctx, bvconst64_term_t *c) {
808✔
1080
  thvar_t x;
1081
  type_t tau;
1082

1083
  if (! context_has_bv_solver(ctx)) {
808✔
1084
    longjmp(ctx->env, BV_NOT_SUPPORTED);
×
1085
  }
1086

1087
  x = ctx->bv.create_const64(ctx->bv_solver, c);
808✔
1088
  tau = bv_type(ctx->types, c->bitsize);
808✔
1089

1090
  return translate_bvvar_to_eterm(ctx, x, tau);
808✔
1091
}
1092

1093
static occ_t map_bvconst_to_eterm(context_t *ctx, bvconst_term_t *c) {
18✔
1094
  thvar_t x;
1095
  type_t tau;
1096

1097
  if (! context_has_bv_solver(ctx)) {
18✔
1098
    longjmp(ctx->env, BV_NOT_SUPPORTED);
×
1099
  }
1100

1101
  x = ctx->bv.create_const(ctx->bv_solver, c);
18✔
1102
  tau = bv_type(ctx->types, c->bitsize);
18✔
1103

1104
  return translate_bvvar_to_eterm(ctx, x, tau);
18✔
1105
}
1106

1107

1108

1109
/***************************************
1110
 *  AXIOMS FOR DIV/MOD/FLOOR/CEIL/ABS  *
1111
 **************************************/
1112

1113
/*
1114
 * Auxiliary function: p and map to represent (x - y)
1115
 * - in polynomial p, only the coefficients are relevant
1116
 * - map[0] stores x and map[1] stores y
1117
 * - both p map must be large enough (at least 2 elements)
1118
 */
1119
static void context_store_diff_poly(polynomial_t *p, thvar_t *map, thvar_t x, thvar_t y) {
×
1120
  p->nterms = 2;
×
1121
  p->mono[0].var = 1;
×
1122
  q_set_one(&p->mono[0].coeff);       // coeff of x = 1
×
1123
  p->mono[1].var = 2;
×
1124
  q_set_minus_one(&p->mono[1].coeff); // coeff of y = -1
×
1125
  p->mono[2].var = max_idx; // end marker
×
1126

1127
  map[0] = x;
×
1128
  map[1] = y;
×
1129
}
×
1130

1131

1132
/*
1133
 * Same thing for the polynomial (x - y - 1)
1134
 */
1135
static void context_store_diff_minus_one_poly(polynomial_t *p, thvar_t *map, thvar_t x, thvar_t y) {
×
1136
  p->nterms = 3;
×
1137
  p->mono[0].var = const_idx;
×
1138
  q_set_minus_one(&p->mono[0].coeff);  // constant = -1
×
1139
  p->mono[1].var = 1;
×
1140
  q_set_one(&p->mono[1].coeff);        // coeff of x = 1
×
1141
  p->mono[2].var = 2;
×
1142
  q_set_minus_one(&p->mono[2].coeff);  // coeff of y = -1
×
1143
  p->mono[3].var = max_idx;
×
1144

1145
  map[0] = null_thvar; // constant
×
1146
  map[1] = x;
×
1147
  map[2] = y;
×
1148
}
×
1149

1150

1151
/*
1152
 * Same thing for the polynomial (x + y)
1153
 */
1154
static void context_store_sum_poly(polynomial_t *p, thvar_t *map, thvar_t x, thvar_t y) {
4✔
1155
  p->nterms = 2;
4✔
1156
  p->mono[0].var = 1;
4✔
1157
  q_set_one(&p->mono[0].coeff); // coeff of x = 1
4✔
1158
  p->mono[1].var = 2;
4✔
1159
  q_set_one(&p->mono[1].coeff); // coeff of y = 1
4✔
1160
  p->mono[2].var = max_idx;
4✔
1161

1162
  map[0] = x;
4✔
1163
  map[1] = y;
4✔
1164
}
4✔
1165

1166

1167
/*
1168
 * The lower bound on y = (div x k)  is (k * y <= x) or (x - k * y >= 0)
1169
 * We store the polynomial x - k * y
1170
 */
1171
static void context_store_div_lower_bound(polynomial_t *p, thvar_t *map, thvar_t x, thvar_t y, const rational_t *k) {
21✔
1172
  p->nterms = 2;
21✔
1173
  p->mono[0].var = 1;
21✔
1174
  q_set_one(&p->mono[0].coeff);    // coeff of x = 1
21✔
1175
  p->mono[1].var = 2;
21✔
1176
  q_set_neg(&p->mono[1].coeff, k); // coeff of y = -k
21✔
1177
  p->mono[2].var = max_idx;
21✔
1178

1179
  map[0] = x;
21✔
1180
  map[1] = y;
21✔
1181
}
21✔
1182

1183

1184
/*
1185
 * For converting (divides k x), we use (divides k x) <=> (x <= k * (div x k))
1186
 * or (k * y - x >= 0) for y = (div x k).
1187
 * We store the polynomial - x + k * y.
1188
 */
1189
static void context_store_divides_constraint(polynomial_t *p, thvar_t *map, thvar_t x, thvar_t y, const rational_t *k) {
8✔
1190
  p->nterms = 2;
8✔
1191
  p->mono[0].var = 1;
8✔
1192
  q_set_minus_one(&p->mono[0].coeff);  // coeff of x = -1
8✔
1193
  p->mono[1].var = 2;
8✔
1194
  q_set(&p->mono[1].coeff, k);         // coeff of y = k
8✔
1195
  p->mono[2].var = max_idx;
8✔
1196

1197
  map[0] = x;
8✔
1198
  map[1] = y;
8✔
1199
}
8✔
1200

1201
/*
1202
 * Upper bound on y = (div x k) when both x and y are integer:
1203
 * We have x <= k * y + |k| - 1 or (-x + k y + |k| - 1 >= 0).
1204
 *
1205
 * We store the polynomial - x + k y + |k| - 1
1206
 *
1207
 * NOTE: we don't normalize the constant (|k| - 1) to zero if |k| = 1.
1208
 * This is safe as the simplex solver does not care.
1209
 */
1210
static void context_store_integer_div_upper_bound(polynomial_t *p, thvar_t *map, thvar_t x, thvar_t y, const rational_t *k) {
20✔
1211
  p->nterms = 3;
20✔
1212
  p->mono[0].var = const_idx;
20✔
1213
  q_set_abs(&p->mono[0].coeff, k);
20✔
1214
  q_sub_one(&p->mono[0].coeff);        // constant term = |k| - 1
20✔
1215
  p->mono[1].var = 1;
20✔
1216
  q_set_minus_one(&p->mono[1].coeff);  // coeff of x = -1
20✔
1217
  p->mono[2].var = 2;
20✔
1218
  q_set(&p->mono[2].coeff, k);         // coeff of y = k
20✔
1219
  p->mono[3].var = max_idx;
20✔
1220

1221
  map[0] = null_thvar;
20✔
1222
  map[1] = x;
20✔
1223
  map[2] = y;
20✔
1224
}
20✔
1225

1226
/*
1227
 * Upper bound on y = (div x k) when x or k is not an integer.
1228
 * We have x < k * y + |k| or x - k*y - |k| < 0 or (not (x - k*y - |k| >= 0))
1229
 *
1230
 * We store the polynomial x - ky - |k|
1231
 */
1232
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✔
1233
  p->nterms = 3;
1✔
1234
  p->mono[0].var = const_idx;
1✔
1235
  q_set_abs(&p->mono[0].coeff, k);
1✔
1236
  q_neg(&p->mono[0].coeff);           // constant term: -|k|
1✔
1237
  p->mono[1].var = 1;
1✔
1238
  q_set_one(&p->mono[1].coeff);       // coeff of x = +1
1✔
1239
  p->mono[2].var = 2;
1✔
1240
  q_set_neg(&p->mono[2].coeff, k);    // coeff of y = -k
1✔
1241
  p->mono[3].var = max_idx;
1✔
1242

1243
  map[0] = null_thvar;
1✔
1244
  map[1] = x;
1✔
1245
  map[2] = y;
1✔
1246
}
1✔
1247

1248

1249
/*
1250
 * Polynomial x - y + k d (for asserting y = k * (div y k) + (mod y k)
1251
 * - d is assumed to be (div y k)
1252
 * - x is assumed to be (mod y k)
1253
 */
1254
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✔
1255
  p->nterms = 3;
11✔
1256
  p->mono[0].var = 1;
11✔
1257
  q_set_one(&p->mono[0].coeff);       // coefficient of x = 1
11✔
1258
  p->mono[1].var = 2;
11✔
1259
  q_set_minus_one(&p->mono[1].coeff); // coefficient of y = -1
11✔
1260
  p->mono[2].var = 3;
11✔
1261
  q_set(&p->mono[2].coeff, k);        // coefficient of d = k
11✔
1262
  p->mono[3].var = max_idx;
11✔
1263

1264
  map[0] = x;
11✔
1265
  map[1] = y;
11✔
1266
  map[2] = d;
11✔
1267
}
11✔
1268

1269

1270
/*
1271
 * Bound on x = (mod y k) assuming x and k are integer:
1272
 * - the bound is x <= |k| - 1 (i.e., |k| - 1 - x >= 0)
1273
 *   so we construct |k| - 1 - x
1274
 */
1275
static void context_store_integer_mod_bound(polynomial_t *p, thvar_t *map, thvar_t x, const rational_t *k) {
10✔
1276
  p->nterms = 2;
10✔
1277
  p->mono[0].var = const_idx;
10✔
1278
  q_set_abs(&p->mono[0].coeff, k);
10✔
1279
  q_sub_one(&p->mono[0].coeff);        // constant = |k| - 1
10✔
1280
  p->mono[1].var = 1;
10✔
1281
  q_set_minus_one(&p->mono[1].coeff);  // coeff of x = -1
10✔
1282
  p->mono[2].var = max_idx;
10✔
1283

1284
  map[0] = null_thvar;
10✔
1285
  map[1] = x;
10✔
1286
}
10✔
1287

1288

1289
/*
1290
 * Bound on x = (mod y k) when x or k are rational
1291
 * - the bound is x < |k| or x - |k| < 0 or (not (x - |k| >= 0))
1292
 *   so we construct x - |k|
1293
 */
1294
static void context_store_rational_mod_bound(polynomial_t *p, thvar_t *map, thvar_t x, const rational_t *k) {
1✔
1295
  p->nterms = 2;
1✔
1296
  p->mono[0].var = const_idx;
1✔
1297
  q_set_abs(&p->mono[0].coeff, k);
1✔
1298
  q_neg(&p->mono[0].coeff);            // constant = -|k|
1✔
1299
  p->mono[1].var = 1;
1✔
1300
  q_set_one(&p->mono[1].coeff);        // coeff of x = +1
1✔
1301
  p->mono[2].var = max_idx;
1✔
1302

1303
  map[0] = null_thvar;
1✔
1304
  map[1] = x;
1✔
1305
}
1✔
1306

1307

1308
/*
1309
 * Assert constraints for x := floor(y)
1310
 * - both x and y are variables in the arithmetic solver
1311
 * - x has type integer
1312
 *
1313
 * We assert (x <= y && y < x+1)
1314
 */
1315
static void assert_floor_axioms(context_t *ctx, thvar_t x, thvar_t y) {
×
1316
  polynomial_t *p;
1317
  thvar_t map[3];
1318

1319
  assert(ctx->arith.arith_var_is_int(ctx->arith_solver, x));
1320

1321
  p = context_get_aux_poly(ctx, 4);
×
1322

1323
  // assert (y - x >= 0)
1324
  context_store_diff_poly(p, map, y, x);
×
1325
  ctx->arith.assert_poly_ge_axiom(ctx->arith_solver, p, map, true);
×
1326

1327
  // assert (y - x - 1 < 0) <=> (not (y - x - 1) >= 0)
1328
  context_store_diff_minus_one_poly(p, map, y, x);
×
1329
  ctx->arith.assert_poly_ge_axiom(ctx->arith_solver, p, map, false);
×
1330
}
×
1331

1332

1333
/*
1334
 * Assert constraints for x == ceil(y)
1335
 * - both x and y are variables in the arithmetic solver
1336
 * - x has type integer
1337
 *
1338
 * We assert (x - 1 < y && y <= x)
1339
 */
1340
static void assert_ceil_axioms(context_t *ctx, thvar_t x, thvar_t y) {
×
1341
  polynomial_t *p;
1342
  thvar_t map[3];
1343

1344
  assert(ctx->arith.arith_var_is_int(ctx->arith_solver, x));
1345

1346
  p = context_get_aux_poly(ctx, 4);
×
1347

1348
  // assert (x - y >= 0)
1349
  context_store_diff_poly(p, map, x, y);
×
1350
  ctx->arith.assert_poly_ge_axiom(ctx->arith_solver, p, map, true);
×
1351

1352
  // assert (x - y - 1 < 0) <=> (not (x - y - 1) >= 0)
1353
  context_store_diff_minus_one_poly(p, map, x, y);
×
1354
  ctx->arith.assert_poly_ge_axiom(ctx->arith_solver, p, map, false);
×
1355
}
×
1356

1357

1358
/*
1359
 * Assert constraints for x == abs(y)
1360
 * - x and y must be variables in the arithmetic solver
1361
 *
1362
 * We assert (x >= 0) AND ((x == y) or (x == -y))
1363
 */
1364
static void assert_abs_axioms(context_t *ctx, thvar_t x, thvar_t y) {
4✔
1365
  polynomial_t *p;
1366
  thvar_t map[2];
1367
  literal_t l1, l2;
1368

1369
  // assert (x >= 0)
1370
  ctx->arith.assert_ge_axiom(ctx->arith_solver, x, true);
4✔
1371

1372
  // create l1 := (x == y)
1373
  l1 = ctx->arith.create_vareq_atom(ctx->arith_solver, x, y);
4✔
1374

1375
  // create l2 := (x == -y) that is (x + y == 0)
1376
  p = context_get_aux_poly(ctx, 3);
4✔
1377
  context_store_sum_poly(p, map, x, y);
4✔
1378
  l2 = ctx->arith.create_poly_eq_atom(ctx->arith_solver, p, map);
4✔
1379

1380
  // assert (or l1 l2)
1381
  add_binary_clause(ctx->core, l1, l2);
4✔
1382
}
4✔
1383

1384

1385
/*
1386
 * Constraints for x == (div y k)
1387
 * - x and y must be variables in the arithmetic solver
1388
 * - x must be an integer variable
1389
 * - k is a non-zero rational constant
1390
 *
1391
 * If k and y are integer, we assert
1392
 *   k * x <= y <= k * x + |k| - 1
1393
 *
1394
 * Otherwise, we assert
1395
 *   k * x <= y < k * x + |k|
1396
 */
1397
static void assert_div_axioms(context_t *ctx, thvar_t x, thvar_t y, const rational_t *k) {
21✔
1398
  polynomial_t *p;
1399
  thvar_t map[3];
1400

1401
  p = context_get_aux_poly(ctx, 4);
21✔
1402

1403
  // assert k*x <= y (i.e., y - k*x >= 0)
1404
  context_store_div_lower_bound(p, map, y, x, k);
21✔
1405
  ctx->arith.assert_poly_ge_axiom(ctx->arith_solver, p, map, true);
21✔
1406

1407
  if (ctx->arith.arith_var_is_int(ctx->arith_solver, y) && q_is_integer(k)) {
21✔
1408
    // y and k are both integer
1409
    // assert y <= k*x + |k| - 1 (i.e., - y + k x + |k| - 1 >= 0)
1410
    context_store_integer_div_upper_bound(p, map, y, x, k);
20✔
1411
    ctx->arith.assert_poly_ge_axiom(ctx->arith_solver, p, map, true);
20✔
1412

1413
  } else {
1414
    // assert y < k*x + |k| (i.e., y - k*x - |k| < 0) or (not (y - k*x - |k| >= 0))
1415
    context_store_rational_div_upper_bound(p, map, y, x, k);
1✔
1416
    ctx->arith.assert_poly_ge_axiom(ctx->arith_solver, p, map, false);
1✔
1417
  }
1418
}
21✔
1419

1420

1421
/*
1422
 * Constraints for x == (mod y k)
1423
 * - d must be the variable equal to (div y k)
1424
 * - x and y must be variables in the arithmetic solver
1425
 * - k is a non-zero rational constant.
1426
 *
1427
 * We assert x = y - k * d (i.e., (mod y k) = x - k * (div y k))
1428
 * and 0 <= x < |k|.
1429
 *
1430
 * NOTE: The 0 <= x < |k| part is redundant. It's implied by the
1431
 * div_axioms for d = (div y k). It's cheap enough that I can't
1432
 * see a problem with adding it anyway (it's just an interval for x).
1433
 */
1434
static void assert_mod_axioms(context_t *ctx, thvar_t x, thvar_t y, thvar_t d, const rational_t *k) {
11✔
1435
  polynomial_t *p;
1436
  thvar_t map[3];
1437

1438
  p = context_get_aux_poly(ctx, 4);
11✔
1439

1440
  // assert y = k * d + x (i.e., x - y + k *d = 0)
1441
  context_store_divmod_eq(p, map, x, y, d, k);
11✔
1442
  ctx->arith.assert_poly_eq_axiom(ctx->arith_solver, p, map, true);
11✔
1443

1444
  // assert x >= 0
1445
  ctx->arith.assert_ge_axiom(ctx->arith_solver, x, true);
11✔
1446

1447
  if (ctx->arith.arith_var_is_int(ctx->arith_solver, x) && q_is_integer(k)) {
11✔
1448
    // both x and |k| are integer
1449
    // assert x <= |k| - 1, i.e., -x + |k| - 1 >= 0
1450
    context_store_integer_mod_bound(p, map, x, k);
10✔
1451
    ctx->arith.assert_poly_ge_axiom(ctx->arith_solver, p, map, true);
10✔
1452
  } else {
1453
    // assert x < |k|, i.e., x - |k| <0, i.e., (not (x - |k| >= 0))
1454
    context_store_rational_mod_bound(p, map, x, k);
1✔
1455
    ctx->arith.assert_poly_ge_axiom(ctx->arith_solver, p, map, false);
1✔
1456
  }
1457
}
11✔
1458

1459

1460

1461
/******************************************************
1462
 *  CONVERSION OF COMPOSITES TO ARITHMETIC VARIABLES  *
1463
 *****************************************************/
1464

1465
/*
1466
 * Convert a conditional to an arithmetic variable
1467
 * - if is_int is true, the variable is integer otherwise, it's real
1468
 */
1469
static thvar_t map_conditional_to_arith(context_t *ctx, conditional_t *c, bool is_int) {
239✔
1470
  literal_t *a;
1471
  uint32_t i, n;
1472
  thvar_t x, v;
1473
  bool all_false;
1474
  term_t t;
1475

1476
#if 0
1477
  printf("---> conditional to arith\n");
1478
#endif
1479

1480
  t = simplify_conditional(ctx, c);
239✔
1481
  if (t != NULL_TERM) {
239✔
1482
    return internalize_to_arith(ctx, t);
13✔
1483
  }
1484

1485
  n = c->nconds;
226✔
1486
  a = alloc_istack_array(&ctx->istack, n);
226✔
1487

1488
  all_false = true;
226✔
1489
  v = null_thvar;
226✔
1490

1491
  for (i=0; i<n; i++) {
703✔
1492
    a[i] = internalize_to_literal(ctx, c->pair[i].cond);
485✔
1493
    if (a[i] == true_literal) {
485✔
1494
      /*
1495
       * a[0] ... a[i-1] are all reducible to false
1496
       * but we can't assume v == null_thvar, since
1497
       * we don't know how the theory solver internalizes
1498
       * the conditions (i.e., some of them may not be false_literal).
1499
       */
1500
      x = internalize_to_arith(ctx, c->pair[i].val);
8✔
1501
      if (all_false) {
8✔
1502
        assert(v == null_thvar);
1503
        v = x;
6✔
1504
      } else {
1505
        // assert (v == x) in the arithmetic solver
1506
        ctx->arith.assert_vareq_axiom(ctx->arith_solver, v, x, true);
2✔
1507
      }
1508
      goto done;
8✔
1509
    }
1510
    if (a[i] != false_literal) {
477✔
1511
      if (all_false) {
460✔
1512
        assert(v == null_thvar);
1513
        v = ctx->arith.create_var(ctx->arith_solver, is_int);
215✔
1514
        all_false = false;
215✔
1515
      }
1516
      // clause for a[i] => (v = c->pair[i].val)
1517
      x = internalize_to_arith(ctx, c->pair[i].val);
460✔
1518
      ctx->arith.assert_cond_vareq_axiom(ctx->arith_solver, a[i], v, x);
460✔
1519
    }
1520
  }
1521

1522
  if (all_false) {
218✔
1523
    assert(v == null_thvar);
1524
    v = internalize_to_arith(ctx, c->defval);
5✔
1525
    goto done;
5✔
1526
  }
1527

1528
  /*
1529
   * last clause (only if some a[i] isn't false):
1530
   * (a[0] \/ ... \/ a[n-1] \/ v == c->defval)
1531
   */
1532
  assert(v != null_thvar);
1533
  x = internalize_to_arith(ctx, c->defval);
213✔
1534
  ctx->arith.assert_clause_vareq_axiom(ctx->arith_solver, n, a, v, x);
213✔
1535

1536
 done:
226✔
1537
  free_istack_array(&ctx->istack, a);
226✔
1538
  return v;
226✔
1539
}
1540

1541

1542
/*
1543
 * Convert nested if-then-else to  an arithmetic variable
1544
 * - ite = term of the form (ite c1 t1 t2)
1545
 * - c = internalization of c1
1546
 * - is_int = true if the if-then-else term is integer (otherwise it's real)
1547
 */
1548
static thvar_t flatten_ite_to_arith(context_t *ctx, composite_term_t *ite, literal_t c, bool is_int) {
2,521✔
1549
  ite_flattener_t *flattener;
1550
  ivector_t *buffer;
1551
  term_t x;
1552
  thvar_t u, v;
1553

1554
  u = ctx->arith.create_var(ctx->arith_solver, is_int);
2,521✔
1555

1556
  flattener = objstack_alloc(&ctx->ostack, sizeof(ite_flattener_t), (cleaner_t) delete_ite_flattener);
2,521✔
1557
  init_ite_flattener(flattener);
2,521✔
1558

1559
  ite_flattener_push(flattener, ite, c);
2,521✔
1560

1561
  while (ite_flattener_is_nonempty(flattener)) {
8,823✔
1562
    if (ite_flattener_last_lit_false(flattener)) {
6,302✔
1563
      // dead branch
1564
      ite_flattener_next_branch(flattener);
7✔
1565
      continue;
7✔
1566
    }
1567
    assert(ite_flattener_branch_is_live(flattener));
1568

1569
    x = ite_flattener_leaf(flattener);
6,295✔
1570
    x = intern_tbl_get_root(&ctx->intern, x);
6,295✔
1571

1572
    /*
1573
     * x is the current leaf
1574
     * If x is of the form (ite c a b) we can push (ite c a b) on the flattener.
1575
     *
1576
     * Heuristics: don't push the term if x is already internalized or if it's
1577
     * shared.
1578
     */
1579
    if (is_pos_term(x) &&
12,590✔
1580
        is_ite_term(ctx->terms, x) &&
6,295✔
1581
        !intern_tbl_root_is_mapped(&ctx->intern, x) &&
3,934✔
1582
        term_is_not_shared(&ctx->sharing, x)) {
1,256✔
1583
      ite = ite_term_desc(ctx->terms, x);
630✔
1584
      assert(ite->arity == 3);
1585
      c = internalize_to_literal(ctx, ite->arg[0]);
630✔
1586
      ite_flattener_push(flattener, ite, c);
630✔
1587
    } else {
1588
      /*
1589
       * Add the clause [branch conditions => x = u]
1590
       */
1591
      v = internalize_to_arith(ctx, x);
5,665✔
1592

1593
      buffer = &ctx->aux_vector;
5,665✔
1594
      assert(buffer->size == 0);
1595
      ite_flattener_get_clause(flattener, buffer);
5,665✔
1596
      ite_prepare_antecedents(buffer);
5,665✔
1597
      // assert [buffer \/ v = u]
1598
      ctx->arith.assert_clause_vareq_axiom(ctx->arith_solver, buffer->size, buffer->data, v, u);
5,665✔
1599
      ivector_reset(buffer);
5,665✔
1600

1601
      ite_flattener_next_branch(flattener);
5,665✔
1602
    }
1603
  }
1604

1605
  //  delete_ite_flattener(&flattener);
1606
   objstack_pop(&ctx->ostack);
2,521✔
1607

1608
  return u;
2,521✔
1609
}
1610

1611
/*
1612
 * Convert if-then-else to an arithmetic variable
1613
 * - if is_int is true, the if-then-else term is integer
1614
 * - otherwise, it's real
1615
 */
1616
static thvar_t map_ite_to_arith(context_t *ctx, composite_term_t *ite, bool is_int) {
3,023✔
1617
  conditional_t *d;
1618
  literal_t c;
1619
  thvar_t v, x;
1620

1621
  assert(ite->arity == 3);
1622

1623
  d = context_make_conditional(ctx, ite);
3,023✔
1624
  if (d != NULL) {
3,023✔
1625
    v = map_conditional_to_arith(ctx, d, is_int);
239✔
1626
    context_free_conditional(ctx, d);
239✔
1627
    return v;
239✔
1628
  }
1629

1630
  c = internalize_to_literal(ctx, ite->arg[0]); // condition
2,784✔
1631
  if (c == true_literal) {
2,784✔
1632
    return internalize_to_arith(ctx, ite->arg[1]);
77✔
1633
  }
1634
  if (c == false_literal) {
2,707✔
1635
    return internalize_to_arith(ctx, ite->arg[2]);
115✔
1636
  }
1637

1638
  if (context_ite_flattening_enabled(ctx)) {
2,592✔
1639
    return flatten_ite_to_arith(ctx, ite, c, is_int);
2,521✔
1640
  }
1641

1642

1643
  /*
1644
   * no simplification: create a fresh variable v and assert (c ==> v = t1)
1645
   * and (not c ==> v = t2)
1646
   */
1647
  v = ctx->arith.create_var(ctx->arith_solver, is_int);
71✔
1648

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

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

1655
  return v;
71✔
1656
}
1657

1658

1659
/*
1660
 * Assert the bounds on t when t is an arithmetic, special if-then-else
1661
 * - x = arithmetic variable mapped to t in the arithmetic solver
1662
 */
1663
static void assert_ite_bounds(context_t *ctx, term_t t, thvar_t x) {
793✔
1664
  term_table_t *terms;
1665
  polynomial_t *p;
1666
  term_t lb, ub;
1667
  thvar_t map[2];
1668

1669
  terms = ctx->terms;
793✔
1670
  assert(is_arithmetic_term(terms, t));
1671

1672
  // get lower and upper bound on t. Both are rational constants
1673
  term_finite_domain_bounds(terms, t, &lb, &ub);
793✔
1674

1675
#if 0
1676
  printf("assert ite bound:\n  term: ");
1677
  print_term_name(stdout, terms, t);
1678
  printf("\n");
1679
  printf("  lower bound: ");
1680
  print_term_full(stdout, terms, lb);
1681
  printf("\n");
1682
  printf("  upper bound: ");
1683
  print_term_full(stdout, terms, ub);
1684
  printf("\n");
1685
#endif
1686

1687
  /*
1688
   * prepare polynomial p:
1689
   * first monomial is a constant, second monomial is either +t or -t
1690
   * map[0] = null (what's mapped to const_idx)
1691
   * map[1] = x = (what's mapped to t)
1692
   */
1693
  p = context_get_aux_poly(ctx, 3);
793✔
1694
  p->nterms = 2;
793✔
1695
  p->mono[0].var = const_idx;
793✔
1696
  p->mono[1].var = t;
793✔
1697
  p->mono[2].var = max_idx;
793✔
1698
  map[0] = null_thvar;
793✔
1699
  map[1] = x;
793✔
1700

1701

1702
  // first bound: t >= lb
1703
  q_set_neg(&p->mono[0].coeff, rational_term_desc(terms, lb)); // -lb
793✔
1704
  q_set_one(&p->mono[1].coeff); // +t
793✔
1705
  ctx->arith.assert_poly_ge_axiom(ctx->arith_solver, p, map, true); // assert -lb + t >= 0
793✔
1706

1707
  // second bound: t <= ub
1708
  q_set(&p->mono[0].coeff, rational_term_desc(terms, ub));  // +ub
793✔
1709
  q_set_minus_one(&p->mono[1].coeff);  // -t
793✔
1710
  ctx->arith.assert_poly_ge_axiom(ctx->arith_solver, p, map, true); // assert +ub - t >= 0
793✔
1711
}
793✔
1712

1713

1714
/*
1715
 * Convert a power product to an arithmetic variable
1716
 */
1717
static thvar_t map_pprod_to_arith(context_t *ctx, pprod_t *p) {
×
1718
  uint32_t i, n;
1719
  thvar_t *a;
1720
  thvar_t x;
1721

1722
  n = p->len;
×
1723
  a = alloc_istack_array(&ctx->istack, n);
×
1724
  for (i=0; i<n; i++) {
×
1725
    a[i] = internalize_to_arith(ctx, p->prod[i].var);
×
1726
  }
1727

1728
  x = ctx->arith.create_pprod(ctx->arith_solver, p, a);
×
1729
  free_istack_array(&ctx->istack, a);
×
1730

1731
  return x;
×
1732
}
1733

1734

1735
/*
1736
 * Convert polynomial p to an arithmetic variable
1737
 */
1738
static thvar_t map_poly_to_arith(context_t *ctx, polynomial_t *p) {
1,762✔
1739
  uint32_t i, n;
1740
  thvar_t *a;
1741
  thvar_t x;
1742

1743
  n = p->nterms;
1,762✔
1744
  a = alloc_istack_array(&ctx->istack, n);
1,762✔
1745

1746
  // skip the constant if any
1747
  i = 0;
1,762✔
1748
  if (p->mono[0].var == const_idx) {
1,762✔
1749
    a[0] = null_thvar;
467✔
1750
    i ++;
467✔
1751
  }
1752

1753
  // deal with the non-constant monomials
1754
  while (i<n) {
5,035✔
1755
    a[i] = internalize_to_arith(ctx, p->mono[i].var);
3,273✔
1756
    i ++;
3,273✔
1757
  }
1758

1759
  // build the polynomial
1760
  x = ctx->arith.create_poly(ctx->arith_solver, p, a);
1,762✔
1761
  free_istack_array(&ctx->istack, a);
1,762✔
1762

1763
  return x;
1,762✔
1764
}
1765

1766

1767
/*
1768
 * Auxiliary function: return y := (floor x)
1769
 * - check the divmod table first.
1770
 *   If there's a record for (floor x), return the corresponding variable.
1771
 * - Otherwise, create a fresh integer variable y,
1772
 *   assert the axioms for y = (floor x)
1773
 *   add a record to the divmod table and return y.
1774
 */
1775
static thvar_t get_floor(context_t *ctx, thvar_t x) {
×
1776
  thvar_t y;
1777

1778
  y = context_find_var_for_floor(ctx, x);
×
1779
  if (y == null_thvar) {
×
1780
    y = ctx->arith.create_var(ctx->arith_solver, true); // y is an integer variable
×
1781
    assert_floor_axioms(ctx, y, x); // assert y = floor(x)
×
1782
    context_record_floor(ctx, x, y); // save the mapping y --> floor(x)
×
1783
  }
1784

1785
  return y;
×
1786
}
1787

1788

1789
/*
1790
 * Return y := (div x k)
1791
 * - check the divmod table first
1792
 * - if (div x k) has already been processed, return the corresponding variable
1793
 * - otherwise create a new variable y, assert the axioms for y = (div x k)
1794
 *   add a record in the divmod table, and return y.
1795
 */
1796
static thvar_t get_div(context_t *ctx, thvar_t x, const rational_t *k) {
25✔
1797
  thvar_t y;
1798

1799
  y = context_find_var_for_div(ctx, x, k);
25✔
1800
  if (y == null_thvar) {
25✔
1801
    // create y := (div x k)
1802
    y = ctx->arith.create_var(ctx->arith_solver, true); // y is an integer
21✔
1803
    assert_div_axioms(ctx, y, x, k);
21✔
1804
    context_record_div(ctx, x, k, y);
21✔
1805
  }
1806

1807
  return y;
25✔
1808
}
1809

1810

1811

1812
/*
1813
 * Convert (floor t) to an arithmetic variable
1814
 */
1815
static thvar_t map_floor_to_arith(context_t *ctx, term_t t) {
×
1816
  thvar_t x, y;
1817

1818
  x = internalize_to_arith(ctx, t);
×
1819
  if (ctx->arith.arith_var_is_int(ctx->arith_solver, x)) {
×
1820
    // x is integer so (floor x) = x
1821
    y = x;
×
1822
  } else {
1823
    y = get_floor(ctx, x);
×
1824
  }
1825

1826
  return y;
×
1827
}
1828

1829

1830
/*
1831
 * Convert (ceil t) to an arithmetic variable
1832
 */
1833
static thvar_t map_ceil_to_arith(context_t *ctx, term_t t) {
×
1834
  thvar_t x, y;
1835

1836
  x = internalize_to_arith(ctx, t);
×
1837
  if (ctx->arith.arith_var_is_int(ctx->arith_solver, x)) {
×
1838
    // x is integer so (ceil x) = x
1839
    y = x;
×
1840
  } else {
1841
    y = context_find_var_for_ceil(ctx, x);
×
1842
    if (y == null_thvar) {
×
1843
      y = ctx->arith.create_var(ctx->arith_solver, true); // y is an integer variable
×
1844
      assert_ceil_axioms(ctx, y, x); // assert y = ceil(x)
×
1845
      context_record_ceil(ctx, x, y); // save the mapping y --> ceil(x)
×
1846
    }
1847
  }
1848

1849
  return y;
×
1850
}
1851

1852

1853
/*
1854
 * Convert (abs t) to an arithmetic variable
1855
 */
1856
static thvar_t map_abs_to_arith(context_t *ctx, term_t t) {
4✔
1857
  thvar_t x, y;
1858
  bool is_int;
1859

1860
  x = internalize_to_arith(ctx, t);
4✔
1861
  is_int = ctx->arith.arith_var_is_int(ctx->arith_solver, x);
4✔
1862
  y = ctx->arith.create_var(ctx->arith_solver, is_int); // y := abs(x) has the same type as x
4✔
1863
  assert_abs_axioms(ctx, y, x);
4✔
1864

1865
  return y;
4✔
1866
}
1867

1868

1869
/*
1870
 * Auxiliary function: check whether t is a non-zero arithmetic constant
1871
 * - if so, store t's value in *val
1872
 */
1873
static bool is_non_zero_rational(term_table_t *tbl, term_t t, rational_t *val) {
17✔
1874
  assert(is_arithmetic_term(tbl, t));
1875

1876
  if (term_kind(tbl, t) == ARITH_CONSTANT) {
17✔
1877
    q_set(val, rational_term_desc(tbl, t));
17✔
1878
    return q_is_nonzero(val);
17✔
1879
  }
1880
  return false;
×
1881
}
1882

1883

1884
/*
1885
 * Error in division: either the divisor is zero or is non-constant
1886
 */
1887
static void __attribute__((noreturn))  bad_divisor(context_t *ctx, term_t t) {
3✔
1888
  term_table_t *tbl;
1889
  int code;
1890

1891
  tbl = ctx->terms;
3✔
1892
  assert(is_arithmetic_term(tbl, t) && is_pos_term(t));
1893

1894
  code = FORMULA_NOT_LINEAR;
3✔
1895
  if (term_kind(tbl, t) == ARITH_CONSTANT && q_is_zero(rational_term_desc(tbl, t))) {
3✔
1896
    code = DIV_BY_ZERO;
3✔
1897
  }
1898
  longjmp(ctx->env, code);
3✔
1899
}
1900

1901
/*
1902
 * Convert (/ t1 t2) to an arithmetic variable
1903
 * - t2 must be a non-zero arithmetic constant
1904
 */
1905
static thvar_t map_rdiv_to_arith(context_t *ctx, composite_term_t *div) {
3✔
1906
  // Could try to evaluate t2 then check whether that's a constant
1907
  assert(div->arity == 2);
1908
  bad_divisor(ctx, div->arg[1]);
3✔
1909
}
1910

1911

1912
/*
1913
 * Convert (div t1 t2) to an arithmetic variable.
1914
 * - fails if t2 is not an arithmetic constant or if it's zero
1915
 */
1916
static thvar_t map_idiv_to_arith(context_t *ctx, composite_term_t *div) {
6✔
1917
  rational_t k;
1918
  thvar_t x, y;
1919

1920
  assert(div->arity == 2);
1921

1922
  q_init(&k);
6✔
1923
  if (is_non_zero_rational(ctx->terms, div->arg[1], &k)) { // k := value of t2
6✔
1924
    assert(q_is_nonzero(&k));
1925
    x = internalize_to_arith(ctx, div->arg[0]); // t1
6✔
1926
    y = get_div(ctx, x, &k);
6✔
1927

1928
  } else {
1929
    // division by a non-constant or by zero: not supported by default
1930
    // arithmetic solver for now
1931
    q_clear(&k);
×
1932
    bad_divisor(ctx, div->arg[1]);
×
1933
  }
1934
  q_clear(&k);
6✔
1935

1936
  return y;
6✔
1937
}
1938

1939

1940
/*
1941
 * Convert (mod t1 t2) to an arithmetic variable
1942
 * - t2 must be a non-zero constant
1943
 */
1944
static thvar_t map_mod_to_arith(context_t *ctx, composite_term_t *mod) {
11✔
1945
  rational_t k;
1946
  thvar_t x, y, r;
1947
  bool is_int;
1948

1949
  assert(mod->arity == 2);
1950

1951
  q_init(&k);
11✔
1952
  if (is_non_zero_rational(ctx->terms, mod->arg[1], &k)) { // k := divider
11✔
1953
    x = internalize_to_arith(ctx, mod->arg[0]);
11✔
1954

1955
    // get y := (div x k)
1956
    assert(q_is_nonzero(&k));
1957
    y = get_div(ctx, x, &k);
11✔
1958

1959
    /*
1960
     * r := (mod x k) is x - k * y where y is an integer.
1961
     * If both x and k are integer, then r has integer type. Otherwise,
1962
     * r is a real variable.
1963
     */
1964
    is_int = ctx->arith.arith_var_is_int(ctx->arith_solver, x) && q_is_integer(&k);
11✔
1965
    r = ctx->arith.create_var(ctx->arith_solver, is_int);
11✔
1966
    assert_mod_axioms(ctx, r, x, y, &k);
11✔
1967

1968
  } else {
1969
    // Non-constant or zero divider
1970
    q_clear(&k);
×
1971
    bad_divisor(ctx, mod->arg[1]);
×
1972
  }
1973

1974
  q_clear(&k);
11✔
1975

1976
  return r;
11✔
1977
}
1978

1979

1980

1981
/******************************************************
1982
 *  CONVERSION OF COMPOSITES TO BIT-VECTOR VARIABLES  *
1983
 *****************************************************/
1984

1985
/*
1986
 * Convert if-then-else to a bitvector variable
1987
 */
1988
static thvar_t map_ite_to_bv(context_t *ctx, composite_term_t *ite) {
20,779✔
1989
  literal_t c;
1990
  thvar_t x, y;
1991

1992
  assert(ite->arity == 3);
1993

1994
  c = internalize_to_literal(ctx, ite->arg[0]);
20,779✔
1995
  if (c == true_literal) {
20,779✔
1996
    return internalize_to_bv(ctx, ite->arg[1]);
23✔
1997
  }
1998
  if (c == false_literal) {
20,756✔
1999
    return internalize_to_bv(ctx, ite->arg[2]);
2,082✔
2000
  }
2001

2002
  // no simplification
2003
  x = internalize_to_bv(ctx, ite->arg[1]);
18,674✔
2004
  y = internalize_to_bv(ctx, ite->arg[2]);
18,674✔
2005

2006
  return ctx->bv.create_bvite(ctx->bv_solver, c, x, y);
18,674✔
2007
}
2008

2009

2010
/*
2011
 * Array of bits b
2012
 * - hackish: we locally disable flattening here
2013
 */
2014
static thvar_t map_bvarray_to_bv(context_t *ctx, composite_term_t *b) {
16,994✔
2015
  uint32_t i, n;
2016
  uint32_t save_options;
2017
  literal_t *a;
2018
  thvar_t x;
2019

2020
  n = b->arity;
16,994✔
2021
  a = alloc_istack_array(&ctx->istack, n);
16,994✔
2022

2023
  save_options = ctx->options;
16,994✔
2024
  disable_diseq_and_or_flattening(ctx);
16,994✔
2025
  for (i=0; i<n; i++) {
497,925✔
2026
    a[i] = internalize_to_literal(ctx, b->arg[i]);
480,931✔
2027
  }
2028
  ctx->options = save_options;
16,994✔
2029

2030
  x = ctx->bv.create_bvarray(ctx->bv_solver, a, n);
16,994✔
2031

2032
  free_istack_array(&ctx->istack, a);
16,994✔
2033

2034
  return x;
16,994✔
2035
}
2036

2037

2038
/*
2039
 * Unsigned division: quotient (div u v)
2040
 */
2041
static thvar_t map_bvdiv_to_bv(context_t *ctx, composite_term_t *div) {
97✔
2042
  thvar_t x, y;
2043

2044
  assert(div->arity == 2);
2045
  x = internalize_to_bv(ctx, div->arg[0]);
97✔
2046
  y = internalize_to_bv(ctx, div->arg[1]);
97✔
2047

2048
  return ctx->bv.create_bvdiv(ctx->bv_solver, x, y);
97✔
2049
}
2050

2051

2052
/*
2053
 * Unsigned division: remainder (rem u v)
2054
 */
2055
static thvar_t map_bvrem_to_bv(context_t *ctx, composite_term_t *rem) {
148✔
2056
  thvar_t x, y;
2057

2058
  assert(rem->arity == 2);
2059
  x = internalize_to_bv(ctx, rem->arg[0]);
148✔
2060
  y = internalize_to_bv(ctx, rem->arg[1]);
148✔
2061

2062
  return ctx->bv.create_bvrem(ctx->bv_solver, x, y);
148✔
2063
}
2064

2065

2066
/*
2067
 * Signed division/rounding toward 0: quotient (sdiv u v)
2068
 */
2069
static thvar_t map_bvsdiv_to_bv(context_t *ctx, composite_term_t *sdiv) {
87✔
2070
  thvar_t x, y;
2071

2072
  assert(sdiv->arity == 2);
2073
  x = internalize_to_bv(ctx, sdiv->arg[0]);
87✔
2074
  y = internalize_to_bv(ctx, sdiv->arg[1]);
87✔
2075

2076
  return ctx->bv.create_bvsdiv(ctx->bv_solver, x, y);
87✔
2077
}
2078

2079

2080
/*
2081
 * Signed division/rounding toward 0: remainder (srem u v)
2082
 */
2083
static thvar_t map_bvsrem_to_bv(context_t *ctx, composite_term_t *srem) {
78✔
2084
  thvar_t x, y;
2085

2086
  assert(srem->arity == 2);
2087
  x = internalize_to_bv(ctx, srem->arg[0]);
78✔
2088
  y = internalize_to_bv(ctx, srem->arg[1]);
78✔
2089

2090
  return ctx->bv.create_bvsrem(ctx->bv_solver, x, y);
78✔
2091
}
2092

2093

2094
/*
2095
 * Signed division/rounding toward -infinity: remainder (smod u v)
2096
 */
2097
static thvar_t map_bvsmod_to_bv(context_t *ctx, composite_term_t *smod) {
×
2098
  thvar_t x, y;
2099

2100
  assert(smod->arity == 2);
2101
  x = internalize_to_bv(ctx, smod->arg[0]);
×
2102
  y = internalize_to_bv(ctx, smod->arg[1]);
×
2103

2104
  return ctx->bv.create_bvsmod(ctx->bv_solver, x, y);
×
2105
}
2106

2107

2108
/*
2109
 * Left shift: (shl u v)
2110
 */
2111
static thvar_t map_bvshl_to_bv(context_t *ctx, composite_term_t *shl) {
85✔
2112
  thvar_t x, y;
2113

2114
  assert(shl->arity == 2);
2115
  x = internalize_to_bv(ctx, shl->arg[0]);
85✔
2116
  y = internalize_to_bv(ctx, shl->arg[1]);
85✔
2117

2118
  return ctx->bv.create_bvshl(ctx->bv_solver, x, y);
85✔
2119
}
2120

2121

2122
/*
2123
 * Logical shift right: (lshr u v)
2124
 */
2125
static thvar_t map_bvlshr_to_bv(context_t *ctx, composite_term_t *lshr) {
1,428✔
2126
  thvar_t x, y;
2127

2128
  assert(lshr->arity == 2);
2129
  x = internalize_to_bv(ctx, lshr->arg[0]);
1,428✔
2130
  y = internalize_to_bv(ctx, lshr->arg[1]);
1,428✔
2131

2132
  return ctx->bv.create_bvlshr(ctx->bv_solver, x, y);
1,428✔
2133
}
2134

2135

2136
/*
2137
 * Arithmetic shift right: (ashr u v)
2138
 */
2139
static thvar_t map_bvashr_to_bv(context_t *ctx, composite_term_t *ashr) {
296✔
2140
  thvar_t x, y;
2141

2142
  assert(ashr->arity == 2);
2143
  x = internalize_to_bv(ctx, ashr->arg[0]);
296✔
2144
  y = internalize_to_bv(ctx, ashr->arg[1]);
296✔
2145

2146
  return ctx->bv.create_bvashr(ctx->bv_solver, x, y);
296✔
2147
}
2148

2149

2150

2151
/*
2152
 * TODO: check for simplifications in bitvector arithmetic
2153
 * before translation to bitvector variables.
2154
 *
2155
 * This matters for the wienand-cav2008 benchmarks.
2156
 */
2157

2158
/*
2159
 * Power product
2160
 */
2161
static thvar_t map_pprod_to_bv(context_t *ctx, pprod_t *p) {
585✔
2162
  uint32_t i, n;
2163
  thvar_t *a;
2164
  thvar_t x;
2165

2166
  n = p->len;
585✔
2167
  a = alloc_istack_array(&ctx->istack, n);
585✔
2168
  for (i=0; i<n; i++) {
1,730✔
2169
    a[i] = internalize_to_bv(ctx, p->prod[i].var);
1,145✔
2170
  }
2171

2172
  x = ctx->bv.create_pprod(ctx->bv_solver, p, a);
585✔
2173
  free_istack_array(&ctx->istack, a);
585✔
2174

2175
  return x;
585✔
2176
}
2177

2178

2179
/*
2180
 * Bitvector polynomial, 64bit coefficients
2181
 */
2182
static thvar_t map_bvpoly64_to_bv(context_t *ctx, bvpoly64_t *p) {
12,151✔
2183
  uint32_t i, n;
2184
  thvar_t *a;
2185
  thvar_t x;
2186

2187
  assert(p->nterms > 0);
2188

2189
  n = p->nterms;
12,151✔
2190
  a = alloc_istack_array(&ctx->istack, n);
12,151✔
2191

2192
  // skip the constant if any
2193
  i = 0;
12,151✔
2194
  if (p->mono[0].var == const_idx) {
12,151✔
2195
    a[0] = null_thvar;
3,162✔
2196
    i ++;
3,162✔
2197
  }
2198

2199
  // non-constant monomials
2200
  while (i < n) {
28,307✔
2201
    a[i] = internalize_to_bv(ctx, p->mono[i].var);
16,156✔
2202
    i ++;
16,156✔
2203
  }
2204

2205
  x = ctx->bv.create_poly64(ctx->bv_solver, p, a);
12,151✔
2206
  free_istack_array(&ctx->istack, a);
12,151✔
2207

2208
  return x;
12,151✔
2209
}
2210

2211

2212
/*
2213
 * Bitvector polynomial, coefficients have more than 64bits
2214
 */
2215
static thvar_t map_bvpoly_to_bv(context_t *ctx, bvpoly_t *p) {
607✔
2216
  uint32_t i, n;
2217
  thvar_t *a;
2218
  thvar_t x;
2219

2220
  assert(p->nterms > 0);
2221

2222
  n = p->nterms;
607✔
2223
  a = alloc_istack_array(&ctx->istack, n);
607✔
2224

2225
  // skip the constant if any
2226
  i = 0;
607✔
2227
  if (p->mono[0].var == const_idx) {
607✔
2228
    a[0] = null_thvar;
539✔
2229
    i ++;
539✔
2230
  }
2231

2232
  // non-constant monomials
2233
  while (i < n) {
1,268✔
2234
    a[i] = internalize_to_bv(ctx, p->mono[i].var);
661✔
2235
    i ++;
661✔
2236
  }
2237

2238
  x = ctx->bv.create_poly(ctx->bv_solver, p, a);
607✔
2239
  free_istack_array(&ctx->istack, a);
607✔
2240

2241
  return x;
607✔
2242
}
2243

2244

2245
#if 0
2246
/*
2247
 * Bvpoly buffer: b must be normalized.
2248
 * - not optimal but this shouldn't be called often.
2249
 */
2250
static thvar_t map_bvpoly_buffer_to_bv(context_t *ctx, bvpoly_buffer_t *b) {
2251
  bvpoly64_t *p;
2252
  bvpoly_t *q;
2253
  uint32_t n;
2254
  thvar_t x;
2255

2256
  n = bvpoly_buffer_bitsize(b);
2257

2258
  if (bvpoly_buffer_is_zero(b)) {
2259
    x = ctx->bv.create_zero(ctx->bv_solver, n);
2260
  } else if (n <= 64) {
2261
    p = bvpoly_buffer_getpoly64(b);
2262
    x = map_bvpoly64_to_bv(ctx, p);
2263
    free_bvpoly64(p);
2264
  } else {
2265
    q = bvpoly_buffer_getpoly(b);
2266
    x = map_bvpoly_to_bv(ctx, q);
2267
    free_bvpoly(q);
2268
  }
2269

2270
  if (ctx->mcsat_supplement_active) {
2271
    mcsat_satellite_t *sat = context_mcsat_satellite(ctx);
2272
    if (sat != NULL) {
2273
      mcsat_satellite_register_arith_term(sat, x, r);
2274
    }
2275
  }
2276

2277
  return x;
2278
}
2279

2280
#endif
2281

2282
/****************************
2283
 *  CONVERSION TO LITERALS  *
2284
 ***************************/
2285

2286
/*
2287
 * Boolean if-then-else
2288
 */
2289
static literal_t map_ite_to_literal(context_t *ctx, composite_term_t *ite) {
1,863✔
2290
  literal_t l1, l2, l3;
2291

2292
  assert(ite->arity == 3);
2293
  l1 = internalize_to_literal(ctx, ite->arg[0]); // condition
1,863✔
2294
  if (l1 == true_literal) {
1,863✔
2295
    return internalize_to_literal(ctx, ite->arg[1]);
32✔
2296
  }
2297
  if (l1 == false_literal) {
1,831✔
2298
    return internalize_to_literal(ctx, ite->arg[2]);
71✔
2299
  }
2300

2301
  l2 = internalize_to_literal(ctx, ite->arg[1]);
1,760✔
2302
  l3 = internalize_to_literal(ctx, ite->arg[2]);
1,760✔
2303

2304
  return mk_ite_gate(&ctx->gate_manager, l1, l2, l3);
1,760✔
2305
}
2306

2307

2308
/*
2309
 * Generic equality: (eq t1 t2)
2310
 * - t1 and t2 are not arithmetic or bitvector terms
2311
 */
2312
static literal_t map_eq_to_literal(context_t *ctx, composite_term_t *eq) {
14,518✔
2313
  occ_t u, v;
2314
  literal_t l1, l2, l;
2315

2316
  assert(eq->arity == 2);
2317

2318
  if (is_boolean_term(ctx->terms, eq->arg[0])) {
14,518✔
2319
    assert(is_boolean_term(ctx->terms, eq->arg[1]));
2320

2321
    l1 = internalize_to_literal(ctx, eq->arg[0]);
9,058✔
2322
    l2 = internalize_to_literal(ctx, eq->arg[1]);
9,058✔
2323
    l = mk_iff_gate(&ctx->gate_manager, l1, l2);
9,058✔
2324
  } else {
2325
    // filter out high-order terms. It's enough to check eq->arg[0]
2326
    check_high_order_support(ctx, eq->arg, 1);
5,460✔
2327

2328
    u = internalize_to_eterm(ctx, eq->arg[0]);
5,460✔
2329
    v = internalize_to_eterm(ctx, eq->arg[1]);
5,460✔
2330
    l = egraph_make_eq(ctx->egraph, u, v);
5,460✔
2331
  }
2332

2333
  return l;
14,518✔
2334
}
2335

2336

2337
/*
2338
 * (or t1 ... t_n)
2339
 */
2340
static literal_t map_or_to_literal(context_t *ctx, composite_term_t *or) {
81,300✔
2341
  int32_t *a;
2342
  ivector_t *v;
2343
  literal_t l;
2344
  uint32_t i, n;
2345

2346
  if (context_flatten_or_enabled(ctx)) {
81,300✔
2347
    // flatten (or ...): store result in v
2348
    v = &ctx->aux_vector;
13,220✔
2349
    assert(v->size == 0);
2350
    flatten_or_term(ctx, v, or);
13,220✔
2351

2352
    // try easy simplification
2353
    n = v->size;
13,220✔
2354
    if (disjunct_is_true(ctx, v->data, n)) {
13,220✔
2355
      ivector_reset(v);
335✔
2356
      return true_literal;
335✔
2357
    }
2358

2359
    // make a copy of v
2360
    a = alloc_istack_array(&ctx->istack, n);
12,885✔
2361
    for (i=0; i<n; i++) {
70,797✔
2362
      a[i] = v->data[i];
57,912✔
2363
    }
2364
    ivector_reset(v);
12,885✔
2365

2366
    // internalize a[0 ... n-1]
2367
    for (i=0; i<n; i++) {
69,824✔
2368
      l = internalize_to_literal(ctx, a[i]);
57,221✔
2369
      if (l == true_literal) goto done;
57,221✔
2370
      a[i] = l;
56,939✔
2371
    }
2372

2373
  } else {
2374
    // no flattening
2375
    n = or->arity;
68,080✔
2376
    if (disjunct_is_true(ctx, or->arg, n)) {
68,080✔
2377
      return true_literal;
204✔
2378
    }
2379

2380
    a = alloc_istack_array(&ctx->istack, n);
67,876✔
2381
    for (i=0; i<n; i++) {
254,012✔
2382
      l = internalize_to_literal(ctx, or->arg[i]);
186,452✔
2383
      if (l == true_literal) goto done;
186,452✔
2384
      a[i] = l;
186,136✔
2385
    }
2386
  }
2387

2388
  l = mk_or_gate(&ctx->gate_manager, n, a);
80,163✔
2389

2390
 done:
80,761✔
2391
  free_istack_array(&ctx->istack, a);
80,761✔
2392

2393
  return l;
80,761✔
2394
}
2395

2396

2397
/*
2398
 * (xor t1 ... t_n)
2399
 */
2400
static literal_t map_xor_to_literal(context_t *ctx, composite_term_t *xor) {
1✔
2401
  int32_t *a;
2402
  literal_t l;
2403
  uint32_t i, n;
2404

2405
  n = xor->arity;
1✔
2406
  a = alloc_istack_array(&ctx->istack, n);
1✔
2407
  for (i=0; i<n; i++) {
4✔
2408
    a[i] = internalize_to_literal(ctx, xor->arg[i]);
3✔
2409
  }
2410

2411
  l = mk_xor_gate(&ctx->gate_manager, n, a);
1✔
2412
  free_istack_array(&ctx->istack, a);
1✔
2413

2414
  return l;
1✔
2415
}
2416

2417

2418
/*
2419
 * Convert (p t_1 .. t_n) to a literal
2420
 * - create an egraph atom
2421
 */
2422
static literal_t map_apply_to_literal(context_t *ctx, composite_term_t *app) {
4,360✔
2423
  occ_t *a;
2424
  uint32_t i, n;
2425
  literal_t l;
2426

2427
  assert(app->arity > 0);
2428
  n = app->arity;
4,360✔
2429
  a = alloc_istack_array(&ctx->istack, n);
4,360✔
2430
  for (i=0; i<n; i++) {
16,301✔
2431
    a[i] = internalize_to_eterm(ctx, app->arg[i]);
11,941✔
2432
  }
2433

2434
  // a[0] = predicate
2435
  // a[1 ...n-1] = arguments
2436
  l = egraph_make_pred(ctx->egraph, a[0], n-1, a + 1);
4,360✔
2437
  free_istack_array(&ctx->istack, a);
4,360✔
2438

2439
  return l;
4,360✔
2440
}
2441

2442

2443

2444
/*
2445
 * Auxiliary function: translate (distinct a[0 ... n-1]) to a literal,
2446
 * when a[0] ... a[n-1] are arithmetic variables.
2447
 *
2448
 * We expand this into a quadratic number of disequalities.
2449
 */
2450
static literal_t make_arith_distinct(context_t *ctx, uint32_t n, thvar_t *a) {
158✔
2451
  uint32_t i, j;
2452
  ivector_t *v;
2453
  literal_t l;
2454

2455
  assert(n >= 2);
2456

2457
  v = &ctx->aux_vector;
158✔
2458
  assert(v->size == 0);
2459
  for (i=0; i<n-1; i++) {
3,776✔
2460
    for (j=i+1; j<n; j++) {
48,648✔
2461
      l = ctx->arith.create_vareq_atom(ctx->arith_solver, a[i], a[j]);
45,030✔
2462
      ivector_push(v, l);
45,030✔
2463
    }
2464
  }
2465
  l = mk_or_gate(&ctx->gate_manager, v->size, v->data);
158✔
2466
  ivector_reset(v);
158✔
2467

2468
  return not(l);
158✔
2469
}
2470

2471

2472
/*
2473
 * Auxiliary function: translate (distinct a[0 ... n-1]) to a literal,
2474
 * when a[0] ... a[n-1] are bitvector variables.
2475
 *
2476
 * We expand this into a quadratic number of disequalities.
2477
 */
2478
static literal_t make_bv_distinct(context_t *ctx, uint32_t n, thvar_t *a) {
2✔
2479
  uint32_t i, j;
2480
  ivector_t *v;
2481
  literal_t l;
2482

2483
  assert(n >= 2);
2484

2485
  v = &ctx->aux_vector;
2✔
2486
  assert(v->size == 0);
2487
  for (i=0; i<n-1; i++) {
11✔
2488
    for (j=i+1; j<n; j++) {
40✔
2489
      l = ctx->bv.create_eq_atom(ctx->bv_solver, a[i], a[j]);
31✔
2490
      ivector_push(v, l);
31✔
2491
    }
2492
  }
2493
  l = mk_or_gate(&ctx->gate_manager, v->size, v->data);
2✔
2494
  ivector_reset(v);
2✔
2495

2496
  return not(l);
2✔
2497
}
2498

2499

2500
/*
2501
 * Convert (distinct t_1 ... t_n) to a literal
2502
 */
2503
static literal_t map_distinct_to_literal(context_t *ctx, composite_term_t *distinct) {
9✔
2504
  int32_t *a;
2505
  literal_t l;
2506
  uint32_t i, n;
2507

2508
  n = distinct->arity;
9✔
2509
  a = alloc_istack_array(&ctx->istack, n);
9✔
2510
  if (context_has_egraph(ctx)) {
9✔
2511
    // fail if arguments are functions and we don't support high-order terms
2512
    // checking the first argument is enough since they all have the same type
2513
    check_high_order_support(ctx, distinct->arg, 1);
6✔
2514

2515
    // default: translate to the egraph
2516
    for (i=0; i<n; i++) {
25✔
2517
      a[i] = internalize_to_eterm(ctx, distinct->arg[i]);
19✔
2518
    }
2519
    l = egraph_make_distinct(ctx->egraph, n, a);
6✔
2520

2521
  } else if (is_arithmetic_term(ctx->terms, distinct->arg[0])) {
3✔
2522
    // translate to arithmetic variables
2523
    for (i=0; i<n; i++) {
12✔
2524
      a[i] = internalize_to_arith(ctx, distinct->arg[i]);
9✔
2525
    }
2526
    l = make_arith_distinct(ctx, n, a);
3✔
2527

2528
  } else if (is_bitvector_term(ctx->terms, distinct->arg[0])) {
×
2529
    // translate to bitvector variables
2530
    for (i=0; i<n; i++) {
×
2531
      a[i] = internalize_to_bv(ctx, distinct->arg[i]);
×
2532
    }
2533
    l = make_bv_distinct(ctx, n, a);
×
2534

2535
  } else {
2536
    longjmp(ctx->env, uf_error_code(ctx, distinct->arg[0]));
×
2537
  }
2538

2539
  free_istack_array(&ctx->istack, a);
9✔
2540

2541
  return l;
9✔
2542
}
2543

2544

2545

2546
/*
2547
 * Arithmetic atom: p == 0
2548
 */
2549
static literal_t map_poly_eq_to_literal(context_t *ctx, polynomial_t *p) {
1,192✔
2550
  uint32_t i, n;
2551
  thvar_t *a;
2552
  literal_t l;
2553

2554
  n = p->nterms;
1,192✔
2555
  a = alloc_istack_array(&ctx->istack, n);
1,192✔
2556

2557
  // skip the constant if any
2558
  i = 0;
1,192✔
2559
  if (p->mono[0].var == const_idx) {
1,192✔
2560
    a[0] = null_thvar;
570✔
2561
    i ++;
570✔
2562
  }
2563

2564
  // deal with the non-constant monomials
2565
  while (i<n) {
4,166✔
2566
    a[i] = internalize_to_arith(ctx, p->mono[i].var);
2,974✔
2567
    i ++;
2,974✔
2568
  }
2569

2570
  // build the atom
2571
  l = ctx->arith.create_poly_eq_atom(ctx->arith_solver, p, a);
1,192✔
2572
  free_istack_array(&ctx->istack, a);
1,192✔
2573

2574
  return l;
1,192✔
2575
}
2576

2577

2578
/*
2579
 * Arithmetic atom: (p >= 0)
2580
 */
2581
static literal_t map_poly_ge_to_literal(context_t *ctx, polynomial_t *p) {
33,493✔
2582
  uint32_t i, n;
2583
  thvar_t *a;
2584
  literal_t l;
2585

2586
  n = p->nterms;
33,493✔
2587
  a = alloc_istack_array(&ctx->istack, n);
33,493✔
2588

2589
  // skip the constant if any
2590
  i = 0;
33,493✔
2591
  if (p->mono[0].var == const_idx) {
33,493✔
2592
    a[0] = null_thvar;
26,318✔
2593
    i ++;
26,318✔
2594
  }
2595

2596
  // deal with the non-constant monomials
2597
  while (i<n) {
84,648✔
2598
    a[i] = internalize_to_arith(ctx, p->mono[i].var);
51,155✔
2599
    i ++;
51,155✔
2600
  }
2601

2602
  // build the atom
2603
  l = ctx->arith.create_poly_ge_atom(ctx->arith_solver, p, a);
33,493✔
2604
  free_istack_array(&ctx->istack, a);
33,493✔
2605

2606
  return l;
33,493✔
2607
}
2608

2609

2610
/*
2611
 * Arithmetic atom: (t >= 0)
2612
 */
2613
static literal_t map_arith_geq_to_literal(context_t *ctx, term_t t) {
33,841✔
2614
  term_table_t *terms;
2615
  thvar_t x;
2616
  literal_t l;
2617

2618
  terms = ctx->terms;
33,841✔
2619
  if (term_kind(terms, t) == ARITH_POLY) {
33,841✔
2620
    l = map_poly_ge_to_literal(ctx, poly_term_desc(terms, t));
33,493✔
2621
  } else {
2622
    x = internalize_to_arith(ctx, t);
348✔
2623
    l = ctx->arith.create_ge_atom(ctx->arith_solver, x);
348✔
2624
  }
2625

2626
  return l;
33,841✔
2627
}
2628

2629

2630

2631
/*
2632
 * Arithmetic equalities (eq t1 t2)
2633
 * 1) try to flatten the if-then-elses
2634
 * 2) also apply cheap lift-if rule: (eq (ite c t1 t2) u1) --> (ite c (eq t1 u1) (eq t2 u1))
2635
 */
2636
static literal_t map_arith_bineq(context_t *ctx, term_t t1, term_t u1);
2637

2638
/*
2639
 * Lift equality: (eq (ite c u1 u2) t) --> (ite c (eq u1 t) (eq u2 t))
2640
 */
2641
static literal_t map_ite_arith_bineq(context_t *ctx, composite_term_t *ite, term_t t) {
42,609✔
2642
  literal_t l1, l2, l3;
2643

2644
  assert(ite->arity == 3);
2645
  l1 = internalize_to_literal(ctx, ite->arg[0]);
42,609✔
2646
  if (l1 == true_literal) {
42,609✔
2647
    // (eq (ite true u1 u2) t) --> (eq u1 t)
2648
    return map_arith_bineq(ctx, ite->arg[1], t);
7✔
2649
  }
2650
  if (l1 == false_literal) {
42,602✔
2651
    // (eq (ite true u1 u2) t) --> (eq u2 t)
2652
    return map_arith_bineq(ctx, ite->arg[2], t);
109✔
2653
  }
2654

2655
  // apply lift-if here
2656
  l2 = map_arith_bineq(ctx, ite->arg[1], t);
42,493✔
2657
  l3 = map_arith_bineq(ctx, ite->arg[2], t);
42,493✔
2658

2659
  return mk_ite_gate(&ctx->gate_manager, l1, l2, l3);
42,493✔
2660
}
2661

2662
static literal_t map_arith_bineq_aux(context_t *ctx, term_t t1, term_t t2) {
53,484✔
2663
  term_table_t *terms;
2664
  thvar_t x, y;
2665
  occ_t u, v;
2666
  literal_t l;
2667

2668
  /*
2669
   * Try to apply lift-if rule: (eq (ite c u1 u2) t2) --> (ite c (eq u1 t2) (eq u2 t2))
2670
   * do this only if t2 is not an if-then-else term.
2671
   *
2672
   * Otherwise add the atom (eq t1 t2) to the egraph if possible
2673
   * or create (eq t1 t2) in the arithmetic solver.
2674
   */
2675
  terms = ctx->terms;
53,484✔
2676
  if (is_ite_term(terms, t1) && ! is_ite_term(terms, t2)) {
53,484✔
2677
    l = map_ite_arith_bineq(ctx, ite_term_desc(terms, t1), t2);
38,573✔
2678
  } else if (is_ite_term(terms, t2) && !is_ite_term(terms, t1)) {
14,911✔
2679
    l = map_ite_arith_bineq(ctx, ite_term_desc(terms, t2), t1);
4,036✔
2680
  } else if (context_has_egraph(ctx)) {
10,875✔
2681
    u = internalize_to_eterm(ctx, t1);
3,754✔
2682
    v = internalize_to_eterm(ctx, t2);
3,754✔
2683
    l = egraph_make_eq(ctx->egraph, u, v);
3,754✔
2684
  } else {
2685
    x = internalize_to_arith(ctx, t1);
7,121✔
2686
    y = internalize_to_arith(ctx, t2);
7,121✔
2687
    l = ctx->arith.create_vareq_atom(ctx->arith_solver, x, y);
7,121✔
2688
  }
2689

2690
  return l;
53,484✔
2691
}
2692

2693
static literal_t map_arith_bineq(context_t *ctx, term_t t1, term_t u1) {
95,341✔
2694
  ivector_t *v;
2695
  int32_t *a;
2696
  uint32_t i, n;
2697
  term_t t2, u2;
2698
  literal_t l;
2699

2700
  t1 = intern_tbl_get_root(&ctx->intern, t1);
95,341✔
2701
  u1 = intern_tbl_get_root(&ctx->intern, u1);
95,341✔
2702

2703
  if (t1 == u1) {
95,341✔
2704
    return true_literal;
3,222✔
2705
  }
2706

2707
  /*
2708
   * Check the cache
2709
   */
2710
  l = find_in_eq_cache(ctx, t1, u1);
92,119✔
2711
  if (l == null_literal) {
92,119✔
2712
    /*
2713
     * The pair (t1, u1) is not mapped already.
2714
     * Try to flatten the if-then-else equalities
2715
     */
2716
    v = &ctx->aux_vector;
53,411✔
2717
    assert(v->size == 0);
2718
    t2 = flatten_ite_equality(ctx, v, t1, u1);
53,411✔
2719
    u2 = flatten_ite_equality(ctx, v, u1, t2);
53,411✔
2720

2721
    /*
2722
     * (t1 == u1) is equivalent to (and (t2 == u2) v[0] ... v[n-1])
2723
     * where v[i] = element i of v
2724
     */
2725
    n = v->size;
53,411✔
2726
    if (n == 0) {
53,411✔
2727
      // empty v: return (t2 == u2)
2728
      assert(t1 == t2 && u1 == u2);
2729
      l = map_arith_bineq_aux(ctx, t2, u2);
10,660✔
2730

2731
    } else {
2732
      // build (and (t2 == u2) v[0] ... v[n-1])
2733
      // first make a copy of v into a[0 .. n-1]
2734
      a = alloc_istack_array(&ctx->istack, n+1);
42,751✔
2735
      for (i=0; i<n; i++) {
766,594✔
2736
        a[i] = v->data[i];
723,843✔
2737
      }
2738
      ivector_reset(v);
42,751✔
2739

2740
      // build the internalization of a[0 .. n-1]
2741
      for (i=0; i<n; i++) {
766,594✔
2742
        a[i] = internalize_to_literal(ctx, a[i]);
723,843✔
2743
      }
2744
      a[n] = map_arith_bineq_aux(ctx, t2, u2);
42,751✔
2745

2746
      // build (and a[0] ... a[n])
2747
      l = mk_and_gate(&ctx->gate_manager, n+1, a);
42,751✔
2748
      free_istack_array(&ctx->istack, a);
42,751✔
2749
    }
2750

2751
    /*
2752
     * Store the mapping (t1, u1) --> l in the cache
2753
     */
2754
    add_to_eq_cache(ctx, t1, u1, l);
53,411✔
2755
  }
2756

2757
  return l;
92,119✔
2758
}
2759

2760

2761
static inline literal_t map_arith_bineq_to_literal(context_t *ctx, composite_term_t *eq) {
8,947✔
2762
  assert(eq->arity == 2);
2763
  return map_arith_bineq(ctx, eq->arg[0], eq->arg[1]);
8,947✔
2764
}
2765

2766

2767

2768
/*
2769
 * Arithmetic atom: (t == 0)
2770
 */
2771
static literal_t map_arith_eq_to_literal(context_t *ctx, term_t t) {
2,229✔
2772
  term_table_t *terms;
2773
  thvar_t x;
2774
  literal_t l;
2775

2776
  terms = ctx->terms;
2,229✔
2777
  if (term_kind(terms, t) == ARITH_POLY) {
2,229✔
2778
    l = map_poly_eq_to_literal(ctx, poly_term_desc(terms, t));
1,192✔
2779
  } else if (is_ite_term(terms, t)) {
1,037✔
2780
    l = map_arith_bineq(ctx, t, zero_term);
426✔
2781
  } else {
2782
    x = internalize_to_arith(ctx, t);
611✔
2783
    l = ctx->arith.create_eq_atom(ctx->arith_solver, x);
611✔
2784
  }
2785
  return l;
2,229✔
2786
}
2787

2788

2789
/*
2790
 * DIVIDES AND IS_INT ATOMS
2791
 */
2792

2793
/*
2794
 * We use the rules
2795
 * - (is_int x)    <=> (x <= floor(x))
2796
 * - (divides k x) <=> (x <= k * div(x, k))
2797
 */
2798

2799
// atom (is_int t)
2800
static literal_t map_arith_is_int_to_literal(context_t *ctx, term_t t) {
×
2801
  polynomial_t *p;
2802
  thvar_t map[2];
2803
  thvar_t x, y;
2804
  literal_t l;
2805

2806
  x = internalize_to_arith(ctx, t);
×
2807
  if (ctx->arith.arith_var_is_int(ctx->arith_solver, x)) {
×
2808
    l = true_literal;
×
2809
  } else {
2810
    // we convert (is_int x) to (x <= floor(x))
2811
    y = get_floor(ctx, x); // y is floor x
×
2812
    p = context_get_aux_poly(ctx, 3);
×
2813
    context_store_diff_poly(p, map, y, x); // (p, map) := (y - x)
×
2814
    // atom (x <= y) is the same as (p >= 0)
2815
    l = ctx->arith.create_poly_ge_atom(ctx->arith_solver, p, map);
×
2816
  }
2817

2818
  return l;
×
2819
}
2820

2821
// atom (divides k t)  we assume k != 0
2822
static literal_t map_arith_divides_to_literal(context_t *ctx, composite_term_t *divides) {
8✔
2823
  rational_t k;
2824
  polynomial_t *p;
2825
  thvar_t map[2];
2826
  thvar_t x, y;
2827
  term_t d;
2828
  literal_t l;
2829

2830
  assert(divides->arity == 2);
2831

2832
  d = divides->arg[0];
8✔
2833
  if (term_kind(ctx->terms, d) == ARITH_CONSTANT) {
8✔
2834
    // make a copy of divides->arg[0] in k
2835
    q_init(&k);
8✔
2836
    q_set(&k, rational_term_desc(ctx->terms, d));
8✔
2837
    assert(q_is_nonzero(&k));
2838

2839
    x = internalize_to_arith(ctx, divides->arg[1]); // this is t
8✔
2840
    y = get_div(ctx, x, &k);  // y := (div x k)
8✔
2841
    p = context_get_aux_poly(ctx, 3);
8✔
2842
    context_store_divides_constraint(p, map, x, y, &k); // p is (- x + k * y)
8✔
2843
    // atom (x <= k * y) is (p >= 0)
2844
    l = ctx->arith.create_poly_ge_atom(ctx->arith_solver, p, map);
8✔
2845

2846
    q_clear(&k);
8✔
2847

2848
    return l;
8✔
2849

2850
  } else {
2851
    // k is not a constant: not supported
2852
    longjmp(ctx->env, FORMULA_NOT_LINEAR);
×
2853
  }
2854
}
2855

2856

2857
/*
2858
 * BITVECTOR ATOMS
2859
 */
2860

2861
/*
2862
 * Auxiliary function: atom for (t == 0)
2863
 */
2864
static literal_t map_bveq0_to_literal(context_t *ctx, term_t t) {
10✔
2865
  uint32_t n;
2866
  thvar_t x, y;
2867

2868
  t = intern_tbl_get_root(&ctx->intern, t);
10✔
2869
  n = term_bitsize(ctx->terms, t);
10✔
2870
  x = internalize_to_bv(ctx, t);
10✔
2871
  y = ctx->bv.create_zero(ctx->bv_solver, n);
10✔
2872

2873
  return ctx->bv.create_eq_atom(ctx->bv_solver, x, y);
10✔
2874
}
2875

2876
static literal_t map_bveq_to_literal(context_t *ctx, composite_term_t *eq) {
4,862✔
2877
  bveq_simp_t simp;
2878
  term_t t, t1, t2;
2879
  thvar_t x, y;
2880

2881
  assert(eq->arity == 2);
2882

2883
  /*
2884
   * Apply substitution then check for simplifications
2885
   */
2886
  t1 = intern_tbl_get_root(&ctx->intern, eq->arg[0]);
4,862✔
2887
  t2 = intern_tbl_get_root(&ctx->intern, eq->arg[1]);
4,862✔
2888
  t = simplify_bitvector_eq(ctx, t1, t2);
4,862✔
2889
  if (t != NULL_TERM) {
4,862✔
2890
    // (bveq t1 t2) is equivalent to t
2891
    return internalize_to_literal(ctx, t);
74✔
2892
  }
2893

2894
  /*
2895
   * More simplifications
2896
   */
2897
  try_arithmetic_bveq_simplification(ctx, &simp, t1, t2);
4,788✔
2898
  switch (simp.code) {
4,788✔
2899
  case BVEQ_CODE_TRUE:
×
2900
    return true_literal;
×
2901

2902
  case BVEQ_CODE_FALSE:
×
2903
    return false_literal;
×
2904

2905
  case BVEQ_CODE_REDUCED:
11✔
2906
    t1 = intern_tbl_get_root(&ctx->intern, simp.left);
11✔
2907
    t2 = intern_tbl_get_root(&ctx->intern, simp.right);
11✔
2908
    break;
11✔
2909

2910
  case BVEQ_CODE_REDUCED0:
10✔
2911
    // (t1 == t2) is reduced to (simp.left == 0)
2912
    // we create the atom directly here:
2913
    return map_bveq0_to_literal(ctx, simp.left);
10✔
2914

2915
  default:
4,767✔
2916
    break;
4,767✔
2917
  }
2918

2919
  if (equal_bitvector_factors(ctx, t1, t2)) {
4,778✔
2920
    return true_literal;
×
2921
  }
2922

2923
  /*
2924
   * NOTE: creating (eq t1 t2) in the egraph instead makes things worse
2925
   */
2926
  x = internalize_to_bv(ctx, t1);
4,778✔
2927
  y = internalize_to_bv(ctx, t2);
4,778✔
2928
  return ctx->bv.create_eq_atom(ctx->bv_solver, x, y);
4,778✔
2929
}
2930

2931
static literal_t map_bvge_to_literal(context_t *ctx, composite_term_t *ge) {
2,333✔
2932
  thvar_t x, y;
2933

2934
  assert(ge->arity == 2);
2935
  x = internalize_to_bv(ctx, ge->arg[0]);
2,333✔
2936
  y = internalize_to_bv(ctx, ge->arg[1]);
2,333✔
2937

2938
  return ctx->bv.create_ge_atom(ctx->bv_solver, x, y);
2,333✔
2939
}
2940

2941
static literal_t map_bvsge_to_literal(context_t *ctx, composite_term_t *sge) {
2,907✔
2942
  thvar_t x, y;
2943

2944
  assert(sge->arity == 2);
2945
  x = internalize_to_bv(ctx, sge->arg[0]);
2,907✔
2946
  y = internalize_to_bv(ctx, sge->arg[1]);
2,907✔
2947

2948
  return ctx->bv.create_sge_atom(ctx->bv_solver, x, y);
2,907✔
2949
}
2950

2951

2952
// Select bit
2953
static literal_t map_bit_select_to_literal(context_t *ctx, select_term_t *select) {
267,936✔
2954
  term_t t, s;
2955
  thvar_t x;
2956

2957
  /*
2958
   * Apply substitution then try to simplify
2959
   */
2960
  t = intern_tbl_get_root(&ctx->intern, select->arg);
267,936✔
2961
  s = extract_bit(ctx->terms, t, select->idx);
267,936✔
2962
  if (s != NULL_TERM) {
267,936✔
2963
    // (select t i) is s
2964
    return internalize_to_literal(ctx, s);
27,229✔
2965
  } else {
2966
    // no simplification
2967
    x = internalize_to_bv(ctx, t);
240,707✔
2968
    return ctx->bv.select_bit(ctx->bv_solver, x, select->idx);
240,707✔
2969
  }
2970
}
2971

2972

2973
/****************************************
2974
 *  INTERNALIZATION TO ETERM: TOPLEVEL  *
2975
 ***************************************/
2976

2977
static occ_t internalize_to_eterm(context_t *ctx, term_t t) {
103,206✔
2978
  term_table_t *terms;
2979
  term_t r;
2980
  uint32_t polarity;
2981
  int32_t code;
2982
  int32_t exception;
2983
  type_t tau;
2984
  occ_t u;
2985
  literal_t l;
2986
  thvar_t x;
2987

2988
  if (! context_has_egraph(ctx)) {
103,206✔
2989
    exception = uf_error_code(ctx, t);
×
2990
    goto abort;
×
2991
  }
2992

2993
  r = intern_tbl_get_root(&ctx->intern, t);
103,206✔
2994
  polarity = polarity_of(r);
103,206✔
2995
  r  = unsigned_term(r);
103,206✔
2996

2997
  /*
2998
   * r is a positive root in the internalization table
2999
   * polarity is 0 or 1
3000
   * if polarity is 0, then t is equal to r by substitution
3001
   * if polarity is 1, then t is equal to (not r)
3002
   */
3003
  if (intern_tbl_root_is_mapped(&ctx->intern, r)) {
103,206✔
3004
    /*
3005
     * r already internalized
3006
     */
3007
    code = intern_tbl_map_of_root(&ctx->intern, r);
64,857✔
3008
    u = translate_code_to_eterm(ctx, r, code);
64,857✔
3009
  } else {
3010
    /*
3011
     * Compute r's internalization:
3012
     * - if it's a boolean term, convert r to a literal l then
3013
     *   remap l to an egraph term
3014
     * - otherwise, recursively construct an egraph term and map it to r
3015
     */
3016
    terms = ctx->terms;
38,349✔
3017
    tau = type_of_root(ctx, r);
38,349✔
3018
    if (is_boolean_type(tau)) {
38,349✔
3019
      l = internalize_to_literal(ctx, r);
38✔
3020
      u = egraph_literal2occ(ctx->egraph, l);
38✔
3021
      intern_tbl_remap_root(&ctx->intern, r, occ2code(u));
38✔
3022
    } else {
3023
      /*
3024
       * r is not a boolean term
3025
       */
3026
      assert(polarity == 0);
3027

3028
      switch (term_kind(terms, r)) {
38,311✔
3029
      case CONSTANT_TERM:
1,164✔
3030
        u = pos_occ(make_egraph_constant(ctx, tau, constant_term_index(terms, r)));
1,164✔
3031
        break;
1,164✔
3032

3033
      case ARITH_CONSTANT:
337✔
3034
        u = map_arith_constant_to_eterm(ctx, rational_term_desc(terms, r));
337✔
3035
        break;
337✔
3036

3037
      case BV64_CONSTANT:
808✔
3038
        u = map_bvconst64_to_eterm(ctx, bvconst64_term_desc(terms, r));
808✔
3039
        break;
808✔
3040

3041
      case BV_CONSTANT:
18✔
3042
        u = map_bvconst_to_eterm(ctx, bvconst_term_desc(terms, r));
18✔
3043
        break;
18✔
3044

3045
      case VARIABLE:
×
3046
        exception = FREE_VARIABLE_IN_FORMULA;
×
3047
        goto abort;
×
3048

3049
      case UNINTERPRETED_TERM:
12,029✔
3050
        u = pos_occ(make_egraph_variable(ctx, tau));
12,029✔
3051
        //        add_type_constraints(ctx, u, tau);
3052
        break;
12,029✔
3053

3054
      case ARITH_FLOOR:
×
3055
        assert(is_integer_type(tau));
3056
        x = map_floor_to_arith(ctx, arith_floor_arg(terms, r));
×
3057
        u = translate_arithvar_to_eterm(ctx, x);
×
3058
        break;
×
3059

3060
      case ARITH_CEIL:
×
3061
        assert(is_integer_type(tau));
3062
        x = map_ceil_to_arith(ctx, arith_ceil_arg(terms, r));
×
3063
        u = translate_arithvar_to_eterm(ctx, x);
×
3064
        break;
×
3065

3066
      case ARITH_ABS:
×
3067
        x = map_abs_to_arith(ctx, arith_abs_arg(terms, r));
×
3068
        u = translate_arithvar_to_eterm(ctx, x);
×
3069
        break;
×
3070

3071
      case ITE_TERM:
2,168✔
3072
      case ITE_SPECIAL:
3073
        u = map_ite_to_eterm(ctx, ite_term_desc(terms, r), tau);
2,168✔
3074
        break;
2,168✔
3075

3076
      case APP_TERM:
6,531✔
3077
        u = map_apply_to_eterm(ctx, app_term_desc(terms, r), tau);
6,531✔
3078
        break;
6,531✔
3079

3080
      case ARITH_RDIV:
×
3081
        assert(is_arithmetic_type(tau));
3082
        x = map_rdiv_to_arith(ctx, arith_rdiv_term_desc(terms, r));
×
3083
        u = translate_arithvar_to_eterm(ctx, x);
×
3084
        break;
×
3085

3086
      case ARITH_IDIV:
×
3087
        assert(is_integer_type(tau));
3088
        x = map_idiv_to_arith(ctx, arith_idiv_term_desc(terms, r));
×
3089
        u = translate_arithvar_to_eterm(ctx, x); // (div t u) has type int
×
3090
        break;
×
3091

3092
      case ARITH_MOD:
2✔
3093
        x = map_mod_to_arith(ctx, arith_mod_term_desc(terms, r));
2✔
3094
        u = translate_arithvar_to_eterm(ctx, x);
2✔
3095
        break;
2✔
3096

3097
      case TUPLE_TERM:
190✔
3098
        u = map_tuple_to_eterm(ctx, tuple_term_desc(terms, r), tau);
190✔
3099
        break;
190✔
3100

3101
      case SELECT_TERM:
245✔
3102
        u = map_select_to_eterm(ctx, select_term_desc(terms, r), tau);
245✔
3103
        break;
245✔
3104

3105
      case UPDATE_TERM:
11,470✔
3106
        u = map_update_to_eterm(ctx, update_term_desc(terms, r), tau);
11,470✔
3107
        break;
11,470✔
3108

3109
      case LAMBDA_TERM:
1✔
3110
        // not ready for lambda terms yet:
3111
        exception = LAMBDAS_NOT_SUPPORTED;
1✔
3112
        goto abort;
1✔
3113

3114
      case BV_ARRAY:
1,711✔
3115
        x = map_bvarray_to_bv(ctx, bvarray_term_desc(terms, r));
1,711✔
3116
        u = translate_thvar_to_eterm(ctx, x, tau);
1,711✔
3117
        break;
1,711✔
3118

3119
      case BV_DIV:
1✔
3120
        x = map_bvdiv_to_bv(ctx, bvdiv_term_desc(terms, r));
1✔
3121
        u = translate_thvar_to_eterm(ctx, x, tau);
1✔
3122
        break;
1✔
3123

3124
      case BV_REM:
2✔
3125
        x = map_bvrem_to_bv(ctx, bvrem_term_desc(terms, r));
2✔
3126
        u = translate_thvar_to_eterm(ctx, x, tau);
2✔
3127
        break;
2✔
3128

3129
      case BV_SDIV:
1✔
3130
        x = map_bvsdiv_to_bv(ctx, bvsdiv_term_desc(terms, r));
1✔
3131
        u = translate_thvar_to_eterm(ctx, x, tau);
1✔
3132
        break;
1✔
3133

3134
      case BV_SREM:
1✔
3135
        x = map_bvsrem_to_bv(ctx, bvsrem_term_desc(terms, r));
1✔
3136
        u = translate_thvar_to_eterm(ctx, x, tau);
1✔
3137
        break;
1✔
3138

3139
      case BV_SMOD:
×
3140
        x = map_bvsmod_to_bv(ctx, bvsmod_term_desc(terms, r));
×
3141
        u = translate_thvar_to_eterm(ctx, x, tau);
×
3142
        break;
×
3143

3144
      case BV_SHL:
×
3145
        x = map_bvshl_to_bv(ctx, bvshl_term_desc(terms, r));
×
3146
        u = translate_thvar_to_eterm(ctx, x, tau);
×
3147
        break;
×
3148

3149
      case BV_LSHR:
1✔
3150
        x = map_bvlshr_to_bv(ctx, bvlshr_term_desc(terms, r));
1✔
3151
        u = translate_thvar_to_eterm(ctx, x, tau);
1✔
3152
        break;
1✔
3153

3154
      case BV_ASHR:
1✔
3155
        x = map_bvashr_to_bv(ctx, bvashr_term_desc(terms, r));
1✔
3156
        u = translate_thvar_to_eterm(ctx, x, tau);
1✔
3157
        break;
1✔
3158

3159
      case POWER_PRODUCT:
1✔
3160
        if (is_arithmetic_type(tau)) {
1✔
3161
          x = map_pprod_to_arith(ctx, pprod_term_desc(terms, r));
×
3162
        } else {
3163
          assert(is_bv_type(ctx->types, tau));
3164
          x = map_pprod_to_bv(ctx, pprod_term_desc(terms, r));
1✔
3165
        }
3166
        u = translate_thvar_to_eterm(ctx, x, tau);
1✔
3167
        break;
1✔
3168

3169
      case ARITH_POLY:
619✔
3170
        x = map_poly_to_arith(ctx, poly_term_desc(terms, r));
619✔
3171
        u = translate_thvar_to_eterm(ctx, x, tau);
619✔
3172
        break;
619✔
3173

3174
      case BV64_POLY:
1,008✔
3175
        x = map_bvpoly64_to_bv(ctx, bvpoly64_term_desc(terms, r));
1,008✔
3176
        u = translate_thvar_to_eterm(ctx, x, tau);
1,008✔
3177
        break;
1,008✔
3178

3179
      case BV_POLY:
2✔
3180
        x = map_bvpoly_to_bv(ctx, bvpoly_term_desc(terms, r));
2✔
3181
        u = translate_thvar_to_eterm(ctx, x, tau);
2✔
3182
        break;
2✔
3183

3184
      default:
×
3185
        exception = INTERNAL_ERROR;
×
3186
        goto abort;
×
3187
      }
3188

3189
      // store the mapping r --> u
3190
      intern_tbl_map_root(&ctx->intern, r, occ2code(u));
38,310✔
3191
    }
3192
  }
3193

3194
  // fix the polarity
3195
  return u ^ polarity;
103,205✔
3196

3197
 abort:
1✔
3198
  longjmp(ctx->env, exception);
1✔
3199
}
3200

3201

3202

3203

3204
/****************************************
3205
 *  CONVERSION TO ARITHMETIC VARIABLES  *
3206
 ***************************************/
3207

3208
/*
3209
 * Translate internalization code x to an arithmetic variable
3210
 * - if the code is for an egraph term u, then we return the
3211
 *   theory variable attached to u in the egraph.
3212
 * - otherwise, x must be the code of an arithmetic variable v,
3213
 *   we return v.
3214
 */
3215
static thvar_t translate_code_to_arith(context_t *ctx, int32_t x) {
99,879✔
3216
  eterm_t u;
3217
  thvar_t v;
3218

3219
  assert(code_is_valid(x));
3220

3221
  if (code_is_eterm(x)) {
99,879✔
3222
    u = code2eterm(x);
6,521✔
3223
    assert(ctx->egraph != NULL && egraph_term_is_arith(ctx->egraph, u));
3224
    v = egraph_term_base_thvar(ctx->egraph, u);
6,521✔
3225
  } else {
3226
    v = code2thvar(x);
93,358✔
3227
  }
3228

3229
  assert(v != null_thvar);
3230
  return v;
99,879✔
3231
}
3232

3233

3234
static thvar_t internalize_to_arith(context_t *ctx, term_t t) {
120,766✔
3235
  term_table_t *terms;
3236
  int32_t exception;
3237
  int32_t code;
3238
  term_t r;
3239
  occ_t u;
3240
  thvar_t x;
3241

3242
  assert(is_arithmetic_term(ctx->terms, t));
3243

3244
  if (! context_has_arith_solver(ctx)) {
120,766✔
3245
    exception = ARITH_NOT_SUPPORTED;
×
3246
    goto abort;
×
3247
  }
3248

3249
  /*
3250
   * Apply term substitution: t --> r
3251
   */
3252
  r = intern_tbl_get_root(&ctx->intern, t);
120,766✔
3253
  if (intern_tbl_root_is_mapped(&ctx->intern, r)) {
120,766✔
3254
    /*
3255
     * r already internalized
3256
     */
3257
    code = intern_tbl_map_of_root(&ctx->intern, r);
99,720✔
3258
    x = translate_code_to_arith(ctx, code);
99,720✔
3259

3260
  } else {
3261
    /*
3262
     * Compute the internalization
3263
     */
3264
    terms = ctx->terms;
21,046✔
3265

3266
    switch (term_kind(terms, r)) {
21,046✔
3267
    case ARITH_CONSTANT:
1,270✔
3268
      x = ctx->arith.create_const(ctx->arith_solver, rational_term_desc(terms, r));
1,270✔
3269
      intern_tbl_map_root(&ctx->intern, r, thvar2code(x));
1,270✔
3270
      break;
1,270✔
3271

3272
    case UNINTERPRETED_TERM:
15,371✔
3273
      x = ctx->arith.create_var(ctx->arith_solver, is_integer_root(ctx, r));
15,371✔
3274
      intern_tbl_map_root(&ctx->intern, r, thvar2code(x));
15,371✔
3275
      //      printf("mapping: %s --> i!%d\n", term_name(ctx->terms, r), (int) x);
3276
      //      fflush(stdout);
3277
      break;
15,371✔
3278

3279
    case ARITH_FLOOR:
×
3280
      x = map_floor_to_arith(ctx, arith_floor_arg(terms, r));
×
3281
      intern_tbl_map_root(&ctx->intern, r, thvar2code(x));
×
3282
      break;
×
3283

3284
    case ARITH_CEIL:
×
3285
      x = map_ceil_to_arith(ctx, arith_ceil_arg(terms, r));
×
3286
      intern_tbl_map_root(&ctx->intern, r, thvar2code(x));
×
3287
      break;
×
3288

3289
    case ARITH_ABS:
4✔
3290
      x = map_abs_to_arith(ctx, arith_abs_arg(terms, r));
4✔
3291
      intern_tbl_map_root(&ctx->intern, r, thvar2code(x));
4✔
3292
      break;
4✔
3293

3294
    case ITE_TERM:
2,230✔
3295
      x = map_ite_to_arith(ctx, ite_term_desc(terms, r), is_integer_root(ctx, r));
2,230✔
3296
      intern_tbl_map_root(&ctx->intern, r, thvar2code(x));
2,230✔
3297
      break;
2,230✔
3298

3299
    case ITE_SPECIAL:
793✔
3300
      x = map_ite_to_arith(ctx, ite_term_desc(terms, r), is_integer_root(ctx, r));
793✔
3301
      intern_tbl_map_root(&ctx->intern, r, thvar2code(x));
793✔
3302
      if (context_ite_bounds_enabled(ctx)) {
793✔
3303
        assert_ite_bounds(ctx, r, x);
793✔
3304
      }
3305
      break;
793✔
3306

3307
    case APP_TERM:
682✔
3308
      u = map_apply_to_eterm(ctx, app_term_desc(terms, r), type_of_root(ctx, r));
682✔
3309
      assert(egraph_term_is_arith(ctx->egraph, term_of_occ(u)));
3310
      intern_tbl_map_root(&ctx->intern, r, occ2code(u));
682✔
3311
      x = egraph_term_base_thvar(ctx->egraph, term_of_occ(u));
682✔
3312
      assert(x != null_thvar);
3313
      break;
682✔
3314

3315
    case ARITH_RDIV:
3✔
3316
      x = map_rdiv_to_arith(ctx, arith_rdiv_term_desc(terms, r));
3✔
3317
      intern_tbl_map_root(&ctx->intern, r, thvar2code(x));
×
3318
      break;
×
3319

3320
    case ARITH_IDIV:
6✔
3321
      x = map_idiv_to_arith(ctx, arith_idiv_term_desc(terms, r));
6✔
3322
      intern_tbl_map_root(&ctx->intern, r, thvar2code(x));
6✔
3323
      break;
6✔
3324

3325
    case ARITH_MOD:
9✔
3326
      x = map_mod_to_arith(ctx, arith_mod_term_desc(terms, r));
9✔
3327
      intern_tbl_map_root(&ctx->intern, r, thvar2code(x));
9✔
3328
      break;
9✔
3329

3330
    case SELECT_TERM:
×
3331
      u = map_select_to_eterm(ctx, select_term_desc(terms, r), type_of_root(ctx, r));
×
3332
      assert(egraph_term_is_arith(ctx->egraph, term_of_occ(u)));
3333
      intern_tbl_map_root(&ctx->intern, r, occ2code(u));
×
3334
      x = egraph_term_base_thvar(ctx->egraph, term_of_occ(u));
×
3335
      assert(x != null_thvar);
3336
      break;
×
3337

3338
    case POWER_PRODUCT:
×
3339
      x = map_pprod_to_arith(ctx, pprod_term_desc(terms, r));
×
3340
      intern_tbl_map_root(&ctx->intern, r, thvar2code(x));
×
3341
      break;
×
3342

3343
    case ARITH_POLY:
678✔
3344
      x = map_poly_to_arith(ctx, poly_term_desc(terms, r));
678✔
3345
      intern_tbl_map_root(&ctx->intern, r, thvar2code(x));
678✔
3346
      break;
678✔
3347

3348
    case VARIABLE:
×
3349
      exception = FREE_VARIABLE_IN_FORMULA;
×
3350
      goto abort;
×
3351

3352
    default:
×
3353
      exception = INTERNAL_ERROR;
×
3354
      goto abort;
×
3355
    }
3356

3357
  }
3358

3359
  if (ctx->mcsat_supplement_active) {
120,763✔
3360
    mcsat_satellite_t *sat;
NEW
3361
    sat = context_mcsat_satellite(ctx);
×
NEW
3362
    if (sat != NULL) {
×
NEW
3363
      mcsat_satellite_register_arith_term(sat, x, unsigned_term(r));
×
3364
    }
3365
  }
3366

3367
  return x;
120,763✔
3368

3369
 abort:
×
3370
  longjmp(ctx->env, exception);
×
3371
}
3372

3373

3374

3375
/***************************************
3376
 *  CONVERSION TO BITVECTOR VARIABLES  *
3377
 **************************************/
3378

3379
/*
3380
 * Translate internalization code x to a bitvector variable
3381
 * - if x is for an egraph term u, then we return the theory variable
3382
 *   attached to u in the egraph.
3383
 * - otherwise, x must be the code of a bitvector variable v, so we return v.
3384
 */
3385
static thvar_t translate_code_to_bv(context_t *ctx, int32_t x) {
300,543✔
3386
  eterm_t u;
3387
  thvar_t v;
3388

3389
  assert(code_is_valid(x));
3390

3391
  if (code_is_eterm(x)) {
300,543✔
3392
    u = code2eterm(x);
29,164✔
3393
    assert(ctx->egraph != NULL && egraph_term_is_bv(ctx->egraph, u));
3394
    v = egraph_term_base_thvar(ctx->egraph, u);
29,164✔
3395
  } else {
3396
    v = code2thvar(x);
271,379✔
3397
  }
3398

3399
  assert(v != null_thvar);
3400

3401
  return v;
300,543✔
3402
}
3403

3404
/*
3405
 * Place holders for now
3406
 */
3407
static thvar_t internalize_to_bv(context_t *ctx, term_t t) {
377,371✔
3408
  term_table_t *terms;
3409
  int32_t exception;
3410
  int32_t code;
3411
  term_t r;
3412
  occ_t u;
3413
  thvar_t x;
3414

3415
  assert(is_bitvector_term(ctx->terms, t));
3416

3417
  if (! context_has_bv_solver(ctx)) {
377,371✔
3418
    exception = BV_NOT_SUPPORTED;
×
3419
    goto abort;
×
3420
  }
3421

3422
  /*
3423
   * Apply the term substitution: t --> r
3424
   */
3425
  r = intern_tbl_get_root(&ctx->intern, t);
377,371✔
3426
  if (intern_tbl_root_is_mapped(&ctx->intern, r)) {
377,371✔
3427
    // r is already internalized
3428
    code = intern_tbl_map_of_root(&ctx->intern, r);
300,543✔
3429
    x = translate_code_to_bv(ctx, code);
300,543✔
3430
  } else {
3431
    // compute r's internalization
3432
    terms = ctx->terms;
76,828✔
3433

3434
    switch (term_kind(terms, r)) {
76,828✔
3435
    case BV64_CONSTANT:
7,009✔
3436
      x = ctx->bv.create_const64(ctx->bv_solver, bvconst64_term_desc(terms, r));
7,009✔
3437
      intern_tbl_map_root(&ctx->intern, r, thvar2code(x));
7,009✔
3438
      break;
7,009✔
3439

3440
    case BV_CONSTANT:
440✔
3441
      x = ctx->bv.create_const(ctx->bv_solver, bvconst_term_desc(terms, r));
440✔
3442
      intern_tbl_map_root(&ctx->intern, r, thvar2code(x));
440✔
3443
      break;
440✔
3444

3445
    case UNINTERPRETED_TERM:
16,321✔
3446
      x = ctx->bv.create_var(ctx->bv_solver, term_bitsize(terms, r));
16,321✔
3447
      intern_tbl_map_root(&ctx->intern, r, thvar2code(x));
16,321✔
3448
      break;
16,321✔
3449

3450
    case ITE_TERM:
20,779✔
3451
    case ITE_SPECIAL:
3452
      x = map_ite_to_bv(ctx, ite_term_desc(terms, r));
20,779✔
3453
      intern_tbl_map_root(&ctx->intern, r, thvar2code(x));
20,779✔
3454
      break;
20,779✔
3455

3456
    case APP_TERM:
2,452✔
3457
      u = map_apply_to_eterm(ctx, app_term_desc(terms, r), type_of_root(ctx, r));
2,452✔
3458
      assert(egraph_term_is_bv(ctx->egraph, term_of_occ(u)));
3459
      intern_tbl_map_root(&ctx->intern, r, occ2code(u));
2,452✔
3460
      x = egraph_term_base_thvar(ctx->egraph, term_of_occ(u));
2,452✔
3461
      assert(x != null_thvar);
3462
      break;
2,452✔
3463

3464
    case BV_ARRAY:
15,283✔
3465
      x = map_bvarray_to_bv(ctx, bvarray_term_desc(terms, r));
15,283✔
3466
      intern_tbl_map_root(&ctx->intern, r, thvar2code(x));
15,283✔
3467
      break;
15,283✔
3468

3469
    case BV_DIV:
96✔
3470
      x = map_bvdiv_to_bv(ctx, bvdiv_term_desc(terms, r));
96✔
3471
      intern_tbl_map_root(&ctx->intern, r, thvar2code(x));
96✔
3472
      break;
96✔
3473

3474
    case BV_REM:
146✔
3475
      x = map_bvrem_to_bv(ctx, bvrem_term_desc(terms, r));
146✔
3476
      intern_tbl_map_root(&ctx->intern, r, thvar2code(x));
146✔
3477
      break;
146✔
3478

3479
    case BV_SDIV:
86✔
3480
      x = map_bvsdiv_to_bv(ctx, bvsdiv_term_desc(terms, r));
86✔
3481
      intern_tbl_map_root(&ctx->intern, r, thvar2code(x));
86✔
3482
      break;
86✔
3483

3484
    case BV_SREM:
77✔
3485
      x = map_bvsrem_to_bv(ctx, bvsrem_term_desc(terms, r));
77✔
3486
      intern_tbl_map_root(&ctx->intern, r, thvar2code(x));
77✔
3487
      break;
77✔
3488

3489
    case BV_SMOD:
×
3490
      x = map_bvsmod_to_bv(ctx, bvsmod_term_desc(terms, r));
×
3491
      intern_tbl_map_root(&ctx->intern, r, thvar2code(x));
×
3492
      break;
×
3493

3494
    case BV_SHL:
85✔
3495
      x = map_bvshl_to_bv(ctx, bvshl_term_desc(terms, r));
85✔
3496
      intern_tbl_map_root(&ctx->intern, r, thvar2code(x));
85✔
3497
      break;
85✔
3498

3499
    case BV_LSHR:
1,427✔
3500
      x = map_bvlshr_to_bv(ctx, bvlshr_term_desc(terms, r));
1,427✔
3501
      intern_tbl_map_root(&ctx->intern, r, thvar2code(x));
1,427✔
3502
      break;
1,427✔
3503

3504
    case BV_ASHR:
295✔
3505
      x = map_bvashr_to_bv(ctx, bvashr_term_desc(terms, r));
295✔
3506
      intern_tbl_map_root(&ctx->intern, r, thvar2code(x));
295✔
3507
      break;
295✔
3508

3509
    case SELECT_TERM:
×
3510
      u = map_select_to_eterm(ctx, select_term_desc(terms, r), type_of_root(ctx, r));
×
3511
      assert(egraph_term_is_bv(ctx->egraph, term_of_occ(u)));
3512
      intern_tbl_map_root(&ctx->intern, r, occ2code(u));
×
3513
      x = egraph_term_base_thvar(ctx->egraph, term_of_occ(u));
×
3514
      assert(x != null_thvar);
3515
      break;
×
3516

3517
    case POWER_PRODUCT:
584✔
3518
      x = map_pprod_to_bv(ctx, pprod_term_desc(terms, r));
584✔
3519
      intern_tbl_map_root(&ctx->intern, r, thvar2code(x));
584✔
3520
      break;
584✔
3521

3522
    case BV64_POLY:
11,143✔
3523
      x = map_bvpoly64_to_bv(ctx, bvpoly64_term_desc(terms, r));
11,143✔
3524
      intern_tbl_map_root(&ctx->intern, r, thvar2code(x));
11,143✔
3525
      break;
11,143✔
3526

3527
    case BV_POLY:
605✔
3528
      x = map_bvpoly_to_bv(ctx, bvpoly_term_desc(terms, r));
605✔
3529
      intern_tbl_map_root(&ctx->intern, r, thvar2code(x));
605✔
3530
      break;
605✔
3531

3532
    case VARIABLE:
×
3533
      exception = FREE_VARIABLE_IN_FORMULA;
×
3534
      goto abort;
×
3535

3536
    default:
×
3537
      exception = INTERNAL_ERROR;
×
3538
      goto abort;
×
3539
    }
3540
  }
3541

3542
  return x;
377,371✔
3543

3544
 abort:
×
3545
  longjmp(ctx->env, exception);
×
3546
}
3547

3548

3549

3550

3551

3552

3553
/****************************
3554
 *  CONVERSION TO LITERALS  *
3555
 ***************************/
3556

3557
/*
3558
 * Translate an internalization code x to a literal
3559
 * - if x is the code of an egraph occurrence u, we return the
3560
 *   theory variable for u in the egraph
3561
 * - otherwise, x should be the code of a literal l in the core
3562
 */
3563
static literal_t translate_code_to_literal(context_t *ctx, int32_t x) {
1,398,885✔
3564
  occ_t u;
3565
  literal_t l;
3566

3567
  assert(code_is_valid(x));
3568
  if (code_is_eterm(x)) {
1,398,885✔
3569
    u = code2occ(x);
129,807✔
3570
    if (term_of_occ(u) == true_eterm) {
129,807✔
3571
      l = mk_lit(const_bvar, polarity_of(u));
129,807✔
3572

3573
      assert((u == true_occ && l == true_literal) ||
3574
             (u == false_occ && l == false_literal));
3575
    } else {
3576
      assert(ctx->egraph != NULL);
3577
      l = egraph_occ2literal(ctx->egraph, u);
×
3578
    }
3579
  } else {
3580
    l = code2literal(x);
1,269,078✔
3581
  }
3582

3583
  return l;
1,398,885✔
3584
}
3585

3586
static literal_t internalize_to_literal(context_t *ctx, term_t t) {
1,830,400✔
3587
  term_table_t *terms;
3588
  int32_t code;
3589
  uint32_t polarity;
3590
  term_t r;
3591
  literal_t l;
3592
  occ_t u;
3593

3594
  assert(is_boolean_term(ctx->terms, t));
3595

3596
  r = intern_tbl_get_root(&ctx->intern, t);
1,830,400✔
3597
  polarity = polarity_of(r);
1,830,400✔
3598
  r = unsigned_term(r);
1,830,400✔
3599

3600
  /*
3601
   * At this point:
3602
   * 1) r is a positive root in the internalization table
3603
   * 2) polarity is 1 or 0
3604
   * 3) if polarity is 0, then t is equal to r by substitution
3605
   *    if polarity is 1, then t is equal to (not r)
3606
   *
3607
   * We get l := internalization of r
3608
   * then return l or (not l) depending on polarity.
3609
   */
3610

3611
  if (intern_tbl_root_is_mapped(&ctx->intern, r)) {
1,830,400✔
3612
    /*
3613
     * r already internalized
3614
     */
3615
    code = intern_tbl_map_of_root(&ctx->intern, r);
1,398,885✔
3616
    l = translate_code_to_literal(ctx, code);
1,398,885✔
3617

3618
  } else {
3619
    /*
3620
     * Recursively compute r's internalization
3621
     */
3622
    if (context_atom_requires_mcsat(ctx, r)) {
431,515✔
3623
      l = map_mcsat_atom_to_literal(ctx, r);
3✔
3624
      intern_tbl_map_root(&ctx->intern, r, literal2code(l));
3✔
3625
      goto done;
3✔
3626
    }
3627

3628
    terms = ctx->terms;
431,512✔
3629
    switch (term_kind(terms, r)) {
431,512✔
3630
    case CONSTANT_TERM:
×
3631
      assert(r == true_term);
3632
      l = true_literal;
×
3633
      break;
×
3634

3635
    case VARIABLE:
×
3636
      longjmp(ctx->env, FREE_VARIABLE_IN_FORMULA);
×
3637
      break;
3638

3639
    case UNINTERPRETED_TERM:
6,398✔
3640
      l = pos_lit(create_boolean_variable(ctx->core));
6,398✔
3641
      break;
6,398✔
3642

3643
    case ITE_TERM:
1,863✔
3644
    case ITE_SPECIAL:
3645
      l = map_ite_to_literal(ctx, ite_term_desc(terms, r));
1,863✔
3646
      break;
1,863✔
3647

3648
    case EQ_TERM:
14,518✔
3649
      l = map_eq_to_literal(ctx, eq_term_desc(terms, r));
14,518✔
3650
      break;
14,518✔
3651

3652
    case OR_TERM:
81,300✔
3653
      l = map_or_to_literal(ctx, or_term_desc(terms, r));
81,300✔
3654
      break;
81,300✔
3655

3656
    case XOR_TERM:
1✔
3657
      l = map_xor_to_literal(ctx, xor_term_desc(terms, r));
1✔
3658
      break;
1✔
3659

3660
    case ARITH_IS_INT_ATOM:
×
3661
      l = map_arith_is_int_to_literal(ctx, arith_is_int_arg(terms, r));
×
3662
      break;
×
3663

3664
    case ARITH_EQ_ATOM:
2,229✔
3665
      l = map_arith_eq_to_literal(ctx, arith_eq_arg(terms, r));
2,229✔
3666
      break;
2,229✔
3667

3668
    case ARITH_GE_ATOM:
33,841✔
3669
      l = map_arith_geq_to_literal(ctx, arith_ge_arg(terms, r));
33,841✔
3670
      break;
33,841✔
3671

3672
    case ARITH_BINEQ_ATOM:
8,947✔
3673
      l = map_arith_bineq_to_literal(ctx, arith_bineq_atom_desc(terms, r));
8,947✔
3674
      break;
8,947✔
3675

3676
    case ARITH_DIVIDES_ATOM:
8✔
3677
      l = map_arith_divides_to_literal(ctx, arith_divides_atom_desc(terms, r));
8✔
3678
      break;
8✔
3679

NEW
3680
    case ARITH_ROOT_ATOM:
×
NEW
3681
      longjmp(ctx->env, FORMULA_NOT_LINEAR);
×
3682
      break;
3683

NEW
3684
    case ARITH_FF_EQ_ATOM:
×
3685
    case ARITH_FF_BINEQ_ATOM:
NEW
3686
      longjmp(ctx->env, CONTEXT_UNSUPPORTED_THEORY);
×
3687
      break;
3688

3689
    case APP_TERM:
4,360✔
3690
      l = map_apply_to_literal(ctx, app_term_desc(terms, r));
4,360✔
3691
      break;
4,360✔
3692

3693
    case SELECT_TERM:
×
3694
      u = map_select_to_eterm(ctx, select_term_desc(terms, r), bool_type(ctx->types));
×
3695
      assert(egraph_term_is_bool(ctx->egraph, term_of_occ(u)));
3696
      intern_tbl_map_root(&ctx->intern, r, occ2code(u));
×
3697
      l = egraph_occ2literal(ctx->egraph, u);
×
3698
      // we don't want to map r to l here
3699
      goto done;
×
3700

3701
    case DISTINCT_TERM:
9✔
3702
      l = map_distinct_to_literal(ctx, distinct_term_desc(terms, r));
9✔
3703
      break;
9✔
3704

3705
    case FORALL_TERM:
×
3706
      if (context_in_strict_mode(ctx)) {
×
3707
        longjmp(ctx->env, QUANTIFIERS_NOT_SUPPORTED);
×
3708
      }
3709
      // lax mode: turn forall into a proposition
3710
      l = pos_lit(create_boolean_variable(ctx->core));
×
3711
      break;
×
3712

3713
    case BIT_TERM:
267,936✔
3714
      l = map_bit_select_to_literal(ctx, bit_term_desc(terms, r));
267,936✔
3715
      break;
267,936✔
3716

3717
    case BV_EQ_ATOM:
4,862✔
3718
      l = map_bveq_to_literal(ctx, bveq_atom_desc(terms, r));
4,862✔
3719
      break;
4,862✔
3720

3721
    case BV_GE_ATOM:
2,333✔
3722
      l = map_bvge_to_literal(ctx, bvge_atom_desc(terms, r));
2,333✔
3723
      break;
2,333✔
3724

3725
    case BV_SGE_ATOM:
2,907✔
3726
      l = map_bvsge_to_literal(ctx, bvsge_atom_desc(terms, r));
2,907✔
3727
      break;
2,907✔
3728

3729
    default:
×
3730
      longjmp(ctx->env, INTERNAL_ERROR);
×
3731
      break;
3732
    }
3733

3734
    // map r to l in the internalization table
3735
    intern_tbl_map_root(&ctx->intern, r, literal2code(l));
431,512✔
3736
  }
3737

3738
 done:
1,830,400✔
3739
  return l ^ polarity;
1,830,400✔
3740
}
3741

3742

3743

3744
/******************************************************
3745
 *  TOP-LEVEL ASSERTIONS: TERMS ALREADY INTERNALIZED  *
3746
 *****************************************************/
3747

3748
/*
3749
 * Assert (x == tt) for an internalization code x
3750
 */
3751
static void assert_internalization_code(context_t *ctx, int32_t x, bool tt) {
924✔
3752
  occ_t g;
3753
  literal_t l;
3754

3755
  assert(code_is_valid(x));
3756

3757
  if (code_is_eterm(x)) {
924✔
3758
    // normalize to assertion (g == true)
3759
    g = code2occ(x);
10✔
3760
    if (! tt) g = opposite_occ(g);
10✔
3761

3762
    // We must deal with 'true_occ/false_occ' separately
3763
    // since they may be used even if there's no actual egraph.
3764
    if (g == false_occ) {
10✔
3765
      longjmp(ctx->env, TRIVIALLY_UNSAT);
4✔
3766
    } else if (g != true_occ) {
6✔
3767
      assert(ctx->egraph != NULL);
3768
      if (!context_quant_enabled(ctx) || egraph_is_at_base_level(ctx->egraph)) {
×
3769
        egraph_assert_axiom(ctx->egraph, g);
×
3770
      } else {
3771
        l = egraph_make_eq(ctx->egraph, g, true_occ);
×
3772
        add_unit_clause(ctx->core, l);
×
3773
      }
3774
    }
3775
  } else {
3776
    l = code2literal(x);
914✔
3777
    if (! tt) l = not(l);
914✔
3778
    add_unit_clause(ctx->core, l);
914✔
3779
  }
3780
}
920✔
3781

3782
/*
3783
 * Assert t == true where t is a term that's already mapped
3784
 * either to a literal or to an egraph occurrence.
3785
 * - t must be a root in the internalization table
3786
 */
3787
static void assert_toplevel_intern(context_t *ctx, term_t t) {
740✔
3788
  int32_t code;
3789
  bool tt;
3790

3791
  assert(is_boolean_term(ctx->terms, t) &&
3792
         intern_tbl_is_root(&ctx->intern, t) &&
3793
         intern_tbl_root_is_mapped(&ctx->intern, t));
3794

3795
  tt = is_pos_term(t);
740✔
3796
  t = unsigned_term(t);
740✔
3797
  code = intern_tbl_map_of_root(&ctx->intern, t);
740✔
3798

3799
  assert_internalization_code(ctx, code, tt);
740✔
3800
}
740✔
3801

3802

3803

3804

3805

3806

3807

3808
/********************************
3809
 *   ARITHMETIC SUBSTITUTIONS   *
3810
 *******************************/
3811

3812
/*
3813
 * TODO: improve this in the integer case:
3814
 * - all_int is based on p's type in the term table and does
3815
 *   not take the context's substitutions into account.
3816
 * - integral_poly_after_div requires all coefficients
3817
 *   to be integer. This could be generalized to polynomials
3818
 *   with integer variables and rational coefficients.
3819
 */
3820

3821
/*
3822
 * Check whether term t can be eliminated by an arithmetic substitution
3823
 * - t's root must be uninterpreted and not internalized yet
3824
 */
3825
static bool is_elimination_candidate(context_t *ctx, term_t t) {
2,742✔
3826
  term_t r;
3827

3828
  r = intern_tbl_get_root(&ctx->intern, t);
2,742✔
3829
  return intern_tbl_root_is_free(&ctx->intern, r);
2,742✔
3830
}
3831

3832

3833
/*
3834
 * Replace every variable of t by the root of t in the internalization table
3835
 * - the result is stored in buffer
3836
 */
3837
static void apply_renaming_to_poly(context_t *ctx, polynomial_t *p,  poly_buffer_t *buffer) {
544✔
3838
  uint32_t i, n;
3839
  term_t t;
3840

3841
  reset_poly_buffer(buffer);
544✔
3842

3843
  assert(poly_buffer_is_zero(buffer));
3844

3845
  n = p->nterms;
544✔
3846
  for (i=0; i<n; i++) {
2,599✔
3847
    t = p->mono[i].var;
2,055✔
3848
    if (t == const_idx) {
2,055✔
3849
      poly_buffer_add_const(buffer, &p->mono[i].coeff);
180✔
3850
    } else {
3851
      // replace t by its root
3852
      t = intern_tbl_get_root(&ctx->intern, t);
1,875✔
3853
      poly_buffer_addmul_term(ctx->terms, buffer, t, &p->mono[i].coeff);
1,875✔
3854
    }
3855
  }
3856

3857
  normalize_poly_buffer(buffer);
544✔
3858
}
544✔
3859

3860

3861
/*
3862
 * Auxiliary function: check whether p/a is an integral polynomial
3863
 * assuming all variables and coefficients of p are integer.
3864
 * - check whether all coefficients are multiple of a
3865
 * - a must be non-zero
3866
 */
3867
static bool integralpoly_after_div(poly_buffer_t *buffer, rational_t *a) {
318✔
3868
  uint32_t i, n;
3869

3870
  if (q_is_one(a) || q_is_minus_one(a)) {
318✔
3871
    return true;
265✔
3872
  }
3873

3874
  n = buffer->nterms;
53✔
3875
  for (i=0; i<n; i++) {
64✔
3876
    if (! q_divides(a, &buffer->mono[i].coeff)) return false;
63✔
3877
  }
3878
  return true;
1✔
3879
}
3880

3881

3882
/*
3883
 * Check whether a top-level assertion (p == 0) can be
3884
 * rewritten (t == q) where t is not internalized yet.
3885
 * - all_int is true if p is an integer polynomial (i.e.,
3886
 *   all coefficients and all terms of p are integer).
3887
 * - p = input polynomial
3888
 * - return t or null_term if no adequate t is found
3889
 */
3890
static term_t try_poly_substitution(context_t *ctx, poly_buffer_t *buffer, bool all_int) {
544✔
3891
  uint32_t i, n;
3892
  term_t t;
3893

3894
  // check for a free variable in buffer
3895
  n = buffer->nterms;
544✔
3896
  for (i=0; i<n; i++) {
1,128✔
3897
    t = buffer->mono[i].var;
1,049✔
3898
    if (t != const_idx && is_elimination_candidate(ctx, t)) {
1,049✔
3899
      if (in_real_class(ctx, t) ||
518✔
3900
          (all_int && integralpoly_after_div(buffer, &buffer->mono[i].coeff))) {
318✔
3901
        // t is candidate for elimination
3902
        return t;
465✔
3903
      }
3904
    }
3905
  }
3906

3907
  return NULL_TERM;
79✔
3908
}
3909

3910

3911
/*
3912
 * Build polynomial - p/a + x in the context's aux_poly buffer
3913
 * where a = coefficient of x in p
3914
 * - x must occur in p
3915
 */
3916
static polynomial_t *build_poly_substitution(context_t *ctx, poly_buffer_t *buffer, term_t x) {
465✔
3917
  polynomial_t *q;
3918
  monomial_t *mono;
3919
  uint32_t i, n;
3920
  term_t y;
3921
  rational_t *a;
3922

3923
  n = buffer->nterms;
465✔
3924

3925
  // first get coefficient of x in buffer
3926
  a = NULL; // otherwise GCC complains
465✔
3927
  for (i=0; i<n; i++) {
2,283✔
3928
    y = buffer->mono[i].var;
1,818✔
3929
    if (y == x) {
1,818✔
3930
      a = &buffer->mono[i].coeff;
465✔
3931
    }
3932
  }
3933
  assert(a != NULL && n > 0);
3934

3935
  q = context_get_aux_poly(ctx, n);
465✔
3936
  q->nterms = n-1;
465✔
3937
  mono = q->mono;
465✔
3938

3939
  // compute - buffer/a (but skip monomial a.x)
3940
  for (i=0; i<n; i++) {
2,283✔
3941
    y = buffer->mono[i].var;
1,818✔
3942
    if (y != x) {
1,818✔
3943
      mono->var = y;
1,353✔
3944
      q_set_neg(&mono->coeff, &buffer->mono[i].coeff);
1,353✔
3945
      q_div(&mono->coeff, a);
1,353✔
3946
      mono ++;
1,353✔
3947
    }
3948
  }
3949

3950
  // end marker
3951
  mono->var = max_idx;
465✔
3952

3953
  return q;
465✔
3954
}
3955

3956

3957

3958
/*
3959
 * Try to eliminate a toplevel equality (p == 0) by variable substitution:
3960
 * - i.e., try to rewrite p == 0 into (x - q) == 0 where x is a free variable
3961
 *   then store the substitution x --> q in the internalization table.
3962
 * - all_int is true if p is an integer polynomial (i.e., all variables and all
3963
 *   coefficients of p are integer)
3964
 *
3965
 * - return true if the elimination succeeds
3966
 * - return false otherwise
3967
 */
3968
static bool try_arithvar_elim(context_t *ctx, polynomial_t *p, bool all_int) {
544✔
3969
  poly_buffer_t *buffer;
3970
  polynomial_t *q;
3971
  uint32_t i, n;
3972
  term_t t, u, r;
3973
  thvar_t x;
3974

3975
  /*
3976
   * First pass: internalize every term of p that's not a variable
3977
   * - we do that first to avoid circular substitutions (occurs-check)
3978
   */
3979
  n = p->nterms;
544✔
3980
  for (i=0; i<n; i++) {
2,599✔
3981
    t = p->mono[i].var;
2,055✔
3982
    if (t != const_idx && ! is_elimination_candidate(ctx, t)) {
2,055✔
3983
      (void) internalize_to_arith(ctx, t);
599✔
3984
    }
3985
  }
3986

3987

3988
  /*
3989
   * Apply variable renaming: this is to avoid circularities
3990
   * if p is of the form ... + a x + ... + b y + ...
3991
   * where both x and y are variables in the same class (i.e.,
3992
   * both are elimination candidates).
3993
   */
3994
  buffer = context_get_poly_buffer(ctx);
544✔
3995
  apply_renaming_to_poly(ctx, p, buffer);
544✔
3996

3997
  /*
3998
   * Search for a variable to substitute
3999
   */
4000
  u = try_poly_substitution(ctx, buffer, all_int);
544✔
4001
  if (u == NULL_TERM) {
544✔
4002
    return false; // no substitution found
79✔
4003
  }
4004

4005
  /*
4006
   * buffer is of the form a.u + p0, we rewrite (buffer == 0) to (u == q)
4007
   * where q = -1/a * p0
4008
   */
4009
  q = build_poly_substitution(ctx, buffer, u); // q is in ctx->aux_poly
465✔
4010

4011
  // convert q to a theory variable in the arithmetic solver
4012
  x = map_poly_to_arith(ctx, q);
465✔
4013

4014
  // map u (and its root) to x
4015
  r = intern_tbl_get_root(&ctx->intern, u);
465✔
4016
  assert(intern_tbl_root_is_free(&ctx->intern, r) && is_pos_term(r));
4017
  intern_tbl_map_root(&ctx->intern, r, thvar2code(x));
465✔
4018

4019
#if TRACE
4020
  printf("---> toplevel equality: ");
4021
  print_polynomial(stdout, p);
4022
  printf(" == 0\n");
4023
  printf("     simplified to ");
4024
  print_term(stdout, ctx->terms, u);
4025
  printf(" := ");
4026
  print_polynomial(stdout, q);
4027
  printf("\n");
4028
#endif
4029

4030
  return true;
465✔
4031
}
4032

4033

4034

4035

4036

4037

4038

4039
/******************************************************
4040
 *  TOP-LEVEL ARITHMETIC EQUALITIES OR DISEQUALITIES  *
4041
 *****************************************************/
4042

4043
static void assert_arith_bineq(context_t *ctx, term_t t1, term_t t2, bool tt);
4044

4045
/*
4046
 * Top-level equality: t == (ite c u1 u2) between arithmetic terms
4047
 * - apply lift-if rule: (t == (ite c u1 u2)) --> (ite c (t == u1) (t == u2)
4048
 * - if tt is true: assert the equality otherwise assert the disequality
4049
 */
4050
static void assert_ite_arith_bineq(context_t *ctx, composite_term_t *ite, term_t t, bool tt) {
451✔
4051
  literal_t l1, l2, l3;
4052

4053
  assert(ite->arity == 3);
4054

4055
  l1 = internalize_to_literal(ctx, ite->arg[0]);
451✔
4056
  if (l1 == true_literal) {
451✔
4057
    // (ite c u1 u2) --> u1
4058
    assert_arith_bineq(ctx, ite->arg[1], t, tt);
3✔
4059
  } else if (l1 == false_literal) {
448✔
4060
    // (ite c u1 u2) --> u2
4061
    assert_arith_bineq(ctx, ite->arg[2], t, tt);
15✔
4062
  } else {
4063
    l2 = map_arith_bineq(ctx, ite->arg[1], t); // (u1 == t)
433✔
4064
    l3 = map_arith_bineq(ctx, ite->arg[2], t); // (u2 == t)
433✔
4065
    assert_ite(&ctx->gate_manager, l1, l2, l3, tt);
433✔
4066
  }
4067
}
451✔
4068

4069

4070
/*
4071
 * Try substitution t1 := t2
4072
 * - both are arithmetic terms and roots in the internalization table
4073
 */
4074
static void try_arithvar_bineq_elim(context_t *ctx, term_t t1, term_t t2) {
164✔
4075
  intern_tbl_t *intern;
4076
  thvar_t x, y;
4077
  int32_t code;
4078

4079
  assert(is_pos_term(t1) && intern_tbl_is_root(&ctx->intern, t1) &&
4080
         intern_tbl_root_is_free(&ctx->intern, t1));
4081

4082
  intern = &ctx->intern;
164✔
4083

4084
  if (is_constant_term(ctx->terms, t2)) {
164✔
4085
    if (intern_tbl_valid_const_subst(intern, t1, t2)) {
2✔
4086
      intern_tbl_add_subst(intern, t1, t2);
2✔
4087
    } else {
4088
      // unsat by type incompatibility
4089
      longjmp(ctx->env, TRIVIALLY_UNSAT);
×
4090
    }
4091

4092
  } else if (intern_tbl_sound_subst(intern, t1, t2)) {
162✔
4093
    /*
4094
     * Internalize t2 to x.
4095
     * If t1 is still free after that, we can map t1 to x
4096
     * otherwise, t2 depends on t1 so we can't substitute.
4097
     */
4098
    x = internalize_to_arith(ctx, t2);
161✔
4099
    if (intern_tbl_root_is_free(intern, t1)) {
161✔
4100
      intern_tbl_map_root(&ctx->intern, t1, thvar2code(x));
2✔
4101
    } else {
4102
      assert(intern_tbl_root_is_mapped(intern, t1));
4103
      code = intern_tbl_map_of_root(intern, t1);
159✔
4104
      y = translate_code_to_arith(ctx, code);
159✔
4105

4106
      // assert x == y in the arithmetic solver
4107
      ctx->arith.assert_vareq_axiom(ctx->arith_solver, x, y, true);
159✔
4108
    }
4109
  } else {
4110
    x = internalize_to_arith(ctx, t1);
1✔
4111
    y = internalize_to_arith(ctx, t2);
1✔
4112
    ctx->arith.assert_vareq_axiom(ctx->arith_solver, x, y, true);
1✔
4113
  }
4114
}
164✔
4115

4116

4117
/*
4118
 * Top-level arithmetic equality t1 == t2:
4119
 * - if tt is true: assert t1 == t2 otherwise assert (t1 != t2)
4120
 * - both t1 and t2 are arithmetic terms and roots in the internalization table
4121
 * - the equality (t1 == t2) is not reducible by if-then-else flattening
4122
 */
4123
static void assert_arith_bineq_aux(context_t *ctx, term_t t1, term_t t2, bool tt) {
3,335✔
4124
  term_table_t *terms;
4125
  intern_tbl_t *intern;;
4126
  bool free1, free2;
4127
  thvar_t x, y;
4128
  occ_t u, v;
4129

4130
  assert(is_pos_term(t1) && intern_tbl_is_root(&ctx->intern, t1) &&
4131
         is_pos_term(t2) && intern_tbl_is_root(&ctx->intern, t2));
4132

4133
  terms = ctx->terms;
3,335✔
4134
  if (is_ite_term(terms, t1) && !is_ite_term(terms, t2)) {
3,335✔
4135
    assert_ite_arith_bineq(ctx, ite_term_desc(terms, t1), t2, tt);
112✔
4136
    return;
112✔
4137
  }
4138

4139
  if (is_ite_term(terms, t2) && !is_ite_term(terms, t1)) {
3,223✔
4140
    assert_ite_arith_bineq(ctx, ite_term_desc(terms, t2), t1, tt);
339✔
4141
    return;
339✔
4142
  }
4143

4144
  if (tt && context_arith_elim_enabled(ctx)) {
2,884✔
4145
    /*
4146
     * try a substitution
4147
     */
4148
    intern = &ctx->intern;
340✔
4149
    free1 = intern_tbl_root_is_free(intern, t1);
340✔
4150
    free2 = intern_tbl_root_is_free(intern, t2);
340✔
4151

4152
    if (free1 && free2) {
340✔
4153
      if (t1 != t2) {
1✔
4154
        intern_tbl_merge_classes(intern, t1, t2);
1✔
4155
      }
4156
      return;
1✔
4157
    }
4158

4159
    if (free1) {
339✔
4160
      try_arithvar_bineq_elim(ctx, t1, t2);
162✔
4161
      return;
162✔
4162
    }
4163

4164
    if (free2) {
177✔
4165
      try_arithvar_bineq_elim(ctx, t2, t1);
2✔
4166
      return;
2✔
4167
    }
4168

4169
  }
4170

4171
  /*
4172
   * Default: assert the constraint in the egraph or in the arithmetic
4173
   * solver if there's no egraph.
4174
   */
4175
  if (context_has_egraph(ctx)) {
2,719✔
4176
    u = internalize_to_eterm(ctx, t1);
1,658✔
4177
    v = internalize_to_eterm(ctx, t2);
1,658✔
4178
    if (!context_quant_enabled(ctx) || egraph_is_at_base_level(ctx->egraph)) {
1,658✔
4179
      if (tt) {
1,658✔
4180
        egraph_assert_eq_axiom(ctx->egraph, u, v);
162✔
4181
      } else {
4182
        egraph_assert_diseq_axiom(ctx->egraph, u, v);
1,496✔
4183
      }
4184
    } else {
4185
      literal_t l = egraph_make_eq(ctx->egraph, u, v);
×
4186
      if (tt) {
×
4187
        add_unit_clause(ctx->core, l);
×
4188
      } else {
4189
        add_unit_clause(ctx->core, not(l));
×
4190
      }
4191
    }
4192
  } else {
4193
    x = internalize_to_arith(ctx, t1);
1,061✔
4194
    y = internalize_to_arith(ctx, t2);
1,061✔
4195
    ctx->arith.assert_vareq_axiom(ctx->arith_solver, x, y, tt);
1,061✔
4196
  }
4197
}
4198

4199

4200

4201

4202
/*****************************************************
4203
 *  INTERNALIZATION OF TOP-LEVEL ATOMS AND FORMULAS  *
4204
 ****************************************************/
4205

4206
/*
4207
 * Recursive function: assert (t == tt) for a boolean term t
4208
 * - this is used when a toplevel formula simplifies to t
4209
 *   For example (ite c t u) --> t if c is true.
4210
 * - t is not necessarily a root in the internalization table
4211
 */
4212
static void assert_term(context_t *ctx, term_t t, bool tt);
4213

4214

4215
/*
4216
 * Top-level predicate: (p t_1 .. t_n)
4217
 * - if tt is true: assert (p t_1 ... t_n)
4218
 * - if tt is false: assert (not (p t_1 ... t_n))
4219
 */
4220
static void assert_toplevel_apply(context_t *ctx, composite_term_t *app, bool tt) {
252✔
4221
  occ_t *a;
4222
  uint32_t i, n;
4223

4224
  assert(app->arity > 0);
4225

4226
  n = app->arity;
252✔
4227

4228
  check_high_order_support(ctx, app->arg+1, n-1);
252✔
4229

4230
  a = alloc_istack_array(&ctx->istack, n);
252✔
4231
  for (i=0; i<n; i++) {
1,030✔
4232
    a[i] = internalize_to_eterm(ctx, app->arg[i]);
778✔
4233
  }
4234

4235
  if (!context_quant_enabled(ctx) || egraph_is_at_base_level(ctx->egraph)) {
252✔
4236
    if (tt) {
242✔
4237
      egraph_assert_pred_axiom(ctx->egraph, a[0], n-1, a+1);
214✔
4238
    } else {
4239
      egraph_assert_notpred_axiom(ctx->egraph, a[0], n-1, a+1);
28✔
4240
    }
4241
  } else {
4242
    literal_t l = egraph_make_pred(ctx->egraph, a[0], n-1, a+1);
10✔
4243
    if (tt) {
10✔
4244
      add_unit_clause(ctx->core, l);
3✔
4245
    } else {
4246
      add_unit_clause(ctx->core, not(l));
7✔
4247
    }
4248
  }
4249

4250
  free_istack_array(&ctx->istack, a);
252✔
4251
}
252✔
4252

4253

4254
/*
4255
 * Top-level (select i t)
4256
 * - if tt is true: assert (select i t)
4257
 * - if tt is false: assert (not (select i t))
4258
 */
4259
static void assert_toplevel_select(context_t *ctx, select_term_t *select, bool tt) {
×
4260
  occ_t u;
4261

4262
  u = map_select_to_eterm(ctx, select, bool_type(ctx->types));
×
4263
  if (! tt) {
×
4264
    u = opposite_occ(u);
×
4265
  }
4266
  if (!context_quant_enabled(ctx) || egraph_is_at_base_level(ctx->egraph)) {
×
4267
    egraph_assert_axiom(ctx->egraph, u);
×
4268
  } else {
4269
    literal_t l = egraph_make_eq(ctx->egraph, u, true_occ);
×
4270
    add_unit_clause(ctx->core, l);
×
4271
  }
4272
}
×
4273

4274

4275
/*
4276
 * Top-level equality between Boolean terms
4277
 * - if tt is true, assert t1 == t2
4278
 * - if tt is false, assert t1 != t2
4279
 */
4280
static void assert_toplevel_iff(context_t *ctx, term_t t1, term_t t2, bool tt) {
2,735✔
4281
  term_t t;
4282
  literal_t l1, l2;
4283

4284
  /*
4285
   * Apply substitution then try flattening
4286
   */
4287
  t1 = intern_tbl_get_root(&ctx->intern, t1);
2,735✔
4288
  t2 = intern_tbl_get_root(&ctx->intern, t2);
2,735✔
4289
  if (t1 == t2) {
2,735✔
4290
    // (eq t1 t2) is true
4291
    if (!tt) {
×
4292
      longjmp(ctx->env, TRIVIALLY_UNSAT);
×
4293
    }
4294
  }
4295
  // try simplification
4296
  t = simplify_bool_eq(ctx, t1, t2);
2,735✔
4297
  if (t != NULL_TERM) {
2,735✔
4298
    // (eq t1 t2) is equivalent to t
4299
    assert_term(ctx, t, tt) ;
17✔
4300
  } else {
4301
    // no simplification
4302
    l1 = internalize_to_literal(ctx, t1);
2,718✔
4303
    l2 = internalize_to_literal(ctx, t2);
2,718✔
4304
    assert_iff(&ctx->gate_manager, l1, l2, tt);
2,718✔
4305

4306
#if 0
4307
    if (tt) {
4308
      printf("top assert: (eq ");
4309
      print_literal(stdout, l1);
4310
      printf(" ");
4311
      print_literal(stdout, l2);
4312
      printf(")\n");
4313
    } else {
4314
      printf("top assert: (xor ");
4315
      print_literal(stdout, l1);
4316
      printf(" ");
4317
      print_literal(stdout, l2);
4318
      printf(")\n");
4319
    }
4320
#endif
4321
  }
4322
}
2,735✔
4323

4324
/*
4325
 * Top-level equality assertion (eq t1 t2):
4326
 * - if tt is true, assert (t1 == t2)
4327
 *   if tt is false, assert (t1 != t2)
4328
 */
4329
static void assert_toplevel_eq(context_t *ctx, composite_term_t *eq, bool tt) {
5,129✔
4330
  occ_t u1, u2;
4331

4332
  assert(eq->arity == 2);
4333

4334
  if (is_boolean_term(ctx->terms, eq->arg[0])) {
5,129✔
4335
    assert(is_boolean_term(ctx->terms, eq->arg[1]));
4336
    assert_toplevel_iff(ctx, eq->arg[0], eq->arg[1], tt);
2,735✔
4337
  } else {
4338
    // filter out high-order terms. It's enough to check eq->arg[0]
4339
    check_high_order_support(ctx, eq->arg, 1);
2,394✔
4340

4341
    u1 = internalize_to_eterm(ctx, eq->arg[0]);
2,393✔
4342
    u2 = internalize_to_eterm(ctx, eq->arg[1]);
2,393✔
4343
    if (!context_quant_enabled(ctx) || egraph_is_at_base_level(ctx->egraph)) {
2,392✔
4344
      if (tt) {
2,378✔
4345
        egraph_assert_eq_axiom(ctx->egraph, u1, u2);
1,323✔
4346
      } else {
4347
        egraph_assert_diseq_axiom(ctx->egraph, u1, u2);
1,055✔
4348
      }
4349
    } else {
4350
      literal_t l = egraph_make_eq(ctx->egraph, u1, u2);
14✔
4351
      if (tt) {
14✔
4352
        add_unit_clause(ctx->core, l);
9✔
4353
      } else {
4354
        add_unit_clause(ctx->core, not(l));
5✔
4355
      }
4356
    }
4357
  }
4358
}
5,127✔
4359

4360

4361
/*
4362
 * Assertion (distinct a[0] .... a[n-1]) == tt
4363
 * when a[0] ... a[n-1] are arithmetic variables.
4364
 */
4365
static void assert_arith_distinct(context_t *ctx, uint32_t n, thvar_t *a, bool tt) {
155✔
4366
  literal_t l;
4367

4368
  l = make_arith_distinct(ctx, n, a);
155✔
4369
  if (! tt) {
155✔
4370
    l = not(l);
4✔
4371
  }
4372
  add_unit_clause(ctx->core, l);
155✔
4373
}
155✔
4374

4375

4376
/*
4377
 * Assertion (distinct a[0] .... a[n-1]) == tt
4378
 * when a[0] ... a[n-1] are bitvector variables.
4379
 */
4380
static void assert_bv_distinct(context_t *ctx, uint32_t n, thvar_t *a, bool tt) {
2✔
4381
  literal_t l;
4382

4383
  l = make_bv_distinct(ctx, n, a);
2✔
4384
  if (! tt) {
2✔
4385
    l = not(l);
1✔
4386
  }
4387
  add_unit_clause(ctx->core, l);
2✔
4388
}
2✔
4389

4390

4391
/*
4392
 * Generic (distinct t1 .. t_n)
4393
 * - if tt: assert (distinct t_1 ... t_n)
4394
 * - otherwise: assert (not (distinct t_1 ... t_n))
4395
 */
4396
static void assert_toplevel_distinct(context_t *ctx, composite_term_t *distinct, bool tt) {
172✔
4397
  uint32_t i, n;
4398
  int32_t *a;
4399

4400
  n = distinct->arity;
172✔
4401
  assert(n >= 2);
4402

4403
  a = alloc_istack_array(&ctx->istack, n);
172✔
4404

4405
  if (context_has_egraph(ctx)) {
172✔
4406
    // fail if arguments have function types and we don't
4407
    // have a function/array solver
4408
    check_high_order_support(ctx, distinct->arg, 1);
15✔
4409

4410
    // forward the assertion to the egraph
4411
    for (i=0; i<n; i++) {
63✔
4412
      a[i] = internalize_to_eterm(ctx, distinct->arg[i]);
50✔
4413
    }
4414

4415
    if (!context_quant_enabled(ctx) || egraph_is_at_base_level(ctx->egraph)) {
13✔
4416
      if (tt) {
13✔
4417
        egraph_assert_distinct_axiom(ctx->egraph, n, a);
9✔
4418
      } else {
4419
        egraph_assert_notdistinct_axiom(ctx->egraph, n, a);
4✔
4420
      }
4421
    } else {
4422
      literal_t l = egraph_make_distinct(ctx->egraph, n, a);
×
4423
      if (tt) {
×
4424
        add_unit_clause(ctx->core, l);
×
4425
      } else {
4426
        add_unit_clause(ctx->core, not(l));
×
4427
      }
4428
    }
4429

4430
  } else if (is_arithmetic_term(ctx->terms, distinct->arg[0])) {
157✔
4431
    // translate to arithmetic then assert
4432
    for (i=0; i<n; i++) {
3,922✔
4433
      a[i] = internalize_to_arith(ctx, distinct->arg[i]);
3,767✔
4434
    }
4435
    assert_arith_distinct(ctx, n, a, tt);
155✔
4436

4437
  } else if (is_bitvector_term(ctx->terms, distinct->arg[0])) {
2✔
4438
    // translate to bitvectors then assert
4439
    for (i=0; i<n; i++) {
13✔
4440
      a[i] = internalize_to_bv(ctx, distinct->arg[i]);
11✔
4441
    }
4442
    assert_bv_distinct(ctx, n, a, tt);
2✔
4443

4444
  } else {
4445
    longjmp(ctx->env, uf_error_code(ctx, distinct->arg[0]));
×
4446
  }
4447

4448
  free_istack_array(&ctx->istack, a);
170✔
4449
}
170✔
4450

4451

4452

4453
/*
4454
 * Top-level arithmetic equality t1 == u1:
4455
 * - t1 and u1 are arithmetic terms
4456
 * - if tt is true assert (t1 == u1) otherwise assert (t1 != u1)
4457
 * - apply lift-if simplifications and variable elimination
4458
 */
4459
static void assert_arith_bineq(context_t *ctx, term_t t1, term_t u1, bool tt) {
3,408✔
4460
  ivector_t *v;
4461
  int32_t *a;
4462
  uint32_t i, n;
4463
  term_t t2, u2;
4464

4465
  /*
4466
   * Apply substitutions then try if-then-else flattening
4467
   */
4468
  t1 = intern_tbl_get_root(&ctx->intern, t1);
3,408✔
4469
  u1 = intern_tbl_get_root(&ctx->intern, u1);
3,408✔
4470

4471
  v = &ctx->aux_vector;
3,408✔
4472
  assert(v->size == 0);
4473
  t2 = flatten_ite_equality(ctx, v, t1, u1);
3,408✔
4474
  u2 = flatten_ite_equality(ctx, v, u1, t2);
3,408✔
4475

4476
  /*
4477
   * (t1 == u1) is now equivalent to
4478
   * the conjunction of (t2 == u2) and all the terms in v
4479
   */
4480
  n = v->size;
3,408✔
4481
  if (n == 0) {
3,408✔
4482
    /*
4483
     * The simple flattening did not work.
4484
     */
4485
    assert(t1 == t2 && u1 == u2);
4486
    assert_arith_bineq_aux(ctx, t2, u2, tt);
3,322✔
4487

4488
  } else {
4489
    // make a copy of v[0 ... n-1]
4490
    // and reserve a[n] for the literal (eq t2 u2)
4491
    a = alloc_istack_array(&ctx->istack, n+1);
86✔
4492
    for (i=0; i<n; i++) {
1,531✔
4493
      a[i] = v->data[i];
1,445✔
4494
    }
4495
    ivector_reset(v);
86✔
4496

4497
    if (tt) {
86✔
4498
      // assert (and a[0] ... a[n-1] (eq t2 u2))
4499
      for (i=0; i<n; i++) {
340✔
4500
        assert_term(ctx, a[i], true);
327✔
4501
      }
4502

4503
      /*
4504
       * The assertions a[0] ... a[n-1] may have
4505
       * caused roots to be merged. So we must
4506
       * apply term substitution again.
4507
       */
4508
      t2 = intern_tbl_get_root(&ctx->intern, t2);
13✔
4509
      u2 = intern_tbl_get_root(&ctx->intern, u2);
13✔
4510
      assert_arith_bineq_aux(ctx, t2, u2, true);
13✔
4511

4512
    } else {
4513
      // assert (or (not a[0]) ... (not a[n-1]) (not (eq t2 u2)))
4514
      for (i=0; i<n; i++) {
1,191✔
4515
        a[i] = not(internalize_to_literal(ctx, a[i]));
1,118✔
4516
      }
4517
      a[n] = not(map_arith_bineq_aux(ctx, t2, u2));
73✔
4518

4519
      add_clause(ctx->core, n+1, a);
73✔
4520
    }
4521

4522
    free_istack_array(&ctx->istack, a);
86✔
4523
  }
4524
}
3,408✔
4525

4526

4527
/*
4528
 * Top-level arithmetic assertion:
4529
 * - if tt is true, assert p == 0
4530
 * - if tt is false, assert p != 0
4531
 */
4532
static void assert_toplevel_poly_eq(context_t *ctx, polynomial_t *p, bool tt) {
1,273✔
4533
  uint32_t i, n;
4534
  thvar_t *a;
4535

4536
  n = p->nterms;
1,273✔
4537
  a = alloc_istack_array(&ctx->istack, n);;
1,273✔
4538
  // skip the constant if any
4539
  i = 0;
1,273✔
4540
  if (p->mono[0].var == const_idx) {
1,273✔
4541
    a[0] = null_thvar;
1,208✔
4542
    i ++;
1,208✔
4543
  }
4544

4545
  // deal with the non-constant monomials
4546
  while (i<n) {
3,928✔
4547
    a[i] = internalize_to_arith(ctx, p->mono[i].var);
2,655✔
4548
    i ++;
2,655✔
4549
  }
4550

4551
  // assertion
4552
  ctx->arith.assert_poly_eq_axiom(ctx->arith_solver, p, a, tt);
1,273✔
4553
  free_istack_array(&ctx->istack, a);
1,273✔
4554
}
1,273✔
4555

4556

4557

4558
/*
4559
 * Top-level arithmetic equality:
4560
 * - t is an arithmetic term
4561
 * - if tt is true, assert (t == 0)
4562
 * - otherwise, assert (t != 0)
4563
 */
4564
static void assert_toplevel_arith_eq(context_t *ctx, term_t t, bool tt) {
2,268✔
4565
  term_table_t *terms;
4566
  polynomial_t *p;
4567
  bool all_int;
4568
  thvar_t x;
4569

4570
  assert(is_arithmetic_term(ctx->terms, t));
4571

4572
  terms = ctx->terms;
2,268✔
4573
  if (tt && context_arith_elim_enabled(ctx) && term_kind(terms, t) == ARITH_POLY) {
2,268✔
4574
    /*
4575
     * Polynomial equality: a_1 t_1 + ... + a_n t_n = 0
4576
     * attempt to eliminate one of t_1 ... t_n
4577
     */
4578
    p = poly_term_desc(terms, t);
544✔
4579
    all_int = is_integer_term(terms, t);
544✔
4580
    if (try_arithvar_elim(ctx, p, all_int)) { // elimination worked
544✔
4581
      return;
465✔
4582
    }
4583
  }
4584

4585
  // default
4586
  if (term_kind(terms, t) == ARITH_POLY) {
1,803✔
4587
    assert_toplevel_poly_eq(ctx, poly_term_desc(terms, t), tt);
1,273✔
4588
  } else if (is_ite_term(terms, t)) {
530✔
4589
    assert_arith_bineq(ctx, t, zero_term, tt);
8✔
4590
  } else {
4591
    x = internalize_to_arith(ctx, t);
522✔
4592
    ctx->arith.assert_eq_axiom(ctx->arith_solver, x, tt);
520✔
4593
  }
4594
}
4595

4596

4597

4598
/*
4599
 * Top-level arithmetic assertion:
4600
 * - if tt is true, assert p >= 0
4601
 * - if tt is false, assert p < 0
4602
 */
4603
static void assert_toplevel_poly_geq(context_t *ctx, polynomial_t *p, bool tt) {
25,930✔
4604
  uint32_t i, n;
4605
  thvar_t *a;
4606

4607
  n = p->nterms;
25,930✔
4608
  a = alloc_istack_array(&ctx->istack, n);;
25,930✔
4609
  // skip the constant if any
4610
  i = 0;
25,930✔
4611
  if (p->mono[0].var == const_idx) {
25,930✔
4612
    a[0] = null_thvar;
25,104✔
4613
    i ++;
25,104✔
4614
  }
4615

4616
  // deal with the non-constant monomials
4617
  while (i<n) {
55,603✔
4618
    a[i] = internalize_to_arith(ctx, p->mono[i].var);
29,674✔
4619
    i ++;
29,673✔
4620
  }
4621

4622
  // assertion
4623
  ctx->arith.assert_poly_ge_axiom(ctx->arith_solver, p, a, tt);
25,929✔
4624
  free_istack_array(&ctx->istack, a);
25,929✔
4625
}
25,929✔
4626

4627

4628

4629
/*
4630
 * Top-level arithmetic inequality:
4631
 * - t is an arithmetic term
4632
 * - if tt is true, assert (t >= 0)
4633
 * - if tt is false, assert (t < 0)
4634
 */
4635
static void assert_toplevel_arith_geq(context_t *ctx, term_t t, bool tt) {
27,855✔
4636
  term_table_t *terms;
4637
  thvar_t x;
4638

4639
  assert(is_arithmetic_term(ctx->terms, t));
4640

4641
  terms = ctx->terms;
27,855✔
4642
  if (term_kind(terms, t) == ARITH_POLY) {
27,855✔
4643
    assert_toplevel_poly_geq(ctx, poly_term_desc(terms, t), tt);
25,930✔
4644
  } else {
4645
    x = internalize_to_arith(ctx, t);
1,925✔
4646
    ctx->arith.assert_ge_axiom(ctx->arith_solver, x, tt);
1,925✔
4647
  }
4648
}
27,854✔
4649

4650

4651
/*
4652
 * Top-level binary equality: (eq t u)
4653
 * - both t and u are arithmetic terms
4654
 * - if tt is true, assert (t == u)
4655
 * - if tt is false, assert (t != u)
4656
 */
4657
static void assert_toplevel_arith_bineq(context_t *ctx, composite_term_t *eq, bool tt) {
3,382✔
4658
  assert(eq->arity == 2);
4659
  assert_arith_bineq(ctx, eq->arg[0], eq->arg[1], tt);
3,382✔
4660
}
3,382✔
4661

4662

4663

4664
/*
4665
 * Top-level (is_int t)
4666
 * - t is an arithmetic term
4667
 * - if tt is true, assert (t <= (floor t))
4668
 * - if tt is false, asssert (t > (floor t))
4669
 *
4670
 * NOTE: instead of asserting (t <= (floor t)) we could create a fresh
4671
 * integer variable z and assert (t = z).
4672
 */
4673
static void assert_toplevel_arith_is_int(context_t *ctx, term_t t, bool tt) {
×
4674
  polynomial_t *p;
4675
  thvar_t map[2];
4676
  thvar_t x, y;
4677

4678
  x = internalize_to_arith(ctx, t);
×
4679
  if (ctx->arith.arith_var_is_int(ctx->arith_solver, x)) {
×
4680
    if (!tt) {
×
4681
      longjmp(ctx->env, TRIVIALLY_UNSAT);
×
4682
    }
4683
  } else {
4684
    // x is not an integer variable
4685
    y = get_floor(ctx, x); // y := (floor x)
×
4686
    p = context_get_aux_poly(ctx, 3);
×
4687
    context_store_diff_poly(p, map, y, x); // (p, map) stores (y - x)
×
4688
    // assert either (p >= 0) --> (x <= floor(x))
4689
    // or (p < 0) --> (x > (floor x)
4690
    ctx->arith.assert_poly_ge_axiom(ctx->arith_solver, p, map, tt);
×
4691
  }
4692
}
×
4693

4694

4695
/*
4696
 * Top-level (divides k t)
4697
 * - if tt is true, assert (t <= k * (div t k))
4698
 * - if tt is false, assert (t > k * (div t k))
4699
 *
4700
 * We assume (k != 0) since (divides 0 t) is rewritten to (t == 0) by
4701
 * the term manager.
4702
 *
4703
 * NOTE: instead of asserting (t <= k * (div t k)) we could create a fresh
4704
 * integer variable z and assert (t = k * z).
4705
 */
4706
static void assert_toplevel_arith_divides(context_t *ctx, composite_term_t *divides, bool tt) {
×
4707
  rational_t k;
4708
  polynomial_t *p;
4709
  thvar_t map[2];
4710
  thvar_t x, y;
4711
  term_t d;
4712

4713
  assert(divides->arity == 2);
4714

4715
  d = divides->arg[0];
×
4716
  if (term_kind(ctx->terms, d) == ARITH_CONSTANT) {
×
4717
    // copy the divider
4718
    q_init(&k);
×
4719
    q_set(&k, rational_term_desc(ctx->terms, d));
×
4720
    assert(q_is_nonzero(&k));
4721

4722
    x = internalize_to_arith(ctx, divides->arg[1]);
×
4723
    y = get_div(ctx, x, &k);  // y := (div x k);
×
4724
    p = context_get_aux_poly(ctx, 3);
×
4725
    context_store_divides_constraint(p, map, x, y, &k); // p is (- x + k * y)
×
4726

4727
    // if tt, assert (p >= 0) <=> x <= k * y
4728
    // if not tt, assert (p < 0) <=> x > k * y
4729
    ctx->arith.assert_poly_ge_axiom(ctx->arith_solver, p, map, tt);
×
4730

4731
    q_clear(&k);
×
4732
  } else {
4733
    // not a constant divider: not supported
4734
    longjmp(ctx->env, FORMULA_NOT_LINEAR);
×
4735
  }
4736
}
×
4737

4738

4739

4740

4741

4742

4743
/*
4744
 * Top-level conditional
4745
 * - c = conditional descriptor
4746
 * - if tt is true: assert c otherwise assert not c
4747
 *
4748
 * - c->nconds = number of clauses in the conditional
4749
 * - for each clause i: c->pair[i] = <cond, val>
4750
 * - c->defval = default value
4751
 */
4752
static void assert_toplevel_conditional(context_t *ctx, conditional_t *c, bool tt) {
13✔
4753
  uint32_t i, n;
4754
  literal_t *a;
4755
  literal_t l;
4756
  bool all_false;
4757
  term_t t;
4758

4759
#if 0
4760
  printf("---> toplevel conditional\n");
4761
#endif
4762

4763
  t = simplify_conditional(ctx, c);
13✔
4764
  if (t != NULL_TERM) {
13✔
4765
    assert_term(ctx, t, tt);
2✔
4766
    return;
2✔
4767
  }
4768

4769
  n = c->nconds;
11✔
4770
  a = alloc_istack_array(&ctx->istack, n + 1);
11✔
4771

4772
  all_false = true;
11✔
4773
  for (i=0; i<n; i++) {
72✔
4774
    // a[i] = condition for pair[i]
4775
    a[i] = internalize_to_literal(ctx, c->pair[i].cond);
62✔
4776
    if (a[i] == true_literal) {
62✔
4777
      // if a[i] is true, all other conditions must be false
4778
      assert_term(ctx, c->pair[i].val, tt);
1✔
4779
      goto done;
1✔
4780
    }
4781
    if (a[i] != false_literal) {
61✔
4782
      // l = value for pair[i]
4783
      l = signed_literal(internalize_to_literal(ctx, c->pair[i].val), tt);
58✔
4784
      add_binary_clause(ctx->core, not(a[i]), l); // a[i] => v[i]
58✔
4785
      all_false = false;
58✔
4786
    }
4787
  }
4788

4789
  if (all_false) {
10✔
4790
    // all a[i]s are false: no need for a clause
4791
    assert_term(ctx, c->defval, tt);
1✔
4792
    goto done;
1✔
4793
  }
4794

4795
  // last clause: (a[0] \/ .... \/ a[n] \/ +/-defval)
4796
  a[n] = signed_literal(internalize_to_literal(ctx, c->defval), tt);
9✔
4797
  add_clause(ctx->core, n+1, a);
9✔
4798

4799
  // cleanup
4800
 done:
11✔
4801
  free_istack_array(&ctx->istack, a);
11✔
4802
}
4803

4804

4805

4806
/*
4807
 * Top-level boolean if-then-else (ite c t1 t2)
4808
 * - if tt is true: assert (ite c t1 t2)
4809
 * - if tt is false: assert (not (ite c t1 t2))
4810
 */
4811
static void assert_toplevel_ite(context_t *ctx, composite_term_t *ite, bool tt) {
70✔
4812
  conditional_t *d;
4813
  literal_t l1, l2, l3;
4814

4815
  assert(ite->arity == 3);
4816

4817
  // high-order ite should work. See map_ite_to_eterm
4818

4819
  d = context_make_conditional(ctx, ite);
70✔
4820
  if (d != NULL) {
70✔
4821
    assert_toplevel_conditional(ctx, d, tt);
13✔
4822
    context_free_conditional(ctx, d);
13✔
4823
    return;
13✔
4824
  }
4825

4826
  l1 = internalize_to_literal(ctx, ite->arg[0]);
57✔
4827
  if (l1 == true_literal) {
57✔
4828
    assert_term(ctx, ite->arg[1], tt);
5✔
4829
  } else if (l1 == false_literal) {
52✔
4830
    assert_term(ctx, ite->arg[2], tt);
2✔
4831
  } else {
4832
    l2 = internalize_to_literal(ctx, ite->arg[1]);
50✔
4833
    l3 = internalize_to_literal(ctx, ite->arg[2]);
50✔
4834
    assert_ite(&ctx->gate_manager, l1, l2, l3, tt);
50✔
4835
  }
4836
}
4837

4838

4839
/*
4840
 * Top-level (or t1 ... t_n)
4841
 * - it tt is true: add a clause
4842
 * - it tt is false: assert (not t1) ... (not t_n)
4843
 */
4844
static void assert_toplevel_or(context_t *ctx, composite_term_t *or, bool tt) {
42,447✔
4845
  ivector_t *v;
4846
  int32_t *a;
4847
  uint32_t i, n;
4848

4849
  if (tt) {
42,447✔
4850
    if (context_flatten_or_enabled(ctx)) {
42,431✔
4851
      // Flatten into vector v
4852
      v = &ctx->aux_vector;
14,495✔
4853
      assert(v->size == 0);
4854
      flatten_or_term(ctx, v, or);
14,495✔
4855

4856
      // if v contains a true_term, ignore the clause
4857
      n = v->size;
14,495✔
4858
      if (disjunct_is_true(ctx, v->data, n)) {
14,495✔
4859
        ivector_reset(v);
803✔
4860
        return;
803✔
4861
      }
4862

4863
      // make a copy of v
4864
      a = alloc_istack_array(&ctx->istack, n);
13,692✔
4865
      for (i=0; i<n; i++) {
57,691✔
4866
        a[i] = v->data[i];
43,999✔
4867
      }
4868
      ivector_reset(v);
13,692✔
4869

4870
      for (i=0; i<n; i++) {
57,368✔
4871
        a[i] = internalize_to_literal(ctx, a[i]);
43,829✔
4872
        if (a[i] == true_literal) goto done;
43,829✔
4873
      }
4874

4875
    } else {
4876
      /*
4877
       * No flattening
4878
       */
4879
      n = or->arity;
27,936✔
4880
      if (disjunct_is_true(ctx, or->arg, n)) {
27,936✔
4881
        return;
2,503✔
4882
      }
4883

4884
      a = alloc_istack_array(&ctx->istack, n);
25,433✔
4885
      for (i=0; i<n; i++) {
143,590✔
4886
        a[i] = internalize_to_literal(ctx, or->arg[i]);
118,257✔
4887
        if (a[i] == true_literal) goto done;
118,257✔
4888
      }
4889
    }
4890

4891
    // assert (or a[0] ... a[n-1])
4892
    add_clause(ctx->core, n, a);
38,872✔
4893

4894
  done:
39,125✔
4895
    free_istack_array(&ctx->istack, a);
39,125✔
4896

4897
  } else {
4898
    /*
4899
     * Propagate to children:
4900
     *  (or t_0 ... t_n-1) is false
4901
     * so all children must be false too
4902
     */
4903
    n = or->arity;
16✔
4904
    for (i=0; i<n; i++) {
48✔
4905
      assert_term(ctx, or->arg[i], false);
32✔
4906
    }
4907
  }
4908

4909
}
4910

4911

4912
/*
4913
 * Top-level (xor t1 ... t_n) == tt
4914
 */
4915
static void assert_toplevel_xor(context_t *ctx, composite_term_t *xor, bool tt) {
7✔
4916
  int32_t *a;
4917
  uint32_t i, n;
4918

4919
  n = xor->arity;
7✔
4920
  a = alloc_istack_array(&ctx->istack, n);
7✔
4921
  for (i=0; i<n; i++) {
58✔
4922
    a[i] = internalize_to_literal(ctx, xor->arg[i]);
51✔
4923
  }
4924

4925
  assert_xor(&ctx->gate_manager, n, a, tt);
7✔
4926
  free_istack_array(&ctx->istack, a);
7✔
4927
}
7✔
4928

4929

4930

4931
/*
4932
 * Top-level bit select
4933
 */
4934
static void assert_toplevel_bit_select(context_t *ctx, select_term_t *select, bool tt) {
32,467✔
4935
  term_t t, s;
4936
  thvar_t x;
4937

4938
  /*
4939
   * Check for simplification
4940
   */
4941
  t = intern_tbl_get_root(&ctx->intern, select->arg);
32,467✔
4942
  s = extract_bit(ctx->terms, t, select->idx);
32,467✔
4943
  if (s != NULL_TERM) {
32,467✔
4944
    // (select t i) is s
4945
    assert_term(ctx, s, tt);
63✔
4946
  } else {
4947
    // no simplification
4948
    x = internalize_to_bv(ctx, select->arg);
32,404✔
4949
    ctx->bv.set_bit(ctx->bv_solver, x, select->idx, tt);
32,404✔
4950
  }
4951
}
32,466✔
4952

4953

4954
/*
4955
 * Top-level bitvector atoms
4956
 */
4957
// Auxiliary function: assert (t == 0) or (t != 0) depending on tt
4958
static void assert_toplevel_bveq0(context_t *ctx, term_t t, bool tt) {
28✔
4959
  uint32_t n;
4960
  thvar_t x, y;
4961

4962
  t = intern_tbl_get_root(&ctx->intern, t);
28✔
4963
  n = term_bitsize(ctx->terms, t);
28✔
4964
  x = internalize_to_bv(ctx, t);
28✔
4965
  y = ctx->bv.create_zero(ctx->bv_solver, n);
28✔
4966
  ctx->bv.assert_eq_axiom(ctx->bv_solver, x, y, tt);
28✔
4967
}
28✔
4968

4969

4970
/*
4971
 * Experimental: when t1 and t2 have a common factor C:
4972
 *   t1 = C * u1
4973
 *   t2 = C * u2
4974
 * then we have (t1 /= t2) implies (u1 /= u2).
4975
 * So we can add (u1 /= u2) when (t1 /= t2) is asserted.
4976
 * This is redundant but it may help solving the problem, especially if C is a
4977
 * complex expression.
4978
 */
4979
static void assert_factored_inequality(context_t *ctx, bvfactoring_t *f) {
9✔
4980
  term_t u1, u2;
4981
  thvar_t x, y;
4982

4983
  assert(f->code == BVFACTOR_FOUND);
4984

4985
  //  printf("Asserting factored inequality\n\n");
4986

4987
  u1 = bitvector_factoring_left_term(ctx, f);
9✔
4988
  u2 = bitvector_factoring_right_term(ctx, f);
9✔
4989
  x = internalize_to_bv(ctx, u1);
9✔
4990
  y = internalize_to_bv(ctx, u2);
9✔
4991
  ctx->bv.assert_eq_axiom(ctx->bv_solver, x,  y, false);
9✔
4992
}
9✔
4993

4994
static void assert_toplevel_bveq(context_t *ctx, composite_term_t *eq, bool tt) {
9,305✔
4995
  bveq_simp_t simp;
4996
  ivector_t *v;
4997
  int32_t *a;
4998
  term_t t, t1, t2;
4999
  thvar_t x, y;
5000
  uint32_t i, n;
5001

5002
  assert(eq->arity == 2);
5003

5004
  t1 = intern_tbl_get_root(&ctx->intern, eq->arg[0]);
9,305✔
5005
  t2 = intern_tbl_get_root(&ctx->intern, eq->arg[1]);
9,305✔
5006
  t = simplify_bitvector_eq(ctx, t1, t2);
9,305✔
5007
  if (t != NULL_TERM) {
9,305✔
5008
    // (bveq t1 t2) is equivalent to t
5009
    assert_term(ctx, t, tt);
18✔
5010
    return;
61✔
5011
  }
5012

5013
  if (tt) {
9,287✔
5014
    // try to flatten to a conjunction of terms
5015
    v = &ctx->aux_vector;
7,511✔
5016
    assert(v->size == 0);
5017
    if (bveq_flattens(ctx->terms, t1, t2, v)) {
7,511✔
5018
      /*
5019
       * (bveq t1 t2) is equivalent to (and v[0] ... v[k])
5020
       * (bveq t1 t2) is true at the toplevel so v[0] ... v[k] must all be true
5021
       */
5022

5023
      // make a copy of v
5024
      n = v->size;
19✔
5025
      a = alloc_istack_array(&ctx->istack, n);
19✔
5026
      for (i=0; i<n; i++) {
579✔
5027
        a[i] = v->data[i];
560✔
5028
      }
5029
      ivector_reset(v);
19✔
5030

5031
      // assert
5032
      for (i=0; i<n; i++) {
548✔
5033
        assert_term(ctx, a[i], true);
530✔
5034
      }
5035

5036
      free_istack_array(&ctx->istack, a);
18✔
5037
      return;
18✔
5038
    }
5039

5040
    // flattening failed
5041
    ivector_reset(v);
7,492✔
5042
  }
5043

5044
  /*
5045
   * Try more simplifications
5046
   */
5047
  try_arithmetic_bveq_simplification(ctx, &simp, t1, t2);
9,268✔
5048
  switch (simp.code) {
9,268✔
5049
  case BVEQ_CODE_TRUE:
×
5050
    if (!tt) longjmp(ctx->env, TRIVIALLY_UNSAT);
×
5051
    break;
×
5052

5053
  case BVEQ_CODE_FALSE:
×
5054
    if (tt) longjmp(ctx->env, TRIVIALLY_UNSAT);
×
5055
    break;
×
5056

5057
  case BVEQ_CODE_REDUCED:
36✔
5058
    t1 = intern_tbl_get_root(&ctx->intern, simp.left);
36✔
5059
    t2 = intern_tbl_get_root(&ctx->intern, simp.right);
36✔
5060
    break;
36✔
5061

5062
  case BVEQ_CODE_REDUCED0:
28✔
5063
    // reduced to simp.left == 0
5064
    assert_toplevel_bveq0(ctx, simp.left, tt);
28✔
5065
    return;
28✔
5066

5067
  default:
9,204✔
5068
    break;
9,204✔
5069
  }
5070

5071
  /*
5072
   * Try Factoring
5073
   */
5074
  if (!tt) {
9,240✔
5075
    bvfactoring_t *factoring;
5076
    bool eq = false;
1,752✔
5077

5078
    factoring = objstack_alloc(&ctx->ostack, sizeof(bvfactoring_t), (cleaner_t) delete_bvfactoring);
1,752✔
5079
    init_bvfactoring(factoring);
1,752✔
5080

5081
    try_bitvector_factoring(ctx, factoring, t1, t2);
1,752✔
5082
    switch (factoring->code) {
1,752✔
5083
    case BVFACTOR_EQUAL:
4✔
5084
      eq = true;
4✔
5085
      break;
4✔
5086

5087
    case BVFACTOR_FOUND:
9✔
5088
      assert_factored_inequality(ctx, factoring);
9✔
5089
      break;
9✔
5090

5091
    default:
1,739✔
5092
      break;
1,739✔
5093
    }
5094
    // delete_bvfactoring(&factoring);
5095
    objstack_pop(&ctx->ostack);
1,752✔
5096

5097
    if (eq) {
1,752✔
5098
      longjmp(ctx->env, TRIVIALLY_UNSAT);
4✔
5099
    }
5100
  }
5101

5102
  /*
5103
   * NOTE: asserting (eq t1 t2) in the egraph instead makes things worse
5104
   */
5105
  x = internalize_to_bv(ctx, t1);
9,236✔
5106
  y = internalize_to_bv(ctx, t2);
9,236✔
5107
  ctx->bv.assert_eq_axiom(ctx->bv_solver, x,  y, tt);
9,236✔
5108
}
5109

5110
static void assert_toplevel_bvge(context_t *ctx, composite_term_t *ge, bool tt) {
1,457✔
5111
  thvar_t x, y;
5112

5113
  assert(ge->arity == 2);
5114

5115
  x = internalize_to_bv(ctx, ge->arg[0]);
1,457✔
5116
  y = internalize_to_bv(ctx, ge->arg[1]);
1,457✔
5117
  ctx->bv.assert_ge_axiom(ctx->bv_solver, x,  y, tt);
1,457✔
5118
}
1,457✔
5119

5120
static void assert_toplevel_bvsge(context_t *ctx, composite_term_t *sge, bool tt) {
459✔
5121
  thvar_t x, y;
5122

5123
  assert(sge->arity == 2);
5124

5125
  x = internalize_to_bv(ctx, sge->arg[0]);
459✔
5126
  y = internalize_to_bv(ctx, sge->arg[1]);
459✔
5127
  ctx->bv.assert_sge_axiom(ctx->bv_solver, x,  y, tt);
459✔
5128
}
459✔
5129

5130

5131

5132
/*
5133
 * Top-level formula t:
5134
 * - t is a boolean term (or the negation of a boolean term)
5135
 * - t must be a root in the internalization table and must be mapped to true
5136
 */
5137
static void assert_toplevel_formula(context_t *ctx, term_t t) {
124,457✔
5138
  term_table_t *terms;
5139
  int32_t code;
5140
  literal_t l;
5141
  bool tt;
5142

5143
  assert(is_boolean_term(ctx->terms, t) &&
5144
         intern_tbl_is_root(&ctx->intern, t) &&
5145
         term_is_true(ctx, t));
5146

5147
  tt = is_pos_term(t);
124,457✔
5148
  t = unsigned_term(t);
124,457✔
5149

5150
  /*
5151
   * Now: t is a root and has positive polarity
5152
   * - tt indicates whether we assert t or (not t):
5153
   *   tt true: assert t
5154
   *   tt false: assert (not t)
5155
   */
5156
  terms = ctx->terms;
124,457✔
5157
  if (context_atom_requires_mcsat(ctx, t)) {
124,457✔
5158
    l = map_mcsat_atom_to_literal(ctx, t);
13✔
5159
    code = literal2code(l);
13✔
5160
    intern_tbl_remap_root(&ctx->intern, t, code);
13✔
5161
    assert_internalization_code(ctx, code, tt);
13✔
5162
    return;
13✔
5163
  }
5164

5165
  switch (term_kind(terms, t)) {
124,444✔
5166
  case CONSTANT_TERM:
×
5167
  case UNINTERPRETED_TERM:
5168
    // should be eliminated by flattening
5169
    code = INTERNAL_ERROR;
×
5170
    goto abort;
×
5171

5172
  case ITE_TERM:
68✔
5173
  case ITE_SPECIAL:
5174
    assert_toplevel_ite(ctx, ite_term_desc(terms, t), tt);
68✔
5175
    break;
68✔
5176

5177
  case OR_TERM:
42,416✔
5178
    assert_toplevel_or(ctx, or_term_desc(terms, t), tt);
42,416✔
5179
    break;
42,416✔
5180

5181
  case XOR_TERM:
7✔
5182
    assert_toplevel_xor(ctx, xor_term_desc(terms, t), tt);
7✔
5183
    break;
7✔
5184

5185
  case EQ_TERM:
5,118✔
5186
    assert_toplevel_eq(ctx, eq_term_desc(terms, t), tt);
5,118✔
5187
    break;
5,116✔
5188

5189
  case ARITH_IS_INT_ATOM:
×
5190
    assert_toplevel_arith_is_int(ctx, arith_is_int_arg(terms, t), tt);
×
5191
    break;
×
5192

5193
  case ARITH_EQ_ATOM:
2,264✔
5194
    assert_toplevel_arith_eq(ctx, arith_eq_arg(terms, t), tt);
2,264✔
5195
    break;
2,262✔
5196

5197
  case ARITH_GE_ATOM:
27,848✔
5198
    assert_toplevel_arith_geq(ctx, arith_ge_arg(terms, t), tt);
27,848✔
5199
    break;
27,847✔
5200

5201
  case ARITH_BINEQ_ATOM:
3,201✔
5202
    assert_toplevel_arith_bineq(ctx, arith_bineq_atom_desc(terms, t), tt);
3,201✔
5203
    break;
3,201✔
5204

5205
  case ARITH_DIVIDES_ATOM:
×
5206
    assert_toplevel_arith_divides(ctx, arith_divides_atom_desc(terms, t), tt);
×
5207
    break;
×
5208

5209
  case APP_TERM:
250✔
5210
    assert_toplevel_apply(ctx, app_term_desc(terms, t), tt);
250✔
5211
    break;
250✔
5212

5213
  case SELECT_TERM:
×
5214
    assert_toplevel_select(ctx, select_term_desc(terms, t), tt);
×
5215
    break;
×
5216

5217
  case DISTINCT_TERM:
172✔
5218
    assert_toplevel_distinct(ctx, distinct_term_desc(terms, t), tt);
172✔
5219
    break;
170✔
5220

5221
  case VARIABLE:
×
5222
    code = FREE_VARIABLE_IN_FORMULA;
×
5223
    goto abort;
×
5224

5225
  case FORALL_TERM:
×
5226
    if (context_in_strict_mode(ctx)) {
×
5227
      code = QUANTIFIERS_NOT_SUPPORTED;
×
5228
      goto abort;
×
5229
    }
5230
    break;
×
5231

5232
  case BIT_TERM:
31,924✔
5233
    assert_toplevel_bit_select(ctx, bit_term_desc(terms, t), tt);
31,924✔
5234
    break;
31,924✔
5235

5236
  case BV_EQ_ATOM:
9,266✔
5237
    assert_toplevel_bveq(ctx, bveq_atom_desc(terms, t), tt);
9,266✔
5238
    break;
9,258✔
5239

5240
  case BV_GE_ATOM:
1,455✔
5241
    assert_toplevel_bvge(ctx, bvge_atom_desc(terms, t), tt);
1,455✔
5242
    break;
1,455✔
5243

5244
  case BV_SGE_ATOM:
455✔
5245
    assert_toplevel_bvsge(ctx, bvsge_atom_desc(terms, t), tt);
455✔
5246
    break;
455✔
5247

5248
  default:
×
5249
    code = INTERNAL_ERROR;
×
5250
    goto abort;
×
5251
  }
5252

5253
  return;
124,429✔
5254

5255
 abort:
×
5256
  longjmp(ctx->env, code);
×
5257
}
5258

5259

5260

5261
/*
5262
 * Assert (t == tt) for a boolean term t:
5263
 * - if t is not internalized, record the mapping
5264
 *   (root t) --> tt in the internalization table
5265
 */
5266
static void assert_term(context_t *ctx, term_t t, bool tt) {
998✔
5267
  term_table_t *terms;
5268
  int32_t code;
5269
  literal_t l;
5270

5271
  assert(is_boolean_term(ctx->terms, t));
5272

5273
  /*
5274
   * Apply substitution + fix polarity
5275
   */
5276
  t = intern_tbl_get_root(&ctx->intern, t);
998✔
5277
  tt ^= is_neg_term(t);
998✔
5278
  t = unsigned_term(t);
998✔
5279

5280
  if (intern_tbl_root_is_mapped(&ctx->intern, t)) {
998✔
5281
    /*
5282
     * The root is already mapped:
5283
     * Either t is already internalized, or it occurs in
5284
     * one of the vectors top_eqs, top_atoms, top_formulas
5285
     * and it will be internalized/asserted later.
5286
     */
5287
    code = intern_tbl_map_of_root(&ctx->intern, t);
171✔
5288
    assert_internalization_code(ctx, code, tt);
171✔
5289

5290
  } else {
5291
    if (context_atom_requires_mcsat(ctx, t)) {
827✔
NEW
5292
      l = map_mcsat_atom_to_literal(ctx, t);
×
NEW
5293
      code = literal2code(l);
×
NEW
5294
      intern_tbl_map_root(&ctx->intern, t, code);
×
NEW
5295
      assert_internalization_code(ctx, code, tt);
×
NEW
5296
      return;
×
5297
    }
5298

5299
    // store the mapping t --> tt
5300
    intern_tbl_map_root(&ctx->intern, t, bool2code(tt));
827✔
5301

5302
    // internalize and assert
5303
    terms = ctx->terms;
827✔
5304
    switch (term_kind(terms, t)) {
827✔
5305
    case CONSTANT_TERM:
×
5306
      // should always be internalized
5307
      code = INTERNAL_ERROR;
×
5308
      goto abort;
×
5309

5310
    case UNINTERPRETED_TERM:
1✔
5311
      // nothing to do: t --> true/false in the internalization table
5312
      break;
1✔
5313

5314
    case ITE_TERM:
2✔
5315
    case ITE_SPECIAL:
5316
      assert_toplevel_ite(ctx, ite_term_desc(terms, t), tt);
2✔
5317
      break;
2✔
5318

5319
    case OR_TERM:
31✔
5320
      assert_toplevel_or(ctx, or_term_desc(terms, t), tt);
31✔
5321
      break;
31✔
5322

5323
    case XOR_TERM:
×
5324
      assert_toplevel_xor(ctx, xor_term_desc(terms, t), tt);
×
5325
      break;
×
5326

5327
    case EQ_TERM:
11✔
5328
      assert_toplevel_eq(ctx, eq_term_desc(terms, t), tt);
11✔
5329
      break;
11✔
5330

5331
    case ARITH_IS_INT_ATOM:
×
5332
      assert_toplevel_arith_is_int(ctx, arith_is_int_arg(terms, t), tt);
×
5333
      break;
×
5334

5335
    case ARITH_EQ_ATOM:
4✔
5336
      assert_toplevel_arith_eq(ctx, arith_eq_arg(terms, t), tt);
4✔
5337
      break;
4✔
5338

5339
    case ARITH_GE_ATOM:
7✔
5340
      assert_toplevel_arith_geq(ctx, arith_ge_arg(terms, t), tt);
7✔
5341
      break;
7✔
5342

5343
    case ARITH_BINEQ_ATOM:
181✔
5344
      assert_toplevel_arith_bineq(ctx, arith_bineq_atom_desc(terms, t), tt);
181✔
5345
      break;
181✔
5346

5347
    case ARITH_DIVIDES_ATOM:
×
5348
      assert_toplevel_arith_divides(ctx, arith_divides_atom_desc(terms, t), tt);
×
5349
      break;
×
5350

5351
    case APP_TERM:
2✔
5352
      assert_toplevel_apply(ctx, app_term_desc(terms, t), tt);
2✔
5353
      break;
2✔
5354

5355
    case SELECT_TERM:
×
5356
      assert_toplevel_select(ctx, select_term_desc(terms, t), tt);
×
5357
      break;
×
5358

5359
    case DISTINCT_TERM:
×
5360
      assert_toplevel_distinct(ctx, distinct_term_desc(terms, t), tt);
×
5361
      break;
×
5362

5363
    case VARIABLE:
×
5364
      code = FREE_VARIABLE_IN_FORMULA;
×
5365
      goto abort;
×
5366

5367
    case FORALL_TERM:
×
5368
      if (context_in_strict_mode(ctx)) {
×
5369
        code = QUANTIFIERS_NOT_SUPPORTED;
×
5370
        goto abort;
×
5371
      }
5372
      break;
×
5373

5374
    case BIT_TERM:
543✔
5375
      assert_toplevel_bit_select(ctx, bit_term_desc(terms, t), tt);
543✔
5376
      break;
542✔
5377

5378
    case BV_EQ_ATOM:
39✔
5379
      assert_toplevel_bveq(ctx, bveq_atom_desc(terms, t), tt);
39✔
5380
      break;
39✔
5381

5382
    case BV_GE_ATOM:
2✔
5383
      assert_toplevel_bvge(ctx, bvge_atom_desc(terms, t), tt);
2✔
5384
      break;
2✔
5385

5386
    case BV_SGE_ATOM:
4✔
5387
      assert_toplevel_bvsge(ctx, bvsge_atom_desc(terms, t), tt);
4✔
5388
      break;
4✔
5389

5390
    default:
×
5391
      code = INTERNAL_ERROR;
×
5392
      goto abort;
×
5393
    }
5394
  }
5395

5396
  return;
993✔
5397

5398
 abort:
×
5399
  longjmp(ctx->env, code);
×
5400
}
5401

5402

5403

5404

5405
/************************
5406
 *  PARAMETERS/OPTIONS  *
5407
 ***********************/
5408

5409
/*
5410
 * Map architecture id to theories word
5411
 */
5412
static const uint32_t arch2theories[NUM_ARCH] = {
5413
  0,                           //  CTX_ARCH_NOSOLVERS --> empty theory
5414

5415
  UF_MASK,                     //  CTX_ARCH_EG
5416
  ARITH_MASK,                  //  CTX_ARCH_SPLX
5417
  IDL_MASK,                    //  CTX_ARCH_IFW
5418
  RDL_MASK,                    //  CTX_ARCH_RFW
5419
  BV_MASK,                     //  CTX_ARCH_BV
5420
  UF_MASK|FUN_MASK,            //  CTX_ARCH_EGFUN
5421
  UF_MASK|ARITH_MASK,          //  CTX_ARCH_EGSPLX
5422
  UF_MASK|BV_MASK,             //  CTX_ARCH_EGBV
5423
  UF_MASK|ARITH_MASK|FUN_MASK, //  CTX_ARCH_EGFUNSPLX
5424
  UF_MASK|BV_MASK|FUN_MASK,    //  CTX_ARCH_EGFUNBV
5425
  UF_MASK|BV_MASK|ARITH_MASK,  //  CTX_ARCH_EGSPLXBV
5426
  ALLTH_MASK,                  //  CTX_ARCH_EGFUNSPLXBV
5427

5428
  IDL_MASK,                    //  CTX_ARCH_AUTO_IDL
5429
  RDL_MASK,                    //  CTX_ARCH_AUTO_RDL
5430

5431
  UF_MASK|ARITH_MASK|FUN_MASK  //  CTX_ARCH_MCSAT
5432
};
5433

5434

5435
/*
5436
 * Each architecture has a fixed set of solver components:
5437
 * - the set of components is stored as a bit vector (on 8bits)
5438
 * - this uses the following bit-masks
5439
 * For the AUTO_xxx architecture, nothing is required initially,
5440
 * so the bitmask is 0.
5441
 */
5442
#define EGRPH  0x1
5443
#define SPLX   0x2
5444
#define IFW    0x4
5445
#define RFW    0x8
5446
#define BVSLVR 0x10
5447
#define FSLVR  0x20
5448
#define MCSAT  0x40
5449

5450
static const uint8_t arch_components[NUM_ARCH] = {
5451
  0,                        //  CTX_ARCH_NOSOLVERS
5452

5453
  EGRPH,                    //  CTX_ARCH_EG
5454
  SPLX,                     //  CTX_ARCH_SPLX
5455
  IFW,                      //  CTX_ARCH_IFW
5456
  RFW,                      //  CTX_ARCH_RFW
5457
  BVSLVR,                   //  CTX_ARCH_BV
5458
  EGRPH|FSLVR,              //  CTX_ARCH_EGFUN
5459
  EGRPH|SPLX,               //  CTX_ARCH_EGSPLX
5460
  EGRPH|BVSLVR,             //  CTX_ARCH_EGBV
5461
  EGRPH|SPLX|FSLVR,         //  CTX_ARCH_EGFUNSPLX
5462
  EGRPH|BVSLVR|FSLVR,       //  CTX_ARCH_EGFUNBV
5463
  EGRPH|SPLX|BVSLVR,        //  CTX_ARCH_EGSPLXBV
5464
  EGRPH|SPLX|BVSLVR|FSLVR,  //  CTX_ARCH_EGFUNSPLXBV
5465

5466
  0,                        //  CTX_ARCH_AUTO_IDL
5467
  0,                        //  CTX_ARCH_AUTO_RDL
5468

5469
  MCSAT                     //  CTX_ARCH_MCSAT
5470
};
5471

5472

5473
/*
5474
 * Smt mode for a context mode
5475
 */
5476
static const smt_mode_t core_mode[NUM_MODES] = {
5477
  SMT_MODE_BASIC,       // one check
5478
  SMT_MODE_BASIC,       // multichecks
5479
  SMT_MODE_PUSHPOP,     // push/pop
5480
  SMT_MODE_INTERACTIVE, // interactive
5481
};
5482

5483

5484
/*
5485
 * Flags for a context mode
5486
 */
5487
static const uint32_t mode2options[NUM_MODES] = {
5488
  0,
5489
  MULTICHECKS_OPTION_MASK,
5490
  MULTICHECKS_OPTION_MASK|PUSHPOP_OPTION_MASK,
5491
  MULTICHECKS_OPTION_MASK|PUSHPOP_OPTION_MASK|CLEANINT_OPTION_MASK,
5492
};
5493

5494

5495

5496

5497

5498

5499
/*
5500
 * SIMPLEX OPTIONS
5501
 */
5502

5503
/*
5504
 * Which version of the arithmetic solver is present
5505
 */
5506
bool context_has_idl_solver(context_t *ctx) {
8✔
5507
  uint8_t solvers;
5508
  solvers = arch_components[ctx->arch];
8✔
5509
  return ctx->arith_solver != NULL && (solvers & IFW);
8✔
5510
}
5511

5512
bool context_has_rdl_solver(context_t *ctx) {
×
5513
  uint8_t solvers;
5514
  solvers = arch_components[ctx->arch];
×
5515
  return ctx->arith_solver != NULL && (solvers & RFW);
×
5516
}
5517

5518
bool context_has_simplex_solver(context_t *ctx) {
45,835✔
5519
  uint8_t solvers;
5520
  solvers = arch_components[ctx->arch];
45,835✔
5521
  return ctx->arith_solver != NULL && (solvers & SPLX);
45,835✔
5522
}
5523

5524

5525
/*
5526
 * If the simplex solver already exists, the options are propagated.
5527
 * Otherwise, they are recorded into the option flags. They will
5528
 * be set up when the simplex solver is created.
5529
 */
5530
void enable_splx_eager_lemmas(context_t *ctx) {
177✔
5531
  ctx->options |= SPLX_EGRLMAS_OPTION_MASK;
177✔
5532
  if (context_has_simplex_solver(ctx)) {
177✔
5533
    simplex_enable_eager_lemmas(ctx->arith_solver);
177✔
5534
  }
5535
}
177✔
5536

5537
void disable_splx_eager_lemmas(context_t *ctx) {
×
5538
  ctx->options &= ~SPLX_EGRLMAS_OPTION_MASK;
×
5539
  if (context_has_simplex_solver(ctx)) {
×
5540
    simplex_disable_eager_lemmas(ctx->arith_solver);
×
5541
  }
5542
}
×
5543

5544

5545
void enable_splx_periodic_icheck(context_t *ctx) {
142✔
5546
  ctx->options |= SPLX_ICHECK_OPTION_MASK;
142✔
5547
  if (context_has_simplex_solver(ctx)) {
142✔
5548
    simplex_enable_periodic_icheck(ctx->arith_solver);
131✔
5549
  }
5550
}
142✔
5551

5552
void disable_splx_periodic_icheck(context_t *ctx) {
×
5553
  ctx->options &= ~SPLX_ICHECK_OPTION_MASK;
×
5554
  if (context_has_simplex_solver(ctx)) {
×
5555
    simplex_disable_periodic_icheck(ctx->arith_solver);
×
5556
  }
5557
}
×
5558

5559
void enable_splx_eqprop(context_t *ctx) {
77✔
5560
  ctx->options |= SPLX_EQPROP_OPTION_MASK;
77✔
5561
  if (context_has_simplex_solver(ctx)) {
77✔
5562
    simplex_enable_eqprop(ctx->arith_solver);
77✔
5563
  }
5564
}
77✔
5565

5566
void disable_splx_eqprop(context_t *ctx) {
×
5567
  ctx->options &= ~SPLX_EQPROP_OPTION_MASK;
×
5568
  if (context_has_simplex_solver(ctx)) {
×
5569
    simplex_disable_eqprop(ctx->arith_solver);
×
5570
  }
5571
}
×
5572

5573

5574

5575

5576
/******************
5577
 *  EMPTY SOLVER  *
5578
 *****************/
5579

5580
/*
5581
 * We need an empty theory solver for initializing
5582
 * the core if the architecture is NOSOLVERS.
5583
 */
5584
static void donothing(void *solver) {
2,575✔
5585
}
2,575✔
5586

5587
static void null_backtrack(void *solver, uint32_t backlevel) {
×
5588
}
×
5589

5590
static bool null_propagate(void *solver) {
×
5591
  return true;
×
5592
}
5593

5594
static fcheck_code_t null_final_check(void *solver) {
×
5595
  return FCHECK_SAT;
×
5596
}
5597

5598
static th_ctrl_interface_t null_ctrl = {
5599
  donothing,        // start_internalization
5600
  donothing,        // start_search
5601
  null_propagate,   // propagate
5602
  null_final_check, // final check
5603
  donothing,        // increase_decision_level
5604
  null_backtrack,   // backtrack
5605
  donothing,        // push
5606
  donothing,        // pop
5607
  donothing,        // reset
5608
  donothing,        // clear
5609
};
5610

5611

5612
// for the smt interface, nothing should be called since there are no atoms
5613
static th_smt_interface_t null_smt = {
5614
  NULL, NULL, NULL, NULL, NULL,
5615
};
5616

5617

5618

5619

5620
/****************************
5621
 *  ARCHITECTURE & SOLVERS  *
5622
 ***************************/
5623

5624
/*
5625
 * Check whether a given architecture includes a specific solver
5626
 */
5627
bool context_arch_has_egraph(context_arch_t arch) {
417✔
5628
  return arch_components[arch] & EGRPH;
417✔
5629
}
5630

5631
bool context_arch_has_bv(context_arch_t arch) {
×
5632
  return arch_components[arch] & BVSLVR;
×
5633
}
5634

5635
bool context_arch_has_fun(context_arch_t arch) {
×
5636
  return arch_components[arch] & FSLVR;
×
5637
}
5638

5639
bool context_arch_has_arith(context_arch_t arch) {
×
5640
  return arch_components[arch] & (SPLX|IFW|RFW);
×
5641
}
5642

5643
bool context_arch_has_mcsat(context_arch_t arch) {
×
5644
  return arch_components[arch] & MCSAT;
×
5645
}
5646

5647
bool context_arch_has_simplex(context_arch_t arch) {
×
5648
  return arch_components[arch] & SPLX;
×
5649
}
5650

5651
bool context_arch_has_ifw(context_arch_t arch) {
×
5652
  return arch_components[arch] & IFW;
×
5653
}
5654

5655
bool context_arch_has_rfw(context_arch_t arch) {
×
5656
  return arch_components[arch] & RFW;
×
5657
}
5658

5659

5660
/****************************
5661
 *  SOLVER INITIALIZATION   *
5662
 ***************************/
5663

5664
/*
5665
 * Create and initialize the egraph
5666
 * - the core must be created first
5667
 */
5668
static void create_egraph(context_t *ctx) {
4,785✔
5669
  egraph_t *egraph;
5670

5671
  assert(ctx->egraph == NULL);
5672

5673
  egraph = (egraph_t *) safe_malloc(sizeof(egraph_t));
4,785✔
5674
  init_egraph(egraph, ctx->types);
4,785✔
5675
  ctx->egraph = egraph;
4,785✔
5676
}
4,785✔
5677

5678

5679
/*
5680
 * Create and initialize the mcsat solver
5681
 */
5682
static void create_mcsat(context_t *ctx) {
679✔
5683
  assert(ctx->mcsat == NULL);
5684
  ctx->mcsat = mcsat_new(ctx);
679✔
5685
}
679✔
5686

5687

5688

5689
/*
5690
 * Create and initialize the idl solver and attach it to the core
5691
 * - there must be no other solvers and no egraph
5692
 * - if automatic is true, attach the solver to the core, otherwise
5693
 *   initialize the core
5694
 * - copy the solver's internalization interface into arith
5695
 */
5696
static void create_idl_solver(context_t *ctx, bool automatic) {
26✔
5697
  idl_solver_t *solver;
5698
  smt_mode_t cmode;
5699

5700
  assert(ctx->egraph == NULL && ctx->arith_solver == NULL && ctx->bv_solver == NULL &&
5701
         ctx->fun_solver == NULL && ctx->core != NULL);
5702

5703
  cmode = core_mode[ctx->mode];
26✔
5704
  solver = (idl_solver_t *) safe_malloc(sizeof(idl_solver_t));
26✔
5705
  init_idl_solver(solver, ctx->core, &ctx->gate_manager);
26✔
5706
  if (automatic) {
26✔
5707
    smt_core_reset_thsolver(ctx->core, solver, idl_ctrl_interface(solver),
26✔
5708
                            idl_smt_interface(solver));
5709
  } else {
5710
    init_smt_core(ctx->core, CTX_DEFAULT_CORE_SIZE, solver, idl_ctrl_interface(solver),
×
5711
                  idl_smt_interface(solver), cmode);
5712
  }
5713
  idl_solver_init_jmpbuf(solver, &ctx->env);
26✔
5714
  ctx->arith_solver = solver;
26✔
5715
  ctx->arith = *idl_arith_interface(solver);
26✔
5716
}
26✔
5717

5718

5719
/*
5720
 * Create and initialize the rdl solver and attach it to the core.
5721
 * - there must be no other solvers and no egraph
5722
 * - if automatic is true, attach rdl to the core, otherwise
5723
 *   initialize the core
5724
 * - copy the solver's internalization interface in ctx->arith
5725
 */
5726
static void create_rdl_solver(context_t *ctx, bool automatic) {
8✔
5727
  rdl_solver_t *solver;
5728
  smt_mode_t cmode;
5729

5730
  assert(ctx->egraph == NULL && ctx->arith_solver == NULL && ctx->bv_solver == NULL &&
5731
         ctx->fun_solver == NULL && ctx->core != NULL);
5732

5733
  cmode = core_mode[ctx->mode];
8✔
5734
  solver = (rdl_solver_t *) safe_malloc(sizeof(rdl_solver_t));
8✔
5735
  init_rdl_solver(solver, ctx->core, &ctx->gate_manager);
8✔
5736
  if (automatic) {
8✔
5737
    smt_core_reset_thsolver(ctx->core, solver, rdl_ctrl_interface(solver),
8✔
5738
                            rdl_smt_interface(solver));
5739
  } else {
5740
    init_smt_core(ctx->core, CTX_DEFAULT_CORE_SIZE, solver, rdl_ctrl_interface(solver),
×
5741
                  rdl_smt_interface(solver), cmode);
5742
  }
5743
  rdl_solver_init_jmpbuf(solver, &ctx->env);
8✔
5744
  ctx->arith_solver = solver;
8✔
5745
  ctx->arith = *rdl_arith_interface(solver);
8✔
5746
}
8✔
5747

5748

5749
/*
5750
 * Create an initialize the simplex solver and attach it to the core
5751
 * or to the egraph if the egraph exists.
5752
 * - if automatic is true, this is part of auto_idl or auto_rdl. So the
5753
 *   core is already initialized.
5754
 */
5755
static void create_simplex_solver(context_t *ctx, bool automatic) {
14,900✔
5756
  simplex_solver_t *solver;
5757
  smt_mode_t cmode;
5758

5759
  assert(ctx->arith_solver == NULL && ctx->core != NULL);
5760

5761
  cmode = core_mode[ctx->mode];
14,900✔
5762
  solver = (simplex_solver_t *) safe_malloc(sizeof(simplex_solver_t));
14,900✔
5763
  init_simplex_solver(solver, ctx->core, &ctx->gate_manager, ctx->egraph);
14,900✔
5764

5765
  // set simplex options
5766
  if (splx_eager_lemmas_enabled(ctx)) {
14,900✔
5767
    simplex_enable_eager_lemmas(solver);
×
5768
  }
5769
  if (splx_periodic_icheck_enabled(ctx)) {
14,900✔
5770
    simplex_enable_periodic_icheck(solver);
×
5771
  }
5772
  if (splx_eqprop_enabled(ctx)) {
14,900✔
5773
    simplex_enable_eqprop(solver);
×
5774
  }
5775

5776
  // row saving must be enabled unless we're in ONECHECK mode
5777
  if (ctx->mode != CTX_MODE_ONECHECK) {
14,900✔
5778
    simplex_enable_row_saving(solver);
14,754✔
5779
  }
5780

5781
  if (ctx->egraph != NULL) {
14,900✔
5782
    // attach the simplex solver as a satellite solver to the egraph
5783
    egraph_attach_arithsolver(ctx->egraph, solver, simplex_ctrl_interface(solver),
4,328✔
5784
                              simplex_smt_interface(solver), simplex_egraph_interface(solver),
5785
                              simplex_arith_egraph_interface(solver));
5786
  } else if (!automatic) {
10,572✔
5787
    // attach simplex to the core and initialize the core
5788
    init_smt_core(ctx->core, CTX_DEFAULT_CORE_SIZE, solver, simplex_ctrl_interface(solver),
10,571✔
5789
                  simplex_smt_interface(solver), cmode);
5790
  } else {
5791
    // the core is already initialized: attach simplex
5792
    smt_core_reset_thsolver(ctx->core, solver, simplex_ctrl_interface(solver),
1✔
5793
                            simplex_smt_interface(solver));
5794
  }
5795

5796
  simplex_solver_init_jmpbuf(solver, &ctx->env);
14,900✔
5797
  ctx->arith_solver = solver;
14,900✔
5798
  ctx->arith = *simplex_arith_interface(solver);
14,900✔
5799
}
14,900✔
5800

5801

5802
/*
5803
 * Create IDL/SIMPLEX solver based on ctx->dl_profile
5804
 */
5805
static void create_auto_idl_solver(context_t *ctx) {
27✔
5806
  dl_data_t *profile;
5807
  int32_t bound;
5808
  double atom_density;
5809

5810
  assert(ctx->dl_profile != NULL);
5811
  profile = ctx->dl_profile;
27✔
5812

5813
  if (q_is_smallint(&profile->path_bound)) {
27✔
5814
    bound = q_get_smallint(&profile->path_bound);
26✔
5815
  } else {
5816
    bound = INT32_MAX;
1✔
5817
  }
5818

5819
  if (bound >= 1073741824) {
27✔
5820
    // simplex required because of arithmetic overflow
5821
    create_simplex_solver(ctx, true);
1✔
5822
    ctx->arch = CTX_ARCH_SPLX;
1✔
5823
  } else if (profile->num_vars >= 1000) {
26✔
5824
    // too many variables for FW
5825
    create_simplex_solver(ctx, true);
×
5826
    ctx->arch = CTX_ARCH_SPLX;
×
5827
  } else if (profile->num_vars <= 200 || profile->num_eqs == 0) {
26✔
5828
    // use FW for now, until we've tested SIMPLEX more
5829
    // 0 equalities usually means a scheduling problem
5830
    // --flatten works better on IDL/FW
5831
    create_idl_solver(ctx, true);
26✔
5832
    ctx->arch = CTX_ARCH_IFW;
26✔
5833
    enable_diseq_and_or_flattening(ctx);
26✔
5834

5835
  } else {
5836

5837
    // problem density
5838
    if (profile->num_vars > 0) {
×
5839
      atom_density = ((double) profile->num_atoms)/profile->num_vars;
×
5840
    } else {
5841
      atom_density = 0;
×
5842
    }
5843

5844
    if (atom_density >= 10.0) {
×
5845
      // high density: use FW
5846
      create_idl_solver(ctx, true);
×
5847
      ctx->arch = CTX_ARCH_IFW;
×
5848
      enable_diseq_and_or_flattening(ctx);
×
5849
    } else {
5850
      create_simplex_solver(ctx, true);
×
5851
      ctx->arch = CTX_ARCH_SPLX;
×
5852
    }
5853
  }
5854
}
27✔
5855

5856

5857
/*
5858
 * Create RDL/SIMPLEX solver based on ctx->dl_profile
5859
 */
5860
static void create_auto_rdl_solver(context_t *ctx) {
8✔
5861
  dl_data_t *profile;
5862
  double atom_density;
5863

5864
  assert(ctx->dl_profile != NULL);
5865
  profile = ctx->dl_profile;
8✔
5866

5867
  if (profile->num_vars >= 1000) {
8✔
5868
    create_simplex_solver(ctx, true);
×
5869
    ctx->arch = CTX_ARCH_SPLX;
×
5870
  } else if (profile->num_vars <= 200 || profile->num_eqs == 0) {
8✔
5871
    create_rdl_solver(ctx, true);
8✔
5872
    ctx->arch = CTX_ARCH_RFW;
8✔
5873
  } else {
5874
    // problem density
5875
    if (profile->num_vars > 0) {
×
5876
      atom_density = ((double) profile->num_atoms)/profile->num_vars;
×
5877
    } else {
5878
      atom_density = 0;
×
5879
    }
5880

5881
    if (atom_density >= 7.0) {
×
5882
      // high density: use FW
5883
      create_rdl_solver(ctx, true);
×
5884
      ctx->arch = CTX_ARCH_RFW;
×
5885
    } else {
5886
      // low-density: use SIMPLEX
5887
      create_simplex_solver(ctx, true);
×
5888
      ctx->arch = CTX_ARCH_SPLX;
×
5889
    }
5890
  }
5891
}
8✔
5892

5893

5894

5895
/*
5896
 * Create the bitvector solver
5897
 * - attach it to the egraph if there's an egraph
5898
 * - attach it to the core and initialize the core otherwise
5899
 */
5900
static void create_bv_solver(context_t *ctx) {
10,563✔
5901
  bv_solver_t *solver;
5902
  smt_mode_t cmode;
5903

5904
  assert(ctx->bv_solver == NULL && ctx->core != NULL);
5905

5906
  cmode = core_mode[ctx->mode];
10,563✔
5907
  solver = (bv_solver_t *) safe_malloc(sizeof(bv_solver_t));
10,563✔
5908
  init_bv_solver(solver, ctx->core, ctx->egraph);
10,563✔
5909

5910
  if (ctx->egraph != NULL) {
10,563✔
5911
    // attach as a satellite to the egraph
5912
    egraph_attach_bvsolver(ctx->egraph, solver, bv_solver_ctrl_interface(solver),
4,411✔
5913
                           bv_solver_smt_interface(solver), bv_solver_egraph_interface(solver),
5914
                           bv_solver_bv_egraph_interface(solver));
5915
  } else {
5916
    // attach to the core and initialize the core
5917
    init_smt_core(ctx->core, CTX_DEFAULT_CORE_SIZE, solver, bv_solver_ctrl_interface(solver),
6,152✔
5918
                  bv_solver_smt_interface(solver), cmode);
5919
  }
5920

5921
  // EXPERIMENT
5922
  //  smt_core_make_etable(ctx->core);
5923
  // END
5924

5925
  bv_solver_init_jmpbuf(solver, &ctx->env);
10,563✔
5926
  ctx->bv_solver = solver;
10,563✔
5927
  ctx->bv = *bv_solver_bv_interface(solver);
10,563✔
5928
}
10,563✔
5929

5930

5931
/*
5932
 * Create the array/function theory solver and attach it to the egraph
5933
 */
5934
static void create_fun_solver(context_t *ctx) {
4,418✔
5935
  fun_solver_t *solver;
5936

5937
  assert(ctx->egraph != NULL && ctx->fun_solver == NULL);
5938

5939
  solver = (fun_solver_t *) safe_malloc(sizeof(fun_solver_t));
4,418✔
5940
  init_fun_solver(solver, ctx->core, &ctx->gate_manager, ctx->egraph, ctx->types);
4,418✔
5941
  egraph_attach_funsolver(ctx->egraph, solver, fun_solver_ctrl_interface(solver),
4,418✔
5942
                          fun_solver_egraph_interface(solver),
5943
                          fun_solver_fun_egraph_interface(solver));
5944

5945
  ctx->fun_solver = solver;
4,418✔
5946
}
4,418✔
5947

5948

5949
/*
5950
 * Allocate and initialize solvers based on architecture and mode
5951
 * - core and gate manager must exist at this point
5952
 * - if the architecture is either AUTO_IDL or AUTO_RDL, no theory solver
5953
 *   is allocated yet, and the core is initialized for Boolean only
5954
 * - otherwise, all components are ready and initialized, including the core.
5955
 */
5956
static void init_solvers(context_t *ctx) {
22,223✔
5957
  uint8_t solvers;
5958
  smt_core_t *core;
5959
  smt_mode_t cmode;
5960
  egraph_t *egraph;
5961

5962
  solvers = arch_components[ctx->arch];
22,223✔
5963

5964
  ctx->egraph = NULL;
22,223✔
5965
  ctx->arith_solver = NULL;
22,223✔
5966
  ctx->bv_solver = NULL;
22,223✔
5967
  ctx->fun_solver = NULL;
22,223✔
5968
  ctx->quant_solver = NULL;
22,223✔
5969
  ctx->mcsat_supplement_active = false;
22,223✔
5970

5971
  // Create egraph first, then satellite solvers
5972
  if (solvers & EGRPH) {
22,223✔
5973
    create_egraph(ctx);
4,785✔
5974
  }
5975

5976
  // Create mcsat
5977
  if (solvers & MCSAT) {
22,223✔
5978
    create_mcsat(ctx);
679✔
5979
  }
5980

5981
  // Arithmetic solver
5982
  if (solvers & SPLX) {
22,223✔
5983
    create_simplex_solver(ctx, false);
14,899✔
5984
  } else if (solvers & IFW) {
7,324✔
5985
    create_idl_solver(ctx, false);
×
5986
  } else if (solvers & RFW) {
7,324✔
5987
    create_rdl_solver(ctx, false);
×
5988
  }
5989

5990
  // Bitvector solver
5991
  if (solvers & BVSLVR) {
22,223✔
5992
    create_bv_solver(ctx);
10,563✔
5993
  }
5994

5995
  // Array solver
5996
  if (solvers & FSLVR) {
22,223✔
5997
    create_fun_solver(ctx);
4,418✔
5998
  }
5999

6000
  /*
6001
   * At this point all solvers are ready and initialized, except the
6002
   * egraph and core if the egraph is present or the core if there are
6003
   * no solvers, or if arch is AUTO_IDL or AUTO_RDL.
6004
   */
6005
  cmode = core_mode[ctx->mode];   // initialization mode for the core
22,223✔
6006
  egraph = ctx->egraph;
22,223✔
6007
  core = ctx->core;
22,223✔
6008
  if (egraph != NULL) {
22,223✔
6009
    init_smt_core(core, CTX_DEFAULT_CORE_SIZE, egraph, egraph_ctrl_interface(egraph),
4,785✔
6010
                  egraph_smt_interface(egraph), cmode);
6011
    egraph_attach_core(egraph, core);
4,785✔
6012

6013
  } else if (solvers == 0) {
17,438✔
6014
    /*
6015
     * Boolean solver only. If arch if AUTO_IDL or AUTO_RDL, the
6016
     * theory solver will be changed later by create_auto_idl_solver
6017
     * or create_auto_rdl_solver.
6018
     */
6019
    assert(ctx->arith_solver == NULL && ctx->bv_solver == NULL && ctx->fun_solver == NULL);
6020
    init_smt_core(core, CTX_DEFAULT_CORE_SIZE, NULL, &null_ctrl, &null_smt, cmode);
36✔
6021
  } else if (solvers == MCSAT) {
17,402✔
6022
    /*
6023
     * MCsat solver only, we create the core, but never use it.
6024
     */
6025
    assert(ctx->egraph == NULL && ctx->arith_solver == NULL &&
6026
           ctx->bv_solver == NULL && ctx->fun_solver == NULL);
6027
    init_smt_core(core, CTX_DEFAULT_CORE_SIZE, NULL, &null_ctrl, &null_smt, cmode);
679✔
6028
  }
6029

6030
  /*
6031
   * Optimization: if the arch is NOSOLVERS or BV then we set bool_only in the core
6032
   */
6033
  if (ctx->arch == CTX_ARCH_NOSOLVERS || ctx->arch == CTX_ARCH_BV) {
22,223✔
6034
    smt_core_set_bool_only(core);
6,152✔
6035
  }
6036
}
22,223✔
6037

6038

6039

6040

6041
/*
6042
 * Delete the arithmetic solver
6043
 */
6044
static void delete_arith_solver(context_t *ctx) {
14,933✔
6045
  uint8_t solvers;
6046

6047
  assert(ctx->arith_solver != NULL);
6048

6049
  solvers = arch_components[ctx->arch];
14,933✔
6050
  if (solvers & IFW) {
14,933✔
6051
    delete_idl_solver(ctx->arith_solver);
26✔
6052
  } else if (solvers & RFW) {
14,907✔
6053
    delete_rdl_solver(ctx->arith_solver);
8✔
6054
  } else if (solvers & SPLX) {
14,899✔
6055
    delete_simplex_solver(ctx->arith_solver);
14,899✔
6056
  }
6057
  safe_free(ctx->arith_solver);
14,933✔
6058
  ctx->arith_solver = NULL;
14,933✔
6059
}
14,933✔
6060

6061

6062

6063

6064
/*****************************
6065
 *  CONTEXT INITIALIZATION   *
6066
 ****************************/
6067

6068
/*
6069
 * Check mode and architecture
6070
 */
6071
#ifndef NDEBUG
6072
static inline bool valid_mode(context_mode_t mode) {
6073
  return CTX_MODE_ONECHECK <= mode && mode <= CTX_MODE_INTERACTIVE;
6074
}
6075

6076
static inline bool valid_arch(context_arch_t arch) {
6077
  return CTX_ARCH_NOSOLVERS <= arch && arch <= CTX_ARCH_MCSAT;
6078
}
6079
#endif
6080

6081

6082
/*
6083
 * Initialize ctx for the given mode and architecture
6084
 * - terms = term table for that context
6085
 * - qflag = true means quantifiers allowed
6086
 * - qflag = false means no quantifiers
6087
 */
6088
void init_context(context_t *ctx, term_table_t *terms, smt_logic_t logic,
22,223✔
6089
                  context_mode_t mode, context_arch_t arch, bool qflag) {
6090
  assert(valid_mode(mode) && valid_arch(arch));
6091

6092
  /*
6093
   * Set architecture and options
6094
   */
6095
  ctx->mode = mode;
22,223✔
6096
  ctx->arch = arch;
22,223✔
6097
  ctx->logic = logic;
22,223✔
6098
  ctx->theories = arch2theories[arch];
22,223✔
6099
  ctx->options = mode2options[mode];
22,223✔
6100
  if (qflag) {
22,223✔
6101
    // quantifiers require egraph
6102
    assert((ctx->theories & UF_MASK) != 0);
6103
    ctx->theories |= QUANT_MASK;
×
6104
  }
6105

6106
  ctx->base_level = 0;
22,223✔
6107

6108
  /*
6109
   * The core is always needed: allocate it here. It's not initialized yet.
6110
   * The other solver are optionals.
6111
   *
6112
   * TODO: we could skip this when we use MCSAT (since then the core is
6113
   * not needed).
6114
   */
6115
  ctx->core = (smt_core_t *) safe_malloc(sizeof(smt_core_t));
22,223✔
6116
  ctx->egraph = NULL;
22,223✔
6117
  ctx->mcsat = NULL;
22,223✔
6118
  ctx->arith_solver = NULL;
22,223✔
6119
  ctx->bv_solver = NULL;
22,223✔
6120
  ctx->fun_solver = NULL;
22,223✔
6121
  ctx->quant_solver = NULL;
22,223✔
6122

6123
  /*
6124
   * Global tables + gate manager
6125
   */
6126
  ctx->types = terms->types;
22,223✔
6127
  ctx->terms = terms;
22,223✔
6128
  init_gate_manager(&ctx->gate_manager, ctx->core);
22,223✔
6129

6130
  /*
6131
   * Simplification/internalization support
6132
   */
6133
  init_intern_tbl(&ctx->intern, 0, terms);
22,223✔
6134
  init_ivector(&ctx->top_eqs, CTX_DEFAULT_VECTOR_SIZE);
22,223✔
6135
  init_ivector(&ctx->top_atoms, CTX_DEFAULT_VECTOR_SIZE);
22,223✔
6136
  init_ivector(&ctx->top_formulas, CTX_DEFAULT_VECTOR_SIZE);
22,223✔
6137
  init_ivector(&ctx->top_interns, CTX_DEFAULT_VECTOR_SIZE);
22,223✔
6138

6139
  /*
6140
   * Force the internalization mapping for true and false
6141
   * - true  term --> true_occ
6142
   * - false term --> false_occ
6143
   * This mapping holds even if there's no egraph.
6144
   */
6145
  intern_tbl_map_root(&ctx->intern, true_term, bool2code(true));
22,223✔
6146

6147
  /*
6148
   * Auxiliary internalization buffers
6149
   */
6150
  init_ivector(&ctx->subst_eqs, CTX_DEFAULT_VECTOR_SIZE);
22,223✔
6151
  init_ivector(&ctx->aux_eqs, CTX_DEFAULT_VECTOR_SIZE);
22,223✔
6152
  init_ivector(&ctx->aux_atoms, CTX_DEFAULT_VECTOR_SIZE);
22,223✔
6153
  init_ivector(&ctx->aux_vector, CTX_DEFAULT_VECTOR_SIZE);
22,223✔
6154
  init_int_queue(&ctx->queue, 0);
22,223✔
6155
  init_istack(&ctx->istack);
22,223✔
6156
  init_objstack(&ctx->ostack);
22,223✔
6157
  init_sharing_map(&ctx->sharing, &ctx->intern);
22,223✔
6158
  init_objstore(&ctx->cstore, sizeof(conditional_t), 32);
22,223✔
6159
  init_assumption_stack(&ctx->assumptions);
22,223✔
6160

6161
  ctx->subst = NULL;
22,223✔
6162
  ctx->marks = NULL;
22,223✔
6163
  ctx->cache = NULL;
22,223✔
6164
  ctx->small_cache = NULL;
22,223✔
6165
  ctx->edge_map = NULL;
22,223✔
6166
  ctx->eq_cache = NULL;
22,223✔
6167
  ctx->divmod_table = NULL;
22,223✔
6168
  ctx->explorer = NULL;
22,223✔
6169
  ctx->unsat_core_cache = NULL;
22,223✔
6170

6171
  ctx->dl_profile = NULL;
22,223✔
6172
  ctx->arith_buffer = NULL;
22,223✔
6173
  ctx->poly_buffer = NULL;
22,223✔
6174
  ctx->aux_poly = NULL;
22,223✔
6175
  ctx->aux_poly_size = 0;
22,223✔
6176

6177
  ctx->bvpoly_buffer = NULL;
22,223✔
6178

6179
  q_init(&ctx->aux);
22,223✔
6180
  init_bvconstant(&ctx->bv_buffer);
22,223✔
6181

6182
  ctx->trace = NULL;
22,223✔
6183

6184
  // mcsat options default
6185
  init_mcsat_options(&ctx->mcsat_options);
22,223✔
6186
  init_ivector(&ctx->mcsat_var_order, CTX_DEFAULT_VECTOR_SIZE);
22,223✔
6187
  init_ivector(&ctx->mcsat_initial_var_order, CTX_DEFAULT_VECTOR_SIZE);
22,223✔
6188
  /*
6189
   * Allocate and initialize the solvers and core
6190
   * NOTE: no theory solver yet if arch is AUTO_IDL or AUTO_RDL
6191
   */
6192
  init_solvers(ctx);
22,223✔
6193

6194
  ctx->en_quant = false;
22,223✔
6195
}
22,223✔
6196

6197

6198

6199

6200
/*
6201
 * Delete ctx
6202
 */
6203
void delete_context(context_t *ctx) {
22,222✔
6204
  if (ctx->core != NULL) {
22,222✔
6205
    delete_smt_core(ctx->core);
22,222✔
6206
    safe_free(ctx->core);
22,222✔
6207
    ctx->core = NULL;
22,222✔
6208
  }
6209

6210
  if (ctx->mcsat != NULL) {
22,222✔
6211
    mcsat_destruct(ctx->mcsat);
679✔
6212
    safe_free(ctx->mcsat);
679✔
6213
    ctx->mcsat = NULL;
679✔
6214
  }
6215

6216
  if (ctx->mcsat_supplement_active) {
22,222✔
6217
    context_disable_mcsat_supplement(ctx);
5✔
6218
  }
6219

6220
  if (ctx->egraph != NULL) {
22,222✔
6221
    delete_egraph(ctx->egraph);
4,784✔
6222
    safe_free(ctx->egraph);
4,784✔
6223
    ctx->egraph = NULL;
4,784✔
6224
  }
6225

6226
  if (ctx->arith_solver != NULL) {
22,222✔
6227
    delete_arith_solver(ctx);
14,933✔
6228
  }
6229

6230
  if (ctx->fun_solver != NULL) {
22,222✔
6231
    delete_fun_solver(ctx->fun_solver);
4,417✔
6232
    safe_free(ctx->fun_solver);
4,417✔
6233
    ctx->fun_solver = NULL;
4,417✔
6234
  }
6235

6236
  if (ctx->quant_solver != NULL) {
22,222✔
6237
    delete_quant_solver(ctx->quant_solver);
52✔
6238
    safe_free(ctx->quant_solver);
52✔
6239
    ctx->quant_solver = NULL;
52✔
6240
  }
6241

6242
  if (ctx->bv_solver != NULL) {
22,222✔
6243
    delete_bv_solver(ctx->bv_solver);
10,562✔
6244
    safe_free(ctx->bv_solver);
10,562✔
6245
    ctx->bv_solver = NULL;
10,562✔
6246
  }
6247

6248
  delete_gate_manager(&ctx->gate_manager);
22,222✔
6249
  /* delete_mcsat_options(&ctx->mcsat_options); // if used then the same memory is freed twice */
6250
  delete_ivector(&ctx->mcsat_var_order);
22,222✔
6251
  delete_ivector(&ctx->mcsat_initial_var_order);
22,222✔
6252

6253
  delete_intern_tbl(&ctx->intern);
22,222✔
6254
  delete_ivector(&ctx->top_eqs);
22,222✔
6255
  delete_ivector(&ctx->top_atoms);
22,222✔
6256
  delete_ivector(&ctx->top_formulas);
22,222✔
6257
  delete_ivector(&ctx->top_interns);
22,222✔
6258

6259
  delete_ivector(&ctx->subst_eqs);
22,222✔
6260
  delete_ivector(&ctx->aux_eqs);
22,222✔
6261
  delete_ivector(&ctx->aux_atoms);
22,222✔
6262
  delete_ivector(&ctx->aux_vector);
22,222✔
6263
  delete_int_queue(&ctx->queue);
22,222✔
6264
  delete_istack(&ctx->istack);
22,222✔
6265
  delete_objstack(&ctx->ostack);
22,222✔
6266
  delete_sharing_map(&ctx->sharing);
22,222✔
6267
  delete_objstore(&ctx->cstore);
22,222✔
6268
  delete_assumption_stack(&ctx->assumptions);
22,222✔
6269

6270
  context_free_subst(ctx);
22,222✔
6271
  context_free_marks(ctx);
22,222✔
6272
  context_free_cache(ctx);
22,222✔
6273
  context_free_small_cache(ctx);
22,222✔
6274
  context_free_eq_cache(ctx);
22,222✔
6275
  context_free_divmod_table(ctx);
22,222✔
6276
  context_free_explorer(ctx);
22,222✔
6277

6278
  context_free_dl_profile(ctx);
22,222✔
6279
  context_free_edge_map(ctx);
22,222✔
6280
  context_free_arith_buffer(ctx);
22,222✔
6281
  context_free_poly_buffer(ctx);
22,222✔
6282
  context_free_aux_poly(ctx);
22,222✔
6283

6284
  context_free_bvpoly_buffer(ctx);
22,222✔
6285
  context_invalidate_unsat_core_cache(ctx);
22,222✔
6286

6287
  q_clear(&ctx->aux);
22,222✔
6288
  delete_bvconstant(&ctx->bv_buffer);
22,222✔
6289
}
22,222✔
6290

6291
void context_invalidate_unsat_core_cache(context_t *ctx) {
87,661✔
6292
  if (ctx->unsat_core_cache != NULL) {
87,661✔
6293
    delete_ivector(ctx->unsat_core_cache);
2,739✔
6294
    safe_free(ctx->unsat_core_cache);
2,739✔
6295
    ctx->unsat_core_cache = NULL;
2,739✔
6296
  }
6297
}
87,661✔
6298

6299

6300

6301
/*
6302
 * Reset: remove all assertions and clear all internalization tables
6303
 */
6304
void reset_context(context_t *ctx) {
13✔
6305
  ctx->base_level = 0;
13✔
6306
  context_invalidate_unsat_core_cache(ctx);
13✔
6307

6308
  reset_smt_core(ctx->core); // this propagates reset to all solvers
13✔
6309

6310
  if (ctx->mcsat_supplement_active) {
13✔
6311
    context_disable_mcsat_supplement(ctx);
5✔
6312
  }
6313

6314
  if (ctx->mcsat != NULL) {
13✔
6315
    mcsat_reset(ctx->mcsat);
7✔
6316
  }
6317

6318
  reset_gate_manager(&ctx->gate_manager);
13✔
6319

6320
  ivector_reset(&ctx->mcsat_var_order);
13✔
6321
  ivector_reset(&ctx->mcsat_initial_var_order);
13✔
6322

6323
  reset_intern_tbl(&ctx->intern);
13✔
6324
  ivector_reset(&ctx->top_eqs);
13✔
6325
  ivector_reset(&ctx->top_atoms);
13✔
6326
  ivector_reset(&ctx->top_formulas);
13✔
6327
  ivector_reset(&ctx->top_interns);
13✔
6328

6329
  // Force the internalization mapping for true and false
6330
  intern_tbl_map_root(&ctx->intern, true_term, bool2code(true));
13✔
6331

6332
  ivector_reset(&ctx->subst_eqs);
13✔
6333
  ivector_reset(&ctx->aux_eqs);
13✔
6334
  ivector_reset(&ctx->aux_atoms);
13✔
6335
  ivector_reset(&ctx->aux_vector);
13✔
6336
  int_queue_reset(&ctx->queue);
13✔
6337
  reset_istack(&ctx->istack);
13✔
6338
  reset_objstack(&ctx->ostack);
13✔
6339
  reset_sharing_map(&ctx->sharing);
13✔
6340
  reset_objstore(&ctx->cstore);
13✔
6341
  reset_assumption_stack(&ctx->assumptions);
13✔
6342

6343
  context_free_subst(ctx);
13✔
6344
  context_free_marks(ctx);
13✔
6345
  context_reset_small_cache(ctx);
13✔
6346
  context_reset_eq_cache(ctx);
13✔
6347
  context_reset_divmod_table(ctx);
13✔
6348
  context_reset_explorer(ctx);
13✔
6349

6350
  context_free_arith_buffer(ctx);
13✔
6351
  context_reset_poly_buffer(ctx);
13✔
6352
  context_free_aux_poly(ctx);
13✔
6353
  context_free_dl_profile(ctx);
13✔
6354

6355
  context_free_bvpoly_buffer(ctx);
13✔
6356

6357
  q_clear(&ctx->aux);
13✔
6358
}
13✔
6359

6360

6361
/*
6362
 * Add tracer to ctx and ctx->core
6363
 */
6364
void context_set_trace(context_t *ctx, tracer_t *trace) {
285✔
6365
  assert(ctx->trace == NULL);
6366
  ctx->trace = trace;
285✔
6367
  smt_core_set_trace(ctx->core, trace);
285✔
6368
  if (ctx->mcsat != NULL) {
285✔
6369
    mcsat_set_tracer(ctx->mcsat, trace);
284✔
6370
  }
6371
  if (ctx->mcsat_supplement_active) {
285✔
NEW
6372
    mcsat_satellite_t *sat = context_mcsat_satellite(ctx);
×
NEW
6373
    if (sat != NULL) {
×
NEW
6374
      mcsat_satellite_set_trace(sat, trace);
×
6375
    }
6376
  }
6377
}
285✔
6378

6379

6380
/*
6381
 * Push and pop
6382
 */
6383
void context_push(context_t *ctx) {
18,491✔
6384
  assert(context_supports_pushpop(ctx));
6385
  context_invalidate_unsat_core_cache(ctx);
18,491✔
6386
  smt_push(ctx->core);  // propagates to all solvers
18,491✔
6387
  if (ctx->mcsat != NULL) {
18,491✔
6388
    mcsat_push(ctx->mcsat);
1,326✔
6389
  }
6390
  intern_tbl_push(&ctx->intern);
18,491✔
6391
  assumption_stack_push(&ctx->assumptions);
18,491✔
6392
  context_eq_cache_push(ctx);
18,491✔
6393
  context_divmod_table_push(ctx);
18,491✔
6394

6395
  ctx->base_level ++;
18,491✔
6396
}
18,491✔
6397

6398
void context_pop(context_t *ctx) {
1,475✔
6399
  assert(context_supports_pushpop(ctx) && ctx->base_level > 0);
6400
  context_invalidate_unsat_core_cache(ctx);
1,475✔
6401
  smt_pop(ctx->core);   // propagates to all solvers
1,475✔
6402
  if (ctx->mcsat != NULL) {
1,475✔
6403
    mcsat_pop(ctx->mcsat);
1,242✔
6404
  }
6405
  intern_tbl_pop(&ctx->intern);
1,475✔
6406
  assumption_stack_pop(&ctx->assumptions);
1,475✔
6407
  context_eq_cache_pop(ctx);
1,475✔
6408
  context_divmod_table_pop(ctx);
1,475✔
6409

6410
  ctx->base_level --;
1,475✔
6411
}
1,475✔
6412

6413

6414

6415

6416

6417
/****************************
6418
 *   ASSERTIONS AND CHECK   *
6419
 ***************************/
6420

6421
/*
6422
 * Build the sharing data
6423
 * - processes all the assertions in vectors top_eqs, top_atoms, top_formulas
6424
 * - this function should be called after building the substitutions
6425
 */
6426
static void context_build_sharing_data(context_t *ctx) {
71,838✔
6427
  sharing_map_t *map;
6428

6429
  map = &ctx->sharing;
71,838✔
6430
  reset_sharing_map(map);
71,838✔
6431
  sharing_map_add_terms(map, ctx->top_eqs.data, ctx->top_eqs.size);
71,838✔
6432
  sharing_map_add_terms(map, ctx->top_atoms.data, ctx->top_atoms.size);
71,838✔
6433
  sharing_map_add_terms(map, ctx->top_formulas.data, ctx->top_formulas.size);
71,838✔
6434
}
71,838✔
6435

6436

6437
#if 0
6438
/*
6439
 * PROVISIONAL: SHOW ASSERTIONS
6440
 */
6441
static void context_show_assertions(const context_t *ctx, uint32_t n, const term_t *a) {
6442
  pp_area_t area;
6443
  yices_pp_t printer;
6444
  uint32_t i;
6445

6446
  area.width = 80;
6447
  area.height = UINT32_MAX;
6448
  area.offset = 0;
6449
  area.stretch = false;
6450
  area.truncate = false;
6451
  init_yices_pp(&printer, stdout, &area, PP_VMODE, 0);
6452

6453
  for (i=0; i<n; i++) {
6454
    pp_term_full(&printer, ctx->terms, a[i]);
6455
    flush_yices_pp(&printer);
6456
  }
6457
  delete_yices_pp(&printer, true);
6458
}
6459
#endif
6460

6461
/*
6462
 * Flatten and internalize assertions a[0 ... n-1]
6463
 * - all elements a[i] must be valid boolean term in ctx->terms
6464
 * - return code:
6465
 *   TRIVIALLY_UNSAT if there's an easy contradiction
6466
 *   CTX_NO_ERROR if the assertions were processed without error
6467
 *   a negative error code otherwise.
6468
 */
6469
static int32_t context_process_assertions(context_t *ctx, uint32_t n, const term_t *a) {
74,474✔
6470
  ivector_t *v;
6471
  uint32_t i;
6472
  int code;
6473

6474
  ivector_reset(&ctx->top_eqs);
74,474✔
6475
  ivector_reset(&ctx->top_atoms);
74,474✔
6476
  ivector_reset(&ctx->top_formulas);
74,474✔
6477
  ivector_reset(&ctx->top_interns);
74,474✔
6478
  ivector_reset(&ctx->subst_eqs);
74,474✔
6479
  ivector_reset(&ctx->aux_eqs);
74,474✔
6480
  ivector_reset(&ctx->aux_atoms);
74,474✔
6481

6482
  if (ctx->arch != CTX_ARCH_MCSAT && context_has_egraph(ctx)) {
74,474✔
6483
    if (context_assertions_need_mcsat_supplement(ctx, n, a)) {
25,661✔
6484
      code = context_enable_mcsat_supplement(ctx);
14✔
6485
      if (code < 0) {
14✔
NEW
6486
        return code;
×
6487
      }
6488
    }
6489
  }
6490

6491
  if (ctx->mcsat_supplement_active) {
74,474✔
6492
    mcsat_satellite_t *sat = context_mcsat_satellite(ctx);
18✔
6493
    if (sat != NULL) {
18✔
6494
      code = mcsat_satellite_assert_formulas(sat, n, a);
18✔
6495
      if (code < 0) {
18✔
NEW
6496
        return code;
×
6497
      }
6498
    }
6499
  }
6500

6501
  code = setjmp(ctx->env);
74,474✔
6502
  if (code == 0) {
74,843✔
6503

6504
    // If using MCSAT, just check and done
6505
    if (ctx->mcsat != NULL) {
74,474✔
6506
      // TBD: quant support
6507
      assert(!context_quant_enabled(ctx));
6508
      code = mcsat_assert_formulas(ctx->mcsat, n, a);
2,282✔
6509
      goto done;
2,282✔
6510
    }
6511

6512
#if 0
6513
    printf("\n=== Context: process assertions ===\n");
6514
    context_show_assertions(ctx, n, a);
6515
    printf("===\n\n");
6516
#endif
6517

6518
    // flatten
6519
    for (i=0; i<n; i++) {
195,439✔
6520
      flatten_assertion(ctx, a[i]);
123,600✔
6521
    }
6522

6523
    trace_printf(ctx->trace, 6, "(done flattening)\n");
71,839✔
6524

6525
    /*
6526
     * At this point, the assertions are stored into the vectors
6527
     * top_eqs, top_atoms, top_formulas, and top_interns
6528
     * - more top-level equalities may be in subst_eqs
6529
     * - ctx->intern stores the internalized terms and the variable
6530
     *   substitutions.
6531
     */
6532

6533
    switch (ctx->arch) {
71,839✔
6534
    // TBD: make sure following preprocessings work with quant enabled
6535
    case CTX_ARCH_EG:
3,329✔
6536
      /*
6537
       * UF problem: we must process subst_eqs last since the
6538
       * preprocessing may add new equalities in aux_eqs that may end
6539
       * up in subst_eqs after the call to process_aux_eqs.
6540
       */
6541
      if (context_breaksym_enabled(ctx)) {
3,329✔
6542
        break_uf_symmetries(ctx);
30✔
6543
      }
6544
      if (context_eq_abstraction_enabled(ctx)) {
3,329✔
6545
        analyze_uf(ctx);
64✔
6546
      }
6547
      if (ctx->aux_eqs.size > 0) {
3,329✔
6548
        process_aux_eqs(ctx);
6✔
6549
      }
6550
      if (ctx->subst_eqs.size > 0) {
3,328✔
6551
        context_process_candidate_subst(ctx);
10✔
6552
      }
6553
      break;
3,328✔
6554

6555
    case CTX_ARCH_AUTO_IDL:
27✔
6556
      /*
6557
       * For difference logic, we must process the subst_eqs first
6558
       * (otherwise analyze_diff_logic may give wrong results).
6559
       */
6560
      if (ctx->subst_eqs.size > 0) {
27✔
6561
        context_process_candidate_subst(ctx);
×
6562
      }
6563
      analyze_diff_logic(ctx, true);
27✔
6564
      create_auto_idl_solver(ctx);
27✔
6565
      break;
27✔
6566

6567
    case CTX_ARCH_AUTO_RDL:
8✔
6568
      /*
6569
       * Difference logic, we must process the subst_eqs first
6570
       */
6571
      trace_printf(ctx->trace, 6, "(auto-idl solver)\n");
8✔
6572
      if (ctx->subst_eqs.size > 0) {
8✔
6573
        context_process_candidate_subst(ctx);
×
6574
      }
6575
      analyze_diff_logic(ctx, false);
8✔
6576
      create_auto_rdl_solver(ctx);
8✔
6577
      break;
8✔
6578

6579
    case CTX_ARCH_SPLX:
30,674✔
6580
      /*
6581
       * Simplex, like EG, may add aux_atoms so we must process
6582
       * subst_eqs last here.
6583
       */
6584
      trace_printf(ctx->trace, 6, "(Simplex solver)\n");
30,674✔
6585
      // more optional processing
6586
      if (context_cond_def_preprocessing_enabled(ctx)) {
30,674✔
6587
        process_conditional_definitions(ctx);
52✔
6588
        if (ctx->aux_eqs.size > 0) {
52✔
6589
          process_aux_eqs(ctx);
2✔
6590
        }
6591
        if (ctx->aux_atoms.size > 0) {
52✔
6592
          process_aux_atoms(ctx);
3✔
6593
        }
6594
      }
6595
      if (ctx->subst_eqs.size > 0) {
30,674✔
6596
        context_process_candidate_subst(ctx);
16✔
6597
      }
6598
      break;
30,674✔
6599

6600
    default:
37,801✔
6601
      /*
6602
       * Process the candidate variable substitutions if any
6603
       */
6604
      if (ctx->subst_eqs.size > 0) {
37,801✔
6605
        context_process_candidate_subst(ctx);
2,778✔
6606
      }
6607
      break;
37,801✔
6608
    }
6609

6610
    /*
6611
     * Sharing
6612
     */
6613
    context_build_sharing_data(ctx);
71,838✔
6614

6615
    /*
6616
     * Notify the core + solver(s)
6617
     */
6618
    if (!context_quant_enabled(ctx)) {
71,838✔
6619
        // TBD: make sure this is correct
6620
      internalization_start(ctx->core);
69,305✔
6621
    }
6622

6623
    /*
6624
     * Assert top_eqs, top_atoms, top_formulas, top_interns
6625
     */
6626
    code = CTX_NO_ERROR;
71,838✔
6627

6628
    // first: all terms that are already internalized
6629
    v = &ctx->top_interns;
71,838✔
6630
    n = v->size;
71,838✔
6631
    if (n > 0) {
71,838✔
6632
      trace_printf(ctx->trace, 6, "(asserting  %"PRIu32" existing terms)\n", n);
285✔
6633
      i = 0;
285✔
6634
      do {
6635
        assert_toplevel_intern(ctx, v->data[i]);
740✔
6636
        i ++;
740✔
6637
      } while (i < n);
740✔
6638

6639
      // one round of propagation
6640
      if (!context_quant_enabled(ctx) && ! base_propagate(ctx->core)) {
285✔
6641
        code = TRIVIALLY_UNSAT;
11✔
6642
        goto done;
11✔
6643
      }
6644
    }
6645

6646
    // second: all top-level equalities
6647
    v = &ctx->top_eqs;
71,827✔
6648
    n = v->size;
71,827✔
6649
    if (n > 0) {
71,827✔
6650
      trace_printf(ctx->trace, 6, "(asserting  %"PRIu32" top-level equalities)\n", n);
6,748✔
6651
      i = 0;
6,748✔
6652
      do {
6653
        assert_toplevel_formula(ctx, v->data[i]);
10,809✔
6654
        i ++;
10,805✔
6655
      } while (i < n);
10,805✔
6656

6657
      // one round of propagation
6658
      if (!context_quant_enabled(ctx) && ! base_propagate(ctx->core)) {
6,744✔
6659
        code = TRIVIALLY_UNSAT;
68✔
6660
        goto done;
68✔
6661
      }
6662
    }
6663

6664
    // third: all top-level atoms (other than equalities)
6665
    v = &ctx->top_atoms;
71,755✔
6666
    n = v->size;
71,755✔
6667
    if (n > 0) {
71,755✔
6668
      trace_printf(ctx->trace, 6, "(asserting  %"PRIu32" top-level atoms)\n", n);
15,454✔
6669
      i = 0;
15,454✔
6670
      do {
6671
        assert_toplevel_formula(ctx, v->data[i]);
68,429✔
6672
        i ++;
68,418✔
6673
      } while (i < n);
68,418✔
6674

6675
      // one round of propagation
6676
      if (!context_quant_enabled(ctx) && ! base_propagate(ctx->core)) {
15,443✔
6677
        code = TRIVIALLY_UNSAT;
74✔
6678
        goto done;
74✔
6679
      }
6680
    }
6681

6682
    // last: all non-atomic, formulas
6683
    v =  &ctx->top_formulas;
71,670✔
6684
    n = v->size;
71,670✔
6685
    if (n > 0) {
71,670✔
6686
      trace_printf(ctx->trace, 6, "(asserting  %"PRIu32" top-level formulas)\n", n);
21,973✔
6687
      i = 0;
21,973✔
6688
      do {
6689
        assert_toplevel_formula(ctx, v->data[i]);
45,219✔
6690
        i ++;
45,219✔
6691
      } while (i < n);
45,219✔
6692

6693
      // one round of propagation
6694
      if (!context_quant_enabled(ctx) && ! base_propagate(ctx->core)) {
21,973✔
6695
        code = TRIVIALLY_UNSAT;
63✔
6696
        goto done;
63✔
6697
      }
6698
    }
6699

6700
  } else {
6701
    /*
6702
     * Exception: return from longjmp(ctx->env, code);
6703
     */
6704
    ivector_reset(&ctx->aux_vector);
369✔
6705
    reset_istack(&ctx->istack);
369✔
6706
    reset_objstack(&ctx->ostack);
369✔
6707
    int_queue_reset(&ctx->queue);
369✔
6708
    context_free_subst(ctx);
369✔
6709
    context_free_marks(ctx);
369✔
6710
  }
6711

6712
 done:
74,474✔
6713
  return code;
74,474✔
6714
}
6715

6716
/*
6717
 * Assert all formulas f[0] ... f[n-1]
6718
 * The context status must be IDLE.
6719
 *
6720
 * Return code:
6721
 * - TRIVIALLY_UNSAT means that an inconsistency is detected
6722
 *   (in that case the context status is set to UNSAT)
6723
 * - CTX_NO_ERROR means no internalization error and status not
6724
 *   determined
6725
 * - otherwise, the code is negative to report an error.
6726
 */
6727
int32_t _o_assert_formulas(context_t *ctx, uint32_t n, const term_t *f) {
71,940✔
6728
  int32_t code;
6729

6730
  assert(ctx->arch == CTX_ARCH_AUTO_IDL ||
6731
         ctx->arch == CTX_ARCH_AUTO_RDL ||
6732
         smt_status(ctx->core) == YICES_STATUS_IDLE);
6733
  assert(!context_quant_enabled(ctx));
6734

6735
  code = context_process_assertions(ctx, n, f);
71,940✔
6736
  if (code == TRIVIALLY_UNSAT) {
71,940✔
6737
    if (ctx->arch == CTX_ARCH_AUTO_IDL || ctx->arch == CTX_ARCH_AUTO_RDL) {
577✔
6738
      // cleanup: reset arch/config to 'no theory'
6739
      assert(ctx->arith_solver == NULL && ctx->bv_solver == NULL && ctx->fun_solver == NULL &&
6740
             ctx->mode == CTX_MODE_ONECHECK);
6741
      ctx->arch = CTX_ARCH_NOSOLVERS;
1✔
6742
      ctx->theories = 0;
1✔
6743
      ctx->options = 0;
1✔
6744
    }
6745

6746
    if( smt_status(ctx->core) != YICES_STATUS_UNSAT) {
577✔
6747
      // force UNSAT in the core
6748
      add_empty_clause(ctx->core);
361✔
6749
      ctx->core->status = YICES_STATUS_UNSAT;
361✔
6750
    }
6751
  }
6752

6753
  return code;
71,940✔
6754
}
6755

6756
/*
6757
 * Assert all formulas f[0] ... f[n-1] during quantifier instantiation
6758
 * The context status must be SEARCHING.
6759
 *
6760
 * Return code:
6761
 * - TRIVIALLY_UNSAT means that an inconsistency is detected
6762
 *   (in that case the context status is set to UNSAT)
6763
 * - CTX_NO_ERROR means no internalization error and status not
6764
 *   determined
6765
 * - otherwise, the code is negative to report an error.
6766
 */
6767
int32_t quant_assert_formulas(context_t *ctx, uint32_t n, const term_t *f) {
2,534✔
6768
  int32_t code;
6769

6770
  assert(context_quant_enabled(ctx));
6771
  assert(smt_status(ctx->core) == YICES_STATUS_SEARCHING);
6772

6773
  code = context_process_assertions(ctx, n, f);
2,534✔
6774
  if (code == TRIVIALLY_UNSAT) {
2,534✔
6775
    if (ctx->arch == CTX_ARCH_AUTO_IDL || ctx->arch == CTX_ARCH_AUTO_RDL) {
1✔
6776
      // cleanup: reset arch/config to 'no theory'
6777
      assert(ctx->arith_solver == NULL && ctx->bv_solver == NULL && ctx->fun_solver == NULL &&
6778
      ctx->mode == CTX_MODE_ONECHECK);
6779
      ctx->arch = CTX_ARCH_NOSOLVERS;
×
6780
      ctx->theories = 0;
×
6781
      ctx->options = 0;
×
6782
    }
6783

6784
    if( smt_status(ctx->core) != YICES_STATUS_UNSAT) {
1✔
6785
      // force UNSAT in the core
6786
      add_empty_clause(ctx->core);
1✔
6787
      ctx->core->status = YICES_STATUS_UNSAT;
1✔
6788
    }
6789
  }
6790

6791
  return code;
2,534✔
6792
}
6793

6794
int32_t assert_formulas(context_t *ctx, uint32_t n, const term_t *f) {
18,455✔
6795
  MT_PROTECT(int32_t, __yices_globals.lock, _o_assert_formulas(ctx, n, f));
18,455✔
6796
}
6797

6798

6799

6800

6801
/*
6802
 * Assert a boolean formula f.
6803
 *
6804
 * The context status must be IDLE.
6805
 *
6806
 * Return code:
6807
 * - TRIVIALLY_UNSAT means that an inconsistency is detected
6808
 *   (in that case the context status is set to UNSAT)
6809
 * - CTX_NO_ERROR means no internalization error and status not
6810
 *   determined
6811
 * - otherwise, the code is negative. The assertion could
6812
 *   not be processed.
6813
 */
6814
int32_t _o_assert_formula(context_t *ctx, term_t f) {
53,485✔
6815
  return _o_assert_formulas(ctx, 1, &f);
53,485✔
6816
}
6817

6818
int32_t assert_formula(context_t *ctx, term_t f) {
49,214✔
6819
  MT_PROTECT(int32_t, __yices_globals.lock, _o_assert_formula(ctx, f));
49,214✔
6820
}
6821

6822

6823
/*
6824
 * Convert boolean term t to a literal l in context ctx
6825
 * - t must be a boolean term
6826
 * - return a negative code if there's an error
6827
 * - return a literal (l >= 0) otherwise.
6828
 */
6829
int32_t context_internalize(context_t *ctx, term_t t) {
91,767✔
6830
  int code;
6831
  literal_t l;
6832

6833
  ivector_reset(&ctx->top_eqs);
91,767✔
6834
  ivector_reset(&ctx->top_atoms);
91,767✔
6835
  ivector_reset(&ctx->top_formulas);
91,767✔
6836
  ivector_reset(&ctx->top_interns);
91,767✔
6837
  ivector_reset(&ctx->subst_eqs);
91,767✔
6838
  ivector_reset(&ctx->aux_eqs);
91,767✔
6839

6840
  if (ctx->arch != CTX_ARCH_MCSAT && context_has_egraph(ctx) && !ctx->mcsat_supplement_active) {
91,767✔
6841
    if (context_assertions_need_mcsat_supplement(ctx, 1, &t)) {
107✔
NEW
6842
      code = context_enable_mcsat_supplement(ctx);
×
NEW
6843
      if (code < 0) {
×
NEW
6844
        return code;
×
6845
      }
6846
    }
6847
  }
6848

6849
  code = setjmp(ctx->env);
91,767✔
6850
  if (code == 0) {
91,767✔
6851
    // we must call internalization start first
6852
    if (!context_quant_enabled(ctx)) {
91,767✔
6853
      // TBD: make sure this is correct
6854
      internalization_start(ctx->core);
91,663✔
6855
    }
6856
    l = internalize_to_literal(ctx, t);
91,767✔
6857
  } else {
6858
    assert(code < 0);
6859
    /*
6860
     * Clean up
6861
     */
6862
    ivector_reset(&ctx->aux_vector);
×
6863
    reset_istack(&ctx->istack);
×
6864
    int_queue_reset(&ctx->queue);
×
6865
    context_free_subst(ctx);
×
6866
    context_free_marks(ctx);
×
6867
    l = code;
×
6868
  }
6869

6870
  return l;
91,767✔
6871
}
6872

6873

6874
/*
6875
 * Build an assumption for Boolean term t:
6876
 * - this converts t to a literal l in context ctx
6877
 *   then create an indicator variable x in the core
6878
 *   and add the clause (x => l) in the core.
6879
 * - return a negative code if t can't be internalized
6880
 * - return the literal x otherwise (where x>=0).
6881
 */
6882
int32_t context_add_assumption(context_t *ctx, term_t t) {
91,663✔
6883
  int32_t l, x;
6884

6885
  // check if we already have an assumption literal for t
6886
  x = assumption_literal_for_term(&ctx->assumptions, t);
91,663✔
6887
  if (x < 0) {
91,663✔
6888
    l = context_internalize(ctx, t);
91,663✔
6889
    if (l < 0) return l; // error code
91,663✔
6890

6891
    x = pos_lit(create_boolean_variable(ctx->core));
91,663✔
6892
    add_binary_clause(ctx->core, not(x), l); // clause (x implies l)
91,663✔
6893

6894
    assumption_stack_add(&ctx->assumptions, t, x);
91,663✔
6895
  }
6896

6897
  return x;
91,663✔
6898
}
6899

6900

6901

6902
/*
6903
 * PROVISIONAL: FOR TESTING/DEBUGGING
6904
 */
6905

6906
/*
6907
 * Preprocess formula f or array of formulas f[0 ... n-1]
6908
 * - this does flattening + build substitutions
6909
 * - return code: as in assert_formulas
6910
 * - the result is stored in the internal vectors
6911
 *     ctx->top_interns
6912
 *     ctx->top_eqs
6913
 *     ctx->top_atoms
6914
 *     ctx->top_formulas
6915
 *   + ctx->intern stores substitutions
6916
 */
6917
int32_t context_process_formulas(context_t *ctx, uint32_t n, term_t *f) {
×
6918
  uint32_t i;
6919
  int code;
6920

6921
  ivector_reset(&ctx->top_eqs);
×
6922
  ivector_reset(&ctx->top_atoms);
×
6923
  ivector_reset(&ctx->top_formulas);
×
6924
  ivector_reset(&ctx->top_interns);
×
6925
  ivector_reset(&ctx->subst_eqs);
×
6926
  ivector_reset(&ctx->aux_eqs);
×
6927
  ivector_reset(&ctx->aux_atoms);
×
6928

NEW
6929
  if (ctx->arch != CTX_ARCH_MCSAT && context_has_egraph(ctx)) {
×
NEW
6930
    if (context_assertions_need_mcsat_supplement(ctx, n, f)) {
×
NEW
6931
      code = context_enable_mcsat_supplement(ctx);
×
NEW
6932
      if (code < 0) {
×
NEW
6933
        return code;
×
6934
      }
6935
    }
6936
  }
6937

6938
  code = setjmp(ctx->env);
×
6939
  if (code == 0) {
×
6940
    // flatten
6941
    for (i=0; i<n; i++) {
×
6942
      flatten_assertion(ctx, f[i]);
×
6943
    }
6944

6945
    /*
6946
     * At this point, the assertions are stored into the vectors
6947
     * top_eqs, top_atoms, top_formulas, and top_interns
6948
     * - more top-level equalities may be in subst_eqs
6949
     * - ctx->intern stores the internalized terms and the variable
6950
     *   substitutions.
6951
     */
6952

6953
    switch (ctx->arch) {
×
6954
    case CTX_ARCH_EG:
×
6955
      /*
6956
       * UF problem: we must process subst_eqs last since the
6957
       * preprocessing may add new equalities in aux_eqs that may end
6958
       * up in subst_eqs after the call to process_aux_eqs.
6959
       */
6960
      if (context_breaksym_enabled(ctx)) {
×
6961
        break_uf_symmetries(ctx);
×
6962
      }
6963
      if (context_eq_abstraction_enabled(ctx)) {
×
6964
        analyze_uf(ctx);
×
6965
      }
6966
      if (ctx->aux_eqs.size > 0) {
×
6967
        process_aux_eqs(ctx);
×
6968
      }
6969
      if (ctx->subst_eqs.size > 0) {
×
6970
        context_process_candidate_subst(ctx);
×
6971
      }
6972
      break;
×
6973

6974
    case CTX_ARCH_AUTO_IDL:
×
6975
      /*
6976
       * For difference logic, we must process the subst_eqs first
6977
       * (otherwise analyze_diff_logic may give wrong results).
6978
       */
6979
      if (ctx->subst_eqs.size > 0) {
×
6980
        context_process_candidate_subst(ctx);
×
6981
      }
6982
      analyze_diff_logic(ctx, true);
×
6983
      create_auto_idl_solver(ctx);
×
6984
      break;
×
6985

6986
    case CTX_ARCH_AUTO_RDL:
×
6987
      /*
6988
       * Difference logic, we must process the subst_eqs first
6989
       */
6990
      if (ctx->subst_eqs.size > 0) {
×
6991
        context_process_candidate_subst(ctx);
×
6992
      }
6993
      analyze_diff_logic(ctx, false);
×
6994
      create_auto_rdl_solver(ctx);
×
6995
      break;
×
6996

6997
    case CTX_ARCH_SPLX:
×
6998
      /*
6999
       * Simplex, like EG, may add aux_atoms so we must process
7000
       * subst_eqs last here.
7001
       */
7002
      // more optional processing
7003
      if (context_cond_def_preprocessing_enabled(ctx)) {
×
7004
        process_conditional_definitions(ctx);
×
7005
        if (ctx->aux_eqs.size > 0) {
×
7006
          process_aux_eqs(ctx);
×
7007
        }
7008
        if (ctx->aux_atoms.size > 0) {
×
7009
          process_aux_atoms(ctx);
×
7010
        }
7011
      }
7012
      if (ctx->subst_eqs.size > 0) {
×
7013
        context_process_candidate_subst(ctx);
×
7014
      }
7015
      break;
×
7016

7017
    default:
×
7018
      /*
7019
       * Process the candidate variable substitutions if any
7020
       */
7021
      if (ctx->subst_eqs.size > 0) {
×
7022
        context_process_candidate_subst(ctx);
×
7023
      }
7024
      break;
×
7025
    }
7026

7027
    /*
7028
     * Sharing
7029
     */
7030
    context_build_sharing_data(ctx);
×
7031

7032
    code = CTX_NO_ERROR;
×
7033

7034
  } else {
7035
    /*
7036
     * Exception: return from longjmp(ctx->env, code);
7037
     */
7038
    ivector_reset(&ctx->aux_vector);
×
7039
    reset_istack(&ctx->istack);
×
7040
    int_queue_reset(&ctx->queue);
×
7041
    context_free_subst(ctx);
×
7042
    context_free_marks(ctx);
×
7043
  }
7044

7045
  return code;
×
7046
}
7047

7048
int32_t context_process_formula(context_t *ctx, term_t f) {
×
7049
  return context_process_formulas(ctx, 1, &f);
×
7050
}
7051

7052

7053

7054
/*
7055
 * The search function 'check_context' is defined in context_solver.c
7056
 */
7057

7058

7059
/*
7060
 * Interrupt the search.
7061
 */
7062
void context_stop_search(context_t *ctx) {
3✔
7063
  if (ctx->mcsat == NULL) {
3✔
7064
    stop_search(ctx->core);
3✔
7065
    if (context_has_simplex_solver(ctx)) {
3✔
7066
      simplex_stop_search(ctx->arith_solver);
3✔
7067
    }
7068
  } else {
7069
    mcsat_stop_search(ctx->mcsat);
×
7070
  }
7071
}
3✔
7072

7073

7074

7075
/*
7076
 * Cleanup: restore ctx to a good state after check_context
7077
 * is interrupted.
7078
 */
7079
void context_cleanup(context_t *ctx) {
1✔
7080
  // restore the state to IDLE, propagate to all solvers (via pop)
7081
  assert(context_supports_cleaninterrupt(ctx));
7082
  context_invalidate_unsat_core_cache(ctx);
1✔
7083
  if (ctx->mcsat == NULL) {
1✔
7084
    smt_cleanup(ctx->core);
1✔
7085
  } else {
7086
    mcsat_clear(ctx->mcsat);
×
7087
  }
7088
}
1✔
7089

7090

7091

7092
/*
7093
 * Clear: prepare for more assertions and checks
7094
 * - free the boolean assignment
7095
 * - reset the status to IDLE
7096
 */
7097
void context_clear(context_t *ctx) {
42,163✔
7098
  assert(context_supports_multichecks(ctx));
7099
  context_invalidate_unsat_core_cache(ctx);
42,163✔
7100
  if (ctx->mcsat == NULL) {
42,163✔
7101
    smt_clear(ctx->core);
41,812✔
7102
  } else {
7103
    mcsat_clear(ctx->mcsat);
351✔
7104
  }
7105
}
42,163✔
7106

7107

7108

7109
/*
7110
 * Clear_unsat: prepare for pop if the status is UNSAT
7111
 * - remove assumptions if any
7112
 *
7113
 * - if clean interrupt is enabled, then there may be a mismatch between
7114
 *   the context's base_level and the core base_level.
7115
 * - it's possible to have ctx->core.base_level = ctx->base_level + 1
7116
 * - this happens because start_search in smt_core does an internal smt_push
7117
 *   to allow the core to be restored to a clean state if search is interrupted.
7118
 * - if search is not interrupted and the search returns UNSAT, then we're
7119
 *   in a state with core base level = context base level + 1.
7120
 */
7121
void context_clear_unsat(context_t *ctx) {
613✔
7122
  context_invalidate_unsat_core_cache(ctx);
613✔
7123
  if (ctx->mcsat == NULL) {
613✔
7124
    smt_clear_unsat(ctx->core);
383✔
7125
    assert(smt_base_level(ctx->core) == ctx->base_level);
7126
  } else {
7127
    mcsat_clear(ctx->mcsat);
230✔
7128
  }
7129
}
613✔
7130

7131

7132

7133
/*
7134
 * Add the blocking clause to ctx
7135
 * - ctx->status must be either SAT or UNKNOWN
7136
 * - this collects all decision literals in the current truth assignment
7137
 *   (say l_1, ..., l_k) then clears the current assignment and adds the
7138
 *  clause ((not l_1) \/ ... \/ (not l_k)).
7139
 *
7140
 * Return code:
7141
 * - TRIVIALLY_UNSAT: means that the blocking clause is empty (i.e., k = 0)
7142
 *   (in that case, the context status is set to UNSAT)
7143
 * - CTX_NO_ERROR: means that the blocking clause is not empty (i.e., k > 0)
7144
 *   (In this case, the context status is set to IDLE)
7145
 */
7146
int32_t assert_blocking_clause(context_t *ctx) {
24,855✔
7147
  ivector_t *v;
7148
  uint32_t i, n;
7149
  int32_t code;
7150

7151
  assert(smt_status(ctx->core) == YICES_STATUS_SAT ||
7152
         smt_status(ctx->core) == YICES_STATUS_UNKNOWN);
7153

7154
  // get decision literals and build the blocking clause
7155
  v = &ctx->aux_vector;
24,855✔
7156
  assert(v->size == 0);
7157
  collect_decision_literals(ctx->core, v);
24,855✔
7158
  n = v->size;
24,855✔
7159
  for (i=0; i<n; i++) {
36,264✔
7160
    v->data[i] = not(v->data[i]);
11,409✔
7161
  }
7162

7163
  // prepare for the new assertion + notify solvers of a new assertion
7164
  context_clear(ctx);
24,855✔
7165
  internalization_start(ctx->core);
24,855✔
7166

7167
  // add the blocking clause
7168
  add_clause(ctx->core, n, v->data);
24,855✔
7169
  ivector_reset(v);
24,855✔
7170

7171
  // force UNSAT if n = 0
7172
  code = CTX_NO_ERROR;
24,855✔
7173
  if (n == 0) {
24,855✔
7174
    code = TRIVIALLY_UNSAT;
16,817✔
7175
    ctx->core->status = YICES_STATUS_UNSAT;
16,817✔
7176
  }
7177

7178
  assert(n == 0 || smt_status(ctx->core) == YICES_STATUS_IDLE);
7179

7180
  return code;
24,855✔
7181
}
7182

7183

7184

7185

7186
/********************************
7187
 *  GARBAGE COLLECTION SUPPORT  *
7188
 *******************************/
7189

7190
/*
7191
 * Marker for all terms present in the eq_map
7192
 * - aux = the relevant term table.
7193
 * - each record p stores <k0, k1, val> where k0 and k1 are both
7194
 *   terms in aux and val is a literal in the core
7195
 */
7196
static void ctx_mark_eq(void *aux, const pmap2_rec_t *p) {
×
7197
  term_table_set_gc_mark(aux, index_of(p->k0));
×
7198
  term_table_set_gc_mark(aux, index_of(p->k1));
×
7199
}
×
7200

7201

7202
/*
7203
 * Go through all data structures in ctx and mark all terms and types
7204
 * that they use.
7205
 */
7206
void context_gc_mark(context_t *ctx) {
7✔
7207
  if (ctx->egraph != NULL) {
7✔
7208
    egraph_gc_mark(ctx->egraph);
1✔
7209
  }
7210
  if (ctx->fun_solver != NULL) {
7✔
7211
    fun_solver_gc_mark(ctx->fun_solver);
1✔
7212
  }
7213

7214
  intern_tbl_gc_mark(&ctx->intern);
7✔
7215

7216
  // empty all the term vectors to be safe
7217
  ivector_reset(&ctx->top_eqs);
7✔
7218
  ivector_reset(&ctx->top_atoms);
7✔
7219
  ivector_reset(&ctx->top_formulas);
7✔
7220
  ivector_reset(&ctx->top_interns);
7✔
7221
  ivector_reset(&ctx->subst_eqs);
7✔
7222
  ivector_reset(&ctx->aux_eqs);
7✔
7223

7224
  if (ctx->eq_cache != NULL) {
7✔
7225
    pmap2_iterate(ctx->eq_cache, ctx->terms, ctx_mark_eq);
×
7226
  }
7227

7228
  if (ctx->unsat_core_cache != NULL) {
7✔
7229
    uint32_t i, n;
7230
    n = ctx->unsat_core_cache->size;
×
7231
    for (i=0; i<n; i++) {
×
7232
      term_table_set_gc_mark(ctx->terms, index_of(ctx->unsat_core_cache->data[i]));
×
7233
    }
7234
  }
7235

7236
  if (ctx->mcsat != NULL) {
7✔
7237
    mcsat_gc_mark(ctx->mcsat);
6✔
7238
  }
7239
  if (ctx->mcsat_supplement_active) {
7✔
NEW
7240
    mcsat_satellite_t *sat = context_mcsat_satellite(ctx);
×
NEW
7241
    if (sat != NULL) {
×
NEW
7242
      mcsat_satellite_gc_mark(sat);
×
7243
    }
7244
  }
7245
}
7✔
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