about summary refs log tree commit diff stats
path: root/040brace.cc
Commit message (Expand)AuthorAgeFilesLines
* 5001 - drop the :(scenario) DSLKartik Agaram2019-03-121-229/+321
* 4987 - support `browse_trace` tool in SubXKartik Agaram2019-02-251-8/+8
* 4466Kartik Agaram2018-08-031-6/+6
* 4223Kartik K. Agaram2018-03-141-2/+1
* 3987Kartik K. Agaram2017-09-011-2/+2
* 3811Kartik K. Agaram2017-04-041-1/+1
* 3810Kartik K. Agaram2017-04-041-5/+5
* 3742 - move instruction.old_name to a later layerKartik K. Agaram2017-02-061-15/+16
* 3657 - better error messageKartik K. Agaram2016-11-101-3/+5
* 3561Kartik K. Agaram2016-10-221-1/+1
* 3549Kartik K. Agaram2016-10-221-2/+2
* 3522Kartik K. Agaram2016-10-191-3/+3
* 3520Kartik K. Agaram2016-10-181-1/+0
* 3381Kartik K. Agaram2016-09-171-3/+3
* 3380Kartik K. Agaram2016-09-171-46/+46
* 3083Kartik K. Agaram2016-07-011-1/+1
* 2990Kartik K. Agaram2016-05-201-5/+5
* 2931 - be explicit about making copiesKartik K. Agaram2016-05-061-1/+1
* 2874Kartik K. Agaram2016-04-271-20/+20
* 2873 - fix a bug in converting conditional returnsKartik K. Agaram2016-04-271-0/+85
* 2773 - switch to 'int'Kartik K. Agaram2016-03-131-8/+8
* 2735 - define recipes using 'def'Kartik K. Agaram2016-03-081-17/+17
* 2712Kartik K. Agaram2016-02-261-4/+4
* 2701 - turn some warnings into errorsKartik K. Agaram2016-02-251-1/+1
* 2685Kartik K. Agaram2016-02-221-1/+1
* 2681 - drop reagent types from reagent propertiesKartik K. Agaram2016-02-211-1/+0
* 2677Kartik K. Agaram2016-02-201-1/+1
* 2494Kartik K. Agaram2015-11-281-1/+1
* 2397Kartik K. Agaram2015-11-081-0/+1
* 2383 - new concern: idempotence of transformsKartik K. Agaram2015-11-061-1/+1
* 2382Kartik K. Agaram2015-11-061-0/+1
* 2377 - stop using operator[] in mapKartik K. Agaram2015-11-061-16/+16
* 2372Kartik K. Agaram2015-11-051-0/+4
* 2360Kartik K. Agaram2015-11-041-3/+35
* 2358 - starting to tackle the phase ordering problemKartik K. Agaram2015-11-041-3/+2
* 2349Kartik K. Agaram2015-11-021-19/+0
* 2348Kartik K. Agaram2015-11-021-5/+5
* 2321 - more preparations for static dispatchKartik K. Agaram2015-10-291-29/+22
* 2311Kartik K. Agaram2015-10-291-55/+55
* 2277 - reagents now have a tree of typesKartik K. Agaram2015-10-251-1/+1
* 2258 - separate warnings from errorsKartik K. Agaram2015-10-061-10/+10
* 2226 - standardize warning formatKartik K. Agaram2015-10-011-2/+2
* 2199 - stop printing numbers in scientific notationKartik K. Agaram2015-09-141-3/+3
* 2137Kartik K. Agaram2015-09-031-1/+0
* 2095Kartik K. Agaram2015-08-281-2/+0
* 2075Kartik K. Agaram2015-08-241-0/+16
* 2043Kartik K. Agaram2015-08-201-1/+1
* 1962Kartik K. Agaram2015-08-091-2/+2
* 1868 - start using naked literals everywhereKartik K. Agaram2015-07-281-38/+38
* 1847Kartik K. Agaram2015-07-251-2/+2
32b18'>^
d82c1609 ^




23d3a022 ^
c9cf358c ^
dc9afcbd ^


23d3a022 ^






