1 //: Exclusive containers contain exactly one of a fixed number of 'variants'
  2 //: of different types.
  3 //:
  4 //: They also implicitly contain a tag describing precisely which variant is
  5 //: currently stored in them.
  6 
  7 :(before "End Mu Types Initialization")
  8 //: We'll use this container as a running example, with two number elements.
  9 {
 10 type_ordinal tmp = put(Type_ordinal, "number-or-point", Next_type_ordinal++);
 11 get_or_insert(Type, tmp);  // initialize
 12 get(Type, tmp).kind = EXCLUSIVE_CONTAINER;
 13 get(Type, tmp).name = "number-or-point";
 14 get(Type, tmp).elements.push_back(reagent("i:number"));
 15 get(Type, tmp).elements.push_back(reagent("p:point"));
 16 }
 17 
 18 //: Tests in this layer often explicitly set up memory before reading it as an
 19 //: array. Don't do this in general. I'm tagging exceptions with /raw to keep
 20 //: checks in future layers from flagging them.
 21 :(scenario copy_exclusive_container)
 22 # Copying exclusive containers copies all their contents and an extra location for the tag.
 23 def main [
 24   1:num <- copy 1  # 'point' variant
 25   2:num <- copy 34
 26   3:num <- copy 35
 27   4:number-or-point <- copy 1:number-or-point/unsafe
 28 ]
 29 +mem: storing 1 in location 4
 30 +mem: storing 34 in location 5
 31 +mem: storing 35 in location 6
 32 
 33 :(before "End size_of(type) Special-cases")
 34 if (t.kind == EXCLUSIVE_CONTAINER) {
 35   // Compute size_of Exclusive Container
 36   return get(Container_metadata, type).size;
 37 }
 38 :(before "End compute_container_sizes Atom Special-cases")
 39 if (info.kind == EXCLUSIVE_CONTAINER) {
 40   compute_exclusive_container_sizes(info, type, pending_metadata, location_for_error_messages);
 41 }
 42 
 43 :(code)
 44 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) {
 45   // size of an exclusive container is the size of its largest variant
 46   // (So, like containers, it can only contain arrays if they're static and
 47   // include their length in the type.)
 48   container_metadata metadata;
 49   for (int i = 0;  i < SIZE(exclusive_container_info.elements);  ++i) {
 50   ¦ reagent/*copy*/ element = exclusive_container_info.elements.at(i);
 51   ¦ // Compute Exclusive Container Size(element, full_type)
 52   ¦ compute_container_sizes(element.type, pending_metadata, location_for_error_messages);
 53   ¦ int variant_size = size_of(element);
 54   ¦ if (variant_size > metadata.size) metadata.size = variant_size;
 55   }
 56   // ...+1 for its tag.
 57   ++metadata.size;
 58   Container_metadata.push_back(pair<type_tree*, container_metadata>(new type_tree(*full_type), metadata));
 59 }
 60 
 61 //:: To access variants of an exclusive container, use 'maybe-convert'.
 62 //: It always returns an address (so that you can modify it) or null (to
 63 //: signal that the conversion failed (because the container contains a
 64 //: different variant).
 65 
 66 //: 'maybe-convert' requires a literal in ingredient 1. We'll use a synonym
 67 //: called 'variant'.
 68 :(before "End Mu Types Initialization")
 69 put(Type_ordinal, "variant", 0);
 70 
 71 :(scenario maybe_convert)
 72 def main [
 73   12:num <- copy 1
 74   13:num <- copy 35
 75   14:num <- copy 36
 76   20:point, 22:bool <- maybe-convert 12:number-or-point/unsafe, 1:variant
 77 ]
 78 # boolean
 79 +mem: storing 1 in location 22
 80 # point
 81 +mem: storing 35 in location 20
 82 +mem: storing 36 in location 21
 83 
 84 :(scenario maybe_convert_fail)
 85 def main [
 86   12:num <- copy 1
 87   13:num <- copy 35
 88   14:num <- copy 36
 89   20:num, 21:bool <- maybe-convert 12:number-or-point/unsafe, 0:variant
 90 ]
 91 # boolean
 92 +mem: storing 0 in location 21
 93 # number: no write
 94 
 95 :(before "End Primitive Recipe Declarations")
 96 MAYBE_CONVERT,
 97 :(before "End Primitive Recipe Numbers")
 98 put(Recipe_ordinal, "maybe-convert", MAYBE_CONVERT);
 99 :(before "End Primitive Recipe Checks")
