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

SRI-CSL / yices2 / 12367760548

17 Dec 2024 06:41AM UTC coverage: 65.285% (-0.6%) from 65.92%
12367760548

push

github

web-flow
Fixes GitHub coverage issue (#544)

* Update action.yml

* Update action.yml

81020 of 124102 relevant lines covered (65.29%)

1509382.01 hits per line

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

94.86
/src/terms/terms.h
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
 * Internal term representation
21
 * ----------------------------
22
 *
23
 * This module provides low-level functions for term construction
24
 * and management of a global term table.
25
 *
26
 * Changes:
27
 *
28
 * Feb. 20, 2007.  Added explicit variables for dealing with
29
 * quantifiers, rather than DeBruijn indices. DeBruijn indices
30
 * do not mix well with hash consing because different occurrences
31
 * of the same variable may have different indices.
32
 * Removed free_vars field since we can't represent free variables
33
 * via a bit vector anymore.
34
 *
35
 * March 07, 2007. Removed bvconstant as a separate representation.
36
 * They can be stored as bdd arrays. That's simpler and does not cause
37
 * much overhead.
38
 *
39
 * March 24, 2007. Removed mandatory names for uninterpreted constants.
40
 * Replaced by a function that creates a new uninterpreted constant (with
41
 * no name) of a given type. Also removed built-in names for the boolean
42
 * constants.
43
 *
44
 * April 20, 2007. Put back a separate representation for bvconstants.
45
 *
46
 * June 6, 2007. Added distinct as a built-in term kind.
47
 *
48
 * June 12, 2007. Added the bv_apply constructor to support bit-vector operations
49
 * that are overloaded but that we want to treat as uninterpreted terms (mostly).
50
 * This is a hack to support the overloaded operators from SMT-LIB 2007 (e.g., bvdiv,
51
 * bvlshr, etc.)
52
 *
53
 * December 11, 2008. Added arith_bineq constructor.
54
 *
55
 * January 27, 2009. Removed BDD representation of bvlogic_expressions
56
 *
57
 *
58
 * MAJOR REVISION: April 2010
59
 *
60
 * 1) Removed the arithmetic and bitvector variables in polynomials
61
 *    To replace them, we represent power-products directly as
62
 *    terms in the term table.
63
 *
64
 * 2) Removed the AIG-style data structures for bitvectors (these
65
 *    are replaced by arrays of boolean terms). Added an n-ary
66
 *    (xor ...) term constructor to help representing bv-xor.
67
 *
68
 * 3) Removed the term constructor 'not' for boolean negation.
69
 *    Used positive/negative polarity bits to replace that:
70
 *    For a boolean term t, we use a one bit tag to denote t+ and t-,
71
 *    where t- means (not t).
72
 *
73
 * 5) Added terms for converting between boolean and bitvector terms:
74
 *    Given a term u of type (bitvector n) then (bit u i) is
75
 *    a boolean term for i=0 to n-1. (bit u 0) is the low-order bit.
76
 *    Conversely, given n boolean terms b_0 ... b_{n-1}, the term
77
 *    (bitarray b0 ... b_{n-1}) is the bitvector formed by b0 ... b_{n-1}.
78
 *
79
 * 6) Added support for unit types: a unit type tau is a type of cardinality
80
 *    one. In that case, all terms of type tau are equal so we build a
81
 *    single representative term for type tau.
82
 *
83
 * 7) General cleanup to make things more consistent: use a generic
84
 *    structure to represent most composite terms.
85
 *
86
 * July 2012: Added lambda terms.
87
 *
88
 * June 2015: div/mod/abs and friends
89
 *
90
 * July 2016: division (by non-constant)
91
 */
92

93
/*
94
 * The internal terms include:
95
 *
96
 * 1) constants:
97
 *    - constants of uninterpreted/scalar
98
 *    - global uninterpreted constants
99
 *
100
 * 2) generic terms
101
 *    - ite c t1 t2
102
 *    - eq t1 t2
103
 *    - apply f t1 ... t_n
104
 *    - mk-tuple t1 ... t_n
105
 *    - select i tuple
106
 *    - update f t1 ... t_n v
107
 *    - distinct t1 ... t_n
108
 *
109
 * 3) variables and quantifiers
110
 *    - variables are identified by their type and an integer index.
111
 *    - quantified formulas are of the form (forall v_1 ... v_n term)
112
 *      where each v_i is a variable
113
 *    - lambda terms are of the form (lambda v_1 ... v_n term) where
114
 *      each v_i is a variable
115
 *
116
 * 4) boolean operators
117
 *    - or t1 ... t_n
118
 *    - xor t1 ... t_n
119
 *    - bit i u (extract bit i of a bitvector term u)
120
 *
121
 * 6) arithmetic terms and atoms
122
 *    - terms are either rational constants, power products, or
123
 *      polynomials with rational coefficients
124
 *    - atoms are either of the form (t == 0) or (t >= 0)
125
 *      where p is a term.
126
 *    - atoms a x - a y == 0 are rewritten to (x = y)
127
 *
128
 * 7) bitvector terms and atoms
129
 *    - bitvector constants
130
 *    - power products
131
 *    - polynomials
132
 *    - bit arrays
133
 *    - other operations: divisions/shift
134
 *    - atoms: three binary predicates
135
 *      bv_eq t1 t2
136
 *      bv_ge t1 t2 (unsigned comparison: t1 >= t2)
137
 *      bv_sge t1 t2 (signed comparison: t1 >= t2)
138
 *
139
 * 8) more arithmetic operators (defined in SMTLIB2)
140
 *    - floor x
141
 *    - ceil x
142
 *    - abs x
143
 *    - div x y
144
 *    - mod x y
145
 *    - divides x y: y is a multiple of y
146
 *    - is_int x: true if x is an integer
147
 *    - real division: (/ x y)
148
 *
149
 * All polynomials p are normalized in deg-lex order:
150
 * If i < j then p->mono[i] has lower degree than p->mono[j],
151
 * or they have the same degree and p->mono[i].var precedes
152
 * p->mono[j].var in the lexicographic order. In particular,
153
 * 1) p->mono[0] contains that constant term of p if any
154
 * 2) if p is a linear polynomial, then we have
155
 *    p->mono[i].var < p->mono[j].var when i < j.
156
 *
157
 * Every term is an index t in a global term table,
158
 * where 0 <= t <= 2^30. There are two term occurrences
159
 * t+ and t- associated with term t.  The two occurrences
160
 * are encoded in signed 32bit integers:
161
 * - bit[31] = sign bit = 0
162
 * - bits[30 ... 1] = t
163
 * - bit[0] = polarity bit: 0 for t+, 1 for t-
164
 *
165
 * For a boolean term t, the occurrence t+ means p
166
 * and t- means (not p). All occurrences of a
167
 * non-boolean term t are positive.
168
 *
169
 * For every term, we keep:
170
 * - type[t] (index in the type table)
171
 * - kind[t] (what kind of term it is)
172
 * - desc[t] = descriptor that depends on the kind
173
 *
174
 * It is possible to attach names to term occurrences (but not directly
175
 * to terms). This is required to deal properly with booleans. For example,
176
 * we want to allow the user to give different names to t and (not t).
177
 */
178

179
#ifndef __TERMS_H
180
#define __TERMS_H
181

182
#include <stdint.h>
183
#include <stdbool.h>
184
#include <assert.h>
185

186
#include "terms/balanced_arith_buffers.h"
187
#include "terms/bvarith64_buffers.h"
188
#include "terms/bvarith_buffers.h"
189
#include "terms/bvpoly_buffers.h"
190
#include "terms/pprod_table.h"
191
#include "terms/types.h"
192
#include "utils/bitvectors.h"
193
#include "utils/indexed_table.h"
194
#include "utils/int_hash_map.h"
195
#include "utils/int_hash_tables.h"
196
#include "utils/int_vectors.h"
197
#include "utils/ptr_hash_map.h"
198
#include "utils/ptr_vectors.h"
199
#include "utils/symbol_tables.h"
200

201

202
/*
203
 * TERM INDICES
204
 */
205

206
/*
207
 * type_t and term_t are aliases for int32_t, defined in yices_types.h.
208
 *
209
 * We use the type term_t to denote term occurrences (i.e., a pair
210
 * term index + polarity bit packed into a signed 32bit integer as
211
 * defined in term_occurrences.h).
212
 *
213
 * NULL_TERM and NULL_TYPE are also defined in yices_types.h (used to
214
 * report errors).
215
 *
216
 * Limits defined in yices_limits.h are relevant here:
217
 *
218
 * 1) YICES_MAX_TERMS = bound on the number of terms
219
 * 2) YICES_MAX_TYPES = bound on the number of types
220
 * 3) YICES_MAX_ARITY = bound on the term arity
221
 * 4) YICES_MAX_VARS  = bound on n in (FORALL (x_1.... x_n) P)
222
 * 5) YICES_MAX_DEGREE = bound on the total degree of polynomials and power-products
223
 * 6) YICES_MAX_BVSIZE = bound on the size of bitvector
224
 *
225
 */
226
#include "yices_types.h"
227
#include "yices_limits.h"
228

229

230

231
/*
232
 * TERM KINDS
233
 */
234
/*
235
 * The enumeration order is significant. We can cheaply check whether
236
 * a term is constant, variable or composite.
237
 */