3473c63a ^

dc9afcbd ^
3473c63a ^

dc9afcbd ^
3473c63a ^

dc9afcbd ^
3473c63a ^



db69ad7a ^

dc9afcbd ^











dc9afcbd ^


1fb0cf9e ^
a97a00df ^
dc9afcbd ^








6c96a437 ^
dc9afcbd ^











4a943d4e ^
















































dc9afcbd ^







4a943d4e ^















dc9afcbd ^







4a943d4e ^














fc19980d ^










4a943d4e ^











dc9afcbd ^



4a943d4e ^

































b9a78a84 ^




dc9afcbd ^
4a943d4e ^

















dc9afcbd ^












4a943d4e ^





















































fc19980d ^










4a943d4e ^















88298924 ^
dc9afcbd ^











4a943d4e ^

















dc9afcbd ^





4a943d4e ^



















































dc9afcbd ^














4a943d4e ^














dc9afcbd ^





4a943d4e ^















dc9afcbd ^


555d95c1 ^
dc9afcbd ^



9dcbec39 ^
dc9afcbd ^









3473c63a ^
dc9afcbd ^



















3473c63a ^
dc9afcbd ^




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
                                                                 



                                                                              















                                                                            
 
                                         



                                                                       












                                        
 
                                                
            

                                          













                                                                        
 
                                                    




























                                                                            
 

                            
                              




                                   
                                                                               

                                                                                                                                



                     

                                                                            

           
                                                

 
                                                          
                                                           
                                                    
                                                                                                                 
                   
                                         






                                                                                                                                       
   


                                                                
                                                                                                                   




                                                                                                                                                  
                                                       
                               


                     






                                                               

                                       
 

                                                 
 

                                                   
 



                                                   

                                                     











                                                   


                                     
                                                                                                                         
                                                                                                                                    








                                  
                                                                                                                











                                                                                          
















































                                                              







                                      















                                                                    







                                     














                                                                                                          










                                                                                                                                                                                                            











                                                                                       



                                      

































                                                   




                                               
 

















                                                












                                         





















































                                                                                                                










                                                                                                                                                                                                                  















                                                                                   
 











                                           

















                                                  





                                        



















































                                                                                            














                                                 














                                                             





                                                 















                                                                  


                               
                                  



                                                                     
                                                                   









                                                
                                                                           



















                                                                                                                                                                                                                                                                                               
                                                                  




                    
//: Go from an address to the payload it points at using /lookup.
//:
//: The tests in this layer use unsafe operations so as to stay decoupled from
//: 'new'.

void test_copy_indirect() {
  run(
      "def main [\n"
      // skip alloc id for 10:&:num
      "  11:num <- copy 20\n"
      // skip alloc id for payload
      "  21:num <- copy 94\n"
      // Treat locations 10 and 11 as an address to look up, pointing at the
      // payload in locations 20 and 21.
      "  30:num <- copy 10:&:num/lookup\n"
      "]\n"
  );
  CHECK_TRACE_CONTENTS(
      "mem: storing 94 in location 30\n"
  );
}

:(before "End Preprocess read_memory(x)")
canonize(x);

//: similarly, write to addresses pointing at other locations using the
//: 'lookup' property
:(code)
void test_store_indirect() {
  run(
      "def main [\n"
      // skip alloc id for 10:&:num
      "  11:num <- copy 10\n"
      "  10:&:num/lookup <- copy 94\n"
      "]\n"
  );
  CHECK_TRACE_CONTENTS(
      "mem: storing 94 in location 11\n"
  );
}

:(before "End Preprocess write_memory(x, data)")
canonize(x);

//: writes to address 0 always loudly fail
:(code)
void test_store_to_0_fails() {
  Hide_errors = true;
  run(
      "def main [\n"
      "  10:&:num <- copy null\n"
      "  10:&:num/lookup <- copy 94\n"
      "]\n"
  );
  CHECK_TRACE_DOESNT_CONTAIN("mem: storing 94 in location 0");
  CHECK_TRACE_CONTENTS(
      "error: main: tried to lookup 0 in '10:&:num/lookup <- copy 94'\n"
  );
}

