mu/057static_dispatch.cc

188 lines
6.6 KiB
C++
Raw Normal View History

2015-10-30 04:23:48 +00:00
//: 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 One-time Setup")
put(Recipe_variants, "main", vector<recipe_ordinal>()); // since we manually added main to Recipe_ordinal
:(before "End Setup")
for (map<string, vector<recipe_ordinal> >::iterator p = Recipe_variants.begin(); p != Recipe_variants.end(); ++p) {
for (long long int i = 0; i < SIZE(p->second); ++i) {
if (p->second.at(i) >= Reserved_for_tests)
p->second.at(i) = -1; // just leave a ghost
}
}
2015-10-30 04:23:48 +00:00
:(before "End Load Recipe Header(result)")
if (contains_key(Recipe_ordinal, result.name)) {
const recipe_ordinal r = get(Recipe_ordinal, result.name);
if ((!contains_key(Recipe, r) || get(Recipe, r).has_header)
&& !header_already_exists(result)) {
string new_name = next_unused_recipe_name(result.name);
put(Recipe_ordinal, new_name, Next_recipe_ordinal++);
get(Recipe_variants, result.name).push_back(get(Recipe_ordinal, new_name));
result.name = new_name;
}
}
else {
// save first variant
put(Recipe_ordinal, result.name, Next_recipe_ordinal++);
get_or_insert(Recipe_variants, result.name).push_back(get(Recipe_ordinal, result.name));
2015-10-30 04:23:48 +00:00
}
:(code)
bool header_already_exists(const recipe& rr) {
const vector<recipe_ordinal>& variants = get(Recipe_variants, rr.name);
2015-10-30 04:23:48 +00:00
for (long long int i = 0; i < SIZE(variants); ++i) {
if (Recipe.find(variants.at(i)) != Recipe.end()
&& all_reagents_match(rr, get(Recipe, variants.at(i)))) {
2015-10-30 04:23:48 +00:00
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())
2015-10-30 04:23:48 +00:00
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
2015-11-05 07:53:11 +00:00
//: after insert_fragments (tangle) and before computing operation ids
//: after filling in all missing types (because we'll be introducing 'blank' types in this transform in a later layer, for shape-shifting recipes)
:(after "Transform.push_back(deduce_types_from_header)")
Transform.push_back(resolve_ambiguous_calls); // idempotent
2015-10-30 04:23:48 +00:00
:(code)
void resolve_ambiguous_calls(recipe_ordinal r) {
recipe& caller_recipe = get(Recipe, r);
if (!caller_recipe.has_header) return;
trace(9991, "transform") << "--- resolve ambiguous calls for recipe " << caller_recipe.name << end();
for (long long int index = 0; index < SIZE(caller_recipe.steps); ++index) {
instruction& inst = caller_recipe.steps.at(index);
2015-10-30 04:23:48 +00:00
if (inst.is_label) continue;
if (!contains_key(Recipe_variants, inst.name)) continue;
assert(!get(Recipe_variants, inst.name).empty());
replace_best_variant(inst, caller_recipe);
2015-10-30 04:23:48 +00:00
}
}
void replace_best_variant(instruction& inst, const recipe& caller_recipe) {
trace(9992, "transform") << "instruction " << inst.name << end();
vector<recipe_ordinal>& variants = get(Recipe_variants, inst.name);
long long int best_score = variant_score(inst, get(Recipe_ordinal, inst.name));
2015-10-30 04:23:48 +00:00
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 = get(Recipe, variants.at(i)).name;
2015-10-30 04:23:48 +00:00
best_score = current_score;
}
}
// End Instruction Dispatch(inst, best_score)
2015-10-30 04:23:48 +00:00
}
long long int variant_score(const instruction& inst, recipe_ordinal variant) {
if (variant == -1) return -1; // ghost from a previous test
const vector<reagent>& header_ingredients = get(Recipe, variant).ingredients;
2015-10-30 04:23:48 +00:00
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(get(Recipe, variant).products)) {
2015-10-30 04:23:48 +00:00
trace(9993, "transform") << "too few products" << end();
return -1;
}
const vector<reagent>& header_products = get(Recipe, variant).products;
2015-10-30 04:23:48 +00:00
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;
}
}
// the greater the number of unused ingredients, the lower the score
return 100 - (SIZE(get(Recipe, variant).products)-SIZE(inst.products))
- (SIZE(inst.ingredients)-SIZE(get(Recipe, variant).ingredients)); // ok to go negative
2015-10-30 04:23:48 +00:00
}
:(scenario static_dispatch_disabled_on_headerless_definition)
% Hide_warnings = true;
recipe test a:number -> z:number [
z <- copy 1
]
recipe test [
2015-11-01 04:56:17 +00:00
reply 34
2015-10-30 04:23:48 +00:00
]
+warn: redefining recipe test
:(scenario static_dispatch_disabled_on_headerless_definition_2)
% Hide_warnings = true;
recipe test [
2015-11-01 04:56:17 +00:00
reply 34
2015-10-30 04:23:48 +00:00
]
recipe test a:number -> z:number [
z <- copy 1
]
+warn: redefining recipe test