238
typedef enum {
239
  /*
240
   * Special marks
241
   */
242
  UNUSED_TERM,    // deleted term
243
  RESERVED_TERM,  // mark for term indices that can't be used
244

245
  /*
246
   * Constants
247
   */
248
  CONSTANT_TERM,    // constant of uninterpreted/scalar/boolean types
249
  ARITH_CONSTANT,   // rational constant
250
  ARITH_FF_CONSTANT,  // finite field constant
251
  BV64_CONSTANT,    // compact bitvector constant (64 bits at most)
252
  BV_CONSTANT,      // generic bitvector constant (more than 64 bits)
253

254
  /*
255
   * Non-constant, atomic terms
256
   */
257
  VARIABLE,            // variable in quantifiers
258
  UNINTERPRETED_TERM,  // (i.e., global variables, can't be bound).
259

260
  /*
261
   * Composites
262
   */
263
  ARITH_EQ_ATOM,      // atom t == 0
264
  ARITH_GE_ATOM,      // atom t >= 0
265
  ARITH_IS_INT_ATOM,  // atom (is_int x)
266
  ARITH_FLOOR,        // floor x
267
  ARITH_CEIL,         // ceil x
268
  ARITH_ABS,          // absolute value
269
  ARITH_ROOT_ATOM,    // atom (k <= root_count(f) && (x r root_k(f)), for f in Z[x, ...], r in { <, <=, == , !=, >=, > }
270

271
  ARITH_FF_EQ_ATOM,   // atom t == 0
272

273
  ITE_TERM,           // if-then-else
274
  ITE_SPECIAL,        // special if-then-else term (NEW: EXPERIMENTAL)
275
  APP_TERM,           // application of an uninterpreted function
276
  UPDATE_TERM,        // function update
277
  TUPLE_TERM,         // tuple constructor
278
  EQ_TERM,            // equality
279
  DISTINCT_TERM,      // distinct t_1 ... t_n
280
  FORALL_TERM,        // quantifier
281
  LAMBDA_TERM,        // lambda
282
  OR_TERM,            // n-ary OR
283
  XOR_TERM,           // n-ary XOR
284

285
  ARITH_BINEQ_ATOM,   // equality: (t1 == t2)  (between two arithmetic terms)
286
  ARITH_RDIV,         // real division: (/ x y)
287
  ARITH_IDIV,         // integer division: (div x y) as defined in SMT-LIB 2
288
  ARITH_MOD,          // remainder: (mod x y) is y - x * (div x y)
289
  ARITH_DIVIDES_ATOM, // divisibility test: (divides x y) is true if y = n * x for an integer n
290

291
  ARITH_FF_BINEQ_ATOM, // equality: (t1 == t2)  (between two finite field arithmetic terms)
292

293
  BV_ARRAY,           // array of boolean terms
294
  BV_DIV,             // unsigned division
295
  BV_REM,             // unsigned remainder
296
  BV_SDIV,            // signed division
297
  BV_SREM,            // remainder in signed division (rounding to 0)
298
  BV_SMOD,            // remainder in signed division (rounding to -infinity)
299
  BV_SHL,             // shift left (padding with 0)
300
  BV_LSHR,            // logical shift right (padding with 0)
301
  BV_ASHR,            // arithmetic shift right (padding with sign bit)
302
  BV_EQ_ATOM,         // equality: (t1 == t2)
303
  BV_GE_ATOM,         // unsigned comparison: (t1 >= t2)
304
  BV_SGE_ATOM,        // signed comparison (t1 >= t2)
305

306
  SELECT_TERM,      // tuple projection
307
  BIT_TERM,         // bit-select
308

309
  // Polynomials
310
  POWER_PRODUCT,    // power products: (t1^d1 * ... * t_n^d_n)
311
  ARITH_POLY,       // polynomial with rational coefficients
312
  ARITH_FF_POLY,    // polynomial with fintie field coefficients
313
  BV64_POLY,        // polynomial with 64bit coefficients
314
  BV_POLY,          // polynomial with generic bitvector coefficients
315
} term_kind_t;
316

317
#define NUM_TERM_KINDS (BV_POLY+1)
318

319

320

321
/*
322
 * PREDEFINED TERMS
323
 */
324

325
/*
326
 * Term index 0 is reserved to make sure there's no possibility
327
 * that a real term has index equal to const_idx (= 0) used in
328
 * polynomials.
329
 *
330
 * The boolean constant true is built-in and always has index 1.
331
 * This gives two terms:
332
 * - true_term = pos_term(bool_const) = 2
333
 * - false_term = neg_term(bool_const) = 3
334
 *
335
 * The constant 0 is also built-in and always has index 2
336
 * - so zero_term = pos_term(zero_const) = 4
337
 */
338
enum {
339
  // indices
340
  bool_const = 1,
341
  zero_const = 2,
342
  // terms
343
  true_term = 2,
344
  false_term = 3,
345
  zero_term = 4,
346
};
347

348

349
/*
350
 * TERM DESCRIPTORS
351
 */
352

353
/*
354
 * Composite: array of n terms
355
 */
356
typedef struct composite_term_s {
357
  uint32_t arity;  // number of subterms
358
  term_t arg[0];  // real size = arity
359
} composite_term_t;
360

361

362
/*
363
 * Tuple projection and bit-extraction:
364
 * - an integer index + a term occurrence
365
 */
366
typedef struct select_term_s {
367
  uint32_t idx;
368
  term_t arg;
369
} select_term_t;
370

371

372
/*
373
 * Comparison relations for arithmetic root atoms
374
 */
375
typedef enum {
376
  ROOT_ATOM_LT,
377
  ROOT_ATOM_LEQ,
378
  ROOT_ATOM_EQ,
379
  ROOT_ATOM_NEQ,
380
  ROOT_ATOM_GEQ,
381
  ROOT_ATOM_GT
382
} root_atom_rel_t;
383

384

385
/*
386
 * Arithmetic root constraint:
387
 * - an integer root index
388
 * - a variable x
389
 * - a polynomial p(x, ...)
390
 * - the relation x r root_k(p)
391
 */
392
typedef struct root_atom_s {
393
  uint32_t k;
394
  term_t x;
395
  term_t p;
396
  root_atom_rel_t r;
397
} root_atom_t;
398

399

400
/*
401
 * Bitvector constants of arbitrary size:
402
 * - bitsize = number of bits
403
 * - data = array of 32bit words (of size equal to ceil(nbits/32))
404
 */
405
typedef struct bvconst_term_s {
406
  uint32_t bitsize;
407
  uint32_t data[0];
408
} bvconst_term_t;
409

410

411
/*
412
 * Bitvector constants of no more than 64bits
413
 */
414
typedef struct bvconst64_term_s {
415
  uint32_t bitsize; // between 1 and 64
416
  uint64_t value;   // normalized value: high-order bits are 0
417
} bvconst64_term_t;
418

419

420
/*
421
 * Special composites: (experimental)
422
 * - a composite_term descriptor preceded by a generic (hidden)
423
 *   pointer.
424
 * - this is an attempt to improve performance on examples
425
 *   that contain deeply nested if-then-else terms, where
426
 *   all the leaves are constant terms.
427
 * - in such a case, we attach the set of leaves (i.e., the
428
 *   possible values for that term) to the special term
429
 *   descriptor.
430
 */
431
typedef struct special_term_s {
432
  void *extra;
433
  composite_term_t body;
434
} special_term_t;
435

436

437
/*
438
 * Descriptor: one of
439
 * - integer index for constant terms and variables
440
 * - rational constant
441
 * - pair  (idx, arg) for select term
442
 * - ptr to a composite, polynomial, power-product, or bvconst
443
 */
444
typedef struct {
445
  /* The term descriptor. */
446
  union {
447
    /* This must be the first element in term_desc_t. */
448
    indexed_table_elem_t elem;
449

450
    int32_t integer;
451
    void *ptr;
452
    rational_t rational;
453
    select_term_t select;
454
  };
455

456
  /* The kind of term. */
457
  uint8_t kind;
458
  
459
  /* The type of the term. */
460
  type_t type;
461
} term_desc_t;
462

463
/*
464
 * Finalizer function: this is called when a special_term
465
 * is deleted (to cleanup the spec->extra field).
466
 * - the default finalizer calls safe_free(spec->extra).
467
 */
468
typedef void (*special_finalizer_t)(special_term_t *spec, term_kind_t tag);
469

470

471
/*
472
 * Term table: valid terms have indices between 0 and nelems - 1
473
 *
474
 * Symbol table and name table:
475
 * - stbl is a symbol table that maps names (strings) to term occurrences.
476
 * - the name table is the reverse. If maps term occurrence to a name.
477
 * The base name of a term occurrence t, is what's mapped to t in ntbl.
478
 * It's used to display t in pretty printing. The symbol table is
479
 * more important.
480
 *
481
 * Other components:
482
 * - types = pointer to an associated type table
483
 * - pprods = pointer to an associated power product table
484
 * - finalize = finalizer function for special terms
485
 * - htbl = hash table for hash consing
486
 * - utbl = table to map singleton types to the unique term of that type
487
 *
488
 * Auxiliary vectors
489
 * - ibuffer: to store an array of integers
490
 * - pbuffer: to store an array of pprods
491
 */
