about summary refs log blame commit diff stats
path: root/033exclusive_container.cc
blob: 86dc1c8af9ce59132c4c00d35a584bd4cf036999 (plain) (tree)
pre { line-height: 125%; } td.linenos .normal { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; } span.linenos { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; } td.linenos .special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; } span.linenos.special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; } .highlight .hll { background-color: #ffffcc } .highlight .c { color: #888888 } /* Comment */ .highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */ .highlight .k { color: #008800; font-weight: bold } /* Keyword */ .highlight .ch { color: #888888 } /* Comment.Hashbang */ .highlight .cm { color: #888888 } /* Comment.Multiline */ .highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */ .highlight .cpf { color: #888888 } /* Comment.PreprocFile */ .highlight .c1 { color: #888888 } /* Comment.Single */ .highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */ .highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */ .highlight .ge { font-style: italic } /* Generic.Emph */ .highlight .ges { font-weight: bold; font-style: italic } /* Generic.EmphStrong */ .highlight .gr { color: #aa0000 } /* Generic.Error */ .highlight .gh { color: #333333 } /* Generic.Heading */ .highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */ .highlight .go { color: #888888 } /* Generic.Output */ .highlight .gp { color: #555555 } /* Generic.Prompt */ .highlight .gs { font-weight: bold } /* Generic.Strong */ .highlight .gu { color: #666666 } /* Generic.Subheading */ .highlight .gt { color: #aa0000 } /* Generic.Traceback */ .highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */ .highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */ .highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */ .highlight .kp { color: #008800 } /* Keyword.Pseudo */ .highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */ .highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */ .highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */ .highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */ .highlight .na { color: #336699 } /* Name.Attribute */ .highlight .nb { color: #003388 } /* Name.Builtin */ .highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */ .highlight .no { color: #003366; font-weight: bold } /* Name.Constant */ .highlight .nd { color: #555555 } /* Name.Decorator */ .highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */ .highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */ .highlight .nl { color: #336699; font-style: italic } /* Name.Label */ .highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */ .highlight .py { color: #336699; font-weight: bold } /* Name.Property */ .highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */ .highlight .nv { color: #336699 } /* Name.Variable */ .highlight .ow { color: #008800 } /* Operator.Word */ .highlight .w { color: #bbbbbb } /* Text.Whitespace */ .highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */ .highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */ .highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */ .highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */ .highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */ .highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */ .highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */ .highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */ .highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */ .highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */ .highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */ .highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */ .highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */ .highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */ .highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */ .highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */ .highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */ .highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */ .highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */ .highlight .vc { color: #336699 } /* Name.Variable.Class */ .highlight .vg { color: #dd7700 } /* Name.Variable.Global */ .highlight .vi { color: #3333bb } /* Name.Variable.Instance */ .highlight .vm { color: #336699 } /* Name.Variable.Magic */ .highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
Run all these from the top-level `mu/` directory.

### Some tools for Mu's build process

These are built automatically.

* `enumerate`: list numeric files in current directory, optionally `--until`
  some prefix.


### Miscellaneous odds and ends

These are built lazily.

* `browse_trace`: debugging tool. See `browse_trace.readme.md` for details.

* `linkify`: inserts hyperlinks from variables to definitions in Mu's html
  sources. Hacky; just see the number of tests. Invoked by `update_html`.

* `treeshake_all`: rebuild SubX binaries without tests and unused functions.
  Hacky; just helps estimate the code needed to perform various tasks.
  ```
  tools/treeshake_all
  ```

### Notes to self: constraints on the tools/ directory
* Don't overwhelm the initial view of the project with lots of crap in the
  root directory.
* Directories go up top in the github view, so too many sub-directories are
  also overwhelming.
* Don't increase increase build time too much; everything in `tools/` shouldn't
  be automatically built.
  * stuff needed all the time is built from root directory.
* `tools/` contains many independent things; don't make it hard to see
  boundaries. Ideally just one source file per tool. If not, give related
  files similar name prefixes.
1211a3ab ^
f890f147 ^



1211a3ab ^
3377364a ^
571791ba ^
af023b32 ^


571791ba ^
69f3fb0a ^
97a41843 ^


6c96a437 ^
af023b32 ^

571791ba ^
3377364a ^
97a41843 ^
69f3fb0a ^

97a41843 ^
af023b32 ^
69f3fb0a ^
6ea73c22 ^
7284d503 ^
dd994cda ^






795f5244 ^
dd994cda ^
88be3dbc ^
1ead3562 ^
192d59d3 ^


d52406cc ^
dd994cda ^
6e793202 ^

7bf9212f ^


dd994cda ^
88be3dbc ^
1ead3562 ^
192d59d3 ^


d52406cc ^
dd994cda ^
7bf9212f ^

6e793202 ^
dd994cda ^
6ea73c22 ^


795f5244 ^
96bb317f ^
dd994cda ^
bd613461 ^
96bb317f ^
2b250717 ^
e4630643 ^

3473c63a ^
15936c91 ^
af023b32 ^



271f101f ^


9dcbec39 ^
18e626df ^

96bb317f ^
9dcbec39 ^
bd613461 ^


7bf9212f ^
2b250717 ^
7bf9212f ^

3473c63a ^
15936c91 ^
fe2725d9 ^

271f101f ^
2b250717 ^
fe2725d9 ^
bd613461 ^
3473c63a ^
bd613461 ^
9dcbec39 ^
eefde1b9 ^

3473c63a ^
15936c91 ^

9dcbec39 ^
7bf9212f ^

96bb317f ^



3473c63a ^
15936c91 ^
b24eb476 ^
96bb317f ^
2b250717 ^
eefde1b9 ^

b24eb476 ^
3473c63a ^
15936c91 ^
3473c63a ^
15936c91 ^
7bf9212f ^
59874868 ^
b24eb476 ^
3473c63a ^
6e793202 ^

0c1ae52f ^

6c96a437 ^
0c1ae52f ^



7bf9212f ^
dd994cda ^

15936c91 ^

dd994cda ^
59874868 ^
dd994cda ^
b98d3876 ^
bd613461 ^
15936c91 ^
8e1c4783 ^



bd613461 ^
af023b32 ^



bd613461 ^
3473c63a ^
bd613461 ^





1ead3562 ^
192d59d3 ^


d52406cc ^
bd613461 ^
9dcbec39 ^
bd613461 ^
0c1ae52f ^

192d59d3 ^


d52406cc ^
0c1ae52f ^


9a81d746 ^
b98d3876 ^


192d59d3 ^

b98d3876 ^
dd2e01e4 ^
acc4792d ^

b98d3876 ^


72d5d018 ^
b98d3876 ^
02a55f71 ^
f592d862 ^



f592d862 ^
192d59d3 ^
f592d862 ^


a0b9fa55 ^
f592d862 ^

192d59d3 ^
f592d862 ^
9dcbec39 ^
f592d862 ^
02a55f71 ^


192d59d3 ^

02a55f71 ^
1ead3562 ^
192d59d3 ^


02a55f71 ^




2eab85b3 ^
5ba1c3b9 ^


5ba1c3b9 ^
192d59d3 ^
5ba1c3b9 ^


192d59d3 ^
5ba1c3b9 ^
1ead3562 ^
5ba1c3b9 ^








192d59d3 ^
5ba1c3b9 ^


192d59d3 ^
5ba1c3b9 ^
1ead3562 ^
192d59d3 ^

5ba1c3b9 ^
192d59d3 ^
5ba1c3b9 ^
835d98a1 ^

192d59d3 ^
835d98a1 ^


192d59d3 ^

835d98a1 ^

192d59d3 ^
835d98a1 ^






1211a3ab ^
5ba1c3b9 ^

15c44292 ^
835d98a1 ^


5ba1c3b9 ^
2b250717 ^
5ba1c3b9 ^

3473c63a ^
5ba1c3b9 ^

2b250717 ^
5ba1c3b9 ^

3473c63a ^
5ba1c3b9 ^








5ba1c3b9 ^
192d59d3 ^
5ba1c3b9 ^


192d59d3 ^

5ba1c3b9 ^
1ead3562 ^
5ba1c3b9 ^









192d59d3 ^
5ba1c3b9 ^


192d59d3 ^

5ba1c3b9 ^
1ead3562 ^
5ba1c3b9 ^




5ba1c3b9 ^
192d59d3 ^
5ba1c3b9 ^


192d59d3 ^

5ba1c3b9 ^
1ead3562 ^
5ba1c3b9 ^







5ba1c3b9 ^
192d59d3 ^
5ba1c3b9 ^


192d59d3 ^

5ba1c3b9 ^
1ead3562 ^
5ba1c3b9 ^






192d59d3 ^
5ba1c3b9 ^


192d59d3 ^

5ba1c3b9 ^
1ead3562 ^
5ba1c3b9 ^



7bf9212f ^

192d59d3 ^
7bf9212f ^


192d59d3 ^

7bf9212f ^






2eab85b3 ^

1211a3ab ^
c432e73a ^

2eab85b3 ^
c6034af3 ^
3473c63a ^
15936c91 ^
af023b32 ^


2eab85b3 ^
2eab85b3 ^



192d59d3 ^

2eab85b3 ^
2eab85b3 ^
192d59d3 ^
2eab85b3 ^

1ead3562 ^
192d59d3 ^



2eab85b3 ^






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






                                                                            
                                                                            
 
                                                                             
                                        

                                          
                                                       
                                                      

 
                                                                              

                                                                             
                                    
                                                                                           
          


                                    
                                                    




                              
                                           



                                            
                                                          
                                       
                                                                                               


       
                                                                                                                                                                                            
                                                                      


                                                                           
                                                                       

                                                                      
                                                                                         
                                        
                                                                   

                       
                  
                                                                                                          
 
 
                                                                       






                                                                           
                                
 
                         
          


                   
                                                                         
 

                              


                               
 
                              
          


                   
                                                                       
 

                              
                  
 


                                             
                                                    
                                       
                     
                                        
                                    
                                                                                                                                      

          
                                                
                                       



                                                                                                                                                               


                                                                                                                                                      
                                                                                                                                                               

          
                                            
                                                                                                                                                                            


                                   
                                 
                                                                                                                                   

          
                                                
                                          

                                           
                                                                   
                                                                                                                          
          
   
                                                            
                                           
                                                                                                                                                                                                                                                                           

          
                                               

                                         
                                                                                                                                                                                             

          



                                                
                                                                 
                                     
                                
                          
                                                                                                                                              

          
                                                          
                                                                 
                                        
                                                                
                                       
                                                                                     
                         
                                                                     
                                                     

                                                                            

                                                        
                                                    



                                                                                                              
     

        

                                                                            
   
        
 
 
       
                                                          



                                                            
                   



                                                              
                                           
                                                  





                                               
          


                   
                                                                       
 
                                                                                                                 
 

                                       


                   
                                                                  


         
                                                         


                               

       
 
                                            

                              


                                            
                                                     
 
 



                                                                            
                         
               


         
                                                               

                         
             
 
                                                            
 


                                                                         

       
 
          


                                                                                      




                              
 


                                                     
                         
       


               
       
 
          








                                                              
       


               
       
 
          

                          
 
                                                                                                                                 
 

                                                                        
       


                         

       

          
                 






                              
                                              

                                                        
                                                                                                                                        


                                                                             
                                                      
                                                                                                                                                                                                                                 

           
                                                                                                                                      

                                                          
                                                                                                                                                                      

           
                                                                     








                                                                   
               
       


                         

       
 
          









                                                                  
       


                         

       
 
          




                                                                      
                         
       


               

       
 
          







                                                                  
                         
       


               

       
 
          






                                                                  
       


               

       
 
          



                                                             

                                                                  
       


               

       






                           

                                                                            
                                              

                                                         
                                              
                                                   
                                                           
                                            


                                                                    
                                   



                                                           

       
 
                         
       

       
          



                                    






                                                
//: Exclusive containers contain exactly one of a fixed number of 'variants'
//: of different types.
//:
//: They also implicitly contain a tag describing precisely which variant is
//: currently stored in them.

:(before "End Mu Types Initialization")
//: We'll use this container as a running example, with two number elements.
{
type_ordinal tmp = put(Type_ordinal, "number-or-point", Next_type_ordinal++);
get_or_insert(Type, tmp);  // initialize
get(Type, tmp).kind = EXCLUSIVE_CONTAINER;
get(Type, tmp).name = "number-or-point";
get(Type, tmp).elements.push_back(reagent("i:number"));
get(Type, tmp).elements.push_back(reagent("p:point"));
}

//: Tests in this layer often explicitly set up memory before reading it as an
//: array. Don't do this in general. I'm tagging exceptions with /raw to keep
//: checks in future layers from flagging them.
:(scenario copy_exclusive_container)
# Copying exclusive containers copies all their contents and an extra location for the tag.
def main [
  1:num <- copy 1  # 'point' variant
  2:num <- copy 34
  3:num <- copy 35
  4:number-or-point <- copy 1:number-or-point/unsafe
]
+mem: storing 1 in location 4
+mem: storing 34 in location 5
+mem: storing 35 in location 6

:(before "End size_of(type) Special-cases")
if (t.kind == EXCLUSIVE_CONTAINER) {
  // Compute size_of Exclusive Container
  return get(Container_metadata, type).size;
}
:(before "End compute_container_sizes Atom Special-cases")
if (info.kind == EXCLUSIVE_CONTAINER) {
  compute_exclusive_container_sizes(info, type, pending_metadata, location_for_error_messages);
}

:(code)
void compute_exclusive_container_sizes(const type_info& exclusive_container_info, const type_tree* full_type, set<type_tree>& pending_metadata, const string& location_for_error_messages) {
  // size of an exclusive container is the size of its largest variant
  // (So, like containers, it can only contain arrays if they're static and
  // include their length in the type.)
  container_metadata metadata;
  for (int i = 0;  i < SIZE(exclusive_container_info.elements);  ++i) {
    reagent/*copy*/ element = exclusive_container_info.elements.at(i);
    // Compute Exclusive Container Size(element, full_type)
    compute_container_sizes(element.type, pending_metadata, location_for_error_messages);
    int variant_size = size_of(element);
    if (variant_size > metadata.size) metadata.size = variant_size;
  }
  // ...+1 for its tag.
  ++metadata.size;
  Container_metadata.push_back(pair<type_tree*, container_metadata>(new type_tree(*full_type), metadata));
}

//:: To access variants of an exclusive container, use 'maybe-convert'.
//: It always returns an address (so that you can modify it) or null (to
//: signal that the conversion failed (because the container contains a
//: different variant).

//: 'maybe-convert' requires a literal in ingredient 1. We'll use a synonym
//: called 'variant'.
:(before "End Mu Types Initialization")
put(Type_ordinal, "variant", 0);

:(scenario maybe_convert)
def main [
  12:num <- copy 1
  13:num <- copy 35
  14:num <- copy 36
  20:point, 22:bool <- maybe-convert 12:number-or-point/unsafe, 1:variant
]
# boolean
+mem: storing 1 in location 22
# point
+mem: storing 35 in location 20
+mem: storing 36 in location 21

:(scenario maybe_convert_fail)
def main [
  12:num <- copy 1
  13:num <- copy 35
  14:num <- copy 36
  20:num, 21:bool <- maybe-convert 12:number-or-point/unsafe, 0:variant
]
# boolean
+mem: storing 0 in location 21
# number: no write

:(before "End Primitive Recipe Declarations")
MAYBE_CONVERT,
:(before "End Primitive Recipe Numbers")
put(Recipe_ordinal, "maybe-convert", MAYBE_CONVERT);
:(before "End Primitive Recipe Checks")
case MAYBE_CONVERT: {
  const recipe& caller = get(Recipe, r);
  if (SIZE(inst.ingredients) != 2) {
    raise << maybe(caller.name) << "'maybe-convert' expects exactly 2 ingredients in '" << to_original_string(inst) << "'\n" << end();
    break;
  }
  reagent/*copy*/ base = inst.ingredients.at(0);
  // Update MAYBE_CONVERT base in Check
  if (!base.type) {
    raise << maybe(caller.name) << "first ingredient of 'maybe-convert' should be an exclusive-container, but got '" << base.original_string << "'\n" << end();
    break;
  }
  const type_tree* base_type = base.type;
  // Update MAYBE_CONVERT base_type in Check
  if (!base_type->atom || base_type->value == 0 || !contains_key(Type, base_type->value) || get(Type, base_type->value).kind != EXCLUSIVE_CONTAINER) {
    raise << maybe(caller.name) << "first ingredient of 'maybe-convert' should be an exclusive-container, but got '" << base.original_string << "'\n" << end();
    break;
  }
  if (!is_literal(inst.ingredients.at(1))) {
    raise << maybe(caller.name) << "second ingredient of 'maybe-convert' should have type 'variant', but got '" << inst.ingredients.at(1).original_string << "'\n" << end();
    break;
  }
  if (inst.products.empty()) break;
  if (SIZE(inst.products) != 2) {
    raise << maybe(caller.name) << "'maybe-convert' expects exactly 2 products in '" << to_original_string(inst) << "'\n" << end();
    break;
  }
  reagent/*copy*/ product = inst.products.at(0);
  // Update MAYBE_CONVERT product in Check
  reagent& offset = inst.ingredients.at(1);
  populate_value(offset);
  if (offset.value >= SIZE(get(Type, base_type->value).elements)) {
    raise << maybe(caller.name) << "invalid tag " << offset.value << " in '" << to_original_string(inst) << '\n' << end();
    break;
  }
  const reagent& variant = variant_type(base, offset.value);
  if (!types_coercible(product, variant)) {
    raise << maybe(caller.name) << "'maybe-convert " << base.original_string << ", " << inst.ingredients.at(1).original_string << "' should write to " << to_string(variant.type) << " but '" << product.name << "' has type " << to_string(product.type) << '\n' << end();
    break;
  }
  reagent/*copy*/ status = inst.products.at(1);
  // Update MAYBE_CONVERT status in Check
  if (!is_mu_boolean(status)) {
    raise << maybe(get(Recipe, r).name) << "second product yielded by 'maybe-convert' should be a boolean, but tried to write to '" << inst.products.at(1).original_string << "'\n" << end();
    break;
  }
  break;
}
:(before "End Primitive Recipe Implementations")
case MAYBE_CONVERT: {
  reagent/*copy*/ base = current_instruction().ingredients.at(0);
  // Update MAYBE_CONVERT base in Run
  int base_address = base.value;
  if (base_address == 0) {
    raise << maybe(current_recipe_name()) << "tried to access location 0 in '" << to_original_string(current_instruction()) << "'\n" << end();
    break;
  }
  int tag = current_instruction().ingredients.at(1).value;
  reagent/*copy*/ product = current_instruction().products.at(0);
  // Update MAYBE_CONVERT product in Run
  reagent/*copy*/ status = current_instruction().products.at(1);
  // Update MAYBE_CONVERT status in Run
  // optimization: directly write results to only update first product when necessary
  write_products = false;
  if (tag == static_cast<int>(get_or_insert(Memory, base_address))) {
    const reagent& variant = variant_type(base, tag);
    trace(9999, "mem") << "storing 1 in location " << status.value << end();
    put(Memory, status.value, 1);
    if (!is_dummy(product)) {
      // Write Memory in Successful MAYBE_CONVERT in Run
      for (int i = 0;  i < size_of(variant);  ++i) {
        double val = get_or_insert(Memory, base_address+/*skip tag*/1+i);
        trace(9999, "mem") << "storing " << no_scientific(val) << " in location " << product.value+i << end();
        put(Memory, product.value+i, val);
      }
    }
  }
  else {
    trace(9999, "mem") << "storing 0 in location " << status.value << end();
    put(Memory, status.value, 0);
  }
  break;
}

:(code)
const reagent variant_type(const reagent& base, int tag) {
  return variant_type(base.type, tag);
}

const reagent variant_type(const type_tree* type, int tag) {
  assert(tag >= 0);
  const type_tree* root_type = type->atom ? type : type->left;
  assert(contains_key(Type, root_type->value));
  assert(!get(Type, root_type->value).name.empty());
  const type_info& info = get(Type, root_type->value);
  assert(info.kind == EXCLUSIVE_CONTAINER);
  reagent/*copy*/ element = info.elements.at(tag);
  // End variant_type Special-cases
  return element;
}

:(scenario maybe_convert_product_type_mismatch)
% Hide_errors = true;
def main [
  12:num <- copy 1
  13:num <- copy 35
  14:num <- copy 36
  20:num, 21:bool <- maybe-convert 12:number-or-point/unsafe, 1:variant
]
+error: main: 'maybe-convert 12:number-or-point/unsafe, 1:variant' should write to point but '20' has type number

:(scenario maybe_convert_dummy_product)
def main [
  12:num <- copy 1
  13:num <- copy 35
  14:num <- copy 36
  _, 21:bool <- maybe-convert 12:number-or-point/unsafe, 1:variant
]
$error: 0

//:: Allow exclusive containers to be defined in Mu code.

:(scenario exclusive_container)
exclusive-container foo [
  x:num
  y:num
]
+parse: --- defining exclusive-container foo
+parse: element: {x: "number"}
+parse: element: {y: "number"}

:(before "End Command Handlers")
else if (command == "exclusive-container") {
  insert_container(command, EXCLUSIVE_CONTAINER, in);
}

//: arrays are disallowed inside exclusive containers unless their length is
//: fixed in advance

:(scenario exclusive_container_contains_array)
exclusive-container foo [
  x:array:num:3
]
$error: 0

:(scenario exclusive_container_disallows_dynamic_array_element)
% Hide_errors = true;
exclusive-container foo [
  x:array:num
]
+error: container 'foo' cannot determine size of element 'x'

//:: To construct exclusive containers out of variant types, use 'merge'.
:(scenario lift_to_exclusive_container)
exclusive-container foo [
  x:num
  y:num
]
def main [
  1:num <- copy 34
  2:foo <- merge 0/x, 1:num  # tag must be a literal when merging exclusive containers
  4:foo <- merge 1/y, 1:num
]
+mem: storing 0 in location 2
+mem: storing 34 in location 3
+mem: storing 1 in location 4
+mem: storing 34 in location 5

//: type-checking for 'merge' on exclusive containers

:(scenario merge_handles_exclusive_container)
exclusive-container foo [
  x:num
  y:bar
]
container bar [
  z:num
]
def main [
  1:foo <- merge 0/x, 34
]
+mem: storing 0 in location 1
+mem: storing 34 in location 2
$error: 0

:(scenario merge_requires_literal_tag_for_exclusive_container)
% Hide_errors = true;
exclusive-container foo [
  x:num
  y:bar
]
container bar [
  z:num
]
def main [
  1:num <- copy 0
  2:foo <- merge 1:num, 34
]
+error: main: ingredient 0 of 'merge' should be a literal, for the tag of exclusive-container 'foo' in '2:foo <- merge 1:num, 34'

:(scenario merge_handles_exclusive_container_inside_exclusive_container)
exclusive-container foo [
  x:num
  y:bar
]
exclusive-container bar [
  a:num
  b:num
]
def main [
  1:num <- copy 0
  2:bar <- merge 0/a, 34
  4:foo <- merge 1/y, 2:bar
]
+mem: storing 0 in location 5
+mem: storing 34 in location 6
$error: 0

:(before "End check_merge_call Special-cases")
case EXCLUSIVE_CONTAINER: {
  assert(state.data.top().container_element_index == 0);
  trace(9999, "transform") << "checking exclusive container " << to_string(container) << " vs ingredient " << ingredient_index << end();
  // easy case: exact match
  if (types_strictly_match(container, inst.ingredients.at(ingredient_index)))
    return;
  if (!is_literal(ingredients.at(ingredient_index))) {
    raise << maybe(caller.name) << "ingredient " << ingredient_index << " of 'merge' should be a literal, for the tag of exclusive-container '" << container_info.name << "' in '" << to_original_string(inst) << "'\n" << end();
    return;
  }
  reagent/*copy*/ ingredient = ingredients.at(ingredient_index);  // unnecessary copy just to keep this function from modifying caller
  populate_value(ingredient);
  if (ingredient.value >= SIZE(container_info.elements)) {
    raise << maybe(caller.name) << "invalid tag at " << ingredient_index << " for '" << container_info.name << "' in '" << to_original_string(inst) << "'\n" << end();
    return;
  }
  const reagent& variant = variant_type(container, ingredient.value);
  trace(9999, "transform") << "tag: " << ingredient.value << end();
  // replace union with its variant
  state.data.pop();
  state.data.push(merge_check_point(variant, 0));
  ++ingredient_index;
  break;
}

:(scenario merge_check_container_containing_exclusive_container)
container foo [
  x:num
  y:bar
]
exclusive-container bar [
  x:num
  y:num
]
def main [
  1:foo <- merge 23, 1/y, 34
]
+mem: storing 23 in location 1
+mem: storing 1 in location 2
+mem: storing 34 in location 3
$error: 0

:(scenario merge_check_container_containing_exclusive_container_2)
% Hide_errors = true;
container foo [
  x:num
  y:bar
]
exclusive-container bar [
  x:num
  y:num
]
def main [
  1:foo <- merge 23, 1/y, 34, 35
]
+error: main: too many ingredients in '1:foo <- merge 23, 1/y, 34, 35'

:(scenario merge_check_exclusive_container_containing_container)
exclusive-container foo [
  x:num
  y:bar
]
container bar [
  x:num
  y:num
]
def main [
  1:foo <- merge 1/y, 23, 34
]
+mem: storing 1 in location 1
+mem: storing 23 in location 2
+mem: storing 34 in location 3
$error: 0

:(scenario merge_check_exclusive_container_containing_container_2)
exclusive-container foo [
  x:num
  y:bar
]
container bar [
  x:num
  y:num
]
def main [
  1:foo <- merge 0/x, 23
]
$error: 0

:(scenario merge_check_exclusive_container_containing_container_3)
% Hide_errors = true;
exclusive-container foo [
  x:num
  y:bar
]
container bar [
  x:num
  y:num
]
def main [
  1:foo <- merge 1/y, 23
]
+error: main: too few ingredients in '1:foo <- merge 1/y, 23'

:(scenario merge_check_exclusive_container_containing_container_4)
exclusive-container foo [
  x:num
  y:bar
]
container bar [
  a:num
  b:num
]
def main [
  1:bar <- merge 23, 24
  3:foo <- merge 1/y, 1:bar
]
$error: 0

//: Since the different variants of an exclusive-container might have
//: different sizes, relax the size mismatch check for 'merge' instructions.
:(before "End size_mismatch(x) Special-cases")
if (current_step_index() < SIZE(Current_routine->steps())
    && current_instruction().operation == MERGE
    && !current_instruction().products.empty()
    && current_instruction().products.at(0).type) {
  reagent/*copy*/ x = current_instruction().products.at(0);
  // Update size_mismatch Check for MERGE(x)
  const type_tree* root_type = x.type->atom ? x.type : x.type->left;
  assert(root_type->atom);
  if (get(Type, root_type->value).kind == EXCLUSIVE_CONTAINER)
    return size_of(x) < SIZE(data);
}

:(scenario merge_exclusive_container_with_mismatched_sizes)
container foo [
  x:num
  y:num
]
exclusive-container bar [
  x:num
  y:foo
]
def main [
  1:num <- copy 34
  2:num <- copy 35
  3:bar <- merge 0/x, 1:num
  6:bar <- merge 1/foo, 1:num, 2:num
]
+mem: storing 0 in location 3
+mem: storing 34 in location 4
# bar is always 3 large so location 5 is skipped
+mem: storing 1 in location 6
+mem: storing 34 in location 7
+mem: storing 35 in location 8