about summary refs log blame commit diff stats
path: root/html/053recipe_header.cc.html
blob: f3beba5bb7e5a8b3ac26ef3
package main

import "github.com/getwtxt/getwtxt/svc"

func main() {
	svc.Start()
}
m> 2016-03-09 02:56:27 -0800 committer Kartik K. Agaram <vc@akkartik.com> 2016-03-09 02:56:27 -0800 2743' href='/akkartik/mu/commit/html/056recipe_header.cc.html?h=main&id=4690ce81e079fc58cae8d6d583e5e3eb3ed81a83'>4690ce81 ^
a654e4ec ^
4690ce81 ^
a654e4ec ^




76755b28 ^

a654e4ec ^





76755b28 ^

a654e4ec ^
76755b28 ^



4690ce81 ^
f344b250 ^
76755b28 ^
f344b250 ^
76755b28 ^

f344b250 ^
4690ce81 ^
76755b28 ^





4690ce81 ^
76755b28 ^




dcc060c7 ^
4690ce81 ^
76755b28 ^




4690ce81 ^
76755b28 ^
4690ce81 ^
76755b28 ^
2f02189d ^

4690ce81 ^
76755b28 ^

67db19a0 ^
76755b28 ^
4690ce81 ^
76755b28 ^


67db19a0 ^
76755b28 ^




4690ce81 ^
f344b250 ^
76755b28 ^
f344b250 ^
76755b28 ^

f344b250 ^
4690ce81 ^
76755b28 ^



4690ce81 ^
76755b28 ^

4690ce81 ^
f344b250 ^
76755b28 ^
4690ce81 ^
f344b250 ^
76755b28 ^


2f02189d ^

f344b250 ^
2f02189d ^


67db19a0 ^

4690ce81 ^
67db19a0 ^
2f02189d ^
67db19a0 ^


4690ce81 ^
67db19a0 ^





2f02189d ^
67db19a0 ^


4690ce81 ^
67db19a0 ^





2f02189d ^
67db19a0 ^
76755b28 ^

a654e4ec ^
76755b28 ^

a654e4ec ^
76755b28 ^




4690ce81 ^
f344b250 ^
76755b28 ^


dcc060c7 ^
4690ce81 ^
76755b28 ^
a654e4ec ^
4690ce81 ^


76755b28 ^







4690ce81 ^
76755b28 ^


0ca56ed8 ^


0ca56ed8 ^

f344b250 ^

0ca56ed8 ^
f344b250 ^
0ca56ed8 ^











76755b28 ^


4690ce81 ^
76755b28 ^
d009e158 ^
a654e4ec ^
d009e158 ^

76755b28 ^




298f8065 ^



76755b28 ^
d009e158 ^





4690ce81 ^
d009e158 ^


4690ce81 ^
d009e158 ^
4690ce81 ^
d009e158 ^





4690ce81 ^
d009e158 ^

a654e4ec ^

d009e158 ^









4690ce81 ^
f344b250 ^
d009e158 ^
f344b250 ^
d009e158 ^

4690ce81 ^
d009e158 ^
f344b250 ^
d009e158 ^


4690ce81 ^

d009e158 ^
f344b250 ^
d009e158 ^

4690ce81 ^
d009e158 ^
4690ce81 ^
d009e158 ^



4690ce81 ^
4690ce81 ^
0ca56ed8 ^
a654e4ec ^
4690ce81 ^




b2566a84 ^
4690ce81 ^
c842d90b ^
d009e158 ^
4690ce81 ^

b2566a84 ^
4690ce81 ^
c842d90b ^
d009e158 ^



76755b28 ^




f344b250 ^
76755b28 ^

f344b250 ^
4690ce81 ^
76755b28 ^
4690ce81 ^
76755b28 ^
db1f56c8 ^
76755b28 ^


4690ce81 ^


76755b28 ^
a654e4ec ^
4690ce81 ^


c842d90b ^
dcc060c7 ^

a654e4ec ^
4690ce81 ^
c842d90b ^
76755b28 ^



dcc060c7 ^

f344b250 ^
dcc060c7 ^

f344b250 ^
4690ce81 ^
dcc060c7 ^
4690ce81 ^
dcc060c7 ^
76755b28 ^

f344b250 ^
76755b28 ^

4690ce81 ^
76755b28 ^
2f02189d ^
76755b28 ^

4690ce81 ^
76755b28 ^
db1f56c8 ^

76755b28 ^

4690ce81 ^
76755b28 ^
4690ce81 ^
d009e158 ^
76755b28 ^
a654e4ec ^
4690ce81 ^
2f02189d ^
76755b28 ^







4690ce81 ^
f344b250 ^
76755b28 ^
f344b250 ^
76755b28 ^


4690ce81 ^
76755b28 ^


db1f56c8 ^
76755b28 ^


4690ce81 ^
76755b28 ^
4690ce81 ^
76755b28 ^
4690ce81 ^
a654e4ec ^
2f02189d ^
76755b28 ^
dcc060c7 ^
76755b28 ^
a654e4ec ^
2f02189d ^
76755b28 ^
dcc060c7 ^
76755b28 ^
a654e4ec ^
76755b28 ^
dcc060c7 ^
a654e4ec ^
4690ce81 ^

76755b28 ^
2f02189d ^

dcc060c7 ^
76755b28 ^
a654e4ec ^
dcc060c7 ^
4690ce81 ^

76755b28 ^
2f02189d ^

dcc060c7 ^
76755b28 ^






dea90217 ^
4690ce81 ^
f344b250 ^
76755b28 ^
f344b250 ^
76755b28 ^


4690ce81 ^
76755b28 ^


db1f56c8 ^
76755b28 ^


4690ce81 ^
76755b28 ^
4690ce81 ^
76755b28 ^
a654e4ec ^
76755b28 ^
4690ce81 ^
76755b28 ^


4690ce81 ^

76755b28 ^






4690ce81 ^

76755b28 ^
a654e4ec ^
76755b28 ^
4690ce81 ^
76755b28 ^

4690ce81 ^
76755b28 ^

4690ce81 ^
76755b28 ^




4690ce81 ^
f344b250 ^
76755b28 ^
f344b250 ^
76755b28 ^



4690ce81 ^
76755b28 ^



dea90217 ^
4690ce81 ^
f344b250 ^
76755b28 ^
f344b250 ^
76755b28 ^



a654e4ec ^
76755b28 ^

dea90217 ^
4690ce81 ^
f344b250 ^
76755b28 ^
f344b250 ^
76755b28 ^


4690ce81 ^
76755b28 ^
a654e4ec ^
f344b250 ^
76755b28 ^

dea90217 ^

f344b250 ^
dea90217 ^
f344b250 ^
dea90217 ^






76755b28 ^

4690ce81 ^
f344b250 ^


76755b28 ^
f344b250 ^
76755b28 ^


f344b250 ^
d009e158 ^

4690ce81 ^

76755b28 ^


