1 |
|
/****************************************************************************** |
2 |
|
* Top contributors (to current version): |
3 |
|
* Andrew Reynolds, Morgan Deters, Dejan Jovanovic |
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 |
|
* Type rules for the builtin theory. |
14 |
|
*/ |
15 |
|
|
16 |
|
#include "cvc5_private.h" |
17 |
|
|
18 |
|
#ifndef CVC5__THEORY__BUILTIN__THEORY_BUILTIN_TYPE_RULES_H |
19 |
|
#define CVC5__THEORY__BUILTIN__THEORY_BUILTIN_TYPE_RULES_H |
20 |
|
|
21 |
|
#include "expr/node.h" |
22 |
|
#include "expr/type_node.h" |
23 |
|
|
24 |
|
#include <sstream> |
25 |
|
|
26 |
|
namespace cvc5 { |
27 |
|
namespace theory { |
28 |
|
namespace builtin { |
29 |
|
|
30 |
|
class EqualityTypeRule { |
31 |
|
public: |
32 |
6005814 |
inline static TypeNode computeType(NodeManager* nodeManager, |
33 |
|
TNode n, |
34 |
|
bool check) |
35 |
|
{ |
36 |
6005814 |
TypeNode booleanType = nodeManager->booleanType(); |
37 |
|
|
38 |
6005814 |
if (check) |
39 |
|
{ |
40 |
12011628 |
TypeNode lhsType = n[0].getType(check); |
41 |
12011628 |
TypeNode rhsType = n[1].getType(check); |
42 |
|
|
43 |
6005814 |
if (TypeNode::leastCommonTypeNode(lhsType, rhsType).isNull()) |
44 |
|
{ |
45 |
156 |
std::stringstream ss; |
46 |
78 |
ss << "Subexpressions must have a common base type:" << std::endl; |
47 |
92 |
ss << "Equation: " << n << std::endl; |
48 |
64 |
ss << "Type 1: " << lhsType << std::endl; |
49 |
64 |
ss << "Type 2: " << rhsType << std::endl; |
50 |
|
|
51 |
64 |
throw TypeCheckingExceptionPrivate(n, ss.str()); |
52 |
|
} |
53 |
|
// TODO : check isFirstClass for these types? (github issue #1202) |
54 |
|
} |
55 |
6005736 |
return booleanType; |
56 |
|
} |
57 |
|
};/* class EqualityTypeRule */ |
58 |
|
|
59 |
|
|
60 |
|
class DistinctTypeRule { |
61 |
|
public: |
62 |
18060 |
inline static TypeNode computeType(NodeManager* nodeManager, TNode n, bool check) { |
63 |
18060 |
if( check ) { |
64 |
18060 |
TNode::iterator child_it = n.begin(); |
65 |
18060 |
TNode::iterator child_it_end = n.end(); |
66 |
36120 |
TypeNode joinType = (*child_it).getType(check); |
67 |
39730 |
for (++child_it; child_it != child_it_end; ++child_it) { |
68 |
43340 |
TypeNode currentType = (*child_it).getType(); |
69 |
21670 |
joinType = TypeNode::leastCommonTypeNode(joinType, currentType); |
70 |
21670 |
if (joinType.isNull()) { |
71 |
|
throw TypeCheckingExceptionPrivate(n, "Not all arguments are of the same type"); |
72 |
|
} |
73 |
|
} |
74 |
|
} |
75 |
18060 |
return nodeManager->booleanType(); |
76 |
|
} |
77 |
|
};/* class DistinctTypeRule */ |
78 |
|
|
79 |
|
class SExprTypeRule { |
80 |
|
public: |
81 |
860534 |
inline static TypeNode computeType(NodeManager* nodeManager, TNode n, bool check) { |
82 |
860534 |
if (check) |
83 |
|
{ |
84 |
3369687 |
for (TNode c : n) |
85 |
|
{ |
86 |
2509153 |
c.getType(check); |
87 |
|
} |
88 |
|
} |
89 |
860534 |
return nodeManager->sExprType(); |
90 |
|
} |
91 |
|
};/* class SExprTypeRule */ |
92 |
|
|
93 |
|
class UninterpretedConstantTypeRule { |
94 |
|
public: |
95 |
|
static TypeNode computeType(NodeManager* nodeManager, TNode n, bool check); |
96 |
|
};/* class UninterpretedConstantTypeRule */ |
97 |
|
|
98 |
|
class AbstractValueTypeRule { |
99 |
|
public: |
100 |
|
inline static TypeNode computeType(NodeManager* nodeManager, TNode n, bool check) { |
101 |
|
// An UnknownTypeException means that this node has no type. For now, |
102 |
|
// only abstract values are like this---and then, only if they are created |
103 |
|
// by the user and don't actually correspond to one that the SolverEngine |
104 |
|
// gave them previously. |
105 |
|
throw UnknownTypeException(n); |
106 |
|
} |
107 |
|
};/* class AbstractValueTypeRule */ |
108 |
|
|
109 |
|
class WitnessTypeRule |
110 |
|
{ |
111 |
|
public: |
112 |
3645 |
inline static TypeNode computeType(NodeManager* nodeManager, |
113 |
|
TNode n, |
114 |
|
bool check) |
115 |
|
{ |
116 |
3645 |
if (n[0].getType(check) != nodeManager->boundVarListType()) |
117 |
|
{ |
118 |
|
std::stringstream ss; |
119 |
|
ss << "expected a bound var list for WITNESS expression, got `" |
120 |
|
<< n[0].getType().toString() << "'"; |
121 |
|
throw TypeCheckingExceptionPrivate(n, ss.str()); |
122 |
|
} |
123 |
3645 |
if (n[0].getNumChildren() != 1) |
124 |
|
{ |
125 |
|
std::stringstream ss; |
126 |
|
ss << "expected a bound var list with one argument for WITNESS expression"; |
127 |
|
throw TypeCheckingExceptionPrivate(n, ss.str()); |
128 |
|
} |
129 |
3645 |
if (check) |
130 |
|
{ |
131 |
7290 |
TypeNode rangeType = n[1].getType(check); |
132 |
3645 |
if (!rangeType.isBoolean()) |
133 |
|
{ |
134 |
|
std::stringstream ss; |
135 |
|
ss << "expected a body of a WITNESS expression to have Boolean type"; |
136 |
|
throw TypeCheckingExceptionPrivate(n, ss.str()); |
137 |
|
} |
138 |
|
} |
139 |
|
// The type of a witness function is the type of its bound variable. |
140 |
3645 |
return n[0][0].getType(); |
141 |
|
} |
142 |
|
}; /* class WitnessTypeRule */ |
143 |
|
|
144 |
|
class SortProperties { |
145 |
|
public: |
146 |
|
inline static bool isWellFounded(TypeNode type) { |
147 |
|
return true; |
148 |
|
} |
149 |
|
static Node mkGroundTerm(TypeNode type); |
150 |
|
};/* class SortProperties */ |
151 |
|
|
152 |
|
} // namespace builtin |
153 |
|
} // namespace theory |
154 |
|
} // namespace cvc5 |
155 |
|
|
156 |
|
#endif /* CVC5__THEORY__BUILTIN__THEORY_BUILTIN_TYPE_RULES_H */ |