492
typedef struct term_table_s {
493
  indexed_table_t terms;
494

495
  byte_t *mark;
496

497
  type_table_t *types;
498
  pprod_table_t *pprods;
499
  special_finalizer_t finalize;
500

501
  int_htbl_t htbl;
502
  stbl_t stbl;
503
  ptr_hmap_t ntbl;
504
  int_hmap_t utbl;
505

506
  ivector_t ibuffer;
507
  pvector_t pbuffer;
508
} term_table_t;
509

510

511
/*
512
 * INITIALIZATION
513
 */
514

515
/*
516
 * Initialize table:
517
 * - n = initial size
518
 * - ttbl = attached type table
519
 * - ptbl = attached power-product table
520
 */
521
extern void init_term_table(term_table_t *table, uint32_t n, type_table_t *ttbl, pprod_table_t *ptbl);
522

523

524
/*
525
 * Delete all terms and descriptors, symbol table, hash table, etc.
526
 */
527
extern void delete_term_table(term_table_t *table);
528

529
/*
530
 * Returns the number of terms in the table.
531
 */
532
static inline uint32_t nterms(const term_table_t *table) {
102,608,979✔
533
  return indexed_table_nelems(&table->terms);
102,608,979✔
534
}
535

536
/*
537
 * Returns the number of live terms in the table.
538
 */
539
static inline uint32_t live_terms(const term_table_t *table) {
8,431✔
540
  return indexed_table_live_elems(&table->terms);
8,431✔
541
}
542

543
/*
544
 * Install f as the special finalizer
545
 */
546
static inline void term_table_set_finalizer(term_table_t *table, special_finalizer_t f) {
547
  table->finalize = f;
548
}
549

550

551
/*
552
 * Empty the table: remove all terms and clean up the symbol table
553
 */
554
extern void reset_term_table(term_table_t *table);
555

556

557
/*
558
 * TERM CONSTRUCTORS
559
 */
560

561
/*
562
 * All term constructors return a term occurrence and all the arguments
563
 * the constructors must be term occurrences (term index + polarity
564
 * bit). The constructors do not check type correctness or attempt any
565
 * simplification. They just do hash consing.
566
 */
567

568
/*
569
 * Constant of the given type and index.
570
 * - tau must be uninterpreted or scalar
571
 * - if tau is scalar of cardinality n, then index must be between 0 and n-1
572
 */
573
extern term_t constant_term(term_table_t *table, type_t tau, int32_t index);
574

575

576
/*
577
 * Declare a new uninterpreted constant of type tau.
578
 * - this always creates a fresh term
579
 */
580
extern term_t new_uninterpreted_term(term_table_t *table, type_t tau);
581

582

583
/*
584
 * New variable of type tau.
585
 * - this always creates a fresh term.
586
 */
587
extern term_t new_variable(term_table_t *table, type_t tau);
588

589

590
/*
591
 * Negation: just flip the polarity bit
592
 * - p must be boolean
593
 */
594
extern term_t not_term(term_table_t *table, term_t p);
595

596

597
/*
598
 * If-then-else term (if cond then left else right)
599
 * - tau must be the super type of left/right.
600
 */
601
extern term_t ite_term(term_table_t *table, type_t tau, term_t cond, term_t left, term_t right);
602

603

604
/*
605
 * Other constructors compute the result type
606
 * - for variable-arity constructor:
607
 *   arg must be an array of n term occurrences
608
 *   and n must be no more than YICES_MAX_ARITY.
609
 */
610
extern term_t app_term(term_table_t *table, term_t fun, uint32_t n, const term_t arg[]);
611
extern term_t update_term(term_table_t *table, term_t fun, uint32_t n, const term_t arg[], term_t new_v);
612
extern term_t tuple_term(term_table_t *table, uint32_t n, const term_t arg[]);
613
extern term_t select_term(term_table_t *table, uint32_t index, term_t tuple);
614
extern term_t eq_term(term_table_t *table, term_t left, term_t right);
615
extern term_t distinct_term(term_table_t *table, uint32_t n, const term_t arg[]);
616
extern term_t forall_term(term_table_t *table, uint32_t n, const term_t var[], term_t body);
617
extern term_t lambda_term(term_table_t *table, uint32_t n, const term_t var[], term_t body);
618
extern term_t or_term(term_table_t *table, uint32_t n, const term_t arg[]);
619
extern term_t xor_term(term_table_t *table, uint32_t n, const term_t arg[]);
620
extern term_t bit_term(term_table_t *table, uint32_t index, term_t bv);
621

622

623

624
/*
625
 * POWER PRODUCT
626
 */
627

628
/*
629
 * Power product: r must be valid in table->ptbl, and must not be a tagged
630
 * variable or empty_pp.
631
 * - each variable index x_i in r must be a term defined in table
632
 * - the x_i's must have compatible types: either they are all arithmetic
633
 *   terms (type int or real) or they are all bit-vector terms of the
634
 *   same type.
635
 * The type of the result is determined from the x_i's type:
636
 * - if all x_i's are int, the result is int
637
 * - if some x_i's are int, some are real, the result is real
638
 * - if all x_i's have type (bitvector k), the result has type (bitvector k)
639
 */
640
extern term_t pprod_term(term_table_t *table, pprod_t *r);
641

642
/*
643
 * Power product from a pp_buffer:
644
 * - b must be normalized and non-empty
645
 * - each variable in b must be a term defined in table
646
 * - the variables must have compatible types
647
 */
648
extern term_t pprod_term_from_buffer(term_table_t *table, pp_buffer_t *b);
649

650

651

652
/*
653
 * ARITHMETIC TERMS
654
 */
655

656
/*
657
 * Rational constant: the result has type int if a is an integer or real otherwise
658
 */
659
extern term_t arith_constant(term_table_t *table, rational_t *a);
660

661

662
/*
663
 * Arithmetic polynomial
664
 * - all variables of b must be real or integer terms defined in table
665
 * - b must be normalized and b->ptbl must be the same as table->ptbl
666
 * - if b contains a non-linear polynomial then the power products that
667
 *   occur in p are converted to terms (using pprod_term)
668
 * - then b is turned into a polynomial object a_1 x_1 + ... + a_n x_n,
669
 *   where x_i is a term.
670
 *
671
 * SIDE EFFECT: b is reset to zero
672
 */
673
extern term_t arith_poly(term_table_t *table, rba_buffer_t *b);
674

675

676
/*
677
 * Atom (t == 0)
678
 * - t must be an arithmetic term
679
 */
680
extern term_t arith_eq_atom(term_table_t *table, term_t t);
681

682

683
/*
684
 * Atom (t >= 0)
685
 * - t must be an arithmetic term
686
 */
687
extern term_t arith_geq_atom(term_table_t *table, term_t t);
688

689

690
/*
691
 * Simple equality between two arithmetic terms (left == right)
692
 */
693
extern term_t arith_bineq_atom(term_table_t *table, term_t left, term_t right);
694

695
/*
696
 * Root constraint x r root_k(p).
697
 */
698
extern term_t arith_root_atom(term_table_t *table, uint32_t k, term_t x, term_t p, root_atom_rel_t r);
699

700

701
/*
702
 * Real division: (/ x y)
703
 * - both x and y must be arithmetic terms
704
 * - the result has type real
705
 */
706
extern term_t arith_rdiv(term_table_t *table, term_t x, term_t y);
707

708
/*
709
 * More arithmetic operations
710
 * - is_int(x): true if x is an integer
711
 * - floor and ceiling
712
 * - absolute value
713
 * - div(x, y)
714
 * - mod(x, y)
715
 * - divides(x, y): x divides y
716
 *
717
 * Intended semantics for div and mod:
718
 * - if y > 0 then div(x, y) is floor(x/y)
719
 * - if y < 0 then div(x, y) is ceil(x/y)
720
 * - 0 <= rem(x, y) < y
721
 * - x = y * div(x, y) + rem(x, y)
722
 * These operations are defined for any x and non-zero y.
723
 * They are not required to be integers.
724
 */
725
extern term_t arith_is_int(term_table_t *table, term_t x);
726
extern term_t arith_floor(term_table_t *table, term_t x);
727
extern term_t arith_ceil(term_table_t *table, term_t x);
728
extern term_t arith_abs(term_table_t *table, term_t x);
729
extern term_t arith_idiv(term_table_t *table, term_t x, term_t y);
730
extern term_t arith_mod(term_table_t *table, term_t x, term_t y);
731
extern term_t arith_divides(term_table_t *table, term_t x, term_t y);
732

733
/*
734
 * Check whether b stores an integer polynomial
735
 */
736
extern bool arith_poly_is_integer(const term_table_t *table, rba_buffer_t *b);
737

738

739
/*
740
 * FINITE FIELD TERMS
741
 */
742

743
extern term_t arith_ff_zero(term_table_t *table, const rational_t *mod);
744

745
extern term_t arith_ff_constant(term_table_t *table, rational_t *a, const rational_t *mod);
746

747
/*
748
 * for finite field types zero_term depends on the type of finite field
749
 */
750
static inline term_t ff_zero_term(term_table_t *table, type_t ff) {
×
751
  return arith_ff_zero(table, ff_type_size(table->types, ff));
×
752
}
753

