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
|
//:: Like container definitions, recipes too can contain type parameters.
:(scenario generic_recipe)
recipe main [
10:point <- merge 14, 15
11:point <- foo 10:point
]
# non-matching variant
recipe foo a:number -> result:number [
local-scope
load-ingredients
result <- copy 34
]
# matching generic variant
recipe foo a:_t -> result:_t [
local-scope
load-ingredients
result <- copy a
]
+mem: storing 14 in location 11
+mem: storing 15 in location 12
//: Before anything else, disable transforms for generic recipes.
:(before "End Transform Checks")
if (any_type_ingredient_in_header(/*recipe_ordinal*/p->first)) continue;
//: We'll be creating recipes without loading them from anywhere by
//: *specializing* existing recipes, so make sure we don't clear any of those
//: when we start running tests.
:(before "End Loading .mu Files")
recently_added_recipes.clear();
recently_added_types.clear();
:(before "End Instruction Dispatch(inst, best_score)")
if (best_score == -1) {
trace(9992, "transform") << "no variant found; searching for variant with suitable type ingredients" << end();
recipe_ordinal exemplar = pick_matching_generic_variant(variants, inst, best_score);
if (exemplar) {
trace(9992, "transform") << "found variant to specialize: " << exemplar << ' ' << get(Recipe, exemplar).name << end();
variants.push_back(new_variant(exemplar, inst));
inst.name = get(Recipe, variants.back()).name;
trace(9992, "transform") << "new specialization: " << inst.name << end();
}
}
:(code)
recipe_ordinal pick_matching_generic_variant(vector<recipe_ordinal>& variants, const instruction& inst, long long int& best_score) {
recipe_ordinal result = 0;
for (long long int i = 0; i < SIZE(variants); ++i) {
if (variants.at(i) == -1) continue; // ghost from a previous test
trace(9992, "transform") << "checking generic variant " << i << end();
long long int current_score = generic_variant_score(inst, variants.at(i));
trace(9992, "transform") << "final score: " << current_score << end();
if (current_score > best_score) {
trace(9992, "transform") << "matches" << end();
result = variants.at(i);
best_score = current_score;
}
}
return result;
}
long long int generic_variant_score(const instruction& inst, recipe_ordinal variant) {
if (!any_type_ingredient_in_header(variant)) {
trace(9993, "transform") << "no type ingredients" << end();
return -1;
}
const vector<reagent>& header_ingredients = get(Recipe, variant).ingredients;
if (SIZE(inst.ingredients) < SIZE(header_ingredients)) {
trace(9993, "transform") << "too few ingredients" << end();
return -1;
}
for (long long int i = 0; i < SIZE(header_ingredients); ++i) {
if (!non_type_ingredients_match(header_ingredients.at(i), inst.ingredients.at(i))) {
trace(9993, "transform") << "mismatch: ingredient " << i << end();
return -1;
}
}
if (SIZE(inst.products) > SIZE(get(Recipe, variant).products)) {
trace(9993, "transform") << "too few products" << end();
return -1;
}
const vector<reagent>& header_products = get(Recipe, variant).products;
for (long long int i = 0; i < SIZE(inst.products); ++i) {
if (!non_type_ingredients_match(header_products.at(i), inst.products.at(i))) {
trace(9993, "transform") << "mismatch: product " << i << end();
return -1;
}
}
// the greater the number of unused ingredients, the lower the score
return 100 - (SIZE(get(Recipe, variant).products)-SIZE(inst.products))
- (SIZE(inst.ingredients)-SIZE(get(Recipe, variant).ingredients)); // ok to go negative
}
bool any_type_ingredient_in_header(recipe_ordinal variant) {
for (long long int i = 0; i < SIZE(get(Recipe, variant).ingredients); ++i) {
if (contains_type_ingredient_name(get(Recipe, variant).ingredients.at(i)))
return true;
}
return false;
}
bool non_type_ingredients_match(const reagent& lhs, const reagent& rhs) {
if (contains_type_ingredient_name(lhs)) return true;
return types_match(lhs, rhs);
}
bool contains_type_ingredient_name(const reagent& x) {
return contains_type_ingredient_name(x.properties.at(0).second);
}
bool contains_type_ingredient_name(const string_tree* type) {
if (!type) return false;
if (is_type_ingredient_name(type->value)) return true;
return contains_type_ingredient_name(type->left) || contains_type_ingredient_name(type->right);
}
bool is_type_ingredient_name(const string& type) {
return !type.empty() && type.at(0) == '_';
}
recipe_ordinal new_variant(recipe_ordinal exemplar, const instruction& inst) {
string new_name = next_unused_recipe_name(inst.name);
trace(9993, "transform") << "switching " << inst.name << " to " << new_name << end();
assert(!contains_key(Recipe_ordinal, new_name));
recipe_ordinal new_recipe_ordinal = put(Recipe_ordinal, new_name, Next_recipe_ordinal++);
// make a copy
assert(contains_key(Recipe, exemplar));
assert(!contains_key(Recipe, new_recipe_ordinal));
recently_added_recipes.push_back(new_recipe_ordinal);
put(Recipe, new_recipe_ordinal, get(Recipe, exemplar));
recipe& new_recipe = get(Recipe, new_recipe_ordinal);
// Since the exemplar never ran any transforms, we have to redo some of the
// work of the check_types_by_name transform while supporting type-ingredients.
compute_type_names(new_recipe);
// that gives enough information to replace type-ingredients with concrete types
new_recipe.name = new_name;
map<string, string> mappings;
compute_type_ingredient_mappings(get(Recipe, exemplar), inst, mappings);
replace_type_ingredients(new_recipe, mappings);
ensure_all_concrete_types(new_recipe);
// finally, perform all transforms on the new specialization
for (long long int t = 0; t < SIZE(Transform); ++t) {
(*Transform.at(t))(new_recipe_ordinal);
}
new_recipe.transformed_until = SIZE(Transform)-1;
return new_recipe_ordinal;
}
void compute_type_names(recipe& variant) {
map<string, string_tree*> type_names;
for (long long int i = 0; i < SIZE(variant.ingredients); ++i) {
save_or_deduce_type_name(variant.ingredients.at(i), type_names);
}
for (long long int i = 0; i < SIZE(variant.products); ++i) {
save_or_deduce_type_name(variant.products.at(i), type_names);
}
for (long long int i = 0; i < SIZE(variant.steps); ++i) {
instruction& inst = variant.steps.at(i);
for (long long int in = 0; in < SIZE(inst.ingredients); ++in) {
save_or_deduce_type_name(inst.ingredients.at(in), type_names);
}
for (long long int out = 0; out < SIZE(inst.products); ++out) {
save_or_deduce_type_name(inst.products.at(out), type_names);
}
}
}
void save_or_deduce_type_name(reagent& x, map<string, string_tree*>& type_name) {
if (!x.properties.at(0).second && contains_key(type_name, x.name)) {
x.properties.at(0).second = new string_tree(*get(type_name, x.name));
return;
}
if (!x.properties.at(0).second) {
raise << "unknown type for " << x.original_string << '\n' << end();
return;
}
if (contains_key(type_name, x.name)) return;
if (x.properties.at(0).second->value == "offset" || x.properties.at(0).second->value == "variant") return; // special-case for container-access instructions
put(type_name, x.name, x.properties.at(0).second);
ostringstream type_name_buf;
dump_property(x.properties.at(0).second, type_name_buf);
trace(9993, "transform") << "type of " << x.name << " is " << type_name_buf.str() << end();
}
void compute_type_ingredient_mappings(const recipe& exemplar, const instruction& inst, map<string, string>& mappings) {
for (long long int i = 0; i < SIZE(exemplar.ingredients); ++i) {
const reagent& base = exemplar.ingredients.at(i);
reagent ingredient = inst.ingredients.at(i);
assert(ingredient.properties.at(0).second);
canonize_type(ingredient);
accumulate_type_ingredients(base, ingredient, mappings, exemplar);
}
for (long long int i = 0; i < SIZE(exemplar.products); ++i) {
const reagent& base = exemplar.products.at(i);
reagent product = inst.products.at(i);
assert(product.properties.at(0).second);
canonize_type(product);
accumulate_type_ingredients(base, product, mappings, exemplar);
}
}
void accumulate_type_ingredients(const reagent& base, reagent& refinement, map<string, string>& mappings, const recipe& exemplar) {
assert(refinement.properties.at(0).second);
accumulate_type_ingredients(base.properties.at(0).second, refinement.properties.at(0).second, mappings, exemplar, base);
}
void accumulate_type_ingredients(const string_tree* base, const string_tree* refinement, map<string, string>& mappings, const recipe& exemplar, const reagent& r) {
if (!base) return;
if (!refinement) {
raise_error << maybe(exemplar.name) << "missing type ingredient in " << r.original_string << '\n' << end();
return;
}
if (!base->value.empty() && base->value.at(0) == '_') {
assert(!refinement->value.empty());
if (!contains_key(mappings, base->value)) {
trace(9993, "transform") << "adding mapping from " << base->value << " to " << refinement->value << end();
put(mappings, base->value, refinement->value);
}
else {
assert(get(mappings, base->value) == refinement->value);
}
}
else {
accumulate_type_ingredients(base->left, refinement->left, mappings, exemplar, r);
}
accumulate_type_ingredients(base->right, refinement->right, mappings, exemplar, r);
}
void replace_type_ingredients(recipe& new_recipe, const map<string, string>& mappings) {
// update its header
if (mappings.empty()) return;
trace(9993, "transform") << "replacing in recipe header ingredients" << end();
for (long long int i = 0; i < SIZE(new_recipe.ingredients); ++i) {
replace_type_ingredients(new_recipe.ingredients.at(i), mappings);
}
trace(9993, "transform") << "replacing in recipe header products" << end();
for (long long int i = 0; i < SIZE(new_recipe.products); ++i) {
replace_type_ingredients(new_recipe.products.at(i), mappings);
}
// update its body
for (long long int i = 0; i < SIZE(new_recipe.steps); ++i) {
instruction& inst = new_recipe.steps.at(i);
trace(9993, "transform") << "replacing in instruction '" << inst.to_string() << "'" << end();
for (long long int j = 0; j < SIZE(inst.ingredients); ++j) {
replace_type_ingredients(inst.ingredients.at(j), mappings);
}
for (long long int j = 0; j < SIZE(inst.products); ++j) {
replace_type_ingredients(inst.products.at(j), mappings);
}
// special-case for new: replace type ingredient in first ingredient *value*
if (inst.name == "new" && inst.ingredients.at(0).name.at(0) != '[') {
string_tree* type_name = parse_string_tree(inst.ingredients.at(0).name);
replace_type_ingredients(type_name, mappings);
inst.ingredients.at(0).name = simple_string(type_name);
delete type_name;
}
}
}
string simple_string(string_tree* x) {
ostringstream out;
simple_string(x, out);
return out.str();
}
void simple_string(string_tree* x, ostream& out) {
if (!x->left && !x->right) {
out << x->value;
return;
}
out << '(';
for (string_tree* curr = x; curr; curr = curr->right) {
if (curr != x) out << ' ';
if (curr->left)
simple_string(curr->left, out);
else
out << curr->value;
}
out << ')';
}
void replace_type_ingredients(reagent& x, const map<string, string>& mappings) {
trace(9993, "transform") << "replacing in ingredient " << x.original_string << end();
// replace properties
assert(x.properties.at(0).second);
replace_type_ingredients(x.properties.at(0).second, mappings);
// refresh types from properties
delete x.type;
x.type = new_type_tree(x.properties.at(0).second);
if (x.type)
trace(9993, "transform") << " after: " << dump_types(x) << end();
}
void replace_type_ingredients(string_tree* type, const map<string, string>& mappings) {
if (!type) return;
if (is_type_ingredient_name(type->value) && contains_key(mappings, type->value)) {
trace(9993, "transform") << type->value << " => " << mappings.find(type->value)->second << end();
type->value = mappings.find(type->value)->second;
}
replace_type_ingredients(type->left, mappings);
replace_type_ingredients(type->right, mappings);
}
void ensure_all_concrete_types(const recipe& new_recipe) {
for (long long int i = 0; i < SIZE(new_recipe.ingredients); ++i) {
ensure_all_concrete_types(new_recipe.ingredients.at(i).type);
}
for (long long int i = 0; i < SIZE(new_recipe.products); ++i) {
ensure_all_concrete_types(new_recipe.products.at(i).type);
}
for (long long int i = 0; i < SIZE(new_recipe.steps); ++i) {
const instruction& inst = new_recipe.steps.at(i);
for (long long int j = 0; j < SIZE(inst.ingredients); ++j) {
ensure_all_concrete_types(inst.ingredients.at(j).type);
}
for (long long int j = 0; j < SIZE(inst.products); ++j) {
ensure_all_concrete_types(inst.products.at(j).type);
}
}
}
void ensure_all_concrete_types(const type_tree* x) {
if (!x) {
raise << "AAA null type\n" << end();
return;
}
if (x->value == -1) {
raise << "AAA unknown type\n" << end();
return;
}
}
:(scenario generic_recipe_2)
recipe main [
10:point <- merge 14, 15
11:point <- foo 10:point
]
# non-matching generic variant
recipe foo a:_t, b:_t -> result:number [
local-scope
load-ingredients
result <- copy 34
]
# matching generic variant
recipe foo a:_t -> result:_t [
local-scope
load-ingredients
result <- copy a
]
+mem: storing 14 in location 11
+mem: storing 15 in location 12
:(scenario generic_recipe_nonroot)
% Hide_warnings = Hide_errors = true;
recipe main [
10:foo:point <- merge 14, 15, 16
20:point/raw <- bar 10:foo:point
]
# generic recipe with type ingredient following some other type
recipe bar a:foo:_t -> result:_t [
local-scope
load-ingredients
result <- get a, x:offset
]
container foo:_t [
x:_t
y:number
]
+mem: storing 14 in location 20
+mem: storing 15 in location 21
|