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 == get(Type_ordinal, "array")) {
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 == get(Type_ordinal, "array")) 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 == get(Type_ordinal, "array")) 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;
342   element.type = copy_array_element(base.type);
343   if (!types_coercible(product, element)) {
344     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();
345     break;
346   }
347   break;
348 }
349 :(before "End Primitive Recipe Implementations")
350 case INDEX: {
351   reagent/*copy*/ base = current_instruction().ingredients.at(0);
352   // Update INDEX base in Run
353   int base_address = base.value;
354   trace(9998, "run") << "base address is " << base_address << end();
355   if (base_address == 0) {
356     raise << maybe(current_recipe_name()) << "tried to access location 0 in '" << to_original_string(current_instruction()) << "'\n" << end();
357     break;
358   }
359   reagent/*copy*/ index = current_instruction().ingredients.at(1);
360   // Update INDEX index in Run
361   vector<double> index_val(read_memory(index));
362   if (index_val.at(0) < 0 || index_val.at(0) >= get_or_insert(Memory, base_address)) {
363     raise << maybe(current_recipe_name()) << "invalid index " << no_scientific(index_val.at(0)) << " in '" << to_original_string(current_instruction()) << "'\n" << end();
364     break;
365   }
366   reagent/*local*/ element;
367   element.type = copy_array_element(base.type);
368   element.set_value(base_address + /*skip length*/1 + index_val.at(0)*size_of(element.type));
369   trace(9998, "run") << "address to copy is " << element.value << end();
370   trace(9998, "run") << "its type is " << to_string(element.type) << end();
371   // Read element
372   products.push_back(read_memory(element));
373   break;
374 }
375 
376 :(code)
377 type_tree* copy_array_element(const type_tree* type) {
378   return new type_tree(*array_element(type));
379 }
380 
381 type_tree* array_element(const type_tree* type) {
382   assert(type->right);
383   if (type->right->atom) {
384     return type->right;
385   }
386   else if (!type->right->right) {
387     return type->right->left;
388   }
389   // hack: support array:num:3 without requiring extra parens
390   else if (type->right->right->left && type->right->right->left->atom && is_integer(type->right->right->left->name)) {
391     assert(!type->right->right->right);
392     return type->right->left;
393   }
394   return type->right;
395 }
396 
397 int array_length(const reagent& x) {
398   // x should already be canonized.
399   // hack: look for length in type
400   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
401       && x.type->right->right->left && x.type->right->right->left->atom && is_integer(x.type->right->right->left->name)) {  // third 'type' is a number
402     // get size from type
403     return to_integer(x.type->right->right->left->name);
404   }
405   // this should never happen at transform time
406   return get_or_insert(Memory, x.value);
407 }
408 
409 :(before "End Unit Tests")
410 void test_array_length_compound() {
411   put(Memory, 1, 3);
412   put(Memory, 2, 14);
413   put(Memory, 3, 15);
414   put(Memory, 4, 16);
415   reagent x("1:array:address:num");  // 3 types, but not a static array
416   populate_value(x);
417   CHECK_EQ(array_length(x), 3);
418 }
419 
420 void test_array_length_static() {
421   reagent x("1:array:num:3");
422   CHECK_EQ(array_length(x), 3);
423 }
424 
425 :(scenario index_truncates)
426 def main [
427   1:array:num:3 <- create-array
428   2:num <- copy 14
429   3:num <- copy 15
430   4:num <- copy 16
431   5:num <- index 1:array:num:3, 1.5  # non-whole number
432 ]
433 # fraction is truncated away
434 +mem: storing 15 in location 5
435 
436 :(scenario index_out_of_bounds)
437 % Hide_errors = true;
438 def main [
439   1:array:num:3 <- create-array
440   2:num <- copy 14
441   3:num <- copy 15
442   4:num <- copy 16
443   5:num <- copy 14
444   6:num <- copy 15
445   7:num <- copy 16
446   index 1:array:num:3, 4  # less than size of array in locations, but larger than its length in elements
447 ]
448 +error: main: invalid index 4 in 'index 1:array:num:3, 4'
449 
450 :(scenario index_out_of_bounds_2)
451 % Hide_errors = true;
452 def main [
453   1:array:point:3 <- create-array
454   2:num <- copy 14
455   3:num <- copy 15
456   4:num <- copy 16
457   5:num <- copy 14
458   6:num <- copy 15
459   7:num <- copy 16
460   index 1:array:point, -1
461 ]
462 +error: main: invalid index -1 in 'index 1:array:point, -1'
463 
464 :(scenario index_product_type_mismatch)
465 % Hide_errors = true;
466 def main [
467   1:array:point:3 <- create-array
468   2:num <- copy 14
469   3:num <- copy 15
470   4:num <- copy 16
471   5:num <- copy 14
472   6:num <- copy 15
473   7:num <- copy 16
474   9:num <- index 1:array:point, 0
475 ]
476 +error: main: 'index' on '1:array:point' can't be saved in '9:num'; type should be 'point'
477 
478 //: we might want to call 'index' without saving the results, say in a sandbox
479 
480 :(scenario index_without_product)
481 def main [
482   1:array:num:3 <- create-array
483   2:num <- copy 14
484   3:num <- copy 15
485   4:num <- copy 16
486   index 1:array:num:3, 0
487 ]
488 # just don't die
489 
490 //:: To write to elements of arrays, use 'put'.
491 
492 :(scenario put_index)
493 def main [
494   1:array:num:3 <- create-array
495   2:num <- copy 14
496   3:num <- copy 15
497   4:num <- copy 16
498   1:array:num <- put-index 1:array:num, 1, 34
499 ]
500 +mem: storing 34 in location 3
501 
502 :(before "End Primitive Recipe Declarations")
503 PUT_INDEX,
504 :(before "End Primitive Recipe Numbers")
505 put(Recipe_ordinal, "put-index", PUT_INDEX);
506 :(before "End Primitive Recipe Checks")
507 case PUT_INDEX: {
508   if (SIZE(inst.ingredients) != 3) {
509     raise << maybe(get(Recipe, r).name) << "'put-index' expects exactly 3 ingredients in '" << to_original_string(inst) << "'\n" << end();
510     break;
511   }
512   reagent/*copy*/ base = inst.ingredients.at(0);
513   // Update PUT_INDEX base in Check
514   if (!is_mu_array(base)) {
515     raise << maybe(get(Recipe, r).name) << "'put-index' on a non-array '" << base.original_string << "'\n" << end();
516     break;
517   }
518   reagent/*copy*/ index = inst.ingredients.at(1);
519   // Update PUT_INDEX index in Check
520   if (!is_mu_number(index)) {
521     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();
522     break;
523   }
524   reagent/*copy*/ value = inst.ingredients.at(2);
525   // Update PUT_INDEX value in Check
526   reagent/*local*/ element;
527   element.type = copy_array_element(base.type);
528   if (!types_coercible(element, value)) {
529     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();
530     break;
531   }
532   if (inst.products.empty()) break;  // no more checks necessary
533   if (inst.products.at(0).name != inst.ingredients.at(0).name) {
534     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();
535     break;
536   }
537   // End PUT_INDEX Product Checks
538   break;
539 }
540 :(before "End Primitive Recipe Implementations")
541 case PUT_INDEX: {
542   reagent/*copy*/ base = current_instruction().ingredients.at(0);
543   // Update PUT_INDEX base in Run
544   int base_address = base.value;
545   if (base_address == 0) {
546     raise << maybe(current_recipe_name()) << "tried to access location 0 in '" << to_original_string(current_instruction()) << "'\n" << end();
547     break;
548   }
549   reagent/*copy*/ index = current_instruction().ingredients.at(1);
550   // Update PUT_INDEX index in Run
551   vector<double> index_val(read_memory(index));
552   if (index_val.at(0) < 0 || index_val.at(0) >= get_or_insert(Memory, base_address)) {
553     raise << maybe(current_recipe_name()) << "invalid index " << no_scientific(index_val.at(0)) << " in '" << to_original_string(current_instruction()) << "'\n" << end();
554     break;
555   }
556   int address = base_address + /*skip length*/1 + index_val.at(0)*size_of(array_element(base.type));
557   trace(9998, "run") << "address to copy to is " << address << end();
558   // optimization: directly write the element rather than updating 'product'
559   // and writing the entire array
560   write_products = false;
561   vector<double> value = read_memory(current_instruction().ingredients.at(2));
562   // Write Memory in PUT_INDEX in Run
563   for (int i = 0;  i < SIZE(value);  ++i) {
564     trace("mem") << "storing " << no_scientific(value.at(i)) << " in location " << address+i << end();
565     put(Memory, address+i, value.at(i));
566   }
567   break;
568 }
569 
570 :(scenario put_index_out_of_bounds)
571 % Hide_errors = true;
572 def main [
573   1:array:point:3 <- create-array
574   2:num <- copy 14
575   3:num <- copy 15
576   4:num <- copy 16
577   5:num <- copy 14
578   6:num <- copy 15
579   7:num <- copy 16
580   8:point <- merge 34, 35
581   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
582 ]
583 +error: main: invalid index 4 in '1:array:point <- put-index 1:array:point, 4, 8:point'
584 
585 :(scenario put_index_out_of_bounds_2)
586 % Hide_errors = true;
587 def main [
588   1:array:point:3 <- create-array
589   2:num <- copy 14
590   3:num <- copy 15
591   4:num <- copy 16
592   5:num <- copy 14
593   6:num <- copy 15
594   7:num <- copy 16
595   8:point <- merge 34, 35
596   1:array:point <- put-index 1:array:point, -1, 8:point
597 ]
598 +error: main: invalid index -1 in '1:array:point <- put-index 1:array:point, -1, 8:point'
599 
600 :(scenario put_index_product_error)
601 % Hide_errors = true;
602 def main [
603   local-scope
604   load-ingredients
605   1:array:num:3 <- create-array
606   4:array:num:3 <- put-index 1:array:num:3, 0, 34
607 ]
608 +error: main: product of 'put-index' must be first ingredient '1:array:num:3', but got '4:array:num:3'
609 
610 //:: compute the length of an array
611 
612 :(scenario array_length)
613 def main [
614   1:array:num:3 <- create-array
615   2:num <- copy 14
616   3:num <- copy 15
617   4:num <- copy 16
618   5:num <- length 1:array:num:3
619 ]
620 +mem: storing 3 in location 5
621 
622 :(before "End Primitive Recipe Declarations")
623 LENGTH,
624 :(before "End Primitive Recipe Numbers")
625 put(Recipe_ordinal, "length", LENGTH);
626 :(before "End Primitive Recipe Checks")
627 case LENGTH: {
628   if (SIZE(inst.ingredients) != 1) {
629     raise << maybe(get(Recipe, r).name) << "'length' expects exactly 2 ingredients in '" << to_original_string(inst) << "'\n" << end();
630     break;
631   }
632   reagent/*copy*/ array = inst.ingredients.at(0);
633   // Update LENGTH array in Check
634   if (!is_mu_array(array)) {
635     raise << "tried to calculate length of non-array '" << array.original_string << "'\n" << end();
636     break;
637   }
638   break;
639 }
640 :(before "End Primitive Recipe Implementations")
641 case LENGTH: {
642   reagent/*copy*/ array = current_instruction().ingredients.at(0);
643   // Update LENGTH array in Run
644   if (array.value == 0) {
645     raise << maybe(current_recipe_name()) << "tried to access location 0 in '" << to_original_string(current_instruction()) << "'\n" << end();
646     break;
647   }
648   products.resize(1);
649   products.at(0).push_back(get_or_insert(Memory, array.value));
650   break;
651 }
652 
653 //: optimization: none of the instructions in this layer use 'ingredients' so
654 //: stop copying potentially huge arrays into it.
655 :(before "End should_copy_ingredients Special-cases")
656 recipe_ordinal r = current_instruction().operation;
657 if (r == CREATE_ARRAY || r == INDEX || r == PUT_INDEX || r == LENGTH)
658   return false;