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   // Begin Reagent->Recipe(r, recipe_header)
212   if (r.type->atom) {
213   ¦ assert(r.type->name == "recipe");
214   ¦ return result_header;
215   }
216   const type_tree* root_type = r.type->atom ? r.type : r.type->left;
217   assert(root_type->atom);
218   assert(root_type->name == "recipe");
219   const type_tree* curr = r.type->right;
220   for (/*nada*/;  curr && !curr->atom;  curr = curr->right) {
221   ¦ if (curr->left->atom && curr->left->name == "->") {
222   ¦ ¦ curr = curr->right;  // skip delimiter
223   ¦ ¦ goto read_products;
224   ¦ }
225   ¦ result_header.ingredients.push_back(next_recipe_reagent(curr->left));
226   }
227   if (curr) {
228   ¦ assert(curr->atom);
229   ¦ result_header.ingredients.push_back(next_recipe_reagent(curr));
230   ¦ return result_header;  // no products
231   }
232   read_products:
233   for (/*nada*/;  curr && !curr->atom;  curr = curr->right)
234   ¦ result_header.products.push_back(next_recipe_reagent(curr->left));
235   if (curr) {
236   ¦ assert(curr->atom);
237   ¦ result_header.products.push_back(next_recipe_reagent(curr));
238   }
239   return result_header;
240 }
241 
242 :(before "End Unit Tests")
243 void test_from_reagent_atomic() {
244   reagent a("{f: recipe}");
245   recipe r_header = from_reagent(a);
246   CHECK(r_header.ingredients.empty());
247   CHECK(r_header.products.empty());
248 }
249 void test_from_reagent_non_atomic() {
250   reagent a("{f: (recipe number -> number)}");
251   recipe r_header = from_reagent(a);
252   CHECK_EQ(SIZE(r_header.ingredients), 1);
253   CHECK_EQ(SIZE(r_header.products), 1);
254 }
255 void test_from_reagent_reads_ingredient_at_end() {
256   reagent a("{f: (recipe number number)}");
257   recipe r_header = from_reagent(a);
258   CHECK_EQ(SIZE(r_header.ingredients), 2);
259   CHECK(r_header.products.empty());
260 }
261 void test_from_reagent_reads_sole_ingredient_at_end() {
262   reagent a("{f: (recipe number)}");
263   recipe r_header = from_reagent(a);
264   CHECK_EQ(SIZE(r_header.ingredients), 1);
265   CHECK(r_header.products.empty());
266 }
267 
268 :(code)
269 reagent next_recipe_reagent(const type_tree* curr) {
270   if (!curr->left) return reagent("recipe:"+curr->name);
271   reagent result;
272   result.name = "recipe";
273   result.type = new type_tree(*curr);
274   return result;
275 }
276 
277 bool is_mu_recipe(const reagent& r) {
278   if (!r.type) return false;
279   if (r.type->atom) {
280   ¦ // End is_mu_recipe Atom Cases(r)
281   ¦ return r.type->name == "recipe-literal";
282   }
283   return r.type->left->atom && r.type->left->name == "recipe";
284 }
285 
286 :(scenario copy_typecheck_recipe_variable)
287 % Hide_errors = true;
288 def main [
289   3:num <- copy 34  # abc def
290   {1: (recipe number -> number)} <- copy f  # store literal in a matching variable
291   {2: (recipe boolean -> boolean)} <- copy {1: (recipe number -> number)}  # mismatch between recipe variables
292 ]
293 def f x:num -> y:num [
294   local-scope
295   load-ingredients
296   y <- copy x
297 ]
298 +error: main: can't copy '{1: (recipe number -> number)}' to '{2: (recipe boolean -> boolean)}'; types don't match
299 
300 :(scenario copy_typecheck_recipe_variable_2)
301 % Hide_errors = true;
302 def main [
303   {1: (recipe number -> number)} <- copy f  # mismatch with a recipe literal
304 ]
305 def f x:bool -> y:bool [
306   local-scope
307   load-ingredients
308   y <- copy x
309 ]
310 +error: main: can't copy 'f' to '{1: (recipe number -> number)}'; types don't match
311 
312 :(before "End Matching Types For Literal(to)")
313 if (is_mu_recipe(to)) {
314   if (!contains_key(Recipe, from.value)) {
315   ¦ raise << "trying to store recipe " << from.name << " into " << to_string(to) << " but there's no such recipe\n" << end();
316   ¦ return false;
317   }
318   const recipe& rrhs = get(Recipe, from.value);
319   const recipe& rlhs = from_reagent(to);
320   for (long int i = 0;  i < min(SIZE(rlhs.ingredients), SIZE(rrhs.ingredients));  ++i) {
321   ¦ if (!types_match(rlhs.ingredients.at(i), rrhs.ingredients.at(i)))
322   ¦ ¦ return false;
323   }
324   for (long int i = 0;  i < min(SIZE(rlhs.products), SIZE(rrhs.products));  ++i) {
325   ¦ if (!types_match(rlhs.products.at(i), rrhs.products.at(i)))
326   ¦ ¦ return false;
327   }
328   return true;
329 }
330 
331 :(scenario call_variable_compound_ingredient)
332 def main [
333   {1: (recipe (address number) -> number)} <- copy f
334   2:&:num <- copy 0
335   3:num <- call {1: (recipe (address number) -> number)}, 2:&:num
336 ]
337 def f x:&:num -> y:num [
338   local-scope
339   load-ingredients
340   y <- copy x
341 ]
342 $error: 0
343 
344 //: make sure we don't accidentally break on a recipe literal
345 :(scenario jump_forbidden_on_recipe_literals)
346 % Hide_errors = true;
347 def foo [
348   local-scope
349 ]
350 def main [
351   local-scope
352   {
353   ¦ break-if foo
354   }
355 ]
356 # error should be as if foo is not a recipe
357 +error: main: missing type for 'foo' in 'break-if foo'
358 
359 :(before "End JUMP_IF Checks")
360 check_for_recipe_literals(inst, get(Recipe, r));
361 :(before "End JUMP_UNLESS Checks")
362 check_for_recipe_literals(inst, get(Recipe, r));
363 :(code)
364 void check_for_recipe_literals(const instruction& inst, const recipe& caller) {
365   for (int i = 0;  i < SIZE(inst.ingredients);  ++i) {
366   ¦ if (is_mu_recipe(inst.ingredients.at(i))) {
367   ¦ ¦ raise << maybe(caller.name) << "missing type for '" << inst.ingredients.at(i).original_string << "' in '" << to_original_string(inst) << "'\n" << end();
368   ¦ ¦ if (is_present_in_ingredients(caller, inst.ingredients.at(i).name))
369   ¦ ¦ ¦ raise << "  did you forget 'load-ingredients'?\n" << end();
370   ¦ }
371   }
372 }
373 
374 :(scenario load_ingredients_missing_error_3)
375 % Hide_errors = true;
376 def foo {f: (recipe num -> num)} [
377   local-scope
378   b:num <- call f, 1
379 ]
380 +error: foo: missing type for 'f' in 'b:num <- call f, 1'
381 +error:   did you forget 'load-ingredients'?
382 
383 :(before "End Mu Types Initialization")
384 put(Type_abbreviations, "function", new_type_tree("recipe"));
385 
386 :(scenario call_function)
387 def main [
388   {1: (function number -> number)} <- copy f
389   2:num <- call {1: (function number -> number)}, 34
390 ]
391 def f x:num -> y:num [
392   local-scope
393   load-ingredients
394   y <- copy x
395 ]
396 +mem: storing 34 in location 2