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/int_hash_map.h"
194 #include "utils/int_hash_tables.h"
195 #include "utils/int_vectors.h"
196 #include "utils/ptr_hash_map.h"
197 #include "utils/ptr_vectors.h"
198 #include "utils/symbol_tables.h"
199 
200 
201 /*
202  * TERM INDICES
203  */
204 
205 /*
206  * type_t and term_t are aliases for int32_t, defined in yices_types.h.
207  *
208  * We use the type term_t to denote term occurrences (i.e., a pair
209  * term index + polarity bit packed into a signed 32bit integer as
210  * defined in term_occurrences.h).
211  *
212  * NULL_TERM and NULL_TYPE are also defined in yices_types.h (used to
213  * report errors).
214  *
215  * Limits defined in yices_limits.h are relevant here:
216  *
217  * 1) YICES_MAX_TERMS = bound on the number of terms
218  * 2) YICES_MAX_TYPES = bound on the number of types
219  * 3) YICES_MAX_ARITY = bound on the term arity
220  * 4) YICES_MAX_VARS  = bound on n in (FORALL (x_1.... x_n) P)
221  * 5) YICES_MAX_DEGREE = bound on the total degree of polynomials and power-products
222  * 6) YICES_MAX_BVSIZE = bound on the size of bitvector
223  *
224  */
225 #include "yices_types.h"
226 #include "yices_limits.h"
227 
228 
229 
230 /*
231  * TERM KINDS
232  */
233 /*
234  * The enumeration order is significant. We can cheaply check whether
235  * a term is constant, variable or composite.
236  */
237 typedef enum {
238   /*
239    * Special marks
240    */
241   UNUSED_TERM,    // deleted term
242   RESERVED_TERM,  // mark for term indices that can't be used
243 
244   /*
245    * Constants
246    */
247   CONSTANT_TERM,    // constant of uninterpreted/scalar/boolean types
248   ARITH_CONSTANT,   // rational constant
249   BV64_CONSTANT,    // compact bitvector constant (64 bits at most)
250   BV_CONSTANT,      // generic bitvector constant (more than 64 bits)
251 
252   /*
253    * Non-constant, atomic terms
254    */
255   VARIABLE,            // variable in quantifiers
256   UNINTERPRETED_TERM,  // (i.e., global variables, can't be bound).
257 
258   /*
259    * Composites
260    */
261   ARITH_EQ_ATOM,      // atom t == 0
262   ARITH_GE_ATOM,      // atom t >= 0
263   ARITH_IS_INT_ATOM,  // atom (is_int x)
264   ARITH_FLOOR,        // floor x
265   ARITH_CEIL,         // ceil x
266   ARITH_ABS,          // absolute value
267   ARITH_ROOT_ATOM,    // atom (k <= root_count(f) && (x r root_k(f)), for f in Z[x, ...], r in { <, <=, == , !=, >=, > }
268 
269   ITE_TERM,           // if-then-else
270   ITE_SPECIAL,        // special if-then-else term (NEW: EXPERIMENTAL)
271   APP_TERM,           // application of an uninterpreted function
272   UPDATE_TERM,        // function update
273   TUPLE_TERM,         // tuple constructor
274   EQ_TERM,            // equality
275   DISTINCT_TERM,      // distinct t_1 ... t_n
276   FORALL_TERM,        // quantifier
277   LAMBDA_TERM,        // lambda
278   OR_TERM,            // n-ary OR
279   XOR_TERM,           // n-ary XOR
280 
281   ARITH_BINEQ_ATOM,   // equality: (t1 == t2)  (between two arithmetic terms)
282   ARITH_RDIV,         // real division: (/ x y)
283   ARITH_IDIV,         // integer division: (div x y) as defined in SMT-LIB 2
284   ARITH_MOD,          // remainder: (mod x y) is y - x * (div x y)
285   ARITH_DIVIDES_ATOM, // divisibility test: (divides x y) is true if y = n * x for an integer n
286 
287   BV_ARRAY,           // array of boolean terms
288   BV_DIV,             // unsigned division
289   BV_REM,             // unsigned remainder
290   BV_SDIV,            // signed division
291   BV_SREM,            // remainder in signed division (rounding to 0)
292   BV_SMOD,            // remainder in signed division (rounding to -infinity)
293   BV_SHL,             // shift left (padding with 0)
294   BV_LSHR,            // logical shift right (padding with 0)
295   BV_ASHR,            // arithmetic shift right (padding with sign bit)
296   BV_EQ_ATOM,         // equality: (t1 == t2)
297   BV_GE_ATOM,         // unsigned comparison: (t1 >= t2)
298   BV_SGE_ATOM,        // signed comparison (t1 >= t2)
299 
300   SELECT_TERM,      // tuple projection
301   BIT_TERM,         // bit-select
302 
303   // Polynomials
304   POWER_PRODUCT,    // power products: (t1^d1 * ... * t_n^d_n)
305   ARITH_POLY,       // polynomial with rational coefficients
306   BV64_POLY,        // polynomial with 64bit coefficients
307   BV_POLY,          // polynomial with generic bitvector coefficients
308 } term_kind_t;
309 
310 #define NUM_TERM_KINDS (BV_POLY+1)
311 
312 
313 
314 /*
315  * PREDEFINED TERMS
316  */
317 
318 /*
319  * Term index 0 is reserved to make sure there's no possibility
320  * that a real term has index equal to const_idx (= 0) used in
321  * polynomials.
322  *
323  * The boolean constant true is built-in and always has index 1.
324  * This gives two terms:
325  * - true_term = pos_term(bool_const) = 2
326  * - false_term = neg_term(bool_const) = 3
327  *
328  * The constant 0 is also built-in and always has index 2
329  * - so zero_term = pos_term(zero_const) = 4
330  */
331 enum {
332   // indices
333   bool_const = 1,
334   zero_const = 2,
335   // terms
336   true_term = 2,
337   false_term = 3,
338   zero_term = 4,
339 };
340 
341 
342 /*
343  * TERM DESCRIPTORS
344  */
345 
346 /*
347  * Composite: array of n terms
348  */
349 typedef struct composite_term_s {
350   uint32_t arity;  // number of subterms
351   term_t arg[0];  // real size = arity
352 } composite_term_t;
353 
354 
355 /*
356  * Tuple projection and bit-extraction:
357  * - an integer index + a term occurrence
358  */
359 typedef struct select_term_s {
360   uint32_t idx;
361   term_t arg;
362 } select_term_t;
363 
364 
365 /*
366  * Comparison relations for arithmetic root atoms..
367  */
368 typedef enum {
369   ROOT_ATOM_LT,
370   ROOT_ATOM_LEQ,
371   ROOT_ATOM_EQ,
372   ROOT_ATOM_NEQ,
373   ROOT_ATOM_GEQ,
374   ROOT_ATOM_GT
375 } root_atom_rel_t;
376 
377 
378 /*
379  * Arithmetic root constraint:
380  * - an integer root index
381  * - a variable x
382  * - a polynomial p(x, ...)
383  * - the relation x r root_k(p)
384  */
385 typedef struct root_atom_s {
386   uint32_t k;
387   term_t x;
388   term_t p;
389   root_atom_rel_t r;
390 } root_atom_t;
391 
392 
393 /*
394  * Bitvector constants of arbitrary size:
395  * - bitsize = number of bits
396  * - data = array of 32bit words (of size equal to ceil(nbits/32))
397  */
398 typedef struct bvconst_term_s {
399   uint32_t bitsize;
400   uint32_t data[0];
401 } bvconst_term_t;
402 
403 
404 /*
405  * Bitvector constants of no more than 64bits
406  */
407 typedef struct bvconst64_term_s {
408   uint32_t bitsize; // between 1 and 64
409   uint64_t value;   // normalized value: high-order bits are 0
410 } bvconst64_term_t;
411 
412 
413 /*
414  * Special composites: (experimental)
415  * - a composite_term descriptor preceded by a generic (hidden)
416  *   pointer.
417  * - this is an attempt to improve performance on examples
418  *   that contain deeply nested if-then-else terms, where
419  *   all the leaves are constant terms.
420  * - in such a case, we attach the set of leaves (i.e., the
421  *   possible values for that term) to the special term
422  *   descriptor.
423  */
424 typedef struct special_term_s {
425   void *extra;
426   composite_term_t body;
427 } special_term_t;
428 
429 
430 /*
431  * Descriptor: one of
432  * - integer index for constant terms and variables
433  * - rational constant
434  * - pair  (idx, arg) for select term
435  * - ptr to a composite, polynomial, power-product, or bvconst
436  */
437 typedef union {
438   int32_t integer;
439   void *ptr;
440   rational_t rational;
441   select_term_t select;
442 } term_desc_t;
443 
444 
445 /*
446  * Finalizer function: this is called when a special_term
447  * is deleted (to cleanup the spec->extra field).
448  * - the default finalizer calls safe_free(spec->extra).
449  */
450 typedef void (*special_finalizer_t)(special_term_t *spec, term_kind_t tag);
451 
452 
453 /*
454  * Term table: valid terms have indices between 0 and nelems - 1
455  *
456  * For each i between 0 and nelems - 1
457  * - kind[i] = term kind
458  * - type[i] = type
459  * - desc[i] = term descriptor
460  * - mark[i] = one bit used during garbage collection
461  * - size = size of these arrays.
462  *
463  * After deletion, term indices are recycled into a free list.
464  * - free_idx = start of the free list (-1 if the list is empty)
465  * - if i is in the free list then kind[i] is UNUSED and
466  *   desc[i].integer is the index of the next term in the free list
467  *   (or -1 if i is the last element in the free list).
468  *
469  * - live_terms = number of actual terms = nelems - size of the free list
470  *
471  * Symbol table and name table:
472  * - stbl is a symbol table that maps names (strings) to term occurrences.
473  * - the name table is the reverse. If maps term occurrence to a name.
474  * The base name of a term occurrence t, is what's mapped to t in ntbl.
475  * It's used to display t in pretty printing. The symbol table is
476  * more important.
477  *
478  * Other components:
479  * - types = pointer to an associated type table
480  * - pprods = pointer to an associated power product table
481  * - finalize = finalizer function for special terms
482  * - htbl = hash table for hash consing
483  * - utbl = table to map singleton types to the unique term of that type
484  *
485  * Auxiliary vectors
486  * - ibuffer: to store an array of integers
487  * - pbuffer: to store an array of pprods
488  */
489 typedef struct term_table_s {
490   uint8_t *kind;
491   term_desc_t *desc;
492   type_t *type;
493   byte_t *mark;
494 
495   uint32_t size;
496   uint32_t nelems;
497   int32_t free_idx;
498   uint32_t live_terms;
499 
500   type_table_t *types;
501   pprod_table_t *pprods;
502   special_finalizer_t finalize;
503 
504   int_htbl_t htbl;
505   stbl_t stbl;
506   ptr_hmap_t ntbl;
507   int_hmap_t utbl;
508 
509   ivector_t ibuffer;
510   pvector_t pbuffer;
511 } term_table_t;
512 
513 
514 
515 
516 /*
517  * INITIALIZATION
518  */
519 
520 /*
521  * Initialize table:
522  * - n = initial size
523  * - ttbl = attached type table
524  * - ptbl = attached power-product table
525  */
526 extern void init_term_table(term_table_t *table, uint32_t n, type_table_t *ttbl, pprod_table_t *ptbl);
527 
528 
529 /*
530  * Delete all terms and descriptors, symbol table, hash table, etc.
531  */
532 extern void delete_term_table(term_table_t *table);
533 
534 
535 /*
536  * Install f as the special finalizer
537  */
term_table_set_finalizer(term_table_t * table,special_finalizer_t f)538 static inline void term_table_set_finalizer(term_table_t *table, special_finalizer_t f) {
539   table->finalize = f;
540 }
541 
542 
543 /*
544  * Empty the table: remove all terms and clean up the symbol table
545  */
546 extern void reset_term_table(term_table_t *table);
547 
548 
549 /*
550  * TERM CONSTRUCTORS
551  */
552 
553 /*
554  * All term constructors return a term occurrence and all the arguments
555  * the constructors must be term occurrences (term index + polarity
556  * bit). The constructors do not check type correctness or attempt any
557  * simplification. They just do hash consing.
558  */
559 
560 /*
561  * Constant of the given type and index.
562  * - tau must be uninterpreted or scalar
563  * - if tau is scalar of cardinality n, then index must be between 0 and n-1
564  */
565 extern term_t constant_term(term_table_t *table, type_t tau, int32_t index);
566 
567 
568 /*
569  * Declare a new uninterpreted constant of type tau.
570  * - this always creates a fresh term
571  */
572 extern term_t new_uninterpreted_term(term_table_t *table, type_t tau);
573 
574 
575 /*
576  * New variable of type tau.
577  * - this always creates a fresh term.
578  */
579 extern term_t new_variable(term_table_t *table, type_t tau);
580 
581 
582 /*
583  * Negation: just flip the polarity bit
584  * - p must be boolean
585  */
586 extern term_t not_term(term_table_t *table, term_t p);
587 
588 
589 /*
590  * If-then-else term (if cond then left else right)
591  * - tau must be the super type of left/right.
592  */
593 extern term_t ite_term(term_table_t *table, type_t tau, term_t cond, term_t left, term_t right);
594 
595 
596 /*
597  * Other constructors compute the result type
598  * - for variable-arity constructor:
599  *   arg must be an array of n term occurrences
600  *   and n must be no more than YICES_MAX_ARITY.
601  */
602 extern term_t app_term(term_table_t *table, term_t fun, uint32_t n, const term_t arg[]);
603 extern term_t update_term(term_table_t *table, term_t fun, uint32_t n, const term_t arg[], term_t new_v);
604 extern term_t tuple_term(term_table_t *table, uint32_t n, const term_t arg[]);
605 extern term_t select_term(term_table_t *table, uint32_t index, term_t tuple);
606 extern term_t eq_term(term_table_t *table, term_t left, term_t right);
607 extern term_t distinct_term(term_table_t *table, uint32_t n, const term_t arg[]);
608 extern term_t forall_term(term_table_t *table, uint32_t n, const term_t var[], term_t body);
609 extern term_t lambda_term(term_table_t *table, uint32_t n, const term_t var[], term_t body);
610 extern term_t or_term(term_table_t *table, uint32_t n, const term_t arg[]);
611 extern term_t xor_term(term_table_t *table, uint32_t n, const term_t arg[]);
612 extern term_t bit_term(term_table_t *table, uint32_t index, term_t bv);
613 
614 
615 
616 /*
617  * POWER PRODUCT
618  */
619 
620 /*
621  * Power product: r must be valid in table->ptbl, and must not be a tagged
622  * variable or empty_pp.
623  * - each variable index x_i in r must be a term defined in table
624  * - the x_i's must have compatible types: either they are all arithmetic
625  *   terms (type int or real) or they are all bit-vector terms of the
626  *   same type.
627  * The type of the result is determined from the x_i's type:
628  * - if all x_i's are int, the result is int
629  * - if some x_i's are int, some are real, the result is real
630  * - if all x_i's have type (bitvector k), the result has type (bitvector k)
631  */
632 extern term_t pprod_term(term_table_t *table, pprod_t *r);
633 
634 /*
635  * Power product from a pp_buffer:
636  * - b must be normalized and non-empty
637  * - each variable in b must be a term defined in table
638  * - the variables must have compatible types
639  */
640 extern term_t pprod_term_from_buffer(term_table_t *table, pp_buffer_t *b);
641 
642 
643 
644 /*
645  * ARITHMETIC TERMS
646  */
647 
648 /*
649  * Rational constant: the result has type int if a is an integer or real otherwise
650  */
651 extern term_t arith_constant(term_table_t *table, rational_t *a);
652 
653 
654 /*
655  * Arithmetic polynomial
656  * - all variables of b must be real or integer terms defined in table
657  * - b must be normalized and b->ptbl must be the same as table->ptbl
658  * - if b contains a non-linear polynomial then the power products that
659  *   occur in p are converted to terms (using pprod_term)
660  * - then b is turned into a polynomial object a_1 x_1 + ... + a_n x_n,
661  *   where x_i is a term.
662  *
663  * SIDE EFFECT: b is reset to zero
664  */
665 extern term_t arith_poly(term_table_t *table, rba_buffer_t *b);
666 
667 
668 /*
669  * Atom (t == 0)
670  * - t must be an arithmetic term
671  */
672 extern term_t arith_eq_atom(term_table_t *table, term_t t);
673 
674 
675 /*
676  * Atom (t >= 0)
677  * - t must be an arithmetic term
678  */
679 extern term_t arith_geq_atom(term_table_t *table, term_t t);
680 
681 
682 /*
683  * Simple equality between two arithmetic terms (left == right)
684  */
685 extern term_t arith_bineq_atom(term_table_t *table, term_t left, term_t right);
686 
687 /*
688  * Root constraint x r root_k(p).
689  */
690 extern term_t arith_root_atom(term_table_t *table, uint32_t k, term_t x, term_t p, root_atom_rel_t r);
691 
692 
693 /*
694  * Real division: (/ x y)
695  * - both x and y must be arithmetic terms
696  * - the result has type real
697  */
698 extern term_t arith_rdiv(term_table_t *table, term_t x, term_t y);
699 
700 /*
701  * More arithmetic operations
702  * - is_int(x): true if x is an integer
703  * - floor and ceiling
704  * - absolute value
705  * - div(x, y)
706  * - mod(x, y)
707  * - divides(x, y): x divides y
708  *
709  * Intended semantics for div and mod:
710  * - if y > 0 then div(x, y) is floor(x/y)
711  * - if y < 0 then div(x, y) is ceil(x/y)
712  * - 0 <= rem(x, y) < y
713  * - x = y * div(x, y) + rem(x, y)
714  * These operations are defined for any x and non-zero y.
715  * They are not required to be integers.
716  */
717 extern term_t arith_is_int(term_table_t *table, term_t x);
718 extern term_t arith_floor(term_table_t *table, term_t x);
719 extern term_t arith_ceil(term_table_t *table, term_t x);
720 extern term_t arith_abs(term_table_t *table, term_t x);
721 extern term_t arith_idiv(term_table_t *table, term_t x, term_t y);
722 extern term_t arith_mod(term_table_t *table, term_t x, term_t y);
723 extern term_t arith_divides(term_table_t *table, term_t x, term_t y);
724 
725 /*
726  * Check whether b stores an integer polynomial
727  */
728 extern bool arith_poly_is_integer(const term_table_t *table, rba_buffer_t *b);
729 
730 
731 
732 /*
733  * BITVECTOR TERMS
734  */
735 
736 /*
737  * Small bitvector constant
738  * - n = bitsize (must be between 1 and 64)
739  * - bv = value (must be normalized modulo 2^n)
740  */
741 extern term_t bv64_constant(term_table_t *table, uint32_t n, uint64_t bv);
742 
743 /*
744  * Bitvector constant:
745  * - n = bitsize
746  * - bv = array of k words (where k = ceil(n/32))
747  * The constant must be normalized (modulo 2^n)
748  * This constructor should be used only for n > 64.
749  */
750 extern term_t bvconst_term(term_table_t *table, uint32_t n, const uint32_t *bv);
751 
752 
753 /*
754  * Bitvector polynomials are constructed from a buffer b
755  * - all variables of b must be bitvector terms defined in table
756  * - b must be normalized and b->ptbl must be the same as table->ptbl
757  * - if b contains non-linear terms, then the power products that
758  *   occur in b are converted to terms (using pprod_term) then
759  *   a polynomial object is created.
760  *
761  * SIDE EFFECT: b is reset to zero.
762  */
763 extern term_t bv64_poly(term_table_t *table, bvarith64_buffer_t *b);
764 extern term_t bv_poly(term_table_t *table, bvarith_buffer_t *b);
765 
766 
767 /*
768  * Variant: use a bvpoly_buffer b
769  * - this works only for linear polynomials
770  * - all variables of b must be terms defined in table
771  * - b must be normalized
772  * - b is not modified
773  */
774 extern term_t bv_poly_from_buffer(term_table_t *table, bvpoly_buffer_t *b);
775 
776 
777 /*
778  * Bitvector formed of arg[0] ... arg[n-1]
779  * - n must be positive and no more than YICES_MAX_BVSIZE
780  * - arg[0] ... arg[n-1] must be boolean terms
781  */
782 extern term_t bvarray_term(term_table_t *table, uint32_t n, const term_t arg[]);
783 
784 
785 /*
786  * Division and shift operators
787  * - the two arguments must be bitvector terms of the same type
788  * - in the division/remainder operators, b is the divisor
789  * - in the shift operator: a is the bitvector to be shifted
790  *   and b is the shift amount
791  */
792 extern term_t bvdiv_term(term_table_t *table, term_t a, term_t b);
793 extern term_t bvrem_term(term_table_t *table, term_t a, term_t b);
794 extern term_t bvsdiv_term(term_table_t *table, term_t a, term_t b);
795 extern term_t bvsrem_term(term_table_t *table, term_t a, term_t b);
796 extern term_t bvsmod_term(term_table_t *table, term_t a, term_t b);
797 
798 extern term_t bvshl_term(term_table_t *table, term_t a, term_t b);
799 extern term_t bvlshr_term(term_table_t *table, term_t a, term_t b);
800 extern term_t bvashr_term(term_table_t *table, term_t a, term_t b);
801 
802 
803 /*
804  * Bitvector atoms: l and r must be bitvector terms of the same type
805  *  (bveq l r): l == r
806  *  (bvge l r): l >= r unsigned
807  *  (bvsge l r): l >= r signed
808  */
809 extern term_t bveq_atom(term_table_t *table, term_t l, term_t r);
810 extern term_t bvge_atom(term_table_t *table, term_t l, term_t r);
811 extern term_t bvsge_atom(term_table_t *table, term_t l, term_t r);
812 
813 
814 
815 
816 
817 /*
818  * SINGLETON TYPES AND REPRESENTATIVE
819  */
820 
821 /*
822  * With every singleton type, we assign a unique representative.  The
823  * mapping from unit type to representative is stored in an internal
824  * hash-table. The following functions add/query this hash table.
825  */
826 
827 /*
828  * Store t as the unique term of type tau:
829  * - tau must be a singleton type
830  * - t must be a valid term of type tau
831  * - there mustn't be a representative for tau already
832  */
833 extern void add_unit_type_rep(term_table_t *table, type_t tau, term_t t);
834 
835 
836 /*
837  * Store t as the unique term of type tau (if it's not already)
838  * - tau must be a singleton type
839  * - t must be a valid term of type tau
840  * - if tau has no representative yet, then t is stored
841  *   otherwise nothing happens.
842  */
843 extern void store_unit_type_rep(term_table_t *table, type_t tau, term_t t);
844 
845 
846 /*
847  * Get the unique term of type tau:
848  * - tau must be a singleton type
849  * - return the term mapped to tau in a previous call to add_unit_type_rep.
850  * - return NULL_TERM if there's no such term.
851  */
852 extern term_t unit_type_rep(term_table_t *table, type_t tau);
853 
854 
855 /*
856  * NAMES
857  */
858 
859 /*
860  * IMPORTANT: we use reference counting on character strings as
861  * implemented in refcount_strings.h.
862  *
863  * Parameter "name" in set_term_name and set_term_basename
864  * must be constructed via the clone_string function.
865  * That's not necessary for get_term_by_name or remove_term_name.
866  * When name is added to the term table, its reference counter
867  * is increased by 1 or 2.  When remove_term_name is
868  * called for an existing symbol, the symbol's reference counter is
869  * decremented.  When the table is deleted (via delete_term_table),
870  * the reference counters of all symbols present in table are also
871  * decremented.
872  */
873 
874 /*
875  * Assign name to term occurrence t.
876  *
877  * If name is already mapped to another term t' then the previous mapping
878  * is hidden. The next calls to get_term_by_name will return t. After a
879  * call to remove_term_name, the mapping [name --> t] is removed and
880  * the previous mapping [name --> t'] is revealed.
881  *
882  * If t does not have a base name already, then 'name' is stored as the
883  * base name for t. That's what's printed for t by the pretty printer.
884  *
885  * Warning: name is stored as a pointer, no copy is made; name must be
886  * created via the clone_string function.
887  */
888 extern void set_term_name(term_table_t *table, term_t t, char *name);
889 
890 
891 /*
892  * Assign name as the base name for term t
893  * - if t already has a base name, then it's replaced by 'name'
894  *   and the previous name's reference counter is decremented
895  */
896 extern void set_term_base_name(term_table_t *table, term_t t, char *name);
897 
898 
899 /*
900  * Get term occurrence with the given name (or NULL_TERM)
901  */
902 extern term_t get_term_by_name(term_table_t *table, const char *name);
903 
904 
905 /*
906  * Remove a name from the symbol table
907  * - if name is not in the symbol table, nothing is done
908  * - if name is mapped to a term t, then the mapping [name -> t]
909  *   is removed. If name was mapped to a previous term t' then
910  *   that mapping is restored.
911  *
912  * If name is the base name of a term t, then that remains unchanged.
913  */
914 extern void remove_term_name(term_table_t *table, const char *name);
915 
916 
917 /*
918  * Get the base name of term occurrence t
919  * - return NULL if t has no base name
920  */
921 extern char *term_name(term_table_t *table, term_t t);
922 
923 
924 /*
925  * Clear name: remove t's base name if any.
926  * - If t has name 'xxx' then 'xxx' is first removed from the symbol
927  *   table (using remove_term_name) then t's base name is erased.
928  *   The reference counter for 'xxx' is decremented twice.
929  * - If t doesn't have a base name, nothing is done.
930  */
931 extern void clear_term_name(term_table_t *table, term_t t);
932 
933 
934 
935 
936 
937 /*
938  * TERM INDICES/POLARITY
939  */
940 
941 /*
942  * Conversion between indices and terms
943  * - the polarity bit is the low-order bit of
944  *   0 means positive polarity
945  *   1 means negative polarity
946  */
pos_term(int32_t i)947 static inline term_t pos_term(int32_t i) {
948   return (i << 1);
949 }
950 
neg_term(int32_t i)951 static inline term_t neg_term(int32_t i) {
952   return (i << 1) | 1;
953 }
954 
955 
956 /*
957  * Term of index i and polarity tt
958  * - true means positive polarity
959  * - false means negative polarity
960  */
mk_term(int32_t i,bool tt)961 static inline term_t mk_term(int32_t i, bool tt) {
962   return (i << 1) | (((int32_t) tt) ^ 1);
963 }
964 
965 
966 /*
967  * Extract term and polarity bit
968  */
index_of(term_t x)969 static inline int32_t index_of(term_t x) {
970   return x>>1;
971 }
972 
polarity_of(term_t x)973 static inline uint32_t polarity_of(term_t x) {
974   return ((uint32_t) x) & 1;
975 }
976 
977 
978 /*
979  * Check polarity
980  */
is_pos_term(term_t x)981 static inline bool is_pos_term(term_t x) {
982   return polarity_of(x) == 0;
983 }
984 
is_neg_term(term_t x)985 static inline bool is_neg_term(term_t x) {
986   return polarity_of(x) != 0;
987 }
988 
989 
990 /*
991  * Complement of x = same term, opposite polarity
992  * - this can be used instead of not_term(table, x)
993  *   if x is known to be a valid boolean term.
994  */
opposite_term(term_t x)995 static inline term_t opposite_term(term_t x) {
996   return x ^ 1;
997 }
998 
999 
1000 /*
1001  * Remove the sign of x:
1002  * - if x has positive polarity: return x
1003  * - if x has negative polarity: return (not x)
1004  */
unsigned_term(term_t x)1005 static inline term_t unsigned_term(term_t x) {
1006   return x & ~1; // clear polarity bit
1007 }
1008 
1009 
1010 /*
1011  * Add polarity tt to term x:
1012  * - if tt is true: return x
1013  * - if tt is false: return (not x)
1014  */
signed_term(term_t x,bool tt)1015 static inline term_t signed_term(term_t x, bool tt) {
1016   return x ^ (((int32_t) tt) ^ 1);
1017 }
1018 
1019 
1020 /*
1021  * Check whether x and y are opposite terms
1022  */
opposite_bool_terms(term_t x,term_t y)1023 static inline bool opposite_bool_terms(term_t x, term_t y) {
1024   return (x ^ y) == 1;
1025 }
1026 
1027 
1028 /*
1029  * Conversion of boolean to true_term or false_term
1030  */
bool2term(bool tt)1031 static inline term_t bool2term(bool tt) {
1032   return mk_term(bool_const, tt);
1033 }
1034 
1035 
1036 
1037 /*
1038  * SUPPORT FOR POLYNOMIAL/BUFFER OPERATIONS
1039  */
1040 
1041 /*
1042  * The term table store polynomials in the form
1043  *      a_0 t_0 + ... + a_n t_n
1044  * where a_i is a coefficient and t_i is a term.
1045  *
1046  * For arithmetic and bit-vector operations that involve buffers and
1047  * terms, we must convert the integer indices t_0 ... t_n to the
1048  * power products r_0 ... r_n that buffers require.
1049  *
1050  * The translation is defined by:
1051  * 1) if t_i is const_idx --> r_i is empty_pp
1052  * 2) if t_i is a power product --> r_i = descriptor for t_i
1053  * 3) otherwise --> r_i = var_pp(t_i)
1054  */
1055 
1056 /*
1057  * Convert term t to a power product:
1058  * - t must be a term (not a term index) present in the table
1059  * - t must have arithmetic or bitvector type
1060  */
1061 extern pprod_t *pprod_for_term(const term_table_t *table, term_t t);
1062 
1063 
1064 /*
1065  * Degree of term t
1066  * - t must be a good term of arithmetic or bitvector type
1067  *
1068  * - if t is a constant --> 0
1069  * - if t is a power product --> that product degree
1070  * - if t is a polynomial --> degree of that polynomial
1071  * - otherwise --> 1
1072  */
1073 extern uint32_t term_degree(const term_table_t *table, term_t t);
1074 
1075 /*
1076  * Check whether t is a linear polynomial:
1077  * - t must be a good term of arithmetic or bitvector type.
1078  * - returns true if t has tag ARITH_POLY/BV64_POLY or BV_POLY
1079  *   and if no monomial of t is a power product.
1080  * - this implies that t has degree 1.
1081  */
1082 extern bool is_linear_poly(const term_table_t *table, term_t t);
1083 
1084 /*
1085  * Convert all indices in polynomial p to power products
1086  * - all variable indices of p must be either const_idx or
1087  *   arithmetic terms present in table.
1088  * - the result is stored in table's internal pbuffer.
1089  * - the function returns pbuffer->data
1090  *
1091  * The number of elements in pbuffer is equal to p->nterms + 1.
1092  * - pbuffer->data[i] = pprod_for_term(table, p->mono[i].var)
1093  * - the last element of buffer->data is the end marker end_pp.
1094  */
1095 extern pprod_t **pprods_for_poly(term_table_t *table, const polynomial_t *p);
1096 
1097 
1098 /*
1099  * Convert all indices in bitvector polynomial p to power products
1100  * - all variable indices of p must be either const_idx or
1101  *   bitvector terms of bitsize <= 64 present in table.
1102  * - the result is stored in table's internal pbuffer.
1103  * - the function returns pbuffer->data.
1104  */
1105 extern pprod_t **pprods_for_bvpoly64(term_table_t *table, const bvpoly64_t *p);
1106 
1107 
1108 /*
1109  * Convert all indices in bitvector polynomial p to power products
1110  * - all variable indices of p must be either const_idx or
1111  *   arithmetic terms present in table.
1112  * - the result is stored in table's internal pbuffer.
1113  * - the function returns pbuffer->data.
1114  */
1115 extern pprod_t **pprods_for_bvpoly(term_table_t *table, const bvpoly_t *p);
1116 
1117 
1118 /*
1119  * Reset the internal pbuffer
1120  */
term_table_reset_pbuffer(term_table_t * table)1121 static inline void term_table_reset_pbuffer(term_table_t *table) {
1122   pvector_reset(&table->pbuffer);
1123 }
1124 
1125 
1126 
1127 
1128 
1129 /*
1130  * ACCESS TO TERMS
1131  */
1132 
1133 /*
1134  * From a term index i
1135  */
valid_term_idx(const term_table_t * table,int32_t i)1136 static inline bool valid_term_idx(const term_table_t *table, int32_t i) {
1137   return 0 <= i && i < table->nelems;
1138 }
1139 
live_term_idx(const term_table_t * table,int32_t i)1140 static inline bool live_term_idx(const term_table_t *table, int32_t i) {
1141   return valid_term_idx(table, i) && table->kind[i] != UNUSED_TERM;
1142 }
1143 
good_term_idx(const term_table_t * table,int32_t i)1144 static inline bool good_term_idx(const term_table_t *table, int32_t i) {
1145   return valid_term_idx(table, i) && table->kind[i] > RESERVED_TERM;
1146 }
1147 
type_for_idx(const term_table_t * table,int32_t i)1148 static inline type_t type_for_idx(const term_table_t *table, int32_t i) {
1149   assert(good_term_idx(table, i));
1150   return table->type[i];
1151 }
1152 
kind_for_idx(const term_table_t * table,int32_t i)1153 static inline term_kind_t kind_for_idx(const term_table_t *table, int32_t i) {
1154   assert(good_term_idx(table, i));
1155   return table->kind[i];
1156 }
1157 
1158 // descriptor converted to an appropriate type
integer_value_for_idx(const term_table_t * table,int32_t i)1159 static inline int32_t integer_value_for_idx(const term_table_t *table, int32_t i) {
1160   assert(good_term_idx(table, i));
1161   return table->desc[i].integer;
1162 }
1163 
composite_for_idx(const term_table_t * table,int32_t i)1164 static inline composite_term_t *composite_for_idx(const term_table_t *table, int32_t i) {
1165   assert(good_term_idx(table, i));
1166   return (composite_term_t *) table->desc[i].ptr;
1167 }
1168 
select_for_idx(const term_table_t * table,int32_t i)1169 static inline select_term_t *select_for_idx(const term_table_t *table, int32_t i) {
1170   assert(good_term_idx(table, i));
1171   return &table->desc[i].select;
1172 }
1173 
root_atom_for_idx(const term_table_t * table,int32_t i)1174 static inline root_atom_t *root_atom_for_idx(const term_table_t *table, int32_t i) {
1175   assert(good_term_idx(table, i));
1176   return (root_atom_t *) table->desc[i].ptr;
1177 }
1178 
pprod_for_idx(const term_table_t * table,int32_t i)1179 static inline pprod_t *pprod_for_idx(const term_table_t *table, int32_t i) {
1180   assert(good_term_idx(table, i));
1181   return (pprod_t *) table->desc[i].ptr;
1182 }
1183 
rational_for_idx(const term_table_t * table,int32_t i)1184 static inline rational_t *rational_for_idx(const term_table_t *table, int32_t i) {
1185   assert(good_term_idx(table, i));
1186   return &table->desc[i].rational;
1187 }
1188 
polynomial_for_idx(const term_table_t * table,int32_t i)1189 static inline polynomial_t *polynomial_for_idx(const term_table_t *table, int32_t i) {
1190   assert(good_term_idx(table, i));
1191   return (polynomial_t *) table->desc[i].ptr;
1192 }
1193 
bvconst64_for_idx(const term_table_t * table,int32_t i)1194 static inline bvconst64_term_t *bvconst64_for_idx(const term_table_t *table, int32_t i) {
1195   assert(good_term_idx(table, i));
1196   return (bvconst64_term_t *) table->desc[i].ptr;
1197 }
1198 
bvconst_for_idx(const term_table_t * table,int32_t i)1199 static inline bvconst_term_t *bvconst_for_idx(const term_table_t *table, int32_t i) {
1200   assert(good_term_idx(table, i));
1201   return (bvconst_term_t *) table->desc[i].ptr;
1202 }
1203 
bvpoly64_for_idx(const term_table_t * table,int32_t i)1204 static inline bvpoly64_t *bvpoly64_for_idx(const term_table_t *table, int32_t i) {
1205   assert(good_term_idx(table, i));
1206   return (bvpoly64_t *) table->desc[i].ptr;
1207 }
1208 
bvpoly_for_idx(const term_table_t * table,int32_t i)1209 static inline bvpoly_t *bvpoly_for_idx(const term_table_t *table, int32_t i) {
1210   assert(good_term_idx(table, i));
1211   return (bvpoly_t *) table->desc[i].ptr;
1212 }
1213 
1214 
1215 // bitsize of bitvector terms
bitsize_for_idx(const term_table_t * table,int32_t i)1216 static inline uint32_t bitsize_for_idx(const term_table_t *table, int32_t i) {
1217   assert(good_term_idx(table, i));
1218   return bv_type_size(table->types, table->type[i]);
1219 }
1220 
1221 
1222 
1223 /*
1224  * Access components using term occurrence t
1225  */
live_term(const term_table_t * table,term_t t)1226 static inline bool live_term(const term_table_t *table, term_t t) {
1227   return live_term_idx(table, index_of(t));
1228 }
1229 
1230 // good_term means good_term_index
1231 // and polarity = 0 (unless t is Boolean)
1232 extern bool good_term(const term_table_t *table, term_t t);
1233 
bad_term(const term_table_t * table,term_t t)1234 static inline bool bad_term(const term_table_t *table, term_t t) {
1235   return ! good_term(table, t);
1236 }
1237 
term_kind(const term_table_t * table,term_t t)1238 static inline term_kind_t term_kind(const term_table_t *table, term_t t) {
1239   return kind_for_idx(table, index_of(t));
1240 }
1241 
term_type(const term_table_t * table,term_t t)1242 static inline type_t term_type(const term_table_t *table, term_t t) {
1243   return type_for_idx(table, index_of(t));
1244 }
1245 
term_type_kind(const term_table_t * table,term_t t)1246 static inline type_kind_t term_type_kind(const term_table_t *table, term_t t) {
1247   return type_kind(table->types, term_type(table, t));
1248 }
1249 
1250 
1251 // Checks on the type of t
is_arithmetic_term(const term_table_t * table,term_t t)1252 static inline bool is_arithmetic_term(const term_table_t *table, term_t t) {
1253   return is_arithmetic_type(term_type(table, t));
1254 }
1255 
is_boolean_term(const term_table_t * table,term_t t)1256 static inline bool is_boolean_term(const term_table_t *table, term_t t) {
1257   return is_boolean_type(term_type(table, t));
1258 }
1259 
is_real_term(const term_table_t * table,term_t t)1260 static inline bool is_real_term(const term_table_t *table, term_t t) {
1261   return is_real_type(term_type(table, t));
1262 }
1263 
is_integer_term(const term_table_t * table,term_t t)1264 static inline bool is_integer_term(const term_table_t *table, term_t t) {
1265   return is_integer_type(term_type(table, t));
1266 }
1267 
is_bitvector_term(const term_table_t * table,term_t t)1268 static inline bool is_bitvector_term(const term_table_t *table, term_t t) {
1269   return term_type_kind(table, t) == BITVECTOR_TYPE;
1270 }
1271 
is_scalar_term(const term_table_t * table,term_t t)1272 static inline bool is_scalar_term(const term_table_t *table, term_t t) {
1273   return term_type_kind(table, t) == SCALAR_TYPE;
1274 }
1275 
is_utype_term(const term_table_t * table,term_t t)1276 static inline bool is_utype_term(const term_table_t *table, term_t t) {
1277   return term_type_kind(table, t) == UNINTERPRETED_TYPE;
1278 }
1279 
is_function_term(const term_table_t * table,term_t t)1280 static inline bool is_function_term(const term_table_t *table, term_t t) {
1281   return term_type_kind(table, t) == FUNCTION_TYPE;
1282 }
1283 
is_tuple_term(const term_table_t * table,term_t t)1284 static inline bool is_tuple_term(const term_table_t *table, term_t t) {
1285   return term_type_kind(table, t) == TUPLE_TYPE;
1286 }
1287 
is_itype_term(const term_table_t * table,term_t t)1288 static inline bool is_itype_term(const term_table_t *table, term_t t) {
1289   return term_type_kind(table, t) == INSTANCE_TYPE;
1290 }
1291 
1292 // Bitsize of term t
term_bitsize(const term_table_t * table,term_t t)1293 static inline uint32_t term_bitsize(const term_table_t *table, term_t t) {
1294   return bitsize_for_idx(table, index_of(t));
1295 }
1296 
1297 
1298 // Check whether t is if-then-else
is_ite_kind(term_kind_t tag)1299 static inline bool is_ite_kind(term_kind_t tag) {
1300   return tag == ITE_TERM || tag == ITE_SPECIAL;
1301 }
1302 
is_ite_term(const term_table_t * table,term_t t)1303 static inline bool is_ite_term(const term_table_t *table, term_t t) {
1304   return is_ite_kind(term_kind(table, t));
1305 }
1306 
1307 
1308 // Check whether t is atomic and constant
is_const_kind(term_kind_t tag)1309 static inline bool is_const_kind(term_kind_t tag) {
1310   return CONSTANT_TERM <= tag && tag <= BV_CONSTANT;
1311 }
1312 
is_const_term(const term_table_t * table,term_t t)1313 static inline bool is_const_term(const term_table_t *table, term_t t) {
1314   return is_const_kind(term_kind(table, t));
1315 }
1316 
1317 // Check whether t is a bitvector polynomials
is_bvpoly_kind(term_kind_t tag)1318 static inline bool is_bvpoly_kind(term_kind_t tag) {
1319   return tag == BV64_POLY || tag == BV_POLY;
1320 }
1321 
is_bvpoly_term(const term_table_t * table,term_t t)1322 static inline bool is_bvpoly_term(const term_table_t *table, term_t t) {
1323   return is_bvpoly_kind(term_kind(table, t));
1324 }
1325 
1326 
1327 /*
1328  * Check the type of a literal.
1329  * - arithmetic_literals: (t == 0) or (t >= 0) or (t1 == t2) between
1330  *   two arithmetic terms
1331  * - bitvector literals: bv-eq, bv-ge, bv-sga
1332  */
1333 extern bool is_arithmetic_literal(const term_table_t *table, term_t t);
1334 extern bool is_bitvector_literal(const term_table_t *table, term_t t);
1335 
1336 
1337 
1338 /*
1339  * CONSTANT TERMS
1340  */
1341 
1342 /*
1343  * Check whether t is a constant tuple
1344  * - t must be a tuple term
1345  */
1346 extern bool is_constant_tuple(const term_table_t *table, term_t t);
1347 
1348 
1349 /*
1350  * Generic version: return true if t is an atomic constant
1351  * or a constant tuple.
1352  */
1353 extern bool is_constant_term(const term_table_t *table, term_t t);
1354 
1355 
1356 /*
1357  * Check whether the table contains a constant term of type tau and the given index
1358  * - tau must be uninterpreted or scalar
1359  * - if tau is scalar, then index must be between 0 and cardinality of tau - 1
1360  * - return NULL_TERM if there's no such term in table
1361  */
1362 extern term_t find_constant_term(term_table_t *table, type_t tau, int32_t index);
1363 
1364 
1365 
1366 
1367 /*
1368  * Descriptor of term t.
1369  *
1370  * NOTE: select_term_desc, bit_term_desc, and rational_term_desc
1371  * should be used with care. They return a pointer that may become
1372  * invalid if new terms are added to the table.
1373  */
constant_term_index(const term_table_t * table,term_t t)1374 static inline int32_t constant_term_index(const term_table_t *table, term_t t) {
1375   assert(term_kind(table, t) == CONSTANT_TERM);
1376   return integer_value_for_idx(table, index_of(t));
1377 }
1378 
variable_term_index(const term_table_t * table,term_t t)1379 static inline int32_t variable_term_index(const term_table_t *table, term_t t) {
1380   assert(term_kind(table, t) == VARIABLE);
1381   return integer_value_for_idx(table, index_of(t));
1382 }
1383 
composite_term_desc(const term_table_t * table,term_t t)1384 static inline composite_term_t *composite_term_desc(const term_table_t *table, term_t t) {
1385   return composite_for_idx(table, index_of(t));
1386 }
1387 
select_term_desc(const term_table_t * table,term_t t)1388 static inline select_term_t *select_term_desc(const term_table_t *table, term_t t) {
1389   assert(term_kind(table, t) == SELECT_TERM);
1390   return select_for_idx(table, index_of(t));
1391 }
1392 
bit_term_desc(const term_table_t * table,term_t t)1393 static inline select_term_t *bit_term_desc(const term_table_t *table, term_t t) {
1394   assert(term_kind(table, t) == BIT_TERM);
1395   return select_for_idx(table, index_of(t));
1396 }
1397 
pprod_term_desc(const term_table_t * table,term_t t)1398 static inline pprod_t *pprod_term_desc(const term_table_t *table, term_t t) {
1399   assert(term_kind(table, t) == POWER_PRODUCT);
1400   return pprod_for_idx(table, index_of(t));
1401 }
1402 
rational_term_desc(const term_table_t * table,term_t t)1403 static inline rational_t *rational_term_desc(const term_table_t *table, term_t t) {
1404   assert(term_kind(table, t) == ARITH_CONSTANT);
1405   return rational_for_idx(table, index_of(t));
1406 }
1407 
poly_term_desc(const term_table_t * table,term_t t)1408 static inline polynomial_t *poly_term_desc(const term_table_t *table, term_t t) {
1409   assert(term_kind(table, t) == ARITH_POLY);
1410   return polynomial_for_idx(table, index_of(t));
1411 }
1412 
bvconst64_term_desc(const term_table_t * table,term_t t)1413 static inline bvconst64_term_t *bvconst64_term_desc(const term_table_t *table, term_t t) {
1414   assert(term_kind(table, t) == BV64_CONSTANT);
1415   return bvconst64_for_idx(table, index_of(t));
1416 }
1417 
bvconst_term_desc(const term_table_t * table,term_t t)1418 static inline bvconst_term_t *bvconst_term_desc(const term_table_t *table, term_t t) {
1419   assert(term_kind(table, t) == BV_CONSTANT);
1420   return bvconst_for_idx(table, index_of(t));
1421 }
1422 
bvpoly64_term_desc(const term_table_t * table,term_t t)1423 static inline bvpoly64_t *bvpoly64_term_desc(const term_table_t *table, term_t t) {
1424   assert(term_kind(table, t) == BV64_POLY);
1425   return bvpoly64_for_idx(table, index_of(t));
1426 }
1427 
bvpoly_term_desc(const term_table_t * table,term_t t)1428 static inline bvpoly_t *bvpoly_term_desc(const term_table_t *table, term_t t) {
1429   assert(term_kind(table, t) == BV_POLY);
1430   return bvpoly_for_idx(table, index_of(t));
1431 }
1432 
1433 
1434 /*
1435  * Subcomponents of a term t
1436  */
1437 // arity of a composite term
composite_term_arity(const term_table_t * table,term_t t)1438 static inline uint32_t composite_term_arity(const term_table_t *table, term_t t) {
1439   return composite_term_desc(table, t)->arity;
1440 }
1441 
1442 // i-th argument of term t (t must be a composite term)
composite_term_arg(const term_table_t * table,term_t t,uint32_t i)1443 static inline term_t composite_term_arg(const term_table_t *table, term_t t, uint32_t i) {
1444   assert(i < composite_term_arity(table, t));
1445   return composite_term_desc(table, t)->arg[i];
1446 }
1447 
1448 // argument of a unary term t
unary_term_arg(const term_table_t * table,term_t t)1449 static inline term_t unary_term_arg(const term_table_t *table, term_t t) {
1450   return integer_value_for_idx(table, index_of(t));
1451 }
1452 
1453 // index of a select term t
select_term_index(const term_table_t * table,term_t t)1454 static inline uint32_t select_term_index(const term_table_t *table, term_t t) {
1455   return select_term_desc(table, t)->idx;
1456 }
1457 
1458 // argument of select term t
select_term_arg(const term_table_t * table,term_t t)1459 static inline term_t select_term_arg(const term_table_t *table, term_t t) {
1460   return select_term_desc(table, t)->arg;
1461 }
1462 
1463 // index of a bit select term t
bit_term_index(const term_table_t * table,term_t t)1464 static inline uint32_t bit_term_index(const term_table_t *table, term_t t) {
1465   return bit_term_desc(table, t)->idx;
1466 }
1467 
1468 // argument of select term t
bit_term_arg(const term_table_t * table,term_t t)1469 static inline term_t bit_term_arg(const term_table_t *table, term_t t) {
1470   return bit_term_desc(table, t)->arg;
1471 }
1472 
1473 // argument of arith eq and arith ge atoms
arith_atom_arg(const term_table_t * table,term_t t)1474 static inline term_t arith_atom_arg(const term_table_t *table, term_t t) {
1475   assert(term_kind(table, t) == ARITH_EQ_ATOM ||
1476          term_kind(table, t) == ARITH_GE_ATOM);
1477   return integer_value_for_idx(table, index_of(t));
1478 }
1479 
arith_eq_arg(const term_table_t * table,term_t t)1480 static inline term_t arith_eq_arg(const term_table_t *table, term_t t) {
1481   assert(term_kind(table, t) == ARITH_EQ_ATOM);
1482   return integer_value_for_idx(table, index_of(t));
1483 }
1484 
arith_ge_arg(const term_table_t * table,term_t t)1485 static inline term_t arith_ge_arg(const term_table_t *table, term_t t) {
1486   assert(term_kind(table, t) == ARITH_GE_ATOM);
1487   return integer_value_for_idx(table, index_of(t));
1488 }
1489 
arith_root_atom_desc(const term_table_t * table,term_t t)1490 static inline root_atom_t *arith_root_atom_desc(const term_table_t *table, term_t t) {
1491   assert(term_kind(table, t) == ARITH_ROOT_ATOM);
1492   return root_atom_for_idx(table, index_of(t));
1493 }
1494 
1495 /*
1496  * Other unary terms
1497  */
arith_is_int_arg(const term_table_t * table,term_t t)1498 static inline term_t arith_is_int_arg(const term_table_t *table, term_t t) {
1499   assert(term_kind(table, t) == ARITH_IS_INT_ATOM);
1500   return integer_value_for_idx(table, index_of(t));
1501 }
1502 
arith_floor_arg(const term_table_t * table,term_t t)1503 static inline term_t arith_floor_arg(const term_table_t *table, term_t t) {
1504   assert(term_kind(table, t) == ARITH_FLOOR);
1505   return integer_value_for_idx(table, index_of(t));
1506 }
1507 
arith_ceil_arg(const term_table_t * table,term_t t)1508 static inline term_t arith_ceil_arg(const term_table_t *table, term_t t) {
1509   assert(term_kind(table, t) == ARITH_CEIL);
1510   return integer_value_for_idx(table, index_of(t));
1511 }
1512 
arith_abs_arg(const term_table_t * table,term_t t)1513 static inline term_t arith_abs_arg(const term_table_t *table, term_t t) {
1514   assert(term_kind(table, t) == ARITH_ABS);
1515   return integer_value_for_idx(table, index_of(t));
1516 }
1517 
1518 
1519 /*
1520  * All the following functions are equivalent to composite_term_desc, but,
1521  * when debugging is enabled, they also check that the term kind is consistent.
1522  */
ite_term_desc(const term_table_t * table,term_t t)1523 static inline composite_term_t *ite_term_desc(const term_table_t *table, term_t t) {
1524   assert(is_ite_kind(term_kind(table, t)));
1525   return composite_for_idx(table, index_of(t));
1526 }
1527 
app_term_desc(const term_table_t * table,term_t t)1528 static inline composite_term_t *app_term_desc(const term_table_t *table, term_t t) {
1529   assert(term_kind(table, t) == APP_TERM);
1530   return composite_for_idx(table, index_of(t));
1531 }
1532 
update_term_desc(const term_table_t * table,term_t t)1533 static inline composite_term_t *update_term_desc(const term_table_t *table, term_t t) {
1534   assert(term_kind(table, t) == UPDATE_TERM);
1535   return composite_for_idx(table, index_of(t));
1536 }
1537 
tuple_term_desc(const term_table_t * table,term_t t)1538 static inline composite_term_t *tuple_term_desc(const term_table_t *table, term_t t) {
1539   assert(term_kind(table, t) == TUPLE_TERM);
1540   return composite_for_idx(table, index_of(t));
1541 }
1542 
eq_term_desc(const term_table_t * table,term_t t)1543 static inline composite_term_t *eq_term_desc(const term_table_t *table, term_t t) {
1544   assert(term_kind(table, t) == EQ_TERM);
1545   return composite_for_idx(table, index_of(t));
1546 }
1547 
distinct_term_desc(const term_table_t * table,term_t t)1548 static inline composite_term_t *distinct_term_desc(const term_table_t *table, term_t t) {
1549   assert(term_kind(table, t) == DISTINCT_TERM);
1550   return composite_for_idx(table, index_of(t));
1551 }
1552 
forall_term_desc(const term_table_t * table,term_t t)1553 static inline composite_term_t *forall_term_desc(const term_table_t *table, term_t t) {
1554   assert(term_kind(table, t) == FORALL_TERM);
1555   return composite_for_idx(table, index_of(t));
1556 }
1557 
lambda_term_desc(const term_table_t * table,term_t t)1558 static inline composite_term_t *lambda_term_desc(const term_table_t *table, term_t t) {
1559   assert(term_kind(table, t) == LAMBDA_TERM);
1560   return composite_for_idx(table, index_of(t));
1561 }
1562 
or_term_desc(const term_table_t * table,term_t t)1563 static inline composite_term_t *or_term_desc(const term_table_t *table, term_t t) {
1564   assert(term_kind(table, t) == OR_TERM);
1565   return composite_for_idx(table, index_of(t));
1566 }
1567 
xor_term_desc(const term_table_t * table,term_t t)1568 static inline composite_term_t *xor_term_desc(const term_table_t *table, term_t t) {
1569   assert(term_kind(table, t) == XOR_TERM);
1570   return composite_for_idx(table, index_of(t));
1571 }
1572 
arith_bineq_atom_desc(const term_table_t * table,term_t t)1573 static inline composite_term_t *arith_bineq_atom_desc(const term_table_t *table, term_t t) {
1574   assert(term_kind(table, t) == ARITH_BINEQ_ATOM);
1575   return composite_for_idx(table, index_of(t));
1576 }
1577 
arith_rdiv_term_desc(const term_table_t * table,term_t t)1578 static inline composite_term_t *arith_rdiv_term_desc(const term_table_t *table, term_t t) {
1579   assert(term_kind(table, t) == ARITH_RDIV);
1580   return composite_for_idx(table, index_of(t));
1581 }
1582 
arith_idiv_term_desc(const term_table_t * table,term_t t)1583 static inline composite_term_t *arith_idiv_term_desc(const term_table_t *table, term_t t) {
1584   assert(term_kind(table, t) == ARITH_IDIV);
1585   return composite_for_idx(table, index_of(t));
1586 }
1587 
arith_mod_term_desc(const term_table_t * table,term_t t)1588 static inline composite_term_t *arith_mod_term_desc(const term_table_t *table, term_t t) {
1589   assert(term_kind(table, t) == ARITH_MOD);
1590   return composite_for_idx(table, index_of(t));
1591 }
1592 
arith_divides_atom_desc(const term_table_t * table,term_t t)1593 static inline composite_term_t *arith_divides_atom_desc(const term_table_t *table, term_t t) {
1594   assert(term_kind(table, t) == ARITH_DIVIDES_ATOM);
1595   return composite_for_idx(table, index_of(t));
1596 }
1597 
bvarray_term_desc(const term_table_t * table,term_t t)1598 static inline composite_term_t *bvarray_term_desc(const term_table_t *table, term_t t) {
1599   assert(term_kind(table, t) == BV_ARRAY);
1600   return composite_for_idx(table, index_of(t));
1601 }
1602 
bvdiv_term_desc(const term_table_t * table,term_t t)1603 static inline composite_term_t *bvdiv_term_desc(const term_table_t *table, term_t t) {
1604   assert(term_kind(table, t) == BV_DIV);
1605   return composite_for_idx(table, index_of(t));
1606 }
1607 
bvrem_term_desc(const term_table_t * table,term_t t)1608 static inline composite_term_t *bvrem_term_desc(const term_table_t *table, term_t t) {
1609   assert(term_kind(table, t) == BV_REM);
1610   return composite_for_idx(table, index_of(t));
1611 }
1612 
bvsdiv_term_desc(const term_table_t * table,term_t t)1613 static inline composite_term_t *bvsdiv_term_desc(const term_table_t *table, term_t t) {
1614   assert(term_kind(table, t) == BV_SDIV);
1615   return composite_for_idx(table, index_of(t));
1616 }
1617 
bvsrem_term_desc(const term_table_t * table,term_t t)1618 static inline composite_term_t *bvsrem_term_desc(const term_table_t *table, term_t t) {
1619   assert(term_kind(table, t) == BV_SREM);
1620   return composite_for_idx(table, index_of(t));
1621 }
1622 
bvsmod_term_desc(const term_table_t * table,term_t t)1623 static inline composite_term_t *bvsmod_term_desc(const term_table_t *table, term_t t) {
1624   assert(term_kind(table, t) == BV_SMOD);
1625   return composite_for_idx(table, index_of(t));
1626 }
1627 
bvshl_term_desc(const term_table_t * table,term_t t)1628 static inline composite_term_t *bvshl_term_desc(const term_table_t *table, term_t t) {
1629   assert(term_kind(table, t) == BV_SHL);
1630   return composite_for_idx(table, index_of(t));
1631 }
1632 
bvlshr_term_desc(const term_table_t * table,term_t t)1633 static inline composite_term_t *bvlshr_term_desc(const term_table_t *table, term_t t) {
1634   assert(term_kind(table, t) == BV_LSHR);
1635   return composite_for_idx(table, index_of(t));
1636 }
1637 
bvashr_term_desc(const term_table_t * table,term_t t)1638 static inline composite_term_t *bvashr_term_desc(const term_table_t *table, term_t t) {
1639   assert(term_kind(table, t) == BV_ASHR);
1640   return composite_for_idx(table, index_of(t));
1641 }
1642 
bveq_atom_desc(const term_table_t * table,term_t t)1643 static inline composite_term_t *bveq_atom_desc(const term_table_t *table, term_t t) {
1644   assert(term_kind(table, t) == BV_EQ_ATOM);
1645   return composite_for_idx(table, index_of(t));
1646 }
1647 
bvge_atom_desc(const term_table_t * table,term_t t)1648 static inline composite_term_t *bvge_atom_desc(const term_table_t *table, term_t t) {
1649   assert(term_kind(table, t) == BV_GE_ATOM);
1650   return composite_for_idx(table, index_of(t));
1651 }
1652 
bvsge_atom_desc(const term_table_t * table,term_t t)1653 static inline composite_term_t *bvsge_atom_desc(const term_table_t *table, term_t t) {
1654   assert(term_kind(table, t) == BV_SGE_ATOM);
1655   return composite_for_idx(table, index_of(t));
1656 }
1657 
1658 
1659 
1660 /*
1661  * Descriptor for special terms
1662  */
special_desc(composite_term_t * p)1663 static inline special_term_t *special_desc(composite_term_t *p) {
1664   return (special_term_t *) (((char *) p) - offsetof(special_term_t, body));
1665 }
1666 
special_for_idx(const term_table_t * table,int32_t i)1667 static inline special_term_t *special_for_idx(const term_table_t *table, int32_t i) {
1668   assert(good_term_idx(table, i));
1669   return special_desc(table->desc[i].ptr);
1670 }
1671 
ite_special_term_desc(const term_table_t * table,term_t t)1672 static inline composite_term_t *ite_special_term_desc(const term_table_t *table, term_t t) {
1673   assert(term_kind(table, t) == ITE_SPECIAL);
1674   return composite_for_idx(table, index_of(t));
1675 }
1676 
ite_special_desc(const term_table_t * table,term_t t)1677 static inline special_term_t *ite_special_desc(const term_table_t *table, term_t t) {
1678   return special_desc(ite_special_term_desc(table, t));
1679 }
1680 
1681 
1682 
1683 
1684 
1685 /*
1686  * GARBAGE COLLECTION
1687  */
1688 
1689 /*
1690  * Mark and sweep mechanism:
1691  * - nothing gets deleted until an explicit call to term_table_gc
1692  * - before calling the garbage collector, the root terms must be
1693  *   marked by calling set_gc_mark.
1694  * - all the terms that can be accessed by a name (i.e., all the terms
1695  *   that are present in the symbol table) are also considered root terms.
1696  *
1697  * Garbage collection process:
1698  * - The predefined terms (bool_const, zero_const, const_idx ) are marked
1699  * - If keep_named is true, all terms accessible from the symbol table are marked too
1700  * - The marks are propagated to subterms, types, and power products.
1701  * - Every term that's not marked is deleted.
1702  * - If keep_named is false, all references to dead terms are removed from the
1703  *   symbol table.
1704  * - The type and power-product tables' own garbage collectors are called.
1705  * - Finally all the marks are cleared.
1706  */
1707 
1708 /*
1709  * Set or clear the mark on a term i. If i is marked, it is preserved
1710  * on the next call to the garbage collector (and all terms reachable
1711  * from i are preserved too).  If the mark is cleared, i may be deleted.
1712  */
term_table_set_gc_mark(term_table_t * table,int32_t i)1713 static inline void term_table_set_gc_mark(term_table_t *table, int32_t i) {
1714   assert(good_term_idx(table, i));
1715   set_bit(table->mark, i);
1716 }
1717 
term_table_clr_gc_mark(term_table_t * table,int32_t i)1718 static inline void term_table_clr_gc_mark(term_table_t *table, int32_t i) {
1719   assert(valid_term_idx(table, i));
1720   clr_bit(table->mark, i);
1721 }
1722 
1723 
1724 /*
1725  * Test whether i is marked
1726  */
term_idx_is_marked(const term_table_t * table,int32_t i)1727 static inline bool term_idx_is_marked(const term_table_t *table, int32_t i) {
1728   assert(valid_term_idx(table, i));
1729   return tst_bit(table->mark, i);
1730 }
1731 
1732 
1733 /*
1734  * Call the garbage collector:
1735  * - every term reachable from a marked term is preserved.
1736  * - recursively calls type_table_gc and pprod_table_gc
1737  * - if keep_named is true, all named terms (reachable from the symbol table)
1738  *   are preserved. Otherwise,  all references to dead terms are removed
1739  *   from the symbol table.
1740  * - then all the marks are cleared.
1741  *
1742  * NOTE: type_table_gc is called with the same keep_named flag.
1743  */
1744 extern void term_table_gc(term_table_t *table, bool keep_named);
1745 
1746 
1747 #endif /* __TERMS_H */
1748