forked from newton-blockchain/ton
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunify-types.cpp
420 lines (395 loc) · 10 KB
/
unify-types.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
/*
This file is part of TON Blockchain Library.
TON Blockchain Library is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
TON Blockchain Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with TON Blockchain Library. If not, see <http://www.gnu.org/licenses/>.
Copyright 2017-2020 Telegram Systems LLP
*/
#include "func.h"
namespace funC {
/*
*
* TYPE EXPRESSIONS
*
*/
int TypeExpr::holes = 0, TypeExpr::type_vars = 0; // not thread safe, but it is ok for now
void TypeExpr::compute_width() {
switch (constr) {
case te_Atomic:
case te_Map:
minw = maxw = 1;
break;
case te_Tensor:
minw = maxw = 0;
for (TypeExpr* arg : args) {
minw += arg->minw;
maxw += arg->maxw;
}
if (minw > w_inf) {
minw = w_inf;
}
if (maxw > w_inf) {
maxw = w_inf;
}
break;
case te_Tuple:
minw = maxw = 1;
for (TypeExpr* arg : args) {
arg->compute_width();
}
break;
case te_Indirect:
minw = args[0]->minw;
maxw = args[0]->maxw;
break;
default:
minw = 0;
maxw = w_inf;
break;
}
}
bool TypeExpr::recompute_width() {
switch (constr) {
case te_Tensor:
case te_Indirect: {
int min = 0, max = 0;
for (TypeExpr* arg : args) {
min += arg->minw;
max += arg->maxw;
}
if (min > maxw || max < minw) {
return false;
}
if (min > w_inf) {
min = w_inf;
}
if (max > w_inf) {
max = w_inf;
}
if (minw < min) {
minw = min;
}
if (maxw > max) {
maxw = max;
}
return true;
}
case te_Tuple: {
for (TypeExpr* arg : args) {
if (arg->minw > 1 || arg->maxw < 1 || arg->minw > arg->maxw) {
return false;
}
}
return true;
}
default:
return false;
}
}
int TypeExpr::extract_components(std::vector<TypeExpr*>& comp_list) {
if (constr != te_Indirect && constr != te_Tensor) {
comp_list.push_back(this);
return 1;
}
int res = 0;
for (TypeExpr* arg : args) {
res += arg->extract_components(comp_list);
}
return res;
}
TypeExpr* TypeExpr::new_map(TypeExpr* from, TypeExpr* to) {
return new TypeExpr{te_Map, std::vector<TypeExpr*>{from, to}};
}
void TypeExpr::replace_with(TypeExpr* te2) {
if (te2 == this) {
return;
}
constr = te_Indirect;
value = 0;
minw = te2->minw;
maxw = te2->maxw;
args.clear();
args.push_back(te2);
}
bool TypeExpr::remove_indirect(TypeExpr*& te, TypeExpr* forbidden) {
assert(te);
while (te->constr == te_Indirect) {
te = te->args[0];
}
if (te->constr == te_Unknown) {
return te != forbidden;
}
bool res = true;
for (auto& x : te->args) {
res &= remove_indirect(x, forbidden);
}
return res;
}
bool TypeExpr::remove_forall(TypeExpr*& te) {
assert(te);
if (te->constr != te_ForAll) {
return false;
}
assert(te->args.size() >= 1);
std::vector<TypeExpr*> new_vars;
for (std::size_t i = 1; i < te->args.size(); i++) {
new_vars.push_back(new_hole(1));
}
TypeExpr* te2 = te;
// std::cerr << "removing universal quantifier in " << te << std::endl;
te = te->args[0];
remove_forall_in(te, te2, new_vars);
// std::cerr << "-> " << te << std::endl;
return true;
}
bool TypeExpr::remove_forall_in(TypeExpr*& te, TypeExpr* te2, const std::vector<TypeExpr*>& new_vars) {
assert(te);
assert(te2 && te2->constr == te_ForAll);
if (te->constr == te_Var) {
for (std::size_t i = 0; i < new_vars.size(); i++) {
if (te == te2->args[i + 1]) {
te = new_vars[i];
return true;
}
}
return false;
}
if (te->constr == te_ForAll) {
return false;
}
if (te->args.empty()) {
return false;
}
auto te1 = new TypeExpr(*te);
bool res = false;
for (auto& arg : te1->args) {
res |= remove_forall_in(arg, te2, new_vars);
}
if (res) {
te = te1;
} else {
delete te1;
}
return res;
}
void TypeExpr::show_width(std::ostream& os) {
os << minw;
if (maxw != minw) {
os << "..";
if (maxw < w_inf) {
os << maxw;
}
}
}
std::ostream& operator<<(std::ostream& os, TypeExpr* type_expr) {
if (!type_expr) {
return os << "(null-type-ptr)";
}
return type_expr->print(os);
}
std::ostream& TypeExpr::print(std::ostream& os, int lex_level) {
switch (constr) {
case te_Unknown:
return os << "??" << value;
case te_Var:
if (value >= -26 && value < 0) {
return os << "_" << (char)(91 + value);
} else if (value >= 0 && value < 26) {
return os << (char)(65 + value);
} else {
return os << "TVAR" << value;
}
case te_Indirect:
return os << args[0];
case te_Atomic: {
switch (value) {
case _Int:
return os << "int";
case _Cell:
return os << "cell";
case _Slice:
return os << "slice";
case _Builder:
return os << "builder";
case _Cont:
return os << "cont";
case _Tuple:
return os << "tuple";
case _Type:
return os << "type";
default:
return os << "atomic-type-" << value;
}
}
case te_Tensor: {
if (lex_level > -127) {
os << "(";
}
auto c = args.size();
if (c) {
for (const auto& x : args) {
x->print(os);
if (--c) {
os << ", ";
}
}
}
if (lex_level > -127) {
os << ")";
}
return os;
}
case te_Tuple: {
os << "[";
auto c = args.size();
if (c == 1 && args[0]->constr == te_Tensor) {
args[0]->print(os, -127);
} else if (c) {
for (const auto& x : args) {
x->print(os);
if (--c) {
os << ", ";
}
}
}
return os << "]";
}
case te_Map: {
assert(args.size() == 2);
if (lex_level > 0) {
os << "(";
}
args[0]->print(os, 1);
os << " -> ";
args[1]->print(os);
if (lex_level > 0) {
os << ")";
}
return os;
}
case te_ForAll: {
assert(args.size() >= 1);
if (lex_level > 0) {
os << '(';
}
os << "Forall ";
for (std::size_t i = 1; i < args.size(); i++) {
os << (i > 1 ? ' ' : '(');
args[i]->print(os);
}
os << ") ";
args[0]->print(os);
if (lex_level > 0) {
os << ')';
}
return os;
}
default:
return os << "unknown-type-expr-" << constr;
}
}
void UnifyError::print_message(std::ostream& os) const {
os << "cannot unify type " << te1 << " with " << te2;
if (!msg.empty()) {
os << ": " << msg;
}
}
std::ostream& operator<<(std::ostream& os, const UnifyError& ue) {
ue.print_message(os);
return os;
}
std::string UnifyError::message() const {
std::ostringstream os;
UnifyError::print_message(os);
return os.str();
}
void check_width_compat(TypeExpr* te1, TypeExpr* te2) {
if (te1->minw > te2->maxw || te2->minw > te1->maxw) {
std::ostringstream os{"cannot unify types of widths ", std::ios_base::ate};
te1->show_width(os);
os << " and ";
te2->show_width(os);
throw UnifyError{te1, te2, os.str()};
}
}
void check_update_widths(TypeExpr* te1, TypeExpr* te2) {
check_width_compat(te1, te2);
te1->minw = te2->minw = std::max(te1->minw, te2->minw);
te1->maxw = te2->maxw = std::min(te1->maxw, te2->maxw);
assert(te1->minw <= te1->maxw);
}
void unify(TypeExpr*& te1, TypeExpr*& te2) {
assert(te1 && te2);
// std::cerr << "unify( " << te1 << " , " << te2 << " )\n";
while (te1->constr == TypeExpr::te_Indirect) {
te1 = te1->args[0];
}
while (te2->constr == TypeExpr::te_Indirect) {
te2 = te2->args[0];
}
if (te1 == te2) {
return;
}
if (te1->constr == TypeExpr::te_ForAll) {
TypeExpr* te = te1;
if (!TypeExpr::remove_forall(te)) {
throw UnifyError{te1, te2, "cannot remove universal type quantifier while performing type unification"};
}
unify(te, te2);
return;
}
if (te2->constr == TypeExpr::te_ForAll) {
TypeExpr* te = te2;
if (!TypeExpr::remove_forall(te)) {
throw UnifyError{te2, te1, "cannot remove universal type quantifier while performing type unification"};
}
unify(te1, te);
return;
}
if (te1->constr == TypeExpr::te_Unknown) {
if (te2->constr == TypeExpr::te_Unknown) {
assert(te1->value != te2->value);
}
if (!TypeExpr::remove_indirect(te2, te1)) {
throw UnifyError{te1, te2, "type unification results in an infinite cyclic type"};
}
check_update_widths(te1, te2);
te1->replace_with(te2);
te1 = te2;
return;
}
if (te2->constr == TypeExpr::te_Unknown) {
if (!TypeExpr::remove_indirect(te1, te2)) {
throw UnifyError{te2, te1, "type unification results in an infinite cyclic type"};
}
check_update_widths(te2, te1);
te2->replace_with(te1);
te2 = te1;
return;
}
if (te1->constr != te2->constr || te1->value != te2->value || te1->args.size() != te2->args.size()) {
throw UnifyError{te1, te2};
}
for (std::size_t i = 0; i < te1->args.size(); i++) {
unify(te1->args[i], te2->args[i]);
}
if (te1->constr == TypeExpr::te_Tensor) {
if (!te1->recompute_width()) {
throw UnifyError{te1, te2, "type unification incompatible with known width of first type"};
}
if (!te2->recompute_width()) {
throw UnifyError{te2, te1, "type unification incompatible with known width of first type"};
}
check_update_widths(te1, te2);
}
te1->replace_with(te2);
te1 = te2;
}
} // namespace funC