754
/*
755
 * Finite field arithmetic term
756
 * - all variables of b must be finite field terms mod m
757
 * - b must be normalized and b->ptbl must be the same as table->ptbl
758
 * - if b contains a non-linear polynomial then the power products that
759
 *   occur in p are converted to terms (using pprod_term)
760
 * - then b is turned into a polynomial object a_1 x_1 + ... + a_n x_n,
761
 *   where x_i is a term.
762
 *
763
 * SIDE EFFECT: b is reset to zero
764
 */
765
extern term_t arith_ff_poly(term_table_t *table, rba_buffer_t *b, const rational_t *mod);
766

767
/*
768
 * Atom (t == 0)
769
 * - t must be an arithmetic term
770
 */
771
extern term_t arith_ff_eq_atom(term_table_t *table, term_t t);
772

773
/*
774
 * Simple equality between two arithmetic terms (left == right)
775
 */
776
extern term_t arith_ff_bineq_atom(term_table_t *table, term_t left, term_t right);
777

778

779
/*
780
 * BITVECTOR TERMS
781
 */
782

783
/*
784
 * Small bitvector constant
785
 * - n = bitsize (must be between 1 and 64)
786
 * - bv = value (must be normalized modulo 2^n)
787
 */
788
extern term_t bv64_constant(term_table_t *table, uint32_t n, uint64_t bv);
789

790
/*
791
 * Bitvector constant:
792
 * - n = bitsize
793
 * - bv = array of k words (where k = ceil(n/32))
794
 * The constant must be normalized (modulo 2^n)
795
 * This constructor should be used only for n > 64.
796
 */
797
extern term_t bvconst_term(term_table_t *table, uint32_t n, const uint32_t *bv);
798

799

800
/*
801
 * Bitvector polynomials are constructed from a buffer b
802
 * - all variables of b must be bitvector terms defined in table
803
 * - b must be normalized and b->ptbl must be the same as table->ptbl
804
 * - if b contains non-linear terms, then the power products that
805
 *   occur in b are converted to terms (using pprod_term) then
806
 *   a polynomial object is created.
807
 *
808
 * SIDE EFFECT: b is reset to zero.
809
 */
810
extern term_t bv64_poly(term_table_t *table, bvarith64_buffer_t *b);
811
extern term_t bv_poly(term_table_t *table, bvarith_buffer_t *b);
812

813

814
/*
815
 * Variant: use a bvpoly_buffer b
816
 * - this works only for linear polynomials
817
 * - all variables of b must be terms defined in table
818
 * - b must be normalized
819
 * - b is not modified
820
 */
821
extern term_t bv_poly_from_buffer(term_table_t *table, bvpoly_buffer_t *b);
822

823

824
/*
825
 * Bitvector formed of arg[0] ... arg[n-1]
826
 * - n must be positive and no more than YICES_MAX_BVSIZE
827
 * - arg[0] ... arg[n-1] must be boolean terms
828
 */
829
extern term_t bvarray_term(term_table_t *table, uint32_t n, const term_t arg[]);
830

831

832
/*
833
 * Division and shift operators
834
 * - the two arguments must be bitvector terms of the same type
835
 * - in the division/remainder operators, b is the divisor
836
 * - in the shift operator: a is the bitvector to be shifted
837
 *   and b is the shift amount
838
 */
839
extern term_t bvdiv_term(term_table_t *table, term_t a, term_t b);
840
extern term_t bvrem_term(term_table_t *table, term_t a, term_t b);
841
extern term_t bvsdiv_term(term_table_t *table, term_t a, term_t b);
842
extern term_t bvsrem_term(term_table_t *table, term_t a, term_t b);
843
extern term_t bvsmod_term(term_table_t *table, term_t a, term_t b);
844

845
extern term_t bvshl_term(term_table_t *table, term_t a, term_t b);
846
extern term_t bvlshr_term(term_table_t *table, term_t a, term_t b);
847
extern term_t bvashr_term(term_table_t *table, term_t a, term_t b);
848

849

850
/*
851
 * Bitvector atoms: l and r must be bitvector terms of the same type
852
 *  (bveq l r): l == r
853
 *  (bvge l r): l >= r unsigned
854
 *  (bvsge l r): l >= r signed
855
 */
856
extern term_t bveq_atom(term_table_t *table, term_t l, term_t r);
857
extern term_t bvge_atom(term_table_t *table, term_t l, term_t r);
858
extern term_t bvsge_atom(term_table_t *table, term_t l, term_t r);
859

860

861

862

863

864
/*
865
 * SINGLETON TYPES AND REPRESENTATIVE
866
 */
867

868
/*
869
 * With every singleton type, we assign a unique representative.  The
870
 * mapping from unit type to representative is stored in an internal
871
 * hash-table. The following functions add/query this hash table.
872
 */
873

874
/*
875
 * Store t as the unique term of type tau:
876
 * - tau must be a singleton type
877
 * - t must be a valid term of type tau
878
 * - there mustn't be a representative for tau already
879
 */
880
extern void add_unit_type_rep(term_table_t *table, type_t tau, term_t t);
881

882

883
/*
884
 * Store t as the unique term of type tau (if it's not already)
885
 * - tau must be a singleton type
886
 * - t must be a valid term of type tau
887
 * - if tau has no representative yet, then t is stored
888
 *   otherwise nothing happens.
889
 */
890
extern void store_unit_type_rep(term_table_t *table, type_t tau, term_t t);
891

892

893
/*
894
 * Get the unique term of type tau:
895
 * - tau must be a singleton type
896
 * - return the term mapped to tau in a previous call to add_unit_type_rep.
897
 * - return NULL_TERM if there's no such term.
898
 */
899
extern term_t unit_type_rep(term_table_t *table, type_t tau);
900

901

902
/*
903
 * NAMES
904
 */
905

906
/*
907
 * IMPORTANT: we use reference counting on character strings as
908
 * implemented in refcount_strings.h.
909
 *
910
 * Parameter "name" in set_term_name and set_term_basename
911
 * must be constructed via the clone_string function.
912
 * That's not necessary for get_term_by_name or remove_term_name.
913
 * When name is added to the term table, its reference counter
914
 * is increased by 1 or 2.  When remove_term_name is
915
 * called for an existing symbol, the symbol's reference counter is
916
 * decremented.  When the table is deleted (via delete_term_table),
917
 * the reference counters of all symbols present in table are also
918
 * decremented.
919
 */
920

921
/*
922
 * Assign name to term occurrence t.
923
 *
924
 * If name is already mapped to another term t' then the previous mapping
925
 * is hidden. The next calls to get_term_by_name will return t. After a
926
 * call to remove_term_name, the mapping [name --> t] is removed and
927
 * the previous mapping [name --> t'] is revealed.
928
 *
929
 * If t does not have a base name already, then 'name' is stored as the
930
 * base name for t. That's what's printed for t by the pretty printer.
931
 *
932
 * Warning: name is stored as a pointer, no copy is made; name must be
933
 * created via the clone_string function.
934
 */
935
extern void set_term_name(term_table_t *table, term_t t, char *name);
936

937

938
/*
939
 * Assign name as the base name for term t
940
 * - if t already has a base name, then it's replaced by 'name'
941
 *   and the previous name's reference counter is decremented
942
 */
943
extern void set_term_base_name(term_table_t *table, term_t t, char *name);
944

945

946
/*
947
 * Get term occurrence with the given name (or NULL_TERM)
948
 */
949
extern term_t get_term_by_name(term_table_t *table, const char *name);
950

951

952
/*
953
 * Remove a name from the symbol table
954
 * - if name is not in the symbol table, nothing is done
955
 * - if name is mapped to a term t, then the mapping [name -> t]
956
 *   is removed. If name was mapped to a previous term t' then
957
 *   that mapping is restored.
958
 *
959
 * If name is the base name of a term t, then that remains unchanged.
960
 */
961
extern void remove_term_name(term_table_t *table, const char *name);
962

963

964
/*
965
 * Get the base name of term occurrence t
966
 * - return NULL if t has no base name
967
 */
968
extern char *term_name(term_table_t *table, term_t t);
969

970

971
/*
972
 * Clear name: remove t's base name if any.
973
 * - If t has name 'xxx' then 'xxx' is first removed from the symbol
974
 *   table (using remove_term_name) then t's base name is erased.
975
 *   The reference counter for 'xxx' is decremented twice.
976
 * - If t doesn't have a base name, nothing is done.
977
 */
978
extern void clear_term_name(term_table_t *table, term_t t);
979

980

981

982

983

984
/*
985
 * TERM INDICES/POLARITY
986
 */
987

988
/*
989
 * Conversion between indices and terms
990
 * - the polarity bit is the low-order bit of
991
 *   0 means positive polarity
992
 *   1 means negative polarity
993
 */
994
static inline term_t pos_term(int32_t i) {
12,733,652✔
995
  return (i << 1);
12,733,652✔
996
}
997

998
static inline term_t neg_term(int32_t i) {
5,560✔
999
  return (i << 1) | 1;
5,560✔
1000
}
1001

1002

1003
/*
1004
 * Term of index i and polarity tt
1005
 * - true means positive polarity
1006
 * - false means negative polarity
1007
 */
1008
static inline term_t mk_term(int32_t i, bool tt) {
14,538,164✔
1009
  return (i << 1) | (((int32_t) tt) ^ 1);
14,538,164✔
1010
}
1011

1012

1013
/*
1014
 * Extract term and polarity bit
1015
 */