//: attempts to /lookup address 0 always loudly fail
void test_lookup_0_fails() {
  Hide_errors = true;
  run(
      "def main [\n"
      "  10:&:num <- copy null\n"
      "  20:num <- copy 10:&:num/lookup\n"
      "]\n"
  );
  CHECK_TRACE_CONTENTS(
      "error: main: tried to lookup 0 in '20:num <- copy 10:&:num/lookup'\n"
  );
}

void test_lookup_0_dumps_callstack() {
  Hide_errors = true;
  run(
      "def main [\n"
      "  foo null\n"
      "]\n"
      "def foo [\n"
      "  10:&:num <- next-input\n"
      "  20:num <- copy 10:&:num/lookup\n"
      "]\n"
  );
  CHECK_TRACE_CONTENTS(
      "error: foo: tried to lookup 0 in '20:num <- copy 10:&:num/lookup'\n"
      "error:   called from main: foo null\n"
  );
}

void canonize(reagent& x) {
  if (is_literal(x)) return;
  // Begin canonize(x) Lookups
  while (has_property(x, "lookup"))
    lookup_memory(x);
}

void lookup_memory(reagent& x) {
  if (!x.type || x.type->atom || x.type->left->value != Address_type_ordinal) {
    raise << maybe(current_recipe_name()) << "tried to lookup '" << x.original_string << "' but it isn't an address\n" << end();
    dump_callstack();
    return;
  }
  // compute value
  if (x.value == 0) {
    raise << maybe(current_recipe_name()) << "tried to lookup 0\n" << end();
    dump_callstack();
    return;
  }
  lookup_memory_core(x, /*check_for_null*/true);
}

void lookup_memory_core(reagent& x, bool check_for_null) {
  double address = x.value + /*skip alloc id in address*/1;
  double new_value = get_or_insert(Memory, address);
  trace(Callstack_depth+1, "mem") << "location " << address << " contains " << no_scientific(new_value) << end();
  // check for null
  if (check_for_null && new_value == 0) {
    if (Current_routine) {
      raise << maybe(current_recipe_name()) << "tried to lookup 0 in '" << to_original_string(current_instruction()) << "'\n" << end();
      dump_callstack();
    }
    else {
      raise << "tried to lookup 0\n" << end();
    }
  }
  // validate alloc-id
  double alloc_id_in_address = get_or_insert(Memory, x.value);
  double alloc_id_in_payload = get_or_insert(Memory, new_value);
//?   cerr << x.value << ": " << alloc_id_in_address << " vs " << new_value << ": " << alloc_id_in_payload << '\n';
  if (alloc_id_in_address != alloc_id_in_payload) {
      raise << maybe(current_recipe_name()) << "address is already abandoned in '" << to_original_string(current_instruction()) << "'\n" << end();
      dump_callstack();
  }
  // all well; complete the lookup
  x.set_value(new_value+/*skip alloc id in payload*/1);
  drop_from_type(x, "address");
  drop_one_lookup(x);
}

:(after "Begin types_coercible(reagent to, reagent from)")
if (!canonize_type(to)) return false;
if (!canonize_type(from)) return false;
:(after "Begin types_match(reagent to, reagent from)")
if (!canonize_type(to)) return false;
if (!canonize_type(from)) return false;
:(after "Begin types_strictly_match(reagent to, reagent from)")
if (!canonize_type(to)) return false;
if (!canonize_type(from)) return false;

:(before "End Preprocess is_mu_array(reagent r)")
if (!canonize_type(r)) return false;

:(before "End Preprocess is_mu_address(reagent r)")
if (!canonize_type(r)) return false;

:(before "End Preprocess is_mu_number(reagent r)")
if (!canonize_type(r)) return false;
:(before "End Preprocess is_mu_boolean(reagent r)")
if (!canonize_type(r)) return false;
:(before "End Preprocess is_mu_character(reagent r)")
if (!canonize_type(r)) return false;

:(after "Update product While Type-checking Merge")
if (!canonize_type(product)) continue;

