1 //: Transform to maintain multiple variants of a recipe depending on the
  2 //: number and types of the ingredients and products. Allows us to use nice
  3 //: names like 'print' or 'length' in many mutually extensible ways.
  4 
  5 :(scenario static_dispatch)
  6 def main [
  7   7:num/raw <- test 3
  8 ]
  9 def test a:num -> z:num [
 10   z <- copy 1
 11 ]
 12 def test a:num, b:num -> z:num [
 13   z <- copy 2
 14 ]
 15 +mem: storing 1 in location 7
 16 
 17 //: When loading recipes, accumulate variants if headers don't collide, and
 18 //: flag an error if headers collide.
 19 
 20 :(before "End Globals")
 21 map<string, vector<recipe_ordinal> > Recipe_variants;
 22 :(before "End One-time Setup")
 23 put(Recipe_variants, "main", vector<recipe_ordinal>());  // since we manually added main to Recipe_ordinal
 24 
 25 :(before "End Globals")
 26 map<string, vector<recipe_ordinal> > Recipe_variants_snapshot;
 27 :(before "End save_snapshots")
 28 Recipe_variants_snapshot = Recipe_variants;
 29 :(before "End restore_snapshots")
 30 Recipe_variants = Recipe_variants_snapshot;
 31 
 32 :(before "End Load Recipe Header(result)")
 33 // there can only ever be one variant for main
 34 if (result.name != "main" && contains_key(Recipe_ordinal, result.name)) {
 35   const recipe_ordinal r = get(Recipe_ordinal, result.name);
 36   if (!contains_key(Recipe, r) || get(Recipe, r).has_header) {
 37   ¦ string new_name = matching_variant_name(result);
 38   ¦ if (new_name.empty()) {
 39   ¦ ¦ // variant doesn't already exist
 40   ¦ ¦ new_name = next_unused_recipe_name(result.name);
 41   ¦ ¦ put(Recipe_ordinal, new_name, Next_recipe_ordinal++);
 42   ¦ ¦ get_or_insert(Recipe_variants, result.name).push_back(get(Recipe_ordinal, new_name));
 43   ¦ }
 44   ¦ trace(9999, "load") << "switching " << result.name << " to " << new_name << end();
 45   ¦ result.name = new_name;
 46   ¦ result.is_autogenerated = true;
 47   }
 48 }
 49 else {
 50   // save first variant
 51   put(Recipe_ordinal, result.name, Next_recipe_ordinal++);
 52   get_or_insert(Recipe_variants, result.name).push_back(get(Recipe_ordinal, result.name));
 53 }
 54 
 55 :(code)
 56 string matching_variant_name(const recipe& rr) {
 57   const vector<recipe_ordinal>& variants = get_or_insert(Recipe_variants, rr.name);
 58   for (int i = 0;  i < SIZE(variants);  ++i) {
 59   ¦ if (!contains_key(Recipe, variants.at(i))) continue;
 60   ¦ const recipe& candidate = get(Recipe, variants.at(i));
 61   ¦ if (!all_reagents_match(rr, candidate)) continue;
 62   ¦ return candidate.name;
 63   }
 64   return "";
 65 }
 66 
 67 bool all_reagents_match(const recipe& r1, const recipe& r2) {
 68   if (SIZE(r1.ingredients) != SIZE(r2.ingredients)) return false;
 69   if (SIZE(r1.products) != SIZE(r2.products)) return false;
 70   for (int i = 0;  i < SIZE(r1.ingredients);  ++i) {
 71   ¦ expand_type_abbreviations(r1.ingredients.at(i).type);
 72   ¦ expand_type_abbreviations(r2.ingredients.at(i).type);
 73   ¦ if (!deeply_equal_type_names(r1.ingredients.at(i), r2.ingredients.at(i)))
 74   ¦ ¦ return false;
 75   }
 76   for (int i = 0;  i < SIZE(r1.products);  ++i) {
 77   ¦ expand_type_abbreviations(r1.products.at(i).type);
 78   ¦ expand_type_abbreviations(r2.products.at(i).type);
 79   ¦ if (!deeply_equal_type_names(r1.products.at(i), r2.products.at(i)))
 80   ¦ ¦ return false;
 81   }
 82   return true;
 83 }
 84 
 85 :(before "End Globals")
 86 set<string> Literal_type_names;
 87 :(before "End One-time Setup")
 88 Literal_type_names.insert("number");
 89 Literal_type_names.insert("character");
 90 :(code)
 91 bool deeply_equal_type_names(const reagent& a, const reagent& b) {
 92   return deeply_equal_type_names(a.type, b.type);
 93 }
 94 bool deeply_equal_type_names(const type_tree* a, const type_tree* b) {
 95   if (!a) return !b;
 96   if (!b) return !a;
 97   if (a->atom != b->atom) return false;
 98   if (a->atom) {
 99   ¦ if (a->name == "literal" && b->name == "literal")
100   ¦ ¦ return true;
101   ¦ if (a->name == "literal")
102   ¦ ¦ return Literal_type_names.find(b->name) != Literal_type_names.end();
103   ¦ if (b->name == "literal")
104   ¦ ¦ return Literal_type_names.find(a->name) != Literal_type_names.end();
105   ¦ return a->name == b->name;
106   }
107   return deeply_equal_type_names(a->left, b->left)
108   ¦ ¦ && deeply_equal_type_names(a->right, b->right);
109 }
110 
111 string next_unused_recipe_name(const string& recipe_name) {
112   for (int i = 2;  /*forever*/;  ++i) {
113   ¦ ostringstream out;
114   ¦ out << recipe_name << '_' << i;
115   ¦ if (!contains_key(Recipe_ordinal, out.str()))
116   ¦ ¦ return out.str();
117   }
118 }
119 
120 //: Once all the recipes are loaded, transform their bodies to replace each
121 //: call with the most suitable variant.
122 
123 :(scenario static_dispatch_picks_most_similar_variant)
124 def main [
125   7:num/raw <- test 3, 4, 5
126 ]
127 def test a:num -> z:num [
128   z <- copy 1
129 ]
130 def test a:num, b:num -> z:num [
131   z <- copy 2
132 ]
133 +mem: storing 2 in location 7
134 
135 //: support recipe headers in a previous transform to fill in missing types
136 :(before "End check_or_set_invalid_types")
137 for (int i = 0;  i < SIZE(caller.ingredients);  ++i)
138   check_or_set_invalid_types(caller.ingredients.at(i).type, maybe(caller.name), "recipe header ingredient");
139 for (int i = 0;  i < SIZE(caller.products);  ++i)
140   check_or_set_invalid_types(caller.products.at(i).type, maybe(caller.name), "recipe header product");
141 
142 //: after filling in all missing types (because we'll be introducing 'blank' types in this transform in a later layer, for shape-shifting recipes)
143 :(after "Transform.push_back(transform_names)")
144 Transform.push_back(resolve_ambiguous_calls);  // idempotent
145 
146 //: In a later layer we'll introduce recursion in resolve_ambiguous_calls, by
147 //: having it generate code for shape-shifting recipes and then transform such
148 //: code. This data structure will help error messages be more useful.
149 //:
150 //: We're punning the 'call' data structure just because it has slots for
151 //: calling recipe and calling instruction.
152 :(before "End Globals")
153 list<call> Resolve_stack;
154 
155 :(code)
156 void resolve_ambiguous_calls(const recipe_ordinal r) {
157   recipe& caller_recipe = get(Recipe, r);
158   trace(9991, "transform") << "--- resolve ambiguous calls for recipe " << caller_recipe.name << end();
159   for (int index = 0;  index < SIZE(caller_recipe.steps);  ++index) {
160   ¦ instruction& inst = caller_recipe.steps.at(index);
161   ¦ if (inst.is_label) continue;
162   ¦ if (non_ghost_size(get_or_insert(Recipe_variants, inst.name)) == 0) continue;
163   ¦ trace(9992, "transform") << "instruction " << inst.original_string << end();
164   ¦ Resolve_stack.push_front(call(r));
165   ¦ Resolve_stack.front().running_step_index = index;
166   ¦ string new_name = best_variant(inst, caller_recipe);
167   ¦ if (!new_name.empty())
168   ¦ ¦ inst.name = new_name;
169   ¦ assert(Resolve_stack.front().running_recipe == r);
170   ¦ assert(Resolve_stack.front().running_step_index == index);
171   ¦ Resolve_stack.pop_front();
172   }
173 }
174 
175 string best_variant(instruction& inst, const recipe& caller_recipe) {
176   vector<recipe_ordinal>& variants = get(Recipe_variants, inst.name);
177   vector<recipe_ordinal> candidates;
178 
179   // Static Dispatch Phase 1
180   candidates = strictly_matching_variants(inst, variants);
181   if (!candidates.empty()) return best_variant(inst, candidates).name;
182 
183   // Static Dispatch Phase 2
184   candidates = strictly_matching_variants_except_literal_against_address_or_boolean(inst, variants);
185   if (!candidates.empty()) return best_variant(inst, candidates).name;
186 
187   // Static Dispatch Phase 3
188   //: (shape-shifting recipes in a later layer)
189   // End Static Dispatch Phase 3
190 
191   // Static Dispatch Phase 4
192   candidates = matching_variants(inst, variants);
193   if (!candidates.empty()) return best_variant(inst, candidates).name;
194 
195   // error messages
196   if (get(Recipe_ordinal, inst.name) >= MAX_PRIMITIVE_RECIPES) {  // we currently don't check types for primitive variants
197   ¦ raise << maybe(caller_recipe.name) << "failed to find a matching call for '" << inst.original_string << "'\n" << end();
198   ¦ for (list<call>::iterator p = /*skip*/++Resolve_stack.begin();  p != Resolve_stack.end();  ++p) {
199   ¦ ¦ const recipe& specializer_recipe = get(Recipe, p->running_recipe);
200   ¦ ¦ const instruction& specializer_inst = specializer_recipe.steps.at(p->running_step_index);
201   ¦ ¦ if (specializer_recipe.name != "interactive")
202   ¦ ¦ ¦ raise << "  (from '" << to_original_string(specializer_inst) << "' in " << specializer_recipe.name << ")\n" << end();
203   ¦ ¦ else
204   ¦ ¦ ¦ raise << "  (from '" << to_original_string(specializer_inst) << "')\n" << end();
205   ¦ ¦ // One special-case to help with the rewrite_stash transform. (cross-layer)
206   ¦ ¦ if (specializer_inst.products.at(0).name.find("stash_") == 0) {
207   ¦ ¦ ¦ instruction stash_inst;
208   ¦ ¦ ¦ if (next_stash(*p, &stash_inst)) {
209   ¦ ¦ ¦ ¦ if (specializer_recipe.name != "interactive")
210   ¦ ¦ ¦ ¦ ¦ raise << "  (part of '" << stash_inst.original_string << "' in " << specializer_recipe.name << ")\n" << end();
211   ¦ ¦ ¦ ¦ else
212   ¦ ¦ ¦ ¦ ¦ raise << "  (part of '" << stash_inst.original_string << "')\n" << end();
213   ¦ ¦ ¦ }
214   ¦ ¦ }
215   ¦ }
216   }
217   return "";
218 }
219 
220 // phase 1
221 vector<recipe_ordinal> strictly_matching_variants(const instruction& inst, vector<recipe_ordinal>& variants) {
222   vector<recipe_ordinal> result;
223   for (int i = 0;  i < SIZE(variants);  ++i) {
224   ¦ if (variants.at(i) == -1) continue;
225   ¦ trace(9992, "transform") << "checking variant (strict) " << i << ": " << header_label(variants.at(i)) << end();
226   ¦ if (all_header_reagents_strictly_match(inst, get(Recipe, variants.at(i))))
227   ¦ ¦ result.push_back(variants.at(i));
228   }
229   return result;
230 }
231 
232 bool all_header_reagents_strictly_match(const instruction& inst, const recipe& variant) {
233   for (int i = 0;  i < min(SIZE(inst.ingredients), SIZE(variant.ingredients));  ++i) {
234   ¦ if (!types_strictly_match(variant.ingredients.at(i), inst.ingredients.at(i))) {
235   ¦ ¦ trace(9993, "transform") << "strict match failed: ingredient " << i << end();
236   ¦ ¦ return false;
237   ¦ }
238   }
239   for (int i = 0;  i < min(SIZE(inst.products), SIZE(variant.products));  ++i) {
240   ¦ if (is_dummy(inst.products.at(i))) continue;
241   ¦ if (!types_strictly_match(variant.products.at(i), inst.products.at(i))) {
242   ¦ ¦ trace(9993, "transform") << "strict match failed: product " << i << end();
243   ¦ ¦ return false;
244   ¦ }
245   }
246   return true;
247 }
248 
249 // phase 2
250 vector<recipe_ordinal> strictly_matching_variants_except_literal_against_address_or_boolean(const instruction& inst, vector<recipe_ordinal>& variants) {
251   vector<recipe_ordinal> result;
252   for (int i = 0;  i < SIZE(variants);  ++i) {
253   ¦ if (variants.at(i) == -1) continue;
254   ¦ trace(9992, "transform") << "checking variant (strict except literal-against-boolean) " << i << ": " << header_label(variants.at(i)) << end();
255   ¦ if (all_header_reagents_strictly_match_except_literal_against_address_or_boolean(inst, get(Recipe, variants.at(i))))
256   ¦ ¦ result.push_back(variants.at(i));
257   }
258   return result;
259 }
260 
261 bool all_header_reagents_strictly_match_except_literal_against_address_or_boolean(const instruction& inst, const recipe& variant) {
262   for (int i = 0;  i < min(SIZE(inst.ingredients), SIZE(variant.ingredients));  ++i) {
263   ¦ if (!types_strictly_match_except_literal_against_address_or_boolean(variant.ingredients.at(i), inst.ingredients.at(i))) {
264   ¦ ¦ trace(9993, "transform") << "match failed: ingredient " << i << end();
265   ¦ ¦ return false;
266   ¦ }
267   }
268   for (int i = 0;  i < min(SIZE(variant.products), SIZE(inst.products));  ++i) {
269   ¦ if (is_dummy(inst.products.at(i))) continue;
270   ¦ if (!types_strictly_match_except_literal_against_address_or_boolean(variant.products.at(i), inst.products.at(i))) {
271   ¦ ¦ trace(9993, "transform") << "match failed: product " << i << end();
272   ¦ ¦ return false;
273   ¦ }
274   }
275   return true;
276 }
277 
278 bool types_strictly_match_except_literal_against_address_or_boolean(const reagent& to, const reagent& from) {
279   if (is_literal(from) && is_mu_boolean(to))
280   ¦ return from.name == "0" || from.name == "1";
281   // Match Literal Zero Against Address {
282   if (is_literal(from) && is_mu_address(to))
283   ¦ return from.name == "0";
284   // }
285   return types_strictly_match(to, from);
286 }
287 
288 // phase 4
289 vector<recipe_ordinal> matching_variants(const instruction& inst, vector<recipe_ordinal>& variants) {
290   vector<recipe_ordinal> result;
291   for (int i = 0;  i < SIZE(variants);  ++i) {
292   ¦ if (variants.at(i) == -1) continue;
293   ¦ trace(9992, "transform") << "checking variant " << i << ": " << header_label(variants.at(i)) << end();
294   ¦ if (all_header_reagents_match(inst, get(Recipe, variants.at(i))))
295   ¦ ¦ result.push_back(variants.at(i));
296   }
297   return result;
298 }
299 
300 bool all_header_reagents_match(const instruction& inst, const recipe& variant) {
301   for (int i = 0;  i < min(SIZE(inst.ingredients), SIZE(variant.ingredients));  ++i) {
302   ¦ if (!types_match(variant.ingredients.at(i), inst.ingredients.at(i))) {
303   ¦ ¦ trace(9993, "transform") << "match failed: ingredient " << i << end();
304   ¦ ¦ return false;
305   ¦ }
306   }
307   for (int i = 0;  i < min(SIZE(variant.products), SIZE(inst.products));  ++i) {
308   ¦ if (is_dummy(inst.products.at(i))) continue;
309   ¦ if (!types_match(variant.products.at(i), inst.products.at(i))) {
310   ¦ ¦ trace(9993, "transform") << "match failed: product " << i << end();
311   ¦ ¦ return false;
312   ¦ }
313   }
314   return true;
315 }
316 
317 // tie-breaker for each phase
318 const recipe& best_variant(const instruction& inst, vector<recipe_ordinal>& candidates) {
319   assert(!candidates.empty());
320   int min_score = 999;
321   int min_index = 0;
322   for (int i = 0;  i < SIZE(candidates);  ++i) {
323   ¦ const recipe& candidate = get(Recipe, candidates.at(i));
324   ¦ int score = abs(SIZE(candidate.products)-SIZE(inst.products))
325   ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦     + abs(SIZE(candidate.ingredients)-SIZE(inst.ingredients));
326   ¦ assert(score < 999);
327   ¦ if (score < min_score) {
328   ¦ ¦ min_score = score;
329   ¦ ¦ min_index = i;
330   ¦ }
331   }
332   return get(Recipe, candidates.at(min_index));
333 }
334 
335 int non_ghost_size(vector<recipe_ordinal>& variants) {
336   int result = 0;
337   for (int i = 0;  i < SIZE(variants);  ++i)
338   ¦ if (variants.at(i) != -1) ++result;
339   return result;
340 }
341 
342 bool next_stash(const call& c, instruction* stash_inst) {
343   const recipe& specializer_recipe = get(Recipe, c.running_recipe);
344   int index = c.running_step_index;
345   for (++index;  index < SIZE(specializer_recipe.steps);  ++index) {
346   ¦ const instruction& inst = specializer_recipe.steps.at(index);
347   ¦ if (inst.name == "stash") {
348   ¦ ¦ *stash_inst = inst;
349   ¦ ¦ return true;
350   ¦ }
351   }
352   return false;
353 }
354 
355 :(scenario static_dispatch_disabled_in_recipe_without_variants)
356 def main [
357   1:num <- test 3
358 ]
359 def test [
360   2:num <- next-ingredient  # ensure no header
361   return 34
362 ]
363 +mem: storing 34 in location 1
364 
365 :(scenario static_dispatch_disabled_on_headerless_definition)
366 % Hide_errors = true;
367 def test a:num -> z:num [
368   z <- copy 1
369 ]
370 def test [
371   return 34
372 ]
373 +error: redefining recipe test
374 
375 :(scenario static_dispatch_disabled_on_headerless_definition_2)
376 % Hide_errors = true;
377 def test [
378   return 34
379 ]
380 def test a:num -> z:num [
381   z <- copy 1
382 ]
383 +error: redefining recipe test
384 
385 :(scenario static_dispatch_on_primitive_names)
386 def main [
387   1:num <- copy 34
388   2:num <- copy 34
389   3:bool <- equal 1:num, 2:num
390   4:bool <- copy 0/false
391   5:bool <- copy 0/false
392   6:bool <- equal 4:bool, 5:bool
393 ]
394 # temporarily hardcode number equality to always fail
395 def equal x:num, y:num -> z:bool [
396   local-scope
397   load-ingredients
398   z <- copy 0/false
399 ]
400 # comparing numbers used overload
401 +mem: storing 0 in location 3
402 # comparing booleans continues to use primitive
403 +mem: storing 1 in location 6
404 
405 :(scenario static_dispatch_works_with_dummy_results_for_containers)
406 def main [
407   _ <- test 3, 4
408 ]
409 def test a:num -> z:point [
410   local-scope
411   load-ingredients
412   z <- merge a, 0
413 ]
414 def test a:num, b:num -> z:point [
415   local-scope
416   load-ingredients
417   z <- merge a, b
418 ]
419 $error: 0
420 
421 :(scenario static_dispatch_works_with_compound_type_containing_container_defined_after_first_use)
422 def main [
423   x:&:foo <- new foo:type
424   test x
425 ]
426 container foo [
427   x:num
428 ]
429 def test a:&:foo -> z:num [
430   local-scope
431   load-ingredients
432   z:num <- get *a, x:offset
433 ]
434 $error: 0
435 
436 :(scenario static_dispatch_works_with_compound_type_containing_container_defined_after_second_use)
437 def main [
438   x:&:foo <- new foo:type
439   test x
440 ]
441 def test a:&:foo -> z:num [
442   local-scope
443   load-ingredients
444   z:num <- get *a, x:offset
445 ]
446 container foo [
447   x:num
448 ]
449 $error: 0
450 
451 :(scenario static_dispatch_prefers_literals_to_be_numbers_rather_than_addresses)
452 def main [
453   1:num <- foo 0
454 ]
455 def foo x:&:num -> y:num [
456   return 34
457 ]
458 def foo x:num -> y:num [
459   return 35
460 ]
461 +mem: storing 35 in location 1
462 
463 :(scenario static_dispatch_on_non_literal_character_ignores_variant_with_numbers)
464 % Hide_errors = true;
465 def main [
466   local-scope
467   x:char <- copy 10/newline
468   1:num/raw <- foo x
469 ]
470 def foo x:num -> y:num [
471   load-ingredients
472   return 34
473 ]
474 +error: main: ingredient 0 has the wrong type at '1:num/raw <- foo x'
475 -mem: storing 34 in location 1
476 
477 :(scenario static_dispatch_dispatches_literal_to_boolean_before_character)
478 def main [
479   1:num/raw <- foo 0  # valid literal for boolean
480 ]
481 def foo x:char -> y:num [
482   local-scope
483   load-ingredients
484   return 34
485 ]
486 def foo x:bool -> y:num [
487   local-scope
488   load-ingredients
489   return 35
490 ]
491 # boolean variant is preferred
492 +mem: storing 35 in location 1
493 
494 :(scenario static_dispatch_dispatches_literal_to_character_when_out_of_boolean_range)
495 def main [
496   1:num/raw <- foo 97  # not a valid literal for boolean
497 ]
498 def foo x:char -> y:num [
499   local-scope
500   load-ingredients
501   return 34
502 ]
503 def foo x:bool -> y:num [
504   local-scope
505   load-ingredients
506   return 35
507 ]
508 # character variant is preferred
509 +mem: storing 34 in location 1
510 
511 :(scenario static_dispatch_dispatches_literal_to_number_if_at_all_possible)
512 def main [
513   1:num/raw <- foo 97
514 ]
515 def foo x:char -> y:num [
516   local-scope
517   load-ingredients
518   return 34
519 ]
520 def foo x:num -> y:num [
521   local-scope
522   load-ingredients
523   return 35
524 ]
525 # number variant is preferred
526 +mem: storing 35 in location 1
527 
528 :(code)
529 string header_label(const recipe_ordinal r) {
530   return header_label(get(Recipe, r));
531 }
532 string header_label(const recipe& caller) {
533   ostringstream out;
534   out << "recipe " << caller.name;
535   for (int i = 0;  i < SIZE(caller.ingredients);  ++i)
536   ¦ out << ' ' << to_string(caller.ingredients.at(i));
537   if (!caller.products.empty()) out << " ->";
538   for (int i = 0;  i < SIZE(caller.products);  ++i)
539   ¦ out << ' ' << to_string(caller.products.at(i));
540   return out.str();
541 }
542 
543 :(scenario reload_variant_retains_other_variants)
544 def main [
545   1:num <- copy 34
546   2:num <- foo 1:num
547 ]
548 def foo x:num -> y:num [
549   local-scope
550   load-ingredients
551   return 34
552 ]
553 def foo x:&:num -> y:num [
554   local-scope
555   load-ingredients
556   return 35
557 ]
558 def! foo x:&:num -> y:num [
559   local-scope
560   load-ingredients
561   return 36
562 ]
563 +mem: storing 34 in location 2
564 $error: 0
565 
566 :(scenario dispatch_errors_come_after_unknown_name_errors)
567 % Hide_errors = true;
568 def main [
569   y:num <- foo x
570 ]
571 def foo a:num -> b:num [
572   local-scope
573   load-ingredients
574   return 34
575 ]
576 def foo a:bool -> b:num [
577   local-scope
578   load-ingredients
579   return 35
580 ]
581 +error: main: missing type for 'x' in 'y:num <- foo x'
582 +error: main: failed to find a matching call for 'y:num <- foo x'
583 
584 :(scenario override_methods_with_type_abbreviations)
585 def main [
586   local-scope
587   s:text <- new [abc]
588   1:num/raw <- foo s
589 ]
590 def foo a:address:array:character -> result:number [
591   return 34
592 ]
593 # identical to previous variant once you take type abbreviations into account
594 def! foo a:text -> result:num [
595   return 35
596 ]
597 +mem: storing 35 in location 1
598 
599 :(before "End Includes")
600 using std::abs;