1016
static inline int32_t index_of(term_t x) {
803,651,780✔
1017
  return x>>1;
803,651,780✔
1018
}
1019

1020
static inline uint32_t polarity_of(term_t x) {
83,096,580✔
1021
  return ((uint32_t) x) & 1;
83,096,580✔
1022
}
1023

1024

1025
/*
1026
 * Check polarity
1027
 */
1028
static inline bool is_pos_term(term_t x) {
19,218,687✔
1029
  return polarity_of(x) == 0;
19,218,687✔
1030
}
1031

1032
static inline bool is_neg_term(term_t x) {
31,767,933✔
1033
  return polarity_of(x) != 0;
31,767,933✔
1034
}
1035

1036

1037
/*
1038
 * Complement of x = same term, opposite polarity
1039
 * - this can be used instead of not_term(table, x)
1040
 *   if x is known to be a valid boolean term.
1041
 */
1042
static inline term_t opposite_term(term_t x) {
11,556,010✔
1043
  return x ^ 1;
11,556,010✔
1044
}
1045

1046

1047
/*
1048
 * Remove the sign of x:
1049
 * - if x has positive polarity: return x
1050
 * - if x has negative polarity: return (not x)
1051
 */
1052
static inline term_t unsigned_term(term_t x) {
107,807,235✔
1053
  return x & ~1; // clear polarity bit
107,807,235✔
1054
}
1055

1056

1057
/*
1058
 * Add polarity tt to term x:
1059
 * - if tt is true: return x
1060
 * - if tt is false: return (not x)
1061
 */
1062
static inline term_t signed_term(term_t x, bool tt) {
71,068✔
1063
  return x ^ (((int32_t) tt) ^ 1);
71,068✔
1064
}
1065

1066

1067
/*
1068
 * Check whether x and y are opposite terms
1069
 */
1070
static inline bool opposite_bool_terms(term_t x, term_t y) {
694,999✔
1071
  return (x ^ y) == 1;
694,999✔
1072
}
1073

1074

1075
/*
1076
 * Conversion of boolean to true_term or false_term
1077
 */
1078
static inline term_t bool2term(bool tt) {
12,841,753✔
1079
  return mk_term(bool_const, tt);
12,841,753✔
1080
}
1081

1082

1083

1084
/*
1085
 * SUPPORT FOR POLYNOMIAL/BUFFER OPERATIONS
1086
 */
1087

1088
/*
1089
 * The term table store polynomials in the form
1090
 *      a_0 t_0 + ... + a_n t_n
1091
 * where a_i is a coefficient and t_i is a term.
1092
 *
1093
 * For arithmetic and bit-vector operations that involve buffers and
1094
 * terms, we must convert the integer indices t_0 ... t_n to the
1095
 * power products r_0 ... r_n that buffers require.
1096
 *
1097
 * The translation is defined by:
1098
 * 1) if t_i is const_idx --> r_i is empty_pp
1099
 * 2) if t_i is a power product --> r_i = descriptor for t_i
1100
 * 3) otherwise --> r_i = var_pp(t_i)
1101
 */
1102

1103
/*
1104
 * Convert term t to a power product:
1105
 * - t must be a term (not a term index) present in the table
1106
 * - t must have arithmetic or bitvector type
1107
 */
1108
extern pprod_t *pprod_for_term(const term_table_t *table, term_t t);
1109

1110

1111
/*
1112
 * Degree of term t
1113
 * - t must be a good term of arithmetic or bitvector type
1114
 *
1115
 * - if t is a constant --> 0
1116
 * - if t is a power product --> that product degree
1117
 * - if t is a polynomial --> degree of that polynomial
1118
 * - otherwise --> 1
1119
 */
1120
extern uint32_t term_degree(const term_table_t *table, term_t t);
1121

1122
/*
1123
 * Check whether t is a linear polynomial:
1124
 * - t must be a good term of arithmetic or bitvector type.
1125
 * - returns true if t has tag ARITH_POLY/BV64_POLY or BV_POLY
1126
 *   and if no monomial of t is a power product.
1127
 * - this implies that t has degree 1.
1128
 */
1129
extern bool is_linear_poly(const term_table_t *table, term_t t);
1130

1131
/*
1132
 * Convert all indices in polynomial p to power products
1133
 * - all variable indices of p must be either const_idx or
1134
 *   arithmetic terms present in table.
1135
 * - the result is stored in table's internal pbuffer.
1136
 * - the function returns pbuffer->data
1137
 *
1138
 * The number of elements in pbuffer is equal to p->nterms + 1.
1139
 * - pbuffer->data[i] = pprod_for_term(table, p->mono[i].var)
1140
 * - the last element of buffer->data is the end marker end_pp.
1141
 */
1142
extern pprod_t **pprods_for_poly(term_table_t *table, const polynomial_t *p);
1143

1144

1145
/*
1146
 * Convert all indices in bitvector polynomial p to power products
1147
 * - all variable indices of p must be either const_idx or
1148
 *   bitvector terms of bitsize <= 64 present in table.
1149
 * - the result is stored in table's internal pbuffer.
1150
 * - the function returns pbuffer->data.
1151
 */
1152
extern pprod_t **pprods_for_bvpoly64(term_table_t *table, const bvpoly64_t *p);
1153

1154

1155
/*
1156
 * Convert all indices in bitvector polynomial p to power products
1157
 * - all variable indices of p must be either const_idx or
1158
 *   arithmetic terms present in table.
1159
 * - the result is stored in table's internal pbuffer.
1160
 * - the function returns pbuffer->data.
1161
 */
1162
extern pprod_t **pprods_for_bvpoly(term_table_t *table, const bvpoly_t *p);
1163

1164

1165
/*
1166
 * Reset the internal pbuffer
1167
 */
1168
static inline void term_table_reset_pbuffer(term_table_t *table) {
138,549✔
1169
  pvector_reset(&table->pbuffer);
138,549✔
1170
}
138,549✔
1171

1172

1173

1174

1175

1176
/*
1177
 * ACCESS TO TERMS
1178
 */
1179

1180
/*
1181
 * From a term index i
1182
 */
1183
static inline bool valid_term_idx(const term_table_t *table, int32_t i) {
102,601,223✔
1184
  return 0 <= i && i < nterms(table);
102,601,223✔
1185
}
1186

1187
static inline term_desc_t *term_desc(const term_table_t *table, int32_t i) {
894,201,788✔
1188
  return indexed_table_elem(term_desc_t, &table->terms, i);
894,201,788✔
1189
}
1190

1191
static inline term_kind_t unchecked_kind_for_idx(const term_table_t *table,
275,758,055✔
1192
                                                 int32_t i) {
1193
  return term_desc(table, i)->kind;
275,758,055✔
1194
}
1195

1196
static inline bool live_term_idx(const term_table_t *table, int32_t i) {
×
1197
  return valid_term_idx(table, i)
×
1198
    && term_desc(table, i)->kind != UNUSED_TERM;
×
1199
}
1200

1201
static inline bool good_term_idx(const term_table_t *table, int32_t i) {
102,601,223✔
1202
  return valid_term_idx(table, i)
102,601,223✔
1203
    && term_desc(table, i)->kind > RESERVED_TERM;
102,601,223✔
1204
}
1205

1206
static inline type_t type_for_idx(const term_table_t *table, int32_t i) {
318,666,435✔
1207
  assert(good_term_idx(table, i));
1208
  return term_desc(table, i)->type;
318,666,435✔
1209
}
1210

1211
static inline term_kind_t kind_for_idx(const term_table_t *table, int32_t i) {
274,234,058✔
1212
  assert(good_term_idx(table, i));
1213
  return unchecked_kind_for_idx(table, i);
274,234,058✔
1214
}
1215

1216
// descriptor converted to an appropriate type
1217
static inline int32_t integer_value_for_idx(const term_table_t *table, int32_t i) {
302,429✔
1218
  assert(good_term_idx(table, i));
1219
  return term_desc(table, i)->integer;
302,429✔
1220
}
1221

1222
static inline void *ptr_for_idx(const term_table_t *table, int32_t i) {
111,226,048✔
1223
  assert(good_term_idx(table, i));
1224
  return term_desc(table, i)->ptr;
111,226,048✔
1225
}
1226

1227
static inline composite_term_t *composite_for_idx(const term_table_t *table, int32_t i) {
97,779,023✔
1228
  return (composite_term_t *) ptr_for_idx(table, i);
97,779,023✔
1229
}
1230

1231
static inline select_term_t *select_for_idx(const term_table_t *table, int32_t i) {
81,067,608✔
1232
  assert(good_term_idx(table, i));
1233
  return &term_desc(table, i)->select;
81,067,608✔
1234
}
1235

1236
static inline root_atom_t *root_atom_for_idx(const term_table_t *table, int32_t i) {
2,713✔
1237
  return (root_atom_t *) ptr_for_idx(table, i);
2,713✔
1238
}
1239

1240
static inline pprod_t *pprod_for_idx(const term_table_t *table, int32_t i) {
126,229✔
1241
  return (pprod_t *) ptr_for_idx(table, i);
126,229✔
1242
}
1243

1244
static inline rational_t *rational_for_idx(const term_table_t *table, int32_t i) {
1,550,335✔
1245
  assert(good_term_idx(table, i));
1246
  return &term_desc(table, i)->rational;
1,550,335✔
1247
}
1248

