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