1 //: Construct types out of their constituent fields.
  2 
  3 :(scenario merge)
  4 container foo [
  5   x:num
  6   y:num
  7 ]
  8 def main [
  9   1:foo <- merge 3, 4
 10 ]
 11 +mem: storing 3 in location 1
 12 +mem: storing 4 in location 2
 13 
 14 :(before "End Primitive Recipe Declarations")
 15 MERGE,
 16 :(before "End Primitive Recipe Numbers")
 17 put(Recipe_ordinal, "merge", MERGE);
 18 :(before "End Primitive Recipe Checks")
 19 case MERGE: {
 20   // type-checking in a separate transform below
 21   break;
 22 }
 23 :(before "End Primitive Recipe Implementations")
 24 case MERGE: {
 25   products.resize(1);
 26   for (int i = 0;  i < SIZE(ingredients);  ++i)
 27   ¦ for (int j = 0;  j < SIZE(ingredients.at(i));  ++j)
 28   ¦ ¦ products.at(0).push_back(ingredients.at(i).at(j));
 29   break;
 30 }
 31 
 32 //: type-check 'merge' to avoid interpreting numbers as addresses
 33 
 34 :(scenario merge_check)
 35 def main [
 36   1:point <- merge 3, 4
 37 ]
 38 $error: 0
 39 
 40 :(scenario merge_check_missing_element)
 41 % Hide_errors = true;
 42 def main [
 43   1:point <- merge 3
 44 ]
 45 +error: main: too few ingredients in '1:point <- merge 3'
 46 
 47 :(scenario merge_check_extra_element)
 48 % Hide_errors = true;
 49 def main [
 50   1:point <- merge 3, 4, 5
 51 ]
 52 +error: main: too many ingredients in '1:point <- merge 3, 4, 5'
 53 
 54 //: We want to avoid causing memory corruption, but other than that we want to
 55 //: be flexible in how we construct containers of containers. It should be
 56 //: equally easy to define a container out of primitives or intermediate
 57 //: container fields.
 58 
 59 :(scenario merge_check_recursive_containers)
 60 def main [
 61   1:point <- merge 3, 4
 62   1:point-number <- merge 1:point, 5
 63 ]
 64 $error: 0
 65 
 66 :(scenario merge_check_recursive_containers_2)
 67 % Hide_errors = true;
 68 def main [
 69   1:point <- merge 3, 4
 70   2:point-number <- merge 1:point
 71 ]
 72 +error: main: too few ingredients in '2:point-number <- merge 1:point'
 73 
 74 :(scenario merge_check_recursive_containers_3)
 75 def main [
 76   1:point-number <- merge 3, 4, 5
 77 ]
 78 $error: 0
 79 
 80 :(scenario merge_check_recursive_containers_4)
 81 % Hide_errors = true;
 82 def main [
 83   1:point-number <- merge 3, 4
 84 ]
 85 +error: main: too few ingredients in '1:point-number <- merge 3, 4'
 86 
 87 :(scenario merge_check_reflexive)
 88 % Hide_errors = true;
 89 def main [
 90   1:point <- merge 3, 4
 91   2:point <- merge 1:point
 92 ]
 93 $error: 0
 94 
 95 //: Since a container can be merged in several ways, we need to be able to
 96 //: backtrack through different possibilities. Later we'll allow creating
 97 //: exclusive containers which contain just one of rather than all of their
 98 //: elements. That will also require backtracking capabilities. Here's the
 99 //: state we need to maintain for backtracking:
