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