This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| template<typename Out, typename Algebra> | |
| Out cata(Algebra f, expression const& ast) | |
| { | |
| return f( | |
| fmap( | |
| [f](expression const& e) -> Out { return cata<Out>(f, e); }, | |
| ast.get())); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| template<typename Tag> | |
| std::string print_op(op<Tag, std::string> const& e, std::string const& op_repr) | |
| { | |
| return std::string("(") + op_repr + " " + boost::algorithm::join(e.rands(), " ") + ")"; | |
| } | |
| std::string print_alg(expression_r<std::string> const& e) | |
| { | |
| if (auto* o = get_as_add(e)) return print_op(*o, "+"); | |
| if (auto* o = get_as_mul(e)) return print_op(*o, "*"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| expression e = add({ | |
| cst(1), | |
| cst(2), | |
| mul({cst(0), var("x"), var("y")}), | |
| mul({cst(1), var("y"), cst(2)}), | |
| add({cst(0), var("x")}) | |
| }); | |
| std::cout << cata<std::string>(print_alg, e) << '\n'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using env = std::map<id, nb>; | |
| auto eval_alg(env const& env) | |
| { | |
| return [&env] (expression_r<int> const& e) | |
| { | |
| if (auto* o = get_as_add(e)) | |
| return boost::accumulate(o->rands(), 0, std::plus<int>()); | |
| if (auto* o = get_as_mul(e)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| template<typename Tag> | |
| std::set<id> join_sets(op<Tag, std::set<id>> const& op) | |
| { | |
| std::set<id> out; | |
| for (auto r: op.rands()) | |
| out.insert(r.begin(), r.end()); | |
| return out; | |
| } | |
| std::set<id> dependencies_alg(expression_r<std::set<id>> const& e) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| expression opt_add_alg(expression_r<expression> const& e) | |
| { | |
| if (auto* op = get_as_add(e)) | |
| return optimize_op(*op, 0, std::plus<int>()); | |
| return e; | |
| } | |
| expression opt_mul_alg(expression_r<expression> const& e) | |
| { | |
| if (auto* op = get_as_mul(e)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| template<typename Tag, typename Step> | |
| expression optimize_op(op<Tag, expression> const& e, int neutral, Step step) | |
| { | |
| int res = neutral; | |
| std::vector<expression> subs; | |
| for (expression const& sub: e.rands()) | |
| { | |
| if (auto* i = get_as_cst(sub.get())) | |
| { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| auto partial_eval_alg(env const& env) | |
| { | |
| return [&env] (expression_r<expression> const& e) -> expression | |
| { | |
| if (auto* v = get_as_var(e)) | |
| { | |
| auto it = env.find(*v); | |
| if (it != env.end()) return cst(it->second); | |
| return var(*v); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| int eval_2(env const& env, expression const& e) | |
| { | |
| auto reduced = partial_eval(env, e); | |
| if (auto* i = get_as_cst(reduced.get()))return *i; | |
| throw_missing_variables(dependencies(reduced)); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** Catamorphisms */ | |
| #include <algorithm> | |
| #include <boost/algorithm/string/join.hpp> | |
| #include <boost/range/algorithm.hpp> | |
| #include <boost/range/adaptors.hpp> | |
| #include <boost/range/numeric.hpp> | |
| #include <boost/variant.hpp> | |
| #include <cassert> | |
| #include <functional> |