100 case MAYBE_CONVERT: {
101   const recipe& caller = get(Recipe, r);
102   if (SIZE(inst.ingredients) != 2) {
103   ¦ raise << maybe(caller.name) << "'maybe-convert' expects exactly 2 ingredients in '" << inst.original_string << "'\n" << end();
104   ¦ break;
105   }
106   reagent/*copy*/ base = inst.ingredients.at(0);
107   // Update MAYBE_CONVERT base in Check
108   if (!base.type) {
109   ¦ raise << maybe(caller.name) << "first ingredient of 'maybe-convert' should be an exclusive-container, but got '" << base.original_string << "'\n" << end();
110   ¦ break;
111   }
112   const type_tree* base_type = base.type;
113   // Update MAYBE_CONVERT base_type in Check
114   if (!base_type->atom || base_type->value == 0 || !contains_key(Type, base_type->value) || get(Type, base_type->value).kind != EXCLUSIVE_CONTAINER) {
115   ¦ raise << maybe(caller.name) << "first ingredient of 'maybe-convert' should be an exclusive-container, but got '" << base.original_string << "'\n" << end();
116   ¦ break;
117   }
118   if (!is_literal(inst.ingredients.at(1))) {
119   ¦ raise << maybe(caller.name) << "second ingredient of 'maybe-convert' should have type 'variant', but got '" << inst.ingredients.at(1).original_string << "'\n" << end();
120   ¦ break;
121   }
122   if (inst.products.empty()) break;
123   if (SIZE(inst.products) != 2) {
124   ¦ raise << maybe(caller.name) << "'maybe-convert' expects exactly 2 products in '" << inst.original_string << "'\n" << end();
125   ¦ break;
126   }
127   reagent/*copy*/ product = inst.products.at(0);
128   // Update MAYBE_CONVERT product in Check
129   reagent& offset = inst.ingredients.at(1);
130   populate_value(offset);
131   if (offset.value >= SIZE(get(Type, base_type->value).elements)) {
132   ¦ raise << maybe(caller.name) << "invalid tag " << offset.value << " in '" << inst.original_string << '\n' << end();
133   ¦ break;
134   }
135   const reagent& variant = variant_type(base, offset.value);
136   if (!types_coercible(product, variant)) {
137   ¦ 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();
138   ¦ break;
139   }
140   reagent/*copy*/ status = inst.products.at(1);
141   // Update MAYBE_CONVERT status in Check
142   if (!is_mu_boolean(status)) {
143   ¦ 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();
144   ¦ break;
145   }
146   break;
147 }
148 :(before "End Primitive Recipe Implementations")
149 case MAYBE_CONVERT: {
150   reagent/*copy*/ base = current_instruction().ingredients.at(0);
151   // Update MAYBE_CONVERT base in Run
152   int base_address = base.value;
153   if (base_address == 0) {
154   ¦ raise << maybe(current_recipe_name()) << "tried to access location 0 in '" << to_original_string(current_instruction()) << "'\n" << end();
155   ¦ break;
156   }
157   int tag = current_instruction().ingredients.at(1).value;
158   reagent/*copy*/ product = current_instruction().products.at(0);
159   // Update MAYBE_CONVERT product in Run
160   reagent/*copy*/ status = current_instruction().products.at(1);
161   // Update MAYBE_CONVERT status in Run
162   // optimization: directly write results to only update first product when necessary
163   if (tag == static_cast<int>(get_or_insert(Memory, base_address))) {
164   ¦ const reagent& variant = variant_type(base, tag);
165   ¦ trace(9999, "mem") << "storing 1 in location " << status.value << end();
166   ¦ put(Memory, status.value, 1);
167   ¦ if (!is_dummy(product)) {
168   ¦ ¦ // Write Memory in Successful MAYBE_CONVERT in Run
169   ¦ ¦ for (int i = 0;  i < size_of(variant);  ++i) {
170   ¦ ¦ ¦ double val = get_or_insert(Memory, base_address+/*skip tag*/1+i);
171   ¦ ¦ ¦ trace(9999, "mem") << "storing " << no_scientific(val) << " in location " << product.value+i << end();
172   ¦ ¦ ¦ put(Memory, product.value+i, val);
173   ¦ ¦ }
174   ¦ }
175   }
176   else {
177   ¦ trace(9999, "mem") << "storing 0 in location " << status.value << end();
178   ¦ put(Memory, status.value, 0);
179   }
180   goto finish_instruction;
181 }
182 
183 :(code)
184 const reagent variant_type(const reagent& base, int tag) {
185   return variant_type(base.type, tag);
186 }
187 
188 const reagent variant_type(const type_tree* type, int tag) {
189   assert(tag >= 0);
190   const type_tree* root_type = type->atom ? type : type->left;
191   assert(contains_key(Type, root_type->value));
192   assert(!get(Type, root_type->value).name.empty());
193   const type_info& info = get(Type, root_type->value);
194   assert(info.kind == EXCLUSIVE_CONTAINER);
195   reagent/*copy*/ element = info.elements.at(tag);
196   // End variant_type Special-cases
197   return element;
198 }
199 
200 :(scenario maybe_convert_product_type_mismatch)
201 % Hide_errors = true;
202 def main [
203   12:num <- copy 1
204   13:num <- copy 35
205   14:num <- copy 36
206   20:num, 21:bool <- maybe-convert 12:number-or-point/unsafe, 1:variant
207 ]
208 +error: main: 'maybe-convert 12:number-or-point/unsafe, 1:variant' should write to point but '20' has type number
209 
210 :(scenario maybe_convert_dummy_product)
211 def main [
212   12:num <- copy 1
213   13:num <- copy 35
214   14:num <- copy 36
215   _, 21:bool <- maybe-convert 12:number-or-point/unsafe, 1:variant
216 ]
217 $error: 0
218 
219 //:: Allow exclusive containers to be defined in Mu code.
220 
221 :(scenario exclusive_container)
222 exclusive-container foo [
223   x:num
224   y:num
225 ]
226 +parse: --- defining exclusive-container foo
227 +parse: element: {x: "number"}
228 +parse: element: {y: "number"}
229 
230 :(before "End Command Handlers")
231 else if (command == "exclusive-container") {
232   insert_container(command, EXCLUSIVE_CONTAINER, in);
233 }
234 
235 //: arrays are disallowed inside exclusive containers unless their length is
236 //: fixed in advance
237 
238 :(scenario exclusive_container_contains_array)
239 exclusive-container foo [
240   x:array:num:3
241 ]
242 $error: 0
243 
244 :(scenario exclusive_container_disallows_dynamic_array_element)
245 % Hide_errors = true;
246 exclusive-container foo [
247   x:array:num
248 ]
249 +error: container 'foo' cannot determine size of element 'x'
250 
251 //:: To construct exclusive containers out of variant types, use 'merge'.
252 :(scenario lift_to_exclusive_container)
253 exclusive-container foo [
254   x:num
255   y:num
256 ]
257 def main [
258   1:num <- copy 34
259   2:foo <- merge 0/x, 1:num  # tag must be a literal when merging exclusive containers
260   4:foo <- merge 1/y, 1:num
261 ]
262 +mem: storing 0 in location 2
263 +mem: storing 34 in location 3
264 +mem: storing 1 in location 4
265 +mem: storing 34 in location 5
266 
267 //: type-checking for 'merge' on exclusive containers
268 
269 :(scenario merge_handles_exclusive_container)
270 exclusive-container foo [
271   x:num
272   y:bar
273 ]
274 container bar [
275   z:num
276 ]
277 def main [
278   1:foo <- merge 0/x, 34
279 ]
280 +mem: storing 0 in location 1
281 +mem: storing 34 in location 2
282 $error: 0
283 
284 :(scenario merge_requires_literal_tag_for_exclusive_container)
285 % Hide_errors = true;
286 exclusive-container foo [
287   x:num
288   y:bar
289 ]
290 container bar [
291   z:num
292 ]
293 def main [
294   1:num <- copy 0
295   2:foo <- merge 1:num, 34
296 ]
297 +error: main: ingredient 0 of 'merge' should be a literal, for the tag of exclusive-container 'foo' in '2:foo <- merge 1:num, 34'
298 
299 :(scenario merge_handles_exclusive_container_inside_exclusive_container)
300 exclusive-container foo [
301   x:num
302   y:bar
303 ]
304 exclusive-container bar [
305   a:num
306   b:num
307 ]
308 def main [
309   1:num <- copy 0
310   2:bar <- merge 0/a, 34
311   4:foo <- merge 1/y, 2:bar
312 ]
313 +mem: storing 0 in location 5
314 +mem: storing 34 in location 6
315 $error: 0
316 
317 :(before "End check_merge_call Special-cases")
318 case EXCLUSIVE_CONTAINER: {
319   assert(state.data.top().container_element_index == 0);
320   trace(9999, "transform") << "checking exclusive container " << to_string(container) << " vs ingredient " << ingredient_index << end();
321   // easy case: exact match
322   if (types_strictly_match(container, inst.ingredients.at(ingredient_index)))
323   ¦ return;
324   if (!is_literal(ingredients.at(ingredient_index))) {
325   ¦ raise << maybe(caller.name) << "ingredient " << ingredient_index << " of 'merge' should be a literal, for the tag of exclusive-container '" << container_info.name << "' in '" << inst.original_string << "'\n" << end();
326   ¦ return;
327   }
328   reagent/*copy*/ ingredient = ingredients.at(ingredient_index);  // unnecessary copy just to keep this function from modifying caller
329   populate_value(ingredient);
330   if (ingredient.value >= SIZE(container_info.elements)) {
331   ¦ raise << maybe(caller.name) << "invalid tag at " << ingredient_index << " for '" << container_info.name << "' in '" << inst.original_string << "'\n" << end();
332   ¦ return;
333   }
334   const reagent& variant = variant_type(container, ingredient.value);
335   trace(9999, "transform") << "tag: " << ingredient.value << end();
336   // replace union with its variant
337   state.data.pop();
338   state.data.push(merge_check_point(variant, 0));
339   ++ingredient_index;
340   break;
341 }
342 
343 :(scenario merge_check_container_containing_exclusive_container)
344 container foo [
345   x:num
346   y:bar
347 ]
348 exclusive-container bar [
349   x:num
350   y:num
351 ]
352 def main [
353   1:foo <- merge 23, 1/y, 34
354 ]
355 +mem: storing 23 in location 1
356 +mem: storing 1 in location 2
357 +mem: storing 34 in location 3
358 $error: 0
359 
360 :(scenario merge_check_container_containing_exclusive_container_2)
361 % Hide_errors = true;
362 container foo [
363   x:num
364   y:bar
365 ]
366 exclusive-container bar [
367   x:num
368   y:num
369 ]
370 def main [
371   1:foo <- merge 23, 1/y, 34, 35
372 ]
373 +error: main: too many ingredients in '1:foo <- merge 23, 1/y, 34, 35'
374 
375 :(scenario merge_check_exclusive_container_containing_container)
376 exclusive-container foo [
377   x:num
378   y:bar
379 ]
380 container bar [
381   x:num
382   y:num
383 ]
384 def main [
385   1:foo <- merge 1/y, 23, 34
386 ]
387 +mem: storing 1 in location 1
388 +mem: storing 23 in location 2
389 +mem: storing 34 in location 3
390 $error: 0
391 
392 :(scenario merge_check_exclusive_container_containing_container_2)
393 exclusive-container foo [
394   x:num
395   y:bar
396 ]
397 container bar [
398   x:num
399   y:num
400 ]
401 def main [
402   1:foo <- merge 0/x, 23
403 ]
404 $error: 0
405 
406 :(scenario merge_check_exclusive_container_containing_container_3)
407 % Hide_errors = true;
408 exclusive-container foo [
409   x:num
410   y:bar
411 ]
412 container bar [
413   x:num
414   y:num
415 ]
416 def main [
417   1:foo <- merge 1/y, 23
418 ]
419 +error: main: too few ingredients in '1:foo <- merge 1/y, 23'
420 
421 :(scenario merge_check_exclusive_container_containing_container_4)
422 exclusive-container foo [
423   x:num
424   y:bar
425 ]
426 container bar [
427   a:num
428   b:num
429 ]
430 def main [
431   1:bar <- merge 23, 24
432   3:foo <- merge 1/y, 1:bar
433 ]
434 $error: 0
435 
436 //: Since the different variants of an exclusive-container might have
437 //: different sizes, relax the size mismatch check for 'merge' instructions.
438 :(before "End size_mismatch(x) Special-cases")
439 if (current_step_index() < SIZE(Current_routine->steps())
440   ¦ && current_instruction().operation == MERGE
441   ¦ && !current_instruction().products.empty()
442   ¦ && current_instruction().products.at(0).type) {
443   reagent/*copy*/ x = current_instruction().products.at(0);
444   // Update size_mismatch Check for MERGE(x)
445   const type_tree* root_type = x.type->atom ? x.type : x.type->left;
446   assert(root_type->atom);
447   if (get(Type, root_type->value).kind == EXCLUSIVE_CONTAINER)
448   ¦ return size_of(x) < SIZE(data);
449 }
450 
451 :(scenario merge_exclusive_container_with_mismatched_sizes)
452 container foo [
453   x:num
454   y:num
455 ]
456 exclusive-container bar [
457   x:num
458   y:foo
459 ]
460 def main [
461   1:num <- copy 34
462   2:num <- copy 35
463   3:bar <- merge 0/x, 1:num
464   6:bar <- merge 1/foo, 1:num, 2:num
465 ]
466 +mem: storing 0 in location 3
467 +mem: storing 34 in location 4
468 # bar is always 3 large so location 5 is skipped
469 +mem: storing 1 in location 6
470 +mem: storing 34 in location 7
471 +mem: storing 35 in location 8