:(before "End Compute Call Ingredient")
canonize_type(ingredient);
:(before "End Preprocess NEXT_INGREDIENT product")
canonize_type(product);
:(before "End Check RETURN Copy(lhs, rhs)
canonize_type(lhs);
canonize_type(rhs);

:(code)
bool canonize_type(reagent& r) {
  while (has_property(r, "lookup")) {
    if (!r.type || r.type->atom || !r.type->left || !r.type->left->atom || r.type->left->value != Address_type_ordinal) {
      raise << "cannot perform lookup on '" << r.name << "' because it has non-address type " << to_string(r.type) << '\n' << end();
      return false;
    }
    drop_from_type(r, "address");
    drop_one_lookup(r);
  }
  return true;
}

void drop_one_lookup(reagent& r) {
  for (vector<pair<string, string_tree*> >::iterator p = r.properties.begin();  p != r.properties.end();  ++p) {
    if (p->first == "lookup") {
      r.properties.erase(p);
      return;
    }
  }
  assert(false);
}

//: Tedious fixup to support addresses in container/array instructions of previous layers.
//: Most instructions don't require fixup if they use the 'ingredients' and
//: 'products' variables in run_current_routine().

void test_get_indirect() {
  run(
      "def main [\n"
      // skip alloc id for 10:&:point
      "  11:num <- copy 20\n"
      // skip alloc id for payload
      "  21:num <- copy 94\n"
      "  22:num <- copy 95\n"
      "  30:num <- get 10:&:point/lookup, 0:offset\n"
      "]\n"
  );
  CHECK_TRACE_CONTENTS(
      "mem: storing 94 in location 30\n"
  );
}

void test_get_indirect2() {
  run(
      "def main [\n"
      // skip alloc id for 10:&:point
      "  11:num <- copy 20\n"
      // skip alloc id for payload
      "  21:num <- copy 94\n"
      "  22:num <- copy 95\n"
      // skip alloc id for destination
      "  31:num <- copy 40\n"
      "  30:&:num/lookup <- get 10:&:point/lookup, 0:offset\n"
      "]\n"
  );
  CHECK_TRACE_CONTENTS(
      "mem: storing 94 in location 41\n"
  );
}

void test_include_nonlookup_properties() {
  run(
      "def main [\n"
      // skip alloc id for 10:&:point
      "  11:num <- copy 20\n"
      // skip alloc id for payload
      "  21:num <- copy 94\n"
      "  22:num <- copy 95\n"
      "  30:num <- get 10:&:point/lookup/foo, 0:offset\n"
      "]\n"
  );
  CHECK_TRACE_CONTENTS(
      "mem: storing 94 in location 30\n"
  );
}

:(after "Update GET base in Check")
if (!canonize_type(base)) break;
:(after "Update GET product in Check")
if (!canonize_type(product)) break;
:(after "Update GET base in Run")
canonize(base);

:(code)
void test_put_indirect() {
  run(
      "def main [\n"
      // skip alloc id for 10:&:point
      "  11:num <- copy 20\n"
      // skip alloc id for payload
      "  21:num <- copy 94\n"
      "  22:num <- copy 95\n"
      "  10:&:point/lookup <- put 10:&:point/lookup, 0:offset, 96\n"
      "]\n"
  );
  CHECK_TRACE_CONTENTS(
      "mem: storing 96 in location 21\n"
  );
}

:(after "Update PUT base in Check")
if (!canonize_type(base)) break;
:(after "Update PUT offset in Check")
if (!canonize_type(offset)) break;
:(after "Update PUT base in Run")
canonize(base);

:(code)
void test_put_product_error_with_lookup() {
  Hide_errors = true;
  run(
      "def main [\n"
      "  11:num <- copy 20\n"
      "  21:num <- copy 94\n"
      "  22:num <- copy 95\n"
      "  10:&:point <- put 10:&:point/lookup, x:offset, 96\n"
      "]\n"
  );
  CHECK_TRACE_CONTENTS(
      "error: main: product of 'put' must be first ingredient '10:&:point/lookup', but got '10:&:point'\n"
  );
}

:(before "End PUT Product Checks")
reagent/*copy*/ p = inst.products.at(0);
if (!canonize_type(p)) break;  // error raised elsewhere
reagent/*copy*/ i = inst.ingredients.at(0);
if (!canonize_type(i)) break;  // error raised elsewhere
if (!types_strictly_match(p, i)) {
  raise << maybe(get(Recipe, r).name) << "product of 'put' must be first ingredient '" << inst.ingredients.at(0).original_string << "', but got '" << inst.products.at(0).original_string << "'\n" << end();
  break;
}

:(code)
void test_new_error() {
  Hide_errors = true;
  run(
      "def main [\n"
      "  1:num/raw <- new num:type\n"
      "]\n"
  );
  CHECK_TRACE_CONTENTS(
      "error: main: product of 'new' has incorrect type: '1:num/raw <- new num:type'\n"
  );
}

:(after "Update NEW product in Check")
canonize_type(product);

:(code)
void test_copy_array_indirect() {
  run(
      "def main [\n"
      // skip alloc id for 10:&:@:num
      "  11:num <- copy 20\n"
      // skip alloc id for payload
      "  21:num <- copy 3\n"  // array length
      "  22:num <- copy 94\n"
      "  23:num <- copy 95\n"
      "  24:num <- copy 96\n"
      "  30:@:num <- copy 10:&:@:num/lookup\n"
      "]\n"
  );
  CHECK_TRACE_CONTENTS(
      "mem: storing 3 in location 30\n"
      "mem: storing 94 in location 31\n"
      "mem: storing 95 in location 32\n"
      "mem: storing 96 in location 33\n"
  );
}

void test_create_array_indirect() {
  run(
      "def main [\n"
      // skip alloc id for 10:&:@:num:3
      "  11:num <- copy 3000\n"
      "  10:&:array:num:3/lookup <- create-array\n"
      "]\n"
  );
  CHECK_TRACE_CONTENTS(
      "mem: storing 3 in location 3001\n"
  );
}

:(after "Update CREATE_ARRAY product in Check")
if (!canonize_type(product)) break;
:(after "Update CREATE_ARRAY product in Run")
canonize(product);

:(code)
void test_index_indirect() {
  run(
      "def main [\n"
      // skip alloc id for 10:&:@:num
      "  11:num <- copy 20\n"
      // skip alloc id for payload
      "  21:num <- copy 3\n"  // array length
      "  22:num <- copy 94\n"
      "  23:num <- copy 95\n"
      "  24:num <- copy 96\n"
      "  30:num <- index 10:&:@:num/lookup, 1\n"
      "]\n"
  );
  CHECK_TRACE_CONTENTS(
      "mem: storing 95 in location 30\n"
  );
}

:(before "Update INDEX base in Check")
if (!canonize_type(base)) break;
:(before "Update INDEX index in Check")
if (!canonize_type(index)) break;
:(before "Update INDEX product in Check")
if (!canonize_type(product)) break;

:(before "Update INDEX base in Run")
canonize(base);
:(before "Update INDEX index in Run")
canonize(index);

:(code)
void test_put_index_indirect() {
  run(
      "def main [\n"
      // skip alloc id for 10:&:@:num
      "  11:num <- copy 20\n"
      // skip alloc id for payload
      "  21:num <- copy 3\n"  // array length
      "  22:num <- copy 94\n"
      "  23:num <- copy 95\n"
      "  24:num <- copy 96\n"
      "  10:&:@:num/lookup <- put-index 10:&:@:num/lookup, 1, 97\n"
      "]\n"
  );
  CHECK_TRACE_CONTENTS(
      "mem: storing 97 in location 23\n"
  );
}

void test_put_index_indirect_2() {
  run(
      "def main [\n"
      "  10:num <- copy 3\n"  // array length
      "  11:num <- copy 94\n"
      "  12:num <- copy 95\n"
      "  13:num <- copy 96\n"
      // skip alloc id for address
      "  21:num <- copy 30\n"
      // skip alloc id for payload
      "  31:num <- copy 1\n"  // index
      "  10:@:num <- put-index 10:@:num, 20:&:num/lookup, 97\n"
      "]\n"
  );
  CHECK_TRACE_CONTENTS(
      "mem: storing 97 in location 12\n"
  );
}

void test_put_index_product_error_with_lookup() {
  Hide_errors = true;
  run(
      "def main [\n"
      "  11:num <- copy 20\n"
      "  21:num <- copy 3\n"  // array length
      "  22:num <- copy 94\n"
      "  23:num <- copy 95\n"
      "  24:num <- copy 96\n"
      "  10:&:@:num <- put-index 10:&:@:num/lookup, 1, 34\n"
      "]\n"
  );
  CHECK_TRACE_CONTENTS(
      "error: main: product of 'put-index' must be first ingredient '10:&:@:num/lookup', but got '10:&:@:num'\n"
  );
}

:(before "End PUT_INDEX Product Checks")
reagent/*copy*/ p = inst.products.at(0);
if (!canonize_type(p)) break;  // error raised elsewhere
reagent/*copy*/ i = inst.ingredients.at(0);
if (!canonize_type(i)) break;  // error raised elsewhere
if (!types_strictly_match(p, i)) {
  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();
  break;
}

:(code)
void test_dilated_reagent_in_static_array() {
  run(
      "def main [\n"
      "  {1: (array (& num) 3)} <- create-array\n"
      "  10:&:num <- new num:type\n"
      "  {1: (array (& num) 3)} <- put-index {1: (array (& num) 3)}, 0, 10:&:num\n"
      "  *10:&:num <- copy 94\n"
      "  20:num <- copy *10:&:num\n"
      "]\n"
  );
  CHECK_TRACE_CONTENTS(
      "run: creating array from 7 locations\n"
      "mem: storing 94 in location 20\n"
  );
}

:(before "Update PUT_INDEX base in Check")
if (!canonize_type(base)) break;
:(before "Update PUT_INDEX index in Check")
if (!canonize_type(index)) break;
:(before "Update PUT_INDEX value in Check")
if (!canonize_type(value)) break;

:(before "Update PUT_INDEX base in Run")
canonize(base);
:(before "Update PUT_INDEX index in Run")
canonize(index);

:(code)
void test_length_indirect() {
  run(
      "def main [\n"
      // skip alloc id for 10:&:@:num
      "  11:num <- copy 20\n"
      // skip alloc id for payload
      "  21:num <- copy 3\n"  // array length
      "  22:num <- copy 94\n"
      "  23:num <- copy 95\n"
      "  24:num <- copy 96\n"
      "  30:num <- length 10:&:array:num/lookup\n"
      "]\n"
  );
  CHECK_TRACE_CONTENTS(
      "mem: storing 3 in location 30\n"
  );
}

:(before "Update LENGTH array in Check")
if (!canonize_type(array)) break;
:(before "Update LENGTH array in Run")
canonize(array);

:(code)
void test_maybe_convert_indirect() {
  run(
      "def main [\n"
      // skip alloc id for 10:&:number-or-point
      "  11:num <- copy 20\n"
      // skip alloc id for payload
      "  21:number-or-point <- merge 0/number, 94\n"
      "  30:num, 31:bool <- maybe-convert 10:&:number-or-point/lookup, i:variant\n"
      "]\n"
  );
  CHECK_TRACE_CONTENTS(
      "mem: storing 1 in location 31\n"
      "mem: storing 94 in location 30\n"
  );
}

void test_maybe_convert_indirect_2() {
  run(
      "def main [\n"
      // skip alloc id for 10:&:number-or-point
      "  11:num <- copy 20\n"
      // skip alloc id for payload
      "  21:number-or-point <- merge 0/number, 94\n"
      // skip alloc id for 30:&:num
      "  31:num <- copy 40\n"
      "  30:&:num/lookup, 50:bool <- maybe-convert 10:&:number-or-point/lookup, i:variant\n"
      "]\n"
  );
  CHECK_TRACE_CONTENTS(
      "mem: storing 1 in location 50\n"
      "mem: storing 94 in location 41\n"
  );
}

void test_maybe_convert_indirect_3() {
  run(
      "def main [\n"
      // skip alloc id for 10:&:number-or-point
      "  11:num <- copy 20\n"
      // skip alloc id for payload
      "  21:number-or-point <- merge 0/number, 94\n"
      // skip alloc id for 30:&:bool
      "  31:num <- copy 40\n"
      "  50:num, 30:&:bool/lookup <- maybe-convert 10:&:number-or-point/lookup, i:variant\n"
      "]\n"
  );
  CHECK_TRACE_CONTENTS(
      "mem: storing 1 in location 41\n"
      "mem: storing 94 in location 50\n"
  );
}

:(before "Update MAYBE_CONVERT base in Check")
if (!canonize_type(base)) break;
:(before "Update MAYBE_CONVERT product in Check")
if (!canonize_type(product)) break;
:(before "Update MAYBE_CONVERT status in Check")
if (!canonize_type(status)) break;

:(before "Update MAYBE_CONVERT base in Run")
canonize(base);
:(before "Update MAYBE_CONVERT product in Run")
canonize(product);
:(before "Update MAYBE_CONVERT status in Run")
canonize(status);

:(code)
void test_merge_exclusive_container_indirect() {
  run(
      "def main [\n"
      // skip alloc id for 10:&:number-or-point
      "  11:num <- copy 20\n"
      "  10:&:number-or-point/lookup <- merge 0/number, 34\n"
      "]\n"
  );
  CHECK_TRACE_CONTENTS(
      // skip alloc id
      "mem: storing 0 in location 21\n"
      "mem: storing 34 in location 22\n"
  );
}

:(before "Update size_mismatch Check for MERGE(x)
canonize(x);

//: abbreviation for '/lookup': a prefix '*'

:(code)
void test_lookup_abbreviation() {
  run(
      "def main [\n"
      // skip alloc id for 10:&:num
      "  11:num <- copy 20\n"
      // skip alloc id for payload
      "  21:num <- copy 94\n"
      "  30:num <- copy *10:&:num\n"
      "]\n"
  );
  CHECK_TRACE_CONTENTS(
      "parse: ingredient: {10: (\"&\" \"num\"), \"lookup\": ()}\n"
      "mem: storing 94 in location 30\n"
  );
}

:(before "End Parsing reagent")
{
  while (starts_with(name, "*")) {
    name.erase(0, 1);
    properties.push_back(pair<string, string_tree*>("lookup", NULL));
  }
  if (name.empty())
    raise << "illegal name '" << original_string << "'\n" << end();
}

//:: helpers for debugging

:(before "End Primitive Recipe Declarations")
_DUMP,
:(before "End Primitive Recipe Numbers")
put(Recipe_ordinal, "$dump", _DUMP);
:(before "End Primitive Recipe Implementations")
case _DUMP: {
  reagent/*copy*/ after_canonize = current_instruction().ingredients.at(0);
  canonize(after_canonize);
  cerr << maybe(current_recipe_name()) << current_instruction().ingredients.at(0).name << ' ' << no_scientific(current_instruction().ingredients.at(0).value) << " => " << no_scientific(after_canonize.value) << " => " << no_scientific(get_or_insert(Memory, after_canonize.value)) << '\n';
  break;
}

//: grab an address, and then dump its value at intervals
//: useful for tracking down memory corruption (writing to an out-of-bounds address)
:(before "End Globals")
int Bar = -1;
:(before "End Primitive Recipe Declarations")
_BAR,
:(before "End Primitive Recipe Numbers")
put(Recipe_ordinal, "$bar", _BAR);
:(before "End Primitive Recipe Implementations")
case _BAR: {
  if (current_instruction().ingredients.empty()) {
    if (Bar != -1) cerr << Bar << ": " << no_scientific(get_or_insert(Memory, Bar)) << '\n';
    else cerr << '\n';
  }
  else {
    reagent/*copy*/ tmp = current_instruction().ingredients.at(0);
    canonize(tmp);
    Bar = tmp.value;
  }
  break;
}