a654e4ec ^
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558



                                                                                          
                                       

                                                
                                  

                                                                                         

                       
                                                                                                 
                                                                                            

                                      
                                
                             
                                  
                                   
                            




                                                                           

        





                               

       
                         



                                                                                                           
          
                                                                                                                                                                                 
 
                                                                                                 

                  
                                                                                                              
                                          





                                                                      
                                                                           




                                                                                
                                                                           
                                                                                                                                                                                                                                            




                                                                                                                                                                                                                                                                                                                        
                                                                                                                                                                                                                      
                                                                                                                       
                                                                                                                                                                                                                                                                                                                                                                                                                                                                
                                                                                           

                                                                                                                                                                                                                                                                                                                              
                                                                                                                                                                                                                             

                                                                                                                                                                                                                                                                                                                                                                                                                                                                 
                                                                                                      
                                  
                                                                                                                                                                                                                                                                                                                                                                                                                                                                


                                                                                                                                                                                                                                                                                                                                                                                                                                                           
                                                                                                      




                                                                     
          
                                                                                                                                                                                 
 
                                                                                                                                 

                  
                                                                                                              
                                          



                                                                       
          

     
                                         
                                                                                                                                                                                
 
         
                                                                                                                                                                                


                                                                

                                                                     
                                                        


                                                                                        

                                                                         
        
                            
                                                                                


                                                                           
        





                                                                        
                                                                                


                                                                           
                                                





                                                                        
                                                                                
 

                                                                                                                                                                 
                                                                                                                                                                                                                                                                                                                                

                                                                                                                                                                                                                                                                                                                                             
                                                                                                                                                                                                                                                                                                                             




                                                                                                                                                                                                                                                                                                                                          
          
                                                                                                              


                                                                   
                                                                            
                                                                                                                                                                                 
                                                                                                                       
                                                                                                                                                                                                                                                                                                                                                                  


                                                                                                                                                                                                                                                                                                  







                                                                                                                                                                                    
                                                                                                                                                                                


                                                                                                                                                                                                                                                                                                                                                                                                                        


                                                                               

             

                                                                                                                                                                                  
 
                                                                                                               











                                                                                                                                                                                                                                                                                                                                                                      


                                                                                                                 
                                                                                                                                                                                                                                      
                                                                             
                                                                                                                                                                                                                       
                                                                                                                                                                                                                                                                                                                                                                        

                                                                                                                                                             




                                                                                                                                                                                                                                                                               



                                                                                                                                                                                                                                                                                                                                                           
 





                                                                                                                                                                                                                                                                      
                                                                                                       


                                                                                         
                                                                                                       
                                                                                                                                                                         
                                                                                                                                                                                                                                                                                                                   





                                                                                                                                                                                                                                                                                                                                                                                             
                                                                   

                                                                                                                                                  

                                                                                                                                                                                                                                                                                  









                                                                                                                                                                                                                                                         
          
                                                                                                             
 
                                                            

                  
                                                                         
 
                                                                                                        


                                                                         

                                                                                                               
 
                                                          

                  
                                          
 
                                                                                                       



                                                                                                                                                                                         
                                                                                                                                                                                                              
                                                                                                                                                                     
                                                                                                                                                                                                                                                                                                                                                                                       
                                                                                                                                                                                                                                                                                                                                                                  




                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
                                                                             
                                                                                                                                                                                                                                                                                                                                                                                                                               
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
                                    

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              
                                                                          
                                                                                                                                                                                                                                                                                                                                                                                                                         
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        



                                    




                                                                                                  
                                                                                                 

                  
                                                                                                                        
                                          
 
                                                                                          
 
                                                               


                                                                                                                                                                                                      


                                                                                                                                                                                                                                
                                                                                                                                                                                                                                                                                                                                                                                                              
                                                                                                                                                                                                                                                                                                                                                                         


                                                                                                                                                                                                                                                                                                                                                                               
                                                                                                                                                                                                                                                                                                                                                                                                                                                                            

                                                                              
                                                                                                                                                                                                                                                                                                                                                                              
                                                                                                                                                                                                                                                                                                                                                                                                                               
                                                                                                                                                                                                                                                                                                                                                                                                                                                                



                                    

                                                                       
                                                      

                  
                                                                                                                        
                                          
 
                                                                                                        
 

                                                                                   
                                                                                                 

                  
                                          
 
                                                                                    

                                                                      
                                                                                                                                  
 

                                                                                                                                                                                       

                                      
                                                                                                                                                                                                            
                                                                                                                                          
                                                                                                                                                                                                                                                                    
                                                                                                                                      
                                                                                                                                                                                                                                                                                                                                                                                                              
                                                                                                                                                                                                                                                                                                                                                                               
                                                                                                                                                                                                                                                                                                                                                                                                              
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            







                                                                                                                                                                                                                                                                                                                                                                        
          
                                                                                                                                                                                 
 
                                                                                                 


                                                                                                                            
                                          


                                                                
                                                                                   


                                                                                                                                                                                       
                                                                                                                                                                                                            
                                                                                                                                          
                                                                                                                                                                                                                                                                    
                                                                                                                                                                                                                                                                                                                                                                                            
                                                                                                                                            
                                                                                                                                                                                                                                                                                                                                                                               
                                                                                                                                                                                                                                                                                                                                                                                                      
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     
                                  
                                                                                                                                                                                                                                                                                                                                                                            
                                                                                                                                                                                                                                                                                                                                                                                                   
                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
                                  
                                                                                                                                                                                                                                                                                                                                                                         
                                                                                                                                                                                  
                                                                                                                                                                                                                                                                                                                                                                                                      
                                                                                                                                                                                                                                                                                                                                                                        

                                                                                                                                                                                                                                                                                                                                                                                                                               
                                                                                

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     
                                    
                                                                                                                                                                                                                                                                                                                                                                     
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  

                                                                                                                                                                                                                                                                                                                                                                                                                            
                                                                                

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               






                                                                                                           
                                                                 
          
                                                                                                                                                                                 
 
                                                                                                 


                                                                              
                                        


                                                                
                                                                                                 


                                                                                                                                                                                        
                                                                                                                                                                           
                                                                                                                                          
                                                                                                                                                                                                                                
                                                                                                                                                                                                                                                                                                                                                                                                                
                                                                                                                                                                                                                                                                                                                                                                         
                                                                                                                                                                                  
                                                                                                                                                                                                                                                                                              


                                                                                                                                            

                                                                                                                                                                                                                                                                                                                                                                                    






                                                                                                                                                                    

                                                                                                                                                                                                                                                                      
                                                                                         
                                                                                                                                                                                                                                                                                                                                                                            
                                                                                        
                                                                                                                                                                                                                           

                                                                                                                                                                                                                                                                                        
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     

                                                                                                                                                                                                                                                                                                                                                                 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             




                                                                        
          
                                                                                                                                                                                                                                                                                      
 
                                                                                                                                       



                                                                                   
                                                                            



                                                                 
                                                                                
          
                                                                                                                                                                                 
 
                                                                                                 



                                                                              
                                                                                         

                                                                
                                                                               
          
                                                                                                                                                                                 
 
                                                                                                 


                                                                                                                            
                                          
 
                                                                          
                                                                     

                                                                

                                                                                         
                                                                                                                                                                                 
 
                                                                                                 






                                                                                                                                                                                                            

                                                                                       
          


                                                                                                                                                                                     
 
                                                                                                 


                  
                                                                                                                            

                                                                 

                                                                          


       
                                     