1249
static inline polynomial_t *polynomial_for_idx(const term_table_t *table, int32_t i) {
534,018✔
1250
  return (polynomial_t *) ptr_for_idx(table, i);
534,018✔
1251
}
1252

1253
static inline bvconst64_term_t *bvconst64_for_idx(const term_table_t *table, int32_t i) {
4,029,977✔
1254
  return (bvconst64_term_t *) ptr_for_idx(table, i);
4,029,977✔
1255
}
1256

1257
static inline bvconst_term_t *bvconst_for_idx(const term_table_t *table, int32_t i) {
9,041✔
1258
  return (bvconst_term_t *) ptr_for_idx(table, i);
9,041✔
1259
}
1260

1261
static inline bvpoly64_t *bvpoly64_for_idx(const term_table_t *table, int32_t i) {
2,683,178✔
1262
  return (bvpoly64_t *) ptr_for_idx(table, i);
2,683,178✔
1263
}
1264

1265
static inline bvpoly_t *bvpoly_for_idx(const term_table_t *table, int32_t i) {
3,731✔
1266
  return (bvpoly_t *) ptr_for_idx(table, i);
3,731✔
1267
}
1268

1269

1270
// bitsize of bitvector terms
1271
static inline uint32_t bitsize_for_idx(const term_table_t *table, int32_t i) {
17,046,792✔
1272
  return bv_type_size(table->types, type_for_idx(table, i));
17,046,792✔
1273
}
1274

1275

1276

1277
/*
1278
 * Access components using term occurrence t
1279
 */
1280
static inline bool live_term(const term_table_t *table, term_t t) {
×
1281
  return live_term_idx(table, index_of(t));
×
1282
}
1283

1284
// good_term means good_term_index
1285
// and polarity = 0 (unless t is Boolean)
1286
extern bool good_term(const term_table_t *table, term_t t);
1287

1288
static inline bool bad_term(const term_table_t *table, term_t t) {
18,010,084✔
1289
  return ! good_term(table, t);
18,010,084✔
1290
}
1291

1292
static inline term_kind_t term_kind(const term_table_t *table, term_t t) {
258,095,878✔
1293
  return kind_for_idx(table, index_of(t));
258,095,878✔
1294
}
1295

1296
static inline type_t term_type(const term_table_t *table, term_t t) {
301,026,596✔
1297
  return type_for_idx(table, index_of(t));
301,026,596✔
1298
}
1299

1300
static inline type_kind_t term_type_kind(const term_table_t *table, term_t t) {
58,181,079✔
1301
  return type_kind(table->types, term_type(table, t));
58,181,079✔
1302
}
1303

1304

1305
// Checks on the type of t
1306
static inline bool is_arithmetic_term(const term_table_t *table, term_t t) {
13,245,502✔
1307
  return is_arithmetic_type(term_type(table, t));
13,245,502✔
1308
}
1309

1310
static inline bool is_boolean_term(const term_table_t *table, term_t t) {
15,892,117✔
1311
  return is_boolean_type(term_type(table, t));
15,892,117✔
1312
}
1313

1314
static inline bool is_real_term(const term_table_t *table, term_t t) {
121,191✔
1315
  return is_real_type(term_type(table, t));
121,191✔
1316
}
1317

1318
static inline bool is_integer_term(const term_table_t *table, term_t t) {
1,872,114✔
1319
  return is_integer_type(term_type(table, t));
1,872,114✔
1320
}
1321

1322
static inline bool is_bitvector_term(const term_table_t *table, term_t t) {
13,902,460✔
1323
  return term_type_kind(table, t) == BITVECTOR_TYPE;
13,902,460✔
1324
}
1325

1326
static inline bool is_finitefield_term(const term_table_t *table, term_t t) {
6,608,852✔
1327
  return term_type_kind(table, t) == FF_TYPE;
6,608,852✔
1328
}
1329

1330
static inline bool is_scalar_term(const term_table_t *table, term_t t) {
×
1331
  return term_type_kind(table, t) == SCALAR_TYPE;
×
1332
}
1333

1334
static inline bool is_utype_term(const term_table_t *table, term_t t) {
32,268✔
1335
  return term_type_kind(table, t) == UNINTERPRETED_TYPE;
32,268✔
1336
}
1337

1338
static inline bool is_function_term(const term_table_t *table, term_t t) {
433,833✔
1339
  return term_type_kind(table, t) == FUNCTION_TYPE;
433,833✔
1340
}
1341

1342
static inline bool is_tuple_term(const term_table_t *table, term_t t) {
3✔
1343
  return term_type_kind(table, t) == TUPLE_TYPE;
3✔
1344
}
1345

1346
static inline bool is_itype_term(const term_table_t *table, term_t t) {
24✔
1347
  return term_type_kind(table, t) == INSTANCE_TYPE;
24✔
1348
}
1349

1350
// Bitsize of term t
1351
static inline uint32_t term_bitsize(const term_table_t *table, term_t t) {
17,024,929✔
1352
  return bitsize_for_idx(table, index_of(t));
17,024,929✔
1353
}
1354

1355

1356
// Check whether t is if-then-else
1357
static inline bool is_ite_kind(term_kind_t tag) {
1,203,288✔
1358
  return tag == ITE_TERM || tag == ITE_SPECIAL;
1,203,288✔
1359
}
1360

1361
static inline bool is_ite_term(const term_table_t *table, term_t t) {
1,203,288✔
1362
  return is_ite_kind(term_kind(table, t));
1,203,288✔
1363
}
1364

1365

1366
// Check whether t is atomic and constant
1367
static inline bool is_const_kind(term_kind_t tag) {
819,703✔
1368
  return CONSTANT_TERM <= tag && tag <= BV_CONSTANT;
819,703✔
1369
}
1370

1371
static inline bool is_const_term(const term_table_t *table, term_t t) {
625,290✔
1372
  return is_const_kind(term_kind(table, t));
625,290✔
1373
}
1374

1375
// Check whether t is a bitvector polynomials
1376
static inline bool is_bvpoly_kind(term_kind_t tag) {
25,329✔
1377
  return tag == BV64_POLY || tag == BV_POLY;
25,329✔
1378
}
1379

1380
static inline bool is_bvpoly_term(const term_table_t *table, term_t t) {
25,329✔
1381
  return is_bvpoly_kind(term_kind(table, t));
25,329✔
1382
}
1383

1384

1385
/*
1386
 * Check the type of a literal.
1387
 * - arithmetic_literals: (t == 0) or (t >= 0) or (t1 == t2) between
1388
 *   two arithmetic terms
1389
 * - bitvector literals: bv-eq, bv-ge, bv-sga
1390
 */
1391
extern bool is_arithmetic_literal(const term_table_t *table, term_t t);
1392
extern bool is_bitvector_literal(const term_table_t *table, term_t t);
1393

1394

1395

1396
/*
1397
 * CONSTANT TERMS
1398
 */
1399

1400
/*
1401
 * Check whether t is a constant tuple
1402
 * - t must be a tuple term
1403
 */
1404
extern bool is_constant_tuple(const term_table_t *table, term_t t);
1405

1406

1407
/*
1408
 * Generic version: return true if t is an atomic constant
1409
 * or a constant tuple.
1410
 */
1411
extern bool is_constant_term(const term_table_t *table, term_t t);
1412

1413

1414
/*
1415
 * Check whether the table contains a constant term of type tau and the given index
1416
 * - tau must be uninterpreted or scalar
1417
 * - if tau is scalar, then index must be between 0 and cardinality of tau - 1
1418
 * - return NULL_TERM if there's no such term in table
1419
 */
1420
extern term_t find_constant_term(term_table_t *table, type_t tau, int32_t index);
1421

1422

1423

1424

1425
/*
1426
 * Descriptor of term t.
1427
 *
1428
 * NOTE: select_term_desc, bit_term_desc, and rational_term_desc
1429
 * should be used with care. They return a pointer that may become
1430
 * invalid if new terms are added to the table.
1431
 */
1432
static inline int32_t constant_term_index(const term_table_t *table, term_t t) {
1,163✔
1433
  assert(term_kind(table, t) == CONSTANT_TERM);
1434
  return integer_value_for_idx(table, index_of(t));
1,163✔
1435
}
1436

1437
static inline int32_t variable_term_index(const term_table_t *table, term_t t) {
×
1438
  assert(term_kind(table, t) == VARIABLE);
1439
  return integer_value_for_idx(table, index_of(t));
×
1440
}
1441

1442
static inline composite_term_t *composite_term_desc(const term_table_t *table, term_t t) {
12,205,062✔
1443
  return composite_for_idx(table, index_of(t));
12,205,062✔
1444
}
1445

1446
static inline select_term_t *select_term_desc(const term_table_t *table, term_t t) {
305✔
1447
  assert(term_kind(table, t) == SELECT_TERM);
1448
  return select_for_idx(table, index_of(t));
305✔
1449
}
1450

1451
static inline select_term_t *bit_term_desc(const term_table_t *table, term_t t) {
76,184,725✔
1452
  assert(term_kind(table, t) == BIT_TERM);
1453
  return select_for_idx(table, index_of(t));
76,184,725✔
1454
}
1455