100 
101 :(before "End Types")
102 struct merge_check_point {
103   reagent container;
104   int container_element_index;
105   merge_check_point(const reagent& c, int i) :container(c), container_element_index(i) {}
106 };
107 
108 struct merge_check_state {
109   stack<merge_check_point> data;
110 };
111 
112 :(before "End Checks")
113 Transform.push_back(check_merge_calls);  // idempotent
114 :(code)
115 void check_merge_calls(const recipe_ordinal r) {
116   const recipe& caller = get(Recipe, r);
117   trace(9991, "transform") << "--- type-check merge instructions in recipe " << caller.name << end();
118   for (int i = 0;  i < SIZE(caller.steps);  ++i) {
119   ¦ const instruction& inst = caller.steps.at(i);
120   ¦ if (inst.name != "merge") continue;
121   ¦ if (SIZE(inst.products) != 1) {
122   ¦ ¦ raise << maybe(caller.name) << "'merge' should yield a single product in '" << to_original_string(inst) << "'\n" << end();
123   ¦ ¦ continue;
124   ¦ }
125   ¦ reagent/*copy*/ product = inst.products.at(0);
126   ¦ // Update product While Type-checking Merge
127   ¦ const type_tree* product_base_type = product.type->atom ? product.type : product.type->left;
128   ¦ assert(product_base_type->atom);
129   ¦ if (product_base_type->value == 0 || !contains_key(Type, product_base_type->value)) {
130   ¦ ¦ raise << maybe(caller.name) << &quo
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>Mu - 071deep_copy.cc</title>
<meta name="Generator" content="Vim/7.4">
<meta name="plugin-version" content="vim7.4_v2">
<meta name="syntax" content="cpp">
<meta name="settings" content="number_lines,use_css,pre_wrap,no_foldcolumn,expand_tabs,line_ids,prevent_copy=">
<meta name="colorscheme" content="minimal">
<style type="text/css">
<!--
pre { white-space: pre-wrap; font-family: monospace; color: #aaaaaa; background-color: #080808; }
body { font-size: 12pt; font-family: monospace; color: #aaaaaa; background-color: #080808; }
a { color:#eeeeee; text-decoration: none; }
a:hover { text-decoration: underline; }
* { font-size: 12pt; font-size: 1em; }
.Constant { color: #00a0a0; }
.muRecipe { color: #ff8700; }
.Special { color: #c00000; }
.Conceal { color: #4e4e4e; }
.muData { color: #ffff00; }
.Comment { color: #9090ff; }
.Comment a { color:#0000ee; text-decoration:underline; }
.Delimiter { color: #800080; }
.LineNr { color: #444444; }
.cSpecial { color: #008000; }
.Normal { color: #aaaaaa; background-color: #080808; padding-bottom: 1px; }
.Identifier { color: #c0a020; }
.traceContains { color: #008000; }
-->
</style>

<script type='text/javascript'>
<!--

/* function to open any folds containing a jumped-to line before jumping to it */
function JumpToLine()
{
  var lineNum;
  lineNum = window.location.hash;
  lineNum = lineNum.substr(1); /* strip off '#' */

  if (lineNum.indexOf('L') == -1) {
    lineNum = 'L'+lineNum;
  }
  lineElem = document.getElementById(lineNum);
  /* Always jump to new location even if the line was hidden inside a fold, or
   * we corrected the raw number to a line ID.
   */
  if (lineElem) {
    lineElem.scrollIntoView(true);
  }
  return true;
}
if ('onhashchange' in window) {
  window.onhashchange = JumpToLine;
}

-->
</script>
</head>
<body onload='JumpToLine();'>
<pre id='vimCodeElement'>
<span id="L1" class="LineNr">  1 </span><span class="Comment">// To recursively copy containers and any addresses they contain, use</span>
<span id="L2" class="LineNr">  2 </span><span class="Comment">// 'deep-copy'.</span>
<span id="L3" class="LineNr">  3 </span><span class="Comment">//</span>
<span id="L4" class="LineNr">  4 </span><span class="Comment">// Invariant: After a deep-copy its ingredient and result will point to no</span>
<span id="L5" class="LineNr">  5 </span><span class="Comment">// common addresses.</span>
<span id="L6" class="LineNr">  6 </span><span class="Comment">// Implications: Refcounts of all data pointed to by the original ingredient</span>
<span id="L7" class="LineNr">  7 </span><span class="Comment">// will remain unchanged. Refcounts of all data pointed to by the (newly</span>
<span id="L8" class="LineNr">  8 </span><span class="Comment">// created) result will be 1, in the absence of cycles.</span>
<span id="L9" class="LineNr">  9 </span><span class="Comment">//</span>
<span id="L10" class="LineNr"> 10 </span><span class="Comment">// We do handle cycles in the ingredient, however. All cycles are translated</span>
<span id="L11" class="LineNr"> 11 </span><span class="Comment">// to new cycles in the product.</span>
<span id="L12" class="LineNr"> 12 </span>
<span id="L13" class="LineNr"> 13 </span><span class="Delimiter">:(scenario deep_copy_number)</span>
<span id="L14" class="LineNr"> 14 </span><span class="muRecipe">def</span> <a href='000organization.cc.html#L113'>main</a> [
<span id="L15" class="LineNr"> 15 </span>  local-scope
<span id="L16" class="LineNr"> 16 </span>  <span class="Normal">x</span>:num<span class="Special"> &lt;- </span>copy <span class="Constant">34</span>
<span id="L17" class="LineNr">