<!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 - 053recipe_header.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; }
.Constant { color: #00a0a0; }
.traceAbsent { color: #c00000; }
.cSpecial { color: #008000; }
.traceContains { color: #008000; }
.SalientComment { color: #00ffff; }
.Comment { color: #9090ff; }
.Delimiter { color: #800080; }
.Special { color: #c00000; }
.Identifier { color: #fcb165; }
.Normal { color: #eeeeee; background-color: #080808; padding-bottom: 1px; }
.Error { color: #ffffff; background-color: #ff6060; padding-bottom: 1px; }
-->
</style>

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

-->
</script>
</head>
<body>
<pre id='vimCodeElement'>
<span class="Comment">//: Advanced notation for the common/easy case where a recipe takes some fixed</span>
<span class="Comment">//: number of ingredients and yields some fixed number of products.</span>

<span class="Delimiter">:(scenario recipe_with_header)</span>
def main [
  <span class="Constant">1</span>:num/<span class="Special">raw &lt;- </span>add2 <span class="Constant">3</span><span class="Delimiter">,</span> <span class="Constant">5</span>
]
def add2 x:num<span class="Delimiter">,</span> y:num <span class="Delimiter">-&gt;</span> z:num [
  local-scope
  load-ingredients
  <span class="Normal">z</span>:num<span class="Special"> &lt;- </span>add x<span class="Delimiter">,</span> y
  <span class="Identifier">return</span> z
]
<span class="traceContains">+mem: storing 8 in location 1</span>

<span class="Comment">//: When loading recipes save any header.</span>

<span class="Delimiter">:(before &quot;End recipe Fields&quot;)</span>
<span class="Normal">bool</span> has_header<span class="Delimiter">;</span>
vector&lt;reagent&gt; ingredients<span class="Delimiter">;</span>
vector&lt;reagent&gt; products<span class="Delimiter">;</span>
<span class="Delimiter">:(before &quot;End recipe Constructor&quot;)</span>
has_header = <span class="Constant">false</span><span class="Delimiter">;</span>

<span class="Delimiter">:(before &quot;End Recipe Refinements&quot;)</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>in<span class="Delimiter">.</span>peek<span class="Delimiter">()</span> != <span class="Constant">'['</span><span class="Delimiter">)</span> <span class="Delimiter">{</span>
  trace<span class="Delimiter">(</span><span class="Constant">9999</span><span class="Delimiter">,</span> <span class="Constant">&quot;parse&quot;</span><span class="Delimiter">)</span> &lt;&lt; <span class="Constant">&quot;recipe has a header; parsing&quot;</span> &lt;&lt; end<span class="Delimiter">();</span>
  load_recipe_header<span class="Delimiter">(</span>in<span class="Delimiter">,</span> result<span class="Delimiter">);</span>
<span class="Delimiter">}</span>

<span class="Delimiter">:(code)</span>
<span class="Normal">void</span> load_recipe_header<span class="Delimiter">(</span>istream&amp; in<span class="Delimiter">,</span> recipe&amp; result<span class="Delimiter">)</span> <span class="Delimiter">{</span>
  result<span class="Delimiter">.</span>has_header = <span class="Constant">true</span><span class="Delimiter">;</span>
  <span class="Normal">while</span> <span class="Delimiter">(</span>has_data<span class="Delimiter">(</span>in<span class="Delimiter">)</span> &amp;&amp; in<span class="Delimiter">.</span>peek<span class="Delimiter">()</span> != <span class="Constant">'['</span> &amp;&amp; in<span class="Delimiter">.</span>peek<span class="Delimiter">()</span> != <span class="cSpecial">'\n'</span><span class="Delimiter">)</span> <span class="Delimiter">{</span>
    string s = next_word<span class="Delimiter">(</span>in<span class="Delimiter">);</span>
    <span class="Normal">if</span> <span class="Delimiter">(</span>s == <span class="Constant">&quot;&lt;-&quot;</span><span class="Delimiter">)</span>
      raise &lt;&lt; <span class="Constant">&quot;recipe &quot;</span> &lt;&lt; result<span class="Delimiter">.</span>name &lt;&lt; <span class="Constant">&quot; should say '-&gt;' and not '&lt;-'</span><span class="cSpecial">\n</span><span class="Constant">&quot;</span> &lt;&lt; end<span class="Delimiter">();</span>
    <span class="Normal">if</span> <span class="Delimiter">(</span>s == <span class="Constant">&quot;-&gt;&quot;</span><span class="Delimiter">)</span> <span class="Identifier">break</span><span class="Delimiter">;</span>
    result<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>push_back<span class="Delimiter">(</span>reagent<span class="Delimiter">(</span>s<span class="Delimiter">));</span>
    trace<span class="Delimiter">(</span><span class="Constant">9999</span><span class="Delimiter">,</span> <span class="Constant">&quot;parse&quot;</span><span class="Delimiter">)</span> &lt;&lt; <span class="Constant">&quot;header ingredient: &quot;</span> &lt;&lt; result<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>back<span class="Delimiter">().</span>original_string &lt;&lt; end<span class="Delimiter">();</span>
    skip_whitespace_but_not_newline<span class="Delimiter">(</span>in<span class="Delimiter">);</span>
  <span class="Delimiter">}</span>
  <span class="Normal">while</span> <span class="Delimiter">(</span>has_data<span class="Delimiter">(</span>in<span class="Delimiter">)</span> &amp;&amp; in<span class="Delimiter">.</span>peek<span class="Delimiter">()</span> != <span class="Constant">'['</span> &amp;&amp; in<span class="Delimiter">.</span>peek<span class="Delimiter">()</span> != <span class="cSpecial">'\n'</span><span class="Delimiter">)</span> <span class="Delimiter">{</span>
    string s = next_word<span class="Delimiter">(</span>in<span class="Delimiter">);</span>
    result<span class="Delimiter">.</span>products<span class="Delimiter">.</span>push_back<span class="Delimiter">(</span>reagent<span class="Delimiter">(</span>s<span class="Delimiter">));</span>
    trace<span class="Delimiter">(</span><span class="Constant">9999</span><span class="Delimiter">,</span> <span class="Constant">&quot;parse&quot;</span><span class="Delimiter">)</span> &lt;&lt; <span class="Constant">&quot;header product: &quot;</span> &lt;&lt; result<span class="Delimiter">.</span>products<span class="Delimiter">.</span>back<span class="Delimiter">().</span>original_string &lt;&lt; end<span class="Delimiter">();</span>
    skip_whitespace_but_not_newline<span class="Delimiter">(</span>in<span class="Delimiter">);</span>
  <span class="Delimiter">}</span>
  <span class="Comment">// End Load Recipe Header(result)</span>
<span class="Delimiter">}</span>

<span class="Delimiter">:(scenario recipe_handles_stray_comma)</span>
def main [
  <span class="Constant">1</span>:num/<span class="Special">raw &lt;- </span>add2 <span class="Constant">3</span><span class="Delimiter">,</span> <span class="Constant">5</span>
]
def add2 x:num<span class="Delimiter">,</span> y:num <span class="Delimiter">-&gt;</span> z:num<span class="Delimiter">,</span> [
  local-scope
  load-ingredients
  <span class="Normal">z</span>:num<span class="Special"> &lt;- </span>add x<span class="Delimiter">,</span> y
  <span class="Identifier">return</span> z
]
<span class="traceContains">+mem: storing 8 in location 1</span>

<span class="Delimiter">:(scenario recipe_handles_stray_comma_2)</span>
def main [
  foo
]
def foo<span class="Delimiter">,</span> [
  <span class="Constant">1</span>:num/<span class="Special">raw &lt;- </span>add <span class="Constant">2</span><span class="Delimiter">,</span> <span class="Constant">2</span>
]
def bar [
  <span class="Constant">1</span>:num/<span class="Special">raw &lt;- </span>add <span class="Constant">2</span><span class="Delimiter">,</span> <span class="Constant">3</span>
]
<span class="traceContains">+mem: storing 4 in location 1</span>

<span class="Delimiter">:(scenario recipe_handles_wrong_arrow)</span>
<span class="Special">% Hide_errors = true;</span>
def foo a:num<span class="Special"> &lt;- </span>b:num [
]
<span class="traceContains">+error: recipe foo should say '-&gt;' and not '&lt;-'</span>

<span class="Delimiter">:(scenario recipe_handles_missing_bracket)</span>
<span class="Special">% Hide_errors = true;</span>
def main
<span class="Error">]</span>
<span class="traceContains">+error: main: recipe body must begin with '['</span>

<span class="Delimiter">:(scenario recipe_handles_missing_bracket_2)</span>
<span class="Special">% Hide_errors = true;</span>
def main
  local-scope
  <span class="Delimiter">{</span>
  <span class="Delimiter">}</span>
<span class="Error">]</span>
<span class="Comment"># doesn't overflow line when reading header</span>
<span class="traceAbsent">-parse: header ingredient: local-scope</span>
<span class="traceContains">+error: main: recipe body must begin with '['</span>

<span class="Delimiter">:(scenario recipe_handles_missing_bracket_3)</span>
<span class="Special">% Hide_errors = true;</span>
def main  <span class="Comment"># comment</span>
  local-scope
  <span class="Delimiter">{</span>
  <span class="Delimiter">}</span>
<span class="Error">]</span>
<span class="Comment"># doesn't overflow line when reading header</span>
<span class="traceAbsent">-parse: header ingredient: local-scope</span>
<span class="traceContains">+error: main: recipe body must begin with '['</span>

<span class="Delimiter">:(after &quot;Begin debug_string(recipe x)&quot;)</span>
out &lt;&lt; <span class="Constant">&quot;ingredients:</span><span class="cSpecial">\n</span><span class="Constant">&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>x<span class="Delimiter">.</span>ingredients<span class="Delimiter">);</span> ++i<span class="Delimiter">)</span>
  out &lt;&lt; <span class="Constant">&quot;  &quot;</span> &lt;&lt; debug_string<span class="Delimiter">(</span>x<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">))</span> &lt;&lt; <span class="cSpecial">'\n'</span><span class="Delimiter">;</span>
out &lt;&lt; <span class="Constant">&quot;products:</span><span class="cSpecial">\n</span><span class="Constant">&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>x<span class="Delimiter">.</span>products<span class="Delimiter">);</span> ++i<span class="Delimiter">)</span>
  out &lt;&lt; <span class="Constant">&quot;  &quot;</span> &lt;&lt; debug_string<span class="Delimiter">(</span>x<span class="Delimiter">.</span>products<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">))</span> &lt;&lt; <span class="cSpecial">'\n'</span><span class="Delimiter">;</span>

<span class="Comment">//: If a recipe never mentions any ingredients or products, assume it has a header.</span>

<span class="Delimiter">:(scenario recipe_without_ingredients_or_products_has_header)</span>
def test [
  <span class="Constant">1</span>:num<span class="Special"> &lt;- </span>copy <span class="Constant">34</span>
]
<span class="traceContains">+parse: recipe test has a header</span>

<span class="Delimiter">:(before &quot;End Recipe Body(result)&quot;)</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>!result<span class="Delimiter">.</span>has_header<span class="Delimiter">)</span> <span class="Delimiter">{</span>
  result<span class="Delimiter">.</span>has_header = <span class="Constant">true</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>result<span class="Delimiter">.</span>steps<span class="Delimiter">);</span> ++i<span class="Delimiter">)</span> <span class="Delimiter">{</span>
    <span class="Normal">const</span> instruction&amp; inst = result<span class="Delimiter">.</span>steps<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">);</span>
    <span class="Normal">if</span> <span class="Delimiter">((</span>inst<span class="Delimiter">.</span>name == <span class="Constant">&quot;reply&quot;</span> &amp;&amp; !inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>empty<span class="Delimiter">())</span>
        || <span class="Delimiter">(</span>inst<span class="Delimiter">.</span>name == <span class="Constant">&quot;return&quot;</span> &amp;&amp; !inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>empty<span class="Delimiter">())</span>
        || inst<span class="Delimiter">.</span>name == <span class="Constant">&quot;next-ingredient&quot;</span>
        || inst<span class="Delimiter">.</span>name == <span class="Constant">&quot;ingredient&quot;</span>
        || inst<span class="Delimiter">.</span>name == <span class="Constant">&quot;rewind-ingredients&quot;</span><span class="Delimiter">)</span> <span class="Delimiter">{</span>
      result<span class="Delimiter">.</span>has_header = <span class="Constant">false</span><span class="Delimiter">;</span>
      <span class="Identifier">break</span><span class="Delimiter">;</span>
    <span class="Delimiter">}</span>
  <span class="Delimiter">}</span>
<span class="Delimiter">}</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>result<span class="Delimiter">.</span>has_header<span class="Delimiter">)</span> <span class="Delimiter">{</span>
  trace<span class="Delimiter">(</span><span class="Constant">9999</span><span class="Delimiter">,</span> <span class="Constant">&quot;parse&quot;</span><span class="Delimiter">)</span> &lt;&lt; <span class="Constant">&quot;recipe &quot;</span> &lt;&lt; result<span class="Delimiter">.</span>name &lt;&lt; <span class="Constant">&quot; has a header&quot;</span> &lt;&lt; end<span class="Delimiter">();</span>
<span class="Delimiter">}</span>

<span class="Comment">//: Support type abbreviations in headers.</span>

<span class="Delimiter">:(scenario type_abbreviations_in_recipe_headers)</span>
def main [
  local-scope
  <span class="Normal">a</span>:text<span class="Special"> &lt;- </span>foo
  <span class="Constant">1</span>:<span class="Normal">char</span>/<span class="Special">raw &lt;- </span>index *a<span class="Delimiter">,</span> <span class="Constant">0</span>
]
def foo <span class="Delimiter">-&gt;</span> a:text [  <span class="Comment"># 'text' is an abbreviation</span>
  local-scope
  load-ingredients
  a<span class="Special"> &lt;- </span><span class="Normal">new</span> [abc]
]
<span class="traceContains">+mem: storing 97 in location 1</span>

<span class="Delimiter">:(before &quot;End Expand Type Abbreviations(caller)&quot;)</span>
<span class="Normal">for</span> <span class="Delimiter">(</span><span class="Normal">long</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>
  expand_type_abbreviations<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>
<span class="Normal">for</span> <span class="Delimiter">(</span><span class="Normal">long</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>
  expand_type_abbreviations<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>

<span class="Comment">//: Rewrite 'load-ingredients' to instructions to create all reagents in the header.</span>

<span class="Delimiter">:(before &quot;End Rewrite Instruction(curr, recipe result)&quot;)</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>curr<span class="Delimiter">.</span>name == <span class="Constant">&quot;load-ingredients&quot;</span><span class="Delimiter">)</span> <span class="Delimiter">{</span>
  curr<span class="Delimiter">.</span>clear<span class="Delimiter">();</span>
  recipe_ordinal op = get<span class="Delimiter">(</span>Recipe_ordinal<span class="Delimiter">,</span> <span class="Constant">&quot;next-ingredient-without-typechecking&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>result<span class="Delimiter">.</span>ingredients<span class="Delimiter">);</span> ++i<span class="Delimiter">)</span> <span class="Delimiter">{</span>
    curr<span class="Delimiter">.</span>operation = op<span class="Delimiter">;</span>
    curr<span class="Delimiter">.</span>name = <span class="Constant">&quot;next-ingredient-without-typechecking&quot;</span><span class="Delimiter">;</span>
    curr<span class="Delimiter">.</span>products<span class="Delimiter">.</span>push_back<span class="Delimiter">(</span>result<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">));</span>
    result<span class="Delimiter">.</span>steps<span class="Delimiter">.</span>push_back<span class="Delimiter">(</span>curr<span class="Delimiter">);</span>
    curr<span class="Delimiter">.</span>clear<span class="Delimiter">();</span>
  <span class="Delimiter">}</span>
<span class="Delimiter">}</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>curr<span class="Delimiter">.</span>name == <span class="Constant">&quot;next-ingredient-without-typechecking&quot;</span><span class="Delimiter">)</span> <span class="Delimiter">{</span>
  raise &lt;&lt; maybe<span class="Delimiter">(</span>result<span class="Delimiter">.</span>name<span class="Delimiter">)</span> &lt;&lt; <span class="Constant">&quot;never call 'next-ingredient-without-typechecking' directly</span><span class="cSpecial">\n</span><span class="Constant">&quot;</span> &lt;&lt; end<span class="Delimiter">();</span>
  curr<span class="Delimiter">.</span>clear<span class="Delimiter">();</span>
<span class="Delimiter">}</span>

<span class="Comment">//: internal version of next-ingredient; don't call this directly</span>
<span class="Delimiter">:(before &quot;End Primitive Recipe Declarations&quot;)</span>
NEXT_INGREDIENT_WITHOUT_TYPECHECKING<span class="Delimiter">,</span>
<span class="Delimiter">:(before &quot;End Primitive Recipe Numbers&quot;)</span>
put<span class="Delimiter">(</span>Recipe_ordinal<span class="Delimiter">,</span> <span class="Constant">&quot;next-ingredient-without-typechecking&quot;</span><span class="Delimiter">,</span> NEXT_INGREDIENT_WITHOUT_TYPECHECKING<span class="Delimiter">);</span>
<span class="Delimiter">:(before &quot;End Primitive Recipe Checks&quot;)</span>
<span class="Normal">case</span> NEXT_INGREDIENT_WITHOUT_TYPECHECKING: <span class="Delimiter">{</span>
  <span class="Identifier">break</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Delimiter">:(before &quot;End Primitive Recipe Implementations&quot;)</span>
<span class="Normal">case</span> NEXT_INGREDIENT_WITHOUT_TYPECHECKING: <span class="Delimiter">{</span>
  assert<span class="Delimiter">(</span>!Current_routine<span class="Delimiter">-&gt;</span>calls<span class="Delimiter">.</span>empty<span class="Delimiter">());</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>current_call<span class="Delimiter">().</span>next_ingredient_to_process &lt; SIZE<span class="Delimiter">(</span>current_call<span class="Delimiter">().</span>ingredient_atoms<span class="Delimiter">))</span> <span class="Delimiter">{</span>
    products<span class="Delimiter">.</span>push_back<span class="Delimiter">(</span>
        current_call<span class="Delimiter">().</span>ingredient_atoms<span class="Delimiter">.</span>at<span class="Delimiter">(</span>current_call<span class="Delimiter">().</span>next_ingredient_to_process<span class="Delimiter">));</span>
    assert<span class="Delimiter">(</span>SIZE<span class="Delimiter">(</span>products<span class="Delimiter">)</span> == <span class="Constant">1</span><span class="Delimiter">);</span>  products<span class="Delimiter">.</span>resize<span class="Delimiter">(</span><span class="Constant">2</span><span class="Delimiter">);</span>  <span class="Comment">// push a new vector</span>
    products<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">1</span><span class="Delimiter">).</span>push_back<span class="Delimiter">(</span><span class="Constant">1</span><span class="Delimiter">);</span>
    ++current_call<span class="Delimiter">().</span>next_ingredient_to_process<span class="Delimiter">;</span>
  <span class="Delimiter">}</span>
  <span class="Normal">else</span> <span class="Delimiter">{</span>
    products<span class="Delimiter">.</span>resize<span class="Delimiter">(</span><span class="Constant">2</span><span class="Delimiter">);</span>
    <span class="Comment">// pad the first product with sufficient zeros to match its type</span>
    <span class="Normal">int</span> size = size_of<span class="Delimiter">(</span>current_instruction<span class="Delimiter">().</span>products<span class="Delimiter">.</span>at<span class="Delimiter">(</span><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> ++i<span class="Delimiter">)</span>
      products<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">).</span>push_back<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">);</span>
    products<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">1</span><span class="Delimiter">).</span>push_back<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">);</span>
  <span class="Delimiter">}</span>
  <span class="Identifier">break</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>

<span class="SalientComment">//:: Check all calls against headers.</span>

<span class="Delimiter">:(scenario show_clear_error_on_bad_call)</span>
<span class="Special">% Hide_errors = true;</span>
def main [
  <span class="Constant">1</span>:num<span class="Special"> &lt;- </span>foo <span class="Constant">34</span>
]
def foo x:point <span class="Delimiter">-&gt;</span> y:num [
  local-scope
  load-ingredients
  <span class="Identifier">return</span> <span class="Constant">35</span>
]
<span class="traceContains">+error: main: ingredient 0 has the wrong type at '1:num &lt;- foo 34'</span>

<span class="Delimiter">:(scenario show_clear_error_on_bad_call_2)</span>
<span class="Special">% Hide_errors = true;</span>
def main [
  <span class="Constant">1</span>:point<span class="Special"> &lt;- </span>foo <span class="Constant">34</span>
]
def foo x:num <span class="Delimiter">-&gt;</span> y:num [
  local-scope
  load-ingredients
  <span class="Identifier">return</span> x
]
<span class="traceContains">+error: main: product 0 has the wrong type at '1:point &lt;- foo 34'</span>

<span class="Delimiter">:(after &quot;Transform.push_back(check_instruction)&quot;)</span>
Transform<span class="Delimiter">.</span>push_back<span class="Delimiter">(</span>check_calls_against_header<span class="Delimiter">);</span>  <span class="Comment">// idempotent</span>
<span class="Delimiter">:(code)</span>
<span class="Normal">void</span> check_calls_against_header<span class="Delimiter">(</span><span class="Normal">const</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>
  trace<span class="Delimiter">(</span><span class="Constant">9991</span><span class="Delimiter">,</span> <span class="Constant">&quot;transform&quot;</span><span class="Delimiter">)</span> &lt;&lt; <span class="Constant">&quot;--- type-check calls inside recipe &quot;</span> &lt;&lt; caller<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>caller<span class="Delimiter">.</span>steps<span class="Delimiter">);</span> ++i<span class="Delimiter">)</span> <span class="Delimiter">{</span>
    <span class="Normal">const</span> instruction&amp; inst = caller<span class="Delimiter">.</span>steps<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">);</span>
    <span class="Normal">if</span> <span class="Delimiter">(</span>inst<span class="Delimiter">.</span>operation &lt; MAX_PRIMITIVE_RECIPES<span class="Delimiter">)</span> <span class="Identifier">continue</span><span class="Delimiter">;</span>
    <span class="Normal">const</span> recipe&amp; callee = get<span class="Delimiter">(</span>Recipe<span class="Delimiter">,</span> inst<span class="Delimiter">.</span>operation<span class="Delimiter">);</span>
    <span class="Normal">if</span> <span class="Delimiter">(</span>!callee<span class="Delimiter">.</span>has_header<span class="Delimiter">)</span> <span class="Identifier">continue</span><span class="Delimiter">;</span>
    <span class="Normal">for</span> <span class="Delimiter">(</span><span class="Normal">long</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>callee<span class="Delimiter">.</span>ingredients<span class="Delimiter">));</span> ++i<span class="Delimiter">)</span> <span class="Delimiter">{</span>
      <span class="Comment">// ingredients coerced from call to callee</span>
      <span class="Normal">if</span> <span class="Delimiter">(</span>!types_coercible<span class="Delimiter">(</span>callee<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>
        raise &lt;&lt; maybe<span class="Delimiter">(</span>caller<span class="Delimiter">.</span>name<span class="Delimiter">)</span> &lt;&lt; <span class="Constant">&quot;ingredient &quot;</span> &lt;&lt; i &lt;&lt; <span class="Constant">&quot; has the wrong type at '&quot;</span> &lt;&lt; inst<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="Delimiter">}</span>
    <span class="Normal">for</span> <span class="Delimiter">(</span><span class="Normal">long</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>callee<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>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="Comment">// products coerced from callee to call</span>
      <span class="Normal">if</span> <span class="Delimiter">(</span>!types_coercible<span class="Delimiter">(</span>inst<span class="Delimiter">.</span>products<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">),</span> callee<span class="Delimiter">.</span>products<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<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;product &quot;</span> &lt;&lt; i &lt;&lt; <span class="Constant">&quot; has the wrong type at '&quot;</span> &lt;&lt; inst<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="Delimiter">}</span>
  <span class="Delimiter">}</span>
<span class="Delimiter">}</span>

<span class="SalientComment">//:: Check types going in and out of all recipes with headers.</span>

<span class="Delimiter">:(scenarios transform)</span>
<span class="Delimiter">:(scenario recipe_headers_are_checked)</span>
<span class="Special">% Hide_errors = true;</span>
def add2 x:num<span class="Delimiter">,</span> y:num <span class="Delimiter">-&gt;</span> z:num [
  local-scope
  load-ingredients
  <span class="Normal">z</span>:&amp;:num<span class="Special"> &lt;- </span>copy <span class="Constant">0</span>/unsafe
  <span class="Identifier">return</span> z
]
<span class="traceContains">+error: add2: replied with the wrong type at 'return z'</span>

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

<span class="Delimiter">:(code)</span>
<span class="Normal">void</span> check_reply_instructions_against_header<span class="Delimiter">(</span><span class="Normal">const</span> recipe_ordinal r<span class="Delimiter">)</span> <span class="Delimiter">{</span>
  <span class="Normal">const</span> recipe&amp; caller_recipe = get<span class="Delimiter">(</span>Recipe<span class="Delimiter">,</span> r<span class="Delimiter">);</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>!caller_recipe<span class="Delimiter">.</span>has_header<span class="Delimiter">)</span> <span class="Identifier">return</span><span class="Delimiter">;</span>
  trace<span class="Delimiter">(</span><span class="Constant">9991</span><span class="Delimiter">,</span> <span class="Constant">&quot;transform&quot;</span><span class="Delimiter">)</span> &lt;&lt; <span class="Constant">&quot;--- checking reply instructions against header for &quot;</span> &lt;&lt; caller_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>caller_recipe<span class="Delimiter">.</span>steps<span class="Delimiter">);</span> ++i<span class="Delimiter">)</span> <span class="Delimiter">{</span>
    <span class="Normal">const</span> instruction&amp; inst = caller_recipe<span class="Delimiter">.</span>steps<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">);</span>
    <span class="Normal">if</span> <span class="Delimiter">(</span>inst<span class="Delimiter">.</span>name != <span class="Constant">&quot;reply&quot;</span> &amp;&amp; inst<span class="Delimiter">.</span>name != <span class="Constant">&quot;return&quot;</span><span class="Delimiter">)</span> <span class="Identifier">continue</span><span class="Delimiter">;</span>
    <span class="Normal">if</span> <span class="Delimiter">(</span>SIZE<span class="Delimiter">(</span>caller_recipe<span class="Delimiter">.</span>products<span class="Delimiter">)</span> != SIZE<span class="Delimiter">(</span>inst<span class="Delimiter">.</span>ingredients<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;replied with the wrong number of products at '&quot;</span> &lt;&lt; inst<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">continue</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_recipe<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>!types_match<span class="Delimiter">(</span>caller_recipe<span class="Delimiter">.</span>products<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>
        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;replied with the wrong type at '&quot;</span> &lt;&lt; inst<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="Delimiter">}</span>
  <span class="Delimiter">}</span>
<span class="Delimiter">}</span>

<span class="Delimiter">:(scenario recipe_headers_are_checked_2)</span>
<span class="Special">% Hide_errors = true;</span>
def add2 x:num<span class="Delimiter">,</span> y:num [
  local-scope
  load-ingredients
  <span class="Normal">z</span>:&amp;:num<span class="Special"> &lt;- </span>copy <span class="Constant">0</span>/unsafe
  <span class="Identifier">return</span> z
]
<span class="traceContains">+error: add2: replied with the wrong number of products at 'return z'</span>

<span class="Delimiter">:(scenario recipe_headers_check_for_duplicate_names)</span>
<span class="Special">% Hide_errors = true;</span>
def add2 x:num<span class="Delimiter">,</span> x:num <span class="Delimiter">-&gt;</span> z:num [
  local-scope
  load-ingredients
  <span class="Identifier">return</span> z
]
<span class="traceContains">+error: add2: 'x' can't repeat in the ingredients</span>

<span class="Delimiter">:(before &quot;End recipe Fields&quot;)</span>
map&lt;string<span class="Delimiter">,</span> <span class="Normal">int</span>&gt; ingredient_index<span class="Delimiter">;</span>

<span class="Delimiter">:(after &quot;Begin Instruction Modifying Transforms&quot;)</span>
Transform<span class="Delimiter">.</span>push_back<span class="Delimiter">(</span>check_header_ingredients<span class="Delimiter">);</span>  <span class="Comment">// idempotent</span>

<span class="Delimiter">:(code)</span>
<span class="Normal">void</span> check_header_ingredients<span class="Delimiter">(</span><span class="Normal">const</span> recipe_ordinal r<span class="Delimiter">)</span> <span class="Delimiter">{</span>
  recipe&amp; caller_recipe = get<span class="Delimiter">(</span>Recipe<span class="Delimiter">,</span> r<span class="Delimiter">);</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>caller_recipe<span class="Delimiter">.</span>products<span class="Delimiter">.</span>empty<span class="Delimiter">())</span> <span class="Identifier">return</span><span class="Delimiter">;</span>
  caller_recipe<span class="Delimiter">.</span>ingredient_index<span class="Delimiter">.</span>clear<span class="Delimiter">();</span>
  trace<span class="Delimiter">(</span><span class="Constant">9991</span><span class="Delimiter">,</span> <span class="Constant">&quot;transform&quot;</span><span class="Delimiter">)</span> &lt;&lt; <span class="Constant">&quot;--- checking reply instructions against header for &quot;</span> &lt;&lt; caller_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>caller_recipe<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_key<span class="Delimiter">(</span>caller_recipe<span class="Delimiter">.</span>ingredient_index<span class="Delimiter">,</span> caller_recipe<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">).</span>name<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;'&quot;</span> &lt;&lt; caller_recipe<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">).</span>name &lt;&lt; <span class="Constant">&quot;' can't repeat in the ingredients</span><span class="cSpecial">\n</span><span class="Constant">&quot;</span> &lt;&lt; end<span class="Delimiter">();</span>
    put<span class="Delimiter">(</span>caller_recipe<span class="Delimiter">.</span>ingredient_index<span class="Delimiter">,</span> caller_recipe<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">).</span>name<span class="Delimiter">,</span> i<span class="Delimiter">);</span>
  <span class="Delimiter">}</span>
<span class="Delimiter">}</span>

<span class="Comment">//: Deduce types from the header if possible.</span>

<span class="Delimiter">:(scenarios run)</span>
<span class="Delimiter">:(scenario deduce_instruction_types_from_recipe_header)</span>
def main [
  <span class="Constant">1</span>:num/<span class="Special">raw &lt;- </span>add2 <span class="Constant">3</span><span class="Delimiter">,</span> <span class="Constant">5</span>
]
def add2 x:num<span class="Delimiter">,</span> y:num <span class="Delimiter">-&gt;</span> z:num [
  local-scope
  load-ingredients
  z<span class="Special"> &lt;- </span>add x<span class="Delimiter">,</span> y  <span class="Comment"># no type for z</span>
  <span class="Identifier">return</span> z
]
<span class="traceContains">+mem: storing 8 in location 1</span>

<span class="Delimiter">:(after &quot;Begin Type Modifying Transforms&quot;)</span>
Transform<span class="Delimiter">.</span>push_back<span class="Delimiter">(</span>deduce_types_from_header<span class="Delimiter">);</span>  <span class="Comment">// idempotent</span>

<span class="Delimiter">:(code)</span>
<span class="Normal">void</span> deduce_types_from_header<span class="Delimiter">(</span><span class="Normal">const</span> recipe_ordinal r<span class="Delimiter">)</span> <span class="Delimiter">{</span>
  recipe&amp; caller_recipe = get<span class="Delimiter">(</span>Recipe<span class="Delimiter">,</span> r<span class="Delimiter">);</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>caller_recipe<span class="Delimiter">.</span>products<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">9991</span><span class="Delimiter">,</span> <span class="Constant">&quot;transform&quot;</span><span class="Delimiter">)</span> &lt;&lt; <span class="Constant">&quot;--- deduce types from header for &quot;</span> &lt;&lt; caller_recipe<span class="Delimiter">.</span>name &lt;&lt; end<span class="Delimiter">();</span>
  map&lt;string<span class="Delimiter">,</span> <span class="Normal">const</span> type_tree*&gt; header_type<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_recipe<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>!caller_recipe<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">).</span>type<span class="Delimiter">)</span> <span class="Identifier">continue</span><span class="Delimiter">;</span>  <span class="Comment">// error handled elsewhere</span>
    put<span class="Delimiter">(</span>header_type<span class="Delimiter">,</span> caller_recipe<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">).</span>name<span class="Delimiter">,</span> caller_recipe<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<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; caller_recipe<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">).</span>name &lt;&lt; <span class="Constant">&quot; is &quot;</span> &lt;&lt; names_to_string<span class="Delimiter">(</span>caller_recipe<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">).</span>type<span class="Delimiter">)</span> &lt;&lt; end<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_recipe<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>!caller_recipe<span class="Delimiter">.</span>products<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">).</span>type<span class="Delimiter">)</span> <span class="Identifier">continue</span><span class="Delimiter">;</span>  <span class="Comment">// error handled elsewhere</span>
    put<span class="Delimiter">(</span>header_type<span class="Delimiter">,</span> caller_recipe<span class="Delimiter">.</span>products<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">).</span>name<span class="Delimiter">,</span> caller_recipe<span class="Delimiter">.</span>products<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<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; caller_recipe<span class="Delimiter">.</span>products<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">).</span>name &lt;&lt; <span class="Constant">&quot; is &quot;</span> &lt;&lt; names_to_string<span class="Delimiter">(</span>caller_recipe<span class="Delimiter">.</span>products<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">).</span>type<span class="Delimiter">)</span> &lt;&lt; end<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_recipe<span class="Delimiter">.</span>steps<span class="Delimiter">);</span> ++i<span class="Delimiter">)</span> <span class="Delimiter">{</span>
    instruction&amp; inst = caller_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">9992</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> i = <span class="Constant">0</span><span class="Delimiter">;</span> i &lt; SIZE<span class="Delimiter">(</span>inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">);</span> ++i<span class="Delimiter">)</span> <span class="Delimiter">{</span>
      <span class="Normal">if</span> <span class="Delimiter">(</span>inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">).</span>type<span class="Delimiter">)</span> <span class="Identifier">continue</span><span class="Delimiter">;</span>
      <span class="Normal">if</span> <span class="Delimiter">(</span>header_type<span class="Delimiter">.</span>find<span class="Delimiter">(</span>inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">).</span>name<span class="Delimiter">)</span> == header_type<span class="Delimiter">.</span>end<span class="Delimiter">())</span>
        <span class="Identifier">continue</span><span class="Delimiter">;</span>
      <span class="Normal">if</span> <span class="Delimiter">(</span>!contains_key<span class="Delimiter">(</span>header_type<span class="Delimiter">,</span> inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">).</span>name<span class="Delimiter">))</span> <span class="Identifier">continue</span><span class="Delimiter">;</span>  <span class="Comment">// error handled elsewhere</span>
      inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">).</span>type = <span class="Normal">new</span> type_tree<span class="Delimiter">(</span>*get<span class="Delimiter">(</span>header_type<span class="Delimiter">,</span> inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">).</span>name<span class="Delimiter">));</span>
      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; inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">).</span>name &lt;&lt; <span class="Constant">&quot; is &quot;</span> &lt;&lt; names_to_string<span class="Delimiter">(</span>inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">).</span>type<span class="Delimiter">)</span> &lt;&lt; end<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>inst<span class="Delimiter">.</span>products<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;  product: &quot;</span> &lt;&lt; to_string<span class="Delimiter">(</span>inst<span class="Delimiter">.</span>products<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">))</span> &lt;&lt; end<span class="Delimiter">();</span>
      <span class="Normal">if</span> <span class="Delimiter">(</span>inst<span class="Delimiter">.</span>products<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">).</span>type<span class="Delimiter">)</span> <span class="Identifier">continue</span><span class="Delimiter">;</span>
      <span class="Normal">if</span> <span class="Delimiter">(</span>header_type<span class="Delimiter">.</span>find<span class="Delimiter">(</span>inst<span class="Delimiter">.</span>products<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">).</span>name<span class="Delimiter">)</span> == header_type<span class="Delimiter">.</span>end<span class="Delimiter">())</span>
        <span class="Identifier">continue</span><span class="Delimiter">;</span>
      <span class="Normal">if</span> <span class="Delimiter">(</span>!contains_key<span class="Delimiter">(</span>header_type<span class="Delimiter">,</span> inst<span class="Delimiter">.</span>products<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">).</span>name<span class="Delimiter">))</span> <span class="Identifier">continue</span><span class="Delimiter">;</span>  <span class="Comment">// error handled elsewhere</span>
      inst<span class="Delimiter">.</span>products<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">).</span>type = <span class="Normal">new</span> type_tree<span class="Delimiter">(</span>*get<span class="Delimiter">(</span>header_type<span class="Delimiter">,</span> inst<span class="Delimiter">.</span>products<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">).</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; <span class="Constant">&quot;type of &quot;</span> &lt;&lt; inst<span class="Delimiter">.</span>products<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">).</span>name &lt;&lt; <span class="Constant">&quot; is &quot;</span> &lt;&lt; names_to_string<span class="Delimiter">(</span>inst<span class="Delimiter">.</span>products<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">).</span>type<span class="Delimiter">)</span> &lt;&lt; end<span class="Delimiter">();</span>
    <span class="Delimiter">}</span>
  <span class="Delimiter">}</span>
<span class="Delimiter">}</span>

<span class="Comment">//: One final convenience: no need to say what to return if the information is</span>
<span class="Comment">//: in the header.</span>

<span class="Delimiter">:(scenario return_based_on_header)</span>
def main [
  <span class="Constant">1</span>:num/<span class="Special">raw &lt;- </span>add2 <span class="Constant">3</span><span class="Delimiter">,</span> <span class="Constant">5</span>
]
def add2 x:num<span class="Delimiter">,</span> y:num <span class="Delimiter">-&gt;</span> z:num [
  local-scope
  load-ingredients
  z<span class="Special"> &lt;- </span>add x<span class="Delimiter">,</span> y
  <span class="Identifier">return</span>
]
<span class="traceContains">+mem: storing 8 in location 1</span>

<span class="Delimiter">:(after &quot;Transform.push_back(check_header_ingredients)&quot;)</span>
Transform<span class="Delimiter">.</span>push_back<span class="Delimiter">(</span>fill_in_reply_ingredients<span class="Delimiter">);</span>  <span class="Comment">// idempotent</span>

<span class="Delimiter">:(code)</span>
<span class="Normal">void</span> fill_in_reply_ingredients<span class="Delimiter">(</span>recipe_ordinal r<span class="Delimiter">)</span> <span class="Delimiter">{</span>
  recipe&amp; caller_recipe = get<span class="Delimiter">(</span>Recipe<span class="Delimiter">,</span> r<span class="Delimiter">);</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>!caller_recipe<span class="Delimiter">.</span>has_header<span class="Delimiter">)</span> <span class="Identifier">return</span><span class="Delimiter">;</span>
  trace<span class="Delimiter">(</span><span class="Constant">9991</span><span class="Delimiter">,</span> <span class="Constant">&quot;transform&quot;</span><span class="Delimiter">)</span> &lt;&lt; <span class="Constant">&quot;--- fill in reply ingredients from header for recipe &quot;</span> &lt;&lt; caller_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>caller_recipe<span class="Delimiter">.</span>steps<span class="Delimiter">);</span> ++i<span class="Delimiter">)</span> <span class="Delimiter">{</span>
    instruction&amp; inst = caller_recipe<span class="Delimiter">.</span>steps<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">);</span>
    <span class="Normal">if</span> <span class="Delimiter">(</span>inst<span class="Delimiter">.</span>name == <span class="Constant">&quot;reply&quot;</span> || inst<span class="Delimiter">.</span>name == <span class="Constant">&quot;return&quot;</span><span class="Delimiter">)</span>
      add_header_products<span class="Delimiter">(</span>inst<span class="Delimiter">,</span> caller_recipe<span class="Delimiter">);</span>
  <span class="Delimiter">}</span>
  <span class="Comment">// fall through reply</span>
  <span class="Normal">const</span> instruction&amp; final_instruction = caller_recipe<span class="Delimiter">.</span>steps<span class="Delimiter">.</span>at<span class="Delimiter">(</span>SIZE<span class="Delimiter">(</span>caller_recipe<span class="Delimiter">.</span>steps<span class="Delimiter">)</span>-<span class="Constant">1</span><span class="Delimiter">);</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>final_instruction<span class="Delimiter">.</span>name != <span class="Constant">&quot;reply&quot;</span> &amp;&amp; final_instruction<span class="Delimiter">.</span>name != <span class="Constant">&quot;return&quot;</span><span class="Delimiter">)</span> <span class="Delimiter">{</span>
    instruction inst<span class="Delimiter">;</span>
    inst<span class="Delimiter">.</span>name = <span class="Constant">&quot;reply&quot;</span><span class="Delimiter">;</span>
    add_header_products<span class="Delimiter">(</span>inst<span class="Delimiter">,</span> caller_recipe<span class="Delimiter">);</span>
    caller_recipe<span class="Delimiter">.</span>steps<span class="Delimiter">.</span>push_back<span class="Delimiter">(</span>inst<span class="Delimiter">);</span>
  <span class="Delimiter">}</span>