1456
static inline pprod_t *pprod_term_desc(const term_table_t *table, term_t t) {
10,584✔
1457
  assert(term_kind(table, t) == POWER_PRODUCT);
1458
  return pprod_for_idx(table, index_of(t));
10,584✔
1459
}
1460

1461
static inline rational_t *rational_term_desc(const term_table_t *table, term_t t) {
1,434,648✔
1462
  assert(term_kind(table, t) == ARITH_CONSTANT);
1463
  return rational_for_idx(table, index_of(t));
1,434,648✔
1464
}
1465

1466
static inline rational_t *finitefield_term_desc(const term_table_t *table, term_t t) {
63✔
1467
  assert(term_kind(table, t) == ARITH_FF_CONSTANT);
1468
  rational_t *q = rational_for_idx(table, index_of(t));
63✔
1469
  assert(q_is_integer(q));
1470
  return q;
63✔
1471
}
1472

1473
static inline polynomial_t *poly_term_desc(const term_table_t *table, term_t t) {
354,172✔
1474
  assert(term_kind(table, t) == ARITH_POLY);
1475
  return polynomial_for_idx(table, index_of(t));
354,172✔
1476
}
1477

1478
static inline polynomial_t *finitefield_poly_term_desc(const term_table_t *table, term_t t) {
12,535✔
1479
  assert(term_kind(table, t) == ARITH_FF_POLY);
1480
  return polynomial_for_idx(table, index_of(t));
12,535✔
1481
}
1482

1483
static inline bvconst64_term_t *bvconst64_term_desc(const term_table_t *table, term_t t) {
3,122,723✔
1484
  assert(term_kind(table, t) == BV64_CONSTANT);
1485
  return bvconst64_for_idx(table, index_of(t));
3,122,723✔
1486
}
1487

1488
static inline bvconst_term_t *bvconst_term_desc(const term_table_t *table, term_t t) {
6,465✔
1489
  assert(term_kind(table, t) == BV_CONSTANT);
1490
  return bvconst_for_idx(table, index_of(t));
6,465✔
1491
}
1492

1493
static inline bvpoly64_t *bvpoly64_term_desc(const term_table_t *table, term_t t) {
2,425,632✔
1494
  assert(term_kind(table, t) == BV64_POLY);
1495
  return bvpoly64_for_idx(table, index_of(t));
2,425,632✔
1496
}
1497

1498
static inline bvpoly_t *bvpoly_term_desc(const term_table_t *table, term_t t) {
2,150✔
1499
  assert(term_kind(table, t) == BV_POLY);
1500
  return bvpoly_for_idx(table, index_of(t));
2,150✔
1501
}
1502

1503

1504
/*
1505
 * Subcomponents of a term t
1506
 */
1507
// arity of a composite term
1508
static inline uint32_t composite_term_arity(const term_table_t *table, term_t t) {
24,943✔
1509
  return composite_term_desc(table, t)->arity;
24,943✔
1510
}
1511

1512
// i-th argument of term t (t must be a composite term)
1513
static inline term_t composite_term_arg(const term_table_t *table, term_t t, uint32_t i) {
261,796✔
1514
  assert(i < composite_term_arity(table, t));
1515
  return composite_term_desc(table, t)->arg[i];
261,796✔
1516
}
1517

1518
// argument of a unary term t
1519
static inline term_t unary_term_arg(const term_table_t *table, term_t t) {
3,119✔
1520
  return integer_value_for_idx(table, index_of(t));
3,119✔
1521
}
1522

1523
// index of a select term t
1524
static inline uint32_t select_term_index(const term_table_t *table, term_t t) {
35✔
1525
  return select_term_desc(table, t)->idx;
35✔
1526
}
1527

1528
// argument of select term t
1529
static inline term_t select_term_arg(const term_table_t *table, term_t t) {
25✔
1530
  return select_term_desc(table, t)->arg;
25✔
1531
}
1532

1533
// index of a bit select term t
1534
static inline uint32_t bit_term_index(const term_table_t *table, term_t t) {
2,620,268✔
1535
  return bit_term_desc(table, t)->idx;
2,620,268✔
1536
}
1537

1538
// argument of select term t
1539
static inline term_t bit_term_arg(const term_table_t *table, term_t t) {
18,261,449✔
1540
  return bit_term_desc(table, t)->arg;
18,261,449✔
1541
}
1542

1543
// argument of arith eq and arith ge atoms
1544
static inline term_t arith_atom_arg(const term_table_t *table, term_t t) {
31,589✔
1545
  assert(term_kind(table, t) == ARITH_EQ_ATOM ||
1546
         term_kind(table, t) == ARITH_GE_ATOM);
1547
  return integer_value_for_idx(table, index_of(t));
31,589✔
1548
}
1549

1550
static inline term_t arith_eq_arg(const term_table_t *table, term_t t) {
46,074✔
1551
  assert(term_kind(table, t) == ARITH_EQ_ATOM);
1552
  return integer_value_for_idx(table, index_of(t));
46,074✔
1553
}
1554

1555
static inline term_t arith_ge_arg(const term_table_t *table, term_t t) {
135,280✔
1556
  assert(term_kind(table, t) == ARITH_GE_ATOM);
1557
  return integer_value_for_idx(table, index_of(t));
135,280✔
1558
}
1559

1560
static inline term_t arith_ff_eq_arg(const term_table_t *table, term_t t) {
13,062✔
1561
  assert(term_kind(table, t) == ARITH_FF_EQ_ATOM);
1562
  return integer_value_for_idx(table, index_of(t));
13,062✔
1563
}
1564

1565
static inline root_atom_t *arith_root_atom_desc(const term_table_t *table, term_t t) {
2,443✔
1566
  assert(term_kind(table, t) == ARITH_ROOT_ATOM);
1567
  return root_atom_for_idx(table, index_of(t));
2,443✔
1568
}
1569

1570
static inline term_t finitefield_atom_arg(const term_table_t *table, term_t t) {
490✔
1571
  assert(term_kind(table, t) == ARITH_FF_EQ_ATOM);
1572
  return integer_value_for_idx(table, index_of(t));
490✔
1573
}
1574

1575
/*
1576
 * Other unary terms
1577
 */
1578
static inline term_t arith_is_int_arg(const term_table_t *table, term_t t) {
1✔
1579
  assert(term_kind(table, t) == ARITH_IS_INT_ATOM);
1580
  return integer_value_for_idx(table, index_of(t));
1✔
1581
}
1582

1583
static inline term_t arith_floor_arg(const term_table_t *table, term_t t) {
13✔
1584
  assert(term_kind(table, t) == ARITH_FLOOR);
1585
  return integer_value_for_idx(table, index_of(t));
13✔
1586
}
1587

1588
static inline term_t arith_ceil_arg(const term_table_t *table, term_t t) {
×
1589
  assert(term_kind(table, t) == ARITH_CEIL);
1590
  return integer_value_for_idx(table, index_of(t));
×
1591
}
1592

1593
static inline term_t arith_abs_arg(const term_table_t *table, term_t t) {
9✔
1594
  assert(term_kind(table, t) == ARITH_ABS);
1595
  return integer_value_for_idx(table, index_of(t));
9✔
1596
}
1597

1598
static inline const rational_t* finitefield_term_order(const term_table_t *table, term_t t) {
3,424✔
1599
  assert(is_ff_type(table->types, term_type(table, t)));
1600
  return ff_type_size(table->types, term_type(table, t));
3,424✔
1601
}
1602

1603
/*
1604
 * All the following functions are equivalent to composite_term_desc, but,
1605
 * when debugging is enabled, they also check that the term kind is consistent.
1606
 */
1607
static inline composite_term_t *ite_term_desc(const term_table_t *table, term_t t) {
1,246,255✔
1608
  assert(is_ite_kind(term_kind(table, t)));
1609
  return composite_for_idx(table, index_of(t));
1,246,255✔
1610
}
1611

1612
static inline composite_term_t *app_term_desc(const term_table_t *table, term_t t) {
68,798,648✔
1613
  assert(term_kind(table, t) == APP_TERM);
1614
  return composite_for_idx(table, index_of(t));
68,798,648✔
1615
}
1616

1617
static inline composite_term_t *update_term_desc(const term_table_t *table, term_t t) {
4,526,328✔
1618
  assert(term_kind(table, t) == UPDATE_TERM);
1619
  return composite_for_idx(table, index_of(t));
4,526,328✔
1620
}
1621

1622
static inline composite_term_t *tuple_term_desc(const term_table_t *table, term_t t) {
1,155✔
1623
  assert(term_kind(table, t) == TUPLE_TERM);
1624
  return composite_for_idx(table, index_of(t));
1,155✔
1625
}
1626

1627
static inline composite_term_t *eq_term_desc(const term_table_t *table, term_t t) {
1,653,860✔
1628
  assert(term_kind(table, t) == EQ_TERM);
1629
  return composite_for_idx(table, index_of(t));
1,653,860✔
1630
}
1631

1632
static inline composite_term_t *distinct_term_desc(const term_table_t *table, term_t t) {
292✔
1633
  assert(term_kind(table, t) == DISTINCT_TERM);
1634
  return composite_for_idx(table, index_of(t));
292✔
1635
}
1636

1637
static inline composite_term_t *forall_term_desc(const term_table_t *table, term_t t) {
23,475✔
1638
  assert(term_kind(table, t) == FORALL_TERM);
1639
  return composite_for_idx(table, index_of(t));
23,475✔
1640
}
1641

