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