<span class="Delimiter">}</span>

<span class="Normal">void</span> add_header_products<span class="Delimiter">(</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>
  assert<span class="Delimiter">(</span>inst<span class="Delimiter">.</span>name == <span class="Constant">&quot;reply&quot;</span> || inst<span class="Delimiter">.</span>name == <span class="Constant">&quot;return&quot;</span><span class="Delimiter">);</span>
  <span class="Comment">// collect any products with the same names as ingredients</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_recipe<span class="Delimiter">.</span>products<span class="Delimiter">);</span> ++i<span class="Delimiter">)</span> <span class="Delimiter">{</span>
    <span class="Comment">// if the ingredient is missing, add it from the header</span>
    <span class="Normal">if</span> <span class="Delimiter">(</span>SIZE<span class="Delimiter">(</span>inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">)</span> == i<span class="Delimiter">)</span>
      inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>push_back<span class="Delimiter">(</span>caller_recipe<span class="Delimiter">.</span>products<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">));</span>
    <span class="Comment">// if it's missing /same_as_ingredient, try to fill it in</span>
    <span class="Normal">if</span> <span class="Delimiter">(</span>contains_key<span class="Delimiter">(</span>caller_recipe<span class="Delimiter">.</span>ingredient_index<span class="Delimiter">,</span> caller_recipe<span class="Delimiter">.</span>products<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">).</span>name<span class="Delimiter">)</span> &amp;&amp; !has_property<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="Constant">&quot;same_as_ingredient&quot;</span><span class="Delimiter">))</span> <span class="Delimiter">{</span>
      ostringstream same_as_ingredient<span class="Delimiter">;</span>
      same_as_ingredient &lt;&lt; get<span class="Delimiter">(</span>caller_recipe<span class="Delimiter">.</span>ingredient_index<span class="Delimiter">,</span> caller_recipe<span class="Delimiter">.</span>products<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">).</span>name<span class="Delimiter">);</span>
      inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">).</span>properties<span class="Delimiter">.</span>push_back<span class="Delimiter">(</span>pair&lt;string<span class="Delimiter">,</span> string_tree*&gt;<span class="Delimiter">(</span><span class="Constant">&quot;same-as-ingredient&quot;</span><span class="Delimiter">,</span> <span class="Normal">new</span> string_tree<span class="Delimiter">(</span>same_as_ingredient<span class="Delimiter">.</span>str<span class="Delimiter">())));</span>
    <span class="Delimiter">}</span>
  <span class="Delimiter">}</span>
<span class="Delimiter">}</span>

<span class="Delimiter">:(scenario explicit_reply_ignores_header)</span>
def main [
  <span class="Constant">1</span>:num/<span class="Special">raw</span><span class="Delimiter">,</span> <span class="Constant">2</span>:num/<span class="Special">raw &lt;- </span>add2 <span class="Constant">3</span><span class="Delimiter">,</span> <span class="Constant">5</span>
]
def add2 a:num<span class="Delimiter">,</span> b:num <span class="Delimiter">-&gt;</span> y:num<span class="Delimiter">,</span> z:num [
  local-scope
  load-ingredients
  y<span class="Special"> &lt;- </span>add a<span class="Delimiter">,</span> b
  z<span class="Special"> &lt;- </span>subtract a<span class="Delimiter">,</span> b
  <span class="Identifier">return</span> a<span class="Delimiter">,</span> z
]
<span class="traceContains">+mem: storing 3 in location 1</span>
<span class="traceContains">+mem: storing -2 in location 2</span>

<span class="Delimiter">:(scenario return_on_fallthrough_based_on_header)</span>
def main [
  <span class="Constant">1</span>:num/<span class="Special">raw &lt;- </span>add2 <span class="Constant">3</span><span class="Delimiter">,</span> <span class="Constant">5</span>
]
def add2 x:num<span class="Delimiter">,</span> y:num <span class="Delimiter">-&gt;</span> z:num [
  local-scope
  load-ingredients
  z<span class="Special"> &lt;- </span>add x<span class="Delimiter">,</span> y
]
<span class="traceContains">+transform: instruction: reply {z: &quot;number&quot;}</span>
<span class="traceContains">+mem: storing 8 in location 1</span>

<span class="Delimiter">:(scenario return_on_fallthrough_already_exists)</span>
def main [
  <span class="Constant">1</span>:num/<span class="Special">raw &lt;- </span>add2 <span class="Constant">3</span><span class="Delimiter">,</span> <span class="Constant">5</span>
]
def add2 x:num<span class="Delimiter">,</span> y:num <span class="Delimiter">-&gt;</span> z:num [
  local-scope
  load-ingredients
  z<span class="Special"> &lt;- </span>add x<span class="Delimiter">,</span> y  <span class="Comment"># no type for z</span>
  <span class="Identifier">return</span> z
]
<span class="traceContains">+transform: instruction: return {z: ()}</span>
<span class="traceAbsent">-transform: instruction: reply z:num</span>
<span class="traceContains">+mem: storing 8 in location 1</span>

<span class="Delimiter">:(scenario return_after_conditional_reply_based_on_header)</span>
def main [
  <span class="Constant">1</span>:num/<span class="Special">raw &lt;- </span>add2 <span class="Constant">3</span><span class="Delimiter">,</span> <span class="Constant">5</span>
]
def add2 x:num<span class="Delimiter">,</span> y:num <span class="Delimiter">-&gt;</span> z:num [
  local-scope
  load-ingredients
  z<span class="Special"> &lt;- </span>add x<span class="Delimiter">,</span> y  <span class="Comment"># no type for z</span>
  <span class="Identifier">return</span>-<span class="Normal">if</span> <span class="Constant">0</span>/<span class="Constant">false</span><span class="Delimiter">,</span> <span class="Constant">34</span>
]
<span class="traceContains">+mem: storing 8 in location 1</span>

<span class="Delimiter">:(scenario recipe_headers_perform_same_ingredient_check)</span>
<span class="Special">% Hide_errors = true;</span>
def 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>copy <span class="Constant">34</span>
  <span class="Constant">3</span>:num<span class="Special"> &lt;- </span>add2 <span class="Constant">1</span>:num<span class="Delimiter">,</span> <span class="Constant">2</span>:num
]
def add2 x:num<span class="Delimiter">,</span> y:num <span class="Delimiter">-&gt;</span> x:num [
  local-scope
  load-ingredients
]
<span class="traceContains">+error: main: '3:num &lt;- add2 1:num, 2:num' should write to '1:num' rather than '3:num'</span>

<span class="Delimiter">:(before &quot;End Includes&quot;)</span>
<span class="Normal">using</span> std::min<span class="Delimiter">;</span>
<span class="Normal">using</span> std::max<span class="Delimiter">;</span>
</pre>
</body>
</html>
<!-- vim: set foldmethod=manual : -->