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("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 //: save original name of recipes before renaming them
143 :(before "End recipe Fields")
144 string original_name;
145 //: original name is only set during load
146 :(before "End Load Recipe Name")
147 result.original_name = result.name;
148 
149 //: after filling in all missing types (because we'll be introducing 'blank' types in this transform in a later layer, for shape-shifting recipes)
150 :(after "Transform.push_back(transform_names)")
151 Transform.push_back(resolve_ambiguous_calls);  // idempotent
152 
153 //: In a later layer we'll introduce recursion in resolve_ambiguous_calls, by
154 //: having it generate code for shape-shifting recipes and then transform such
155 //: code. This data structure will help error messages be more useful.
156 //:
157 //: We're punning the 'call' data structure just because it has slots for
158 //: calling recipe and calling instruction.
159 :(before "End Globals")
160 list<call> Resolve_stack;
161 
162 :(code)
163 void resolve_ambiguous_calls(const recipe_ordinal r) {
164   recipe& caller_recipe = get(Recipe, r);
165   trace(9991, "transform") << "--- resolve ambiguous calls for recipe " << caller_recipe.name << end();
166   for (int index = 0;  index < SIZE(caller_recipe.steps);  ++index) {
167   ¦ instruction& inst = caller_recipe.steps.at(index);
168   ¦ if (inst.is_label) continue;
169   ¦ if (non_ghost_size(get_or_insert(Recipe_variants, inst.name)) == 0) continue;
170   ¦ trace(9992, "transform") << "instruction " << to_original_string(inst) << end();
171   ¦ Resolve_stack.push_front(call(r));
172   ¦ Resolve_stack.front().running_step_index = index;
173   ¦ string new_name = best_variant(inst, caller_recipe);
174   ¦ if (!new_name.empty())
175   ¦ ¦ inst.name = new_name;
176   ¦ assert(Resolve_stack.front().running_recipe == r);
177   ¦ assert(Resolve_stack.front().running_step_index == index);
178   ¦ Resolve_stack.pop_front();
179   }
180 }
181 
182 string best_variant(const instruction& inst, const recipe& caller_recipe) {
183   const vector<recipe_ordinal>& variants = get(Recipe_variants, inst.name);
184   vector<recipe_ordinal> candidates;
185 
186   // Static Dispatch Phase 1
187 //?   cerr << inst.name << " phase 1\n";
188   candidates = strictly_matching_variants(inst, variants);
189   if (!candidates.empty()) return best_variant(inst, candidates).name;
190 
191   // Static Dispatch Phase 2
192 //?   cerr << inst.name << " phase 2\n";
193   candidates = strictly_matching_variants_except_literal_against_address_or_boolean(inst, variants);
194   if (!candidates.empty()) return best_variant(inst, candidates).name;
195 
196 //?   cerr << inst.name << " phase 3\n";
197   // Static Dispatch Phase 3
198   //: (shape-shifting recipes in a later layer)
199   // End Static Dispatch Phase 3
200 
201   // Static Dispatch Phase 4
202 //?   cerr << inst.name << " phase 4\n";
203   candidates = matching_variants(inst, variants);
204   if (!candidates.empty()) return best_variant(inst, candidates).name;
205 
206   // error messages
207   if (!is_primitive(get(Recipe_ordinal, inst.name))) {  // we currently don't check types for primitive variants
208   ¦ if (SIZE(variants) == 1) {
209   ¦ ¦ raise << maybe(caller_recipe.name) << "types don't match in call for '" << to_original_string(inst) << "'\n" << end();
210   ¦ ¦ raise << "  which tries to call '" << original_header_label(get(Recipe, variants.at(0))) << "'\n" << end();
211   ¦ }
212   ¦ else {
213   ¦ ¦ raise << maybe(caller_recipe.name) << "failed to find a matching call for '" << to_original_string(inst) << "'\n" << end();
214   ¦ ¦ raise << "  available variants are:\n" << end();
215   ¦ ¦ for (int i = 0;  i < SIZE(variants);  ++i)
216   ¦ ¦ ¦ raise << "    " << original_header_label(get(Recipe, variants.at(i))) << '\n' << end();
217   ¦ }
218   ¦ for (list<call>::iterator p = /*skip*/++Resolve_stack.begin();  p != Resolve_stack.end();  ++p) {
219   ¦ ¦ const recipe& specializer_recipe = get(Recipe, p->running_recipe);
220   ¦ ¦ const instruction& specializer_inst = specializer_recipe.steps.at(p->running_step_index);
221   ¦ ¦ if (specializer_recipe.name != "interactive")
222   ¦ ¦ ¦ raise << "  (from '" << to_original_string(specializer_inst) << "' in " << specializer_recipe.name << ")\n" << end();
223   ¦ ¦ else
224   ¦ ¦ ¦ raise << "  (from '" << to_original_string(specializer_inst) << "')\n" << end();
225   ¦ ¦ // One special-case to help with the rewrite_stash transform. (cross-layer)
226   ¦ ¦ if (specializer_inst.products.at(0).name.find("stash_") == 0) {
227   ¦ ¦ ¦ instruction stash_inst;
228   ¦ ¦ ¦ if (next_stash(*p, &stash_inst)) {
229   ¦ ¦ ¦ ¦ if (specializer_recipe.name != "interactive")
230   ¦ ¦ ¦ ¦ ¦ raise << "  (part of '" << to_original_string(stash_inst) << "' in " << specializer_recipe.name << ")\n" << end();
231   ¦ ¦ ¦ ¦ else
232   ¦ ¦ ¦ ¦ ¦ raise << "  (part of '" << to_original_string(stash_inst) << "')\n" << end();
233   ¦ ¦ ¦ }
234   ¦ ¦ }
235   ¦ }
236   }
237   return "";
238 }
239 
240 // phase 1
241 vector<recipe_ordinal> strictly_matching_variants(const instruction& inst, const vector<recipe_ordinal>& variants) {
242   vector<recipe_ordinal> result;
243   for (int i = 0;  i < SIZE(variants);  ++i) {
244   ¦ if (variants.at(i) == -1) continue;
245   ¦ trace(9992, "transform") << "checking variant (strict) " << i << ": " << header_label(variants.at(i)) << end();
246   ¦ if (all_header_reagents_strictly_match(inst, get(Recipe, variants.at(i))))
247   ¦ ¦ result.push_back(variants.at(i));
248   }
249   return result;
250 }
251 
252 bool all_header_reagents_strictly_match(const instruction& inst, const recipe& variant) {
253   for (int i = 0;  i < min(SIZE(inst.ingredients), SIZE(variant.ingredients));  ++i) {
254   ¦ if (!types_strictly_match(variant.ingredients.at(i), inst.ingredients.at(i))) {
255   ¦ ¦ trace(9993, "transform") << "strict match failed: ingredient " << i << end();
256   ¦ ¦ return false;
257   ¦ }
258   }
259   for (int i = 0;  i < min(SIZE(inst.products), SIZE(variant.products));  ++i) {
260   ¦ if (is_dummy(inst.products.at(i))) continue;
261   ¦ if (!types_strictly_match(variant.products.at(i), inst.products.at(i))) {
262   ¦ ¦ trace(9993, "transform") << "strict match failed: product " << i << end();
263   ¦ ¦ return false;
264   ¦ }
265   }
266   return true;
267 }
268 
269 // phase 2
270 vector<recipe_ordinal> strictly_matching_variants_except_literal_against_address_or_boolean(const instruction& inst, const vector<recipe_ordinal>& variants) {
271   vector<recipe_ordinal> result;
272   for (int i = 0;  i < SIZE(variants);  ++i) {
273   ¦ if (variants.at(i) == -1) continue;
274   ¦ trace(9992, "transform") << "checking variant (strict except literal-against-boolean) " << i << ": " << header_label(variants.at(i)) << end();
275   ¦ if (all_header_reagents_strictly_match_except_literal_against_address_or_boolean(inst, get(Recipe, variants.at(i))))
276   ¦ ¦ result.push_back(variants.at(i));
277   }
278   return result;
279 }
280 
281 bool all_header_reagents_strictly_match_except_literal_against_address_or_boolean(const instruction& inst, const recipe& variant) {
282   for (int i = 0;  i < min(SIZE(inst.ingredients), SIZE(variant.ingredients));  ++i) {
283   ¦ if (!types_strictly_match_except_literal_against_address_or_boolean(variant.ingredients.at(i), inst.ingredients.at(i))) {
284   ¦ ¦ trace(9993, "transform") << "match failed: ingredient " << i << end();
285   ¦ ¦ return false;
286   ¦ }
287   }
288   for (int i = 0;  i < min(SIZE(variant.products), SIZE(inst.products));  ++i) {
289   ¦ if (is_dummy(inst.products.at(i))) continue;
290   ¦ if (!types_strictly_match_except_literal_against_address_or_boolean(variant.products.at(i), inst.products.at(i))) {
291   ¦ ¦ trace(9993, "transform") << "match failed: product " << i << end();
292   ¦ ¦ return false;
293   ¦ }
294   }
295   return true;
296 }
297 
298 bool types_strictly_match_except_literal_against_address_or_boolean(const reagent& to, const reagent& from) {
299   if (is_literal(from) && is_mu_boolean(to))
300   ¦ return from.name == "0" || from.name == "1";
301   // Match Literal Zero Against Address {
302   if (is_literal(from) && is_mu_address(to))
303   ¦ return from.name == "0";
304   // }
305   return types_strictly_match(to, from);
306 }
307 
308 // phase 4
309 vector<recipe_ordinal> matching_variants(const instruction& inst, const vector<recipe_ordinal>& variants) {
310   vector<recipe_ordinal> result;
311   for (int i = 0;  i < SIZE(variants);  ++i) {
312   ¦ if (variants.at(i) == -1) continue;
313   ¦ trace(9992, "transform") << "checking variant " << i << ": " << header_label(variants.at(i)) << end();
314   ¦ if (all_header_reagents_match(inst, get(Recipe, variants.at(i))))
315   ¦ ¦ result.push_back(variants.at(i));
316   }
317   return result;
318 }
319 
320 bool all_header_reagents_match(const instruction& inst, const recipe& variant) {
321   for (int i = 0;  i < min(SIZE(inst.ingredients), SIZE(variant.ingredients));  ++i) {
322   ¦ if (!types_match(variant.ingredients.at(i), inst.ingredients.at(i))) {
323   ¦ ¦ trace(9993, "transform") << "match failed: ingredient " << i << end();
324   ¦ ¦ return false;
325   ¦ }
326   }
327   for (int i = 0;  i < min(SIZE(variant.products), SIZE(inst.products));  ++i) {
328   ¦ if (is_dummy(inst.products.at(i))) continue;
329   ¦ if (!types_match(variant.products.at(i), inst.products.at(i))) {
330   ¦ ¦ trace(9993, "transform") << "match failed: product " << i << end();
331   ¦ ¦ return false;
332   ¦ }
333   }
334   return true;
335 }
336 
337 // tie-breaker for each phase
338 const recipe& best_variant(const instruction& inst, vector<recipe_ordinal>& candidates) {
339   assert(!candidates.empty());
340   if (SIZE(candidates) == 1) return get(Recipe, candidates.at(0));
341   int min_score = 999;
342   int min_index = 0;
343   for (int i = 0;  i < SIZE(candidates);  ++i) {
344   ¦ const recipe& candidate = get(Recipe, candidates.at(i));
345   ¦ // prefer variants without extra or missing ingredients or products
346   ¦ int score = abs(SIZE(candidate.products)-SIZE(inst.products))
347   ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦     + abs(SIZE(candidate.ingredients)-SIZE(inst.ingredients));
348   ¦ // prefer variants with non-address ingredients or products
349   ¦ for (int j = 0;  j < SIZE(candidate.ingredients);  ++j) {
350   ¦ ¦ if (is_mu_address(candidate.ingredients.at(j)))
351   ¦ ¦ ¦ ++score;
352   ¦ }
353   ¦ for (int j = 0;  j < SIZE(candidate.products);  ++j) {
354   ¦ ¦ if (is_mu_address(candidate.products.at(j)))
355   ¦ ¦ ¦ ++score;
356   ¦ }
357   ¦ assert(score < 999);
358   ¦ if (score < min_score) {
359   ¦ ¦ min_score = score;
360   ¦ ¦ min_index = i;
361   ¦ }
362   }
363   return get(Recipe, candidates.at(min_index));
364 }
365 
366 int non_ghost_size(vector<recipe_ordinal>& variants) {
367   int result = 0;
368   for (int i = 0;  i < SIZE(variants);  ++i)
369   ¦ if (variants.at(i) != -1) ++result;
370   return result;
371 }
372 
373 bool next_stash(const call& c, instruction* stash_inst) {
374   const recipe& specializer_recipe = get(Recipe, c.running_recipe);
375   int index = c.running_step_index;
376   for (++index;  index < SIZE(specializer_recipe.steps);  ++index) {
377   ¦ const instruction& inst = specializer_recipe.steps.at(index);
378   ¦ if (inst.name == "stash") {
379   ¦ ¦ *stash_inst = inst;
380   ¦ ¦ return true;
381   ¦ }
382   }
383   return false;
384 }
385 
386 :(scenario static_dispatch_disabled_in_recipe_without_variants)
387 def main [
388   1:num <- test 3
389 ]
390 def test [
391   2:num <- next-ingredient  # ensure no header
392   return 34
393 ]
394 +mem: storing 34 in location 1
395 
396 :(scenario static_dispatch_disabled_on_headerless_definition)
397 % Hide_errors = true;
398 def test a:num -> z:num [
399   z <- copy 1
400 ]
401 def test [
402   return 34
403 ]
404 +error: redefining recipe test
405 
406 :(scenario static_dispatch_disabled_on_headerless_definition_2)
407 % Hide_errors = true;
408 def test [
409   return 34
410 ]
411 def test a:num -> z:num [
412   z <- copy 1
413 ]
414 +error: redefining recipe test
415 
416 :(scenario static_dispatch_on_primitive_names)
417 def main [
418   1:num <- copy 34
419   2:num <- copy 34
420   3:bool <- equal 1:num, 2:num
421   4:bool <- copy 0/false
422   5:bool <- copy 0/false
423   6:bool <- equal 4:bool, 5:bool
424 ]
425 # temporarily hardcode number equality to always fail
426 def equal x:num, y:num -> z:bool [
427   local-scope
428   load-ingredients
429   z <- copy 0/false
430 ]
431 # comparing numbers used overload
432 +mem: storing 0 in location 3
433 # comparing booleans continues to use primitive
434 +mem: storing 1 in location 6
435 
436 :(scenario static_dispatch_works_with_dummy_results_for_containers)
437 def main [
438   _ <- test 3, 4
439 ]
440 def test a:num -> z:point [
441   local-scope
442   load-ingredients
443   z <- merge a, 0
444 ]
445 def test a:num, b:num -> z:point [
446   local-scope
447   load-ingredients
448   z <- merge a, b
449 ]
450 $error: 0
451 
452 :(scenario static_dispatch_works_with_compound_type_containing_container_defined_after_first_use)
453 def main [
454   x:&:foo <- new foo:type
455   test x
456 ]
457 container foo [
458   x:num
459 ]
460 def test a:&:foo -> z:num [
461   local-scope
462   load-ingredients
463   z:num <- get *a, x:offset
464 ]
465 $error: 0
466 
467 :(scenario static_dispatch_works_with_compound_type_containing_container_defined_after_second_use)
468 def main [
469   x:&:foo <- new foo:type
470   test x
471 ]
472 def test a:&:foo -> z:num [
473   local-scope
474   load-ingredients
475   z:num <- get *a, x:offset
476 ]
477 container foo [
478   x:num
479 ]
480 $error: 0
481 
482 :(scenario static_dispatch_prefers_literals_to_be_numbers_rather_than_addresses)
483 def main [
484   1:num <- foo 0
485 ]
486 def foo x:&:num -> y:num [
487   return 34
488 ]
489 def foo x:num -> y:num [
490   return 35
491 ]
492 +mem: storing 35 in location 1
493 
494 :(scenario static_dispatch_prefers_literals_to_be_numbers_rather_than_addresses_2)
495 def main [
496   1:num <- foo 0 0
497 ]
498 # Both variants need to bind 0 to address in first ingredient.
499 # We still want to prefer the variant with a number rather than address for
500 # _subsequent_ ingredients.
501 def foo x:&:num y:&:num -> z:num [  # put the bad match before the good one
502   return 34
503 ]
504 def foo x:&:num y:num -> z:num [
505   return 35
506 ]
507 +mem: storing 35 in location 1
508 
509 :(scenario static_dispatch_on_non_literal_character_ignores_variant_with_numbers)
510 % Hide_errors = true;
511 def main [
512   local-scope
513   x:char <- copy 10/newline
514   1:num/raw <- foo x
515 ]
516 def foo x:num -> y:num [
517   load-ingredients
518   return 34
519 ]
520 +error: main: ingredient 0 has the wrong type at '1:num/raw <- foo x'
521 -mem: storing 34 in location 1
522 
523 :(scenario static_dispatch_dispatches_literal_to_boolean_before_character)
524 def main [
525   1:num/raw <- foo 0  # valid literal for boolean
526 ]
527 def foo x:char -> y:num [
528   local-scope
529   load-ingredients
530   return 34
531 ]
532 def foo x:bool -> y:num [
533   local-scope
534   load-ingredients
535   return 35
536 ]
537 # boolean variant is preferred
538 +mem: storing 35 in location 1
539 
540 :(scenario static_dispatch_dispatches_literal_to_character_when_out_of_boolean_range)
541 def main [
542   1:num/raw <- foo 97  # not a valid literal for boolean
543 ]
544 def foo x:char -> y:num [
545   local-scope
546   load-ingredients
547   return 34
548 ]
549 def foo x:bool -> y:num [
550   local-scope
551   load-ingredients
552   return 35
553 ]
554 # character variant is preferred
555 +mem: storing 34 in location 1
556 
557 :(scenario static_dispatch_dispatches_literal_to_number_if_at_all_possible)
558 def main [
559   1:num/raw <- foo 97
560 ]
561 def foo x:char -> y:num [
562   local-scope
563   load-ingredients
564   return 34
565 ]
566 def foo x:num -> y:num [
567   local-scope
568   load-ingredients
569   return 35
570 ]
571 # number variant is preferred
572 +mem: storing 35 in location 1
573 
574 :(replace{} "string header_label(const recipe_ordinal r)")
575 string header_label(const recipe_ordinal r) {
576   return header_label(get(Recipe, r));
577 }
578 :(code)
579 string header_label(const recipe& caller) {
580   ostringstream out;
581   out << "recipe " << caller.name;
582   for (int i = 0;  i < SIZE(caller.ingredients);  ++i)
583   ¦ out << ' ' << to_string(caller.ingredients.at(i));
584   if (!caller.products.empty()) out << " ->";
585   for (int i = 0;  i < SIZE(caller.products);  ++i)
586   ¦ out << ' ' << to_string(caller.products.at(i));
587   return out.str();
588 }
589 
590 string original_header_label(const recipe& caller) {
591   ostringstream out;
592   out << "recipe " << caller.original_name;
593   for (int i = 0;  i < SIZE(caller.ingredients);  ++i)
594   ¦ out << ' ' << caller.ingredients.at(i).original_string;
595   if (!caller.products.empty()) out << " ->";
596   for (int i = 0;  i < SIZE(caller.products);  ++i)
597   ¦ out << ' ' << caller.products.at(i).original_string;
598   return out.str();
599 }
600 
601 :(scenario reload_variant_retains_other_variants)
602 def main [
603   1:num <- copy 34
604   2:num <- foo 1:num
605 ]
606 def foo x:num -> y:num [
607   local-scope
608   load-ingredients
609   return 34
610 ]
611 def foo x:&:num -> y:num [
612   local-scope
613   load-ingredients
614   return 35
615 ]
616 def! foo x:&:num -> y:num [
617   local-scope
618   load-ingredients
619   return 36
620 ]
621 +mem: storing 34 in location 2
622 $error: 0
623 
624 :(scenario dispatch_errors_come_after_unknown_name_errors)
625 % Hide_errors = true;
626 def main [
627   y:num <- foo x
628 ]
629 def foo a:num -> b:num [
630   local-scope
631   load-ingredients
632   return 34
633 ]
634 def foo a:bool -> b:num [
635   local-scope
636   load-ingredients
637   return 35
638 ]
639 +error: main: missing type for 'x' in 'y:num <- foo x'
640 +error: main: failed to find a matching call for 'y:num <- foo x'
641 
642 :(scenario override_methods_with_type_abbreviations)
643 def main [
644   local-scope
645   s:text <- new [abc]
646   1:num/raw <- foo s
647 ]
648 def foo a:address:array:character -> result:number [
649   return 34
650 ]
651 # identical to previous variant once you take type abbreviations into account
652 def! foo a:text -> result:num [
653   return 35
654 ]
655 +mem: storing 35 in location 1
656 
657 :(scenario ignore_static_dispatch_in_type_errors_without_overloading)
658 % Hide_errors = true;
659 def main [
660   local-scope
661   x:&:num <- copy 0
662   foo x
663 ]
664 def foo x:&:char [
665   local-scope
666   load-ingredients
667 ]
668 +error: main: types don't match in call for 'foo x'
669 +error:   which tries to call 'recipe foo x:&:char'
670 
671 :(scenario show_available_variants_in_dispatch_errors)
672 % Hide_errors = true;
673 def main [
674   local-scope
675   x:&:num <- copy 0
676   foo x
677 ]
678 def foo x:&:char [
679   local-scope
680   load-ingredients
681 ]
682 def foo x:&:bool [
683   local-scope
684   load-ingredients
685 ]
686 +error: main: failed to find a matching call for 'foo x'
687 +error:   available variants are:
688 +error:     recipe foo x:&:char
689 +error:     recipe foo x:&:bool
690 
691 :(before "End Includes")
692 using std::abs;