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