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   instruction/*copy*/ call_instruction = current_instruction();
118   call_instruction.operation = ingredients.at(0).at(0);
119   call_instruction.ingredients.erase(call_instruction.ingredients.begin());
120   Current_routine->calls.push_front(call(ingredients.at(0).at(0)));
121   ingredients.erase(ingredients.begin());  // drop the callee
122   finish_call_housekeeping(call_instruction, ingredients);
123   continue;
124 }
125 
126 //:: check types for 'call' instructions
127 
128 :(scenario call_check_literal_recipe)
129 % Hide_errors = true;
130 def main [
131   1:num <- call f, 34
132 ]
133 def f x:point -> y:point [
134   local-scope
135   load-ingredients
136   y <- copy x
137 ]
138 +error: main: ingredient 0 has the wrong type at '1:num <- call f, 34'
139 +error: main: product 0 has the wrong type at '1:num <- call f, 34'
140 
141 :(scenario call_check_variable_recipe)
142 % Hide_errors = true;
143 def main [
144   {1: (recipe point -> point)} <- copy f
145   2:num <- call {1: (recipe point -> point)}, 34
146 ]
147 def f x:point -> y:point [
148   local-scope
149   load-ingredients
150   y <- copy x
151 ]
152 +error: main: ingredient 0 has the wrong type at '2:num <- call {1: (recipe point -> point)}, 34'
153 +error: main: product 0 has the wrong type at '2:num <- call {1: (recipe point -> point)}, 34'
154 
155 :(after "Transform.push_back(check_instruction)")
156 Transform.push_back(check_indirect_calls_against_header);  // idempotent
157 :(code)
158 void check_indirect_calls_against_header(const recipe_ordinal r) {
159   trace(9991, "transform") << "--- type-check 'call' instructions inside recipe " << get(Recipe, r).name << end();
160   const recipe& caller = get(Recipe, r);
161   for (int i = 0;  i < SIZE(caller.steps);  ++i) {
162   ¦ const instruction& inst = caller.steps.at(i);
163   ¦ if (!is_indirect_call(inst.operation)) continue;
164   ¦ if (inst.ingredients.empty()) continue;  // error raised above
165   ¦ const reagent& callee = inst.ingredients.at(0);
166   ¦ if (!is_mu_recipe(callee)) continue;  // error raised above
167   ¦ const recipe callee_header = is_literal(callee) ? get(Recipe, callee.value) : from_reagent(inst.ingredients.at(0));
168   ¦ if (!callee_header.has_header) continue;
169   ¦ if (is_indirect_call_with_ingredients(inst.operation)) {
170   ¦ ¦ for (long int i = /*skip callee*/1;  i < min(SIZE(inst.ingredients), SIZE(callee_header.ingredients)+/*skip callee*/1);  ++i) {
171   ¦ ¦ ¦ if (!types_coercible(callee_header.ingredients.at(i-/*skip callee*/1), inst.ingredients.at(i)))
172   ¦ ¦ ¦ ¦ raise << maybe(caller.name) << "ingredient " << i-/*skip callee*/1 << " has the wrong type at '" << inst.original_string << "'\n" << end();
173   ¦ ¦ }
174   ¦ }
175   ¦ if (is_indirect_call_with_products(inst.operation)) {
176   ¦ ¦ for (long int i = 0;  i < min(SIZE(inst.products), SIZE(callee_header.products));  ++i) {
177   ¦ ¦ ¦ if (is_dummy(inst.products.at(i))) continue;
178   ¦ ¦ ¦ if (!types_coercible(callee_header.products.at(i), inst.products.at(i)))
179   ¦ ¦ ¦ ¦ raise << maybe(caller.name) << "product " << i << " has the wrong type at '" << inst.original_string << "'\n" << end();
180   ¦ ¦ }
181   ¦ }
182   }
183 }
184 
185 bool is_indirect_call(const recipe_ordinal r) {
186   return is_indirect_call_with_ingredients(r) || is_indirect_call_with_products(r);
187 }
188 
189 bool is_indirect_call_with_ingredients(const recipe_ordinal r) {
190   if (r == CALL) return true;
191   // End is_indirect_call_with_ingredients Special-cases
192   return false;
193 }
194 bool is_indirect_call_with_products(const recipe_ordinal r) {
195   if (r == CALL) return true;
196   // End is_indirect_call_with_products Special-cases
197   return false;
198 }
199 
200 recipe from_reagent(const reagent& r) {
201   assert(r.type);
202   recipe result_header;  // will contain only ingredients and products, nothing else
203   result_header.has_header = true;
204   if (r.type->atom) {
205   ¦ assert(r.type->name == "recipe");
206   ¦ return result_header;
207   }
208   const type_tree* root_type = r.type->atom ? r.type : r.type->left;
209   assert(root_type->atom);
210   assert(root_type->name == "recipe");
211   const type_tree* curr = r.type->right;
212   for (/*nada*/;  curr && !curr->atom;  curr = curr->right) {
213   ¦ if (curr->left->atom && curr->left->name == "->") {
214   ¦ ¦ curr = curr->right;  // skip delimiter
215   ¦ ¦ goto read_products;
216   ¦ }
217   ¦ result_header.ingredients.push_back(next_recipe_reagent(curr->left));
218   }
219   if (curr) {
220   ¦ assert(curr->atom);
221   ¦ result_header.ingredients.push_back(next_recipe_reagent(curr));
222   ¦ return result_header;  // no products
223   }
224   read_products:
225   for (/*nada*/;  curr && !curr->atom;  curr = curr->right)
226   ¦ result_header.products.push_back(next_recipe_reagent(curr->left));
227   if (curr) {
228   ¦ assert(curr->atom);
229   ¦ result_header.products.push_back(next_recipe_reagent(curr));
230   }
231   return result_header;
232 }
233 
234 :(before "End Unit Tests")
235 void test_from_reagent_atomic() {
236   reagent a("{f: recipe}");
237   recipe r_header = from_reagent(a);
238   CHECK(r_header.ingredients.empty());
239   CHECK(r_header.products.empty());
240 }
241 void test_from_reagent_non_atomic() {
242   reagent a("{f: (recipe number -> number)}");
243   recipe r_header = from_reagent(a);
244   CHECK_EQ(SIZE(r_header.ingredients), 1);
245   CHECK_EQ(SIZE(r_header.products), 1);
246 }
247 void test_from_reagent_reads_ingredient_at_end() {
248   reagent a("{f: (recipe number number)}");
249   recipe r_header = from_reagent(a);
250   CHECK_EQ(SIZE(r_header.ingredients), 2);
251   CHECK(r_header.products.empty());
252 }
253 void test_from_reagent_reads_sole_ingredient_at_end() {
254   reagent a("{f: (recipe number)}");
255   recipe r_header = from_reagent(a);
256   CHECK_EQ(SIZE(r_header.ingredients), 1);
257   CHECK(r_header.products.empty());
258 }
259 
260 :(code)
261 reagent next_recipe_reagent(const type_tree* curr) {
262   if (!curr->left) return reagent("recipe:"+curr->name);
263   reagent result;
264   result.name = "recipe";
265   result.type = new type_tree(*curr);
266   return result;
267 }
268 
269 bool is_mu_recipe(const reagent& r) {
270   if (!r.type) return false;
271   if (r.type->atom)
272   ¦ return r.type->name == "recipe-literal";
273   if (!r.type->left->atom) return false;
274   if (r.type->left->name == "recipe") return true;
275   return false;
276 }
277 
278 :(scenario copy_typecheck_recipe_variable)
279 % Hide_errors = true;
280 def main [
281   3:num <- copy 34  # abc def
282   {1: (recipe number -> number)} <- copy f  # store literal in a matching variable
283   {2: (recipe boolean -> boolean)} <- copy {1: (recipe number -> number)}  # mismatch between recipe variables
284 ]
285 def f x:num -> y:num [
286   local-scope
287   load-ingredients
288   y <- copy x
289 ]
290 +error: main: can't copy '{1: (recipe number -> number)}' to '{2: (recipe boolean -> boolean)}'; types don't match
291 
292 :(scenario copy_typecheck_recipe_variable_2)
293 % Hide_errors = true;
294 def main [
295   {1: (recipe number -> number)} <- copy f  # mismatch with a recipe literal
296 ]
297 def f x:bool -> y:bool [
298   local-scope
299   load-ingredients
300   y <- copy x
301 ]
302 +error: main: can't copy 'f' to '{1: (recipe number -> number)}'; types don't match
303 
304 :(before "End Matching Types For Literal(to)")
305 if (is_mu_recipe(to)) {
306   if (!contains_key(Recipe, from.value)) {
307   ¦ raise << "trying to store recipe " << from.name << " into " << to_string(to) << " but there's no such recipe\n" << end();
308   ¦ return false;
309   }
310   const recipe& rrhs = get(Recipe, from.value);
311   const recipe& rlhs = from_reagent(to);
312   for (long int i = 0;  i < min(SIZE(rlhs.ingredients), SIZE(rrhs.ingredients));  ++i) {
313   ¦ if (!types_match(rlhs.ingredients.at(i), rrhs.ingredients.at(i)))
314   ¦ ¦ return false;
315   }
316   for (long int i = 0;  i < min(SIZE(rlhs.products), SIZE(rrhs.products));  ++i) {
317   ¦ if (!types_match(rlhs.products.at(i), rrhs.products.at(i)))
318   ¦ ¦ return false;
319   }
320   return true;
321 }
322 
323 :(scenario call_variable_compound_ingredient)
324 def main [
325   {1: (recipe (address number) -> number)} <- copy f
326   2:&:num <- copy 0
327   3:num <- call {1: (recipe (address number) -> number)}, 2:&:num
328 ]
329 def f x:&:num -> y:num [
330   local-scope
331   load-ingredients
332   y <- copy x
333 ]
334 $error: 0
335 
336 //: make sure we don't accidentally break on a function literal
337 :(scenario jump_forbidden_on_recipe_literals)
338 % Hide_errors = true;
339 def foo [
340   local-scope
341 ]
342 def main [
343   local-scope
344   {
345   ¦ break-if foo
346   }
347 ]
348 # error should be as if foo is not a recipe
349 +error: main: missing type for 'foo' in 'break-if foo'
350 
351 :(before "End JUMP_IF Checks")
352 check_for_recipe_literals(inst, get(Recipe, r));
353 :(before "End JUMP_UNLESS Checks")
354 check_for_recipe_literals(inst, get(Recipe, r));
355 :(code)
356 void check_for_recipe_literals(const instruction& inst, const recipe& caller) {
357   for (int i = 0;  i < SIZE(inst.ingredients);  ++i) {
358   ¦ if (is_mu_recipe(inst.ingredients.at(i))) {
359   ¦ ¦ raise << maybe(caller.name) << "missing type for '" << inst.ingredients.at(i).original_string << "' in '" << inst.original_string << "'\n" << end();
360   ¦ ¦ if (is_present_in_ingredients(caller, inst.ingredients.at(i).name))
361   ¦ ¦ ¦ raise << "  did you forget 'load-ingredients'?\n" << end();
362   ¦ }
363   }
364 }
365 
366 :(scenario load_ingredients_missing_error_3)
367 % Hide_errors = true;
368 def foo {f: (recipe num -> num)} [
369   local-scope
370   b:num <- call f, 1
371 ]
372 +error: foo: missing type for 'f' in 'b:num <- call f, 1'
373 +error:   did you forget 'load-ingredients'?