1 //: Arrays contain a variable number of elements of the same type. Their value
  2 //: starts with the length of the array.
  3 //:
  4 //: You can create arrays of containers, but containers can only contain
  5 //: elements of a fixed size, so you can't create containers containing arrays.
  6 //: Create containers containing addresses to arrays instead.
  7 
  8 //: You can create arrays using 'create-array'.
  9 :(scenario create_array)
 10 def main [
 11   # create an array occupying locations 1 (for the size) and 2-4 (for the elements)
 12   1:array:num:3 <- create-array
 13 ]
 14 +run: creating array of size 4
 15 
 16 :(before "End Primitive Recipe Declarations")
 17 CREATE_ARRAY,
 18 :(before "End Primitive Recipe Numbers")
 19 put(Recipe_ordinal, "create-array", CREATE_ARRAY);
 20 :(before "End Primitive Recipe Checks")
 21 case CREATE_ARRAY: {
 22   if (inst.products.empty()) {
 23     raise << maybe(get(Recipe, r).name) << "'create-array' needs one product and no ingredients but got '" << to_original_string(inst) << '\n' << end();
 24     break;
 25   }
 26   reagent/*copy*/ product = inst.products.at(0);
 27   // Update CREATE_ARRAY product in Check
 28   if (!is_mu_array(product)) {
 29     raise << maybe(get(Recipe, r).name) << "'create-array' cannot create non-array '" << product.original_string << "'\n" << end();
 30     break;
 31   }
 32   if (!product.type->right) {
 33     raise << maybe(get(Recipe, r).name) << "create array of what? '" << to_original_string(inst) << "'\n" << end();
 34     break;
 35   }
 36   // 'create-array' will need to check properties rather than types
 37   type_tree* array_length_from_type = product.type->right->right;
 38   if (!array_length_from_type) {
 39     raise << maybe(get(Recipe, r).name) << "create array of what size? '" << to_original_string(inst) << "'\n" << end();
 40     break;
 41   }
 42   if (!product.type->right->right->atom)
 43     array_length_from_type = array_length_from_type->left;
 44   if (!is_integer(array_length_from_type->name)) {
 45     raise << maybe(get(Recipe, r).name) << "'create-array' product should specify size of array after its element type, but got '" << product.type->right->right->name << "'\n" << end();
 46     break;
 47   }
 48   break;
 49 }
 50 :(before "End Primitive Recipe Implementations")
 51 case CREATE_ARRAY: {
 52   reagent/*copy*/ product = current_instruction().products.at(0);
 53   // Update CREATE_ARRAY product in Run
 54   int base_address = product.value;
 55   type_tree* array_length_from_type = product.type->right->right;
 56   if (!product.type->right->right->atom)
 57     array_length_from_type = array_length_from_type->left;
 58   int array_length = to_integer(array_length_from_type->name);
 59   // initialize array length, so that size_of will work
 60   trace("mem") << "storing " << array_length << " in location " << base_address << end();
 61   put(Memory, base_address, array_length);  // in array elements
 62   int size = size_of(product);  // in locations
 63   trace(9998, "run") << "creating array of size " << size << end();
 64   // initialize array
 65   for (int i = 1;  i <= size_of(product);  ++i)
 66     put(Memory, base_address+i, 0);
 67   // no need to update product
 68   write_products = false;
 69   break;
 70 }
 71 
 72 :(scenario copy_array)
 73 # Arrays can be copied around with a single instruction just like numbers,
 74 # no matter how large they are.
 75 # You don't need to pass the size around, since each array variable stores its
 76 # size in memory at run-time. We'll call a variable with an explicit size a
 77 # 'static' array, and one without a 'dynamic' array since it can contain
 78 # arrays of many different sizes.
 79 def main [
 80   1:array:num:3 <- create-array
 81   2:num <- copy 14
 82   3:num <- copy 15
 83   4:num <- copy 16
 84   5:array:num <- copy 1:array:num:3
 85 ]
 86 +mem: storing 3 in location 5
 87 +mem: storing 14 in location 6
 88 +mem: storing 15 in location 7
 89 +mem: storing 16 in location 8
 90 
 91 :(scenario stash_array)
 92 def main [
 93   1:array:num:3 <- create-array
 94   2:num <- copy 14
 95   3:num <- copy 15
 96   4:num <- copy 16
 97   stash [foo:], 1:array:num:3
 98 ]
 99 +app: foo: 3 14 15 16
