1 //: So far we've been calling a fixed recipe in each instruction, but we'd
  2 //: also like to make the recipe a variable, pass recipes to "higher-order"
  3 //: recipes, return recipes from recipes and so on.
  4 //:
  5 //: todo: support storing shape-shifting recipes into recipe variables and calling them
  6 
  7 :(scenario call_literal_recipe)
  8 def main [
  9   1:num <- call f, 34
 10 ]
 11 def f x:num -> y:num [
 12   local-scope
 13   load-ingredients
 14   y <- copy x
 15 ]
 16 +mem: storing 34 in location 1
 17 
 18 :(before "End Mu Types Initialization")
 19 put(Type_ordinal, "recipe-literal", 0);
 20 // 'recipe' variables can store recipe-literal
 21 type_ordinal recipe = put(Type_ordinal, "recipe", Next_type_ordinal++);
 22 get_or_insert(Type, recipe).name = "recipe";
 23 
 24 :(after "Deduce Missing Type(x, caller)")
 25 if (!x.type)
 26   try_initialize_recipe_literal(x, caller);
 27 :(before "Type Check in Type-ingredient-aware check_or_set_types_by_name")
 28 if (!x.type)
 29   try_initialize_recipe_literal(x, variant);
 30 :(code)
 31 void try_initialize_recipe_literal(reagent& x, const recipe& caller) {
 32   if (x.type) return;
 33   if (!contains_key(Recipe_ordinal, x.name)) return;
 34   if (contains_reagent_with_non_recipe_literal_type(caller, x.name)) return;
 35   x.type = new type_tree("recipe-literal");
 36   x.set_value(get(Recipe_ordinal, x.name));
 37 }
 38 bool contains_reagent_with_non_recipe_literal_type(const recipe& caller, const string& name) {
 39   for (int i = 0;  i < SIZE(caller.steps);  ++i) {
 40   ¦ const instruction& inst = caller.steps.at(i);
 41   ¦ for (int i = 0;  i < SIZE(inst.ingredients);  ++i)
 42   ¦ ¦ if (is_matching_non_recipe_literal(inst.ingredients.at(i), name)) return true;
 43   ¦ for (int i = 0;  i < SIZE(inst.products);  ++i)
 44   ¦ ¦ if (is_matching_non_recipe_literal(inst.products.at(i), name)) return true;
 45   }
 46   return false;
 47 }
 48 bool is_matching_non_recipe_literal(const reagent& x, const string& name) {
 49   if (x.name != name) return false;
 50   if (!x.type) return false;
 51   return !x.type->atom || x.type->name != "recipe-literal";
 52 }
 53 
 54 //: It's confusing to use variable names that are also recipe names. Always
 55 //: assume variable types override recipe literals.
 56 :(scenario error_on_recipe_literal_used_as_a_variable)
 57 % Hide_errors = true;
 58 def main [
 59   local-scope
 60   a:bool <- equal break 0
 61   break:bool <- copy 0
 62 ]
 63 +error: main: missing type for 'break' in 'a:bool <- equal break, 0'
 64 
 65 :(before "End Primitive Recipe Declarations")
 66 CALL,
 67 :(before "End Primitive Recipe Numbers")
 68 put(Recipe_ordinal, "call", CALL);
 69 :(before "End Primitive Recipe Checks")
 70 case CALL: {
 71   if (inst.ingredients.empty()) {
 72   ¦ raise << maybe(get(Recipe, r).name) << "'call' requires at least one ingredient (the recipe to call)\n" << end();
 73   ¦ break;
 74   }
 75   if (!is_mu_recipe(inst.ingredients.at(0))) {
 76   ¦ raise << maybe(get(Recipe, r).name) << "first ingredient of 'call' should be a recipe, but got '" << inst.ingredients.at(0).original_string << "'\n" << end();
 77   ¦ break;
 78   }
 79   break;
 80 }
 81 :(before "End Primitive Recipe Implementations")
 82 case CALL: {
 83   // Begin Call
 84   if (Trace_stream) {
 85   ¦ ++Trace_stream->callstack_depth;
 86   ¦ trace("trace") << "indirect 'call': incrementing callstack depth to " << Trace_stream->callstack_depth << end();
 87   ¦ assert(Trace_stream->callstack_depth < 9000);  // 9998-101 plus cushion
 88   }
 89   if (!ingredients.at(0).at(0)) {
 90   ¦ raise << maybe(current_recipe_name()) << "tried to call empty recipe in '" << to_string(current_instruction()) << "'" << end();
 91   ¦ break;
 92   }
 93   const call& caller_frame = current_call();
 94   instruction/*copy*/ call_instruction = to_instruction(caller_frame);
 95   call_instruction.operation = ingredients.at(0).at(0);
 96   call_instruction.ingredients.erase(call_instruction.ingredients.begin());
 97   Current_routine->calls.push_front(call(ingredients.at(0).at(0)));
 98   ingredients.erase(ingredients.begin());  // drop the callee
 99   finish_call_housekeeping(call_instruction, ingredients);
100   Num_refcount_updates[caller_frame.running_recipe][caller_frame.running_step_index]
101   ¦ ¦ += (Total_refcount_updates - initial_num_refcount_updates);
102   initial_num_refcount_updates = Total_refcount_updates;
103   // not done with caller
104   write_products = false;
105   fall_through_to_next_instruction = false;
106   break;
107 }
108 
109 :(scenario call_variable)
110 def main [
111   {1: (recipe number -> number)} <- copy f
112   2:num <- call {1: (recipe number -> number)}, 34
113 ]
114 def f x:num -> y:num [
115   local-scope
116   load-ingredients
117   y <- copy x
118 ]
119 +mem: storing 34 in location 2
120 
121 :(scenario call_literal_recipe_repeatedly)
122 def main [
123   1:num <- call f, 34
124   1:num <- call f, 35
125 ]
126 def f x:num -> y:num [
127   local-scope
128   load-ingredients
129   y <- copy x
130 ]
131 +mem: storing 34 in location 1
132 +mem: storing 35 in location 1
133 
134 :(scenario call_shape_shifting_recipe)
135 def main [
136   1:num <- call f, 34
137 ]
138 def f x:_elem -> y:_elem [
139   local-scope
140   load-ingredients
141   y <- copy x
142 ]
143 +mem: storing 34 in location 1
144 
145 :(scenario call_shape_shifting_recipe_inside_shape_shifting_recipe)
146 def main [
147   1:num <- f 34
148 ]
149 def f x:_elem -> y:_elem [
150   local-scope
151   load-ingredients
152   y <- call g x
153 ]
154 def g x:_elem -> y:_elem [
155   local-scope
156   load-ingredients
157   y <- copy x
158 ]
159 +mem: storing 34 in location 1
160 
161 :(scenario call_shape_shifting_recipe_repeatedly_inside_shape_shifting_recipe)
162 def main [
163   1:num <- f 34
164 ]
165 def f x:_elem -> y:_elem [
166   local-scope
167   load-ingredients
168   y <- call g x
169   y <- call g x
170 ]
171 def g x:_elem -> y:_elem [
172   local-scope
173   load-ingredients
174   y <- copy x
175 ]
176 +mem: storing 34 in location 1
177 
178 //:: check types for 'call' instructions
179 
180 :(scenario call_check_literal_recipe)
181 % Hide_errors = true;
182 def main [
183   1:num <- call f, 34
184 ]
185 def f x:point -> y:point [
186   local-scope
187   load-ingredients
188   y <- copy x
189 ]
190 +error: main: ingredient 0 has the wrong type at '1:num <- call f, 34'
191 +error: main: product 0 has the wrong type at '1:num <- call f, 34'
192 
193 :(scenario call_check_variable_recipe)
194 % Hide_errors = true;
195 def main [
196   {1: (recipe point -> point)} <- copy f
197   2:num <- call {1: (recipe point -> point)}, 34
198 ]
199 def f x:point -> y:point [
200   local-scope
201   load-ingredients
202   y <- copy x
203 ]
204 +error: main: ingredient 0 has the wrong type at '2:num <- call {1: (recipe point -> point)}, 34'
205 +error: main: product 0 has the wrong type at '2:num <- call {1: (recipe point -> point)}, 34'
206 
207 :(before "End resolve_ambiguous_call(r, index, inst, caller_recipe) Special-cases")
208 if (inst.name == "call" && first_ingredient_is_recipe_literal(inst)) {
209   resolve_indirect_ambiguous_call(r, index, inst, caller_recipe);
210   return;
211 }
212 :(code)
213 bool first_ingredient_is_recipe_literal(const instruction& inst) {
214   if (inst.ingredients.empty()) return false;
215   const reagent& ingredient = inst.ingredients.at(0);
216   return ingredient.type && ingredient.type->atom && ingredient.type->name == "recipe-literal";
217 }
218 void resolve_indirect_ambiguous_call(const recipe_ordinal r, int index, instruction& inst, const recipe& caller_recipe) {
219   instruction inst2;
220   inst2.name = inst.ingredients.at(0).name;
221   for (int i = /*skip recipe*/1;  i < SIZE(inst.ingredients);  ++i)
222   ¦ inst2.ingredients.push_back(inst.ingredients.at(i));
223   for (int i = 0;  i < SIZE(inst.products);  ++i)
224   ¦ inst2.products.push_back(inst.products.at(i));
225   resolve_ambiguous_call(r, index, inst2, caller_recipe);
226   inst.ingredients.at(0).name = inst2.name;
227   inst.ingredients.at(0).set_value(get(Recipe_ordinal, inst2.name));
228 }
229 
230 :(after "Transform.push_back(check_instruction)")
231 Transform.push_back(check_indirect_calls_against_header);  // idempotent
232 :(code)
233 void check_indirect_calls_against_header(const recipe_ordinal r) {
234   trace(9991, "transform") << "--- type-check 'call' instructions inside recipe " << get(Recipe, r).name << end();
235   const recipe& caller = get(Recipe, r);
236   for (int i = 0;  i < SIZE(caller.steps);  ++i) {
237   ¦ const instruction& inst = caller.steps.at(i);
238   ¦ if (!is_indirect_call(inst.operation)) continue;
239   ¦ if (inst.ingredients.empty()) continue;  // error raised above
240   ¦ const reagent& callee = inst.ingredients.at(0);
241   ¦ if (!is_mu_recipe(callee)) continue;  // error raised above
242   ¦ const recipe callee_header = is_literal(callee) ? get(Recipe, callee.value) : from_reagent(inst.ingredients.at(0));
243   ¦ if (!callee_header.has_header) continue;
244   ¦ if (is_indirect_call_with_ingredients(inst.operation)) {
245   ¦ ¦ for (long int i = /*skip callee*/1;  i < min(SIZE(inst.ingredients), SIZE(callee_header.ingredients)+/*skip callee*/1);  ++i) {
246   ¦ ¦ ¦ if (!types_coercible(callee_header.ingredients.at(i-/*skip callee*/1), inst.ingredients.at(i)))
247   ¦ ¦ ¦ ¦ raise << maybe(caller.name) << "ingredient " << i-/*skip callee*/1 << " has the wrong type at '" << to_original_string(inst) << "'\n" << end();
248   ¦ ¦ }
249   ¦ }
250   ¦ if (is_indirect_call_with_products(inst.operation)) {
251   ¦ ¦ for (long int i = 0;  i < min(SIZE(inst.products), SIZE(callee_header.products));  ++i) {
252   ¦ ¦ ¦ if (is_dummy(inst.products.at(i))) continue;
253   ¦ ¦ ¦ if (!types_coercible(callee_header.products.at(i), inst.products.at(i)))
254   ¦ ¦ ¦ ¦ raise << maybe(caller.name) << "product " << i << " has the wrong type at '" << to_original_string(inst) << "'\n" << end();
255   ¦ ¦ }
256   ¦ }
257   }
258 }
259 
260 bool is_indirect_call(const recipe_ordinal r) {
261   return is_indirect_call_with_ingredients(r) || is_indirect_call_with_products(r);
262 }
263 
264 bool is_indirect_call_with_ingredients(const recipe_ordinal r) {
265   if (r == CALL) return true;
266   // End is_indirect_call_with_ingredients Special-cases
267   return false;
268 }
269 bool is_indirect_call_with_products(const recipe_ordinal r) {
270   if (r == CALL) return true;
271   // End is_indirect_call_with_products Special-cases
272   return false;
273 }
274 
275 recipe from_reagent(const reagent& r) {
276   assert(r.type);
277   recipe result_header;  // will contain only ingredients and products, nothing else
278   result_header.has_header = true;
279   // Begin Reagent->Recipe(r, recipe_header)
280   if (r.type->atom) {
281   ¦ assert(r.type->name == "recipe");
282   ¦ return result_header;
283   }
284   const type_tree* root_type = r.type->atom ? r.type : r.type->left;
285   assert(root_type->atom);
286   assert(root_type->name == "recipe");
287   const type_tree* curr = r.type->right;
288   for (/*nada*/;  curr && !curr->atom;  curr = curr->right) {
289   ¦ if (curr->left->atom && curr->left->name == "->") {
290   ¦ ¦ curr = curr->right;  // skip delimiter
291   ¦ ¦ goto read_products;
292   ¦ }
293   ¦ result_header.ingredients.push_back(next_recipe_reagent(curr->left));
294   }
295   if (curr) {
296   ¦ assert(curr->atom);
297   ¦ result_header.ingredients.push_back(next_recipe_reagent(curr));
298   ¦ return result_header;  // no products
299   }
300   read_products:
301   for (/*nada*/;  curr && !curr->atom;  curr = curr->right)
302   ¦ result_header.products.push_back(next_recipe_reagent(curr->left));
303   if (curr) {
304   ¦ assert(curr->atom);
305   ¦ result_header.products.push_back(next_recipe_reagent(curr));
306   }
307   return result_header;
308 }
309 
310 :(before "End Unit Tests")
311 void test_from_reagent_atomic() {
312   reagent a("{f: recipe}");
313   recipe r_header = from_reagent(a);
314   CHECK(r_header.ingredients.empty());
315   CHECK(r_header.products.empty());
316 }
317 void test_from_reagent_non_atomic() {
318   reagent a("{f: (recipe number -> number)}");
319   recipe r_header = from_reagent(a);
320   CHECK_EQ(SIZE(r_header.ingredients), 1);
321   CHECK_EQ(SIZE(r_header.products), 1);
322 }
323 void test_from_reagent_reads_ingredient_at_end() {
324   reagent a("{f: (recipe number number)}");
325   recipe r_header = from_reagent(a);
326   CHECK_EQ(SIZE(r_header.ingredients), 2);
327   CHECK(r_header.products.empty());
328 }
329 void test_from_reagent_reads_sole_ingredient_at_end() {
330   reagent a("{f: (recipe number)}");
331   recipe r_header = from_reagent(a);
332   CHECK_EQ(SIZE(r_header.ingredients), 1);
333   CHECK(r_header.products.empty());
334 }
335 
336 :(code)
337 reagent next_recipe_reagent(const type_tree* curr) {
338   if (!curr->left) return reagent("recipe:"+curr->name);
339   reagent result;
340   result.name = "recipe";
341   result.type = new type_tree(*curr);
342   return result;
343 }
344 
345 bool is_mu_recipe(const reagent& r) {
346   if (!r.type) return false;
347   if (r.type->atom) {
348   ¦ // End is_mu_recipe Atom Cases(r)
349   ¦ return r.type->name == "recipe-literal";
350   }
351   return r.type->left->atom && r.type->left->name == "recipe";
352 }
353 
354 :(scenario copy_typecheck_recipe_variable)
355 % Hide_errors = true;
356 def main [
357   3:num <- copy 34  # abc def
358   {1: (recipe number -> number)} <- copy f  # store literal in a matching variable
359   {2: (recipe boolean -> boolean)} <- copy {1: (recipe number -> number)}  # mismatch between recipe variables
360 ]
361 def f x:num -> y:num [
362   local-scope
363   load-ingredients
364   y <- copy x
365 ]
366 +error: main: can't copy '{1: (recipe number -> number)}' to '{2: (recipe boolean -> boolean)}'; types don't match
367 
368 :(scenario copy_typecheck_recipe_variable_2)
369 % Hide_errors = true;
370 def main [
371   {1: (recipe number -> number)} <- copy f  # mismatch with a recipe literal
372 ]
373 def f x:bool -> y:bool [
374   local-scope
375   load-ingredients
376   y <- copy x
377 ]
378 +error: main: can't copy 'f' to '{1: (recipe number -> number)}'; types don't match
379 
380 :(before "End Matching Types For Literal(to)")
381 if (is_mu_recipe(to)) {
382   if (!contains_key(Recipe, from.value)) {
383   ¦ raise << "trying to store recipe " << from.name << " into " << to_string(to) << " but there's no such recipe\n" << end();
384   ¦ return false;
385   }
386   const recipe& rrhs = get(Recipe, from.value);
387   const recipe& rlhs = from_reagent(to);
388   for (long int i = 0;  i < min(SIZE(rlhs.ingredients), SIZE(rrhs.ingredients));  ++i) {
389   ¦ if (!types_match(rlhs.ingredients.at(i), rrhs.ingredients.at(i)))
390   ¦ ¦ return false;
391   }
392   for (long int i = 0;  i < min(SIZE(rlhs.products), SIZE(rrhs.products));  ++i) {
393   ¦ if (!types_match(rlhs.products.at(i), rrhs.products.at(i)))
394   ¦ ¦ return false;
395   }
396   return true;
397 }
398 
399 :(scenario call_variable_compound_ingredient)
400 def main [
401   {1: (recipe (address number) -> number)} <- copy f
402   2:&:num <- copy 0
403   3:num <- call {1: (recipe (address number) -> number)}, 2:&:num
404 ]
405 def f x:&:num -> y:num [
406   local-scope
407   load-ingredients
408   y <- copy x
409 ]
410 $error: 0
411 
412 //: make sure we don't accidentally break on a recipe literal
413 :(scenario jump_forbidden_on_recipe_literals)
414 % Hide_errors = true;
415 def foo [
416   local-scope
417 ]
418 def main [
419   local-scope
420   {
421   ¦ break-if foo
422   }
423 ]
424 # error should be as if foo is not a recipe
425 +error: main: missing type for 'foo' in 'break-if foo'
426 
427 :(before "End JUMP_IF Checks")
428 check_for_recipe_literals(inst, get(Recipe, r));
429 :(before "End JUMP_UNLESS Checks")
430 check_for_recipe_literals(inst, get(Recipe, r));
431 :(code)
432 void check_for_recipe_literals(const instruction& inst, const recipe& caller) {
433   for (int i = 0;  i < SIZE(inst.ingredients);  ++i) {
434   ¦ if (is_mu_recipe(inst.ingredients.at(i))) {
435   ¦ ¦ raise << maybe(caller.name) << "missing type for '" << inst.ingredients.at(i).original_string << "' in '" << to_original_string(inst) << "'\n" << end();
436   ¦ ¦ if (is_present_in_ingredients(caller, inst.ingredients.at(i).name))
437   ¦ ¦ ¦ raise << "  did you forget 'load-ingredients'?\n" << end();
438   ¦ }
439   }
440 }
441 
442 :(scenario load_ingredients_missing_error_3)
443 % Hide_errors = true;
444 def foo {f: (recipe num -> num)} [
445   local-scope
446   b:num <- call f, 1
447 ]
448 +error: foo: missing type for 'f' in 'b:num <- call f, 1'
449 +error:   did you forget 'load-ingredients'?
450 
451 :(before "End Mu Types Initialization")
452 put(Type_abbreviations, "function", new_type_tree("recipe"));
453 
454 :(scenario call_function)
455 def main [
456   {1: (function number -> number)} <- copy f
457   2:num <- call {1: (function number -> number)}, 34
458 ]
459 def f x:num -> y:num [
460   local-scope
461   load-ingredients
462   y <- copy x
463 ]
464 +mem: storing 34 in location 2