about summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--011load.cc2
-rw-r--r--057static_dispatch.cc164
-rw-r--r--static_dispatch.mu21
3 files changed, 186 insertions, 1 deletions
diff --git a/011load.cc b/011load.cc
index d848fd55..36b45214 100644
--- a/011load.cc
+++ b/011load.cc
@@ -53,7 +53,7 @@ long long int slurp_recipe(istream& in) {
   if (Recipe.find(Recipe_ordinal[result.name]) != Recipe.end()) {
     trace(9991, "parse") << "already exists" << end();
     if (warn_on_redefine(result.name))
-      raise << "redefining recipe " << Recipe[Recipe_ordinal[result.name]].name << "\n" << end();
+      raise << "redefining recipe " << result.name << "\n" << end();
     Recipe.erase(Recipe_ordinal[result.name]);
   }
   slurp_body(in, result);
diff --git a/057static_dispatch.cc b/057static_dispatch.cc
new file mode 100644
index 00000000..fa77cbde
--- /dev/null
+++ b/057static_dispatch.cc
@@ -0,0 +1,164 @@
+//: Transform to maintain multiple variants of a recipe depending on the
+//: number and types of the ingredients and products. Allows us to use nice
+//: names like 'print' or 'length' in many mutually extensible ways.
+
+:(scenario static_dispatch)
+recipe main [
+  7:number/raw <- test 3
+]
+recipe test a:number -> z:number [
+  z <- copy 1
+]
+recipe test a:number, b:number -> z:number [
+  z <- copy 2
+]
++mem: storing 1 in location 7
+
+//: When loading recipes, accumulate variants if headers don't collide, and
+//: raise a warning if headers collide.
+
+:(before "End Globals")
+map<string, vector<recipe_ordinal> > Recipe_variants;
+
+:(before "End Load Recipe Header(result)")
+if (Recipe_ordinal.find(result.name) != Recipe_ordinal.end()
+    && (Recipe.find(Recipe_ordinal[result.name]) == Recipe.end()
+        || Recipe[Recipe_ordinal[result.name]].has_header)
+    && !header_already_exists(result)) {
+  string new_name = next_unused_recipe_name(result.name);
+  Recipe_ordinal[new_name] = Next_recipe_ordinal++;
+  Recipe_variants[result.name].push_back(Recipe_ordinal[new_name]);
+  result.name = new_name;
+}
+
+:(code)
+bool header_already_exists(const recipe& rr) {
+  const vector<recipe_ordinal>& variants = Recipe_variants[rr.name];
+  for (long long int i = 0; i < SIZE(variants); ++i) {
+    if (Recipe.find(variants.at(i)) != Recipe.end()
+        && all_reagents_match(rr, Recipe[variants.at(i)])) {
+      return true;
+    }
+  }
+  return false;
+}
+
+bool all_reagents_match(const recipe& r1, const recipe& r2) {
+  if (SIZE(r1.ingredients) != SIZE(r2.ingredients)) return false;
+  if (SIZE(r1.products) != SIZE(r2.products)) return false;
+  for (long long int i = 0; i < SIZE(r1.ingredients); ++i) {
+    if (!exact_match(r1.ingredients.at(i).type, r2.ingredients.at(i).type))
+      return false;
+  }
+  for (long long int i = 0; i < SIZE(r1.products); ++i) {
+    if (!exact_match(r1.products.at(i).type, r2.products.at(i).type))
+      return false;
+  }
+  return true;
+}
+
+bool exact_match(type_tree* a, type_tree* b) {
+  if (a == b) return true;
+  return a->value == b->value
+      && exact_match(a->left, b->left)
+      && exact_match(a->right, b->right);
+}
+
+string next_unused_recipe_name(const string& recipe_name) {
+  for (long long int i = 2; ; ++i) {
+    ostringstream out;
+    out << recipe_name << '_' << i;
+    if (Recipe_ordinal.find(out.str()) == Recipe_ordinal.end()) {
+      return out.str();
+    }
+  }
+}
+
+//: Once all the recipes are loaded, transform their bodies to replace each
+//: call with the most suitable variant.
+
+:(scenario static_dispatch_picks_most_similar_variant)
+recipe main [
+  7:number/raw <- test 3, 4, 5
+]
+recipe test a:number -> z:number [
+  z <- copy 1
+]
+recipe test a:number, b:number -> z:number [
+  z <- copy 2
+]
++mem: storing 2 in location 7
+
+:(after "int main")
+  Transform.push_back(resolve_ambiguous_calls);
+
+:(code)
+void resolve_ambiguous_calls(recipe_ordinal r) {
+  trace(9991, "transform") << "--- resolve ambiguous calls for recipe " << Recipe[r].name << end();
+  for (long long int index = 0; index < SIZE(Recipe[r].steps); ++index) {
+    instruction& inst = Recipe[r].steps.at(index);
+    if (inst.is_label) continue;
+    if (Recipe_variants.find(inst.name) == Recipe_variants.end()) continue;
+    replace_best_variant(inst);
+  }
+}
+
+void replace_best_variant(instruction& inst) {
+  vector<recipe_ordinal>& variants = Recipe_variants[inst.name];
+  long long int best_score = variant_score(inst, Recipe_ordinal[inst.name]);
+  for (long long int i = 0; i < SIZE(variants); ++i) {
+    long long int current_score = variant_score(inst, variants.at(i));
+    trace(9992, "transform") << "checking variant " << i << ": " << current_score << end();
+    if (current_score > best_score) {
+      inst.name = Recipe[variants.at(i)].name;
+      best_score = current_score;
+    }
+  }
+}
+
+long long int variant_score(const instruction& inst, recipe_ordinal variant) {
+  const vector<reagent>& header_ingredients = Recipe[variant].ingredients;
+  if (SIZE(inst.ingredients) < SIZE(header_ingredients)) {
+    trace(9993, "transform") << "too few ingredients" << end();
+    return -1;
+  }
+  for (long long int i = 0; i < SIZE(header_ingredients); ++i) {
+    if (!types_match(header_ingredients.at(i), inst.ingredients.at(i))) {
+      trace(9993, "transform") << "mismatch: ingredient " << i << end();
+      return -1;
+    }
+  }
+  if (SIZE(inst.products) > SIZE(Recipe[variant].products)) {
+    trace(9993, "transform") << "too few products" << end();
+    return -1;
+  }
+  const vector<reagent>& header_products = Recipe[variant].products;
+  for (long long int i = 0; i < SIZE(inst.products); ++i) {
+    if (!types_match(header_products.at(i), inst.products.at(i))) {
+      trace(9993, "transform") << "mismatch: product " << i << end();
+      return -1;
+    }
+  }
+  return 100 - (SIZE(Recipe[variant].products)-SIZE(inst.products))
+             - (SIZE(inst.ingredients)-SIZE(Recipe[variant].ingredients));  // ok to go negative
+}
+
+:(scenario static_dispatch_disabled_on_headerless_definition)
+% Hide_warnings = true;
+recipe test a:number -> z:number [
+  z <- copy 1
+]
+recipe test [
+  z:number <- copy 1
+]
++warn: redefining recipe test
+
+:(scenario static_dispatch_disabled_on_headerless_definition_2)
+% Hide_warnings = true;
+recipe test [
+  z:number <- copy 1
+]
+recipe test a:number -> z:number [
+  z <- copy 1
+]
++warn: redefining recipe test
diff --git a/static_dispatch.mu b/static_dispatch.mu
new file mode 100644
index 00000000..00160a2a
--- /dev/null
+++ b/static_dispatch.mu
@@ -0,0 +1,21 @@
+recipe test a:number -> b:number [
+  local-scope
+  load-ingredients
+  b <- add a, 1
+]
+
+recipe test a:number, b:number -> c:number [
+  local-scope
+  load-ingredients
+  c <- add a, b
+]
+
+recipe main [
+  local-scope
+  a:number <- test 3  # selects single-ingredient version
+  $print a, 10/newline
+  b:number <- test 3, 4  # selects double-ingredient version
+  $print b, 10/newline
+  c:number <- test 3, 4, 5  # prefers double- to single-ingredient version
+  $print c, 10/newline
+]
id=8fa32599bb288f9e51ebcf6f7c00339692c2a5b5'>8fa32599 ^
6070c23e ^
2104d1a7 ^
8fa32599 ^
2104d1a7 ^
8fa32599 ^
2104d1a7 ^
9a777801 ^
2104d1a7 ^

8fa32599 ^
9a777801 ^
8fa32599 ^
6070c23e ^
2104d1a7 ^
8fa32599 ^
2104d1a7 ^
8fa32599 ^


6070c23e ^
8fa32599 ^



























2104d1a7 ^
8fa32599 ^

2104d1a7 ^
8fa32599 ^

9a777801 ^
ec73ed12 ^
3350c34a ^
9a777801 ^
















ec73ed12 ^
3350c34a ^
9a777801 ^





















ec73ed12 ^
3350c34a ^
9a777801 ^





















ec73ed12 ^
3350c34a ^
9a777801 ^




3d1c4216 ^
9a777801 ^











































3d1c4216 ^
9a777801 ^

3d1c4216 ^
9a777801 ^
3350c34a ^
9a777801 ^


3d1c4216 ^
9a777801 ^
3350c34a ^
9a777801 ^


3d1c4216 ^

5396e24c ^
3d1c4216 ^
9a777801 ^






5396e24c ^
9a777801 ^
3d1c4216 ^
9a777801 ^

3d1c4216 ^
9a777801 ^
3350c34a ^
9a777801 ^


3d1c4216 ^
9a777801 ^
3350c34a ^
9a777801 ^

d3a9db3a ^
9a777801 ^





3d1c4216 ^

9a777801 ^




























3d1c4216 ^
5396e24c ^
ec73ed12 ^
3350c34a ^
9a777801 ^














3d1c4216 ^
5396e24c ^
3350c34a ^
9a777801 ^














3350c34a ^
5396e24c ^
3350c34a ^
9a777801 ^



















3d1c4216 ^
5396e24c ^
ec73ed12 ^
3350c34a ^
9a777801 ^




3d1c4216 ^


























5396e24c ^
3d1c4216 ^

3350c34a ^
3d1c4216 ^
























3350c34a ^








8fa32599 ^



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