100 
101 :(before "End types_coercible Special-cases")
102 if (is_mu_array(from) && is_mu_array(to))
103   return types_strictly_match(array_element(from.type), array_element(to.type));
104 
105 :(before "End size_of(reagent r) Special-cases")
106 if (!r.type->atom && r.type->left->atom && r.type->left->value == Array_type_ordinal) {
107   if (!r.type->right) {
108     raise << maybe(current_recipe_name()) << "'" << r.original_string << "' is an array of what?\n" << end();
109     return 1;
110   }
111   return /*space for length*/1 + array_length(r)*size_of(array_element(r.type));
112 }
113 
114 :(before "End size_of(type) Non-atom Special-cases")
115 if (type->left->value == Array_type_ordinal) return static_array_length(type);
116 :(code)
117 int static_array_length(const type_tree* type) {
118   if (!type->atom && type->right && !type->right->atom && type->right->right && !type->right->right->atom && !type->right->right->right  // exactly 3 types
119       && type->right->right->left && type->right->right->left->atom && is_integer(type->right->right->left->name)) {  // third 'type' is a number
120     // get size from type
121     return to_integer(type->right->right->left->name);
122   }
123   cerr << to_string(type) << '\n';
124   assert(false);
125 }
126 
127 //: disable the size mismatch check for arrays since the destination array
128 //: need not be initialized
129 :(before "End size_mismatch(x) Special-cases")
130 if (x.type && !x.type->atom && x.type->left->value == Array_type_ordinal) return false;
131 
132 //:: arrays inside containers
133 //: arrays are disallowed inside containers unless their length is fixed in
134 //: advance
135 
136 :(scenario container_permits_static_array_element)
137 container foo [
138   x:array:num:3
139 ]
140 $error: 0
141 
142 :(before "End insert_container Special-cases")
143 else if (is_integer(type->name)) {  // sometimes types will contain non-type tags, like numbers for the size of an array
144   type->value = 0;
145 }
146 
147 :(scenario container_disallows_dynamic_array_element)
148 % Hide_errors = true;
149 container foo [
150   x:array:num
151 ]
152 +error: container 'foo' cannot determine size of element 'x'
153 
154 :(before "End Load Container Element Definition")
155 {
156   const type_tree* type = info.elements.back().type;
157   if (type && type->atom && type->name == "array") {
158     raise << "container '" << name << "' doesn't specify type of array elements for '" << info.elements.back().name << "'\n" << end();
159     continue;
160   }
161   if (type && !type->atom && type->left->atom && type->left->name == "array") {
162     if (!type->right) {
163       raise << "container '" << name << "' doesn't specify type of array elements for '" << info.elements.back().name << "'\n" << end();
164       continue;
165     }
166     if (!type->right->right || !is_integer(type->right->right->left->name)) {  // array has no length
167       raise << "container '" << name << "' cannot determine size of element '" << info.elements.back().name << "'\n" << end();
168       continue;
169     }
170   }
171 }
172 
173 //: disable the size mismatch check for 'merge' instructions since containers
174 //: can contain arrays, and since we already do plenty of checking for them
175 :(before "End size_mismatch(x) Special-cases")
176 if (current_call().running_step_index < SIZE(get(Recipe, current_call().running_recipe).steps)
177     && current_instruction().operation == MERGE) {
178   return false;
179 }
180 
181 :(scenario merge_static_array_into_container)
182 container foo [
183   x:num
184   y:array:num:3
185 ]
186 def main [
187   1:array:num:3 <- create-array
188   10:foo <- merge 34, 1:array:num:3
189 ]
190 # no errors
191 
192 :(scenario code_inside_container)
193 % Hide_errors = true;
194 container card [
195   rank:num <- next-ingredient
196 ]
197 def foo [
198   1:card <- merge 3
199   2:num <- get 1:card rank:offset
200 ]
201 # shouldn't die
202 
203 //:: containers inside arrays
204 //: make sure we compute container sizes inside arrays
205 
206 :(before "End compute_container_sizes Non-atom Special-cases")
207 else if (type->left->name == "array")
208   compute_container_sizes(array_element(type), pending_metadata, location_for_error_messages);
209 
210 :(before "End Unit Tests")
211 void test_container_sizes_from_array() {
212   // a container we don't have the size for
213   reagent container("x:point");
214   CHECK(!contains_key(Container_metadata, container.type));
215   // scanning an array of the container precomputes the size of the container
216   reagent r("x:array:point");
217   compute_container_sizes(r, "");
218   CHECK(contains_key(Container_metadata, container.type));
219   CHECK_EQ(get(Container_metadata, container.type).size, 2);
220 }
221 
222 void test_container_sizes_from_address_to_array() {
223   // a container we don't have the size for
224   reagent container("x:point");
225   CHECK(!contains_key(Container_metadata, container.type));
226   // scanning an address to an array of the container precomputes the size of the container
227   reagent r("x:address:array:point");
228   compute_container_sizes(r, "");
229   CHECK(contains_key(Container_metadata, container.type));
230   CHECK_EQ(get(Container_metadata, container.type).size, 2);
231 }
232 
233 void test_container_sizes_from_static_array() {
234   // a container we don't have the size for
235   reagent container("x:point");
236   int old_size = SIZE(Container_metadata);
237   // scanning an address to an array of the container precomputes the size of the container
238   reagent r("x:array:point:10");
239   compute_container_sizes(r, "");
240   CHECK(contains_key(Container_metadata, container.type));
241   CHECK_EQ(get(Container_metadata, container.type).size, 2);
242   // no non-container types precomputed
243   CHECK_EQ(SIZE(Container_metadata)-old_size, 1);
244 }
245 
246 void test_container_sizes_from_address_to_static_array() {
247   // a container we don't have the size for
248   reagent container("x:point");
249   int old_size = SIZE(Container_metadata);
250   // scanning an address to an array of the container precomputes the size of the container
251   reagent r("x:address:array:point:10");
252   compute_container_sizes(r, "");
253   CHECK(contains_key(Container_metadata, container.type));
254   CHECK_EQ(get(Container_metadata, container.type).size, 2);
255   // no non-container types precomputed
256   CHECK_EQ(SIZE(Container_metadata)-old_size, 1);
257 }
258 
259 void test_container_sizes_from_repeated_address_and_array_types() {
260   // a container we don't have the size for
261   reagent container("x:point");
262   int old_size = SIZE(Container_metadata);
263   // scanning repeated address and array types modifying the container precomputes the size of the container
264   reagent r("x:address:array:address:array:point:10");
265   compute_container_sizes(r, "");
266   CHECK(contains_key(Container_metadata, container.type));
267   CHECK_EQ(get(Container_metadata, container.type).size, 2);
268   // no non-container types precomputed
269   CHECK_EQ(SIZE(Container_metadata)-old_size, 1);
270 }
271 
272 void test_container_sizes_on_unknown_type() {
273   // a container we don't have the size for
274   reagent container("x:point");
275   int old_size = SIZE(Container_metadata);
276   // scanning address to array with a typo
277   reagent r("x:address:array:adress:number");
278   compute_container_sizes(r, "");  // should not crash
279   // no non-container types precomputed
280   CHECK_EQ(SIZE(Container_metadata), old_size);
281 }
282 
283 //:: To access elements of an array, use 'index'
284 
285 :(scenario index)
286 def main [
287   1:array:num:3 <- create-array
288   2:num <- copy 14
289   3:num <- copy 15
290   4:num <- copy 16
291   5:num <- index 1:array:num:3, 0/index  # the index must be a non-negative whole number
292 ]
293 +mem: storing 14 in location 5
294 
295 :(scenario index_compound_element)
296 def main [
297   {1: (array (address number) 3)} <- create-array
298   2:num <- copy 14
299   3:num <- copy 15
300   4:num <- copy 16
301   5:address:num <- index {1: (array (address number) 3)}, 0
302 ]
303 +mem: storing 14 in location 5
304 
305 :(scenario index_direct_offset)
306 def main [
307   1:array:num:3 <- create-array
308   2:num <- copy 14
309   3:num <- copy 15
310   4:num <- copy 16
311   5:num <- copy 0
312   6:num <- index 1:array:num, 5:num
313 ]
314 +mem: storing 14 in location 6
315 
316 :(before "End Primitive Recipe Declarations")
317 INDEX,
318 :(before "End Primitive Recipe Numbers")
319 put(Recipe_ordinal, "index", INDEX);
320 :(before "End Primitive Recipe Checks")
321 case INDEX: {
322   if (SIZE(inst.ingredients) != 2) {
323     raise << maybe(get(Recipe, r).name) << "'index' expects exactly 2 ingredients in '" << to_original_string(inst) << "'\n" << end();
324     break;
325   }
326   reagent/*copy*/ base = inst.ingredients.at(0);
327   // Update INDEX base in Check
328   if (!is_mu_array(base)) {
329     raise << maybe(get(Recipe, r).name) << "'index' on a non-array '" << base.original_string << "'\n" << end();
330     break;
331   }
332   reagent/*copy*/ index = inst.ingredients.at(1);
333   // Update INDEX index in Check
334   if (!is_mu_number(index)) {
335     raise << maybe(get(Recipe, r).name) << "second ingredient of 'index' should be a number, but got '" << index.original_string << "'\n" << end();
336     break;
337   }
338   if (inst.products.empty()) break;
339   reagent/*copy*/ product = inst.products.at(0);
340   // Update INDEX product in Check
341   reagent/*local*/ element(copy_array_element(base.type));
342   if (!types_coercible(product, element)) {
343     raise << maybe(get(Recipe, r).name) << "'index' on '" << base.original_string << "' can't be saved in '" << product.original_string << "'; type should be '" << names_to_string_without_quotes(element.type) << "'\n" << end();
344     break;
345   }
346   break;
347 }
348 :(before "End Primitive Recipe Implementations")
349 case INDEX: {
350   reagent/*copy*/ base = current_instruction().ingredients.at(0);
351   // Update INDEX base in Run
352   int base_address = base.value;
353   trace(9998, "run") << "base address is " << base_address << end();
354   if (base_address == 0) {
355     raise << maybe(current_recipe_name()) << "tried to access location 0 in '" << to_original_string(current_instruction()) << "'\n" << end();
356     break;
357   }
358   reagent/*copy*/ index = current_instruction().ingredients.at(1);
359   // Update INDEX index in Run
360   vector<double> index_val(read_memory(index));
361   if (index_val.at(0) < 0 || index_val.at(0) >= get_or_insert(Memory, base_address)) {
362     raise << maybe(current_recipe_name()) << "invalid index " << no_scientific(index_val.at(0)) << " in '" << to_original_string(current_instruction()) << "'\n" << end();
363     break;
364   }
365   reagent/*local*/ element(copy_array_element(base.type));
366   element.set_value(base_address + /*skip length*/1 + index_val.at(0)*size_of(element.type));
367   trace(9998, "run") << "address to copy is " << element.value << end();
368   trace(9998, "run") << "its type is " << to_string(element.type) << end();
369   // Read element
370   products.push_back(read_memory(element));
371   break;
372 }
373 
374 :(code)
375 type_tree* copy_array_element(const type_tree* type) {
376   return new type_tree(*array_element(type));
377 }
378 
379 type_tree* array_element(const type_tree* type) {
380   assert(type->right);
381   if (type->right->atom) {
382     return type->right;
383   }
384   else if (!type->right->right) {
385     return type->right->left;
386   }
387   // hack: support array:num:3 without requiring extra parens
388   else if (type->right->right->left && type->right->right->left->atom && is_integer(type->right->right->left->name)) {
389     assert(!type->right->right->right);
390     return type->right->left;
391   }
392   return type->right;
393 }
394 
395 int array_length(const reagent& x) {
396   // x should already be canonized.
397   // hack: look for length in type
398   if (!x.type->atom && x.type->right && !x.type->right->atom && x.type->right->right && !x.type->right->right->atom && !x.type->right->right->right  // exactly 3 types
399       && x.type->right->right->left && x.type->right->right->left->atom && is_integer(x.type->right->right->left->name)) {  // third 'type' is a number
400     // get size from type
401     return to_integer(x.type->right->right->left->name);
402   }
403   // this should never happen at transform time
404   return get_or_insert(Memory, x.value);
405 }
406 
407 :(before "End Unit Tests")
408 void test_array_length_compound() {
409   put(Memory, 1, 3);
410   put(Memory, 2, 14);
411   put(Memory, 3, 15);
412   put(Memory, 4, 16);
413   reagent x("1:array:address:num");  // 3 types, but not a static array
414   populate_value(x);
415   CHECK_EQ(array_length(x), 3);
416 }
417 
418 void test_array_length_static() {
419   reagent x("1:array:num:3");
420   CHECK_EQ(array_length(x), 3);
421 }
422 
423 :(scenario index_truncates)
424 def main [
425   1:array:num:3 <- create-array
426   2:num <- copy 14
427   3:num <- copy 15
428   4:num <- copy 16
429   5:num <- index 1:array:num:3, 1.5  # non-whole number
430 ]
431 # fraction is truncated away
432 +mem: storing 15 in location 5
433 
434 :(scenario index_out_of_bounds)
435 % Hide_errors = true;
436 def main [
437   1:array:num:3 <- create-array
438   2:num <- copy 14
439   3:num <- copy 15
440   4:num <- copy 16
441   5:num <- copy 14
442   6:num <- copy 15
443   7:num <- copy 16
444   index 1:array:num:3, 4  # less than size of array in locations, but larger than its length in elements
445 ]
446 +error: main: invalid index 4 in 'index 1:array:num:3, 4'
447 
448 :(scenario index_out_of_bounds_2)
449 % Hide_errors = true;
450 def main [
451   1:array:point:3 <- create-array
452   2:num <- copy 14
453   3:num <- copy 15
454   4:num <- copy 16
455   5:num <- copy 14
456   6:num <- copy 15
457   7:num <- copy 16
458   index 1:array:point, -1
459 ]
460 +error: main: invalid index -1 in 'index 1:array:point, -1'
461 
462 :(scenario index_product_type_mismatch)
463 % Hide_errors = true;
464 def main [
465   1:array:point:3 <- create-array
466   2:num <- copy 14
467   3:num <- copy 15
468   4:num <- copy 16
469   5:num <- copy 14
470   6:num <- copy 15
471   7:num <- copy 16
472   9:num <- index 1:array:point, 0
473 ]
474 +error: main: 'index' on '1:array:point' can't be saved in '9:num'; type should be 'point'
475 
476 //: we might want to call 'index' without saving the results, say in a sandbox
477 
478 :(scenario index_without_product)
479 def main [
480   1:array:num:3 <- create-array
481   2:num <- copy 14
482   3:num <- copy 15
483   4:num <- copy 16
484   index 1:array:num:3, 0
485 ]
486 # just don't die
487 
488 //:: To write to elements of arrays, use 'put'.
489 
490 :(scenario put_index)
491 def main [
492   1:array:num:3 <- create-array
493   2:num <- copy 14
494   3:num <- copy 15
495   4:num <- copy 16
496   1:array:num <- put-index 1:array:num, 1, 34
497 ]
498 +mem: storing 34 in location 3
499 
500 :(before "End Primitive Recipe Declarations")
501 PUT_INDEX,
502 :(before "End Primitive Recipe Numbers")
503 put(Recipe_ordinal, "put-index", PUT_INDEX);
504 :(before "End Primitive Recipe Checks")
505 case PUT_INDEX: {
506   if (SIZE(inst.ingredients) != 3) {
507     raise << maybe(get(Recipe, r).name) << "'put-index' expects exactly 3 ingredients in '" << to_original_string(inst) << "'\n" << end();
508     break;
509   }
510   reagent/*copy*/ base = inst.ingredients.at(0);
511   // Update PUT_INDEX base in Check
512   if (!is_mu_array(base)) {
513     raise << maybe(get(Recipe, r).name) << "'put-index' on a non-array '" << base.original_string << "'\n" << end();
514     break;
515   }
516   reagent/*copy*/ index = inst.ingredients.at(1);
517   // Update PUT_INDEX index in Check
518   if (!is_mu_number(index)) {
519     raise << maybe(get(Recipe, r).name) << "second ingredient of 'put-index' should have type 'number', but got '" << inst.ingredients.at(1).original_string << "'\n" << end();
520     break;
521   }
522   reagent/*copy*/ value = inst.ingredients.at(2);
523   // Update PUT_INDEX value in Check
524   reagent/*local*/ element(copy_array_element(base.type));
525   if (!types_coercible(element, value)) {
526     raise << maybe(get(Recipe, r).name) << "'put-index " << base.original_string << ", " << inst.ingredients.at(1).original_string << "' should store " << names_to_string_without_quotes(element.type) << " but '" << value.name << "' has type " << names_to_string_without_quotes(value.type) << '\n' << end();
527     break;
528   }
529   if (inst.products.empty()) break;  // no more checks necessary
530   if (inst.products.at(0).name != inst.ingredients.at(0).name) {
531     raise << maybe(get(Recipe, r).name) << "product of 'put-index' must be first ingredient '" << inst.ingredients.at(0).original_string << "', but got '" << inst.products.at(0).original_string << "'\n" << end();
532     break;
533   }
534   // End PUT_INDEX Product Checks
535   break;
536 }
537 :(before "End Primitive Recipe Implementations")
538 case PUT_INDEX: {
539   reagent/*copy*/ base = current_instruction().ingredients.at(0);
540   // Update PUT_INDEX base in Run
541   int base_address = base.value;
542   if (base_address == 0) {
543     raise << maybe(current_recipe_name()) << "tried to access location 0 in '" << to_original_string(current_instruction()) << "'\n" << end();
544     break;
545   }
546   reagent/*copy*/ index = current_instruction().ingredients.at(1);
547   // Update PUT_INDEX index in Run
548   vector<double> index_val(read_memory(index));
549   if (index_val.at(0) < 0 || index_val.at(0) >= get_or_insert(Memory, base_address)) {
550     raise << maybe(current_recipe_name()) << "invalid index " << no_scientific(index_val.at(0)) << " in '" << to_original_string(current_instruction()) << "'\n" << end();
551     break;
552   }
553   int address = base_address + /*skip length*/1 + index_val.at(0)*size_of(array_element(base.type));
554   trace(9998, "run") << "address to copy to is " << address << end();
555   // optimization: directly write the element rather than updating 'product'
556   // and writing the entire array
557   write_products = false;
558   vector<double> value = read_memory(current_instruction().ingredients.at(2));
559   // Write Memory in PUT_INDEX in Run
560   for (int i = 0;  i < SIZE(value);  ++i) {
561     trace("mem") << "storing " << no_scientific(value.at(i)) << " in location " << address+i << end();
562     put(Memory, address+i, value.at(i));
563   }
564   break;
565 }
566 
567 :(scenario put_index_out_of_bounds)
568 % Hide_errors = true;
569 def main [
570   1:array:point:3 <- create-array
571   2:num <- copy 14
572   3:num <- copy 15
573   4:num <- copy 16
574   5:num <- copy 14
575   6:num <- copy 15
576   7:num <- copy 16
577   8:point <- merge 34, 35
578   1:array:point <- put-index 1:array:point, 4, 8:point  # '4' is less than size of array in locations, but larger than its length in elements
579 ]
580 +error: main: invalid index 4 in '1:array:point <- put-index 1:array:point, 4, 8:point'
581 
582 :(scenario put_index_out_of_bounds_2)
583 % Hide_errors = true;
584 def main [
585   1:array:point:3 <- create-array
586   2:num <- copy 14
587   3:num <- copy 15
588   4:num <- copy 16
589   5:num <- copy 14
590   6:num <- copy 15
591   7:num <- copy 16
592   8:point <- merge 34, 35
593   1:array:point <- put-index 1:array:point, -1, 8:point
594 ]
595 +error: main: invalid index -1 in '1:array:point <- put-index 1:array:point, -1, 8:point'
596 
597 :(scenario put_index_product_error)
598 % Hide_errors = true;
599 def main [
600   local-scope
601   load-ingredients
602   1:array:num:3 <- create-array
603   4:array:num:3 <- put-index 1:array:num:3, 0, 34
604 ]
605 +error: main: product of 'put-index' must be first ingredient '1:array:num:3', but got '4:array:num:3'
606 
607 //:: compute the length of an array
608 
609 :(scenario array_length)
610 def main [
611   1:array:num:3 <- create-array
612   2:num <- copy 14
613   3:num <- copy 15
614   4:num <- copy 16
615   5:num <- length 1:array:num:3
616 ]
617 +mem: storing 3 in location 5
618 
619 :(before "End Primitive Recipe Declarations")
620 LENGTH,
621 :(before "End Primitive Recipe Numbers")
622 put(Recipe_ordinal, "length", LENGTH);
623 :(before "End Primitive Recipe Checks")
624 case LENGTH: {
625   if (SIZE(inst.ingredients) != 1) {
626     raise << maybe(get(Recipe, r).name) << "'length' expects exactly 2 ingredients in '" << to_original_string(inst) << "'\n" << end();
627     break;
628   }
629   reagent/*copy*/ array = inst.ingredients.at(0);
630   // Update LENGTH array in Check
631   if (!is_mu_array(array)) {
632     raise << "tried to calculate length of non-array '" << array.original_string << "'\n" << end();
633     break;
634   }
635   break;
636 }
637 :(before "End Primitive Recipe Implementations")
638 case LENGTH: {
639   reagent/*copy*/ array = current_instruction().ingredients.at(0);
640   // Update LENGTH array in Run
641   if (array.value == 0) {
642     raise << maybe(current_recipe_name()) << "tried to access location 0 in '" << to_original_string(current_instruction()) << "'\n" << end();
643     break;
644   }
645   products.resize(1);
646   products.at(0).push_back(get_or_insert(Memory, array.value));
647   break;
648 }
649 
650 //: optimization: none of the instructions in this layer use 'ingredients' so
651 //: stop copying potentially huge arrays into it.
652 :(before "End should_copy_ingredients Special-cases")
653 recipe_ordinal r = current_instruction().operation;
654 if (r == CREATE_ARRAY || r == INDEX || r == PUT_INDEX || r == LENGTH)
655   return false;