1642
static inline composite_term_t *lambda_term_desc(const term_table_t *table, term_t t) {
2,058✔
1643
  assert(term_kind(table, t) == LAMBDA_TERM);
1644
  return composite_for_idx(table, index_of(t));
2,058✔
1645
}
1646

1647
static inline composite_term_t *or_term_desc(const term_table_t *table, term_t t) {
5,239,764✔
1648
  assert(term_kind(table, t) == OR_TERM);
1649
  return composite_for_idx(table, index_of(t));
5,239,764✔
1650
}
1651

1652
static inline composite_term_t *xor_term_desc(const term_table_t *table, term_t t) {
62✔
1653
  assert(term_kind(table, t) == XOR_TERM);
1654
  return composite_for_idx(table, index_of(t));
62✔
1655
}
1656

1657
static inline composite_term_t *arith_bineq_atom_desc(const term_table_t *table, term_t t) {
48,710✔
1658
  assert(term_kind(table, t) == ARITH_BINEQ_ATOM);
1659
  return composite_for_idx(table, index_of(t));
48,710✔
1660
}
1661

1662
static inline composite_term_t *arith_rdiv_term_desc(const term_table_t *table, term_t t) {
119✔
1663
  assert(term_kind(table, t) == ARITH_RDIV);
1664
  return composite_for_idx(table, index_of(t));
119✔
1665
}
1666

1667
static inline composite_term_t *arith_idiv_term_desc(const term_table_t *table, term_t t) {
319✔
1668
  assert(term_kind(table, t) == ARITH_IDIV);
1669
  return composite_for_idx(table, index_of(t));
319✔
1670
}
1671

1672
static inline composite_term_t *arith_mod_term_desc(const term_table_t *table, term_t t) {
381✔
1673
  assert(term_kind(table, t) == ARITH_MOD);
1674
  return composite_for_idx(table, index_of(t));
381✔
1675
}
1676

1677
static inline composite_term_t *arith_divides_atom_desc(const term_table_t *table, term_t t) {
8✔
1678
  assert(term_kind(table, t) == ARITH_DIVIDES_ATOM);
1679
  return composite_for_idx(table, index_of(t));
8✔
1680
}
1681

1682
static inline composite_term_t *arith_ff_bineq_atom_desc(const term_table_t *table, term_t t) {
16✔
1683
  assert(term_kind(table, t) == ARITH_FF_BINEQ_ATOM);
1684
  return composite_for_idx(table, index_of(t));
16✔
1685
}
1686

1687
static inline composite_term_t *bvarray_term_desc(const term_table_t *table, term_t t) {
1,707,084✔
1688
  assert(term_kind(table, t) == BV_ARRAY);
1689
  return composite_for_idx(table, index_of(t));
1,707,084✔
1690
}
1691

1692
static inline composite_term_t *bvdiv_term_desc(const term_table_t *table, term_t t) {
1,953✔
1693
  assert(term_kind(table, t) == BV_DIV);
1694
  return composite_for_idx(table, index_of(t));
1,953✔
1695
}
1696

1697
static inline composite_term_t *bvrem_term_desc(const term_table_t *table, term_t t) {
1,845✔
1698
  assert(term_kind(table, t) == BV_REM);
1699
  return composite_for_idx(table, index_of(t));
1,845✔
1700
}
1701

1702
static inline composite_term_t *bvsdiv_term_desc(const term_table_t *table, term_t t) {
1,546✔
1703
  assert(term_kind(table, t) == BV_SDIV);
1704
  return composite_for_idx(table, index_of(t));
1,546✔
1705
}
1706

1707
static inline composite_term_t *bvsrem_term_desc(const term_table_t *table, term_t t) {
1,438✔
1708
  assert(term_kind(table, t) == BV_SREM);
1709
  return composite_for_idx(table, index_of(t));
1,438✔
1710
}
1711

1712
static inline composite_term_t *bvsmod_term_desc(const term_table_t *table, term_t t) {
7✔
1713
  assert(term_kind(table, t) == BV_SMOD);
1714
  return composite_for_idx(table, index_of(t));
7✔
1715
}
1716

1717
static inline composite_term_t *bvshl_term_desc(const term_table_t *table, term_t t) {
1,560✔
1718
  assert(term_kind(table, t) == BV_SHL);
1719
  return composite_for_idx(table, index_of(t));
1,560✔
1720
}
1721

1722
static inline composite_term_t *bvlshr_term_desc(const term_table_t *table, term_t t) {
3,459✔
1723
  assert(term_kind(table, t) == BV_LSHR);
1724
  return composite_for_idx(table, index_of(t));
3,459✔
1725
}
1726

1727
static inline composite_term_t *bvashr_term_desc(const term_table_t *table, term_t t) {
1,724✔
1728
  assert(term_kind(table, t) == BV_ASHR);
1729
  return composite_for_idx(table, index_of(t));
1,724✔
1730
}
1731

1732
static inline composite_term_t *bveq_atom_desc(const term_table_t *table, term_t t) {
218,223✔
1733
  assert(term_kind(table, t) == BV_EQ_ATOM);
1734
  return composite_for_idx(table, index_of(t));
218,223✔
1735
}
1736

1737
static inline composite_term_t *bvge_atom_desc(const term_table_t *table, term_t t) {
18,323✔
1738
  assert(term_kind(table, t) == BV_GE_ATOM);
1739
  return composite_for_idx(table, index_of(t));
18,323✔
1740
}
1741

1742
static inline composite_term_t *bvsge_atom_desc(const term_table_t *table, term_t t) {
17,864✔
1743
  assert(term_kind(table, t) == BV_SGE_ATOM);
1744
  return composite_for_idx(table, index_of(t));
17,864✔
1745
}
1746

1747

1748

1749
/*
1750
 * Descriptor for special terms
1751
 */
1752
static inline special_term_t *special_desc(composite_term_t *p) {
349,515✔
1753
  return (special_term_t *) (((char *) p) - offsetof(special_term_t, body));
349,515✔
1754
}
1755

1756
static inline special_term_t *special_for_idx(const term_table_t *table, int32_t i) {
1757
  return special_desc(ptr_for_idx(table, i));
1758
}
1759

1760
static inline composite_term_t *ite_special_term_desc(const term_table_t *table, term_t t) {
326,217✔
1761
  assert(term_kind(table, t) == ITE_SPECIAL);
1762
  return composite_for_idx(table, index_of(t));
326,217✔
1763
}
1764

1765
static inline special_term_t *ite_special_desc(const term_table_t *table, term_t t) {
326,217✔
1766
  return special_desc(ite_special_term_desc(table, t));
326,217✔
1767
}
1768

1769

1770

1771

1772

1773
/*
1774
 * GARBAGE COLLECTION
1775
 */
1776

1777
/*
1778
 * Mark and sweep mechanism:
1779
 * - nothing gets deleted until an explicit call to term_table_gc
1780
 * - before calling the garbage collector, the root terms must be
1781
 *   marked by calling set_gc_mark.
1782
 * - all the terms that can be accessed by a name (i.e., all the terms
1783
 *   that are present in the symbol table) are also considered root terms.
1784
 *
1785
 * Garbage collection process:
1786
 * - The predefined terms (bool_const, zero_const, const_idx ) are marked
1787
 * - If keep_named is true, all terms accessible from the symbol table are marked too
1788
 * - The marks are propagated to subterms, types, and power products.
1789
 * - Every term that's not marked is deleted.
1790
 * - If keep_named is false, all references to dead terms are removed from the
1791
 *   symbol table.
1792
 * - The type and power-product tables' own garbage collectors are called.
1793
 * - Finally all the marks are cleared.
1794
 */
1795

1796
/*
1797
 * Set or clear the mark on a term i. If i is marked, it is preserved
1798
 * on the next call to the garbage collector (and all terms reachable
1799
 * from i are preserved too).  If the mark is cleared, i may be deleted.
1800
 */
1801
static inline void term_table_set_gc_mark(term_table_t *table, int32_t i) {
2,933✔
1802
  assert(good_term_idx(table, i));
1803
  set_bit(table->mark, i);
2,933✔
1804
}
2,933✔
1805

1806
static inline void term_table_clr_gc_mark(term_table_t *table, int32_t i) {
1807
  assert(valid_term_idx(table, i));
1808
  clr_bit(table->mark, i);
1809
}
1810

1811

1812
/*
1813
 * Test whether i is marked
1814
 */
1815
static inline bool term_idx_is_marked(const term_table_t *table, int32_t i) {
33,234✔
1816
  assert(valid_term_idx(table, i));
1817
  return tst_bit(table->mark, i);
33,234✔
1818
}
1819

1820

1821
/*
1822
 * Call the garbage collector:
1823
 * - every term reachable from a marked term is preserved.
1824
 * - recursively calls type_table_gc and pprod_table_gc
1825
 * - if keep_named is true, all named terms (reachable from the symbol table)
1826
 *   are preserved. Otherwise,  all references to dead terms are removed
1827
 *   from the symbol table.
1828
 * - then all the marks are cleared.
1829
 *
1830
 * NOTE: type_table_gc is called with the same keep_named flag.
1831
 */
1832
extern void term_table_gc(term_table_t *table, bool keep_named);
1833

1834

1835
#endif /* __TERMS_H */
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

© 2025 Coveralls, Inc