1 |
|
/****************************************************************************** |
2 |
|
* Top contributors (to current version): |
3 |
|
* Andrew Reynolds |
4 |
|
* |
5 |
|
* This file is part of the cvc5 project. |
6 |
|
* |
7 |
|
* Copyright (c) 2009-2021 by the authors listed in the file AUTHORS |
8 |
|
* in the top-level source directory and their institutional affiliations. |
9 |
|
* All rights reserved. See the file COPYING in the top-level source |
10 |
|
* directory for licensing information. |
11 |
|
* **************************************************************************** |
12 |
|
* |
13 |
|
* Utilities for constructing canonical terms. |
14 |
|
*/ |
15 |
|
|
16 |
|
#include "cvc5_private.h" |
17 |
|
|
18 |
|
#ifndef CVC5__EXPR__TERM_CANONIZE_H |
19 |
|
#define CVC5__EXPR__TERM_CANONIZE_H |
20 |
|
|
21 |
|
#include <map> |
22 |
|
#include "expr/node.h" |
23 |
|
|
24 |
|
namespace cvc5 { |
25 |
|
namespace expr { |
26 |
|
|
27 |
|
/** TermCanonize |
28 |
|
* |
29 |
|
* This class contains utilities for canonizing terms with respect to |
30 |
|
* free variables (which are of kind BOUND_VARIABLE). For example, this |
31 |
|
* class infers that terms like f(BOUND_VARIABLE_1) and f(BOUND_VARIABLE_2) |
32 |
|
* are effectively the same term. |
33 |
|
*/ |
34 |
|
class TermCanonize |
35 |
|
{ |
36 |
|
public: |
37 |
|
TermCanonize(); |
38 |
6298 |
~TermCanonize() {} |
39 |
|
|
40 |
|
/** Maps operators to an identifier, useful for ordering. */ |
41 |
|
int getIdForOperator(Node op); |
42 |
|
/** Maps types to an identifier, useful for ordering. */ |
43 |
|
int getIdForType(TypeNode t); |
44 |
|
/** get term order |
45 |
|
* |
46 |
|
* Returns true if a <= b in the term ordering used by this class. The |
47 |
|
* term order is determined by the leftmost position in a and b whose |
48 |
|
* operators o_a and o_b are distinct at that position. Then a <= b iff |
49 |
|
* getIdForOperator( o_a ) <= getIdForOperator( o_b ). |
50 |
|
*/ |
51 |
|
bool getTermOrder(Node a, Node b); |
52 |
|
/** get canonical free variable #i of type tn */ |
53 |
|
Node getCanonicalFreeVar(TypeNode tn, unsigned i); |
54 |
|
/** get canonical term |
55 |
|
* |
56 |
|
* This returns a canonical (alpha-equivalent) version of n, where |
57 |
|
* bound variables in n may be replaced by other ones, and arguments of |
58 |
|
* commutative operators of n may be sorted (if apply_torder is true). If |
59 |
|
* doHoVar is true, we also canonicalize bound variables that occur in |
60 |
|
* operators. |
61 |
|
* |
62 |
|
* In detail, we replace bound variables in n so the the leftmost occurrence |
63 |
|
* of a bound variable for type T is the first canonical free variable for T, |
64 |
|
* the second leftmost is the second, and so on, for each type T. |
65 |
|
*/ |
66 |
|
Node getCanonicalTerm(TNode n, |
67 |
|
bool apply_torder = false, |
68 |
|
bool doHoVar = true); |
69 |
|
|
70 |
|
private: |
71 |
|
/** the number of ids we have allocated for operators */ |
72 |
|
int d_op_id_count; |
73 |
|
/** map from operators to id */ |
74 |
|
std::map<Node, int> d_op_id; |
75 |
|
/** the number of ids we have allocated for types */ |
76 |
|
int d_typ_id_count; |
77 |
|
/** map from type to id */ |
78 |
|
std::map<TypeNode, int> d_typ_id; |
79 |
|
/** free variables for each type */ |
80 |
|
std::map<TypeNode, std::vector<Node> > d_cn_free_var; |
81 |
|
/** |
82 |
|
* Map from each free variable above to their index in their respective vector |
83 |
|
*/ |
84 |
|
std::map<Node, size_t> d_fvIndex; |
85 |
|
/** |
86 |
|
* Return the range of the free variable in the above map, or 0 if it does not |
87 |
|
* exist. |
88 |
|
*/ |
89 |
|
size_t getIndexForFreeVariable(Node v) const; |
90 |
|
/** get canonical term |
91 |
|
* |
92 |
|
* This is a helper function for getCanonicalTerm above. We maintain a |
93 |
|
* counter of how many variables we have allocated for each type (var_count), |
94 |
|
* and a cache of visited nodes (visited). |
95 |
|
*/ |
96 |
|
Node getCanonicalTerm(TNode n, |
97 |
|
bool apply_torder, |
98 |
|
bool doHoVar, |
99 |
|
std::map<TypeNode, unsigned>& var_count, |
100 |
|
std::map<TNode, Node>& visited); |
101 |
|
}; |
102 |
|
|
103 |
|
} // namespace expr |
104 |
|
} // namespace cvc5 |
105 |
|
|
106 |
|
#endif /* CVC5__EXPR__TERM_CANONIZE_H */ |