about summary refs log blame commit diff stats
path: root/html/056shape_shifting_recipe.cc.html
blob: 6bb9c81e256a7fe61745b1a899c3c6d6828eaec1 (plain) (tree)
1
2
3
4
5
6
7
8
9
10



                                                                                          
                                               

                                                
                                  

                                                                                         

                       
                                                                                                 
                                                                                            
                                      



                                   
                                  

                                                                           
                             

                             
                            
                               
                            

        





                               

       
                         


                                                                                                             
                                        



                                                                                                                                                                                   
                                                                                            




                                                                                 
                                                                                          






                                                                  



                                                                                                          
 



                                                                                                        
                                                                         

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         
 
                                                                           
                                                                                                                                                                                                                                                   

                                                                                                                                                                                                                                                                                           

                                                                                                                                                                                                                                                                 



                                                                                                   
                                                                                       
                                                                                                                                                                                                                                                                          
 



                                                                          
                                                                         
                                                                                                                                
 
                                                                           
                                                                                                                                                                    
                                                                                                                                                                                  


                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
                                                                                                                                                                                                                                                        


                                                                                                                                                                                   
                                                                                                                                                                                                                      
                                                                                                                                                                                                                                                                                                                                                                                        
                                                                                                                                                                                                                                                                                                                                    
                                                                                                                            

                                                                                                                                                                                                                                                                                                                                                                                                                                                    
                                                                                                                                                                                                        
                                    
                                  


                                                                                                                                                                                                                                                                                                                                                                         
                                
                                                                                         
 

                                                                                                      

                                                                                     
                                                                                                                                                                                                                                                                                                                    
                                                                                                                                                                                           
                                                                                                                                                                                                                                                                                                                                                                                                                                                 


                                                                        






                                                                                                                                                                                                                                                                                                                               
                                      
                                                          
                                                                                                                                                                                                                                                                                                         
                                                                     
                                                                                                                                                                                                                                                                                                                                 

                                                                                                                                                                                                                                                                                                                           

                                                                                                                                                                                                                                                                                                                                                                                                                                                     



                                                                               
                                                                                                                                                                                                                                                                                                                          
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  


                                                                                                                                                                                                                                                                                                                                                      
                                  
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 

                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
                                                                                                                                                                                                                                                                                                                                                   





                                                                                                                
                                                       
                                                                                                                                                                                                                                                                               

                                                                                                                           
                                                                                                              
                                                                                                                                                                                                                                                                                                                                   
                                                                                                                                                                                                                        
                                                                                                                      
                                                                                                                                                                         

                                                                                  

                                                                                                                
                                                                                                                                                                                                                                                                                                                                   
                                                                                                                                                                                                                         
                                                                                                 

                                                                                                                                                                                                                                                       
                                                                                                                                                                                                                                                                                                                    

                                                                                                                                                                                                                                                                                                           
                                                                                                                                                          

                                                         

                                    
                                                                                                                                                                

                                

                                                                                                                                                                                     
                                                                                                                                                                                                                                                                                                                                                                          
                                                                                                                                                                                                                                                                                      

                                                                                                               
                                                                                                                                                                                                                                                                                                                                                                       
                                                                                                                                                                                                                                                                                   




                                                                                                               
                                                                                                                                                                                                                                                                                                                                                                                                                         

                                                                                    
                                                                                                                                                                                                                                                                                                

                                


                                                                                                                                                                                                                                                                                                                                                                                                
                                                                                                                                                                                                                                                                                                                                                                                                                                     
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   











                                                                                                                                                                                                                                                                                                                

                                
                                                                                                                                                                                                               
                                                                                                                                                                            

                                


                                                                                                                                                                                                                                                                                                               


                                                                                                                                                                                                                                                                                                                                 





















                                                                                                                                                                                                                                                                                                                                         
                                                                                                                                                                                                                                                                                                                                      
                                                                                                                                                    




                                                                                                                                                                                                                       
                                                                                                                                         
                                                                                           
                                                                                                                                 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                





                                                                                                     
                                                                                             


                                                                                                      


                                                                                                               

                                                                                                                                           
                                                                                                                                                                                                                                                                                                                                                               
                                                                                                                                                                                                                                                                                                                                           
                                                                                                                                                                                                                                                      
                                                                                                                                                                                                                                                                                                                                                                                         
                                                                                                                    
                                                                                                                                                                                                               
                                  
                                                                                                                                                                                                                          
                                                                                                                                                                          


                                                                                           
                                                                                                                                                                       
                                                                                                                                                                                                                                                                                                                                                                            
                                                                                                         
                                                                                                                                                                                                                                                                                                                                          
                                                                                                                                                                                                                                                                                                                                                                                 
                                                                                                                                                                                                                                                                                                                                       
                                                                                                                                                                                                                                                                                                                                                                              
                                                                                                                                                                                                                                                                                                                                                                     
                                                                                                                                                                            
                                                                                                                                                                                                                                                                                                                                                                                                        
                                                                                                                                                                                                                                                                                                                                            
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          
                                                                                                                                                                                                                                                                                                                                            
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        


                                  
                                                                                                                                                                                                                                                                                                                                                                                                                                                           
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     
                                                                                                                                                                                                                                                                                                           
                                                                                                                                                                                                                                                                     
                                                                                                                                                                                                                                                                                                                                                                                                                                                     

                                                                          
                                                                                                                                                                        
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              

                                                                          

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
                                                                                                                                                                                                                      
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               

                                
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 
                                                                                                                                                                                                                                                                                                                           
                                                                                                                                                                                                                                                                                         
                                                                                                                                                                                                                             
                                                                                                                                                                                                                                                                           
                                                                                            
                                                                                                                                                                                                                                                                                                                                                                                                                                     

                                                                                                                                                                                                                                                                                                                                                                      
                                                                                                                                                                                                                                                                                     
                                                                                                                                                                                                                                                                                         
                                                                                                                                                                                                                          
                                                                                                                                                                                                                                                                     
                                                                                                                                                                                                                            




                                                                                                                                                                                                                                                                                                                                                                   
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         

                                


                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
                                                        
                                                                                                                                    

                                                                                                                                                                                                                                                                                                                                                                                        

                                                                          











                                                                                                                                                                                                                                                                                                                                                                                                                                                                              
                                      

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
                                                                                                                                                                                                                                                                                                          
                                      












                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    

                                    
                                                                   
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       
                                  

                                
                                                                                                                                                                                                                                                                                                                                                               
                                                   
                                                                                                                                                                                                                       
                                                                                                                                                                                                                                                                                                                                      
                                                                                                                                                                                                                                                                                                                                             
                                                                                                                                                                                                                                                                                                          
                                                                                                                                                                                                                                                                                                                                   
                                                                                                                                                                                                                                                                                                                                          
                                                                                                                                                                                                                                                                                                       
                                                 
                                                                                                                                                                                                                                                                                                                                                                        
                                                                                                                                                                               
                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
                                                                                                                                                                                                                                                                                                                                         
                                                                                                                                                                                                                                                                                                      
                                                                                                                                                                                                                                                                                                                                      
                                                                                                                                                                                                                                                                                                   
                                                                                                             
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  


                                                                                                                                                                                                                                                                                             
                                                                             



                                    
                                                                                                                                                                                                                                                                                                                                                                                                                                            
                                                                                             
                                                                                                                                                                                                                                                                                                                                                                                  
                                                                                                                                                                        
                                                                                                                                                                                                                                                                                                                                                                                                                                           

                                                                          
                                                                                                                                                                         

                                

                                                                                                                                                                                                                                                                                                                                                        
                                                                                                                                                                               



                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            



                                                                                                                                                                                   



                                                                                                                                                                                                                                                                                               

                                                                                                                                                                                                                                                                                                                                                                                                                                                                 



                                                                                                                                                                                                                                                                                  
                                    

                                                                                                                                                                                                                                                                                                                                                                    
                                  
                                                                   
                                                        
                                  

                                


                                                                                                                                                                                      
                                                                                                                                                                                                                                                                                                                                         
                                                                                                                                                                                                                                                                                                          
                                                                                                                                                                                                                                                                                                                                      










                                                                                                                                                                                                                                                                                                                                                            
                                                                                                                                                                          


                                                                                                       


                                                                               
                                                                                                                                                             




                                                                                                                    

                                                                                                                                                                                                                                             



                                                                                          
                                                                                                                                                                                                                                                                                                                    

                                                                                                                                                                                           
                                                                                                                                                                   
                                    




                                                                                               
                                                                                                                                                                                                                                                                                                                                                                         
                                                                                                                                                                                                                                                                                                                                                                                               
                                                                                                                                                                                                                                                                                                                                             
                                                                                                                                                                                                                                                                
                                                                                                                                                                                                                                                                                                                                          
                                                                                                                                                                                                                                                             
                                                                                                                                                                                                                                                                                                                                                                        
                                                                                                                                                                               
                                                                                                                                                                                                                                                                                                                                         
                                                                                                                                                                                                                                                            
                                                                                                                                                                                                                                                                                                                                      
                                                                                                                                                                                                                                                         


                                  



                                                                                                                                                                                                                                                                                                                                                                                                                                                                     

                                                                          

                                                                                                                                                                                                                                                                                                                                                                                 




                                                                          
                                        



                                                                                                                                                                                   
                                                                                                                                




                                                                                 
                                                                                          







                                                                        
                                        



                                                                                                                                                                                                                                                        
                                                                                              

                  
                                                                                                                        
 
                                              
                                  
                                   



                                                                  
                                                                       
                                               

                                  
 
                                        
                                                                                                             
                                                                                                                                                                                                                                                                                                                  

       
                                                     


                  
                                        
 
                                                                                               
                                        


                                                                                                                                                                                                                                                        
                                                                                              

                  
                                                                                                           
                                                                                                                                                                                              
 
                                              
                                  
                                   



                                                                  
                                                                      
                                        


                                                                 
                                            


                                              
                                                                                                      
                                        

                                                                                                                                     
 
                                              
                                  
                                   
 
                                                                                                     


                                                                           
                                                                                                                                                                                          





                                                                                                        
                                        

                                                                                                                                     
 
                                                                                                     


                                                                           
                                                                                                                                                                                          

                                                          
                                              
                                  
                                   




                                                                 
                                                                                  
                                        

                                                                           
                                                                                                








                                                                                                                                                                       



                                                                                                                                                                     
                                                                                                                                                      








                                                                                                                                                                  
                                                                                                                     


                                                                                                                                                
                                                                                                                                                                                                             




                                                                                                                                                                                                                                              
                                                                                    
                                                                                        


                                                                                                                                                                                                                                                                          

                                                                                                                                                                                               
 
                                                                                          





                                                                 















                                                                                                                                                                                                                                                   

                                                                      
                                        

                                                                                                           
 
                                                                                     


                                             
                                                                                            

                                                                                  
                                        

                                     
                                       
             

                                                                                                                                               
 
                                                                                           






                                                                                                            
                                        

                                                                
                                                                                                                
 
                                                                                           






                                                                                                            
                                        

                                                                   
                                                                                                                                             
 
                                                                                           






                                                                                                            
                                        

                                                                                               
                                                                                                                                                   
 
                                                                                                       








                                                                    
                                        



                                                                                    
                                                                                                       



                                             
                                                                                              

                                                                    
                                        

                                                                                                                                                                              
                                                                                       

                  
                                                                                                                    




                                                                                                  
                                        



                                                                                                                                                                                

                                                                 
                                                                                            

                  
                                                                         
 
                                                                                            

                  
                                                                         

                                                                     
                                                
                                     
 
                                                
                                   
                                     





                                                                                                           
                                        



                                                                                                                                                                                                                                 
 
                                                                                            

                  
                                                                         
 
                                                                                            

                  
                                                                         
 
                                                
                                     
 
                                                
                                   
                                     


                                                                 


                                                                                
                                        
                                                                                                               

       
                                                                                            


                                                         
                                                                         
 
                                                
                                     
 
                                                                                                           
                                                                                 



                                                                                  
                                        
                                                                                                               

       
                                                                                            

                  
                                                                                                                                                                          
                                                                         
 
                                                
                                     
 
                                                                                                                    
                                                                                 



                                                                                     
                                        

                                                                                                                
 
                                                                                         



                                         
                                                                              
                                  
                                          





                                                                          

                                                                                                                                                        
                                                                                                                      
 
                                                                                         

                  
                                                                         
 
                                                                                               

                  
                                                                         

                                                                 


                                                                                                
                                        


                                                                                                                                                                                            

                                                         
                                                                                                                                                                                           

                  
                                                                         

                                                     
                                                                                                                                                                                             

                  
                                                                         




                                                                            
                                        

                                                                                                               
 
                                              
                                                                                                              
 
                                                     
                                                                                                              




                                                                          
                                        
                                                                                                             
 
                                                                                         

                  
                                                                         
 
                                                                                                                    

                  
                                                                         




                                                                         
                                        
                                                                                                            

                                                         
                                                                                       

                  
                                                                         

                                                     
                                                                                               

                  
                                                                         


                                                                                                                       
 
                                                                        
                                        
                                                                                                            

                                                                 
                                                                                             




                                                                         
                                                                                               






                                                                                                                       


                                                                             
                                               

                                                                
                                            
                                   
 
                                        

                                                                                                              
                                                                                                                                                                     
                                                                                                      
                                                                                                            







                                                                                    
                                               

                                                                
                                            
                                   
 
                                        

                                                                                                              
                                                                                                                                                                     
                                                                                                      
                                                                                                                  



                                                                                    


                                                                       
                                               




                                                                                  
                                                    


                                                                               
                                        


                                      

                                       

                                                                                              
                                               




                                                                                  
                                                    


                                                                                                                                                                                          
                                        




                                       

                                                                                        
                                                         

                                                                                                                          
                                        



                                                                                                     


       
                                     
