about summary refs log blame commit diff stats
path: root/html/060immutable.cc.html
blob: 167e28b698ac4fae570e98e9c913c5de8f2b4622 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16















                                                                                                 




                              
                               














                                                                                                           

             
                                                                          







                                                                                                            
                                       

                                                                                        

             
                                                                          

                                            
                                                                                               




                                                                                                             
                                       

                                                                                             

             
                                                                    

                                                   
                                                                              

                  
                                                                    






                                                                                                                                                             
                                       

                                                                                           
                                                  

             
                                                                          

       
                                   




                                                                                                             
                                                                                                                                                                                     

                                                                                                
                                                  

             
                                                                          

       
                                   



                  
                                                                                               




                                                                                                             
                                                                                                                                        

                                                                                        
                                                  

             
                                                                          

       
                                   

                  
                                                                  

                                                                                                             
                                                                                                                                                                                                             

                                                                             
                     
                               


             
                                                                                  

       
                                       

                  
                                                                      
 
                                                                                                       



                                                                                         
                                       

                                                                                             
             
                                                                            


                                                                            
                                     







                                                                                     
                                                                                                                           
 
                                       









                                                                                                                                                                                          
                                                                                                                                                                                                                                                                                                                                                                                                  


                                                                                                                                                                                                                                                                                                                          






                                                                                                                                                                                                                                                                                                                                                                  












































                                                                                                                                                                                                                                                                                                                                                              
                                                  
                     
                               


             
                                                                                  

       
                                                                                      

                  

                                                                                                                                                                                              
 
                                                                                                                            



                                                                                         
                                                                                                                                                                                                                                     






                                                                                                                                                                                                                                                                                                                                                                                                          






                                                                                                                                                                                                                                          
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   
            
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                






                                                                                                                                                                                                                                                                                                             
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
            
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                







                                                                                                                                                                                                                                                                   
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   












































                                                                                                                                                                                                                                                                                                                                                                                                           
                     
                               


             
                                                                                  

       
                                                                                                       

                  
                                                                            

                                                                                       
                                                                                                             



                                                                                         
                                                                                                                                                                                                  

                  
                                                                                                                                                                                        
 
                                       






                                                                                                                                                                                                                                                          
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               





                                                                          
<!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 - 060immutable.cc</title>
<meta name="Generator" content="Vim/7.4">
<meta name="plugin-version" content="vim7.4_v1">
<meta name="syntax" content="cpp">
<meta name="settings" content="use_css,pre_wrap,no_foldcolumn,expand_tabs,prevent_copy=">
<meta name="colorscheme" content="minimal">
<style type="text/css">
<!--
pre { white-space: pre-wrap; font-family: monospace; color: #eeeeee; background-color: #080808; }
body { font-family: monospace; color: #eeeeee; background-color: #080808; }
* { font-size: 1.05em; }
.traceContains { color: #008000; }
.cSpecial { color: #008000; }
.Constant { color: #00a0a0; }
.Comment { color: #9090ff; }
.Delimiter { color: #a04060; }
.Special { color: #ff6060; }
.Identifier { color: #804000; }
-->
</style>

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

-->
</script>
</head>
<body>
<pre id='vimCodeElement'>
<span class="Comment">//: Addresses passed into of a recipe are meant to be immutable unless they're</span>
<span class="Comment">//: also products. This layer will start enforcing this check.</span>

<span class="Delimiter">:(scenario can_modify_value_ingredients)</span>
recipe main [
  local-scope
  p:address:shared:point<span class="Special"> &lt;- </span>new point:type
  foo *p
]
recipe foo p:point [
  local-scope
  load-ingredients
  x:address:number<span class="Special"> &lt;- </span>get-address p<span class="Delimiter">,</span> x:offset
  *x<span class="Special"> &lt;- </span>copy <span class="Constant">34</span>
]
$error: <span class="Constant">0</span>

<span class="Delimiter">:(scenario can_modify_ingredients_that_are_also_products)</span>
recipe main [
  local-scope
  p:address:shared:point<span class="Special"> &lt;- </span>new point:type
  p<span class="Special"> &lt;- </span>foo p
]
recipe foo p:address:shared:point <span class="Delimiter">-&gt;</span> p:address:shared:point [
  local-scope
  load-ingredients
  x:address:number<span class="Special"> &lt;- </span>get-address *p<span class="Delimiter">,</span> x:offset
  *x<span class="Special"> &lt;- </span>copy <span class="Constant">34</span>
]
$error: <span class="Constant">0</span>

<span class="Delimiter">:(scenario ignore_literal_ingredients_for_immutability_checks)</span>
recipe main [
  local-scope
  p:address:shared:d1<span class="Special"> &lt;- </span>new d1:type
  q:number<span class="Special"> &lt;- </span>foo p
]
recipe foo p:address:shared:d1 <span class="Delimiter">-&gt;</span> q:number [
  local-scope
  load-ingredients
  x:address:shared:d1<span class="Special"> &lt;- </span>new d1:type
  y:address:number<span class="Special"> &lt;- </span>get-address *x<span class="Delimiter">,</span> p:offset  <span class="Comment"># ignore this 'p'</span>
  q<span class="Special"> &lt;- </span>copy <span class="Constant">34</span>
]
container d1 [
  p:number
  q:number
]
$error: <span class="Constant">0</span>

<span class="Delimiter">:(scenario cannot_take_address_inside_immutable_ingredients)</span>
<span class="Special">% Hide_errors = true;</span>
recipe main [
  local-scope
  p:address:shared:point<span class="Special"> &lt;- </span>new point:type
  foo p
]
recipe foo p:address:shared:point [
  local-scope
  load-ingredients
  x:address:number<span class="Special"> &lt;- </span>get-address *p<span class="Delimiter">,</span> x:offset
  *x<span class="Special"> &lt;- </span>copy <span class="Constant">34</span>
]
<span class="traceContains">+error: foo: cannot modify ingredient p after instruction 'x:address:number &lt;- get-address *p, x:offset' because it's not also a product of foo</span>

<span class="Delimiter">:(scenario cannot_call_mutating_recipes_on_immutable_ingredients)</span>
<span class="Special">% Hide_errors = true;</span>
recipe main [
  local-scope
  p:address:shared:point<span class="Special"> &lt;- </span>new point:type
  foo p
]
recipe foo p:address:shared:point [
  local-scope
  load-ingredients
  bar p
]
recipe bar p:address:shared:point <span class="Delimiter">-&gt;</span> p:address:shared:point [
  local-scope
  load-ingredients
  x:address:number<span class="Special"> &lt;- </span>get-address *p<span class="Delimiter">,</span> x:offset
  *x<span class="Special"> &lt;- </span>copy <span class="Constant">34</span>
]
<span class="traceContains">+error: foo: cannot modify ingredient p at instruction 'bar p' because it's not also a product of foo</span>

<span class="Delimiter">:(scenario cannot_modify_copies_of_immutable_ingredients)</span>
<span class="Special">% Hide_errors = true;</span>
recipe main [
  local-scope
  p:address:shared:point<span class="Special"> &lt;- </span>new point:type
  foo p
]
recipe foo p:address:shared:point [
  local-scope
  load-ingredients
  q:address:shared:point<span class="Special"> &lt;- </span>copy p
  x:address:number<span class="Special"> &lt;- </span>get-address *q<span class="Delimiter">,</span> x:offset
]
<span class="traceContains">+error: foo: cannot modify q after instruction 'x:address:number &lt;- get-address *q, x:offset' because that would modify ingredient p which is not also a product of foo</span>

<span class="Delimiter">:(scenario can_traverse_immutable_ingredients)</span>
container test-list [
  next:address:shared:test-list
]
recipe main [
  local-scope
  p:address:shared:test-list<span class="Special"> &lt;- </span>new test-list:type
  foo p
]
recipe foo p:address:shared:test-list [
  local-scope
  load-ingredients
  p2:address:shared:test-list<span class="Special"> &lt;- </span>bar p
]
recipe bar x:address:shared:test-list <span class="Delimiter">-&gt;</span> y:address:shared:test-list [
  local-scope
  load-ingredients
  y<span class="Special"> &lt;- </span>get *x<span class="Delimiter">,</span> next:offset
]
$error: <span class="Constant">0</span>

<span class="Delimiter">:(scenario handle_optional_ingredients_in_immutability_checks)</span>
recipe main [
  k:address:shared:number<span class="Special"> &lt;- </span>new number:type
  test k
]
<span class="Comment"># recipe taking an immutable address ingredient</span>
recipe test k:address:shared:number [
  local-scope
  load-ingredients
  foo k
]
<span class="Comment"># ..calling a recipe with an optional address ingredient</span>
recipe foo <span class="Delimiter">-&gt;</span> [
  local-scope
  load-ingredients
  k:address:shared:number<span class="Delimiter">,</span> found?:boolean<span class="Special"> &lt;- </span>next-ingredient
]
$error: <span class="Constant">0</span>

<span class="Delimiter">:(before &quot;End Transforms&quot;)</span>
Transform<span class="Delimiter">.</span>push_back<span class="Delimiter">(</span>check_immutable_ingredients<span class="Delimiter">);</span>  <span class="Comment">// idempotent</span>

<span class="Delimiter">:(code)</span>
void check_immutable_ingredients<span class="Delimiter">(</span>recipe_ordinal r<span class="Delimiter">)</span> <span class="Delimiter">{</span>
  <span class="Comment">// to ensure a reagent isn't modified, it suffices to show that we never</span>
  <span class="Comment">// call get-address or index-address with it, and that any non-primitive</span>
  <span class="Comment">// recipe calls in the body aren't returning it as a product.</span>
  const recipe&amp; caller = get<span class="Delimiter">(</span>Recipe<span class="Delimiter">,</span> r<span class="Delimiter">);</span>
  trace<span class="Delimiter">(</span><span class="Constant">9991</span><span class="Delimiter">,</span> <span class="Constant">&quot;transform&quot;</span><span class="Delimiter">)</span> &lt;&lt; <span class="Constant">&quot;--- check mutability of ingredients in recipe &quot;</span> &lt;&lt; caller<span class="Delimiter">.</span>name &lt;&lt; end<span class="Delimiter">();</span>
  if <span class="Delimiter">(</span>!caller<span class="Delimiter">.</span>has_header<span class="Delimiter">)</span> <span class="Identifier">return</span><span class="Delimiter">;</span>  <span class="Comment">// skip check for old-style recipes calling next-ingredient directly</span>
  for <span class="Delimiter">(</span>long long int i = <span class="Constant">0</span><span class="Delimiter">;</span> i &lt; SIZE<span class="Delimiter">(</span>caller<span class="Delimiter">.</span>ingredients<span class="Delimiter">);</span> ++i<span class="Delimiter">)</span> <span class="Delimiter">{</span>
    const reagent&amp; current_ingredient = caller<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">);</span>
    if <span class="Delimiter">(</span>!is_mu_address<span class="Delimiter">(</span>current_ingredient<span class="Delimiter">))</span> <span class="Identifier">continue</span><span class="Delimiter">;</span>  <span class="Comment">// will be copied</span>
    if <span class="Delimiter">(</span>is_present_in_products<span class="Delimiter">(</span>caller<span class="Delimiter">,</span> current_ingredient<span class="Delimiter">.</span>name<span class="Delimiter">))</span> <span class="Identifier">continue</span><span class="Delimiter">;</span>  <span class="Comment">// not expected to be immutable</span>
    <span class="Comment">// End Immutable Ingredients Special-cases</span>
    set&lt;string&gt; immutable_vars<span class="Delimiter">;</span>
    immutable_vars<span class="Delimiter">.</span>insert<span class="Delimiter">(</span>current_ingredient<span class="Delimiter">.</span>name<span class="Delimiter">);</span>
    for <span class="Delimiter">(</span>long long int i = <span class="Constant">0</span><span class="Delimiter">;</span> i &lt; SIZE<span class="Delimiter">(</span>caller<span class="Delimiter">.</span>steps<span class="Delimiter">);</span> ++i<span class="Delimiter">)</span> <span class="Delimiter">{</span>
      const instruction&amp; inst = caller<span class="Delimiter">.</span>steps<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">);</span>
      check_immutable_ingredient_in_instruction<span class="Delimiter">(</span>inst<span class="Delimiter">,</span> immutable_vars<span class="Delimiter">,</span> current_ingredient<span class="Delimiter">.</span>name<span class="Delimiter">,</span> caller<span class="Delimiter">);</span>
      update_aliases<span class="Delimiter">(</span>inst<span class="Delimiter">,</span> immutable_vars<span class="Delimiter">);</span>
    <span class="Delimiter">}</span>
  <span class="Delimiter">}</span>
<span class="Delimiter">}</span>

void update_aliases<span class="Delimiter">(</span>const instruction&amp; inst<span class="Delimiter">,</span> set&lt;string&gt;&amp; current_ingredient_and_aliases<span class="Delimiter">)</span> <span class="Delimiter">{</span>
  set&lt;long long int&gt; current_ingredient_indices = ingredient_indices<span class="Delimiter">(</span>inst<span class="Delimiter">,</span> current_ingredient_and_aliases<span class="Delimiter">);</span>
  if <span class="Delimiter">(</span>!contains_key<span class="Delimiter">(</span>Recipe<span class="Delimiter">,</span> inst<span class="Delimiter">.</span>operation<span class="Delimiter">))</span> <span class="Delimiter">{</span>
    <span class="Comment">// primitive recipe</span>
    if <span class="Delimiter">(</span>inst<span class="Delimiter">.</span>operation == COPY<span class="Delimiter">)</span> <span class="Delimiter">{</span>
      for <span class="Delimiter">(</span>set&lt;long long int&gt;::iterator p = current_ingredient_indices<span class="Delimiter">.</span>begin<span class="Delimiter">();</span> p != current_ingredient_indices<span class="Delimiter">.</span>end<span class="Delimiter">();</span> ++p<span class="Delimiter">)</span> <span class="Delimiter">{</span>
        current_ingredient_and_aliases<span class="Delimiter">.</span>insert<span class="Delimiter">(</span>inst<span class="Delimiter">.</span>products<span class="Delimiter">.</span>at<span class="Delimiter">(</span>*p<span class="Delimiter">).</span>name<span class="Delimiter">);</span>
      <span class="Delimiter">}</span>
    <span class="Delimiter">}</span>
  <span class="Delimiter">}</span>
  else <span class="Delimiter">{</span>
    <span class="Comment">// defined recipe</span>
    set&lt;long long int&gt; contained_in_product_indices = scan_contained_in_product_indices<span class="Delimiter">(</span>inst<span class="Delimiter">,</span> current_ingredient_indices<span class="Delimiter">);</span>
    for <span class="Delimiter">(</span>set&lt;long long int&gt;::iterator p = contained_in_product_indices<span class="Delimiter">.</span>begin<span class="Delimiter">();</span> p != contained_in_product_indices<span class="Delimiter">.</span>end<span class="Delimiter">();</span> ++p<span class="Delimiter">)</span> <span class="Delimiter">{</span>
      if <span class="Delimiter">(</span>*p &lt; SIZE<span class="Delimiter">(</span>inst<span class="Delimiter">.</span>products<span class="Delimiter">))</span>
        current_ingredient_and_aliases<span class="Delimiter">.</span>insert<span class="Delimiter">(</span>inst<span class="Delimiter">.</span>products<span class="Delimiter">.</span>at<span class="Delimiter">(</span>*p<span class="Delimiter">).</span>name<span class="Delimiter">);</span>
    <span class="Delimiter">}</span>
  <span class="Delimiter">}</span>
<span class="Delimiter">}</span>

set&lt;long long int&gt; scan_contained_in_product_indices<span class="Delimiter">(</span>const instruction&amp; inst<span class="Delimiter">,</span> set&lt;long long int&gt;&amp; ingredient_indices<span class="Delimiter">)</span> <span class="Delimiter">{</span>
  set&lt;string&gt; selected_ingredient_names<span class="Delimiter">;</span>
  const recipe&amp; callee = get<span class="Delimiter">(</span>Recipe<span class="Delimiter">,</span> inst<span class="Delimiter">.</span>operation<span class="Delimiter">);</span>
  for <span class="Delimiter">(</span>set&lt;long long int&gt;::iterator p = ingredient_indices<span class="Delimiter">.</span>begin<span class="Delimiter">();</span> p != ingredient_indices<span class="Delimiter">.</span>end<span class="Delimiter">();</span> ++p<span class="Delimiter">)</span> <span class="Delimiter">{</span>
    if <span class="Delimiter">(</span>*p &gt;= SIZE<span class="Delimiter">(</span>callee<span class="Delimiter">.</span>ingredients<span class="Delimiter">))</span> <span class="Identifier">continue</span><span class="Delimiter">;</span>  <span class="Comment">// optional immutable ingredient</span>
    selected_ingredient_names<span class="Delimiter">.</span>insert<span class="Delimiter">(</span>callee<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span>*p<span class="Delimiter">).</span>name<span class="Delimiter">);</span>
  <span class="Delimiter">}</span>
  set&lt;long long int&gt; result<span class="Delimiter">;</span>
  for <span class="Delimiter">(</span>long long int i = <span class="Constant">0</span><span class="Delimiter">;</span> i &lt; SIZE<span class="Delimiter">(</span>callee<span class="Delimiter">.</span>products<span class="Delimiter">);</span> ++i<span class="Delimiter">)</span> <span class="Delimiter">{</span>
    const reagent&amp; current_product = callee<span class="Delimiter">.</span>products<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">);</span>
    const string_tree* contained_in_name = property<span class="Delimiter">(</span>current_product<span class="Delimiter">,</span> <span class="Constant">&quot;contained-in&quot;</span><span class="Delimiter">);</span>
    if <span class="Delimiter">(</span>contained_in_name &amp;&amp; selected_ingredient_names<span class="Delimiter">.</span>find<span class="Delimiter">(</span>contained_in_name<span class="Delimiter">-&gt;</span>value<span class="Delimiter">)</span> != selected_ingredient_names<span class="Delimiter">.</span>end<span class="Delimiter">())</span>
      result<span class="Delimiter">.</span>insert<span class="Delimiter">(</span>i<span class="Delimiter">);</span>
  <span class="Delimiter">}</span>
  <span class="Identifier">return</span> result<span class="Delimiter">;</span>
<span class="Delimiter">}</span>

<span class="Delimiter">:(scenarios transform)</span>
<span class="Delimiter">:(scenario immutability_infects_contained_in_variables)</span>
<span class="Special">% Hide_errors = true;</span>
container test-list [
  next:address:shared:test-list
]
recipe main [
  local-scope
  p:address:shared:test-list<span class="Special"> &lt;- </span>new test-list:type
  foo p
]
recipe foo p:address:shared:test-list [  <span class="Comment"># p is immutable</span>
  local-scope
  load-ingredients
  p2:address:shared:test-list<span class="Special"> &lt;- </span>test-next p  <span class="Comment"># p2 is immutable</span>
  p3:address:address:shared:test-list<span class="Special"> &lt;- </span>get-address *p2<span class="Delimiter">,</span> next:offset  <span class="Comment"># signal modification of p2</span>
]
recipe test-next x:address:shared:test-list <span class="Delimiter">-&gt;</span> y:address:shared:test-list/contained-in:x [
  local-scope
  load-ingredients
  y<span class="Special"> &lt;- </span>get *x<span class="Delimiter">,</span> next:offset
]
<span class="traceContains">+error: foo: cannot modify p2 after instruction 'p3:address:address:shared:test-list &lt;- get-address *p2, next:offset' because that would modify ingredient p which is not also a product of foo</span>

<span class="Delimiter">:(code)</span>
void check_immutable_ingredient_in_instruction<span class="Delimiter">(</span>const instruction&amp; inst<span class="Delimiter">,</span> const set&lt;string&gt;&amp; current_ingredient_and_aliases<span class="Delimiter">,</span> const string&amp; original_ingredient_name<span class="Delimiter">,</span> const recipe&amp; caller<span class="Delimiter">)</span> <span class="Delimiter">{</span>
  set&lt;long long int&gt; current_ingredient_indices = ingredient_indices<span class="Delimiter">(</span>inst<span class="Delimiter">,</span> current_ingredient_and_aliases<span class="Delimiter">);</span>
  if <span class="Delimiter">(</span>current_ingredient_indices<span class="Delimiter">.</span>empty<span class="Delimiter">())</span> <span class="Identifier">return</span><span class="Delimiter">;</span>  <span class="Comment">// ingredient not found in call</span>
  for <span class="Delimiter">(</span>set&lt;long long int&gt;::iterator p = current_ingredient_indices<span class="Delimiter">.</span>begin<span class="Delimiter">();</span> p != current_ingredient_indices<span class="Delimiter">.</span>end<span class="Delimiter">();</span> ++p<span class="Delimiter">)</span> <span class="Delimiter">{</span>
    const long long int current_ingredient_index = *p<span class="Delimiter">;</span>
    reagent current_ingredient = inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span>current_ingredient_index<span class="Delimiter">);</span>
    canonize_type<span class="Delimiter">(</span>current_ingredient<span class="Delimiter">);</span>
    const string&amp; current_ingredient_name = current_ingredient<span class="Delimiter">.</span>name<span class="Delimiter">;</span>
    if <span class="Delimiter">(</span>!contains_key<span class="Delimiter">(</span>Recipe<span class="Delimiter">,</span> inst<span class="Delimiter">.</span>operation<span class="Delimiter">))</span> <span class="Delimiter">{</span>
      <span class="Comment">// primitive recipe</span>
      if <span class="Delimiter">(</span>inst<span class="Delimiter">.</span>operation == GET_ADDRESS || inst<span class="Delimiter">.</span>operation == INDEX_ADDRESS<span class="Delimiter">)</span> <span class="Delimiter">{</span>
        if <span class="Delimiter">(</span>current_ingredient_name == original_ingredient_name<span class="Delimiter">)</span>
          raise_error &lt;&lt; maybe<span class="Delimiter">(</span>caller<span class="Delimiter">.</span>name<span class="Delimiter">)</span> &lt;&lt; <span class="Constant">&quot;cannot modify ingredient &quot;</span> &lt;&lt; current_ingredient_name &lt;&lt; <span class="Constant">&quot; after instruction '&quot;</span> &lt;&lt; to_string<span class="Delimiter">(</span>inst<span class="Delimiter">)</span> &lt;&lt; <span class="Constant">&quot;' because it's not also a product of &quot;</span> &lt;&lt; caller<span class="Delimiter">.</span>name &lt;&lt; <span class="cSpecial">'\n'</span> &lt;&lt; end<span class="Delimiter">();</span>
        else
          raise_error &lt;&lt; maybe<span class="Delimiter">(</span>caller<span class="Delimiter">.</span>name<span class="Delimiter">)</span> &lt;&lt; <span class="Constant">&quot;cannot modify &quot;</span> &lt;&lt; current_ingredient_name &lt;&lt; <span class="Constant">&quot; after instruction '&quot;</span> &lt;&lt; to_string<span class="Delimiter">(</span>inst<span class="Delimiter">)</span> &lt;&lt; <span class="Constant">&quot;' because that would modify ingredient &quot;</span> &lt;&lt; original_ingredient_name &lt;&lt; <span class="Constant">&quot; which is not also a product of &quot;</span> &lt;&lt; caller<span class="Delimiter">.</span>name &lt;&lt; <span class="cSpecial">'\n'</span> &lt;&lt; end<span class="Delimiter">();</span>
      <span class="Delimiter">}</span>
    <span class="Delimiter">}</span>
    else <span class="Delimiter">{</span>
      <span class="Comment">// defined recipe</span>
      if <span class="Delimiter">(</span>!is_mu_address<span class="Delimiter">(</span>current_ingredient<span class="Delimiter">))</span> <span class="Identifier">return</span><span class="Delimiter">;</span>  <span class="Comment">// making a copy is ok</span>
      if <span class="Delimiter">(</span>is_modified_in_recipe<span class="Delimiter">(</span>inst<span class="Delimiter">.</span>operation<span class="Delimiter">,</span> current_ingredient_index<span class="Delimiter">,</span> caller<span class="Delimiter">))</span> <span class="Delimiter">{</span>
        if <span class="Delimiter">(</span>current_ingredient_name == original_ingredient_name<span class="Delimiter">)</span>
          raise_error &lt;&lt; maybe<span class="Delimiter">(</span>caller<span class="Delimiter">.</span>name<span class="Delimiter">)</span> &lt;&lt; <span class="Constant">&quot;cannot modify ingredient &quot;</span> &lt;&lt; current_ingredient_name &lt;&lt; <span class="Constant">&quot; at instruction '&quot;</span> &lt;&lt; to_string<span class="Delimiter">(</span>inst<span class="Delimiter">)</span> &lt;&lt; <span class="Constant">&quot;' because it's not also a product of &quot;</span> &lt;&lt; caller<span class="Delimiter">.</span>name &lt;&lt; <span class="cSpecial">'\n'</span> &lt;&lt; end<span class="Delimiter">();</span>
        else
          raise_error &lt;&lt; maybe<span class="Delimiter">(</span>caller<span class="Delimiter">.</span>name<span class="Delimiter">)</span> &lt;&lt; <span class="Constant">&quot;cannot modify &quot;</span> &lt;&lt; current_ingredient_name &lt;&lt; <span class="Constant">&quot; after instruction '&quot;</span> &lt;&lt; to_string<span class="Delimiter">(</span>inst<span class="Delimiter">)</span> &lt;&lt; <span class="Constant">&quot;' because that would modify ingredient &quot;</span> &lt;&lt; original_ingredient_name &lt;&lt; <span class="Constant">&quot; which is not also a product of &quot;</span> &lt;&lt; caller<span class="Delimiter">.</span>name &lt;&lt; <span class="cSpecial">'\n'</span> &lt;&lt; end<span class="Delimiter">();</span>
      <span class="Delimiter">}</span>
    <span class="Delimiter">}</span>
  <span class="Delimiter">}</span>
<span class="Delimiter">}</span>

bool is_modified_in_recipe<span class="Delimiter">(</span>recipe_ordinal r<span class="Delimiter">,</span> long long int ingredient_index<span class="Delimiter">,</span> const recipe&amp; caller<span class="Delimiter">)</span> <span class="Delimiter">{</span>
  const recipe&amp; callee = get<span class="Delimiter">(</span>Recipe<span class="Delimiter">,</span> r<span class="Delimiter">);</span>
  if <span class="Delimiter">(</span>!callee<span class="Delimiter">.</span>has_header<span class="Delimiter">)</span> <span class="Delimiter">{</span>
    raise_error &lt;&lt; maybe<span class="Delimiter">(</span>caller<span class="Delimiter">.</span>name<span class="Delimiter">)</span> &lt;&lt; <span class="Constant">&quot;can't check mutability of ingredients in &quot;</span> &lt;&lt; callee<span class="Delimiter">.</span>name &lt;&lt; <span class="Constant">&quot; because it uses 'next-ingredient' directly, rather than a recipe header.</span><span class="cSpecial">\n</span><span class="Constant">&quot;</span> &lt;&lt; end<span class="Delimiter">();</span>
    <span class="Identifier">return</span> <span class="Constant">true</span><span class="Delimiter">;</span>
  <span class="Delimiter">}</span>
  if <span class="Delimiter">(</span>ingredient_index &gt;= SIZE<span class="Delimiter">(</span>callee<span class="Delimiter">.</span>ingredients<span class="Delimiter">))</span> <span class="Identifier">return</span> <span class="Constant">false</span><span class="Delimiter">;</span>  <span class="Comment">// optional immutable ingredient</span>
  <span class="Identifier">return</span> is_present_in_products<span class="Delimiter">(</span>callee<span class="Delimiter">,</span> callee<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span>ingredient_index<span class="Delimiter">).</span>name<span class="Delimiter">);</span>
<span class="Delimiter">}</span>

bool is_present_in_products<span class="Delimiter">(</span>const recipe&amp; callee<span class="Delimiter">,</span> const string&amp; ingredient_name<span class="Delimiter">)</span> <span class="Delimiter">{</span>
  for <span class="Delimiter">(</span>long long int i = <span class="Constant">0</span><span class="Delimiter">;</span> i &lt; SIZE<span class="Delimiter">(</span>callee<span class="Delimiter">.</span>products<span class="Delimiter">);</span> ++i<span class="Delimiter">)</span> <span class="Delimiter">{</span>
    if <span class="Delimiter">(</span>callee<span class="Delimiter">.</span>products<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">).</span>name == ingredient_name<span class="Delimiter">)</span>
      <span class="Identifier">return</span> <span class="Constant">true</span><span class="Delimiter">;</span>
  <span class="Delimiter">}</span>
  <span class="Identifier">return</span> <span class="Constant">false</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>

bool is_present_in_ingredients<span class="Delimiter">(</span>const recipe&amp; callee<span class="Delimiter">,</span> const string&amp; ingredient_name<span class="Delimiter">)</span> <span class="Delimiter">{</span>
  for <span class="Delimiter">(</span>long long int i = <span class="Constant">0</span><span class="Delimiter">;</span> i &lt; SIZE<span class="Delimiter">(</span>callee<span class="Delimiter">.</span>ingredients<span class="Delimiter">);</span> ++i<span class="Delimiter">)</span> <span class="Delimiter">{</span>
    if <span class="Delimiter">(</span>callee<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">).</span>name == ingredient_name<span class="Delimiter">)</span>
      <span class="Identifier">return</span> <span class="Constant">true</span><span class="Delimiter">;</span>
  <span class="Delimiter">}</span>
  <span class="Identifier">return</span> <span class="Constant">false</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>

set&lt;long long int&gt; ingredient_indices<span class="Delimiter">(</span>const instruction&amp; inst<span class="Delimiter">,</span> const set&lt;string&gt;&amp; ingredient_names<span class="Delimiter">)</span> <span class="Delimiter">{</span>
  set&lt;long long int&gt; result<span class="Delimiter">;</span>
  for <span class="Delimiter">(</span>long long int i = <span class="Constant">0</span><span class="Delimiter">;</span> i &lt; SIZE<span class="Delimiter">(</span>inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">);</span> ++i<span class="Delimiter">)</span> <span class="Delimiter">{</span>
    if <span class="Delimiter">(</span>is_literal<span class="Delimiter">(</span>inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">)))</span> <span class="Identifier">continue</span><span class="Delimiter">;</span>
    if <span class="Delimiter">(</span>ingredient_names<span class="Delimiter">.</span>find<span class="Delimiter">(</span>inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">).</span>name<span class="Delimiter">)</span> != ingredient_names<span class="Delimiter">.</span>end<span class="Delimiter">())</span>
      result<span class="Delimiter">.</span>insert<span class="Delimiter">(</span>i<span class="Delimiter">);</span>
  <span class="Delimiter">}</span>
  <span class="Identifier">return</span> result<span class="Delimiter">;</span>
<span class="Delimiter">}</span>

<span class="Comment">//: Sometimes you want to pass in two addresses, one pointing inside the</span>
<span class="Comment">//: other. For example, you want to delete a node from a linked list. You</span>
<span class="Comment">//: can't pass both pointers back out, because if a caller tries to make both</span>
<span class="Comment">//: identical then you can't tell which value will be written on the way out.</span>
<span class="Comment">//:</span>
<span class="Comment">//: Experimental solution: just tell mu that one points inside the other.</span>
<span class="Comment">//: This way we can return just one pointer as high up as necessary to capture</span>
<span class="Comment">//: all modifications performed by a recipe.</span>
<span class="Comment">//:</span>
<span class="Comment">//: We'll see if we end up wanting to abuse /contained-in for other reasons.</span>

<span class="Delimiter">:(scenarios transform)</span>
<span class="Delimiter">:(scenario can_modify_contained_in_addresses)</span>
container test-list [
  next:address:shared:test-list
]
recipe main [
  local-scope
  p:address:shared:test-list<span class="Special"> &lt;- </span>new test-list:type
  foo p
]
recipe foo p:address:shared:test-list <span class="Delimiter">-&gt;</span> p:address:shared:test-list [
  local-scope
  load-ingredients
  p2:address:shared:test-list<span class="Special"> &lt;- </span>test-next p
  p<span class="Special"> &lt;- </span>test-remove p2<span class="Delimiter">,</span> p
]
recipe test-next x:address:shared:test-list <span class="Delimiter">-&gt;</span> y:address:shared:test-list [
  local-scope
  load-ingredients
  y<span class="Special"> &lt;- </span>get *x<span class="Delimiter">,</span> next:offset
]
recipe test-remove x:address:shared:test-list/contained-in:from<span class="Delimiter">,</span> from:address:shared:test-list <span class="Delimiter">-&gt;</span> from:address:shared:test-list [
  local-scope
  load-ingredients
  x2:address:address:shared:test-list<span class="Special"> &lt;- </span>get-address *x<span class="Delimiter">,</span> next:offset  <span class="Comment"># pretend modification</span>
]
$error: <span class="Constant">0</span>

<span class="Delimiter">:(before &quot;End Immutable Ingredients Special-cases&quot;)</span>
if <span class="Delimiter">(</span>has_property<span class="Delimiter">(</span>current_ingredient<span class="Delimiter">,</span> <span class="Constant">&quot;contained-in&quot;</span><span class="Delimiter">))</span> <span class="Delimiter">{</span>
  const string_tree* tmp = property<span class="Delimiter">(</span>current_ingredient<span class="Delimiter">,</span> <span class="Constant">&quot;contained-in&quot;</span><span class="Delimiter">);</span>
  if <span class="Delimiter">(</span>tmp<span class="Delimiter">-&gt;</span>left || tmp<span class="Delimiter">-&gt;</span>right
      || !is_present_in_ingredients<span class="Delimiter">(</span>caller<span class="Delimiter">,</span> tmp<span class="Delimiter">-&gt;</span>value<span class="Delimiter">)</span>
      || !is_present_in_products<span class="Delimiter">(</span>caller<span class="Delimiter">,</span> tmp<span class="Delimiter">-&gt;</span>value<span class="Delimiter">))</span>
    raise_error &lt;&lt; maybe<span class="Delimiter">(</span>caller<span class="Delimiter">.</span>name<span class="Delimiter">)</span> &lt;&lt; <span class="Constant">&quot;contained-in can only point to another ingredient+product, but got &quot;</span> &lt;&lt; to_string<span class="Delimiter">(</span>property<span class="Delimiter">(</span>current_ingredient<span class="Delimiter">,</span> <span class="Constant">&quot;contained-in&quot;</span><span class="Delimiter">))</span> &lt;&lt; <span class="cSpecial">'\n'</span> &lt;&lt; end<span class="Delimiter">();</span>
  <span class="Identifier">continue</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
</pre>
</body>
</html>
<!-- vim: set foldmethod=manual : -->