<!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 - 056shape_shifting_recipe.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="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-size: 12pt; font-family: monospace; color: #eeeeee; background-color: #080808; }
* { font-size: 12pt; font-size: 1em; }
.SalientComment { color: #00ffff; }
.CommentedCode { color: #6c6c6c; }
.muRecipe { color: #ff8700; }
.muData { color: #ffff00; }
.traceContains { color: #008000; }
.Delimiter { color: #800080; }
.Normal { color: #eeeeee; background-color: #080808; padding-bottom: 1px; }
.cSpecial { color: #008000; }
.Comment { color: #9090ff; }
.Constant { color: #00a0a0; }
.Special { color: #c00000; }
.Identifier { color: #c0a020; }
.PreProc { color: #800080; }
-->
</style>

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

-->
</script>
</head>
<body>
<pre id='vimCodeElement'>
<span class="SalientComment">//:: Like container definitions, recipes too can contain type parameters.</span>

<span class="Delimiter">:(scenario shape_shifting_recipe)</span>
<span class="muRecipe">def</span> main [
  <span class="Constant">10</span>:point<span class="Special"> &lt;- </span>merge <span class="Constant">14</span><span class="Delimiter">,</span> <span class="Constant">15</span>
  <span class="Constant">11</span>:point<span class="Special"> &lt;- </span>foo <span class="Constant">10</span>:point
]
<span class="Comment"># non-matching variant</span>
<span class="muRecipe">def</span> foo a:num<span class="muRecipe"> -&gt; </span>result:num [
  local-scope
  load-ingredients
  result<span class="Special"> &lt;- </span>copy <span class="Constant">34</span>
]
<span class="Comment"># matching shape-shifting variant</span>
<span class="muRecipe">def</span> foo a:_t<span class="muRecipe"> -&gt; </span>result:_t [
  local-scope
  load-ingredients
  result<span class="Special"> &lt;- </span>copy a
]
<span class="traceContains">+mem: storing 14 in location 11</span>
<span class="traceContains">+mem: storing 15 in location 12</span>

<span class="Comment">//: Before anything else, disable transforms for shape-shifting recipes and</span>
<span class="Comment">//: make sure we never try to actually run a shape-shifting recipe. We should</span>
<span class="Comment">//: be rewriting such instructions to *specializations* with the type</span>
<span class="Comment">//: ingredients filled in.</span>

<span class="Comment">//: One exception (and this makes things very ugly): we need to expand type</span>
<span class="Comment">//: abbreviations in shape-shifting recipes because we need them types for</span>
<span class="Comment">//: deciding which variant to specialize.</span>

<span class="Delimiter">:(before &quot;End Transform Checks&quot;)</span>
r<span class="Delimiter">.</span>transformed_until = t<span class="Delimiter">;</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>Transform<span class="Delimiter">.</span>at<span class="Delimiter">(</span>t<span class="Delimiter">)</span> != <span class="Normal">static_cast</span>&lt;transform_fn&gt;<span class="Delimiter">(</span>expand_type_abbreviations<span class="Delimiter">)</span> &amp;&amp; any_type_ingredient_in_header<span class="Delimiter">(</span><span class="Comment">/*</span><span class="Comment">recipe_ordinal</span><span class="Comment">*/</span>p<span class="Delimiter">-&gt;</span>first<span class="Delimiter">))</span> <span class="Identifier">continue</span><span class="Delimiter">;</span>

<span class="Delimiter">:(after &quot;Running One Instruction&quot;)</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>Current_routine<span class="Delimiter">-&gt;</span>calls<span class="Delimiter">.</span>front<span class="Delimiter">().</span>running_step_index == <span class="Constant">0</span>
    &amp;&amp; any_type_ingredient_in_header<span class="Delimiter">(</span>Current_routine<span class="Delimiter">-&gt;</span>calls<span class="Delimiter">.</span>front<span class="Delimiter">().</span>running_recipe<span class="Delimiter">))</span> <span class="Delimiter">{</span>
<span class="CommentedCode">//?   DUMP(&quot;&quot;);</span>
  raise &lt;&lt; <span class="Constant">&quot;ran into unspecialized shape-shifting recipe &quot;</span> &lt;&lt; current_recipe_name<span class="Delimiter">()</span> &lt;&lt; <span class="cSpecial">'\n'</span> &lt;&lt; end<span class="Delimiter">();</span>
<span class="CommentedCode">//?   exit(0);</span>
<span class="Delimiter">}</span>

<span class="Comment">//: Make sure we don't match up literals with type ingredients without</span>
<span class="Comment">//: specialization.</span>
<span class="Delimiter">:(before &quot;End Matching Types For Literal(to)&quot;)</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>contains_type_ingredient_name<span class="Delimiter">(</span>to<span class="Delimiter">))</span> <span class="Identifier">return</span> <span class="Constant">false</span><span class="Delimiter">;</span>

<span class="Comment">//: save original name of specialized recipes</span>
<span class="Delimiter">:(before &quot;End recipe Fields&quot;)</span>
string original_name<span class="Delimiter">;</span>
<span class="Comment">//: original name is only set during load</span>
<span class="Delimiter">:(before &quot;End Load Recipe Name&quot;)</span>
result<span class="Delimiter">.</span>original_name = result<span class="Delimiter">.</span>name<span class="Delimiter">;</span>

<span class="Delimiter">:(after &quot;Static Dispatch Phase 3&quot;)</span>
candidates = strictly_matching_shape_shifting_variants<span class="Delimiter">(</span>inst<span class="Delimiter">,</span> variants<span class="Delimiter">);</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>!candidates<span class="Delimiter">.</span>empty<span class="Delimiter">())</span> <span class="Delimiter">{</span>
  recipe_ordinal exemplar = best_shape_shifting_variant<span class="Delimiter">(</span>inst<span class="Delimiter">,</span> candidates<span class="Delimiter">);</span>
  trace<span class="Delimiter">(</span><span class="Constant">9992</span><span class="Delimiter">,</span> <span class="Constant">&quot;transform&quot;</span><span class="Delimiter">)</span> &lt;&lt; <span class="Constant">&quot;found variant to specialize: &quot;</span> &lt;&lt; exemplar &lt;&lt; <span class="Constant">' '</span> &lt;&lt; get<span class="Delimiter">(</span>Recipe<span class="Delimiter">,</span> exemplar<span class="Delimiter">).</span>name &lt;&lt; end<span class="Delimiter">();</span>
  recipe_ordinal new_recipe_ordinal = new_variant<span class="Delimiter">(</span>exemplar<span class="Delimiter">,</span> inst<span class="Delimiter">,</span> caller_recipe<span class="Delimiter">);</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>new_recipe_ordinal == <span class="Constant">0</span><span class="Delimiter">)</span> <span class="Identifier">goto</span> skip_shape_shifting_variants<span class="Delimiter">;</span>
  variants<span class="Delimiter">.</span>push_back<span class="Delimiter">(</span>new_recipe_ordinal<span class="Delimiter">);</span>  <span class="Comment">// side-effect</span>
  recipe&amp; variant = get<span class="Delimiter">(</span>Recipe<span class="Delimiter">,</span> new_recipe_ordinal<span class="Delimiter">);</span>
  <span class="Comment">// perform all transforms on the new specialization</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>!variant<span class="Delimiter">.</span>steps<span class="Delimiter">.</span>empty<span class="Delimiter">())</span> <span class="Delimiter">{</span>
    trace<span class="Delimiter">(</span><span class="Constant">9992</span><span class="Delimiter">,</span> <span class="Constant">&quot;transform&quot;</span><span class="Delimiter">)</span> &lt;&lt; <span class="Constant">&quot;transforming new specialization: &quot;</span> &lt;&lt; variant<span class="Delimiter">.</span>name &lt;&lt; end<span class="Delimiter">();</span>
    <span class="Normal">for</span> <span class="Delimiter">(</span><span class="Normal">int</span> t = <span class="Constant">0</span><span class="Delimiter">;</span>  t &lt; SIZE<span class="Delimiter">(</span>Transform<span class="Delimiter">);</span>  ++t<span class="Delimiter">)</span> <span class="Delimiter">{</span>
      <span class="Comment">// one exception: skip tangle, which would have already occurred inside new_variant above</span>
      <span class="Normal">if</span> <span class="Delimiter">(</span>Transform<span class="Delimiter">.</span>at<span class="Delimiter">(</span>t<span class="Delimiter">)</span> == <span class="Comment">/*</span><span class="Comment">disambiguate overloading</span><span class="Comment">*/</span><span class="Normal">static_cast</span>&lt;transform_fn&gt;<span class="Delimiter">(</span>insert_fragments<span class="Delimiter">))</span>
        <span class="Identifier">continue</span><span class="Delimiter">;</span>
      <span class="Delimiter">(</span>*Transform<span class="Delimiter">.</span>at<span class="Delimiter">(</span>t<span class="Delimiter">))(</span>new_recipe_ordinal<span class="Delimiter">);</span>
    <span class="Delimiter">}</span>
  <span class="Delimiter">}</span>
  variant<span class="Delimiter">.</span>transformed_until = SIZE<span class="Delimiter">(</span>Transform<span class="Delimiter">)</span>-<span class="Constant">1</span><span class="Delimiter">;</span>
  trace<span class="Delimiter">(</span><span class="Constant">9992</span><span class="Delimiter">,</span> <span class="Constant">&quot;transform&quot;</span><span class="Delimiter">)</span> &lt;&lt; <span class="Constant">&quot;new specialization: &quot;</span> &lt;&lt; variant<span class="Delimiter">.</span>name &lt;&lt; end<span class="Delimiter">();</span>
  <span class="Identifier">return</span> variant<span class="Delimiter">.</span>name<span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Normal">skip_shape_shifting_variants</span>:<span class="Delimiter">;</span>

<span class="Comment">//: before running Mu programs, make sure no unspecialized shape-shifting</span>
<span class="Comment">//: recipes can be called</span>

<span class="Delimiter">:(before &quot;End Instruction Operation Checks&quot;)</span>
<span class="Normal">if</span> <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> &amp;&amp; inst<span class="Delimiter">.</span>operation &gt;= MAX_PRIMITIVE_RECIPES
    &amp;&amp; any_type_ingredient_in_header<span class="Delimiter">(</span>inst<span class="Delimiter">.</span>operation<span class="Delimiter">))</span> <span class="Delimiter">{</span>
  raise &lt;&lt; maybe<span class="Delimiter">(</span>caller<span class="Delimiter">.</span>name<span class="Delimiter">)</span> &lt;&lt; <span class="Constant">&quot;instruction '&quot;</span> &lt;&lt; inst<span class="Delimiter">.</span>name &lt;&lt; <span class="Constant">&quot;' has no valid specialization</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="Delimiter">;</span>
<span class="Delimiter">}</span>

<span class="Delimiter">:(replace{} &quot;bool types_strictly_match_except_literal_zero_against_address(const reagent&amp; to, const reagent&amp; from)&quot;)</span>
<span class="Normal">bool</span> types_strictly_match_except_literal_zero_against_address<span class="Delimiter">(</span><span class="Normal">const</span> reagent&amp; to<span class="Delimiter">,</span> <span class="Normal">const</span> reagent&amp; from<span class="Delimiter">)</span> <span class="Delimiter">{</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>is_literal<span class="Delimiter">(</span>from<span class="Delimiter">)</span> &amp;&amp; is_mu_address<span class="Delimiter">(</span>to<span class="Delimiter">))</span>
    <span class="Identifier">return</span> from<span class="Delimiter">.</span>name == <span class="Constant">&quot;0&quot;</span> &amp;&amp; !contains_type_ingredient_name<span class="Delimiter">(</span>to<span class="Delimiter">);</span>
  <span class="Identifier">return</span> types_strictly_match<span class="Delimiter">(</span>to<span class="Delimiter">,</span> from<span class="Delimiter">);</span>
<span class="Delimiter">}</span>

<span class="Delimiter">:(code)</span>
<span class="Comment">// phase 3 of static dispatch</span>
vector&lt;recipe_ordinal&gt; strictly_matching_shape_shifting_variants<span class="Delimiter">(</span><span class="Normal">const</span> instruction&amp; inst<span class="Delimiter">,</span> vector&lt;recipe_ordinal&gt;&amp; variants<span class="Delimiter">)</span> <span class="Delimiter">{</span>
  vector&lt;recipe_ordinal&gt; result<span class="Delimiter">;</span>
  <span class="Normal">for</span> <span class="Delimiter">(</span><span class="Normal">int</span> i = <span class="Constant">0</span><span class="Delimiter">;</span>  i &lt; SIZE<span class="Delimiter">(</span>variants<span class="Delimiter">);</span>  ++i<span class="Delimiter">)</span> <span class="Delimiter">{</span>
    <span class="Normal">if</span> <span class="Delimiter">(</span>variants<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">)</span> == -<span class="Constant">1</span><span class="Delimiter">)</span> <span class="Identifier">continue</span><span class="Delimiter">;</span>
    <span class="Normal">if</span> <span class="Delimiter">(</span>!any_type_ingredient_in_header<span class="Delimiter">(</span>variants<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">)))</span> <span class="Identifier">continue</span><span class="Delimiter">;</span>
    <span class="Normal">if</span> <span class="Delimiter">(</span>!all_concrete_header_reagents_strictly_match<span class="Delimiter">(</span>inst<span class="Delimiter">,</span> get<span class="Delimiter">(</span>Recipe<span class="Delimiter">,</span> variants<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">))))</span> <span class="Identifier">continue</span><span class="Delimiter">;</span>
    result<span class="Delimiter">.</span>push_back<span class="Delimiter">(</span>variants<span class="Delimiter">.</span>at<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="Normal">bool</span> all_concrete_header_reagents_strictly_match<span class="Delimiter">(</span><span class="Normal">const</span> instruction&amp; inst<span class="Delimiter">,</span> <span class="Normal">const</span> recipe&amp; variant<span class="Delimiter">)</span> <span class="Delimiter">{</span>
  <span class="Normal">for</span> <span class="Delimiter">(</span><span class="Normal">int</span> i = <span class="Constant">0</span><span class="Delimiter">;</span>  i &lt; min<span class="Delimiter">(</span>SIZE<span class="Delimiter">(</span>inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">),</span> SIZE<span class="Delimiter">(</span>variant<span class="Delimiter">.</span>ingredients<span class="Delimiter">));</span>  ++i<span class="Delimiter">)</span> <span class="Delimiter">{</span>
    <span class="Normal">if</span> <span class="Delimiter">(</span>!concrete_type_names_strictly_match<span class="Delimiter">(</span>variant<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<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="Delimiter">{</span>
      trace<span class="Delimiter">(</span><span class="Constant">9993</span><span class="Delimiter">,</span> <span class="Constant">&quot;transform&quot;</span><span class="Delimiter">)</span> &lt;&lt; <span class="Constant">&quot;concrete-type match failed: ingredient &quot;</span> &lt;&lt; i &lt;&lt; end<span class="Delimiter">();</span>
      <span class="Identifier">return</span> <span class="Constant">false</span><span class="Delimiter">;</span>
    <span class="Delimiter">}</span>
  <span class="Delimiter">}</span>
  <span class="Normal">for</span> <span class="Delimiter">(</span><span class="Normal">int</span> i = <span class="Constant">0</span><span class="Delimiter">;</span>  i &lt; min<span class="Delimiter">(</span>SIZE<span class="Delimiter">(</span>inst<span class="Delimiter">.</span>products<span class="Delimiter">),</span> SIZE<span class="Delimiter">(</span>variant<span class="Delimiter">.</span>ingredients<span class="Delimiter">));</span>  ++i<span class="Delimiter">)</span> <span class="Delimiter">{</span>
    <span class="Normal">if</span> <span class="Delimiter">(</span>is_dummy<span class="Delimiter">(</span>inst<span class="Delimiter">.</span>products<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">)))</span> <span class="Identifier">continue</span><span class="Delimiter">;</span>
    <span class="Normal">if</span> <span class="Delimiter">(</span>!concrete_type_names_strictly_match<span class="Delimiter">(</span>variant<span class="Delimiter">.</span>products<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">),</span> inst<span class="Delimiter">.</span>products<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">)))</span> <span class="Delimiter">{</span>
      trace<span class="Delimiter">(</span><span class="Constant">9993</span><span class="Delimiter">,</span> <span class="Constant">&quot;transform&quot;</span><span class="Delimiter">)</span> &lt;&lt; <span class="Constant">&quot;concrete-type match failed: product &quot;</span> &lt;&lt; i &lt;&lt; end<span class="Delimiter">();</span>
      <span class="Identifier">return</span> <span class="Constant">false</span><span class="Delimiter">;</span>
    <span class="Delimiter">}</span>
  <span class="Delimiter">}</span>
  <span class="Identifier">return</span> <span class="Constant">true</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>

<span class="Comment">// tie-breaker for phase 3</span>
recipe_ordinal best_shape_shifting_variant<span class="Delimiter">(</span><span class="Normal">const</span> instruction&amp; inst<span class="Delimiter">,</span> vector&lt;recipe_ordinal&gt;&amp; candidates<span class="Delimiter">)</span> <span class="Delimiter">{</span>
  assert<span class="Delimiter">(</span>!candidates<span class="Delimiter">.</span>empty<span class="Delimiter">());</span>
  <span class="Comment">// primary score</span>
  <span class="Normal">int</span> max_score = -<span class="Constant">1</span><span class="Delimiter">;</span>
  <span class="Normal">for</span> <span class="Delimiter">(</span><span class="Normal">int</span> i = <span class="Constant">0</span><span class="Delimiter">;</span>  i &lt; SIZE<span class="Delimiter">(</span>candidates<span class="Delimiter">);</span>  ++i<span class="Delimiter">)</span> <span class="Delimiter">{</span>
    <span class="Normal">int</span> score = number_of_concrete_type_names<span class="Delimiter">(</span>candidates<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">));</span>
    assert<span class="Delimiter">(</span>score &gt; -<span class="Constant">1</span><span class="Delimiter">);</span>
    <span class="Normal">if</span> <span class="Delimiter">(</span>score &gt; max_score<span class="Delimiter">)</span> max_score = score<span class="Delimiter">;</span>
  <span class="Delimiter">}</span>
  <span class="Comment">// break any ties at max_score by a secondary score</span>
  <span class="Normal">int</span> min_score2 = <span class="Constant">999</span><span class="Delimiter">;</span>
  <span class="Normal">int</span> best_index = <span class="Constant">0</span><span class="Delimiter">;</span>
  <span class="Normal">for</span> <span class="Delimiter">(</span><span class="Normal">int</span> i = <span class="Constant">0</span><span class="Delimiter">;</span>  i &lt; SIZE<span class="Delimiter">(</span>candidates<span class="Delimiter">);</span>  ++i<span class="Delimiter">)</span> <span class="Delimiter">{</span>
    <span class="Normal">int</span> score1 = number_of_concrete_type_names<span class="Delimiter">(</span>candidates<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">));</span>
    assert<span class="Delimiter">(</span>score1 &lt;= max_score<span class="Delimiter">);</span>
    <span class="Normal">if</span> <span class="Delimiter">(</span>score1 != max_score<span class="Delimiter">)</span> <span class="Identifier">continue</span><span class="Delimiter">;</span>
    <span class="Normal">const</span> recipe&amp; candidate = get<span class="Delimiter">(</span>Recipe<span class="Delimiter">,</span> candidates<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">));</span>
    <span class="Normal">int</span> score2 = <span class="Delimiter">(</span>SIZE<span class="Delimiter">(</span>candidate<span class="Delimiter">.</span>products<span class="Delimiter">)</span>-SIZE<span class="Delimiter">(</span>inst<span class="Delimiter">.</span>products<span class="Delimiter">))</span>
                           + <span class="Delimiter">(</span>SIZE<span class="Delimiter">(</span>inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">)</span>-SIZE<span class="Delimiter">(</span>candidate<span class="Delimiter">.</span>ingredients<span class="Delimiter">));</span>
    assert<span class="Delimiter">(</span>score2 &lt; <span class="Constant">999</span><span class="Delimiter">);</span>
    <span class="Normal">if</span> <span class="Delimiter">(</span>score2 &lt; min_score2<span class="Delimiter">)</span> <span class="Delimiter">{</span>
      min_score2 = score2<span class="Delimiter">;</span>
      best_index = i<span class="Delimiter">;</span>
    <span class="Delimiter">}</span>
  <span class="Delimiter">}</span>
  <span class="Identifier">return</span> candidates<span class="Delimiter">.</span>at<span class="Delimiter">(</span>best_index<span class="Delimiter">);</span>
<span class="Delimiter">}</span>

<span class="Normal">bool</span> any_type_ingredient_in_header<span class="Delimiter">(</span>recipe_ordinal variant<span class="Delimiter">)</span> <span class="Delimiter">{</span>
  <span class="Normal">const</span> recipe&amp; caller = get<span class="Delimiter">(</span>Recipe<span class="Delimiter">,</span> variant<span class="Delimiter">);</span>
  <span class="Normal">for</span> <span class="Delimiter">(</span><span class="Normal">int</span> 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>
    <span class="Normal">if</span> <span class="Delimiter">(</span>contains_type_ingredient_name<span class="Delimiter">(</span>caller<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">)))</span>
      <span class="Identifier">return</span> <span class="Constant">true</span><span class="Delimiter">;</span>
  <span class="Delimiter">}</span>
  <span class="Normal">for</span> <span class="Delimiter">(</span><span class="Normal">int</span> i = <span class="Constant">0</span><span class="Delimiter">;</span>  i &lt; SIZE<span class="Delimiter">(</span>caller<span class="Delimiter">.</span>products<span class="Delimiter">);</span>  ++i<span class="Delimiter">)</span> <span class="Delimiter">{</span>
    <span class="Normal">if</span> <span class="Delimiter">(</span>contains_type_ingredient_name<span class="Delimiter">(</span>caller<span class="Delimiter">.</span>products<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<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>

<span class="Normal">bool</span> concrete_type_names_strictly_match<span class="Delimiter">(</span>reagent<span class="Comment">/*</span><span class="Comment">copy</span><span class="Comment">*/</span> to<span class="Delimiter">,</span> reagent<span class="Comment">/*</span><span class="Comment">copy</span><span class="Comment">*/</span> from<span class="Delimiter">)</span> <span class="Delimiter">{</span>
  canonize_type<span class="Delimiter">(</span>to<span class="Delimiter">);</span>
  canonize_type<span class="Delimiter">(</span>from<span class="Delimiter">);</span>
  <span class="Identifier">return</span> concrete_type_names_strictly_match<span class="Delimiter">(</span>to<span class="Delimiter">.</span>type<span class="Delimiter">,</span> from<span class="Delimiter">.</span>type<span class="Delimiter">,</span> from<span class="Delimiter">);</span>
<span class="Delimiter">}</span>

<span class="Normal">bool</span> concrete_type_names_strictly_match<span class="Delimiter">(</span><span class="Normal">const</span> type_tree* to<span class="Delimiter">,</span> <span class="Normal">const</span> type_tree* from<span class="Delimiter">,</span> <span class="Normal">const</span> reagent&amp; rhs_reagent<span class="Delimiter">)</span> <span class="Delimiter">{</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>!to<span class="Delimiter">)</span> <span class="Identifier">return</span> !from<span class="Delimiter">;</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>!from<span class="Delimiter">)</span> <span class="Identifier">return</span> !to<span class="Delimiter">;</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>to<span class="Delimiter">-&gt;</span>atom &amp;&amp; is_type_ingredient_name<span class="Delimiter">(</span>to<span class="Delimiter">-&gt;</span>name<span class="Delimiter">))</span> <span class="Identifier">return</span> <span class="Constant">true</span><span class="Delimiter">;</span>  <span class="Comment">// type ingredient matches anything</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>!to<span class="Delimiter">-&gt;</span>atom &amp;&amp; to<span class="Delimiter">-&gt;</span>right == <span class="Constant">NULL</span> &amp;&amp; to<span class="Delimiter">-&gt;</span>left != <span class="Constant">NULL</span> &amp;&amp; to<span class="Delimiter">-&gt;</span>left<span class="Delimiter">-&gt;</span>atom &amp;&amp; is_type_ingredient_name<span class="Delimiter">(</span>to<span class="Delimiter">-&gt;</span>left<span class="Delimiter">-&gt;</span>name<span class="Delimiter">))</span> <span class="Identifier">return</span> <span class="Constant">true</span><span class="Delimiter">;</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>from<span class="Delimiter">-&gt;</span>atom &amp;&amp; is_mu_address<span class="Delimiter">(</span>to<span class="Delimiter">))</span>
    <span class="Identifier">return</span> from<span class="Delimiter">-&gt;</span>name == <span class="Constant">&quot;literal&quot;</span> &amp;&amp; rhs_reagent<span class="Delimiter">.</span>name == <span class="Constant">&quot;0&quot;</span><span class="Delimiter">;</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>!from<span class="Delimiter">-&gt;</span>atom &amp;&amp; !to<span class="Delimiter">-&gt;</span>atom<span class="Delimiter">)</span>
    <span class="Identifier">return</span> concrete_type_names_strictly_match<span class="Delimiter">(</span>to<span class="Delimiter">-&gt;</span>left<span class="Delimiter">,</span> from<span class="Delimiter">-&gt;</span>left<span class="Delimiter">,</span> rhs_reagent<span class="Delimiter">)</span>
        &amp;&amp; concrete_type_names_strictly_match<span class="Delimiter">(</span>to<span class="Delimiter">-&gt;</span>right<span class="Delimiter">,</span> from<span class="Delimiter">-&gt;</span>right<span class="Delimiter">,</span> rhs_reagent<span class="Delimiter">);</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>from<span class="Delimiter">-&gt;</span>atom != to<span class="Delimiter">-&gt;</span>atom<span class="Delimiter">)</span> <span class="Identifier">return</span> <span class="Constant">false</span><span class="Delimiter">;</span>
  <span class="Comment">// both from and to are atoms</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>from<span class="Delimiter">-&gt;</span>name == <span class="Constant">&quot;literal&quot;</span><span class="Delimiter">)</span>
    <span class="Identifier">return</span> Literal_type_names<span class="Delimiter">.</span>find<span class="Delimiter">(</span>to<span class="Delimiter">-&gt;</span>name<span class="Delimiter">)</span> != Literal_type_names<span class="Delimiter">.</span>end<span class="Delimiter">();</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>to<span class="Delimiter">-&gt;</span>name == <span class="Constant">&quot;literal&quot;</span><span class="Delimiter">)</span>
    <span class="Identifier">return</span> Literal_type_names<span class="Delimiter">.</span>find<span class="Delimiter">(</span>from<span class="Delimiter">-&gt;</span>name<span class="Delimiter">)</span> != Literal_type_names<span class="Delimiter">.</span>end<span class="Delimiter">();</span>
  <span class="Identifier">return</span> to<span class="Delimiter">-&gt;</span>name == from<span class="Delimiter">-&gt;</span>name<span class="Delimiter">;</span>
<span class="Delimiter">}</span>

<span class="Normal">bool</span> contains_type_ingredient_name<span class="Delimiter">(</span><span class="Normal">const</span> reagent&amp; x<span class="Delimiter">)</span> <span class="Delimiter">{</span>
  <span class="Identifier">return</span> contains_type_ingredient_name<span class="Delimiter">(</span>x<span class="Delimiter">.</span>type<span class="Delimiter">);</span>
<span class="Delimiter">}</span>

<span class="Normal">bool</span> contains_type_ingredient_name<span class="Delimiter">(</span><span class="Normal">const</span> type_tree* type<span class="Delimiter">)</span> <span class="Delimiter">{</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>!type<span class="Delimiter">)</span> <span class="Identifier">return</span> <span class="Constant">false</span><span class="Delimiter">;</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>is_type_ingredient_name<span class="Delimiter">(</span>type<span class="Delimiter">-&gt;</span>name<span class="Delimiter">))</span> <span class="Identifier">return</span> <span class="Constant">true</span><span class="Delimiter">;</span>
  <span class="Identifier">return</span> contains_type_ingredient_name<span class="Delimiter">(</span>type<span class="Delimiter">-&gt;</span>left<span class="Delimiter">)</span> || contains_type_ingredient_name<span class="Delimiter">(</span>type<span class="Delimiter">-&gt;</span>right<span class="Delimiter">);</span>
<span class="Delimiter">}</span>

<span class="Normal">int</span> number_of_concrete_type_names<span class="Delimiter">(</span>recipe_ordinal r<span class="Delimiter">)</span> <span class="Delimiter">{</span>
  <span class="Normal">const</span> recipe&amp; caller = get<span class="Delimiter">(</span>Recipe<span class="Delimiter">,</span> r<span class="Delimiter">);</span>
  <span class="Normal">int</span> result = <span class="Constant">0</span><span class="Delimiter">;</span>
  <span class="Normal">for</span> <span class="Delimiter">(</span><span class="Normal">int</span> 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>
    result += number_of_concrete_type_names<span class="Delimiter">(</span>caller<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">));</span>
  <span class="Normal">for</span> <span class="Delimiter">(</span><span class="Normal">int</span> i = <span class="Constant">0</span><span class="Delimiter">;</span>  i &lt; SIZE<span class="Delimiter">(</span>caller<span class="Delimiter">.</span>products<span class="Delimiter">);</span>  ++i<span class="Delimiter">)</span>
    result += number_of_concrete_type_names<span class="Delimiter">(</span>caller<span class="Delimiter">.</span>products<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">));</span>
  <span class="Identifier">return</span> result<span class="Delimiter">;</span>
<span class="Delimiter">}</span>

<span class="Normal">int</span> number_of_concrete_type_names<span class="Delimiter">(</span><span class="Normal">const</span> reagent&amp; r<span class="Delimiter">)</span> <span class="Delimiter">{</span>
  <span class="Identifier">return</span> number_of_concrete_type_names<span class="Delimiter">(</span>r<span class="Delimiter">.</span>type<span class="Delimiter">);</span>
<span class="Delimiter">}</span>

<span class="Normal">int</span> number_of_concrete_type_names<span class="Delimiter">(</span><span class="Normal">const</span> type_tree* type<span class="Delimiter">)</span> <span class="Delimiter">{</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>!type<span class="Delimiter">)</span> <span class="Identifier">return</span> <span class="Constant">0</span><span class="Delimiter">;</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>type<span class="Delimiter">-&gt;</span>atom<span class="Delimiter">)</span>
    <span class="Identifier">return</span> is_type_ingredient_name<span class="Delimiter">(</span>type<span class="Delimiter">-&gt;</span>name<span class="Delimiter">)</span> ? <span class="Constant">0</span> : <span class="Constant">1</span><span class="Delimiter">;</span>
  <span class="Identifier">return</span> number_of_concrete_type_names<span class="Delimiter">(</span>type<span class="Delimiter">-&gt;</span>left<span class="Delimiter">)</span>
       + number_of_concrete_type_names<span class="Delimiter">(</span>type<span class="Delimiter">-&gt;</span>right<span class="Delimiter">);</span>
<span class="Delimiter">}</span>

recipe_ordinal new_variant<span class="Delimiter">(</span>recipe_ordinal exemplar<span class="Delimiter">,</span> <span class="Normal">const</span> instruction&amp; inst<span class="Delimiter">,</span> <span class="Normal">const</span> recipe&amp; caller_recipe<span class="Delimiter">)</span> <span class="Delimiter">{</span>
  string new_name = next_unused_recipe_name<span class="Delimiter">(</span>inst<span class="Delimiter">.</span>name<span class="Delimiter">);</span>
  assert<span class="Delimiter">(</span>!contains_key<span class="Delimiter">(</span>Recipe_ordinal<span class="Delimiter">,</span> new_name<span class="Delimiter">));</span>
  recipe_ordinal new_recipe_ordinal = put<span class="Delimiter">(</span>Recipe_ordinal<span class="Delimiter">,</span> new_name<span class="Delimiter">,</span> Next_recipe_ordinal++<span class="Delimiter">);</span>
  <span class="Comment">// make a copy</span>
  assert<span class="Delimiter">(</span>contains_key<span class="Delimiter">(</span>Recipe<span class="Delimiter">,</span> exemplar<span class="Delimiter">));</span>
  assert<span class="Delimiter">(</span>!contains_key<span class="Delimiter">(</span>Recipe<span class="Delimiter">,</span> new_recipe_ordinal<span class="Delimiter">));</span>
  recipe new_recipe = get<span class="Delimiter">(</span>Recipe<span class="Delimiter">,</span> exemplar<span class="Delimiter">);</span>
  new_recipe<span class="Delimiter">.</span>name = new_name<span class="Delimiter">;</span>
  new_recipe<span class="Delimiter">.</span>is_autogenerated = <span class="Constant">true</span><span class="Delimiter">;</span>
  trace<span class="Delimiter">(</span><span class="Constant">9993</span><span class="Delimiter">,</span> <span class="Constant">&quot;transform&quot;</span><span class="Delimiter">)</span> &lt;&lt; <span class="Constant">&quot;switching &quot;</span> &lt;&lt; inst<span class="Delimiter">.</span>name &lt;&lt; <span class="Constant">&quot; to specialized &quot;</span> &lt;&lt; header_label<span class="Delimiter">(</span>new_recipe<span class="Delimiter">)</span> &lt;&lt; end<span class="Delimiter">();</span>

  <span class="Comment">// Replace type ingredients with concrete types in new_recipe.</span>
  <span class="Comment">//</span>
  <span class="Comment">// preprocessing: micro-manage a couple of transforms</span>
  <span class="Comment">// a) perform tangle *before* replacing type ingredients, just in case</span>
  <span class="Comment">// inserted code involves type ingredients</span>
  insert_fragments<span class="Delimiter">(</span>new_recipe<span class="Delimiter">);</span>
  <span class="Comment">// b) do the work of check_or_set_types_by_name (and its prerequisites)</span>
  <span class="Comment">// while supporting type-ingredients</span>
  expand_type_abbreviations<span class="Delimiter">(</span>new_recipe<span class="Delimiter">);</span>
  compute_type_names<span class="Delimiter">(</span>new_recipe<span class="Delimiter">);</span>
  <span class="Comment">// that gives enough information to replace type-ingredients with concrete types</span>
  <span class="Delimiter">{</span>
    map&lt;string<span class="Delimiter">,</span> <span class="Normal">const</span> type_tree*&gt; mappings<span class="Delimiter">;</span>
    <span class="Normal">bool</span> error = <span class="Constant">false</span><span class="Delimiter">;</span>
    compute_type_ingredient_mappings<span class="Delimiter">(</span>get<span class="Delimiter">(</span>Recipe<span class="Delimiter">,</span> exemplar<span class="Delimiter">),</span> inst<span class="Delimiter">,</span> mappings<span class="Delimiter">,</span> caller_recipe<span class="Delimiter">,</span> &amp;error<span class="Delimiter">);</span>
    <span class="Normal">if</span> <span class="Delimiter">(</span>!error<span class="Delimiter">)</span> error = <span class="Delimiter">(</span>SIZE<span class="Delimiter">(</span>mappings<span class="Delimiter">)</span> != type_ingredient_count_in_header<span class="Delimiter">(</span>exemplar<span class="Delimiter">));</span>
    <span class="Normal">if</span> <span class="Delimiter">(</span>!error<span class="Delimiter">)</span> replace_type_ingredients<span class="Delimiter">(</span>new_recipe<span class="Delimiter">,</span> mappings<span class="Delimiter">);</span>
    <span class="Normal">for</span> <span class="Delimiter">(</span>map&lt;string<span class="Delimiter">,</span> <span class="Normal">const</span> type_tree*&gt;::iterator p = mappings<span class="Delimiter">.</span>begin<span class="Delimiter">();</span>  p != mappings<span class="Delimiter">.</span>end<span class="Delimiter">();</span>  ++p<span class="Delimiter">)</span>
      <span class="Normal">delete</span> p<span class="Delimiter">-&gt;</span>second<span class="Delimiter">;</span>
    <span class="Normal">if</span> <span class="Delimiter">(</span>error<span class="Delimiter">)</span> <span class="Identifier">return</span> <span class="Constant">0</span><span class="Delimiter">;</span>
  <span class="Delimiter">}</span>
  ensure_all_concrete_types<span class="Delimiter">(</span>new_recipe<span class="Delimiter">,</span> get<span class="Delimiter">(</span>Recipe<span class="Delimiter">,</span> exemplar<span class="Delimiter">));</span>
  put<span class="Delimiter">(</span>Recipe<span class="Delimiter">,</span> new_recipe_ordinal<span class="Delimiter">,</span> new_recipe<span class="Delimiter">);</span>
  <span class="Identifier">return</span> new_recipe_ordinal<span class="Delimiter">;</span>
<span class="Delimiter">}</span>

<span class="Normal">void</span> compute_type_names<span class="Delimiter">(</span>recipe&amp; variant<span class="Delimiter">)</span> <span class="Delimiter">{</span>
  trace<span class="Delimiter">(</span><span class="Constant">9993</span><span class="Delimiter">,</span> <span class="Constant">&quot;transform&quot;</span><span class="Delimiter">)</span> &lt;&lt; <span class="Constant">&quot;-- compute type names: &quot;</span> &lt;&lt; variant<span class="Delimiter">.</span>name &lt;&lt; end<span class="Delimiter">();</span>
  map&lt;string<span class="Delimiter">,</span> type_tree*&gt; type_names<span class="Delimiter">;</span>
  <span class="Normal">for</span> <span class="Delimiter">(</span><span class="Normal">int</span> i = <span class="Constant">0</span><span class="Delimiter">;</span>  i &lt; SIZE<span class="Delimiter">(</span>variant<span class="Delimiter">.</span>ingredients<span class="Delimiter">);</span>  ++i<span class="Delimiter">)</span>
    save_or_deduce_type_name<span class="Delimiter">(</span>variant<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">),</span> type_names<span class="Delimiter">,</span> variant<span class="Delimiter">,</span> <span class="Constant">&quot;&quot;</span><span class="Delimiter">);</span>
  <span class="Normal">for</span> <span class="Delimiter">(</span><span class="Normal">int</span> i = <span class="Constant">0</span><span class="Delimiter">;</span>  i &lt; SIZE<span class="Delimiter">(</span>variant<span class="Delimiter">.</span>products<span class="Delimiter">);</span>  ++i<span class="Delimiter">)</span>
    save_or_deduce_type_name<span class="Delimiter">(</span>variant<span class="Delimiter">.</span>products<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">),</span> type_names<span class="Delimiter">,</span> variant<span class="Delimiter">,</span> <span class="Constant">&quot;&quot;</span><span class="Delimiter">);</span>
  <span class="Normal">for</span> <span class="Delimiter">(</span><span class="Normal">int</span> i = <span class="Constant">0</span><span class="Delimiter">;</span>  i &lt; SIZE<span class="Delimiter">(</span>variant<span class="Delimiter">.</span>steps<span class="Delimiter">);</span>  ++i<span class="Delimiter">)</span> <span class="Delimiter">{</span>
    instruction&amp; inst = variant<span class="Delimiter">.</span>steps<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">);</span>
    trace<span class="Delimiter">(</span><span class="Constant">9993</span><span class="Delimiter">,</span> <span class="Constant">&quot;transform&quot;</span><span class="Delimiter">)</span> &lt;&lt; <span class="Constant">&quot;  instruction: &quot;</span> &lt;&lt; to_string<span class="Delimiter">(</span>inst<span class="Delimiter">)</span> &lt;&lt; end<span class="Delimiter">();</span>
    <span class="Normal">for</span> <span class="Delimiter">(</span><span class="Normal">int</span> in = <span class="Constant">0</span><span class="Delimiter">;</span>  in &lt; SIZE<span class="Delimiter">(</span>inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">);</span>  ++in<span class="Delimiter">)</span>
      save_or_deduce_type_name<span class="Delimiter">(</span>inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span>in<span class="Delimiter">),</span> type_names<span class="Delimiter">,</span> variant<span class="Delimiter">,</span> <span class="Constant">&quot; in '&quot;</span> + inst<span class="Delimiter">.</span>original_string + <span class="Constant">&quot;'&quot;</span><span class="Delimiter">);</span>
    <span class="Normal">for</span> <span class="Delimiter">(</span><span class="Normal">int</span> out = <span class="Constant">0</span><span class="Delimiter">;</span>  out &lt; SIZE<span class="Delimiter">(</span>inst<span class="Delimiter">.</span>products<span class="Delimiter">);</span>  ++out<span class="Delimiter">)</span>
      save_or_deduce_type_name<span class="Delimiter">(</span>inst<span class="Delimiter">.</span>products<span class="Delimiter">.</span>at<span class="Delimiter">(</span>out<span class="Delimiter">),</span> type_names<span class="Delimiter">,</span> variant<span class="Delimiter">,</span> <span class="Constant">&quot; in '&quot;</span> + inst<span class="Delimiter">.</span>original_string + <span class="Constant">&quot;'&quot;</span><span class="Delimiter">);</span>
  <span class="Delimiter">}</span>
<span class="Delimiter">}</span>

<span class="Normal">void</span> save_or_deduce_type_name<span class="Delimiter">(</span>reagent&amp; x<span class="Delimiter">,</span> map&lt;string<span class="Delimiter">,</span> type_tree*&gt;&amp; type<span class="Delimiter">,</span> <span class="Normal">const</span> recipe&amp; variant<span class="Delimiter">,</span> <span class="Normal">const</span> string&amp; context<span class="Delimiter">)</span> <span class="Delimiter">{</span>
  trace<span class="Delimiter">(</span><span class="Constant">9994</span><span class="Delimiter">,</span> <span class="Constant">&quot;transform&quot;</span><span class="Delimiter">)</span> &lt;&lt; <span class="Constant">&quot;    checking &quot;</span> &lt;&lt; to_string<span class="Delimiter">(</span>x<span class="Delimiter">)</span> &lt;&lt; <span class="Constant">&quot;: &quot;</span> &lt;&lt; names_to_string<span class="Delimiter">(</span>x<span class="Delimiter">.</span>type<span class="Delimiter">)</span> &lt;&lt; end<span class="Delimiter">();</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>!x<span class="Delimiter">.</span>type &amp;&amp; contains_key<span class="Delimiter">(</span>type<span class="Delimiter">,</span> x<span class="Delimiter">.</span>name<span class="Delimiter">))</span> <span class="Delimiter">{</span>
    x<span class="Delimiter">.</span>type = <span class="Normal">new</span> type_tree<span class="Delimiter">(</span>*get<span class="Delimiter">(</span>type<span class="Delimiter">,</span> x<span class="Delimiter">.</span>name<span class="Delimiter">));</span>
    trace<span class="Delimiter">(</span><span class="Constant">9994</span><span class="Delimiter">,</span> <span class="Constant">&quot;transform&quot;</span><span class="Delimiter">)</span> &lt;&lt; <span class="Constant">&quot;    deducing type to &quot;</span> &lt;&lt; names_to_string<span class="Delimiter">(</span>x<span class="Delimiter">.</span>type<span class="Delimiter">)</span> &lt;&lt; end<span class="Delimiter">();</span>
    <span class="Identifier">return</span><span class="Delimiter">;</span>
  <span class="Delimiter">}</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>!x<span class="Delimiter">.</span>type<span class="Delimiter">)</span> <span class="Delimiter">{</span>
    raise &lt;&lt; maybe<span class="Delimiter">(</span>variant<span class="Delimiter">.</span>original_name<span class="Delimiter">)</span> &lt;&lt; <span class="Constant">&quot;unknown type for '&quot;</span> &lt;&lt; x<span class="Delimiter">.</span>original_string &lt;&lt; <span class="Constant">&quot;'&quot;</span> &lt;&lt; context &lt;&lt; <span class="Constant">&quot; (check the name for typos)</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="Delimiter">;</span>
  <span class="Delimiter">}</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>contains_key<span class="Delimiter">(</span>type<span class="Delimiter">,</span> x<span class="Delimiter">.</span>name<span class="Delimiter">))</span> <span class="Identifier">return</span><span class="Delimiter">;</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>x<span class="Delimiter">.</span>type<span class="Delimiter">-&gt;</span>name == <span class="Constant">&quot;offset&quot;</span> || x<span class="Delimiter">.</span>type<span class="Delimiter">-&gt;</span>name == <span class="Constant">&quot;variant&quot;</span><span class="Delimiter">)</span> <span class="Identifier">return</span><span class="Delimiter">;</span>  <span class="Comment">// special-case for container-access instructions</span>
  put<span class="Delimiter">(</span>type<span class="Delimiter">,</span> x<span class="Delimiter">.</span>name<span class="Delimiter">,</span> x<span class="Delimiter">.</span>type<span class="Delimiter">);</span>
  trace<span class="Delimiter">(</span><span class="Constant">9993</span><span class="Delimiter">,</span> <span class="Constant">&quot;transform&quot;</span><span class="Delimiter">)</span> &lt;&lt; <span class="Constant">&quot;type of '&quot;</span> &lt;&lt; x<span class="Delimiter">.</span>name &lt;&lt; <span class="Constant">&quot;' is &quot;</span> &lt;&lt; names_to_string<span class="Delimiter">(</span>x<span class="Delimiter">.</span>type<span class="Delimiter">)</span> &lt;&lt; end<span class="Delimiter">();</span>
<span class="Delimiter">}</span>

<span class="Normal">void</span> compute_type_ingredient_mappings<span class="Delimiter">(</span><span class="Normal">const</span> recipe&amp; exemplar<span class="Delimiter">,</span> <span class="Normal">const</span> instruction&amp; inst<span class="Delimiter">,</span> map&lt;string<span class="Delimiter">,</span> <span class="Normal">const</span> type_tree*&gt;&amp; mappings<span class="Delimiter">,</span> <span class="Normal">const</span> recipe&amp; caller_recipe<span class="Delimiter">,</span> <span class="Normal">bool</span>* error<span class="Delimiter">)</span> <span class="Delimiter">{</span>
  <span class="Normal">int</span> limit = min<span class="Delimiter">(</span>SIZE<span class="Delimiter">(</span>inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">),</span> SIZE<span class="Delimiter">(</span>exemplar<span class="Delimiter">.</span>ingredients<span class="Delimiter">));</span>
  <span class="Normal">for</span> <span class="Delimiter">(</span><span class="Normal">int</span> i = <span class="Constant">0</span><span class="Delimiter">;</span>  i &lt; limit<span class="Delimiter">;</span>  ++i<span class="Delimiter">)</span> <span class="Delimiter">{</span>
    <span class="Normal">const</span> reagent&amp; exemplar_reagent = exemplar<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">);</span>
    reagent<span class="Comment">/*</span><span class="Comment">copy</span><span class="Comment">*/</span> ingredient = inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">);</span>
    canonize_type<span class="Delimiter">(</span>ingredient<span class="Delimiter">);</span>
    <span class="Normal">if</span> <span class="Delimiter">(</span>is_mu_address<span class="Delimiter">(</span>exemplar_reagent<span class="Delimiter">)</span> &amp;&amp; ingredient<span class="Delimiter">.</span>name == <span class="Constant">&quot;0&quot;</span><span class="Delimiter">)</span> <span class="Identifier">continue</span><span class="Delimiter">;</span>  <span class="Comment">// assume it matches</span>
    accumulate_type_ingredients<span class="Delimiter">(</span>exemplar_reagent<span class="Delimiter">,</span> ingredient<span class="Delimiter">,</span> mappings<span class="Delimiter">,</span> exemplar<span class="Delimiter">,</span> inst<span class="Delimiter">,</span> caller_recipe<span class="Delimiter">,</span> error<span class="Delimiter">);</span>
  <span class="Delimiter">}</span>
  limit = min<span class="Delimiter">(</span>SIZE<span class="Delimiter">(</span>inst<span class="Delimiter">.</span>products<span class="Delimiter">),</span> SIZE<span class="Delimiter">(</span>exemplar<span class="Delimiter">.</span>products<span class="Delimiter">));</span>
  <span class="Normal">for</span> <span class="Delimiter">(</span><span class="Normal">int</span> i = <span class="Constant">0</span><span class="Delimiter">;</span>  i &lt; limit<span class="Delimiter">;</span>  ++i<span class="Delimiter">)</span> <span class="Delimiter">{</span>
    <span class="Normal">const</span> reagent&amp; exemplar_reagent = exemplar<span class="Delimiter">.</span>products<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">);</span>
    reagent<span class="Comment">/*</span><span class="Comment">copy</span><span class="Comment">*/</span> product = inst<span class="Delimiter">.</span>products<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">);</span>
    <span class="Normal">if</span> <span class="Delimiter">(</span>is_dummy<span class="Delimiter">(</span>product<span class="Delimiter">))</span> <span class="Identifier">continue</span><span class="Delimiter">;</span>
    canonize_type<span class="Delimiter">(</span>product<span class="Delimiter">);</span>
    accumulate_type_ingredients<span class="Delimiter">(</span>exemplar_reagent<span class="Delimiter">,</span> product<span class="Delimiter">,</span> mappings<span class="Delimiter">,</span> exemplar<span class="Delimiter">,</span> inst<span class="Delimiter">,</span> caller_recipe<span class="Delimiter">,</span> error<span class="Delimiter">);</span>
  <span class="Delimiter">}</span>
<span class="Delimiter">}</span>

<span class="Normal">void</span> accumulate_type_ingredients<span class="Delimiter">(</span><span class="Normal">const</span> reagent&amp; exemplar_reagent<span class="Delimiter">,</span> reagent&amp; refinement<span class="Delimiter">,</span> map&lt;string<span class="Delimiter">,</span> <span class="Normal">const</span> type_tree*&gt;&amp; mappings<span class="Delimiter">,</span> <span class="Normal">const</span> recipe&amp; exemplar<span class="Delimiter">,</span> <span class="Normal">const</span> instruction&amp; call_instruction<span class="Delimiter">,</span> <span class="Normal">const</span> recipe&amp; caller_recipe<span class="Delimiter">,</span> <span class="Normal">bool</span>* error<span class="Delimiter">)</span> <span class="Delimiter">{</span>
  assert<span class="Delimiter">(</span>refinement<span class="Delimiter">.</span>type<span class="Delimiter">);</span>
  accumulate_type_ingredients<span class="Delimiter">(</span>exemplar_reagent<span class="Delimiter">.</span>type<span class="Delimiter">,</span> refinement<span class="Delimiter">.</span>type<span class="Delimiter">,</span> mappings<span class="Delimiter">,</span> exemplar<span class="Delimiter">,</span> exemplar_reagent<span class="Delimiter">,</span> call_instruction<span class="Delimiter">,</span> caller_recipe<span class="Delimiter">,</span> error<span class="Delimiter">);</span>
<span class="Delimiter">}</span>

<span class="Normal">void</span> accumulate_type_ingredients<span class="Delimiter">(</span><span class="Normal">const</span> type_tree* exemplar_type<span class="Delimiter">,</span> <span class="Normal">const</span> type_tree* refinement_type<span class="Delimiter">,</span> map&lt;string<span class="Delimiter">,</span> <span class="Normal">const</span> type_tree*&gt;&amp; mappings<span class="Delimiter">,</span> <span class="Normal">const</span> recipe&amp; exemplar<span class="Delimiter">,</span> <span class="Normal">const</span> reagent&amp; exemplar_reagent<span class="Delimiter">,</span> <span class="Normal">const</span> instruction&amp; call_instruction<span class="Delimiter">,</span> <span class="Normal">const</span> recipe&amp; caller_recipe<span class="Delimiter">,</span> <span class="Normal">bool</span>* error<span class="Delimiter">)</span> <span class="Delimiter">{</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>!exemplar_type<span class="Delimiter">)</span> <span class="Identifier">return</span><span class="Delimiter">;</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>!refinement_type<span class="Delimiter">)</span> <span class="Delimiter">{</span>
    <span class="Comment">// probably a bug in mu</span>
    <span class="Comment">// todo: make this smarter; only flag an error if exemplar_type contains some *new* type ingredient</span>
    raise &lt;&lt; maybe<span class="Delimiter">(</span>exemplar<span class="Delimiter">.</span>name<span class="Delimiter">)</span> &lt;&lt; <span class="Constant">&quot;missing type ingredient for &quot;</span> &lt;&lt; exemplar_reagent<span class="Delimiter">.</span>original_string &lt;&lt; <span class="cSpecial">'\n'</span> &lt;&lt; end<span class="Delimiter">();</span>
    raise &lt;&lt; <span class="Constant">&quot;  (called from '&quot;</span> &lt;&lt; to_original_string<span class="Delimiter">(</span>call_instruction<span class="Delimiter">)</span> &lt;&lt; <span class="Constant">&quot;')</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="Delimiter">;</span>
  <span class="Delimiter">}</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>!exemplar_type<span class="Delimiter">-&gt;</span>atom &amp;&amp; exemplar_type<span class="Delimiter">-&gt;</span>right == <span class="Constant">NULL</span> &amp;&amp; !refinement_type<span class="Delimiter">-&gt;</span>atom &amp;&amp; refinement_type<span class="Delimiter">-&gt;</span>right != <span class="Constant">NULL</span><span class="Delimiter">)</span> <span class="Delimiter">{</span>
    exemplar_type = exemplar_type<span class="Delimiter">-&gt;</span>left<span class="Delimiter">;</span>
    assert_for_now<span class="Delimiter">(</span>exemplar_type<span class="Delimiter">-&gt;</span>atom<span class="Delimiter">);</span>
  <span class="Delimiter">}</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>exemplar_type<span class="Delimiter">-&gt;</span>atom<span class="Delimiter">)</span> <span class="Delimiter">{</span>
    <span class="Normal">if</span> <span class="Delimiter">(</span>is_type_ingredient_name<span class="Delimiter">(</span>exemplar_type<span class="Delimiter">-&gt;</span>name<span class="Delimiter">))</span> <span class="Delimiter">{</span>
      <span class="Normal">const</span> type_tree* curr_refinement_type = <span class="Constant">NULL</span><span class="Delimiter">;</span>  <span class="Comment">// temporary heap allocation; must always be deleted before it goes out of scope</span>
      <span class="Normal">if</span> <span class="Delimiter">(</span>exemplar_type<span class="Delimiter">-&gt;</span>atom<span class="Delimiter">)</span>
        curr_refinement_type = <span class="Normal">new</span> type_tree<span class="Delimiter">(</span>*refinement_type<span class="Delimiter">);</span>
      <span class="Normal">else</span> <span class="Delimiter">{</span>
        assert<span class="Delimiter">(</span>!refinement_type<span class="Delimiter">-&gt;</span>atom<span class="Delimiter">);</span>
        curr_refinement_type = <span class="Normal">new</span> type_tree<span class="Delimiter">(</span>*refinement_type<span class="Delimiter">-&gt;</span>left<span class="Delimiter">);</span>
      <span class="Delimiter">}</span>
      <span class="Normal">if</span> <span class="Delimiter">(</span>!contains_key<span class="Delimiter">(</span>mappings<span class="Delimiter">,</span> exemplar_type<span class="Delimiter">-&gt;</span>name<span class="Delimiter">))</span> <span class="Delimiter">{</span>
        trace<span class="Delimiter">(</span><span class="Constant">9993</span><span class="Delimiter">,</span> <span class="Constant">&quot;transform&quot;</span><span class="Delimiter">)</span> &lt;&lt; <span class="Constant">&quot;adding mapping from &quot;</span> &lt;&lt; exemplar_type<span class="Delimiter">-&gt;</span>name &lt;&lt; <span class="Constant">&quot; to &quot;</span> &lt;&lt; to_string<span class="Delimiter">(</span>curr_refinement_type<span class="Delimiter">)</span> &lt;&lt; end<span class="Delimiter">();</span>
        put<span class="Delimiter">(</span>mappings<span class="Delimiter">,</span> exemplar_type<span class="Delimiter">-&gt;</span>name<span class="Delimiter">,</span> <span class="Normal">new</span> type_tree<span class="Delimiter">(</span>*curr_refinement_type<span class="Delimiter">));</span>
      <span class="Delimiter">}</span>
      <span class="Normal">else</span> <span class="Delimiter">{</span>
        <span class="Normal">if</span> <span class="Delimiter">(</span>!deeply_equal_type_names<span class="Delimiter">(</span>get<span class="Delimiter">(</span>mappings<span class="Delimiter">,</span> exemplar_type<span class="Delimiter">-&gt;</span>name<span class="Delimiter">),</span> curr_refinement_type<span class="Delimiter">))</span> <span class="Delimiter">{</span>
          raise &lt;&lt; maybe<span class="Delimiter">(</span>caller_recipe<span class="Delimiter">.</span>name<span class="Delimiter">)</span> &lt;&lt; <span class="Constant">&quot;no call found for '&quot;</span> &lt;&lt; to_original_string<span class="Delimiter">(</span>call_instruction<span class="Delimiter">)</span> &lt;&lt; <span class="Constant">&quot;'</span><span class="cSpecial">\n</span><span class="Constant">&quot;</span> &lt;&lt; end<span class="Delimiter">();</span>
          *error = <span class="Constant">true</span><span class="Delimiter">;</span>
          <span class="Normal">delete</span> curr_refinement_type<span class="Delimiter">;</span>
          <span class="Identifier">return</span><span class="Delimiter">;</span>
        <span class="Delimiter">}</span>
        <span class="Normal">if</span> <span class="Delimiter">(</span>get<span class="Delimiter">(</span>mappings<span class="Delimiter">,</span> exemplar_type<span class="Delimiter">-&gt;</span>name<span class="Delimiter">)-&gt;</span>name == <span class="Constant">&quot;literal&quot;</span><span class="Delimiter">)</span> <span class="Delimiter">{</span>
          <span class="Normal">delete</span> get<span class="Delimiter">(</span>mappings<span class="Delimiter">,</span> exemplar_type<span class="Delimiter">-&gt;</span>name<span class="Delimiter">);</span>
          put<span class="Delimiter">(</span>mappings<span class="Delimiter">,</span> exemplar_type<span class="Delimiter">-&gt;</span>name<span class="Delimiter">,</span> <span class="Normal">new</span> type_tree<span class="Delimiter">(</span>*curr_refinement_type<span class="Delimiter">));</span>
        <span class="Delimiter">}</span>
      <span class="Delimiter">}</span>
      <span class="Normal">delete</span> curr_refinement_type<span class="Delimiter">;</span>
    <span class="Delimiter">}</span>
  <span class="Delimiter">}</span>
  <span class="Normal">else</span> <span class="Delimiter">{</span>
    accumulate_type_ingredients<span class="Delimiter">(</span>exemplar_type<span class="Delimiter">-&gt;</span>left<span class="Delimiter">,</span> refinement_type<span class="Delimiter">-&gt;</span>left<span class="Delimiter">,</span> mappings<span class="Delimiter">,</span> exemplar<span class="Delimiter">,</span> exemplar_reagent<span class="Delimiter">,</span> call_instruction<span class="Delimiter">,</span> caller_recipe<span class="Delimiter">,</span> error<span class="Delimiter">);</span>
    accumulate_type_ingredients<span class="Delimiter">(</span>exemplar_type<span class="Delimiter">-&gt;</span>right<span class="Delimiter">,</span> refinement_type<span class="Delimiter">-&gt;</span>right<span class="Delimiter">,</span> mappings<span class="Delimiter">,</span> exemplar<span class="Delimiter">,</span> exemplar_reagent<span class="Delimiter">,</span> call_instruction<span class="Delimiter">,</span> caller_recipe<span class="Delimiter">,</span> error<span class="Delimiter">);</span>
  <span class="Delimiter">}</span>
<span class="Delimiter">}</span>

<span class="Normal">void</span> replace_type_ingredients<span class="Delimiter">(</span>recipe&amp; new_recipe<span class="Delimiter">,</span> <span class="Normal">const</span> map&lt;string<span class="Delimiter">,</span> <span class="Normal">const</span> type_tree*&gt;&amp; mappings<span class="Delimiter">)</span> <span class="Delimiter">{</span>
  <span class="Comment">// update its header</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>mappings<span class="Delimiter">.</span>empty<span class="Delimiter">())</span> <span class="Identifier">return</span><span class="Delimiter">;</span>
  trace<span class="Delimiter">(</span><span class="Constant">9993</span><span class="Delimiter">,</span> <span class="Constant">&quot;transform&quot;</span><span class="Delimiter">)</span> &lt;&lt; <span class="Constant">&quot;replacing in recipe header ingredients&quot;</span> &lt;&lt; end<span class="Delimiter">();</span>
  <span class="Normal">for</span> <span class="Delimiter">(</span><span class="Normal">int</span> i = <span class="Constant">0</span><span class="Delimiter">;</span>  i &lt; SIZE<span class="Delimiter">(</span>new_recipe<span class="Delimiter">.</span>ingredients<span class="Delimiter">);</span>  ++i<span class="Delimiter">)</span>
    replace_type_ingredients<span class="Delimiter">(</span>new_recipe<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">),</span> mappings<span class="Delimiter">,</span> new_recipe<span class="Delimiter">);</span>
  trace<span class="Delimiter">(</span><span class="Constant">9993</span><span class="Delimiter">,</span> <span class="Constant">&quot;transform&quot;</span><span class="Delimiter">)</span> &lt;&lt; <span class="Constant">&quot;replacing in recipe header products&quot;</span> &lt;&lt; end<span class="Delimiter">();</span>
  <span class="Normal">for</span> <span class="Delimiter">(</span><span class="Normal">int</span> i = <span class="Constant">0</span><span class="Delimiter">;</span>  i &lt; SIZE<span class="Delimiter">(</span>new_recipe<span class="Delimiter">.</span>products<span class="Delimiter">);</span>  ++i<span class="Delimiter">)</span>
    replace_type_ingredients<span class="Delimiter">(</span>new_recipe<span class="Delimiter">.</span>products<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">),</span> mappings<span class="Delimiter">,</span> new_recipe<span class="Delimiter">);</span>
  <span class="Comment">// update its body</span>
  <span class="Normal">for</span> <span class="Delimiter">(</span><span class="Normal">int</span> i = <span class="Constant">0</span><span class="Delimiter">;</span>  i &lt; SIZE<span class="Delimiter">(</span>new_recipe<span class="Delimiter">.</span>steps<span class="Delimiter">);</span>  ++i<span class="Delimiter">)</span> <span class="Delimiter">{</span>
    instruction&amp; inst = new_recipe<span class="Delimiter">.</span>steps<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">);</span>
    trace<span class="Delimiter">(</span><span class="Constant">9993</span><span class="Delimiter">,</span> <span class="Constant">&quot;transform&quot;</span><span class="Delimiter">)</span> &lt;&lt; <span class="Constant">&quot;replacing in instruction '&quot;</span> &lt;&lt; to_string<span class="Delimiter">(</span>inst<span class="Delimiter">)</span> &lt;&lt; <span class="Constant">&quot;'&quot;</span> &lt;&lt; end<span class="Delimiter">();</span>
    <span class="Normal">for</span> <span class="Delimiter">(</span><span class="Normal">int</span> j = <span class="Constant">0</span><span class="Delimiter">;</span>  j &lt; SIZE<span class="Delimiter">(</span>inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">);</span>  ++j<span class="Delimiter">)</span>
      replace_type_ingredients<span class="Delimiter">(</span>inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span>j<span class="Delimiter">),</span> mappings<span class="Delimiter">,</span> new_recipe<span class="Delimiter">);</span>
    <span class="Normal">for</span> <span class="Delimiter">(</span><span class="Normal">int</span> j = <span class="Constant">0</span><span class="Delimiter">;</span>  j &lt; SIZE<span class="Delimiter">(</span>inst<span class="Delimiter">.</span>products<span class="Delimiter">);</span>  ++j<span class="Delimiter">)</span>
      replace_type_ingredients<span class="Delimiter">(</span>inst<span class="Delimiter">.</span>products<span class="Delimiter">.</span>at<span class="Delimiter">(</span>j<span class="Delimiter">),</span> mappings<span class="Delimiter">,</span> new_recipe<span class="Delimiter">);</span>
    <span class="Comment">// special-case for new: replace type ingredient in first ingredient *value*</span>
    <span class="Normal">if</span> <span class="Delimiter">(</span>inst<span class="Delimiter">.</span>name == <span class="Constant">&quot;new&quot;</span> &amp;&amp; inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">).</span>type<span class="Delimiter">-&gt;</span>name != <span class="Constant">&quot;literal-string&quot;</span><span class="Delimiter">)</span> <span class="Delimiter">{</span>
      type_tree* type = parse_type_tree<span class="Delimiter">(</span>inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">).</span>name<span class="Delimiter">);</span>
      replace_type_ingredients<span class="Delimiter">(</span>type<span class="Delimiter">,</span> mappings<span class="Delimiter">);</span>
      inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">).</span>name = inspect<span class="Delimiter">(</span>type<span class="Delimiter">);</span>
      <span class="Normal">delete</span> type<span class="Delimiter">;</span>
    <span class="Delimiter">}</span>
  <span class="Delimiter">}</span>
<span class="Delimiter">}</span>

<span class="Normal">void</span> replace_type_ingredients<span class="Delimiter">(</span>reagent&amp; x<span class="Delimiter">,</span> <span class="Normal">const</span> map&lt;string<span class="Delimiter">,</span> <span class="Normal">const</span> type_tree*&gt;&amp; mappings<span class="Delimiter">,</span> <span class="Normal">const</span> recipe&amp; caller<span class="Delimiter">)</span> <span class="Delimiter">{</span>
  string before = to_string<span class="Delimiter">(</span>x<span class="Delimiter">);</span>
  trace<span class="Delimiter">(</span><span class="Constant">9993</span><span class="Delimiter">,</span> <span class="Constant">&quot;transform&quot;</span><span class="Delimiter">)</span> &lt;&lt; <span class="Constant">&quot;replacing in ingredient &quot;</span> &lt;&lt; x<span class="Delimiter">.</span>original_string &lt;&lt; end<span class="Delimiter">();</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>!x<span class="Delimiter">.</span>type<span class="Delimiter">)</span> <span class="Delimiter">{</span>
    raise &lt;&lt; <span class="Constant">&quot;specializing &quot;</span> &lt;&lt; caller<span class="Delimiter">.</span>original_name &lt;&lt; <span class="Constant">&quot;: missing type for '&quot;</span> &lt;&lt; x<span class="Delimiter">.</span>original_string &lt;&lt; <span class="Constant">&quot;'</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="Delimiter">;</span>
  <span class="Delimiter">}</span>
  replace_type_ingredients<span class="Delimiter">(</span>x<span class="Delimiter">.</span>type<span class="Delimiter">,</span> mappings<span class="Delimiter">);</span>
<span class="Delimiter">}</span>

<span class="Normal">void</span> replace_type_ingredients<span class="Delimiter">(</span>type_tree* type<span class="Delimiter">,</span> <span class="Normal">const</span> map&lt;string<span class="Delimiter">,</span> <span class="Normal">const</span> type_tree*&gt;&amp; mappings<span class="Delimiter">)</span> <span class="Delimiter">{</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>!type<span class="Delimiter">)</span> <span class="Identifier">return</span><span class="Delimiter">;</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>!type<span class="Delimiter">-&gt;</span>atom<span class="Delimiter">)</span> <span class="Delimiter">{</span>
    <span class="Normal">if</span> <span class="Delimiter">(</span>type<span class="Delimiter">-&gt;</span>right == <span class="Constant">NULL</span> &amp;&amp; type<span class="Delimiter">-&gt;</span>left != <span class="Constant">NULL</span> &amp;&amp; type<span class="Delimiter">-&gt;</span>left<span class="Delimiter">-&gt;</span>atom &amp;&amp; contains_key<span class="Delimiter">(</span>mappings<span class="Delimiter">,</span> type<span class="Delimiter">-&gt;</span>left<span class="Delimiter">-&gt;</span>name<span class="Delimiter">)</span> &amp;&amp; !get<span class="Delimiter">(</span>mappings<span class="Delimiter">,</span> type<span class="Delimiter">-&gt;</span>left<span class="Delimiter">-&gt;</span>name<span class="Delimiter">)-&gt;</span>atom &amp;&amp; get<span class="Delimiter">(</span>mappings<span class="Delimiter">,</span> type<span class="Delimiter">-&gt;</span>left<span class="Delimiter">-&gt;</span>name<span class="Delimiter">)-&gt;</span>right != <span class="Constant">NULL</span><span class="Delimiter">)</span> <span class="Delimiter">{</span>
      *type = *get<span class="Delimiter">(</span>mappings<span class="Delimiter">,</span> type<span class="Delimiter">-&gt;</span>left<span class="Delimiter">-&gt;</span>name<span class="Delimiter">);</span>
      <span class="Identifier">return</span><span class="Delimiter">;</span>
    <span class="Delimiter">}</span>
    replace_type_ingredients<span class="Delimiter">(</span>type<span class="Delimiter">-&gt;</span>left<span class="Delimiter">,</span> mappings<span class="Delimiter">);</span>
    replace_type_ingredients<span class="Delimiter">(</span>type<span class="Delimiter">-&gt;</span>right<span class="Delimiter">,</span> mappings<span class="Delimiter">);</span>
    <span class="Identifier">return</span><span class="Delimiter">;</span>
  <span class="Delimiter">}</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>contains_key<span class="Delimiter">(</span>Type_ordinal<span class="Delimiter">,</span> type<span class="Delimiter">-&gt;</span>name<span class="Delimiter">))</span>  <span class="Comment">// todo: ugly side effect</span>
    type<span class="Delimiter">-&gt;</span>value = get<span class="Delimiter">(</span>Type_ordinal<span class="Delimiter">,</span> type<span class="Delimiter">-&gt;</span>name<span class="Delimiter">);</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>!contains_key<span class="Delimiter">(</span>mappings<span class="Delimiter">,</span> type<span class="Delimiter">-&gt;</span>name<span class="Delimiter">))</span>
    <span class="Identifier">return</span><span class="Delimiter">;</span>
  <span class="Normal">const</span> type_tree* replacement = get<span class="Delimiter">(</span>mappings<span class="Delimiter">,</span> type<span class="Delimiter">-&gt;</span>name<span class="Delimiter">);</span>
  trace<span class="Delimiter">(</span><span class="Constant">9993</span><span class="Delimiter">,</span> <span class="Constant">&quot;transform&quot;</span><span class="Delimiter">)</span> &lt;&lt; type<span class="Delimiter">-&gt;</span>name &lt;&lt; <span class="Constant">&quot; =&gt; &quot;</span> &lt;&lt; names_to_string<span class="Delimiter">(</span>replacement<span class="Delimiter">)</span> &lt;&lt; end<span class="Delimiter">();</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>replacement<span class="Delimiter">-&gt;</span>atom<span class="Delimiter">)</span> <span class="Delimiter">{</span>
    <span class="Normal">if</span> <span class="Delimiter">(</span>!contains_key<span class="Delimiter">(</span>Type_ordinal<span class="Delimiter">,</span> replacement<span class="Delimiter">-&gt;</span>name<span class="Delimiter">))</span> <span class="Delimiter">{</span>
      <span class="Comment">// error in program; should be reported elsewhere</span>
      <span class="Identifier">return</span><span class="Delimiter">;</span>
    <span class="Delimiter">}</span>
    type<span class="Delimiter">-&gt;</span>name = <span class="Delimiter">(</span>replacement<span class="Delimiter">-&gt;</span>name == <span class="Constant">&quot;literal&quot;</span><span class="Delimiter">)</span> ? <span class="Constant">&quot;number&quot;</span> : replacement<span class="Delimiter">-&gt;</span>name<span class="Delimiter">;</span>
    type<span class="Delimiter">-&gt;</span>value = get<span class="Delimiter">(</span>Type_ordinal<span class="Delimiter">,</span> type<span class="Delimiter">-&gt;</span>name<span class="Delimiter">);</span>
  <span class="Delimiter">}</span>
  <span class="Normal">else</span> <span class="Delimiter">{</span>
    *type = *replacement<span class="Delimiter">;</span>
  <span class="Delimiter">}</span>
<span class="Delimiter">}</span>

<span class="Normal">int</span> type_ingredient_count_in_header<span class="Delimiter">(</span>recipe_ordinal variant<span class="Delimiter">)</span> <span class="Delimiter">{</span>
  <span class="Normal">const</span> recipe&amp; caller = get<span class="Delimiter">(</span>Recipe<span class="Delimiter">,</span> variant<span class="Delimiter">);</span>
  set&lt;string&gt; type_ingredients<span class="Delimiter">;</span>
  <span class="Normal">for</span> <span class="Delimiter">(</span><span class="Normal">int</span> 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>
    accumulate_type_ingredients<span class="Delimiter">(</span>caller<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">).</span>type<span class="Delimiter">,</span> type_ingredients<span class="Delimiter">);</span>
  <span class="Normal">for</span> <span class="Delimiter">(</span><span class="Normal">int</span> i = <span class="Constant">0</span><span class="Delimiter">;</span>  i &lt; SIZE<span class="Delimiter">(</span>caller<span class="Delimiter">.</span>products<span class="Delimiter">);</span>  ++i<span class="Delimiter">)</span>
    accumulate_type_ingredients<span class="Delimiter">(</span>caller<span class="Delimiter">.</span>products<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">).</span>type<span class="Delimiter">,</span> type_ingredients<span class="Delimiter">);</span>
  <span class="Identifier">return</span> SIZE<span class="Delimiter">(</span>type_ingredients<span class="Delimiter">);</span>
<span class="Delimiter">}</span>

<span class="Normal">void</span> accumulate_type_ingredients<span class="Delimiter">(</span><span class="Normal">const</span> type_tree* type<span class="Delimiter">,</span> set&lt;string&gt;&amp; out<span class="Delimiter">)</span> <span class="Delimiter">{</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>!type<span class="Delimiter">)</span> <span class="Identifier">return</span><span class="Delimiter">;</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>is_type_ingredient_name<span class="Delimiter">(</span>type<span class="Delimiter">-&gt;</span>name<span class="Delimiter">))</span> out<span class="Delimiter">.</span>insert<span class="Delimiter">(</span>type<span class="Delimiter">-&gt;</span>name<span class="Delimiter">);</span>
  accumulate_type_ingredients<span class="Delimiter">(</span>type<span class="Delimiter">-&gt;</span>left<span class="Delimiter">,</span> out<span class="Delimiter">);</span>
  accumulate_type_ingredients<span class="Delimiter">(</span>type<span class="Delimiter">-&gt;</span>right<span class="Delimiter">,</span> out<span class="Delimiter">);</span>
<span class="Delimiter">}</span>

type_tree* parse_type_tree<span class="Delimiter">(</span><span class="Normal">const</span> string&amp; s<span class="Delimiter">)</span> <span class="Delimiter">{</span>
  string_tree* s2 = parse_string_tree<span class="Delimiter">(</span>s<span class="Delimiter">);</span>
  type_tree* result = new_type_tree<span class="Delimiter">(</span>s2<span class="Delimiter">);</span>
  <span class="Normal">delete</span> s2<span class="Delimiter">;</span>
  <span class="Identifier">return</span> result<span class="Delimiter">;</span>
<span class="Delimiter">}</span>

string inspect<span class="Delimiter">(</span><span class="Normal">const</span> type_tree* x<span class="Delimiter">)</span> <span class="Delimiter">{</span>
  ostringstream out<span class="Delimiter">;</span>
  dump_inspect<span class="Delimiter">(</span>x<span class="Delimiter">,</span> out<span class="Delimiter">);</span>
  <span class="Identifier">return</span> out<span class="Delimiter">.</span>str<span class="Delimiter">();</span>
<span class="Delimiter">}</span>

<span class="Normal">void</span> dump_inspect<span class="Delimiter">(</span><span class="Normal">const</span> type_tree* x<span class="Delimiter">,</span> ostream&amp; out<span class="Delimiter">)</span> <span class="Delimiter">{</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>!x<span class="Delimiter">-&gt;</span>left &amp;&amp; !x<span class="Delimiter">-&gt;</span>right<span class="Delimiter">)</span> <span class="Delimiter">{</span>
    out &lt;&lt; x<span class="Delimiter">-&gt;</span>name<span class="Delimiter">;</span>
    <span class="Identifier">return</span><span class="Delimiter">;</span>
  <span class="Delimiter">}</span>
  out &lt;&lt; <span class="Constant">'('</span><span class="Delimiter">;</span>
  <span class="Normal">for</span> <span class="Delimiter">(</span><span class="Normal">const</span> type_tree* curr = x<span class="Delimiter">;</span>  curr<span class="Delimiter">;</span>  curr = curr<span class="Delimiter">-&gt;</span>right<span class="Delimiter">)</span> <span class="Delimiter">{</span>
    <span class="Normal">if</span> <span class="Delimiter">(</span>curr != x<span class="Delimiter">)</span> out &lt;&lt; <span class="Constant">' '</span><span class="Delimiter">;</span>
    <span class="Normal">if</span> <span class="Delimiter">(</span>curr<span class="Delimiter">-&gt;</span>left<span class="Delimiter">)</span>
      dump_inspect<span class="Delimiter">(</span>curr<span class="Delimiter">-&gt;</span>left<span class="Delimiter">,</span> out<span class="Delimiter">);</span>
    <span class="Normal">else</span>
      out &lt;&lt; curr<span class="Delimiter">-&gt;</span>name<span class="Delimiter">;</span>
  <span class="Delimiter">}</span>
  out &lt;&lt; <span class="Constant">')'</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>

<span class="Normal">void</span> ensure_all_concrete_types<span class="Delimiter">(</span><span class="Comment">/*</span><span class="Comment">const</span><span class="Comment">*/</span> recipe&amp; new_recipe<span class="Delimiter">,</span> <span class="Normal">const</span> recipe&amp; exemplar<span class="Delimiter">)</span> <span class="Delimiter">{</span>
  trace<span class="Delimiter">(</span><span class="Constant">9993</span><span class="Delimiter">,</span> <span class="Constant">&quot;transform&quot;</span><span class="Delimiter">)</span> &lt;&lt; <span class="Constant">&quot;-- ensure all concrete types in recipe &quot;</span> &lt;&lt; new_recipe<span class="Delimiter">.</span>name &lt;&lt; end<span class="Delimiter">();</span>
  <span class="Normal">for</span> <span class="Delimiter">(</span><span class="Normal">int</span> i = <span class="Constant">0</span><span class="Delimiter">;</span>  i &lt; SIZE<span class="Delimiter">(</span>new_recipe<span class="Delimiter">.</span>ingredients<span class="Delimiter">);</span>  ++i<span class="Delimiter">)</span>
    ensure_all_concrete_types<span class="Delimiter">(</span>new_recipe<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">),</span> exemplar<span class="Delimiter">);</span>
  <span class="Normal">for</span> <span class="Delimiter">(</span><span class="Normal">int</span> i = <span class="Constant">0</span><span class="Delimiter">;</span>  i &lt; SIZE<span class="Delimiter">(</span>new_recipe<span class="Delimiter">.</span>products<span class="Delimiter">);</span>  ++i<span class="Delimiter">)</span>
    ensure_all_concrete_types<span class="Delimiter">(</span>new_recipe<span class="Delimiter">.</span>products<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">),</span> exemplar<span class="Delimiter">);</span>
  <span class="Normal">for</span> <span class="Delimiter">(</span><span class="Normal">int</span> i = <span class="Constant">0</span><span class="Delimiter">;</span>  i &lt; SIZE<span class="Delimiter">(</span>new_recipe<span class="Delimiter">.</span>steps<span class="Delimiter">);</span>  ++i<span class="Delimiter">)</span> <span class="Delimiter">{</span>
    instruction&amp; inst = new_recipe<span class="Delimiter">.</span>steps<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">);</span>
    <span class="Normal">for</span> <span class="Delimiter">(</span><span class="Normal">int</span> j = <span class="Constant">0</span><span class="Delimiter">;</span>  j &lt; SIZE<span class="Delimiter">(</span>inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">);</span>  ++j<span class="Delimiter">)</span>
      ensure_all_concrete_types<span class="Delimiter">(</span>inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span>j<span class="Delimiter">),</span> exemplar<span class="Delimiter">);</span>
    <span class="Normal">for</span> <span class="Delimiter">(</span><span class="Normal">int</span> j = <span class="Constant">0</span><span class="Delimiter">;</span>  j &lt; SIZE<span class="Delimiter">(</span>inst<span class="Delimiter">.</span>products<span class="Delimiter">);</span>  ++j<span class="Delimiter">)</span>
      ensure_all_concrete_types<span class="Delimiter">(</span>inst<span class="Delimiter">.</span>products<span class="Delimiter">.</span>at<span class="Delimiter">(</span>j<span class="Delimiter">),</span> exemplar<span class="Delimiter">);</span>
  <span class="Delimiter">}</span>
<span class="Delimiter">}</span>

<span class="Normal">void</span> ensure_all_concrete_types<span class="Delimiter">(</span><span class="Comment">/*</span><span class="Comment">const</span><span class="Comment">*/</span> reagent&amp; x<span class="Delimiter">,</span> <span class="Normal">const</span> recipe&amp; exemplar<span class="Delimiter">)</span> <span class="Delimiter">{</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>!x<span class="Delimiter">.</span>type || contains_type_ingredient_name<span class="Delimiter">(</span>x<span class="Delimiter">.</span>type<span class="Delimiter">))</span> <span class="Delimiter">{</span>
    raise &lt;&lt; maybe<span class="Delimiter">(</span>exemplar<span class="Delimiter">.</span>name<span class="Delimiter">)</span> &lt;&lt; <span class="Constant">&quot;failed to map a type to &quot;</span> &lt;&lt; x<span class="Delimiter">.</span>original_string &lt;&lt; <span class="cSpecial">'\n'</span> &lt;&lt; end<span class="Delimiter">();</span>
    <span class="Normal">if</span> <span class="Delimiter">(</span>!x<span class="Delimiter">.</span>type<span class="Delimiter">)</span> x<span class="Delimiter">.</span>type = <span class="Normal">new</span> type_tree<span class="Delimiter">(</span><span class="Constant">&quot;&quot;</span><span class="Delimiter">,</span> <span class="Constant">0</span><span class="Delimiter">);</span>  <span class="Comment">// just to prevent crashes later</span>
    <span class="Identifier">return</span><span class="Delimiter">;</span>
  <span class="Delimiter">}</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>x<span class="Delimiter">.</span>type<span class="Delimiter">-&gt;</span>value == -<span class="Constant">1</span><span class="Delimiter">)</span> <span class="Delimiter">{</span>
    raise &lt;&lt; maybe<span class="Delimiter">(</span>exemplar<span class="Delimiter">.</span>name<span class="Delimiter">)</span> &lt;&lt; <span class="Constant">&quot;failed to map a type to the unknown &quot;</span> &lt;&lt; x<span class="Delimiter">.</span>original_string &lt;&lt; <span class="cSpecial">'\n'</span> &lt;&lt; end<span class="Delimiter">();</span>
    <span class="Identifier">return</span><span class="Delimiter">;</span>
  <span class="Delimiter">}</span>
<span class="Delimiter">}</span>

<span class="Delimiter">:(scenario shape_shifting_recipe_2)</span>
<span class="muRecipe">def</span> main [
  <span class="Constant">10</span>:point<span class="Special"> &lt;- </span>merge <span class="Constant">14</span><span class="Delimiter">,</span> <span class="Constant">15</span>
  <span class="Constant">11</span>:point<span class="Special"> &lt;- </span>foo <span class="Constant">10</span>:point
]
<span class="Comment"># non-matching shape-shifting variant</span>
<span class="muRecipe">def</span> foo a:_t<span class="Delimiter">,</span> b:_t<span class="muRecipe"> -&gt; </span>result:num [
  local-scope
  load-ingredients
  result<span class="Special"> &lt;- </span>copy <span class="Constant">34</span>
]
<span class="Comment"># matching shape-shifting variant</span>
<span class="muRecipe">def</span> foo a:_t<span class="muRecipe"> -&gt; </span>result:_t [
  local-scope
  load-ingredients
  result<span class="Special"> &lt;- </span>copy a
]
<span class="traceContains">+mem: storing 14 in location 11</span>
<span class="traceContains">+mem: storing 15 in location 12</span>

<span class="Delimiter">:(scenario shape_shifting_recipe_nonroot)</span>
<span class="muRecipe">def</span> main [
  <span class="Constant">10</span>:foo:point<span class="Special"> &lt;- </span>merge <span class="Constant">14</span><span class="Delimiter">,</span> <span class="Constant">15</span><span class="Delimiter">,</span> <span class="Constant">16</span>
  <span class="Constant">20</span>:point/<span class="Special">raw &lt;- </span>bar <span class="Constant">10</span>:foo:point
]
<span class="Comment"># shape-shifting recipe with type ingredient following some other type</span>
<span class="muRecipe">def</span> bar a:foo:_t<span class="muRecipe"> -&gt; </span>result:_t [
  local-scope
  load-ingredients
  result<span class="Special"> &lt;- </span>get a<span class="Delimiter">,</span> <span class="Constant">x:offset</span>
]
<span class="muData">container</span> foo:_t [
  <span class="Normal">x</span>:_t
  <span class="Normal">y</span>:num
]
<span class="traceContains">+mem: storing 14 in location 20</span>
<span class="traceContains">+mem: storing 15 in location 21</span>

<span class="Delimiter">:(scenario shape_shifting_recipe_nested)</span>
<span class="muData">container</span> c:_a:_b [
  <span class="Normal">a</span>:_a
  <span class="Normal">b</span>:_b
]
<span class="muRecipe">def</span> main [
  <span class="Normal">s</span>:text<span class="Special"> &lt;- </span><span class="Normal">new</span> [abc]
  <span class="Delimiter">{</span>x: <span class="Delimiter">(</span>c <span class="Delimiter">(</span>address array character<span class="Delimiter">)</span> number<span class="Delimiter">)}</span><span class="Special"> &lt;- </span>merge s<span class="Delimiter">,</span> <span class="Constant">34</span>
  foo x
]
<span class="muRecipe">def</span> foo x:c:_bar:_baz [
  local-scope
  load-ingredients
]
<span class="Comment"># no errors</span>

<span class="Delimiter">:(scenario shape_shifting_recipe_type_deduction_ignores_offsets)</span>
<span class="muRecipe">def</span> main [
  <span class="Constant">10</span>:foo:point<span class="Special"> &lt;- </span>merge <span class="Constant">14</span><span class="Delimiter">,</span> <span class="Constant">15</span><span class="Delimiter">,</span> <span class="Constant">16</span>
  <span class="Constant">20</span>:point/<span class="Special">raw &lt;- </span>bar <span class="Constant">10</span>:foo:point
]
<span class="muRecipe">def</span> bar a:foo:_t<span class="muRecipe"> -&gt; </span>result:_t [
  local-scope
  load-ingredients
  <span class="Normal">x</span>:num<span class="Special"> &lt;- </span>copy <span class="Constant">1</span>
  result<span class="Special"> &lt;- </span>get a<span class="Delimiter">,</span> <span class="Constant">x:offset</span>  <span class="Comment"># shouldn't collide with other variable</span>
]
<span class="muData">container</span> foo:_t [
  <span class="Normal">x</span>:_t
  <span class="Normal">y</span>:num
]
<span class="traceContains">+mem: storing 14 in location 20</span>
<span class="traceContains">+mem: storing 15 in location 21</span>

<span class="Delimiter">:(scenario shape_shifting_recipe_empty)</span>
<span class="muRecipe">def</span> main [
  foo <span class="Constant">1</span>
]
<span class="Comment"># shape-shifting recipe with no body</span>
<span class="muRecipe">def</span> foo a:_t [
]
<span class="Comment"># shouldn't crash</span>

<span class="Delimiter">:(scenario shape_shifting_recipe_handles_shape_shifting_new_ingredient)</span>
<span class="muRecipe">def</span> main [
  <span class="Constant">1</span>:&amp;:foo:point<span class="Special"> &lt;- </span>bar <span class="Constant">3</span>
  <span class="Constant">11</span>:foo:point<span class="Special"> &lt;- </span>copy *<span class="Constant">1</span>:&amp;:foo:point
]
<span class="muData">container</span> foo:_t [
  <span class="Normal">x</span>:_t
  <span class="Normal">y</span>:num
]
<span class="muRecipe">def</span> bar x:num<span class="muRecipe"> -&gt; </span>result:&amp;:foo:_t [
  local-scope
  load-ingredients
  <span class="Comment"># new refers to _t in its ingredient *value*</span>
  result<span class="Special"> &lt;- </span><span class="Normal">new</span> <span class="Delimiter">{(</span>foo _t<span class="Delimiter">)</span> : type<span class="Delimiter">}</span>
]
<span class="traceContains">+mem: storing 0 in location 11</span>
<span class="traceContains">+mem: storing 0 in location 12</span>
<span class="traceContains">+mem: storing 0 in location 13</span>

<span class="Delimiter">:(scenario shape_shifting_recipe_handles_shape_shifting_new_ingredient_2)</span>
<span class="muRecipe">def</span> main [
  <span class="Constant">1</span>:&amp;:foo:point<span class="Special"> &lt;- </span>bar <span class="Constant">3</span>
  <span class="Constant">11</span>:foo:point<span class="Special"> &lt;- </span>copy *<span class="Constant">1</span>:&amp;:foo:point
]
<span class="muRecipe">def</span> bar x:num<span class="muRecipe"> -&gt; </span>result:&amp;:foo:_t [
  local-scope
  load-ingredients
  <span class="Comment"># new refers to _t in its ingredient *value*</span>
  result<span class="Special"> &lt;- </span><span class="Normal">new</span> <span class="Delimiter">{(</span>foo _t<span class="Delimiter">)</span> : type<span class="Delimiter">}</span>
]
<span class="Comment"># container defined after use</span>
<span class="muData">container</span> foo:_t [
  <span class="Normal">x</span>:_t
  <span class="Normal">y</span>:num
]
<span class="traceContains">+mem: storing 0 in location 11</span>
<span class="traceContains">+mem: storing 0 in location 12</span>
<span class="traceContains">+mem: storing 0 in location 13</span>

<span class="Delimiter">:(scenario shape_shifting_recipe_called_with_dummy)</span>
<span class="muRecipe">def</span> main [
  _<span class="Special"> &lt;- </span>bar <span class="Constant">34</span>
]
<span class="muRecipe">def</span> bar x:_t<span class="muRecipe"> -&gt; </span>result:&amp;:_t [
  local-scope
  load-ingredients
  result<span class="Special"> &lt;- </span>copy <span class="Constant">0</span>
]
$error: <span class="Constant">0</span>

<span class="Delimiter">:(code)</span>
<span class="Comment">// this one needs a little more fine-grained control</span>
<span class="Normal">void</span> test_shape_shifting_new_ingredient_does_not_pollute_global_namespace<span class="Delimiter">()</span> <span class="Delimiter">{</span>
  <span class="Comment">// if you specialize a shape-shifting recipe that allocates a type-ingredient..</span>
  transform<span class="Delimiter">(</span><span class="Constant">&quot;def barz x:_elem [</span><span class="cSpecial">\n</span><span class="Constant">&quot;</span>
            <span class="Constant">&quot;  local-scope</span><span class="cSpecial">\n</span><span class="Constant">&quot;</span>
            <span class="Constant">&quot;  load-ingredients</span><span class="cSpecial">\n</span><span class="Constant">&quot;</span>
            <span class="Constant">&quot;  y:&amp;:num &lt;- new _elem:type</span><span class="cSpecial">\n</span><span class="Constant">&quot;</span>
            <span class="Constant">&quot;]</span><span class="cSpecial">\n</span><span class="Constant">&quot;</span>
            <span class="Constant">&quot;def fooz [</span><span class="cSpecial">\n</span><span class="Constant">&quot;</span>
            <span class="Constant">&quot;  local-scope</span><span class="cSpecial">\n</span><span class="Constant">&quot;</span>
            <span class="Constant">&quot;  barz 34</span><span class="cSpecial">\n</span><span class="Constant">&quot;</span>
            <span class="Constant">&quot;]</span><span class="cSpecial">\n</span><span class="Constant">&quot;</span><span class="Delimiter">);</span>
  <span class="Comment">// ..and if you then try to load a new shape-shifting container with that</span>
  <span class="Comment">// type-ingredient</span>
  run<span class="Delimiter">(</span><span class="Constant">&quot;container foo:_elem [</span><span class="cSpecial">\n</span><span class="Constant">&quot;</span>
      <span class="Constant">&quot;  x:_elem</span><span class="cSpecial">\n</span><span class="Constant">&quot;</span>
      <span class="Constant">&quot;  y:num</span><span class="cSpecial">\n</span><span class="Constant">&quot;</span>
      <span class="Constant">&quot;]</span><span class="cSpecial">\n</span><span class="Constant">&quot;</span><span class="Delimiter">);</span>
  <span class="Comment">// then it should work as usual</span>
  reagent callsite<span class="Delimiter">(</span><span class="Constant">&quot;x:foo:point&quot;</span><span class="Delimiter">);</span>
  reagent element = element_type<span class="Delimiter">(</span>callsite<span class="Delimiter">.</span>type<span class="Delimiter">,</span> <span class="Constant">0</span><span class="Delimiter">);</span>
  CHECK_EQ<span class="Delimiter">(</span>element<span class="Delimiter">.</span>name<span class="Delimiter">,</span> <span class="Constant">&quot;x&quot;</span><span class="Delimiter">);</span>
  CHECK_EQ<span class="Delimiter">(</span>element<span class="Delimiter">.</span>type<span class="Delimiter">-&gt;</span>name<span class="Delimiter">,</span> <span class="Constant">&quot;point&quot;</span><span class="Delimiter">);</span>
  CHECK<span class="Delimiter">(</span>!element<span class="Delimiter">.</span>type<span class="Delimiter">-&gt;</span>right<span class="Delimiter">);</span>
<span class="Delimiter">}</span>

<span class="Comment">//: specializing a type ingredient with a compound type</span>
<span class="Delimiter">:(scenario shape_shifting_recipe_supports_compound_types)</span>
<span class="muRecipe">def</span> main [
  <span class="Constant">1</span>:&amp;:point<span class="Special"> &lt;- </span><span class="Normal">new</span> <span class="Constant">point:type</span>
  *<span class="Constant">1</span>:&amp;:point<span class="Special"> &lt;- </span>put *<span class="Constant">1</span>:&amp;:point<span class="Delimiter">,</span> <span class="Constant">y:offset</span><span class="Delimiter">,</span> <span class="Constant">34</span>
  <span class="Constant">3</span>:&amp;:point<span class="Special"> &lt;- </span>bar <span class="Constant">1</span>:&amp;:point  <span class="Comment"># specialize _t to address:point</span>
  <span class="Constant">4</span>:point<span class="Special"> &lt;- </span>copy *<span class="Constant">3</span>:&amp;:point
]
<span class="muRecipe">def</span> bar a:_t<span class="muRecipe"> -&gt; </span>result:_t [
  local-scope
  load-ingredients
  result<span class="Special"> &lt;- </span>copy a
]
<span class="traceContains">+mem: storing 34 in location 5</span>

<span class="Comment">//: specializing a type ingredient with a compound type -- while *inside* another compound type</span>
<span class="Delimiter">:(scenario shape_shifting_recipe_supports_compound_types_2)</span>
<span class="muData">container</span> foo:_t [
  <span class="Normal">value</span>:_t
]
<span class="muRecipe">def</span> bar x:&amp;:foo:_t<span class="muRecipe"> -&gt; </span>result:_t [
  local-scope
  load-ingredients
  result<span class="Special"> &lt;- </span>get *x<span class="Delimiter">,</span> <span class="Constant">value:offset</span>
]
<span class="muRecipe">def</span> main [
  <span class="Constant">1</span>:&amp;:foo:&amp;:point<span class="Special"> &lt;- </span><span class="Normal">new</span> <span class="Delimiter">{(</span>foo address point<span class="Delimiter">)</span>: type<span class="Delimiter">}</span>
  <span class="Constant">2</span>:&amp;:point<span class="Special"> &lt;- </span>bar <span class="Constant">1</span>:&amp;:foo:&amp;:point
]
<span class="Comment"># no errors; call to 'bar' successfully specialized</span>

<span class="Delimiter">:(scenario shape_shifting_recipe_error)</span>
<span class="Special">% Hide_errors = true;</span>
<span class="muRecipe">def</span> main [
  <span class="Normal">a</span>:num<span class="Special"> &lt;- </span>copy <span class="Constant">3</span>
  <span class="Normal">b</span>:&amp;:num<span class="Special"> &lt;- </span>foo a
]
<span class="muRecipe">def</span> foo a:_t<span class="muRecipe"> -&gt; </span>b:_t [
  load-ingredients
  b<span class="Special"> &lt;- </span>copy a
]
<span class="traceContains">+error: main: no call found for 'b:&amp;:num &lt;- foo a'</span>

<span class="Delimiter">:(scenario specialize_inside_recipe_without_header)</span>
<span class="muRecipe">def</span> main [
  foo <span class="Constant">3</span>
]
<span class="muRecipe">def</span> foo [
  local-scope
  <span class="Normal">x</span>:num<span class="Special"> &lt;- </span>next-ingredient  <span class="Comment"># ensure no header</span>
  <span class="Constant">1</span>:num/<span class="Special">raw &lt;- </span>bar x  <span class="Comment"># call a shape-shifting recipe</span>
]
<span class="muRecipe">def</span> bar x:_elem<span class="muRecipe"> -&gt; </span>y:_elem [
  local-scope
  load-ingredients
  y<span class="Special"> &lt;- </span>add x<span class="Delimiter">,</span> <span class="Constant">1</span>
]
<span class="traceContains">+mem: storing 4 in location 1</span>

<span class="Delimiter">:(scenario specialize_with_literal)</span>
<span class="muRecipe">def</span> main [
  local-scope
  <span class="Comment"># permit literal to map to number</span>
  <span class="Constant">1</span>:num/<span class="Special">raw &lt;- </span>foo <span class="Constant">3</span>
]
<span class="muRecipe">def</span> foo x:_elem<span class="muRecipe"> -&gt; </span>y:_elem [
  local-scope
  load-ingredients
  y<span class="Special"> &lt;- </span>add x<span class="Delimiter">,</span> <span class="Constant">1</span>
]
<span class="traceContains">+mem: storing 4 in location 1</span>

<span class="Delimiter">:(scenario specialize_with_literal_2)</span>
<span class="muRecipe">def</span> main [
  local-scope
  <span class="Comment"># permit literal to map to character</span>
  <span class="Constant">1</span>:<span class="Normal">char</span>/<span class="Special">raw &lt;- </span>foo <span class="Constant">3</span>
]
<span class="muRecipe">def</span> foo x:_elem<span class="muRecipe"> -&gt; </span>y:_elem [
  local-scope
  load-ingredients
  y<span class="Special"> &lt;- </span>add x<span class="Delimiter">,</span> <span class="Constant">1</span>
]
<span class="traceContains">+mem: storing 4 in location 1</span>

<span class="Delimiter">:(scenario specialize_with_literal_3)</span>
<span class="muRecipe">def</span> main [
  local-scope
  <span class="Comment"># permit '0' to map to address to shape-shifting type-ingredient</span>
  <span class="Constant">1</span>:&amp;:<span class="Normal">char</span>/<span class="Special">raw &lt;- </span>foo <span class="Constant">0</span>
]
<span class="muRecipe">def</span> foo x:&amp;:_elem<span class="muRecipe"> -&gt; </span>y:&amp;:_elem [
  local-scope
  load-ingredients
  y<span class="Special"> &lt;- </span>copy x
]
<span class="traceContains">+mem: storing 0 in location 1</span>
$error: <span class="Constant">0</span>

<span class="Delimiter">:(scenario specialize_with_literal_4)</span>
<span class="Special">% Hide_errors = true;</span>
<span class="muRecipe">def</span> main [
  local-scope
  <span class="Comment"># ambiguous call: what's the type of its ingredient?!</span>
  foo <span class="Constant">0</span>
]
<span class="muRecipe">def</span> foo x:&amp;:_elem<span class="muRecipe"> -&gt; </span>y:&amp;:_elem [
  local-scope
  load-ingredients
  y<span class="Special"> &lt;- </span>copy x
]
<span class="traceContains">+error: main: instruction 'foo' has no valid specialization</span>

<span class="Delimiter">:(scenario specialize_with_literal_5)</span>
<span class="muRecipe">def</span> main [
  foo <span class="Constant">3</span><span class="Delimiter">,</span> <span class="Constant">4</span>  <span class="Comment"># recipe mapping two variables to literals</span>
]
<span class="muRecipe">def</span> foo x:_elem<span class="Delimiter">,</span> y:_elem [
  local-scope
  load-ingredients
  <span class="Constant">1</span>:num/<span class="Special">raw &lt;- </span>add x<span class="Delimiter">,</span> y
]
<span class="traceContains">+mem: storing 7 in location 1</span>

<span class="Delimiter">:(scenario multiple_shape_shifting_variants)</span>
<span class="Comment"># try to call two different shape-shifting recipes with the same name</span>
<span class="muRecipe">def</span> main [
  <span class="Normal">e1</span>:d1:num<span class="Special"> &lt;- </span>merge <span class="Constant">3</span>
  <span class="Normal">e2</span>:d2:num<span class="Special"> &lt;- </span>merge <span class="Constant">4</span><span class="Delimiter">,</span> <span class="Constant">5</span>
  <span class="Constant">1</span>:num/<span class="Special">raw &lt;- </span>foo e1
  <span class="Constant">2</span>:num/<span class="Special">raw &lt;- </span>foo e2
]
<span class="Comment"># the two shape-shifting definitions</span>
<span class="muRecipe">def</span> foo a:d1:_elem<span class="muRecipe"> -&gt; </span>b:num [
  local-scope
  load-ingredients
  <span class="Identifier">return</span> <span class="Constant">34</span>
]
<span class="muRecipe">def</span> foo a:d2:_elem<span class="muRecipe"> -&gt; </span>b:num [
  local-scope
  load-ingredients
  <span class="Identifier">return</span> <span class="Constant">35</span>
]
<span class="Comment"># the shape-shifting containers they use</span>
<span class="muData">container</span> d1:_elem [
  <span class="Normal">x</span>:_elem
]
<span class="muData">container</span> d2:_elem [
  <span class="Normal">x</span>:num
  <span class="Normal">y</span>:_elem
]
<span class="traceContains">+mem: storing 34 in location 1</span>
<span class="traceContains">+mem: storing 35 in location 2</span>

<span class="Delimiter">:(scenario multiple_shape_shifting_variants_2)</span>
<span class="Comment"># static dispatch between shape-shifting variants, _including pointer lookups_</span>
<span class="muRecipe">def</span> main [
  <span class="Normal">e1</span>:d1:num<span class="Special"> &lt;- </span>merge <span class="Constant">3</span>
  <span class="Normal">e2</span>:&amp;:d2:num<span class="Special"> &lt;- </span><span class="Normal">new</span> <span class="Delimiter">{(</span>d2 number<span class="Delimiter">)</span>: type<span class="Delimiter">}</span>
  <span class="Constant">1</span>:num/<span class="Special">raw &lt;- </span>foo e1
  <span class="Constant">2</span>:num/<span class="Special">raw &lt;- </span>foo *e2  <span class="Comment"># different from previous scenario</span>
]
<span class="muRecipe">def</span> foo a:d1:_elem<span class="muRecipe"> -&gt; </span>b:num [
  local-scope
  load-ingredients
  <span class="Identifier">return</span> <span class="Constant">34</span>
]
<span class="muRecipe">def</span> foo a:d2:_elem<span class="muRecipe"> -&gt; </span>b:num [
  local-scope
  load-ingredients
  <span class="Identifier">return</span> <span class="Constant">35</span>
]
<span class="muData">container</span> d1:_elem [
  <span class="Normal">x</span>:_elem
]
<span class="muData">container</span> d2:_elem [
  <span class="Normal">x</span>:num
  <span class="Normal">y</span>:_elem
]
<span class="traceContains">+mem: storing 34 in location 1</span>
<span class="traceContains">+mem: storing 35 in location 2</span>

<span class="Delimiter">:(scenario missing_type_in_shape_shifting_recipe)</span>
<span class="Special">% Hide_errors = true;</span>
<span class="muRecipe">def</span> main [
  <span class="Normal">a</span>:d1:num<span class="Special"> &lt;- </span>merge <span class="Constant">3</span>
  foo a
]
<span class="muRecipe">def</span> foo a:d1:_elem<span class="muRecipe"> -&gt; </span>b:num [
  local-scope
  load-ingredients
  copy e  <span class="Comment"># no such variable</span>
  <span class="Identifier">return</span> <span class="Constant">34</span>
]
<span class="muData">container</span> d1:_elem [
  <span class="Normal">x</span>:_elem
]
<span class="traceContains">+error: foo: unknown type for 'e' in 'copy e' (check the name for typos)</span>
<span class="traceContains">+error: specializing foo: missing type for 'e'</span>
<span class="Comment"># and it doesn't crash</span>

<span class="Delimiter">:(scenario missing_type_in_shape_shifting_recipe_2)</span>
<span class="Special">% Hide_errors = true;</span>
<span class="muRecipe">def</span> main [
  <span class="Normal">a</span>:d1:num<span class="Special"> &lt;- </span>merge <span class="Constant">3</span>
  foo a
]
<span class="muRecipe">def</span> foo a:d1:_elem<span class="muRecipe"> -&gt; </span>b:num [
  local-scope
  load-ingredients
  get e<span class="Delimiter">,</span> <span class="Constant">x:offset</span>  <span class="Comment"># unknown variable in a 'get', which does some extra checking</span>
  <span class="Identifier">return</span> <span class="Constant">34</span>
]
<span class="muData">container</span> d1:_elem [
  <span class="Normal">x</span>:_elem
]
<span class="traceContains">+error: foo: unknown type for 'e' in 'get e, x:offset' (check the name for typos)</span>
<span class="traceContains">+error: specializing foo: missing type for 'e'</span>
<span class="Comment"># and it doesn't crash</span>

<span class="Delimiter">:(scenarios transform)</span>
<span class="Delimiter">:(scenario specialize_recursive_shape_shifting_recipe)</span>
<span class="muRecipe">def</span> main [
  <span class="Constant">1</span>:num<span class="Special"> &lt;- </span>copy <span class="Constant">34</span>
  <span class="Constant">2</span>:num<span class="Special"> &lt;- </span>foo <span class="Constant">1</span>:num
]
<span class="muRecipe">def</span> foo x:_elem<span class="muRecipe"> -&gt; </span>y:num [
  local-scope
  load-ingredients
  <span class="Delimiter">{</span>
    <span class="Identifier">break</span>
    <span class="Normal">y</span>:num<span class="Special"> &lt;- </span>foo x
  <span class="Delimiter">}</span>
  <span class="Identifier">return</span> y
]
<span class="traceContains">+transform: new specialization: foo_2</span>
<span class="Comment"># transform terminates</span>

<span class="Delimiter">:(scenarios run)</span>
<span class="Delimiter">:(scenario specialize_most_similar_variant)</span>
<span class="muRecipe">def</span> main [
  <span class="Constant">1</span>:&amp;:num<span class="Special"> &lt;- </span><span class="Normal">new</span> <span class="Constant">number:type</span>
  <span class="Constant">2</span>:num<span class="Special"> &lt;- </span>foo <span class="Constant">1</span>:&amp;:num
]
<span class="muRecipe">def</span> foo x:_elem<span class="muRecipe"> -&gt; </span>y:num [
  local-scope
  load-ingredients
  <span class="Identifier">return</span> <span class="Constant">34</span>
]
<span class="muRecipe">def</span> foo x:&amp;:_elem<span class="muRecipe"> -&gt; </span>y:num [
  local-scope
  load-ingredients
  <span class="Identifier">return</span> <span class="Constant">35</span>
]
<span class="traceContains">+mem: storing 35 in location 2</span>

<span class="Delimiter">:(scenario specialize_most_similar_variant_2)</span>
<span class="Comment"># version with headers padded with lots of unrelated concrete types</span>
<span class="muRecipe">def</span> main [
  <span class="Constant">1</span>:num<span class="Special"> &lt;- </span>copy <span class="Constant">23</span>
  <span class="Constant">2</span>:&amp;:@:num<span class="Special"> &lt;- </span>copy <span class="Constant">0</span>
  <span class="Constant">3</span>:num<span class="Special"> &lt;- </span>foo <span class="Constant">2</span>:&amp;:@:num<span class="Delimiter">,</span> <span class="Constant">1</span>:num
]
<span class="Comment"># variant with concrete type</span>
<span class="muRecipe">def</span> foo dummy:&amp;:@:num<span class="Delimiter">,</span> x:num<span class="muRecipe"> -&gt; </span>y:num<span class="Delimiter">,</span> dummy:&amp;:@:num [
  local-scope
  load-ingredients
  <span class="Identifier">return</span> <span class="Constant">34</span>
]
<span class="Comment"># shape-shifting variant</span>
<span class="muRecipe">def</span> foo dummy:&amp;:@:num<span class="Delimiter">,</span> x:_elem<span class="muRecipe"> -&gt; </span>y:num<span class="Delimiter">,</span> dummy:&amp;:@:num [
  local-scope
  load-ingredients
  <span class="Identifier">return</span> <span class="Constant">35</span>
]
<span class="Comment"># prefer the concrete variant</span>
<span class="traceContains">+mem: storing 34 in location 3</span>

<span class="Delimiter">:(scenario specialize_most_similar_variant_3)</span>
<span class="muRecipe">def</span> main [
  <span class="Constant">1</span>:text<span class="Special"> &lt;- </span><span class="Normal">new</span> [abc]
  foo <span class="Constant">1</span>:text
]
<span class="muRecipe">def</span> foo x:text [
  <span class="Constant">2</span>:num<span class="Special"> &lt;- </span>copy <span class="Constant">34</span>
]
<span class="muRecipe">def</span> foo x:&amp;:_elem [
  <span class="Constant">2</span>:num<span class="Special"> &lt;- </span>copy <span class="Constant">35</span>
]
<span class="Comment"># make sure the more precise version was used</span>
<span class="traceContains">+mem: storing 34 in location 2</span>

<span class="Delimiter">:(scenario specialize_literal_as_number)</span>
<span class="muRecipe">def</span> main [
  <span class="Constant">1</span>:num<span class="Special"> &lt;- </span>foo <span class="Constant">23</span>
]
<span class="muRecipe">def</span> foo x:_elem<span class="muRecipe"> -&gt; </span>y:num [
  local-scope
  load-ingredients
  <span class="Identifier">return</span> <span class="Constant">34</span>
]
<span class="muRecipe">def</span> foo x:<span class="Normal">char</span><span class="muRecipe"> -&gt; </span>y:num [
  local-scope
  load-ingredients
  <span class="Identifier">return</span> <span class="Constant">35</span>
]
<span class="traceContains">+mem: storing 34 in location 1</span>

<span class="Delimiter">:(scenario specialize_literal_as_number_2)</span>
<span class="Comment"># version calling with literal</span>
<span class="muRecipe">def</span> main [
  <span class="Constant">1</span>:num<span class="Special"> &lt;- </span>foo <span class="Constant">0</span>
]
<span class="Comment"># variant with concrete type</span>
<span class="muRecipe">def</span> foo x:num<span class="muRecipe"> -&gt; </span>y:num [
  local-scope
  load-ingredients
  <span class="Identifier">return</span> <span class="Constant">34</span>
]
<span class="Comment"># shape-shifting variant</span>
<span class="muRecipe">def</span> foo x:&amp;:_elem<span class="muRecipe"> -&gt; </span>y:num [
  local-scope
  load-ingredients
  <span class="Identifier">return</span> <span class="Constant">35</span>
]
<span class="Comment"># prefer the concrete variant, ignore concrete types in scoring the shape-shifting variant</span>
<span class="traceContains">+mem: storing 34 in location 1</span>

<span class="Delimiter">:(scenario specialize_literal_as_address)</span>
<span class="muRecipe">def</span> main [
  <span class="Constant">1</span>:num<span class="Special"> &lt;- </span>foo <span class="Constant">0</span>
]
<span class="Comment"># variant with concrete address type</span>
<span class="muRecipe">def</span> foo x:&amp;:num<span class="muRecipe"> -&gt; </span>y:num [
  local-scope
  load-ingredients
  <span class="Identifier">return</span> <span class="Constant">34</span>
]
<span class="Comment"># shape-shifting variant</span>
<span class="muRecipe">def</span> foo x:&amp;:_elem<span class="muRecipe"> -&gt; </span>y:num [
  local-scope
  load-ingredients
  <span class="Identifier">return</span> <span class="Constant">35</span>
]
<span class="Comment"># prefer the concrete variant, ignore concrete types in scoring the shape-shifting variant</span>
<span class="traceContains">+mem: storing 34 in location 1</span>

<span class="Delimiter">:(scenario missing_type_during_specialization)</span>
<span class="Special">% Hide_errors = true;</span>
<span class="Comment"># define a shape-shifting recipe</span>
<span class="muRecipe">def</span> foo a:_elem [
]
<span class="Comment"># define a container with field 'z'</span>
<span class="muData">container</span> foo2 [
  <span class="Normal">z</span>:num
]
<span class="muRecipe">def</span> main [
  local-scope
  <span class="Normal">x</span>:foo2<span class="Special"> &lt;- </span>merge <span class="Constant">34</span>
  <span class="Normal">y</span>:num<span class="Special"> &lt;- </span>get x<span class="Delimiter">,</span> z:offse  <span class="Comment"># typo in 'offset'</span>
<span class="PreProc">  </span><span class="Comment"># define a variable with the same name 'z'</span>
  <span class="Normal">z</span>:num<span class="Special"> &lt;- </span>copy <span class="Constant">34</span>
  <span class="Comment"># trigger specialization of the shape-shifting recipe</span>
  foo z
]
<span class="Comment"># shouldn't crash</span>

<span class="Delimiter">:(scenario missing_type_during_specialization2)</span>
<span class="Special">% Hide_errors = true;</span>
<span class="Comment"># define a shape-shifting recipe</span>
<span class="muRecipe">def</span> foo a:_elem [
]
<span class="Comment"># define a container with field 'z'</span>
<span class="muData">container</span> foo2 [
  <span class="Normal">z</span>:num
]
<span class="muRecipe">def</span> main [
  local-scope
  <span class="Normal">x</span>:foo2<span class="Special"> &lt;- </span>merge <span class="Constant">34</span>
  <span class="Normal">y</span>:num<span class="Special"> &lt;- </span>get x<span class="Delimiter">,</span> z:offse  <span class="Comment"># typo in 'offset'</span>
<span class="PreProc">  </span><span class="Comment"># define a variable with the same name 'z'</span>
  <span class="Normal">z</span>:&amp;:num<span class="Special"> &lt;- </span>copy <span class="Constant">34</span>
  <span class="Comment"># trigger specialization of the shape-shifting recipe</span>
  foo *z
]
<span class="Comment"># shouldn't crash</span>

<span class="Delimiter">:(scenario tangle_shape_shifting_recipe)</span>
<span class="Comment"># shape-shifting recipe</span>
<span class="muRecipe">def</span> foo a:_elem [
  local-scope
  load-ingredients
  &lt;label1&gt;
]
<span class="Comment"># tangle some code that refers to the type ingredient</span>
<span class="muRecipe">after</span> &lt;label1&gt; [
  <span class="Normal">b</span>:_elem<span class="Special"> &lt;- </span>copy a
]
<span class="Comment"># trigger specialization</span>
<span class="muRecipe">def</span> main [
  local-scope
  foo <span class="Constant">34</span>
]
$error: <span class="Constant">0</span>

<span class="Delimiter">:(scenario tangle_shape_shifting_recipe_with_type_abbreviation)</span>
<span class="Comment"># shape-shifting recipe</span>
<span class="muRecipe">def</span> foo a:_elem [
  local-scope
  load-ingredients
  &lt;label1&gt;
]
<span class="Comment"># tangle some code that refers to the type ingredient</span>
<span class="muRecipe">after</span> &lt;label1&gt; [
  <span class="Normal">b</span>:<span class="Normal">bool</span><span class="Special"> &lt;- </span>copy <span class="Constant">0</span>  <span class="Comment"># type abbreviation</span>
]
<span class="Comment"># trigger specialization</span>
<span class="muRecipe">def</span> main [
  local-scope
  foo <span class="Constant">34</span>
]
$error: <span class="Constant">0</span>

<span class="Delimiter">:(scenario shape_shifting_recipe_coexists_with_primitive)</span>
<span class="Comment"># recipe overloading a primitive with a generic type</span>
<span class="muRecipe">def</span> add a:&amp;:foo:_elem [
  assert <span class="Constant">0</span><span class="Delimiter">,</span> [should <span class="Normal">not</span> get here]
]
<span class="muRecipe">def</span> main [
  <span class="Comment"># call primitive add with literal 0</span>
  add <span class="Constant">0</span><span class="Delimiter">,</span> <span class="Constant">0</span>
]
$error: <span class="Constant">0</span>
</pre>
</body>
</html>
<!-- vim: set foldmethod=manual : -->